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

ST7580.cpp

Committer:
Arthrik
Date:
2019-04-15
Revision:
0:e88514a784bb
Child:
1:edbcde816013

File content as of revision 0:e88514a784bb:

#include "ST7580.h"

ST7580::ST7580(PinName tx, PinName rx, PinName t_req, PinName reset, void (*usr_callback)(unsigned char *, int))
{
    this->_rcv_data_idx = 0;
    this->_rcv_data_offset = 0;
    this->_step = 0;
    
    _plm_t_req = new DigitalOut(t_req);
    _plm_reset = new DigitalOut(reset);
    
    _plm_uart = new RawSerial(tx, rx);
    _plm_uart->baud(57600);
    _plm_uart->format();                  //8N1
    _plm_uart->attach(callback(this, &ST7580::rx_callback));

    _usr_callback = usr_callback;
}

void ST7580::init()
{
    printf("PLM INIT START\n");
    
    _plm_t_req->write(1);
    
    _plm_reset->write(0);
    wait_ms(1500);
    _plm_reset->write(1);
    
    this->wait_reset();
    
    printf("PLM INIT DONE\n");
}

void ST7580::send_frame(unsigned char *msg, int msg_length)
{
    unsigned char   data[255];
    unsigned char   full_data[255 + 5];
    unsigned int    checksum;
    unsigned int    data_length = msg_length + 1;
    unsigned int    frame_length = data_length + 1;

    data[0] = 0x44;     //Default modulation
    for(int i = 0; i < msg_length; i++)
    {
        data[i + 1] = msg[i];
    }

    //checksum calculation
    checksum = 0;
    checksum += data_length;
    checksum += CMD_DL_DATA_REQ;           //command, data request
    for(int i = 0; i < data_length; ++i){
        checksum += data[i];
    }
    full_data[0] = ST7580_STX_02;        //start of frame
    full_data[1] = data_length;
    full_data[2] = CMD_DL_DATA_REQ;

    for(unsigned char i = 0;i < data_length; ++i)
    {
        full_data[i + 3] = data[i];
    }

    full_data[data_length + 3] = (unsigned char)( checksum );
    full_data[data_length + 4] = (unsigned char)( checksum >> 8 );

    _plm_t_req->write(0);
    
    this->wait_status();
    
    wait_ms(ST7580_TSR);   //p.18 de la doc ST7580
    
    _plm_uart->printf((const char*)full_data);
    //for (int i = 0; i < frame_length; i++)
    //{
    //    _plm_uart->putc(full_data[i]);
    //}
    _plm_t_req->write(1);
    printf("SENT DATA\n");
}

void ST7580::wait_status()
{
    while (this->_rx_char != ST7580_STX_STATUS);
}

void ST7580::wait_reset()
{
    while (this->_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
}

void ST7580::rx_complete()
{
    this->_usr_callback((unsigned char *)this->_rcv_data, this->_rcv_data_idx); //callback into the user app
    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)
    {
        if (this->_rx_char == '%')
        {
            this->rx_complete();
        }
        else
        {
            this->_rcv_data[this->_rcv_data_idx++] = this->_rx_char;
            this->_rcv_data_idx++;
        }
    }
    this->_step++;
}