Skip to content

Commit

Permalink
Avoid race lock up in usart
Browse files Browse the repository at this point in the history
Long external interrupts may cause usart reads to lock up if they
prevent usart interrupts to be handled quick enough. This patch
increases usart interrupt priority and prevents the usart interrupt from
being delayed.
  • Loading branch information
gauteh committed May 17, 2012
1 parent 0c314c3 commit 1e10299
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libmaple/usart.c
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion libmaple/usart.h
Expand Up @@ -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);
}

/**
Expand Down

1 comment on commit 1e10299

@gauteh
Copy link
Owner Author

@gauteh gauteh commented on 1e10299 May 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.