Skip to content

Commit

Permalink
Merge pull request #30 from arduino/enqueue-fine-tuning
Browse files Browse the repository at this point in the history
Fix: various race conditions and packet transmission bugs (and allow multi sub-packets per meta-packet)
  • Loading branch information
aentinger authored Oct 20, 2023
2 parents 30c28b0 + b39766e commit a08e21e
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 295 deletions.
1 change: 0 additions & 1 deletion include/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ enum AnalogPins {
**************************************************************************************/

void adc_init();
void get_adc_value(enum AnalogPins name);

#endif //ADC_H
4 changes: 2 additions & 2 deletions include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ typedef enum {
**************************************************************************************/

void can_init();
void can_handle_data();
int can_handle_data();

void can_init_device(FDCAN_HandleTypeDef * handle, CANName peripheral, CanNominalBitTimingResult const can_bit_timing);
int can_frequency(FDCAN_HandleTypeDef * handle, uint32_t const can_bitrate);

void can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg);
int can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg);
int can_read(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message *msg);
int can_filter(FDCAN_HandleTypeDef * handle, uint32_t const filter_index, uint32_t const id, uint32_t const mask, bool const is_extended_id);
unsigned char can_rderror(FDCAN_HandleTypeDef * handle);
Expand Down
2 changes: 1 addition & 1 deletion include/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ void gpio_init();

void gpio_set_initial_config();

void gpio_handle_data();
int gpio_handle_data();

#endif //GPIO_H
2 changes: 0 additions & 2 deletions include/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ struct rtc_time {
**************************************************************************************/

void rtc_init();
void rtc_set_date(uint8_t *data);
void rtc_get_date(uint8_t *data);

#endif //RTC_H
2 changes: 1 addition & 1 deletion include/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ void spi_init();

void spi_end();

void spi_transmit_receive(uint8_t peripheral, uint8_t *tx_buf, uint8_t *rx_buf, uint16_t size);
void spi_transmit_receive(uint8_t * tx_buf, uint8_t * rx_buf, uint16_t size);

#endif //SPI_H
13 changes: 6 additions & 7 deletions include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,16 @@ void system_init();

void dma_init();

void enqueue_packet(uint8_t peripheral, uint8_t opcode, uint16_t size, void* data);
int enqueue_packet(uint8_t const peripheral, uint8_t const opcode, uint16_t const size, void * data);
void set_nirq_low();
void set_nirq_high();
uint16_t get_tx_packet_size();
bool is_dma_transfer_complete();

void dispatchPacket(uint8_t peripheral, uint8_t opcode, uint16_t size, uint8_t* data);

struct complete_packet* get_dma_packet();

int get_dma_packet_size();

void dma_handle_data();

typedef void (*PeriphCallbackFunc) (uint8_t opcode, uint8_t *data, uint16_t size);
typedef int (*PeriphCallbackFunc) (uint8_t opcode, uint8_t *data, uint16_t size);
void register_peripheral_callback(uint8_t peripheral, PeriphCallbackFunc func);

#endif //SYSTEM_H
4 changes: 2 additions & 2 deletions include/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ int uart_write_with_timeout(uint8_t *data, uint16_t size, uint32_t timeout);

int uart_data_available();

void uart_handle_data();
int uart_handle_data();

int virtual_uart_data_available();

void virtual_uart_handle_data();
int virtual_uart_handle_data();

void UART2_enable_rx_irq();

Expand Down
26 changes: 18 additions & 8 deletions src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,30 @@ static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_ADC3_Init(void);

static int get_adc_value(enum AnalogPins name);

/**************************************************************************************
* FUNCTION DEFINITION
**************************************************************************************/

void adc_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
if (opcode == CONFIGURE) {
int adc_handler(uint8_t opcode, uint8_t *data, uint16_t size)
{
if (opcode == CONFIGURE)
{
/* Note: ADC currently only supports polling mode.
* uint16_t adc_sample_rate = *((uint16_t*)data);
* dbg_printf("Setting ADC samplerate to %d milliseconds\n", adc_sample_rate);
*/
} else if ((opcode >= A0) && (opcode <= A7)) {
get_adc_value(opcode);
} else {
dbg_printf("Invalid ADC opcode %02x\n", opcode);
return 0;
}
else if ((opcode >= A0) && (opcode <= A7))
{
return get_adc_value(opcode);
}
else
{
dbg_printf("adc_handler: invalid ADC opcode %02x\n", opcode);
return 0;
}
}

Expand All @@ -89,7 +99,7 @@ void adc_init() {
register_peripheral_callback(PERIPH_ADC, &adc_handler);
}

void get_adc_value(enum AnalogPins name) {
int get_adc_value(enum AnalogPins name) {
ADC_ChannelConfTypeDef conf = {0};
ADC_HandleTypeDef* peripheral;

Expand All @@ -109,7 +119,7 @@ void get_adc_value(enum AnalogPins name) {

dbg_printf("ADC%d: %d\n", name-1, value);

enqueue_packet(PERIPH_ADC, name, sizeof(value), &value);
return enqueue_packet(PERIPH_ADC, name, sizeof(value), &value);
}

static void MX_ADC1_Init(void) {
Expand Down
63 changes: 36 additions & 27 deletions src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ static void error(char* string) {
while (1);
}

void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
int fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
if (opcode == CONFIGURE)
{
uint32_t const can_bitrate = *((uint32_t *)data);
can_frequency(&fdcan_1, can_bitrate);
dbg_printf("fdcan1_handler: configuring fdcan1 with frequency %ld\n", can_bitrate);
return 0;
}
else if (opcode == CAN_FILTER)
{
Expand All @@ -175,27 +176,31 @@ void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
{
dbg_printf("fdcan1_handler: can_filter failed for idx: %ld, id: %lX, mask: %lX\n", x8h7_msg.field.idx, x8h7_msg.field.id, x8h7_msg.field.mask);
}
return 0;
}
else if (opcode == CAN_TX_FRAME)
{
union x8h7_can_frame_message msg;
memcpy(&msg, data, size);

dbg_printf("fdcan1_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n", msg.id, msg.len, msg.data[0]);

can_write(&fdcan_1, &msg);
dbg_printf("fdcan1_handler: sending CAN message to %lx, size %d, content[0]=0x%02X\n", msg.field.id, msg.field.len, msg.field.data[0]);
return can_write(&fdcan_1, &msg);
}
else {
else
{
dbg_printf("fdcan1_handler: error invalid opcode (:%d)\n", opcode);
return 0;
}
}

void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
int fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size)
{
if (opcode == CONFIGURE)
{
uint32_t const can_bitrate = *((uint32_t *)data);
can_frequency(&fdcan_2, can_bitrate);
dbg_printf("fdcan2_handler: configuring fdcan2 with frequency %ld\n", can_bitrate);
return 0;
}
else if (opcode == CAN_FILTER)
{
Expand All @@ -210,18 +215,20 @@ void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
{
dbg_printf("fdcan2_handler: can_filter failed for idx: %ld, id: %lX, mask: %lX\n", x8h7_msg.field.idx, x8h7_msg.field.id, x8h7_msg.field.mask);
}
return 0;
}
else if (opcode == CAN_TX_FRAME)
{
union x8h7_can_frame_message msg;
memcpy(&msg, data, size);

dbg_printf("fdcan2_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n", msg.id, msg.len, msg.data[0]);

can_write(&fdcan_2, &msg);
dbg_printf("fdcan2_handler: sending CAN message to %lx, size %d, content[0]=0x%02X\n", msg.field.id, msg.field.len, msg.field.data[0]);
return can_write(&fdcan_2, &msg);
}
else {
else
{
dbg_printf("fdcan2_handler: error invalid opcode (:%d)\n", opcode);
return 0;
}
}

Expand Down Expand Up @@ -251,25 +258,28 @@ void can_init()
register_peripheral_callback(PERIPH_FDCAN2, &fdcan2_handler);
}

void can_handle_data()
int can_handle_data()
{
int bytes_enqueued = 0;
union x8h7_can_frame_message msg;

if (can_read(&fdcan_1, &msg))
/* Note: the last read package is lost in this implementation. We need to fix this by
* implementing some peek method or by buffering messages in a ringbuffer.
*/

for (int rc_enq = 0; can_read(&fdcan_1, &msg); bytes_enqueued += rc_enq)
{
enqueue_packet(PERIPH_FDCAN1,
CAN_RX_FRAME,
X8H7_CAN_HEADER_SIZE + msg.field.len,
msg.buf);
rc_enq = enqueue_packet(PERIPH_FDCAN1, CAN_RX_FRAME, X8H7_CAN_HEADER_SIZE + msg.field.len, msg.buf);
if (!rc_enq) return bytes_enqueued;
}

if (can_read(&fdcan_2, &msg))
for (int rc_enq = 0; can_read(&fdcan_2, &msg); bytes_enqueued += rc_enq)
{
enqueue_packet(PERIPH_FDCAN2,
CAN_RX_FRAME,
X8H7_CAN_HEADER_SIZE + msg.field.len,
msg.buf);
rc_enq = enqueue_packet(PERIPH_FDCAN2, CAN_RX_FRAME, X8H7_CAN_HEADER_SIZE + msg.field.len, msg.buf);
if (!rc_enq) return bytes_enqueued;
}

return bytes_enqueued;
}

/** Call all the init functions
Expand Down Expand Up @@ -405,7 +415,7 @@ int can_filter(FDCAN_HandleTypeDef * handle, uint32_t const filter_index, uint32
}


void can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg)
int can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg)
{
FDCAN_TxHeaderTypeDef TxHeader = {0};

Expand Down Expand Up @@ -455,22 +465,21 @@ void can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const
uint8_t msg[2] = {X8H7_CAN_STS_INT_ERR, 0};
if (err_code == HAL_FDCAN_ERROR_FIFO_FULL) msg[1] = X8H7_CAN_STS_FLG_TX_OVR;

enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
return enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
}
else
{
uint8_t msg[2] = {X8H7_CAN_STS_INT_TX, 0};
enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
return enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
}
}

int can_read(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message *msg)
{
static const uint8_t DLCtoBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};

if (HAL_FDCAN_GetRxFifoFillLevel(handle, FDCAN_RX_FIFO0) == 0) {
return 0; // No message arrived
}
if (HAL_FDCAN_GetRxFifoFillLevel(handle, FDCAN_RX_FIFO0) == 0)
return 0; // No message arrived

FDCAN_RxHeaderTypeDef RxHeader = {0};
uint8_t RxData[64] = {0};
Expand Down
67 changes: 0 additions & 67 deletions src/crc32.c

This file was deleted.

Loading

0 comments on commit a08e21e

Please sign in to comment.