# HG changeset patch # User root@rika # Date 1240510924 -7200 # Node ID 4567e7bc404c65595d931c72993c0968dd1d8e3d # Parent 7fd00015f62f2933e78e70f859b8705e53e98ccf added bootloader command reworked input-handling diff -r 7fd00015f62f -r 4567e7bc404c tools/picctldisplaytest/src/picctldisplaytest.cpp --- a/tools/picctldisplaytest/src/picctldisplaytest.cpp Thu Apr 23 19:10:12 2009 +0200 +++ b/tools/picctldisplaytest/src/picctldisplaytest.cpp Thu Apr 23 20:22:04 2009 +0200 @@ -32,7 +32,6 @@ #include #include #include -#include #include #include "picctldisplaytest.h" @@ -41,12 +40,23 @@ runtime_flags myflags; int fd; +bool ack_flag; +uint8_t buf[255]; +uint8_t buf_pos; +uint8_t buf_cmd_start; +bool buf_flag_escape; + + int main(int argc, char *argv[]) { bool kill_flag = false; struct termios options; - struct sigaction saio; char key; + + buf_pos = 0; + ack_flag = false; + buf_cmd_start = 255; + buf_flag_escape = false; cout << "** picctldisplaytest - simple test utility for picctl lcd displays **" << endl << ">"; @@ -83,19 +93,6 @@ fd = open(myconfig.modem_device, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd <0) {perror(myconfig.modem_device); exit(-1); } - /* install the signal handler before making the device asynchronous */ - saio.sa_handler = signal_handler_IO; - sigemptyset (&saio.sa_mask); - saio.sa_flags = 0; - saio.sa_restorer = NULL; - sigaction(SIGIO,&saio,NULL); - // allow the process to receive SIGIO - fcntl(fd, F_SETOWN, getpid()); - // Make the file descriptor asynchronous - fcntl(fd, F_SETFL, FASYNC); - - tcgetattr(fd, &options); - cfsetispeed(&options, BAUDRATE); cfsetospeed(&options, BAUDRATE); @@ -131,6 +128,10 @@ cout << "Sending Clear Display..." << endl; send_data(CMD_DISP_CLEAR_SCREEN,(sizeof(CMD_DISP_CLEAR_SCREEN)/sizeof(char))); break; + case 'b': + cout << "Sending Bootload Start..." << endl; + send_data(CMD_BOOT,(sizeof(CMD_BOOT)/sizeof(char))); + break; case 'h': print_help(); break; @@ -314,7 +315,7 @@ default: break; } - sleep(1); + //sleep(1); if (key != 0x13 && key != '\n') cout << ">"; } close(fd); @@ -367,30 +368,60 @@ return true; } -/*! \brief sends size bytes of buffer data to the display - -\warning send_data doesn't check if buffer really is numbytes big. Expect all kind of weirdness if abused. -\param buffer pointer to a char buffer conatining the data -\param numbytes Number of bytes in buffer -*/ -bool send_data(const unsigned char * buffer, int numbytes) { - if (numbytes > 64 ) { - cout << "Buffer size exceeds 64 Bytes, send_data aborted." << endl; - return false; - } - write(fd,buffer,numbytes); - if (myconfig.debug || myconfig.verbosity > 0) cout << numbytes <<" bytes sent."; - if (myconfig.debug || myconfig.verbosity > 1) { - cout <<" Data: " << endl ; - for ( int i = 0; i 0 && ((i-3)%8 ==0 || i == 3)) cout << endl; - } - } - if (myconfig.debug || myconfig.verbosity > 0) cout << endl; - usleep(100000); - return true; -} + +void read_data() +{ + // read all available data (always get single bytes...) + while (read(fd,&buf[buf_pos],1) > 0) + { + // search for SYNC byte + if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape)) + { + if (buf_cmd_start != 255) + { + #ifdef DEBUG + uint8_t i; + printf("Damaged Packet: "); + for (i=buf_cmd_start; i= CMD_DATA_START) + { + // if we recieved a valid packet: start decoding + if (buf[buf_cmd_start + CMD_HDR_LENGTH+1] == (buf_pos - buf_cmd_start - CMD_DATA_START)) + { + DecodeCMD(); + buf_pos = 0; + buf_cmd_start = 255; + } + } + } +} + + /*! \brief prints some help */ @@ -420,6 +451,8 @@ cout << endl; cout << "c\tclear display" << endl; cout << endl; + cout << "b\tenter bootload mode" << endl; + cout << endl; cout << "h\tprint this help" << endl; cout << "j\tprint command line options" << endl; cout << "q\tquit" << endl; @@ -468,17 +501,14 @@ /*! \brief handles SIGIO */ -void signal_handler_IO (int status) { - - unsigned char buf[255]; - int res = read(fd,buf,255); - +void DecodeCMD() { if (myconfig.debug || myconfig.verbosity > 0) { //cout << "Received Data\n"; - if (buf[0] == 0x55) { - switch (buf[1]) { + if (buf[buf_cmd_start] == CMD_SYNC_RECV) { + switch (buf[buf_cmd_start + 1]) { case 0x01: cout << "Received an ACK packet, payload: " << (int) buf[3] << " bytes." << endl; + ack_flag = true; break; case 0x02: cout << "Received a NACK packet." << endl; @@ -497,7 +527,7 @@ } if (myflags.parse_return_clockram || myconfig.debug || myconfig.verbosity > 1) { - for (int i=0; i 3) { switch (i) { @@ -537,5 +567,41 @@ } cout << endl; } - if (myconfig.debug || myconfig.verbosity > 0) cout << dec << res << " bytes received.\n"; -} + if (myconfig.debug || myconfig.verbosity > 0) cout << dec << (buf_pos - buf_cmd_start) << " bytes received.\n"; +} + +/*! \brief sends size bytes of buffer data to the display + +\warning send_data doesn't check if buffer really is numbytes big. Expect all kind of weirdness if abused. +\param buffer pointer to a char buffer conatining the data +\param numbytes Number of bytes in buffer +*/ +bool send_data(const unsigned char * buffer, int numbytes) { + + int tout = 0; + + if (numbytes > 64 ) { + cout << "Buffer size exceeds 64 Bytes, send_data aborted." << endl; + return false; + } + write(fd,buffer,numbytes); + if (myconfig.debug || myconfig.verbosity > 0) cout << numbytes <<" bytes sent."; + if (myconfig.debug || myconfig.verbosity > 1) { + cout <<" Data: " << endl ; + for ( int i = 0; i 0 && ((i-3)%8 ==0 || i == 3)) cout << endl; + } + } + if (myconfig.debug || myconfig.verbosity > 0) cout << endl; + + ack_flag = false; + + while (!ack_flag && tout < 1000) { + tout++; + read_data(); + usleep(100); + } + + return ack_flag; +} diff -r 7fd00015f62f -r 4567e7bc404c tools/picctldisplaytest/src/picctldisplaytest.h --- a/tools/picctldisplaytest/src/picctldisplaytest.h Thu Apr 23 19:10:12 2009 +0200 +++ b/tools/picctldisplaytest/src/picctldisplaytest.h Thu Apr 23 20:22:04 2009 +0200 @@ -32,7 +32,8 @@ void print_cli_options(); int dec2bcd(int decimal); int bcd2dec(int bcd); -void signal_handler_IO (int status); +void DecodeCMD (); +void read_data(); bool write_gfx_ram(const unsigned char * buffer, int numbytes); static const unsigned char CMD_DISP_CLEAR_SCREEN[] = {0xAA,0x10,0x00,0x00}; @@ -41,8 +42,20 @@ static const unsigned char CMD_GET_CLOCK_MEMORY[] = {0xAA,0x40,0x00,0x00}; static unsigned char CMD_SET_PWM1[] = {0xAA,0x45,0x00,0x01,0x00}; static unsigned char CMD_SET_PWM2[] = {0xAA,0x46,0x00,0x01,0x00}; +static unsigned char CMD_BOOT[] = {0xAA,0x80,0x00,0x00}; #define BAUDRATE B921600 + +const unsigned char CMD_HDR_SYNC = 0; +const unsigned char CMD_HDR_COMMAND = 1; +const unsigned char CMD_HDR_LENGTH = 2; +const unsigned char CMD_DATA_START = 4; + +const unsigned char CMD_SYNC_SEND = 0xAA; +const unsigned char CMD_SYNC_RECV = 0x55; +const unsigned char CMD_ESCAPE_BYTE = 0x42; + + /*! \brief configuration set by command line */ struct config { bool debug;