/* * IO_Test.c * * Created on: 2020¦~4¤ë21¤é * Author: Wendell */ #include "IO_Test.h" #include #include #include //write, close, usleep, read #include //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"); }