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

Committer:
Arthrik
Date:
Mon Apr 15 06:41:00 2019 +0000
Revision:
0:e88514a784bb
Child:
1:edbcde816013
Initial commit. rx_interrupt not stable yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Arthrik 0:e88514a784bb 1 #include "ST7580.h"
Arthrik 0:e88514a784bb 2
Arthrik 0:e88514a784bb 3 ST7580::ST7580(PinName tx, PinName rx, PinName t_req, PinName reset, void (*usr_callback)(unsigned char *, int))
Arthrik 0:e88514a784bb 4 {
Arthrik 0:e88514a784bb 5 this->_rcv_data_idx = 0;
Arthrik 0:e88514a784bb 6 this->_rcv_data_offset = 0;
Arthrik 0:e88514a784bb 7 this->_step = 0;
Arthrik 0:e88514a784bb 8
Arthrik 0:e88514a784bb 9 _plm_t_req = new DigitalOut(t_req);
Arthrik 0:e88514a784bb 10 _plm_reset = new DigitalOut(reset);
Arthrik 0:e88514a784bb 11
Arthrik 0:e88514a784bb 12 _plm_uart = new RawSerial(tx, rx);
Arthrik 0:e88514a784bb 13 _plm_uart->baud(57600);
Arthrik 0:e88514a784bb 14 _plm_uart->format(); //8N1
Arthrik 0:e88514a784bb 15 _plm_uart->attach(callback(this, &ST7580::rx_callback));
Arthrik 0:e88514a784bb 16
Arthrik 0:e88514a784bb 17 _usr_callback = usr_callback;
Arthrik 0:e88514a784bb 18 }
Arthrik 0:e88514a784bb 19
Arthrik 0:e88514a784bb 20 void ST7580::init()
Arthrik 0:e88514a784bb 21 {
Arthrik 0:e88514a784bb 22 printf("PLM INIT START\n");
Arthrik 0:e88514a784bb 23
Arthrik 0:e88514a784bb 24 _plm_t_req->write(1);
Arthrik 0:e88514a784bb 25
Arthrik 0:e88514a784bb 26 _plm_reset->write(0);
Arthrik 0:e88514a784bb 27 wait_ms(1500);
Arthrik 0:e88514a784bb 28 _plm_reset->write(1);
Arthrik 0:e88514a784bb 29
Arthrik 0:e88514a784bb 30 this->wait_reset();
Arthrik 0:e88514a784bb 31
Arthrik 0:e88514a784bb 32 printf("PLM INIT DONE\n");
Arthrik 0:e88514a784bb 33 }
Arthrik 0:e88514a784bb 34
Arthrik 0:e88514a784bb 35 void ST7580::send_frame(unsigned char *msg, int msg_length)
Arthrik 0:e88514a784bb 36 {
Arthrik 0:e88514a784bb 37 unsigned char data[255];
Arthrik 0:e88514a784bb 38 unsigned char full_data[255 + 5];
Arthrik 0:e88514a784bb 39 unsigned int checksum;
Arthrik 0:e88514a784bb 40 unsigned int data_length = msg_length + 1;
Arthrik 0:e88514a784bb 41 unsigned int frame_length = data_length + 1;
Arthrik 0:e88514a784bb 42
Arthrik 0:e88514a784bb 43 data[0] = 0x44; //Default modulation
Arthrik 0:e88514a784bb 44 for(int i = 0; i < msg_length; i++)
Arthrik 0:e88514a784bb 45 {
Arthrik 0:e88514a784bb 46 data[i + 1] = msg[i];
Arthrik 0:e88514a784bb 47 }
Arthrik 0:e88514a784bb 48
Arthrik 0:e88514a784bb 49 //checksum calculation
Arthrik 0:e88514a784bb 50 checksum = 0;
Arthrik 0:e88514a784bb 51 checksum += data_length;
Arthrik 0:e88514a784bb 52 checksum += CMD_DL_DATA_REQ; //command, data request
Arthrik 0:e88514a784bb 53 for(int i = 0; i < data_length; ++i){
Arthrik 0:e88514a784bb 54 checksum += data[i];
Arthrik 0:e88514a784bb 55 }
Arthrik 0:e88514a784bb 56 full_data[0] = ST7580_STX_02; //start of frame
Arthrik 0:e88514a784bb 57 full_data[1] = data_length;
Arthrik 0:e88514a784bb 58 full_data[2] = CMD_DL_DATA_REQ;
Arthrik 0:e88514a784bb 59
Arthrik 0:e88514a784bb 60 for(unsigned char i = 0;i < data_length; ++i)
Arthrik 0:e88514a784bb 61 {
Arthrik 0:e88514a784bb 62 full_data[i + 3] = data[i];
Arthrik 0:e88514a784bb 63 }
Arthrik 0:e88514a784bb 64
Arthrik 0:e88514a784bb 65 full_data[data_length + 3] = (unsigned char)( checksum );
Arthrik 0:e88514a784bb 66 full_data[data_length + 4] = (unsigned char)( checksum >> 8 );
Arthrik 0:e88514a784bb 67
Arthrik 0:e88514a784bb 68 _plm_t_req->write(0);
Arthrik 0:e88514a784bb 69
Arthrik 0:e88514a784bb 70 this->wait_status();
Arthrik 0:e88514a784bb 71
Arthrik 0:e88514a784bb 72 wait_ms(ST7580_TSR); //p.18 de la doc ST7580
Arthrik 0:e88514a784bb 73
Arthrik 0:e88514a784bb 74 _plm_uart->printf((const char*)full_data);
Arthrik 0:e88514a784bb 75 //for (int i = 0; i < frame_length; i++)
Arthrik 0:e88514a784bb 76 //{
Arthrik 0:e88514a784bb 77 // _plm_uart->putc(full_data[i]);
Arthrik 0:e88514a784bb 78 //}
Arthrik 0:e88514a784bb 79 _plm_t_req->write(1);
Arthrik 0:e88514a784bb 80 printf("SENT DATA\n");
Arthrik 0:e88514a784bb 81 }
Arthrik 0:e88514a784bb 82
Arthrik 0:e88514a784bb 83 void ST7580::wait_status()
Arthrik 0:e88514a784bb 84 {
Arthrik 0:e88514a784bb 85 while (this->_rx_char != ST7580_STX_STATUS);
Arthrik 0:e88514a784bb 86 }
Arthrik 0:e88514a784bb 87
Arthrik 0:e88514a784bb 88 void ST7580::wait_reset()
Arthrik 0:e88514a784bb 89 {
Arthrik 0:e88514a784bb 90 while (this->_rx_char != CMD_RESET_IND);
Arthrik 0:e88514a784bb 91 }
Arthrik 0:e88514a784bb 92
Arthrik 0:e88514a784bb 93 void ST7580::reset_reception_buffer()
Arthrik 0:e88514a784bb 94 {
Arthrik 0:e88514a784bb 95 this->_step = 0;
Arthrik 0:e88514a784bb 96 this->_rcv_data_idx = 0;
Arthrik 0:e88514a784bb 97 memset((char *)this->_rcv_data, 0, sizeof(char) * sizeof(this->_rcv_data)); //Reset reception buffer
Arthrik 0:e88514a784bb 98 }
Arthrik 0:e88514a784bb 99
Arthrik 0:e88514a784bb 100 void ST7580::rx_complete()
Arthrik 0:e88514a784bb 101 {
Arthrik 0:e88514a784bb 102 this->_usr_callback((unsigned char *)this->_rcv_data, this->_rcv_data_idx); //callback into the user app
Arthrik 0:e88514a784bb 103 this->reset_reception_buffer();
Arthrik 0:e88514a784bb 104 //printf("OVERFLOW: RESET\n");
Arthrik 0:e88514a784bb 105 }
Arthrik 0:e88514a784bb 106
Arthrik 0:e88514a784bb 107 void ST7580::rx_callback()
Arthrik 0:e88514a784bb 108 {
Arthrik 0:e88514a784bb 109 if (this->_rcv_data_idx >= 512) this->reset_reception_buffer();
Arthrik 0:e88514a784bb 110 this->_rx_char = _plm_uart->getc();
Arthrik 0:e88514a784bb 111 if (this->_step > 9)
Arthrik 0:e88514a784bb 112 {
Arthrik 0:e88514a784bb 113 if (this->_rx_char == '%')
Arthrik 0:e88514a784bb 114 {
Arthrik 0:e88514a784bb 115 this->rx_complete();
Arthrik 0:e88514a784bb 116 }
Arthrik 0:e88514a784bb 117 else
Arthrik 0:e88514a784bb 118 {
Arthrik 0:e88514a784bb 119 this->_rcv_data[this->_rcv_data_idx++] = this->_rx_char;
Arthrik 0:e88514a784bb 120 this->_rcv_data_idx++;
Arthrik 0:e88514a784bb 121 }
Arthrik 0:e88514a784bb 122 }
Arthrik 0:e88514a784bb 123 this->_step++;
Arthrik 0:e88514a784bb 124 }