1.1 --- a/tools/picctldisplaytest/src/picctldisplaytest.cpp Thu Apr 23 19:10:12 2009 +0200
1.2 +++ b/tools/picctldisplaytest/src/picctldisplaytest.cpp Thu Apr 23 20:22:04 2009 +0200
1.3 @@ -32,7 +32,6 @@
1.4 #include <iostream>
1.5 #include <cstdlib>
1.6 #include <termios.h>
1.7 -#include <signal.h>
1.8 #include <fcntl.h>
1.9 #include "picctldisplaytest.h"
1.10
1.11 @@ -41,12 +40,23 @@
1.12 runtime_flags myflags;
1.13 int fd;
1.14
1.15 +bool ack_flag;
1.16 +uint8_t buf[255];
1.17 +uint8_t buf_pos;
1.18 +uint8_t buf_cmd_start;
1.19 +bool buf_flag_escape;
1.20 +
1.21 +
1.22 int main(int argc, char *argv[])
1.23 {
1.24 bool kill_flag = false;
1.25 struct termios options;
1.26 - struct sigaction saio;
1.27 char key;
1.28 +
1.29 + buf_pos = 0;
1.30 + ack_flag = false;
1.31 + buf_cmd_start = 255;
1.32 + buf_flag_escape = false;
1.33
1.34 cout << "** picctldisplaytest - simple test utility for picctl lcd displays **" << endl
1.35 << ">";
1.36 @@ -83,19 +93,6 @@
1.37 fd = open(myconfig.modem_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
1.38 if (fd <0) {perror(myconfig.modem_device); exit(-1); }
1.39
1.40 - /* install the signal handler before making the device asynchronous */
1.41 - saio.sa_handler = signal_handler_IO;
1.42 - sigemptyset (&saio.sa_mask);
1.43 - saio.sa_flags = 0;
1.44 - saio.sa_restorer = NULL;
1.45 - sigaction(SIGIO,&saio,NULL);
1.46 - // allow the process to receive SIGIO
1.47 - fcntl(fd, F_SETOWN, getpid());
1.48 - // Make the file descriptor asynchronous
1.49 - fcntl(fd, F_SETFL, FASYNC);
1.50 -
1.51 - tcgetattr(fd, &options);
1.52 -
1.53 cfsetispeed(&options, BAUDRATE);
1.54 cfsetospeed(&options, BAUDRATE);
1.55
1.56 @@ -131,6 +128,10 @@
1.57 cout << "Sending Clear Display..." << endl;
1.58 send_data(CMD_DISP_CLEAR_SCREEN,(sizeof(CMD_DISP_CLEAR_SCREEN)/sizeof(char)));
1.59 break;
1.60 + case 'b':
1.61 + cout << "Sending Bootload Start..." << endl;
1.62 + send_data(CMD_BOOT,(sizeof(CMD_BOOT)/sizeof(char)));
1.63 + break;
1.64 case 'h':
1.65 print_help();
1.66 break;
1.67 @@ -314,7 +315,7 @@
1.68 default:
1.69 break;
1.70 }
1.71 - sleep(1);
1.72 + //sleep(1);
1.73 if (key != 0x13 && key != '\n') cout << ">";
1.74 }
1.75 close(fd);
1.76 @@ -367,30 +368,60 @@
1.77 return true;
1.78 }
1.79
1.80 -/*! \brief sends size bytes of buffer data to the display
1.81 -
1.82 -\warning send_data doesn't check if buffer really is numbytes big. Expect all kind of weirdness if abused.
1.83 -\param buffer pointer to a char buffer conatining the data
1.84 -\param numbytes Number of bytes in buffer
1.85 -*/
1.86 -bool send_data(const unsigned char * buffer, int numbytes) {
1.87 - if (numbytes > 64 ) {
1.88 - cout << "Buffer size exceeds 64 Bytes, send_data aborted." << endl;
1.89 - return false;
1.90 - }
1.91 - write(fd,buffer,numbytes);
1.92 - if (myconfig.debug || myconfig.verbosity > 0) cout << numbytes <<" bytes sent.";
1.93 - if (myconfig.debug || myconfig.verbosity > 1) {
1.94 - cout <<" Data: " << endl ;
1.95 - for ( int i = 0; i <numbytes; i++) {
1.96 - cout << " 0x" << ((int) buffer[i] < 10 ? "0" : "") << hex << (int) buffer[i] << dec;
1.97 - if (i > 0 && ((i-3)%8 ==0 || i == 3)) cout << endl;
1.98 - }
1.99 - }
1.100 - if (myconfig.debug || myconfig.verbosity > 0) cout << endl;
1.101 - usleep(100000);
1.102 - return true;
1.103 -}
1.104 +
1.105 +void read_data()
1.106 +{
1.107 + // read all available data (always get single bytes...)
1.108 + while (read(fd,&buf[buf_pos],1) > 0)
1.109 + {
1.110 + // search for SYNC byte
1.111 + if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape))
1.112 + {
1.113 + if (buf_cmd_start != 255)
1.114 + {
1.115 + #ifdef DEBUG
1.116 + uint8_t i;
1.117 + printf("Damaged Packet: ");
1.118 + for (i=buf_cmd_start; i<buf_pos; i++)
1.119 + {
1.120 + printf("0x%02x ",buf[i]);
1.121 + }
1.122 + printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);
1.123 + #endif
1.124 +
1.125 + buf_pos = 0;
1.126 + buf[buf_pos] = CMD_SYNC_RECV;
1.127 + }
1.128 + buf_cmd_start = buf_pos;
1.129 + }
1.130 +
1.131 +
1.132 + // escaping is not used so far...
1.133 +/* if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
1.134 + {
1.135 + // this byte was for masking only.. ignore it!
1.136 + buf_flag_escape = true;
1.137 + continue;
1.138 + }
1.139 + buf_flag_escape = false;*/
1.140 +
1.141 + buf_pos++;
1.142 +
1.143 + // we need to check for a valid packet...
1.144 + if ((buf_pos - buf_cmd_start) >= CMD_DATA_START)
1.145 + {
1.146 + // if we recieved a valid packet: start decoding
1.147 + if (buf[buf_cmd_start + CMD_HDR_LENGTH+1] == (buf_pos - buf_cmd_start - CMD_DATA_START))
1.148 + {
1.149 + DecodeCMD();
1.150 + buf_pos = 0;
1.151 + buf_cmd_start = 255;
1.152 + }
1.153 + }
1.154 + }
1.155 +}
1.156 +
1.157 +
1.158
1.159 /*! \brief prints some help
1.160 */
1.161 @@ -420,6 +451,8 @@
1.162 cout << endl;
1.163 cout << "c\tclear display" << endl;
1.164 cout << endl;
1.165 + cout << "b\tenter bootload mode" << endl;
1.166 + cout << endl;
1.167 cout << "h\tprint this help" << endl;
1.168 cout << "j\tprint command line options" << endl;
1.169 cout << "q\tquit" << endl;
1.170 @@ -468,17 +501,14 @@
1.171
1.172 /*! \brief handles SIGIO
1.173 */
1.174 -void signal_handler_IO (int status) {
1.175 -
1.176 - unsigned char buf[255];
1.177 - int res = read(fd,buf,255);
1.178 -
1.179 +void DecodeCMD() {
1.180 if (myconfig.debug || myconfig.verbosity > 0) {
1.181 //cout << "Received Data\n";
1.182 - if (buf[0] == 0x55) {
1.183 - switch (buf[1]) {
1.184 + if (buf[buf_cmd_start] == CMD_SYNC_RECV) {
1.185 + switch (buf[buf_cmd_start + 1]) {
1.186 case 0x01:
1.187 cout << "Received an ACK packet, payload: " << (int) buf[3] << " bytes." << endl;
1.188 + ack_flag = true;
1.189 break;
1.190 case 0x02:
1.191 cout << "Received a NACK packet." << endl;
1.192 @@ -497,7 +527,7 @@
1.193
1.194 }
1.195 if (myflags.parse_return_clockram || myconfig.debug || myconfig.verbosity > 1) {
1.196 - for (int i=0; i<res; i++) {
1.197 + for (int i=buf_cmd_start; i<buf_pos; i++) {
1.198 cout << "0x" << ((int) buf[i] < 10 ? "0" : "") << hex << (int) buf[i] << " ";
1.199 if (myflags.parse_return_clockram && i > 3) {
1.200 switch (i) {
1.201 @@ -537,5 +567,41 @@
1.202 }
1.203 cout << endl;
1.204 }
1.205 - if (myconfig.debug || myconfig.verbosity > 0) cout << dec << res << " bytes received.\n";
1.206 -}
1.207 + if (myconfig.debug || myconfig.verbosity > 0) cout << dec << (buf_pos - buf_cmd_start) << " bytes received.\n";
1.208 +}
1.209 +
1.210 +/*! \brief sends size bytes of buffer data to the display
1.211 +
1.212 +\warning send_data doesn't check if buffer really is numbytes big. Expect all kind of weirdness if abused.
1.213 +\param buffer pointer to a char buffer conatining the data
1.214 +\param numbytes Number of bytes in buffer
1.215 +*/
1.216 +bool send_data(const unsigned char * buffer, int numbytes) {
1.217 +
1.218 + int tout = 0;
1.219 +
1.220 + if (numbytes > 64 ) {
1.221 + cout << "Buffer size exceeds 64 Bytes, send_data aborted." << endl;
1.222 + return false;
1.223 + }
1.224 + write(fd,buffer,numbytes);
1.225 + if (myconfig.debug || myconfig.verbosity > 0) cout << numbytes <<" bytes sent.";
1.226 + if (myconfig.debug || myconfig.verbosity > 1) {
1.227 + cout <<" Data: " << endl ;
1.228 + for ( int i = 0; i <numbytes; i++) {
1.229 + cout << " 0x" << ((int) buffer[i] < 10 ? "0" : "") << hex << (int) buffer[i] << dec;
1.230 + if (i > 0 && ((i-3)%8 ==0 || i == 3)) cout << endl;
1.231 + }
1.232 + }
1.233 + if (myconfig.debug || myconfig.verbosity > 0) cout << endl;
1.234 +
1.235 + ack_flag = false;
1.236 +
1.237 + while (!ack_flag && tout < 1000) {
1.238 + tout++;
1.239 + read_data();
1.240 + usleep(100);
1.241 + }
1.242 +
1.243 + return ack_flag;
1.244 +}
2.1 --- a/tools/picctldisplaytest/src/picctldisplaytest.h Thu Apr 23 19:10:12 2009 +0200
2.2 +++ b/tools/picctldisplaytest/src/picctldisplaytest.h Thu Apr 23 20:22:04 2009 +0200
2.3 @@ -32,7 +32,8 @@
2.4 void print_cli_options();
2.5 int dec2bcd(int decimal);
2.6 int bcd2dec(int bcd);
2.7 -void signal_handler_IO (int status);
2.8 +void DecodeCMD ();
2.9 +void read_data();
2.10 bool write_gfx_ram(const unsigned char * buffer, int numbytes);
2.11
2.12 static const unsigned char CMD_DISP_CLEAR_SCREEN[] = {0xAA,0x10,0x00,0x00};
2.13 @@ -41,8 +42,20 @@
2.14 static const unsigned char CMD_GET_CLOCK_MEMORY[] = {0xAA,0x40,0x00,0x00};
2.15 static unsigned char CMD_SET_PWM1[] = {0xAA,0x45,0x00,0x01,0x00};
2.16 static unsigned char CMD_SET_PWM2[] = {0xAA,0x46,0x00,0x01,0x00};
2.17 +static unsigned char CMD_BOOT[] = {0xAA,0x80,0x00,0x00};
2.18 #define BAUDRATE B921600
2.19
2.20 +
2.21 +const unsigned char CMD_HDR_SYNC = 0;
2.22 +const unsigned char CMD_HDR_COMMAND = 1;
2.23 +const unsigned char CMD_HDR_LENGTH = 2;
2.24 +const unsigned char CMD_DATA_START = 4;
2.25 +
2.26 +const unsigned char CMD_SYNC_SEND = 0xAA;
2.27 +const unsigned char CMD_SYNC_RECV = 0x55;
2.28 +const unsigned char CMD_ESCAPE_BYTE = 0x42;
2.29 +
2.30 +
2.31 /*! \brief configuration set by command line */
2.32 struct config {
2.33 bool debug;