diff --git a/demo/matrix-uart/Makefile b/demo/matrix-uart/Makefile new file mode 100644 index 0000000..3589481 --- /dev/null +++ b/demo/matrix-uart/Makefile @@ -0,0 +1,11 @@ +CC = gcc + +TARGET = commtest +SRC = commtest.c + +$(TARGET):$(SRC) + $(CC) -o $@ $^ -REENTRANT -Wall -lfahw -lm -lpthread + +.PHONY: clean +clean: + rm -f $(TARGET) diff --git a/demo/matrix-uart/commtest b/demo/matrix-uart/commtest new file mode 100644 index 0000000..30a4225 Binary files /dev/null and b/demo/matrix-uart/commtest differ diff --git a/demo/matrix-uart/commtest.c b/demo/matrix-uart/commtest.c new file mode 100644 index 0000000..5d7e274 --- /dev/null +++ b/demo/matrix-uart/commtest.c @@ -0,0 +1,271 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FALSE 0 +#define TRUE 1 + +#define PRINTF oprintf + +#include + +int speed_arr[] = {B115200, B576000, B38400, B19200, B9600, B4800, B2400, B1200, B300, + B38400, B19200, B9600, B4800, B2400, B1200, B300, }; +int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300, + 38400, 19200, 9600, 4800, 2400, 1200, 300, }; + +void set_speed(int fd, int speed) +{ + int i; + int status; + struct termios Opt; + tcgetattr(fd, &Opt); + for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) + { + if (speed == name_arr[i]) + { + tcflush(fd, TCIOFLUSH); + cfsetispeed(&Opt, speed_arr[i]); + cfsetospeed(&Opt, speed_arr[i]); + status = tcsetattr(fd, TCSANOW, &Opt); + if (status != 0) + perror("tcsetattr fd1"); + return; + } + tcflush(fd,TCIOFLUSH); + } +} + +int set_Parity(int fd,int databits,int stopbits,int parity) +{ + struct termios options; + if ( tcgetattr( fd,&options) != 0) + { + perror("SetupSerial 1"); + return(FALSE); + } + options.c_cflag &= ~CSIZE; + switch (databits) + { + case 7: + options.c_cflag |= CS7; + break; + case 8: + options.c_cflag |= CS8; + break; + default: + fprintf(stderr,"Unsupported data size\n"); + return (FALSE); + } + switch (parity) + { + case 'n': + case 'N': + options.c_cflag &= ~PARENB; /* Clear parity enable */ + options.c_iflag &= ~INPCK; /* Enable parity checking */ + break; + case 'o': + case 'O': + options.c_cflag |= (PARODD | PARENB); + options.c_iflag |= INPCK; + break; + case 'e': + case 'E': + options.c_cflag |= PARENB; /* Enable parity */ + options.c_cflag &= ~PARODD; + options.c_iflag |= INPCK; + break; + case 'S': + case 's': /*as no parity*/ + options.c_cflag &= ~PARENB; + options.c_cflag &= ~CSTOPB; + break; + default: + fprintf(stderr,"Unsupported parity\n"); + return (FALSE); + } + switch (stopbits) + { + case 1: + options.c_cflag &= ~CSTOPB; + break; + case 2: + options.c_cflag |= CSTOPB; + break; + default: + fprintf(stderr,"Unsupported stop bits\n"); + return (FALSE); + } + /* Set input parity option */ + if (parity != 'n') + options.c_iflag |= INPCK; + options.c_cc[VTIME] = 150; // 15 seconds + options.c_cc[VMIN] = 0; + + options.c_lflag = 0; + + tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */ + if (tcsetattr(fd,TCSANOW,&options) != 0) + { + perror("SetupSerial 3"); + return (FALSE); + } + return (TRUE); +} + +int OpenDev(char *Dev) +{ + int fd = open( Dev, O_RDWR | O_NDELAY | O_NOCTTY); //| O_NOCTTY | O_NDELAY + if (-1 == fd) + { + perror("Can't Open Serial Port"); + return -1; + } + else + return fd; + +} + +int SerialOpen(char* dev, int baud, char* opt) +{ + int fd; + int bits; + int stopbits; + char parity; + fd = OpenDev(dev); + if (fd>0) + set_speed(fd,baud); + else + { + printf("Can't Open Serial Port:%s!\n",dev); + exit(0); + } + parity = opt[0]; + bits = opt[1] - '0'; + stopbits = opt[2] - '0'; + + switch(parity){ + case 'N': + case 'O': + case 'E': + case 'n': + case 'o': + case 'e': + case 'S': + case 's': + break; + default: + printf("Parity false\n"); + exit(1); + } + + if((stopbits != 1) && (stopbits != 2)){ + printf("Stopbits false\n"); + exit(1); + } + + if((bits != 7) && (bits != 8)){ + printf("Bits false\n"); + exit(1); + } + + if (set_Parity(fd,8,1,'N')== FALSE) + { + printf("Set Parity Error\n"); + exit(1); + } + return fd; +} + +#define RX_BUF_SIZE 512 +char rx_buf[RX_BUF_SIZE]; +char now_time[8]; +char time_fre = 0; +void *com_rx (void *arg) +{ + int serialfd = (int)arg; + int nread = 0; + int nback = 0; + int timer = 0; + int status = 0; + while(1){ + nread = read(serialfd, rx_buf + nback, RX_BUF_SIZE - nback); + if(nread > 0){ + nback += nread; + } + + if(nread > 0){ + timer = 0; + status = 1; + } + else if(status == 1){ + timer++; + } + + if(status && (timer > 10)){ + rx_buf[nback] = '\0'; + char *p = strstr(rx_buf,"$GNRMC,"); + if(p){ + p += 7; + unsigned char h = (p[0] - 48) * 10 + p[1] - 40; + now_time[0] = h/10 + 48; + now_time[1] = h%10 + 48; + now_time[2] = '-'; + now_time[3] = p[2]; + now_time[4] = p[3]; + now_time[5] = '-'; + now_time[6] = p[4]; + now_time[7] = p[5]; + now_time[8] = '\0'; + time_fre = 1; + } + status = 0; + timer = 0; + memset(rx_buf,'\0',nback); + nback = 0; + } + usleep(1000); + } +} + +struct oled_cursor{ + int x; + int y; +}; + +extern struct oled_cursor __oled_cursor; + +int main (void) +{ + pthread_t rx_thread; + boardInit(); + int SerialFD = SerialOpen("/dev/ttyS1",115200,"N81"); + OLEDInit(7,11); + OLEDCleanScreen(); + if(SerialFD > 0){ + PRINTF("Serial Opened\n"); + }else{ + PRINTF("Serial Open failed\n"); + + } + + pthread_create(&rx_thread,NULL,com_rx,(void*)SerialFD); + + while(1){ + if(time_fre){ + time_fre = 0; + __oled_cursor.x = 0; + __oled_cursor.y = 1; + PRINTF("Time:%s",now_time); + } + } + + return 0; +} diff --git a/lib/includes/libfahw-oled.h b/lib/includes/libfahw-oled.h index 3ab8c54..324f998 100644 --- a/lib/includes/libfahw-oled.h +++ b/lib/includes/libfahw-oled.h @@ -4,11 +4,13 @@ #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #define OLED_WIDTH (132) #define OLED_HEIGHT (64) +#define CURSOR_X_MAX (OLED_WIDTH / 8 - 1) +#define CURSOR_Y_MAX (OLED_HEIGHT / 16 -1) int OLEDDisp8x16Str(int devFD, int x, int y, char ch[]); int OLEDDisp8x16Char(int devFD, int x, int y, char ch); -int OLEDCleanScreen(int devFD); +int OLEDCleanScreen(void); int OLEDInit(int resetPin, int cmdDatPin); void OLEDDeInit(int devFD); - +int oprintf(const char* fmt, ...); #endif diff --git a/lib/includes/libfahw.h b/lib/includes/libfahw.h index b07d9b8..801ecdb 100644 --- a/lib/includes/libfahw.h +++ b/lib/includes/libfahw.h @@ -1,7 +1,7 @@ #ifndef __FRIENDLYARM_HARDWARE_H__ #define __FRIENDLYARM_HARDWARE_H__ -#include "../common.h" +#include "common.h" #include "libfahw-filectl.h" #include "libfahw-gpio.h" #include "libfahw-i2c.h" diff --git a/lib/oled.c b/lib/oled.c index b0c9bbd..6dbccbe 100644 --- a/lib/oled.c +++ b/lib/oled.c @@ -6,6 +6,7 @@ #include "libfahw-gpio.h" #include "common.h" +static int oledFD; static int gCmdDatPin = -1; static int gResetPin = -1; static unsigned char spiMode = 0; @@ -256,10 +257,10 @@ EXPORT int OLEDInit(int cmdDatPin, int resetPin) ret = -1; } */ - if (OLEDSetPos(devFD, 0, 0) == -1) { - ret = -1; - } + ret = OLEDSetPos(devFD, 0, 0); + if (ret == 0) { + oledFD = devFD; return devFD; } else { unexportGPIOPin(resetPin); @@ -317,23 +318,68 @@ EXPORT int OLEDDisp8x16Str(int devFD, int x, int y, char ch[]) return 0; } -EXPORT int OLEDCleanScreen(int devFD) +EXPORT int OLEDCleanScreen(void) { int x,y; - unsigned char data = 0x00; int ret = 0; - for (y=0; y<8; y++) + for (y=0; y <= CURSOR_Y_MAX; y++) { - // set pos (y, 0) - OLEDWriteCmd(devFD, 0xb0+y); - OLEDWriteCmd(devFD, 0x00); - OLEDWriteCmd(devFD, 0x10); - for (x=0; x