2022-10-03 04:54 AM
/*
* OTA Command format
*
* ________________________________________
* | | Packet | | | | |
* | SOF | Type | Len | CMD | CRC | EOF |
* |_____|________|_____|_____|_____|_____|
* 1B 1B 2B 1B 4B 1B
*/
typedef struct
{
uint8_t sof;
uint8_t packet_type;
uint16_t data_len;
uint8_t cmd;
uint32_t crc;
uint8_t eof;
}__attribute__((packed)) ETX_OTA_COMMAND_;
int send_ota_start(int comport)
{
uint16_t len;
ETX_OTA_COMMAND_ *ota_start = (ETX_OTA_COMMAND_*)DATA_BUF;
int ex = 0;
memset(DATA_BUF, 0, ETX_OTA_PACKET_MAX_SIZE);
ota_start->sof = ETX_OTA_SOF;
ota_start->packet_type = ETX_OTA_PACKET_TYPE_CMD;
ota_start->data_len = 1;
ota_start->cmd = ETX_OTA_CMD_START;
ota_start->crc = 0x00; //TODO: Add CRC
ota_start->eof = ETX_OTA_EOF;
len = sizeof(ETX_OTA_COMMAND_);
//send OTA START
for(int i = 0; i < len; i++)
{
delay(1);
if( RS232_SendByte(comport, DATA_BUF[i]) )
{
//some data missed.
printf("OTA START : Send Err\n");
ex = -1;
break;
}
}
if( ex >= 0 )
{
if( !is_ack_resp_received( comport ) )
{
//Received NACK
printf("OTA START : NACK\n");
ex = -1;
}
}
printf("OTA START [ex = %d]\n", ex);
return ex;
}
int RS232_SendByte(int comport_number, unsigned char byte)
{
int n;
if(!WriteFile(Cport[comport_number], &byte, 1, (LPDWORD)((void *)&n), NULL))
{
return(1);
}
if(n<0) return(1);
return(0);
}
hi,
I am working on FOTA implementation programm. for that i have to send flash to serial.
i have done some code gcc to send data. but instead of 10 byte i have received 13 byte .
below is the function code.
the compiler i used is GCC on windows 64 bit
please guide me where i am wrong
2022-10-03 05:16 AM
You'll need to byte pack the structure. Check sizeof()
2022-10-03 05:30 AM
Hello ykn,
try the following instead:
typedef struct __attribute__((packed)) {
uint8_t sof;
uint8_t packet_type;
uint16_t data_len;
uint8_t cmd;
uint32_t crc;
uint8_t eof;
} ETX_OTA_COMMAND_;
BR,
Jaroslav
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-10-03 05:37 AM
2022-10-03 05:43 AM
same output, not working
2022-10-03 05:48 AM
returning 13 size. what is he correct format
2022-10-03 06:07 AM
Define crc as a vector of four bytes and fill it byte by byte. Looks like packed attribute cannot deal with unaligned data and results in stripping the final padding only, which means IT DOESN'T work (since it's supposed to support vectors of structures correctly).
2022-10-03 03:45 PM
To my surprise :( the modern GCC documentation for attribute((packed)) does not mention that attribute((packed)) can be applied to a struct as a whole.
Rather, it provides example where packed is applied to a struct member/field, and attribute((aligned)) may be applied to a whole struct or a member/field. And aligned behaves differently on a struct object definition vs. a typedef.
This IMHO is in favor of what @gbm advises. Live and learn...
2022-10-04 07:54 PM
#include <stdint.h>
#include <assert.h>
typedef struct __attribute__((packed)) {
uint8_t sof;
uint8_t packet_type;
uint16_t data_len;
uint8_t cmd;
uint32_t crc;
uint8_t eof;
} ETX_OTA_COMMAND_;
static_assert(sizeof(ETX_OTA_COMMAND_) == 10);
Compiles without any problems on ARM GNU Toolchain 10.3-2021.10 .
2022-10-05 03:44 PM
My bad. Looked in a wrong GCC manual section. Your example shows a type attribute, not variable attribute. For types (whole struct) it is legal.