|
@@ -6,6 +6,8 @@ date: 2025/01/30
|
|
|
licence: MIT
|
|
|
---
|
|
|
|
|
|
+[Back](CooperativeMultitasking.html)
|
|
|
+
|
|
|
> 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
|
|
|
> fit it into the *event-dispatcher* system pretty easily.
|
|
@@ -64,12 +66,12 @@ be sent or processed:
|
|
|
|
|
|
/* 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;
|
|
|
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;
|
|
|
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
|
|
|
(-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.
|
|
|
|
|
|
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 */
|
|
|
|
|
|
-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;
|
|
|
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;
|
|
|
const char * const rx_end = rx_buffer + RX_SIZE;
|
|
|
```
|
|
@@ -173,6 +176,29 @@ void uartDiscardChar(int i) {
|
|
|
uartGetChar();
|
|
|
}
|
|
|
|
|
|
+/** add character to the tx buffer
|
|
|
+ * @return -1 if the buffer is full
|
|
|
+ * */
|
|
|
+char uartSendChar(char c) {
|
|
|
+ *tx_next_empty = c; // increment AFTER adding to queue
|
|
|
+ if (++tx_next_empty >= tx_end)
|
|
|
+ tx_next_empty = tx_buffer;
|
|
|
+ if (tx_next_empty == tx_next_send)
|
|
|
+ c = -1;
|
|
|
+ // if the USART is empty, trigger the first send
|
|
|
+ if (USART2->ISR & 0x80)
|
|
|
+ USART2->CR1 |= 0x80; // enable the tx interrupt
|
|
|
+ return c;
|
|
|
+}
|
|
|
+
|
|
|
+/** send a string a specified length out the uart */
|
|
|
+void uartSendString(const char* p, int len) {
|
|
|
+ while (len--)
|
|
|
+ uartSendChar(*p++);
|
|
|
+}
|
|
|
+
|
|
|
+/* ============ INTERRUPTS =================== */
|
|
|
+
|
|
|
/* the for a RX character
|
|
|
* deposit it into the buffer, issue a EVT_CHAR and return
|
|
|
*/
|
|
@@ -184,6 +210,7 @@ void uart_rx_isr(void) {
|
|
|
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
|
|
@@ -197,26 +224,5 @@ void uart_tx_isr(void) {
|
|
|
USART2->CR1 &= ~0x80;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-/** add character to the tx buffer
|
|
|
- * @return -1 if the buffer is full
|
|
|
- * */
|
|
|
-char uartSendChar(char c) {
|
|
|
- *tx_next_empty = c; // increment AFTER adding to queue
|
|
|
- if (++tx_next_empty >= tx_end)
|
|
|
- tx_next_empty = tx_buffer;
|
|
|
- if (tx_next_empty == tx_next_send)
|
|
|
- c = -1;
|
|
|
- // if the USART is empty, trigger the first send
|
|
|
- if (USART2->ISR & 0x80)
|
|
|
- USART2->CR1 |= 0x80; // enable the tx interrupt
|
|
|
- return c;
|
|
|
-}
|
|
|
-
|
|
|
-/** send a string a specified length out the uart */
|
|
|
-void uartSendString(const char* p, int len) {
|
|
|
- while (len--)
|
|
|
- uartSendChar(*p++);
|
|
|
-}
|
|
|
```
|
|
|
|