-
Notifications
You must be signed in to change notification settings - Fork 513
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
Serial.availableForWrite() and Serial.blockOnOverrun() implementation, USB Serial fixes for STM32F2 #812
Merged
Merged
Serial.availableForWrite() and Serial.blockOnOverrun() implementation, USB Serial fixes for STM32F2 #812
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
30589c6
Serial.availableForWrite() and Serial.blockOnOverrun() implementation…
avtolstoy ee84259
Safeguards in USB Serial (STM32F2) against non-atomic modifications t…
avtolstoy 431bf44
flush() implemntation for USB Serial on STM32F2, proper detection of …
avtolstoy 88ecb24
Proper USB serial deinitialization on STM32F2
avtolstoy 4740831
Fixes issue with USB reattachment
avtolstoy 6c97bd2
Merge remote-tracking branch 'upstream/develop' into serialfixes
avtolstoy c8fa49a
Detach USB before entering STOP mode and reattach after exiting
avtolstoy 5985028
Merge remote-tracking branch 'upstream/develop' into serialfixes
avtolstoy eeaebd4
Remove delay in USB_USART_LineCoding_BitRate_Handler
avtolstoy 3f495bf
USARTs should be flushed before going into STOP mode as busy USART mi…
avtolstoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,10 +54,13 @@ extern uint32_t USBD_OTG_EP1OUT_ISR_Handler(USB_OTG_CORE_HANDLE *pdev); | |
extern volatile LINE_CODING linecoding; | ||
extern volatile uint8_t USB_DEVICE_CONFIGURED; | ||
extern volatile uint8_t USB_Rx_Buffer[]; | ||
extern volatile uint8_t APP_Rx_Buffer[]; | ||
extern volatile uint32_t APP_Rx_ptr_in; | ||
extern volatile uint16_t USB_Rx_length; | ||
extern volatile uint16_t USB_Rx_ptr; | ||
extern volatile uint32_t USB_Rx_Buffer_head; | ||
extern volatile uint32_t USB_Rx_Buffer_tail; | ||
extern volatile uint32_t USB_Rx_Buffer_length; | ||
extern volatile uint8_t USB_Tx_Buffer[]; | ||
extern volatile uint32_t USB_Tx_Buffer_head; | ||
extern volatile uint32_t USB_Tx_Buffer_tail; | ||
extern volatile uint32_t USB_Tx_Buffer_length; | ||
extern volatile uint8_t USB_Tx_State; | ||
extern volatile uint8_t USB_Rx_State; | ||
#endif | ||
|
@@ -140,6 +143,10 @@ void USB_USART_LineCoding_BitRate_Handler(void (*handler)(uint32_t bitRate)) | |
SetLineCodingBitRateHandler(handler); | ||
} | ||
|
||
static inline bool USB_USART_Connected() { | ||
return linecoding.bitrate > 0 && USB_OTG_dev.dev.device_status == USB_OTG_CONFIGURED; | ||
} | ||
|
||
/******************************************************************************* | ||
* Function Name : USB_USART_Available_Data. | ||
* Description : Return the length of available data received from USB. | ||
|
@@ -148,9 +155,14 @@ void USB_USART_LineCoding_BitRate_Handler(void (*handler)(uint32_t bitRate)) | |
*******************************************************************************/ | ||
uint8_t USB_USART_Available_Data(void) | ||
{ | ||
if(USB_Rx_State == 1) | ||
if (USB_USART_Connected()) | ||
{ | ||
return (USB_Rx_length - USB_Rx_ptr); | ||
int32_t available = 0; | ||
if (USB_Rx_Buffer_head >= USB_Rx_Buffer_tail) | ||
available = USB_Rx_Buffer_head - USB_Rx_Buffer_tail; | ||
else | ||
available = USB_Rx_Buffer_length + USB_Rx_Buffer_head - USB_Rx_Buffer_tail; | ||
return available; | ||
} | ||
|
||
return 0; | ||
|
@@ -164,20 +176,34 @@ uint8_t USB_USART_Available_Data(void) | |
*******************************************************************************/ | ||
int32_t USB_USART_Receive_Data(uint8_t peek) | ||
{ | ||
if(USB_Rx_State == 1) | ||
if (USB_USART_Available_Data() > 0) | ||
{ | ||
if(!peek && ((USB_Rx_length - USB_Rx_ptr) == 1)) | ||
{ | ||
USB_Rx_State = 0; | ||
|
||
/* Prepare Out endpoint to receive next packet */ | ||
DCD_EP_PrepareRx(&USB_OTG_dev, | ||
CDC_OUT_EP, | ||
(uint8_t*)(USB_Rx_Buffer), | ||
CDC_DATA_OUT_PACKET_SIZE); | ||
uint32_t tail = USB_Rx_Buffer_tail; | ||
uint8_t data = USB_Rx_Buffer[USB_Rx_Buffer_tail]; | ||
if (!peek) { | ||
tail++; | ||
if (tail == USB_Rx_Buffer_length) | ||
tail = 0; | ||
USB_Rx_Buffer_tail = tail; | ||
} | ||
return data; | ||
} | ||
|
||
return USB_Rx_Buffer[peek ? USB_Rx_ptr : USB_Rx_ptr++]; | ||
return -1; | ||
} | ||
|
||
/******************************************************************************* | ||
* Function Name : USB_USART_Available_Data_For_Write. | ||
* Description : Return the length of available space in TX buffer | ||
* Input : None. | ||
* Return : Length. | ||
*******************************************************************************/ | ||
int32_t USB_USART_Available_Data_For_Write(void) | ||
{ | ||
if (USB_USART_Connected()) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here - because |
||
return USB_TX_BUFFER_SIZE - (USB_Tx_Buffer_head >= USB_Tx_Buffer_tail ? | ||
USB_Tx_Buffer_head - USB_Tx_Buffer_tail : USB_TX_BUFFER_SIZE + USB_Tx_Buffer_head - USB_Tx_Buffer_tail) - 1; | ||
} | ||
|
||
return -1; | ||
|
@@ -191,18 +217,20 @@ int32_t USB_USART_Receive_Data(uint8_t peek) | |
*******************************************************************************/ | ||
void USB_USART_Send_Data(uint8_t Data) | ||
{ | ||
APP_Rx_Buffer[APP_Rx_ptr_in] = Data; | ||
int32_t available = 0; | ||
do { | ||
available = USB_USART_Available_Data_For_Write(); | ||
} | ||
while (available < 1 && available != -1); | ||
// Confirm once again that the Host is connected | ||
if (USB_USART_Connected()) | ||
{ | ||
uint32_t head = USB_Tx_Buffer_head; | ||
|
||
APP_Rx_ptr_in++; | ||
USB_Tx_Buffer[USB_Tx_Buffer_head] = Data; | ||
|
||
/* To avoid buffer overflow */ | ||
if(APP_Rx_ptr_in == APP_RX_DATA_SIZE) | ||
{ | ||
APP_Rx_ptr_in = 0; | ||
USB_Tx_Buffer_head = ++head % USB_TX_BUFFER_SIZE; | ||
} | ||
|
||
//Delay 100us to avoid losing the data | ||
HAL_Delay_Microseconds(100); | ||
} | ||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned that USB_Rx_Buffer_head / USB_Rx_Buffer_tail will be changed by the USB IRQ after being tested.
As a minimum, capture the tail and head into a local variables, and use that so we are certain they are consistent.
A context switch after comparing head >= tail could see head roll over to the start of the buffer, which would then result in the incorrect size being returned.