diff --git a/libmaple/usart.c b/libmaple/usart.c index 0bdc37ae8..7d6e7ca57 100644 --- a/libmaple/usart.c +++ b/libmaple/usart.c @@ -102,6 +102,11 @@ void usart_init(usart_dev *dev) { rb_init(dev->rb, USART_RX_BUF_SIZE, dev->rx_buf); rcc_clk_enable(dev->clk_id); nvic_irq_enable(dev->irq_num); + + /* Set to max priority to avoid race condition where register presumably + * might change before it can be picked out by USART interrupt and cause + * it to lock up. */ + nvic_irq_set_priority (dev->irq_num, 0); } /** diff --git a/libmaple/usart.h b/libmaple/usart.h index ed00e16cb..4b55b280f 100644 --- a/libmaple/usart.h +++ b/libmaple/usart.h @@ -309,7 +309,9 @@ static inline void usart_putstr(usart_dev *dev, const char* str) { * @see usart_data_available() */ static inline uint8 usart_getc(usart_dev *dev) { - return rb_remove(dev->rb); + /* Use rb_safe_remove to avoid race in case another interrupt have reset + * or cleared the ringbuffer */ + return rb_safe_remove (dev->rb); } /**