aboutsummaryrefslogtreecommitdiff
path: root/drivers/chibios/serial_usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/chibios/serial_usart.c')
-rw-r--r--drivers/chibios/serial_usart.c208
1 files changed, 0 insertions, 208 deletions
diff --git a/drivers/chibios/serial_usart.c b/drivers/chibios/serial_usart.c
deleted file mode 100644
index cae29388c..000000000
--- a/drivers/chibios/serial_usart.c
+++ /dev/null
@@ -1,208 +0,0 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "serial_usart.h"
18
19#ifndef USE_GPIOV1
20// The default PAL alternate modes are used to signal that the pins are used for USART
21# ifndef SERIAL_USART_TX_PAL_MODE
22# define SERIAL_USART_TX_PAL_MODE 7
23# endif
24#endif
25
26#ifndef SERIAL_USART_DRIVER
27# define SERIAL_USART_DRIVER SD1
28#endif
29
30#ifdef SOFT_SERIAL_PIN
31# define SERIAL_USART_TX_PIN SOFT_SERIAL_PIN
32#endif
33
34static inline msg_t sdWriteHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size) {
35 msg_t ret = sdWrite(driver, data, size);
36
37 // Half duplex requires us to read back the data we just wrote - just throw it away
38 uint8_t dump[size];
39 sdRead(driver, dump, size);
40
41 return ret;
42}
43#undef sdWrite
44#define sdWrite sdWriteHalfDuplex
45
46static inline msg_t sdWriteTimeoutHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size, uint32_t timeout) {
47 msg_t ret = sdWriteTimeout(driver, data, size, timeout);
48
49 // Half duplex requires us to read back the data we just wrote - just throw it away
50 uint8_t dump[size];
51 sdReadTimeout(driver, dump, size, timeout);
52
53 return ret;
54}
55#undef sdWriteTimeout
56#define sdWriteTimeout sdWriteTimeoutHalfDuplex
57
58static inline void sdClear(SerialDriver* driver) {
59 while (sdGetTimeout(driver, TIME_IMMEDIATE) != MSG_TIMEOUT) {
60 // Do nothing with the data
61 }
62}
63
64static SerialConfig sdcfg = {
65 (SERIAL_USART_SPEED), // speed - mandatory
66 (SERIAL_USART_CR1), // CR1
67 (SERIAL_USART_CR2), // CR2
68 (SERIAL_USART_CR3) // CR3
69};
70
71void handle_soft_serial_slave(void);
72
73/*
74 * This thread runs on the slave and responds to transactions initiated
75 * by the master
76 */
77static THD_WORKING_AREA(waSlaveThread, 2048);
78static THD_FUNCTION(SlaveThread, arg) {
79 (void)arg;
80 chRegSetThreadName("slave_transport");
81
82 while (true) {
83 handle_soft_serial_slave();
84 }
85}
86
87__attribute__((weak)) void usart_init(void) {
88#if defined(USE_GPIOV1)
89 palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
90#else
91 palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
92#endif
93
94#if defined(USART_REMAP)
95 USART_REMAP;
96#endif
97}
98
99void usart_master_init(void) {
100 usart_init();
101
102 sdcfg.cr3 |= USART_CR3_HDSEL;
103 sdStart(&SERIAL_USART_DRIVER, &sdcfg);
104}
105
106void usart_slave_init(void) {
107 usart_init();
108
109 sdcfg.cr3 |= USART_CR3_HDSEL;
110 sdStart(&SERIAL_USART_DRIVER, &sdcfg);
111
112 // Start transport thread
113 chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL);
114}
115
116static SSTD_t* Transaction_table = NULL;
117static uint8_t Transaction_table_size = 0;
118
119void soft_serial_initiator_init(SSTD_t* sstd_table, int sstd_table_size) {
120 Transaction_table = sstd_table;
121 Transaction_table_size = (uint8_t)sstd_table_size;
122
123 usart_master_init();
124}
125
126void soft_serial_target_init(SSTD_t* sstd_table, int sstd_table_size) {
127 Transaction_table = sstd_table;
128 Transaction_table_size = (uint8_t)sstd_table_size;
129
130 usart_slave_init();
131}
132
133void handle_soft_serial_slave(void) {
134 uint8_t sstd_index = sdGet(&SERIAL_USART_DRIVER); // first chunk is always transaction id
135 SSTD_t* trans = &Transaction_table[sstd_index];
136
137 // Always write back the sstd_index as part of a basic handshake
138 sstd_index ^= HANDSHAKE_MAGIC;
139 sdWrite(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index));
140
141 if (trans->initiator2target_buffer_size) {
142 sdRead(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size);
143 }
144
145 if (trans->target2initiator_buffer_size) {
146 sdWrite(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size);
147 }
148
149 if (trans->status) {
150 *trans->status = TRANSACTION_ACCEPTED;
151 }
152}
153
154/////////
155// start transaction by initiator
156//
157// int soft_serial_transaction(int sstd_index)
158//
159// Returns:
160// TRANSACTION_END
161// TRANSACTION_NO_RESPONSE
162// TRANSACTION_DATA_ERROR
163#ifndef SERIAL_USE_MULTI_TRANSACTION
164int soft_serial_transaction(void) {
165 uint8_t sstd_index = 0;
166#else
167int soft_serial_transaction(int index) {
168 uint8_t sstd_index = index;
169#endif
170
171 if (sstd_index > Transaction_table_size) return TRANSACTION_TYPE_ERROR;
172 SSTD_t* trans = &Transaction_table[sstd_index];
173 msg_t res = 0;
174
175 sdClear(&SERIAL_USART_DRIVER);
176
177 // First chunk is always transaction id
178 sdWriteTimeout(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index), TIME_MS2I(SERIAL_USART_TIMEOUT));
179
180 uint8_t sstd_index_shake = 0xFF;
181
182 // Which we always read back first so that we can error out correctly
183 // - due to the half duplex limitations on return codes, we always have to read *something*
184 // - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready
185 res = sdReadTimeout(&SERIAL_USART_DRIVER, &sstd_index_shake, sizeof(sstd_index_shake), TIME_MS2I(SERIAL_USART_TIMEOUT));
186 if (res < 0 || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) {
187 dprintf("serial::usart_shake NO_RESPONSE\n");
188 return TRANSACTION_NO_RESPONSE;
189 }
190
191 if (trans->initiator2target_buffer_size) {
192 res = sdWriteTimeout(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT));
193 if (res < 0) {
194 dprintf("serial::usart_transmit NO_RESPONSE\n");
195 return TRANSACTION_NO_RESPONSE;
196 }
197 }
198
199 if (trans->target2initiator_buffer_size) {
200 res = sdReadTimeout(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT));
201 if (res < 0) {
202 dprintf("serial::usart_receive NO_RESPONSE\n");
203 return TRANSACTION_NO_RESPONSE;
204 }
205 }
206
207 return TRANSACTION_END;
208}