How can I convert 16 bytes of data into 2 pieces of 8 bytes of data?

102 views Asked by At

I am using the TMS320F28x card. I am sending a struct worth 16 bytes from the STM32 card.

I want to receive this data on the TMS320F28x card. After receiving the data, I want to divide it into 2 parts of 8 bytes. The code I wrote is as follows. How can I achieve this? The code block I mentioned is in the while loop in main.

#include "F2802x_Device.h"
#include "F2802x_Examples.h"
#include "DSP28x_Project.h"
#include "f2802x_globalprototypes.h"

#define LSPCLK_FREQ 1000
#define SCI_BAUDRATE 1200
#define SCI_PRD (Uint16)((LSPCLK_FREQ / (8 * SCI_BAUDRATE)) - 1)
#define DATA_SIZE 16

typedef struct Data {
    Uint16 received_data1;
    Uint16 received_data2;
    Uint16 received_data3;
    Uint16 received_data4;
    Uint16 received_data5;
    Uint16 received_data6;
} Data;

typedef struct TransmittedData {
    Uint64  latitude;
    Uint64  longitude
   
} TransmittedData;

void Gpio_init(void);
void ProcessGpioData(const Data *d);
void Scia_fifo_init(void);
void delay_loop(long end);
void ReadSciData(Uint16 *data, Uint16 dataSize);
void InitSciP(void);
void TransmitData(const TransmittedData *data);
void InitSciaGpioR(void);

int main(void)
{
    Gpio_init();

//    InitPieCtrl();
//    InitPieVectTable();

    Uint16 receivedData[DATA_SIZE];
    InitSciaGpioR();
    //InitSysCtrl();
    InitSciP();
    Scia_fifo_init();
    Data d1;
    d1.received_data1 = GpioDataRegs.GPADAT.bit.GPIO0;
    d1.received_data2 = GpioDataRegs.GPADAT.bit.GPIO1;
    d1.received_data3 = GpioDataRegs.GPADAT.bit.GPIO2;
    d1.received_data4 = GpioDataRegs.GPADAT.bit.GPIO3;
    d1.received_data5 = GpioDataRegs.GPADAT.bit.GPIO4;
    d1.received_data6 = GpioDataRegs.GPADAT.bit.GPIO5;
    ProcessGpioData(&d1);
     //delay_loop(100000);
    while (1) {
         ReadSciData(receivedData, DATA_SIZE);
         TransmittedData tData;
         tData.latitude = (Uint64)((Uint64)receivedData[0] << 48)| 
                     (Uint64)((Uint64)receivedData[1] << 32) |
                     (Uint64)((Uint64)receivedData[2] << 16) |
                     (Uint64)receivedData[3];
         tData.longitude = (Uint64)((Uint64)receivedData[4] << 48)| 
                      (Uint64)((Uint64)receivedData[5] << 32) |
                      (Uint64)((Uint64)receivedData[6] << 16) |
                      (Uint64)receivedData[7];

    TransmitData(&tData);
     }
}

void delay_loop(long end)
{
    long i;
    for (i = 0; i < end; i++)
    {
        asm(" NOP");
        EALLOW;
        SysCtrlRegs.WDKEY = 0x55;
        SysCtrlRegs.WDKEY = 0xAA;
        EDIS;
    }
}

void ProcessGpioData(const Data *d) {
    if (d->received_data1 == 0) {
        GpioDataRegs.GPACLEAR.bit.GPIO16 = 1;
    } if (d->received_data2 == 1) {
        GpioDataRegs.GPASET.bit.GPIO17 = 1;
    } if (d->received_data3 == 0) {
        GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
    } if (d->received_data4 == 1) {
        GpioDataRegs.GPBSET.bit.GPIO33 = 1;
    } if (d->received_data5 == 0) {
        GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1;
    } if (d->received_data6 == 1) {
        GpioDataRegs.GPBSET.bit.GPIO38 = 1;
    }
}

void ReadSciData(Uint16 *data, Uint16 dataSize)
{
    Uint16 i;
    for (i = 0; i < dataSize; ++i)
    {
        while (SciaRegs.SCIFFRX.bit.RXFFST == 0) {
            // Bekleme
        }
        // SCI veri alımı
        data[i] = SciaRegs.SCIRXBUF.all;
    }
    // FIFO temizleme
    SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1;
}

void TransmitData(const TransmittedData *data){
    // SCI TX FIFO temizleme
    while (SciaRegs.SCIFFTX.bit.TXFFST != 0)
        ;  // FIFO dolu olduğu sürece bekle
    SciaRegs.SCITXBUF = data->latitude_high;
    while (SciaRegs.SCIFFTX.bit.TXFFST != 0)
        ;  // FIFO dolu olduğu sürece bekle
    SciaRegs.SCITXBUF = data->latitude_low;
    while (SciaRegs.SCIFFTX.bit.TXFFST != 0)
        ;  // FIFO dolu olduğu sürece bekle
    SciaRegs.SCITXBUF = data->longitude_high;
    while (SciaRegs.SCIFFTX.bit.TXFFST != 0)
        ;  // FIFO dolu olduğu sürece bekle
    SciaRegs.SCITXBUF = data->longitude_low;
}

void Gpio_init(void){
    EALLOW;

    //for input pins
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO0 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO1 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO2 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO3 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO4 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO5 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;

    //for output pins
    GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 0; // selecting the gpio pin
    GpioCtrlRegs.GPADIR.bit.GPIO16 = 1; // 1 means output pin,0 means input pin
    GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; // input selection completed

    GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO17 = 1;
    GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0;

    GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 0;
    GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1;
    GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0;

    GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 0;
    GpioCtrlRegs.GPBDIR.bit.GPIO33 = 1;
    GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0;

    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;
    GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;
    GpioCtrlRegs.GPBPUD.bit.GPIO34 = 0;

    GpioCtrlRegs.GPBMUX1.bit.GPIO38 = 0;
    GpioCtrlRegs.GPBDIR.bit.GPIO38 = 1;
    GpioCtrlRegs.GPBPUD.bit.GPIO38 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0;
    GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;
    GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0;

    EDIS;
}

void Scia_fifo_init()
{
    // 1 stop bit, 8 data bits, no parity
    SciaRegs.SCIFFCT.all = 0x00;  // Default FIFO settings
    SciaRegs.SCIFFTX.all = 0xE040;
    SciaRegs.SCIFFRX.all = 0x2044;
    SciaRegs.SCIFFTX.bit.TXFIFOXRESET = 1;
    SciaRegs.SCIFFRX.bit.RXFIFORESET = 1;
    SciaRegs.SCICTL1.all = 0x0003;  // Enable TX, RX, internal SCICLK, Disable RX ERR, SLEEP, TXWAKE
    SciaRegs.SCICTL2.bit.TXINTENA = 0;    // Disable TX interrupt
    SciaRegs.SCICTL2.bit.RXBKINTENA = 0;  // Disable RX interrupt
    SciaRegs.SCIHBAUD = 0x0000;          // Baud rate high
    SciaRegs.SCILBAUD = SCI_PRD;         // Baud rate low
    SciaRegs.SCICTL1.all = 0x0023;        // Relinquish SCI from Reset
}

void InitSciP(void)
{
    // SCI ayarları
    SciaRegs.SCICCR.all = 0x0007;            // 1 stop bit, 8 char bits, no parity
    SciaRegs.SCICTL1.all = 0x0003;           // Enable TX, RX, internal SCICLK, Disable RX ERR, SLEEP, TXWAKE
    SciaRegs.SCICTL2.bit.TXINTENA = 0;       // TX interrupt disable
    SciaRegs.SCICTL2.bit.RXBKINTENA = 0;     // RX interrupt disable
    SciaRegs.SCIHBAUD = 405>>5;          // Baud rate high
    SciaRegs.SCILBAUD = SCI_PRD;         // Baud rate low
    SciaRegs.SCICTL1.all = 0x0023;           // Relinquish SCI from Reset
}

void InitSciaGpioR(void){
    EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0;
    GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pull-up for GPIO19 (SCIRXDA)
    //GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;

    GpioCtrlRegs.GPAPUD.bit.GPIO29 = 1;
    GpioCtrlRegs.GPAPUD.bit.GPIO18 = 1;
    //GpioCtrlRegs.GPAPUD.bit.GPIO12 = 1;  // Disable pull-up for GPIO12 (SCITXDA)

    GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3;  // Asynch input GPIO28 (SCIRXDA)
    GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3;  // Asynch input GPIO19 (SCIRXDA)
    //GpioCtrlRegs.GPAQSEL1.bit.GPIO7 = 3;   // Asynch input GPIO7 (SCIRXDA)

    GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 2;   // Configure GPIO28 for SCIRXDA
    GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 2;   // Configure GPIO19 for SCIRXDA
    //GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 2;    // Configure GPIO7  for SCIRXDA

    GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 2;   // Configure GPIO29 for SCITXDA
    GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 2;   // Configure GPIO18 for SCITXDA
    //GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 2;   // Configure GPIO12 for SCITXDA

    EDIS;
}

I tried to do the conversion, but I'm not sure of its accuracy. I checked whether the data was received in the simulation environment, but I observed that the data did not arrive.

1

There are 1 answers

4
embeddedstack On

I'm not sure that the example code you given related with your problem. But my understanding from your problem description that you have a value which is 16 bits and you want to split to two 8 bits. Is it correct ?

You can do something like;

struct tx_t
{ 
  uint16_t val;  
}tx_t;

int main()
{
  struct tx_t transmitted = {.val = 0x1234};

  uint8_t recv_buf[2] = { 0 };

  recv_buf[0] = (uint8_t)((transmitted.val >> 8) & 0xFF);
  recv_buf[1] = (uint8_t)(transmitted.val);

  printf("recv_buf[0] : 0x%x, recv_buf[1] : 0x%x", recv_buf[0], recv_buf[1]);
}