diff options
Diffstat (limited to 'drivers/chibios')
-rw-r--r-- | drivers/chibios/uart.c | 59 | ||||
-rw-r--r-- | drivers/chibios/uart.h | 77 |
2 files changed, 136 insertions, 0 deletions
diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c new file mode 100644 index 000000000..6e94899b9 --- /dev/null +++ b/drivers/chibios/uart.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* Copyright 2021 | ||
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 <https://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "uart.h" | ||
18 | |||
19 | #include "quantum.h" | ||
20 | |||
21 | static SerialConfig serialConfig = { | ||
22 | SERIAL_DEFAULT_BITRATE, | ||
23 | SD1_CR1, | ||
24 | SD1_CR2, | ||
25 | SD1_CR3 | ||
26 | }; | ||
27 | |||
28 | void uart_init(uint32_t baud) { | ||
29 | static bool is_initialised = false; | ||
30 | |||
31 | if (!is_initialised) { | ||
32 | is_initialised = true; | ||
33 | |||
34 | serialConfig.speed = baud; | ||
35 | |||
36 | #if defined(USE_GPIOV1) | ||
37 | palSetLineMode(SD1_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
38 | palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
39 | #else | ||
40 | palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
41 | palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
42 | #endif | ||
43 | sdStart(&SERIAL_DRIVER, &serialConfig); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | void uart_putchar(uint8_t c) { | ||
48 | sdPut(&SERIAL_DRIVER, c); | ||
49 | } | ||
50 | |||
51 | uint8_t uart_getchar(void) { | ||
52 | msg_t res = sdGet(&SERIAL_DRIVER); | ||
53 | |||
54 | return (uint8_t)res; | ||
55 | } | ||
56 | |||
57 | bool uart_available(void) { | ||
58 | return !sdGetWouldBlock(&SERIAL_DRIVER); | ||
59 | } | ||
diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h new file mode 100644 index 000000000..b4e20e9fd --- /dev/null +++ b/drivers/chibios/uart.h | |||
@@ -0,0 +1,77 @@ | |||
1 | /* Copyright 2021 | ||
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 <https://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #pragma once | ||
18 | |||
19 | #include <stdint.h> | ||
20 | |||
21 | #include <hal.h> | ||
22 | |||
23 | #ifndef SERIAL_DRIVER | ||
24 | # define SERIAL_DRIVER SD1 | ||
25 | #endif | ||
26 | |||
27 | #ifndef SD1_TX_PIN | ||
28 | # define SD1_TX_PIN A9 | ||
29 | #endif | ||
30 | |||
31 | #ifndef SD1_TX_PAL_MODE | ||
32 | # define SD1_TX_PAL_MODE 7 | ||
33 | #endif | ||
34 | |||
35 | #ifndef SD1_RX_PIN | ||
36 | # define SD1_RX_PIN A10 | ||
37 | #endif | ||
38 | |||
39 | #ifndef SD1_RX_PAL_MODE | ||
40 | # define SD1_RX_PAL_MODE 7 | ||
41 | #endif | ||
42 | |||
43 | #ifndef SD1_CTS_PIN | ||
44 | # define SD1_CTS_PIN A11 | ||
45 | #endif | ||
46 | |||
47 | #ifndef SD1_CTS_PAL_MODE | ||
48 | # define SD1_CTS_PAL_MODE 7 | ||
49 | #endif | ||
50 | |||
51 | #ifndef SD1_RTS_PIN | ||
52 | # define SD1_RTS_PIN A12 | ||
53 | #endif | ||
54 | |||
55 | #ifndef SD1_RTS_PAL_MODE | ||
56 | # define SD1_RTS_PAL_MODE 7 | ||
57 | #endif | ||
58 | |||
59 | #ifndef SD1_CR1 | ||
60 | # define SD1_CR1 0 | ||
61 | #endif | ||
62 | |||
63 | #ifndef SD1_CR2 | ||
64 | # define SD1_CR2 0 | ||
65 | #endif | ||
66 | |||
67 | #ifndef SD1_CR3 | ||
68 | # define SD1_CR3 0 | ||
69 | #endif | ||
70 | |||
71 | void uart_init(uint32_t baud); | ||
72 | |||
73 | void uart_putchar(uint8_t c); | ||
74 | |||
75 | uint8_t uart_getchar(void); | ||
76 | |||
77 | bool uart_available(void); | ||