123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /*
- * IO_Test.c
- *
- * Created on: 2020¦~4¤ë21¤é
- * Author: Wendell
- */
- #include "IO_Test.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h> //write, close, usleep, read
- #include <fcntl.h> //uart
- const int System_GPIO_Pin_Table[SYS_GPIO_NUM] =
- {
- PIN_AM_OK_FLAG, PIN_IO_BD1_1, PIN_IO_BD1_2, PIN_IO_BD2_1, PIN_IO_BD2_2, PIN_ID_BD1_1, PIN_ID_BD1_2, PIN_ID_BD2_1,
- PIN_ID_BD2_2, PIN_AM_RFID_RST, PIN_AM_RFID_ICC, PIN_BOARD1_PROXIMITY, PIN_BOARD2_PROXIMITY, PIN_AM_DE_1, PIN_AM_RE_1, PIN_ETHERNET_RESET
- };
- void gpio_export(int pin)
- {
- char buffer[64];
- snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/export", pin);
- system(buffer);
- }
- void gpio_unexport(int pin)
- {
- char buffer[64];
- snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/unexport", pin);
- system(buffer);
- }
- void gpio_set_direction(int pin, unsigned char dir)
- {
- /*
- char buffer[64];
- snprintf(buffer, sizeof(buffer), "echo %s > /sys/class/gpio/gpio%d/direction", dir == GPIO_DIR_INPUT ? "in" : "out", pin);
- system(buffer);
- */
- int fd;
- char buffer[64];
- snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/direction", pin);
- fd = open(buffer, O_WRONLY);
- if (fd < 0)
- {
- gpio_export(pin);
- fd = open(buffer, O_WRONLY);
- if(fd < 0)
- {
- printf("\r\nFailed to open gpio%d direction for writing!", pin);
- return;
- }
- }
- write(fd, dir == GPIO_DIR_INPUT ? "in" : "out", dir == GPIO_DIR_INPUT ? 2 : 3);
- close(fd);
- }
- void gpio_write(int pin, unsigned char value)
- {
- char buffer[64];
- snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/gpio%d/value", value > 0 ? 1 : 0, pin);
- system(buffer);
- }
- int gpio_read(int pin)
- {
- int fd, value = 0;
- char ch;
- char buffer[64];
- snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/value", pin);
- fd = open(buffer, O_RDONLY);
- if (fd < 0)
- {
- return -1;
- }
- if (read(fd, &ch, 4) < 0)
- {
- return -1;
- }
- value = atoi(&ch);
- close(fd);
- return value;
- }
- int adc_read(int adc_port)
- {
- int fd, value = 0;
- char ch[5];
- char buffer[64];
- snprintf(buffer,sizeof(buffer), "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw", adc_port);
- fd = open(buffer, O_RDONLY);
- if(fd < 0)
- {
- return -1;
- }
- if(read(fd, ch, 4) < 0)
- {
- return -1;
- }
- value = atoi(ch);
- close(fd);
- return value;
- }
- void InitIO(void)
- {
- /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
- gpio_set_direction(PIN_ID_BD1_1, GPIO_DIR_INPUT);
- /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
- gpio_set_direction(PIN_ID_BD1_2, GPIO_DIR_INPUT);
- /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
- gpio_set_direction(PIN_IO_BD1_1, GPIO_DIR_OUTPUT);
- gpio_write(PIN_IO_BD1_1, 0);
- /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
- gpio_set_direction(PIN_IO_BD1_2, GPIO_DIR_OUTPUT);
- gpio_write(PIN_IO_BD1_2, 0);
- /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
- gpio_set_direction(PIN_AM_RFID_RST, GPIO_DIR_OUTPUT);
- gpio_write(PIN_AM_RFID_RST, 0);
- /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
- gpio_set_direction(PIN_AM_RFID_ICC, GPIO_DIR_OUTPUT);
- gpio_write(PIN_AM_RFID_ICC, 0);
- /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
- gpio_set_direction(PIN_ID_BD2_1, GPIO_DIR_INPUT);
- /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
- gpio_set_direction(PIN_ID_BD2_2, GPIO_DIR_INPUT);
- /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
- gpio_set_direction(PIN_IO_BD2_1, GPIO_DIR_OUTPUT);
- gpio_write(PIN_IO_BD2_1, 0);
- /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
- gpio_set_direction(PIN_IO_BD2_2, GPIO_DIR_OUTPUT);
- gpio_write(PIN_IO_BD2_2, 0);
- /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
- gpio_set_direction(PIN_AM_OK_FLAG, GPIO_DIR_OUTPUT);
- gpio_write(PIN_AM_OK_FLAG, 0);
- gpio_set_direction(PIN_BOARD1_PROXIMITY, GPIO_DIR_INPUT);
- gpio_set_direction(PIN_BOARD2_PROXIMITY, GPIO_DIR_INPUT);
- }
- void DeInitIO(void)
- {
- gpio_unexport(PIN_ID_BD1_1);
- gpio_unexport(PIN_ID_BD1_2);
- gpio_unexport(PIN_IO_BD1_1);
- gpio_unexport(PIN_IO_BD1_2);
- gpio_unexport(PIN_AM_RFID_RST);
- gpio_unexport(PIN_AM_RFID_ICC);
- gpio_unexport(PIN_ID_BD2_1);
- gpio_unexport(PIN_ID_BD2_2);
- gpio_unexport(PIN_IO_BD2_1);
- gpio_unexport(PIN_IO_BD2_2);
- gpio_unexport(PIN_AM_OK_FLAG);
- }
- void DoIOTest(void)
- {
- InitIO();
- gpio_write(PIN_IO_BD1_1, 1);
- if(gpio_read(PIN_ID_BD1_1) == 1)
- {
- gpio_write(PIN_IO_BD1_1, 0);
- if(gpio_read(PIN_ID_BD1_1) == 0)
- {
- printf("\r\nID_BD1_1 Test OK");
- }
- else
- {
- printf("\r\nID_BD1_1 Low Test Fail");
- return;
- }
- }
- else
- {
- printf("\r\nID_BD1_1 High Test Fail");
- return;
- }
- gpio_write(PIN_IO_BD1_2, 1);
- if(gpio_read(PIN_ID_BD1_2) == 1)
- {
- gpio_write(PIN_IO_BD1_2, 0);
- if(gpio_read(PIN_ID_BD1_2) == 0)
- {
- printf("\r\nID_BD1_2 Test OK");
- }
- else
- {
- printf("\r\nID_BD1_2 Low Test Fail");
- return;
- }
- }
- else
- {
- printf("\r\nID_BD1_2 High Test Fail");
- return;
- }
- gpio_write(PIN_IO_BD2_1, 1);
- if(gpio_read(PIN_ID_BD2_1) == 1)
- {
- gpio_write(PIN_IO_BD2_1, 0);
- if(gpio_read(PIN_ID_BD2_1) == 0)
- {
- printf("\r\nID_BD2_1 Test OK");
- }
- else
- {
- printf("\r\nID_BD2_1 Low Test Fail");
- return;
- }
- }
- else
- {
- printf("\r\nID_BD2_1 High Test Fail");
- return;
- }
- gpio_write(PIN_IO_BD2_2, 1);
- if(gpio_read(PIN_ID_BD2_2) == 1)
- {
- gpio_write(PIN_IO_BD2_2, 0);
- if(gpio_read(PIN_ID_BD2_2) == 0)
- {
- printf("\r\nID_BD2_2 Test OK");
- }
- else
- {
- printf("\r\nID_BD2_2 Low Test Fail");
- return;
- }
- }
- else
- {
- printf("\r\nID_BD2_2 High Test Fail");
- return;
- }
- gpio_write(PIN_AM_RFID_RST, 1);
- if(gpio_read(PIN_BOARD1_PROXIMITY) == 1 && gpio_read(PIN_BOARD2_PROXIMITY) == 1)
- {
- gpio_write(PIN_AM_RFID_RST, 0);
- if(gpio_read(PIN_BOARD1_PROXIMITY) == 0 && gpio_read(PIN_BOARD2_PROXIMITY) == 0)
- {
- printf("\r\nBoard1 & Board2 Proximity Test OK");
- }
- else
- {
- printf("\r\nBoard1 & Board2 Proximity Low Test Fail");
- return;
- }
- }
- else
- {
- printf("\r\nBoard1 & Board2 Proximity High Test Fail");
- return;
- }
- gpio_write(PIN_AM_RFID_ICC, 1);
- usleep(100000);
- if(adc_read(ADC_AIN0) < 100 && adc_read(ADC_AIN1) < 100 && adc_read(ADC_AIN2) < 100 && adc_read(ADC_AIN3) < 100)
- {
- gpio_write(PIN_AM_RFID_ICC, 0);
- usleep(100000);
- if(adc_read(ADC_AIN0) > 4000 && adc_read(ADC_AIN1) > 4000 && adc_read(ADC_AIN2) > 4000 && adc_read(ADC_AIN3) > 4000)
- {
- printf("\r\nAIN0, AIN1, AIN2, AIN3 Test OK");
- }
- else
- {
- printf("\r\nAIN0, AIN1, AIN2, AIN3 High Test Fail");
- return;
- }
- }
- else
- {
- printf("\r\nAIN0, AIN1, AIN2, AIN3 Low Test Fail");
- return;
- }
- gpio_write(PIN_AM_OK_FLAG, 1);
- printf("\r\nIO Test Done!");
- printf("\r\nSuccess!\r\n");
- }
|