I have a memory structure that contains various structures and variables defined as below with CRCr as UNION struct that defines uint16_t and two uint8_t variables. Later struct containing bits and single byte for Flags is represented correctly. For some weird reason when iterating using pointer over whole struct I get extra byte as shown with logs below.(sequence goes with 05 06 00 07 08 )
In struct GEN_CRCD_int there are two uint8_t - D7 and D8 residing in the same place as CRC uint16_t. This is placed as union CRCr, therefore padding (before this struct) is probably applied due to 32 bit architecture ad I'm ending record on 7th byte.
Is this correct?
So probably this exact code worked fine on AVR 8 bit but gives this behavior on RPI PICO (ARM) 32bit doe to different architecture.
struct GEN_CRCD_int
{
volatile uint8_t D7;
volatile uint8_t D8;
};
struct GENet_header
{
volatile uint16_t dstAddr;
volatile uint16_t srcAddr;
volatile uint8_t D0;
volatile uint8_t D1;
volatile uint8_t D2;
volatile uint8_t D3;
volatile uint8_t D4;
volatile uint8_t D5;
volatile uint8_t D6;
volatile union
{
volatile struct GEN_CRCD_int D;
volatile uint16_t CRC;
} CRCr;
volatile uint8_t L;
volatile union
{
volatile uint8_t F;
volatile struct GENet_flags flags;
} Flags;
volatile uint8_t PN;
volatile uint8_t CH; // CRC8 header
Debug code :
frame->header.D1=1;
frame->header.D2=2;
frame->header.D3=3;
frame->header.D4=4;
frame->header.D5=5;
frame->header.D6=6;
frame->header.CRCr.D.D7=7;
frame->header.CRCr.D.D8=8;
frame->header.L=9;
frame->header.locked = 1;
uint16_t avail = GEN_send_available(usart); // we assume uart buffer is 8 bit size ! :)
if (avail > 8) // if we have at least 8 bytes in buffer
{
uint16_t frame_size = GEN_get_frame_size(frame);
char *bpointer = (char *)&frame->buffer;
uint8_t limit = avail;
// we need to reserve at least 2 bytes more to compensate for pre-header flush and sync bytes
if (frame_size - frame->header.frame_index + 2 <= avail)
{
limit = frame_size - frame->header.frame_index + 2;
}
else
{
limit = avail;
}
GEN_transmit_ar(1,"SENDING [",9);// debug
uint8_t i = 0;
char *pointer;
if (frame->header.frame_index == 0)
{
GEN_transmit(usart, 0xFF); // flush byte - helps unclog bad data in usarts
limit--;
GEN_transmit(usart, 0b10101010); // sync byte
limit--;
}
for (i = 0; i < limit; i++)
{
if (frame->header.frame_index < 17)
{
pointer = (char *)frame + frame->header.frame_index;
GEN_transmit(usart, *pointer);
byteToHex(*pointer & 0xFF,tchar);// debug
GEN_transmit_ar(1,tchar,2);// debug
GEN_transmit(1,' ');// debug
}
else
{
pointer = (char *)bpointer + frame->header.frame_index - 17;
GEN_transmit(usart, *pointer);
byteToHex(*pointer & 0xFF,tchar);// debug
GEN_transmit_ar(1,tchar,2);// debug
GEN_transmit(1,' ');// debug
}
frame->header.frame_index++;
}
if (frame->header.frame_index == frame_size)
{
frame->header.is_sent = 1;
frame->header.sending = 0;
}
GEN_transmit(1,']');// debug
GEN_transmit(1,13);// debug
GEN_transmit(1,10);// debug
Output :
SENDING [01 00 23 A4 1B 01 02 03 04 05 06 00 07 08 09 92 00 ]
[prfe evt: 12 PN:0 sys:1]
[TX:from <0xA423> to <0x0001> port=0 L_INDEX=0 L=0 F=0x92 PN=0x00 CRC8=0x93 #]
Receiver sees the same extra byte :
Error during GENet frame reception at index 17 from : 42019 to 1 error code : 1
Received frame from <0xA423> to <0x0001> size [25] 010023A41B010203040506000708099200FFAA010023A41B01 [errors : 1]