diff options
author | Ryan <fauxpark@gmail.com> | 2021-11-14 05:23:14 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-13 18:23:14 +0000 |
commit | 04b51e381e2ff3f4c7aba662e1c817ce5daa931d (patch) | |
tree | 3b5c4415b94184f182d11e2629b405af9b3f0c57 | |
parent | 7e86c37962935e2f791864201b684345995f0b82 (diff) | |
download | qmk_firmware-04b51e381e2ff3f4c7aba662e1c817ce5daa931d.tar.gz qmk_firmware-04b51e381e2ff3f4c7aba662e1c817ce5daa931d.zip |
Update UART driver API (#14839)
* Add uart_puts() and uart_gets()
* Add some docs
* Rework API
* Formatting
* Update docs/uart_driver.md
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
* Simplify a uart_write() loop
* Update platforms/avr/drivers/uart.c
Co-authored-by: Joel Challis <git@zvecr.com>
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
Co-authored-by: Joel Challis <git@zvecr.com>
-rw-r--r-- | docs/uart_driver.md | 38 | ||||
-rw-r--r-- | keyboards/mschwingen/modelm/modelm.c | 2 | ||||
-rw-r--r-- | keyboards/nullbitsco/common/remote_kb.c | 6 | ||||
-rw-r--r-- | paths.mk | 1 | ||||
-rw-r--r-- | platforms/avr/drivers/uart.c | 26 | ||||
-rw-r--r-- | platforms/avr/drivers/uart.h | 8 | ||||
-rw-r--r-- | platforms/chibios/drivers/uart.c | 8 | ||||
-rw-r--r-- | platforms/chibios/drivers/uart.h | 8 |
8 files changed, 72 insertions, 25 deletions
diff --git a/docs/uart_driver.md b/docs/uart_driver.md index 4d1716975..340b64818 100644 --- a/docs/uart_driver.md +++ b/docs/uart_driver.md | |||
@@ -60,30 +60,56 @@ Initialize the UART driver. This function must be called only once, before any o | |||
60 | 60 | ||
61 | --- | 61 | --- |
62 | 62 | ||
63 | ### `void uart_putchar(uint8_t c)` | 63 | ### `void uart_write(uint8_t data)` |
64 | 64 | ||
65 | Transmit a single byte. | 65 | Transmit a single byte. |
66 | 66 | ||
67 | #### Arguments | 67 | #### Arguments |
68 | 68 | ||
69 | - `uint8_t c` | 69 | - `uint8_t data` |
70 | The byte (character) to send, from 0 to 255. | 70 | The byte to write. |
71 | 71 | ||
72 | --- | 72 | --- |
73 | 73 | ||
74 | ### `uint8_t uart_getchar(void)` | 74 | ### `uint8_t uart_read(void)` |
75 | 75 | ||
76 | Receive a single byte. | 76 | Receive a single byte. |
77 | 77 | ||
78 | #### Return Value | 78 | #### Return Value |
79 | 79 | ||
80 | The byte read from the receive buffer. | 80 | The byte read from the receive buffer. This function will block if the buffer is empty (ie. no data to read). |
81 | |||
82 | --- | ||
83 | |||
84 | ### `void uart_transmit(const uint8_t *data, uint16_t length)` | ||
85 | |||
86 | Transmit multiple bytes. | ||
87 | |||
88 | #### Arguments | ||
89 | |||
90 | - `const uint8_t *data` | ||
91 | A pointer to the data to write from. | ||
92 | - `uint16_t length` | ||
93 | The number of bytes to write. Take care not to overrun the length of `data`. | ||
94 | |||
95 | --- | ||
96 | |||
97 | ### `void uart_receive(char *data, uint16_t length)` | ||
98 | |||
99 | Receive multiple bytes. | ||
100 | |||
101 | #### Arguments | ||
102 | |||
103 | - `uint8_t *data` | ||
104 | A pointer to the buffer to read into. | ||
105 | - `uint16_t length` | ||
106 | The number of bytes to read. Take care not to overrun the length of `data`. | ||
81 | 107 | ||
82 | --- | 108 | --- |
83 | 109 | ||
84 | ### `bool uart_available(void)` | 110 | ### `bool uart_available(void)` |
85 | 111 | ||
86 | Return whether the receive buffer contains data. Call this function to determine if `uart_getchar()` will return meaningful data. | 112 | Return whether the receive buffer contains data. Call this function to determine if `uart_read()` will return data immediately. |
87 | 113 | ||
88 | #### Return Value | 114 | #### Return Value |
89 | 115 | ||
diff --git a/keyboards/mschwingen/modelm/modelm.c b/keyboards/mschwingen/modelm/modelm.c index c1180612a..7dcd4ac02 100644 --- a/keyboards/mschwingen/modelm/modelm.c +++ b/keyboards/mschwingen/modelm/modelm.c | |||
@@ -27,7 +27,7 @@ | |||
27 | # undef sendchar | 27 | # undef sendchar |
28 | static int8_t capture_sendchar(uint8_t c) { | 28 | static int8_t capture_sendchar(uint8_t c) { |
29 | // sendchar(c); | 29 | // sendchar(c); |
30 | uart_putchar(c); | 30 | uart_write(c); |
31 | return 0; | 31 | return 0; |
32 | } | 32 | } |
33 | #endif | 33 | #endif |
diff --git a/keyboards/nullbitsco/common/remote_kb.c b/keyboards/nullbitsco/common/remote_kb.c index 89cbf9a92..4dcc9f461 100644 --- a/keyboards/nullbitsco/common/remote_kb.c +++ b/keyboards/nullbitsco/common/remote_kb.c | |||
@@ -63,9 +63,7 @@ static void send_msg(uint16_t keycode, bool pressed) { | |||
63 | msg[IDX_PRESSED] = pressed; | 63 | msg[IDX_PRESSED] = pressed; |
64 | msg[IDX_CHECKSUM] = chksum8(msg, UART_MSG_LEN-1); | 64 | msg[IDX_CHECKSUM] = chksum8(msg, UART_MSG_LEN-1); |
65 | 65 | ||
66 | for (int i=0; i<UART_MSG_LEN; i++) { | 66 | uart_transmit(msg, UART_MSG_LEN); |
67 | uart_putchar(msg[i]); | ||
68 | } | ||
69 | } | 67 | } |
70 | 68 | ||
71 | static void print_message_buffer(void) { | 69 | static void print_message_buffer(void) { |
@@ -103,7 +101,7 @@ static void process_uart(void) { | |||
103 | 101 | ||
104 | static void get_msg(void) { | 102 | static void get_msg(void) { |
105 | while (uart_available()) { | 103 | while (uart_available()) { |
106 | msg[msg_idx] = uart_getchar(); | 104 | msg[msg_idx] = uart_read(); |
107 | dprintf("idx: %u, recv: %u\n", msg_idx, msg[msg_idx]); | 105 | dprintf("idx: %u, recv: %u\n", msg_idx, msg[msg_idx]); |
108 | if (msg_idx == 0 && (msg[msg_idx] != UART_PREAMBLE)) { | 106 | if (msg_idx == 0 && (msg[msg_idx] != UART_PREAMBLE)) { |
109 | dprintf("Byte sync error!\n"); | 107 | dprintf("Byte sync error!\n"); |
@@ -24,6 +24,5 @@ COMMON_VPATH += $(QUANTUM_PATH) | |||
24 | COMMON_VPATH += $(QUANTUM_PATH)/keymap_extras | 24 | COMMON_VPATH += $(QUANTUM_PATH)/keymap_extras |
25 | COMMON_VPATH += $(QUANTUM_PATH)/audio | 25 | COMMON_VPATH += $(QUANTUM_PATH)/audio |
26 | COMMON_VPATH += $(QUANTUM_PATH)/process_keycode | 26 | COMMON_VPATH += $(QUANTUM_PATH)/process_keycode |
27 | COMMON_VPATH += $(QUANTUM_PATH)/api | ||
28 | COMMON_VPATH += $(QUANTUM_PATH)/sequencer | 27 | COMMON_VPATH += $(QUANTUM_PATH)/sequencer |
29 | COMMON_VPATH += $(DRIVER_PATH) | 28 | COMMON_VPATH += $(DRIVER_PATH) |
diff --git a/platforms/avr/drivers/uart.c b/platforms/avr/drivers/uart.c index c6abcb6fe..01cf6b1fb 100644 --- a/platforms/avr/drivers/uart.c +++ b/platforms/avr/drivers/uart.c | |||
@@ -100,7 +100,7 @@ void uart_init(uint32_t baud) { | |||
100 | } | 100 | } |
101 | 101 | ||
102 | // Transmit a byte | 102 | // Transmit a byte |
103 | void uart_putchar(uint8_t c) { | 103 | void uart_write(uint8_t data) { |
104 | uint8_t i; | 104 | uint8_t i; |
105 | 105 | ||
106 | i = tx_buffer_head + 1; | 106 | i = tx_buffer_head + 1; |
@@ -110,27 +110,39 @@ void uart_putchar(uint8_t c) { | |||
110 | while (tx_buffer_tail == i) | 110 | while (tx_buffer_tail == i) |
111 | ; // wait until space in buffer | 111 | ; // wait until space in buffer |
112 | // cli(); | 112 | // cli(); |
113 | tx_buffer[i] = c; | 113 | tx_buffer[i] = data; |
114 | tx_buffer_head = i; | 114 | tx_buffer_head = i; |
115 | UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn) | (1 << UDRIEn); | 115 | UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn) | (1 << UDRIEn); |
116 | // sei(); | 116 | // sei(); |
117 | } | 117 | } |
118 | 118 | ||
119 | // Receive a byte | 119 | // Receive a byte |
120 | uint8_t uart_getchar(void) { | 120 | uint8_t uart_read(void) { |
121 | uint8_t c, i; | 121 | uint8_t data, i; |
122 | 122 | ||
123 | while (rx_buffer_head == rx_buffer_tail) | 123 | while (rx_buffer_head == rx_buffer_tail) |
124 | ; // wait for character | 124 | ; // wait for character |
125 | i = rx_buffer_tail + 1; | 125 | i = rx_buffer_tail + 1; |
126 | if (i >= RX_BUFFER_SIZE) i = 0; | 126 | if (i >= RX_BUFFER_SIZE) i = 0; |
127 | c = rx_buffer[i]; | 127 | data = rx_buffer[i]; |
128 | rx_buffer_tail = i; | 128 | rx_buffer_tail = i; |
129 | return c; | 129 | return data; |
130 | } | ||
131 | |||
132 | void uart_transmit(const uint8_t *data, uint16_t length) { | ||
133 | for (uint16_t i = 0; i < length; i++) { | ||
134 | uart_write(data[i]); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | void uart_receive(uint8_t *data, uint16_t length) { | ||
139 | for (uint16_t i = 0; i < length; i++) { | ||
140 | data[i] = uart_read(); | ||
141 | } | ||
130 | } | 142 | } |
131 | 143 | ||
132 | // Return whether the number of bytes waiting in the receive buffer is nonzero. | 144 | // Return whether the number of bytes waiting in the receive buffer is nonzero. |
133 | // Call this before uart_getchar() to check if it will need | 145 | // Call this before uart_read() to check if it will need |
134 | // to wait for a byte to arrive. | 146 | // to wait for a byte to arrive. |
135 | bool uart_available(void) { | 147 | bool uart_available(void) { |
136 | uint8_t head, tail; | 148 | uint8_t head, tail; |
diff --git a/platforms/avr/drivers/uart.h b/platforms/avr/drivers/uart.h index 602eb3d8b..9cb7652b0 100644 --- a/platforms/avr/drivers/uart.h +++ b/platforms/avr/drivers/uart.h | |||
@@ -28,8 +28,12 @@ | |||
28 | 28 | ||
29 | void uart_init(uint32_t baud); | 29 | void uart_init(uint32_t baud); |
30 | 30 | ||
31 | void uart_putchar(uint8_t c); | 31 | void uart_write(uint8_t data); |
32 | 32 | ||
33 | uint8_t uart_getchar(void); | 33 | uint8_t uart_read(void); |
34 | |||
35 | void uart_transmit(const char *data, uint16_t length); | ||
36 | |||
37 | void uart_receive(char *data, uint16_t length); | ||
34 | 38 | ||
35 | bool uart_available(void); | 39 | bool uart_available(void); |
diff --git a/platforms/chibios/drivers/uart.c b/platforms/chibios/drivers/uart.c index 0e8e0515a..297c1892c 100644 --- a/platforms/chibios/drivers/uart.c +++ b/platforms/chibios/drivers/uart.c | |||
@@ -39,12 +39,16 @@ void uart_init(uint32_t baud) { | |||
39 | } | 39 | } |
40 | } | 40 | } |
41 | 41 | ||
42 | void uart_putchar(uint8_t c) { sdPut(&SERIAL_DRIVER, c); } | 42 | void uart_write(uint8_t data) { sdPut(&SERIAL_DRIVER, c); } |
43 | 43 | ||
44 | uint8_t uart_getchar(void) { | 44 | uint8_t uart_read(void) { |
45 | msg_t res = sdGet(&SERIAL_DRIVER); | 45 | msg_t res = sdGet(&SERIAL_DRIVER); |
46 | 46 | ||
47 | return (uint8_t)res; | 47 | return (uint8_t)res; |
48 | } | 48 | } |
49 | 49 | ||
50 | void uart_transmit(const uint8_t *data, uint16_t length) { sdWrite(&SERIAL_DRIVER, data, length); } | ||
51 | |||
52 | void uart_receive(uint8_t *data, uint16_t length) { sdRead(&SERIAL_DRIVER, data, length); } | ||
53 | |||
50 | bool uart_available(void) { return !sdGetWouldBlock(&SERIAL_DRIVER); } | 54 | bool uart_available(void) { return !sdGetWouldBlock(&SERIAL_DRIVER); } |
diff --git a/platforms/chibios/drivers/uart.h b/platforms/chibios/drivers/uart.h index b4e20e9fd..5bc487590 100644 --- a/platforms/chibios/drivers/uart.h +++ b/platforms/chibios/drivers/uart.h | |||
@@ -70,8 +70,12 @@ | |||
70 | 70 | ||
71 | void uart_init(uint32_t baud); | 71 | void uart_init(uint32_t baud); |
72 | 72 | ||
73 | void uart_putchar(uint8_t c); | 73 | void uart_write(uint8_t data); |
74 | 74 | ||
75 | uint8_t uart_getchar(void); | 75 | uint8_t uart_read(void); |
76 | |||
77 | void uart_transmit(const uint8_t *data, uint16_t length); | ||
78 | |||
79 | void uart_receive(uint8_t *data, uint16_t length); | ||
76 | 80 | ||
77 | bool uart_available(void); | 81 | bool uart_available(void); |