From 3c0a6fc53ba1d21367309d6e6ed36e87dc33d10c Mon Sep 17 00:00:00 2001 From: Ethan Zonca Date: Fri, 13 Jan 2023 14:50:52 -0500 Subject: [PATCH] Disable interrupts during critical period --- inc/system.h | 3 +++ src/system.c | 17 +++++++++++++++++ src/usbd_cdc_if.c | 2 ++ 3 files changed, 22 insertions(+) diff --git a/inc/system.h b/inc/system.h index 48d7884..7fb639a 100644 --- a/inc/system.h +++ b/inc/system.h @@ -4,5 +4,8 @@ void system_init(void); void system_hex32(char *out, uint32_t val); +void system_irq_enable(void); +void system_irq_disable(void); + #endif diff --git a/src/system.c b/src/system.c index b9a14ab..59712de 100644 --- a/src/system.c +++ b/src/system.c @@ -86,3 +86,20 @@ void system_hex32(char *out, uint32_t val) p--; } } + + +// Disable all interrupts +void system_irq_disable(void) +{ + __disable_irq(); + __DSB(); + __ISB(); +} + + +// Enable all interrupts +void system_irq_enable(void) +{ + __enable_irq(); +} + diff --git a/src/usbd_cdc_if.c b/src/usbd_cdc_if.c index 077003f..78e4b9a 100644 --- a/src/usbd_cdc_if.c +++ b/src/usbd_cdc_if.c @@ -167,6 +167,7 @@ static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) // Process incoming USB-CDC messages from RX FIFO void cdc_process(void) { + system_irq_disable(); if(rxbuf.tail != rxbuf.head) { // Process one whole buffer @@ -201,6 +202,7 @@ void cdc_process(void) // Move on to next buffer rxbuf.tail = (rxbuf.tail + 1) % NUM_RX_BUFS; } + system_irq_enable(); }