Skip to content

Commit a08e21e

Browse files
authored
Merge pull request #30 from arduino/enqueue-fine-tuning
Fix: various race conditions and packet transmission bugs (and allow multi sub-packets per meta-packet)
2 parents 30c28b0 + b39766e commit a08e21e

File tree

17 files changed

+309
-295
lines changed

17 files changed

+309
-295
lines changed

include/adc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,5 @@ enum AnalogPins {
4545
**************************************************************************************/
4646

4747
void adc_init();
48-
void get_adc_value(enum AnalogPins name);
4948

5049
#endif //ADC_H

include/can.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ typedef enum {
7676
**************************************************************************************/
7777

7878
void can_init();
79-
void can_handle_data();
79+
int can_handle_data();
8080

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

84-
void can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg);
84+
int can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg);
8585
int can_read(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message *msg);
8686
int can_filter(FDCAN_HandleTypeDef * handle, uint32_t const filter_index, uint32_t const id, uint32_t const mask, bool const is_extended_id);
8787
unsigned char can_rderror(FDCAN_HandleTypeDef * handle);

include/gpio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ void gpio_init();
3333

3434
void gpio_set_initial_config();
3535

36-
void gpio_handle_data();
36+
int gpio_handle_data();
3737

3838
#endif //GPIO_H

include/rtc.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,5 @@ struct rtc_time {
4444
**************************************************************************************/
4545

4646
void rtc_init();
47-
void rtc_set_date(uint8_t *data);
48-
void rtc_get_date(uint8_t *data);
4947

5048
#endif //RTC_H

include/spi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ void spi_init();
3333

3434
void spi_end();
3535

36-
void spi_transmit_receive(uint8_t peripheral, uint8_t *tx_buf, uint8_t *rx_buf, uint16_t size);
36+
void spi_transmit_receive(uint8_t * tx_buf, uint8_t * rx_buf, uint16_t size);
3737

3838
#endif //SPI_H

include/system.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,16 @@ void system_init();
6565

6666
void dma_init();
6767

68-
void enqueue_packet(uint8_t peripheral, uint8_t opcode, uint16_t size, void* data);
68+
int enqueue_packet(uint8_t const peripheral, uint8_t const opcode, uint16_t const size, void * data);
69+
void set_nirq_low();
70+
void set_nirq_high();
71+
uint16_t get_tx_packet_size();
72+
bool is_dma_transfer_complete();
6973

70-
void dispatchPacket(uint8_t peripheral, uint8_t opcode, uint16_t size, uint8_t* data);
71-
72-
struct complete_packet* get_dma_packet();
73-
74-
int get_dma_packet_size();
7574

7675
void dma_handle_data();
7776

78-
typedef void (*PeriphCallbackFunc) (uint8_t opcode, uint8_t *data, uint16_t size);
77+
typedef int (*PeriphCallbackFunc) (uint8_t opcode, uint8_t *data, uint16_t size);
7978
void register_peripheral_callback(uint8_t peripheral, PeriphCallbackFunc func);
8079

8180
#endif //SYSTEM_H

include/uart.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ int uart_write_with_timeout(uint8_t *data, uint16_t size, uint32_t timeout);
5151

5252
int uart_data_available();
5353

54-
void uart_handle_data();
54+
int uart_handle_data();
5555

5656
int virtual_uart_data_available();
5757

58-
void virtual_uart_handle_data();
58+
int virtual_uart_handle_data();
5959

6060
void UART2_enable_rx_irq();
6161

src/adc.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,30 @@ static void MX_ADC1_Init(void);
6363
static void MX_ADC2_Init(void);
6464
static void MX_ADC3_Init(void);
6565

66+
static int get_adc_value(enum AnalogPins name);
67+
6668
/**************************************************************************************
6769
* FUNCTION DEFINITION
6870
**************************************************************************************/
6971

70-
void adc_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
71-
if (opcode == CONFIGURE) {
72+
int adc_handler(uint8_t opcode, uint8_t *data, uint16_t size)
73+
{
74+
if (opcode == CONFIGURE)
75+
{
7276
/* Note: ADC currently only supports polling mode.
7377
* uint16_t adc_sample_rate = *((uint16_t*)data);
7478
* dbg_printf("Setting ADC samplerate to %d milliseconds\n", adc_sample_rate);
7579
*/
76-
} else if ((opcode >= A0) && (opcode <= A7)) {
77-
get_adc_value(opcode);
78-
} else {
79-
dbg_printf("Invalid ADC opcode %02x\n", opcode);
80+
return 0;
81+
}
82+
else if ((opcode >= A0) && (opcode <= A7))
83+
{
84+
return get_adc_value(opcode);
85+
}
86+
else
87+
{
88+
dbg_printf("adc_handler: invalid ADC opcode %02x\n", opcode);
89+
return 0;
8090
}
8191
}
8292

@@ -89,7 +99,7 @@ void adc_init() {
8999
register_peripheral_callback(PERIPH_ADC, &adc_handler);
90100
}
91101

92-
void get_adc_value(enum AnalogPins name) {
102+
int get_adc_value(enum AnalogPins name) {
93103
ADC_ChannelConfTypeDef conf = {0};
94104
ADC_HandleTypeDef* peripheral;
95105

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

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

112-
enqueue_packet(PERIPH_ADC, name, sizeof(value), &value);
122+
return enqueue_packet(PERIPH_ADC, name, sizeof(value), &value);
113123
}
114124

115125
static void MX_ADC1_Init(void) {

src/can.c

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ static void error(char* string) {
155155
while (1);
156156
}
157157

158-
void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
158+
int fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
159159
if (opcode == CONFIGURE)
160160
{
161161
uint32_t const can_bitrate = *((uint32_t *)data);
162162
can_frequency(&fdcan_1, can_bitrate);
163163
dbg_printf("fdcan1_handler: configuring fdcan1 with frequency %ld\n", can_bitrate);
164+
return 0;
164165
}
165166
else if (opcode == CAN_FILTER)
166167
{
@@ -175,27 +176,31 @@ void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
175176
{
176177
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);
177178
}
179+
return 0;
178180
}
179181
else if (opcode == CAN_TX_FRAME)
180182
{
181183
union x8h7_can_frame_message msg;
182184
memcpy(&msg, data, size);
183185

184-
dbg_printf("fdcan1_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n", msg.id, msg.len, msg.data[0]);
185-
186-
can_write(&fdcan_1, &msg);
186+
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]);
187+
return can_write(&fdcan_1, &msg);
187188
}
188-
else {
189+
else
190+
{
189191
dbg_printf("fdcan1_handler: error invalid opcode (:%d)\n", opcode);
192+
return 0;
190193
}
191194
}
192195

193-
void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
196+
int fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size)
197+
{
194198
if (opcode == CONFIGURE)
195199
{
196200
uint32_t const can_bitrate = *((uint32_t *)data);
197201
can_frequency(&fdcan_2, can_bitrate);
198202
dbg_printf("fdcan2_handler: configuring fdcan2 with frequency %ld\n", can_bitrate);
203+
return 0;
199204
}
200205
else if (opcode == CAN_FILTER)
201206
{
@@ -210,18 +215,20 @@ void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
210215
{
211216
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);
212217
}
218+
return 0;
213219
}
214220
else if (opcode == CAN_TX_FRAME)
215221
{
216222
union x8h7_can_frame_message msg;
217223
memcpy(&msg, data, size);
218224

219-
dbg_printf("fdcan2_handler: sending CAN message to %x, size %d, content[0]=0x%02X\n", msg.id, msg.len, msg.data[0]);
220-
221-
can_write(&fdcan_2, &msg);
225+
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]);
226+
return can_write(&fdcan_2, &msg);
222227
}
223-
else {
228+
else
229+
{
224230
dbg_printf("fdcan2_handler: error invalid opcode (:%d)\n", opcode);
231+
return 0;
225232
}
226233
}
227234

@@ -251,25 +258,28 @@ void can_init()
251258
register_peripheral_callback(PERIPH_FDCAN2, &fdcan2_handler);
252259
}
253260

254-
void can_handle_data()
261+
int can_handle_data()
255262
{
263+
int bytes_enqueued = 0;
256264
union x8h7_can_frame_message msg;
257265

258-
if (can_read(&fdcan_1, &msg))
266+
/* Note: the last read package is lost in this implementation. We need to fix this by
267+
* implementing some peek method or by buffering messages in a ringbuffer.
268+
*/
269+
270+
for (int rc_enq = 0; can_read(&fdcan_1, &msg); bytes_enqueued += rc_enq)
259271
{
260-
enqueue_packet(PERIPH_FDCAN1,
261-
CAN_RX_FRAME,
262-
X8H7_CAN_HEADER_SIZE + msg.field.len,
263-
msg.buf);
272+
rc_enq = enqueue_packet(PERIPH_FDCAN1, CAN_RX_FRAME, X8H7_CAN_HEADER_SIZE + msg.field.len, msg.buf);
273+
if (!rc_enq) return bytes_enqueued;
264274
}
265275

266-
if (can_read(&fdcan_2, &msg))
276+
for (int rc_enq = 0; can_read(&fdcan_2, &msg); bytes_enqueued += rc_enq)
267277
{
268-
enqueue_packet(PERIPH_FDCAN2,
269-
CAN_RX_FRAME,
270-
X8H7_CAN_HEADER_SIZE + msg.field.len,
271-
msg.buf);
278+
rc_enq = enqueue_packet(PERIPH_FDCAN2, CAN_RX_FRAME, X8H7_CAN_HEADER_SIZE + msg.field.len, msg.buf);
279+
if (!rc_enq) return bytes_enqueued;
272280
}
281+
282+
return bytes_enqueued;
273283
}
274284

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

407417

408-
void can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg)
418+
int can_write(FDCAN_HandleTypeDef * handle, union x8h7_can_frame_message const * msg)
409419
{
410420
FDCAN_TxHeaderTypeDef TxHeader = {0};
411421

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

458-
enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
468+
return enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
459469
}
460470
else
461471
{
462472
uint8_t msg[2] = {X8H7_CAN_STS_INT_TX, 0};
463-
enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
473+
return enqueue_packet(handle == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
464474
}
465475
}
466476

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

471-
if (HAL_FDCAN_GetRxFifoFillLevel(handle, FDCAN_RX_FIFO0) == 0) {
472-
return 0; // No message arrived
473-
}
481+
if (HAL_FDCAN_GetRxFifoFillLevel(handle, FDCAN_RX_FIFO0) == 0)
482+
return 0; // No message arrived
474483

475484
FDCAN_RxHeaderTypeDef RxHeader = {0};
476485
uint8_t RxData[64] = {0};

src/crc32.c

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)