Arthur Richard / ST7580
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ST7580.cpp Source File

ST7580.cpp

00001 #include "ST7580.h"
00002 
00003 ST7580::ST7580(PinName tx, PinName rx, PinName t_req, PinName reset, void (*usr_callback)(unsigned char *, int))
00004 {
00005     this->_rcv_data_idx = 0;
00006     this->_rcv_data_offset = 0;
00007     this->_rcv_payload_len = 255;
00008     
00009     _plm_t_req = new DigitalOut(t_req);
00010     _plm_reset = new DigitalOut(reset);
00011     
00012     _plm_uart = new RawSerial(tx, rx);
00013     _plm_uart->baud(57600);
00014     _plm_uart->format();                  //8N1
00015     _plm_uart->attach(callback(this, &ST7580::rx_callback));
00016 
00017     _usr_callback = usr_callback;
00018 }
00019 
00020 void ST7580::init()
00021 {
00022     //printf("PLM INIT START\n");
00023     
00024     _plm_t_req->write(1);
00025     
00026     _plm_reset->write(0);
00027     wait_ms(1500);
00028     _plm_reset->write(1);
00029     
00030     this->wait_reset();
00031     
00032     //printf("PLM INIT DONE\n");
00033 }
00034 
00035 void ST7580::send_frame(unsigned char *msg, int msg_length)
00036 {
00037     unsigned char   data[255];
00038     unsigned char   full_data[255 + 5];
00039     unsigned int    checksum;
00040     unsigned int    data_length = msg_length + 1;
00041     unsigned int    frame_length = data_length + 1;
00042 
00043     data[0] = 0x44;     //Default modulation
00044     for(int i = 0; i < msg_length; i++)
00045     {
00046         data[i + 1] = msg[i];
00047     }
00048 
00049     //checksum calculation
00050     checksum = 0;
00051     checksum += data_length;
00052     checksum += CMD_DL_DATA_REQ;           //command, data request
00053     for(int i = 0; i < data_length; ++i){
00054         checksum += data[i];
00055     }
00056     full_data[0] = ST7580_STX_02;        //start of frame
00057     full_data[1] = data_length;
00058     full_data[2] = CMD_DL_DATA_REQ;
00059 
00060     for(unsigned char i = 0;i < data_length; ++i)
00061     {
00062         full_data[i + 3] = data[i];
00063     }
00064 
00065     full_data[data_length + 3] = (unsigned char)( checksum );
00066     full_data[data_length + 4] = (unsigned char)( checksum >> 8 );
00067 
00068     _plm_t_req->write(0);
00069     
00070     //this->wait_status();
00071 
00072     wait_ms(ST7580_TSR);   //p.18 de la doc ST7580
00073     
00074     _plm_uart->printf((const char*)full_data);
00075     //for (int i = 0; i < frame_length; i++)
00076     //{
00077     //    _plm_uart->putc(full_data[i]);
00078     //}
00079     _plm_t_req->write(1);
00080     //printf("SENT DATA\n");
00081 }
00082 
00083 void ST7580::wait_status()
00084 {
00085     while (_rx_char != ST7580_STX_STATUS);
00086 }
00087 
00088 void ST7580::wait_reset()
00089 {
00090     while (_rx_char != CMD_RESET_IND);
00091 }
00092 
00093 void ST7580::reset_reception_buffer()
00094 {
00095     _rcv_payload_len = 255;
00096     _rcv_data_idx = 0;
00097     memset((char *)_rcv_data, 0, sizeof(_rcv_data)); //Reset reception buffer
00098 }
00099 
00100 void ST7580::rx_complete()
00101 {
00102     //printf("%s\n", (unsigned char *)this->_rcv_data);
00103     this->_usr_callback((unsigned char *)_rcv_data + 7, _rcv_payload_len); //callback into the user app, trims the PLC data;
00104     this->reset_reception_buffer();
00105 }
00106 
00107 void ST7580::rx_callback()
00108 {
00109     _rx_char = _plm_uart->getc();
00110     _rcv_data[_rcv_data_idx] = _rx_char;
00111     _rcv_data_idx++;
00112     if (_rcv_data[0] == ST7580_STX_02)         //If this is a start condition
00113     {
00114         if (_rcv_data_idx > 1)
00115         {
00116             _rcv_payload_len = _rcv_data[1];
00117             if (_rcv_payload_len < 5)
00118             {
00119                 this->reset_reception_buffer();
00120             }
00121             if (_rcv_data_idx >= _rcv_payload_len)
00122             {
00123                 this->rx_complete();
00124             }
00125         }
00126     }
00127     else
00128     {
00129         this->reset_reception_buffer();
00130     }
00131 }