Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround (for reference): Avoid race lock up in usart #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libmaple/usart.c
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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