mirror of
https://wiki.pbeirne.com/patb/cmt.git
synced 2026-12-03 12:05:20 -05:00
fixup in uart.md (clean code, erase comments, grammar)
This commit is contained in:
@@ -6,6 +6,8 @@ date: 2025/01/30
|
|||||||
licence: MIT
|
licence: MIT
|
||||||
---
|
---
|
||||||
|
|
||||||
|
[Back](CooperativeMultitasking.html)
|
||||||
|
|
||||||
> This is not a state machine, per se. This page is included because
|
> This is not a state machine, per se. This page is included because
|
||||||
> I found it very common to need UART communication, and I found I could
|
> I found it very common to need UART communication, and I found I could
|
||||||
> fit it into the *event-dispatcher* system pretty easily.
|
> fit it into the *event-dispatcher* system pretty easily.
|
||||||
@@ -64,12 +66,12 @@ be sent or processed:
|
|||||||
|
|
||||||
/* circular buffers */
|
/* circular buffers */
|
||||||
|
|
||||||
char tx_buffer[TX_SIZE]; // the largest packet we need to tx is 50 bytes
|
char tx_buffer[TX_SIZE];
|
||||||
char *tx_next_empty = tx_buffer, *tx_next_send = tx_buffer;
|
char *tx_next_empty = tx_buffer, *tx_next_send = tx_buffer;
|
||||||
const char * const tx_end = tx_buffer + TX_SIZE;
|
const char * const tx_end = tx_buffer + TX_SIZE;
|
||||||
|
|
||||||
|
|
||||||
char rx_buffer[RX_SIZE]; // the largest packet we need to rx is 192 bytes
|
char rx_buffer[RX_SIZE];
|
||||||
char *rx_next_empty = rx_buffer, *rx_next_rcv = rx_buffer;
|
char *rx_next_empty = rx_buffer, *rx_next_rcv = rx_buffer;
|
||||||
const char * const rx_end = rx_buffer + RX_SIZE;
|
const char * const rx_end = rx_buffer + RX_SIZE;
|
||||||
```
|
```
|
||||||
@@ -105,7 +107,8 @@ is ready.
|
|||||||
|
|
||||||
The `uartGetChar()` checks the rx buffer, and returns a char if one is available
|
The `uartGetChar()` checks the rx buffer, and returns a char if one is available
|
||||||
(-1 otherwise). The `uartCheckChar()` and `uartGetCharCount()`
|
(-1 otherwise). The `uartCheckChar()` and `uartGetCharCount()`
|
||||||
let the designer check the first character of a command string, and determine
|
let the designer check the first character of a command string, decide if
|
||||||
|
it is relevant to a specific task, plus determine
|
||||||
whether the command has been fully loaded into the rxBuffer.
|
whether the command has been fully loaded into the rxBuffer.
|
||||||
|
|
||||||
Here is a fully functional module that implements the above:
|
Here is a fully functional module that implements the above:
|
||||||
@@ -117,12 +120,12 @@ Here is a fully functional module that implements the above:
|
|||||||
|
|
||||||
/* circular buffers */
|
/* circular buffers */
|
||||||
|
|
||||||
char tx_buffer[TX_SIZE]; // the largest packet we need to tx is 50 bytes
|
char tx_buffer[TX_SIZE];
|
||||||
char *tx_next_empty = tx_buffer, *tx_next_send = tx_buffer;
|
char *tx_next_empty = tx_buffer, *tx_next_send = tx_buffer;
|
||||||
const char * const tx_end = tx_buffer + TX_SIZE;
|
const char * const tx_end = tx_buffer + TX_SIZE;
|
||||||
|
|
||||||
|
|
||||||
char rx_buffer[RX_SIZE]; // the largest packet we need to rx is 192 bytes
|
char rx_buffer[RX_SIZE];
|
||||||
char *rx_next_empty = rx_buffer, *rx_next_rcv = rx_buffer;
|
char *rx_next_empty = rx_buffer, *rx_next_rcv = rx_buffer;
|
||||||
const char * const rx_end = rx_buffer + RX_SIZE;
|
const char * const rx_end = rx_buffer + RX_SIZE;
|
||||||
```
|
```
|
||||||
@@ -173,31 +176,6 @@ void uartDiscardChar(int i) {
|
|||||||
uartGetChar();
|
uartGetChar();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the for a RX character
|
|
||||||
* deposit it into the buffer, issue a EVT_CHAR and return
|
|
||||||
*/
|
|
||||||
void uart_rx_isr(void) {
|
|
||||||
// rx data is ready, read it.....status bit auto-clears
|
|
||||||
*rx_next_empty++ = USART2->RDR;
|
|
||||||
if (rx_next_empty >= rx_end) // wrap back to beginning
|
|
||||||
rx_next_empty = rx_buffer;
|
|
||||||
newEvent(EVT_CHAR); // inform all tasks that there is
|
|
||||||
// something in the rx buffer
|
|
||||||
}
|
|
||||||
void uart_tx_isr(void) {
|
|
||||||
// the uart tx is empty
|
|
||||||
// if there is tx data available to send, push it into the uart
|
|
||||||
if (tx_next_send != tx_next_empty) {
|
|
||||||
USART2->TDR = *tx_next_send;
|
|
||||||
if (++tx_next_send >= tx_end) // wrap back to beginning
|
|
||||||
tx_next_send = tx_buffer;
|
|
||||||
}
|
|
||||||
// else, just disable the interrupt
|
|
||||||
else
|
|
||||||
USART2->CR1 &= ~0x80;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** add character to the tx buffer
|
/** add character to the tx buffer
|
||||||
* @return -1 if the buffer is full
|
* @return -1 if the buffer is full
|
||||||
* */
|
* */
|
||||||
@@ -218,5 +196,33 @@ void uartSendString(const char* p, int len) {
|
|||||||
while (len--)
|
while (len--)
|
||||||
uartSendChar(*p++);
|
uartSendChar(*p++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ============ INTERRUPTS =================== */
|
||||||
|
|
||||||
|
/* the for a RX character
|
||||||
|
* deposit it into the buffer, issue a EVT_CHAR and return
|
||||||
|
*/
|
||||||
|
void uart_rx_isr(void) {
|
||||||
|
// rx data is ready, read it.....status bit auto-clears
|
||||||
|
*rx_next_empty++ = USART2->RDR;
|
||||||
|
if (rx_next_empty >= rx_end) // wrap back to beginning
|
||||||
|
rx_next_empty = rx_buffer;
|
||||||
|
newEvent(EVT_CHAR); // inform all tasks that there is
|
||||||
|
// something in the rx buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
void uart_tx_isr(void) {
|
||||||
|
// the uart tx is empty
|
||||||
|
// if there is tx data available to send, push it into the uart
|
||||||
|
if (tx_next_send != tx_next_empty) {
|
||||||
|
USART2->TDR = *tx_next_send;
|
||||||
|
if (++tx_next_send >= tx_end) // wrap back to beginning
|
||||||
|
tx_next_send = tx_buffer;
|
||||||
|
}
|
||||||
|
// else, just disable the interrupt
|
||||||
|
else
|
||||||
|
USART2->CR1 &= ~0x80;
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user