https://www.st.com/en/ecosystems/x-nucleo-plm01a1.html MBED driver for the ST7580 IC.

Revision:
1:edbcde816013
Parent:
0:e88514a784bb
diff -r e88514a784bb -r edbcde816013 ST7580.cpp
--- a/ST7580.cpp	Mon Apr 15 06:41:00 2019 +0000
+++ b/ST7580.cpp	Sat Apr 20 20:18:49 2019 +0000
@@ -4,7 +4,7 @@
 {
     this->_rcv_data_idx = 0;
     this->_rcv_data_offset = 0;
-    this->_step = 0;
+    this->_rcv_payload_len = 255;
     
     _plm_t_req = new DigitalOut(t_req);
     _plm_reset = new DigitalOut(reset);
@@ -19,7 +19,7 @@
 
 void ST7580::init()
 {
-    printf("PLM INIT START\n");
+    //printf("PLM INIT START\n");
     
     _plm_t_req->write(1);
     
@@ -29,7 +29,7 @@
     
     this->wait_reset();
     
-    printf("PLM INIT DONE\n");
+    //printf("PLM INIT DONE\n");
 }
 
 void ST7580::send_frame(unsigned char *msg, int msg_length)
@@ -67,8 +67,8 @@
 
     _plm_t_req->write(0);
     
-    this->wait_status();
-    
+    //this->wait_status();
+
     wait_ms(ST7580_TSR);   //p.18 de la doc ST7580
     
     _plm_uart->printf((const char*)full_data);
@@ -77,48 +77,55 @@
     //    _plm_uart->putc(full_data[i]);
     //}
     _plm_t_req->write(1);
-    printf("SENT DATA\n");
+    //printf("SENT DATA\n");
 }
 
 void ST7580::wait_status()
 {
-    while (this->_rx_char != ST7580_STX_STATUS);
+    while (_rx_char != ST7580_STX_STATUS);
 }
 
 void ST7580::wait_reset()
 {
-    while (this->_rx_char != CMD_RESET_IND);
+    while (_rx_char != CMD_RESET_IND);
 }
 
 void ST7580::reset_reception_buffer()
 {
-    this->_step = 0;
-    this->_rcv_data_idx = 0;
-    memset((char *)this->_rcv_data, 0, sizeof(char) * sizeof(this->_rcv_data)); //Reset reception buffer
+    _rcv_payload_len = 255;
+    _rcv_data_idx = 0;
+    memset((char *)_rcv_data, 0, sizeof(_rcv_data)); //Reset reception buffer
 }
 
 void ST7580::rx_complete()
 {
-    this->_usr_callback((unsigned char *)this->_rcv_data, this->_rcv_data_idx); //callback into the user app
+    //printf("%s\n", (unsigned char *)this->_rcv_data);
+    this->_usr_callback((unsigned char *)_rcv_data + 7, _rcv_payload_len); //callback into the user app, trims the PLC data;
     this->reset_reception_buffer();
-    //printf("OVERFLOW: RESET\n");
 }
 
 void ST7580::rx_callback()
 {
-    if (this->_rcv_data_idx >= 512)     this->reset_reception_buffer();
-    this->_rx_char = _plm_uart->getc();
-    if (this->_step > 9)
+    _rx_char = _plm_uart->getc();
+    _rcv_data[_rcv_data_idx] = _rx_char;
+    _rcv_data_idx++;
+    if (_rcv_data[0] == ST7580_STX_02)         //If this is a start condition
     {
-        if (this->_rx_char == '%')
+        if (_rcv_data_idx > 1)
         {
-            this->rx_complete();
-        }
-        else
-        {
-            this->_rcv_data[this->_rcv_data_idx++] = this->_rx_char;
-            this->_rcv_data_idx++;
+            _rcv_payload_len = _rcv_data[1];
+            if (_rcv_payload_len < 5)
+            {
+                this->reset_reception_buffer();
+            }
+            if (_rcv_data_idx >= _rcv_payload_len)
+            {
+                this->rx_complete();
+            }
         }
     }
-    this->_step++;
-}
\ No newline at end of file
+    else
+    {
+        this->reset_reception_buffer();
+    }
+}