From 6d67e9df4be6981de05517626c1a504da31abf16 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Fri, 17 Jun 2022 22:06:44 +0200 Subject: [Core] Allow usage of ChibiOS's SIO driver for split keyboards (#15907) --- platforms/chibios/drivers/serial_usart.c | 173 +++++++++++++++++++++++-------- 1 file changed, 129 insertions(+), 44 deletions(-) (limited to 'platforms/chibios/drivers/serial_usart.c') diff --git a/platforms/chibios/drivers/serial_usart.c b/platforms/chibios/drivers/serial_usart.c index e9fa4af7a3..7ea25d005b 100644 --- a/platforms/chibios/drivers/serial_usart.c +++ b/platforms/chibios/drivers/serial_usart.c @@ -1,44 +1,47 @@ -/* Copyright 2021 QMK - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ +// Copyright 2021 QMK +// Copyright 2022 Stefan Kerkmann +// SPDX-License-Identifier: GPL-2.0-or-later #include "serial_usart.h" #include "synchronization_util.h" #if defined(SERIAL_USART_CONFIG) -static SerialConfig serial_config = SERIAL_USART_CONFIG; +static QMKSerialConfig serial_config = SERIAL_USART_CONFIG; #else -static SerialConfig serial_config = { - .speed = (SERIAL_USART_SPEED), /* speed - mandatory */ +static QMKSerialConfig serial_config = { +# if HAL_USE_SERIAL + .speed = (SERIAL_USART_SPEED), /* baudrate - mandatory */ +# else + .baud = (SERIAL_USART_SPEED), /* baudrate - mandatory */ +# endif .cr1 = (SERIAL_USART_CR1), .cr2 = (SERIAL_USART_CR2), # if !defined(SERIAL_USART_FULL_DUPLEX) .cr3 = ((SERIAL_USART_CR3) | USART_CR3_HDSEL) /* activate half-duplex mode */ # else - .cr3 = (SERIAL_USART_CR3) + .cr3 = (SERIAL_USART_CR3) # endif }; #endif -static SerialDriver* serial_driver = &SERIAL_USART_DRIVER; +static QMKSerialDriver* serial_driver = (QMKSerialDriver*)&SERIAL_USART_DRIVER; static inline bool react_to_transactions(void); static inline bool __attribute__((nonnull)) receive(uint8_t* destination, const size_t size); +static inline bool __attribute__((nonnull)) receive_blocking(uint8_t* destination, const size_t size); static inline bool __attribute__((nonnull)) send(const uint8_t* source, const size_t size); static inline bool initiate_transaction(uint8_t sstd_index); static inline void usart_clear(void); +static inline void usart_driver_start(void); + +#if HAL_USE_SERIAL + +/** + * @brief SERIAL Driver startup routine. + */ +static inline void usart_driver_start(void) { + sdStart(serial_driver, &serial_config); +} /** * @brief Clear the receive input queue. @@ -64,6 +67,45 @@ static inline void usart_clear(void) { } } +#elif HAL_USE_SIO + +void clear_rx_evt_cb(SIODriver* siop) { + osalSysLockFromISR(); + /* If errors occured during transactions this callback is invoked. We just + * clear the error sources and move on. We rely on the fact that we check + * for the success of the transaction by comparing the received/send bytes + * with the actual received/send bytes in the send/receive functions. */ + sioGetAndClearEventsI(serial_driver); + osalSysUnlockFromISR(); +} + +static const SIOOperation serial_usart_operation = {.rx_cb = NULL, .rx_idle_cb = NULL, .tx_cb = NULL, .tx_end_cb = NULL, .rx_evt_cb = &clear_rx_evt_cb}; + +/** + * @brief SIO Driver startup routine. + */ +static inline void usart_driver_start(void) { + sioStart(serial_driver, &serial_config); + sioStartOperation(serial_driver, &serial_usart_operation); +} + +/** + * @brief Clear the receive input queue, as some MCUs have built-in hardware FIFOs. + */ +static inline void usart_clear(void) { + osalSysLock(); + while (!sioIsRXEmptyX(serial_driver)) { + (void)sioGetX(serial_driver); + } + osalSysUnlock(); +} + +#else + +# error Either the SERIAL or SIO driver has to be activated to use the usart driver for split keyboards. + +#endif + /** * @brief Blocking send of buffer with timeout. * @@ -71,15 +113,46 @@ static inline void usart_clear(void) { * @return false Send failed. */ static inline bool send(const uint8_t* source, const size_t size) { - bool success = (size_t)sdWriteTimeout(serial_driver, source, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; + bool success = (size_t)chnWriteTimeout(serial_driver, source, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; #if !defined(SERIAL_USART_FULL_DUPLEX) - if (success) { - /* Half duplex fills the input queue with the data we wrote - just throw it away. - Under the right circumstances (e.g. bad cables paired with high baud rates) - less bytes can be present in the input queue, therefore a timeout is needed. */ - uint8_t dump[size]; - return receive(dump, size); + /* Half duplex fills the input queue with the data we wrote - just throw it away. */ + if (likely(success)) { + size_t bytes_left = size; +# if HAL_USE_SERIAL + /* The SERIAL driver uses large soft FIFOs that are filled from an IRQ + * context, so there is a delay between receiving the data and it + * becoming actually available, therefore we have to apply a timeout + * mechanism. Under the right circumstances (e.g. bad cables paired with + * high baud rates) less bytes can be present in the input queue as + * well. */ + uint8_t dump[64]; + + while (unlikely(bytes_left >= 64)) { + if (unlikely(!receive(dump, 64))) { + return false; + } + bytes_left -= 64; + } + + return receive(dump, bytes_left); +# else + /* The SIO driver directly accesses the hardware FIFOs of the USART + * peripheral. As these are limited in depth, the RX FIFO might have been + * overflowed by a large that we just send. Therefore we attempt to read + * back all the data we send or until the FIFO runs empty in case it + * overflowed and data was truncated. */ + if (unlikely(sioSynchronizeTXEnd(serial_driver, TIME_MS2I(SERIAL_USART_TIMEOUT)) < MSG_OK)) { + return false; + } + + osalSysLock(); + while (bytes_left > 0 && !sioIsRXEmptyX(serial_driver)) { + (void)sioGetX(serial_driver); + bytes_left--; + } + osalSysUnlock(); +# endif } #endif @@ -90,10 +163,21 @@ static inline bool send(const uint8_t* source, const size_t size) { * @brief Blocking receive of size * bytes with timeout. * * @return true Receive success. - * @return false Receive failed. + * @return false Receive failed, e.g. by timeout. */ static inline bool receive(uint8_t* destination, const size_t size) { - bool success = (size_t)sdReadTimeout(serial_driver, destination, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; + bool success = (size_t)chnReadTimeout(serial_driver, destination, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; + return success; +} + +/** + * @brief Blocking receive of size * bytes. + * + * @return true Receive success. + * @return false Receive failed. + */ +static inline bool receive_blocking(uint8_t* destination, const size_t size) { + bool success = (size_t)chnRead(serial_driver, destination, size) == size; return success; } @@ -146,7 +230,7 @@ __attribute__((weak)) void usart_init(void) { /** * @brief Overridable master specific initializations. */ -__attribute__((weak, nonnull)) void usart_master_init(SerialDriver** driver) { +__attribute__((weak, nonnull)) void usart_master_init(QMKSerialDriver** driver) { (void)driver; usart_init(); } @@ -154,7 +238,7 @@ __attribute__((weak, nonnull)) void usart_master_init(SerialDriver** driver) { /** * @brief Overridable slave specific initializations. */ -__attribute__((weak, nonnull)) void usart_slave_init(SerialDriver** driver) { +__attribute__((weak, nonnull)) void usart_slave_init(QMKSerialDriver** driver) { (void)driver; usart_init(); } @@ -169,7 +253,7 @@ static THD_FUNCTION(SlaveThread, arg) { chRegSetThreadName("usart_tx_rx"); while (true) { - if (!react_to_transactions()) { + if (unlikely(!react_to_transactions())) { /* Clear the receive queue, to start with a clean slate. * Parts of failed transactions or spurious bytes could still be in it. */ usart_clear(); @@ -184,7 +268,7 @@ static THD_FUNCTION(SlaveThread, arg) { void soft_serial_target_init(void) { usart_slave_init(&serial_driver); - sdStart(serial_driver, &serial_config); + usart_driver_start(); /* Start transport thread. */ chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); @@ -195,10 +279,11 @@ void soft_serial_target_init(void) { */ static inline bool react_to_transactions(void) { /* Wait until there is a transaction for us. */ - uint8_t sstd_index = (uint8_t)sdGet(serial_driver); + uint8_t sstd_index = 0; + receive_blocking(&sstd_index, sizeof(sstd_index)); /* Sanity check that we are actually responding to a valid transaction. */ - if (sstd_index >= NUM_TOTAL_TRANSACTIONS) { + if (unlikely(sstd_index >= NUM_TOTAL_TRANSACTIONS)) { return false; } @@ -208,13 +293,13 @@ static inline bool react_to_transactions(void) { /* Send back the handshake which is XORed as a simple checksum, to signal that the slave is ready to receive possible transaction buffers */ sstd_index ^= HANDSHAKE_MAGIC; - if (!send(&sstd_index, sizeof(sstd_index))) { + if (unlikely(!send(&sstd_index, sizeof(sstd_index)))) { return false; } /* Receive transaction buffer from the master. If this transaction requires it.*/ if (trans->initiator2target_buffer_size) { - if (!receive(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size)) { + if (unlikely(!receive(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size))) { return false; } } @@ -226,7 +311,7 @@ static inline bool react_to_transactions(void) { /* Send transaction buffer to the master. If this transaction requires it. */ if (trans->target2initiator_buffer_size) { - if (!send(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) { + if (unlikely(!send(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size))) { return false; } } @@ -244,7 +329,7 @@ void soft_serial_initiator_init(void) { serial_config.cr2 |= USART_CR2_SWAP; // master has swapped TX/RX pins #endif - sdStart(serial_driver, &serial_config); + usart_driver_start(); } /** @@ -270,7 +355,7 @@ bool soft_serial_transaction(int index) { */ static inline bool initiate_transaction(uint8_t sstd_index) { /* Sanity check that we are actually starting a valid transaction. */ - if (sstd_index >= NUM_TOTAL_TRANSACTIONS) { + if (unlikely(sstd_index >= NUM_TOTAL_TRANSACTIONS)) { dprintln("USART: Illegal transaction Id."); return false; } @@ -278,7 +363,7 @@ static inline bool initiate_transaction(uint8_t sstd_index) { split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; /* Send transaction table index to the slave, which doubles as basic handshake token. */ - if (!send(&sstd_index, sizeof(sstd_index))) { + if (unlikely(!send(&sstd_index, sizeof(sstd_index)))) { dprintln("USART: Send Handshake failed."); return false; } @@ -289,14 +374,14 @@ static inline bool initiate_transaction(uint8_t sstd_index) { * - due to the half duplex limitations on return codes, we always have to read *something*. * - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready. */ - if (!receive(&sstd_index_shake, sizeof(sstd_index_shake)) || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { + if (unlikely(!receive(&sstd_index_shake, sizeof(sstd_index_shake)) || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC)))) { dprintln("USART: Handshake failed."); return false; } /* Send transaction buffer to the slave. If this transaction requires it. */ if (trans->initiator2target_buffer_size) { - if (!send(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size)) { + if (unlikely(!send(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size))) { dprintln("USART: Send failed."); return false; } @@ -304,7 +389,7 @@ static inline bool initiate_transaction(uint8_t sstd_index) { /* Receive transaction buffer from the slave. If this transaction requires it. */ if (trans->target2initiator_buffer_size) { - if (!receive(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) { + if (unlikely(!receive(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size))) { dprintln("USART: Receive failed."); return false; } -- cgit v1.2.3 From fe680a8568d275732738b07166b8f8a950d1e282 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Sat, 18 Jun 2022 00:04:17 +0200 Subject: [Core] Split ChibiOS usart split driver in protocol and hardware driver part (#16669) --- platforms/chibios/drivers/serial_usart.c | 191 ++----------------------------- 1 file changed, 10 insertions(+), 181 deletions(-) (limited to 'platforms/chibios/drivers/serial_usart.c') diff --git a/platforms/chibios/drivers/serial_usart.c b/platforms/chibios/drivers/serial_usart.c index 7ea25d005b..f76afb5db4 100644 --- a/platforms/chibios/drivers/serial_usart.c +++ b/platforms/chibios/drivers/serial_usart.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "serial_usart.h" +#include "serial_protocol.h" #include "synchronization_util.h" #if defined(SERIAL_USART_CONFIG) @@ -26,14 +27,6 @@ static QMKSerialConfig serial_config = { static QMKSerialDriver* serial_driver = (QMKSerialDriver*)&SERIAL_USART_DRIVER; -static inline bool react_to_transactions(void); -static inline bool __attribute__((nonnull)) receive(uint8_t* destination, const size_t size); -static inline bool __attribute__((nonnull)) receive_blocking(uint8_t* destination, const size_t size); -static inline bool __attribute__((nonnull)) send(const uint8_t* source, const size_t size); -static inline bool initiate_transaction(uint8_t sstd_index); -static inline void usart_clear(void); -static inline void usart_driver_start(void); - #if HAL_USE_SERIAL /** @@ -43,10 +36,7 @@ static inline void usart_driver_start(void) { sdStart(serial_driver, &serial_config); } -/** - * @brief Clear the receive input queue. - */ -static inline void usart_clear(void) { +inline void serial_transport_driver_clear(void) { osalSysLock(); bool volatile queue_not_empty = !iqIsEmptyI(&serial_driver->iqueue); osalSysUnlock(); @@ -89,10 +79,7 @@ static inline void usart_driver_start(void) { sioStartOperation(serial_driver, &serial_usart_operation); } -/** - * @brief Clear the receive input queue, as some MCUs have built-in hardware FIFOs. - */ -static inline void usart_clear(void) { +inline void serial_transport_driver_clear(void) { osalSysLock(); while (!sioIsRXEmptyX(serial_driver)) { (void)sioGetX(serial_driver); @@ -106,13 +93,7 @@ static inline void usart_clear(void) { #endif -/** - * @brief Blocking send of buffer with timeout. - * - * @return true Send success. - * @return false Send failed. - */ -static inline bool send(const uint8_t* source, const size_t size) { +inline bool serial_transport_send(const uint8_t* source, const size_t size) { bool success = (size_t)chnWriteTimeout(serial_driver, source, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; #if !defined(SERIAL_USART_FULL_DUPLEX) @@ -129,13 +110,13 @@ static inline bool send(const uint8_t* source, const size_t size) { uint8_t dump[64]; while (unlikely(bytes_left >= 64)) { - if (unlikely(!receive(dump, 64))) { + if (unlikely(!serial_transport_receive(dump, 64))) { return false; } bytes_left -= 64; } - return receive(dump, bytes_left); + return serial_transport_receive(dump, bytes_left); # else /* The SIO driver directly accesses the hardware FIFOs of the USART * peripheral. As these are limited in depth, the RX FIFO might have been @@ -159,24 +140,12 @@ static inline bool send(const uint8_t* source, const size_t size) { return success; } -/** - * @brief Blocking receive of size * bytes with timeout. - * - * @return true Receive success. - * @return false Receive failed, e.g. by timeout. - */ -static inline bool receive(uint8_t* destination, const size_t size) { +inline bool serial_transport_receive(uint8_t* destination, const size_t size) { bool success = (size_t)chnReadTimeout(serial_driver, destination, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; return success; } -/** - * @brief Blocking receive of size * bytes. - * - * @return true Receive success. - * @return false Receive failed. - */ -static inline bool receive_blocking(uint8_t* destination, const size_t size) { +inline bool serial_transport_receive_blocking(uint8_t* destination, const size_t size) { bool success = (size_t)chnRead(serial_driver, destination, size) == size; return success; } @@ -243,86 +212,12 @@ __attribute__((weak, nonnull)) void usart_slave_init(QMKSerialDriver** driver) { usart_init(); } -/** - * @brief This thread runs on the slave and responds to transactions initiated - * by the master. - */ -static THD_WORKING_AREA(waSlaveThread, 1024); -static THD_FUNCTION(SlaveThread, arg) { - (void)arg; - chRegSetThreadName("usart_tx_rx"); - - while (true) { - if (unlikely(!react_to_transactions())) { - /* Clear the receive queue, to start with a clean slate. - * Parts of failed transactions or spurious bytes could still be in it. */ - usart_clear(); - } - split_shared_memory_unlock(); - } -} - -/** - * @brief Slave specific initializations. - */ -void soft_serial_target_init(void) { +void serial_transport_driver_slave_init(void) { usart_slave_init(&serial_driver); - usart_driver_start(); - - /* Start transport thread. */ - chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); -} - -/** - * @brief React to transactions started by the master. - */ -static inline bool react_to_transactions(void) { - /* Wait until there is a transaction for us. */ - uint8_t sstd_index = 0; - receive_blocking(&sstd_index, sizeof(sstd_index)); - - /* Sanity check that we are actually responding to a valid transaction. */ - if (unlikely(sstd_index >= NUM_TOTAL_TRANSACTIONS)) { - return false; - } - - split_shared_memory_lock(); - split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; - - /* Send back the handshake which is XORed as a simple checksum, - to signal that the slave is ready to receive possible transaction buffers */ - sstd_index ^= HANDSHAKE_MAGIC; - if (unlikely(!send(&sstd_index, sizeof(sstd_index)))) { - return false; - } - - /* Receive transaction buffer from the master. If this transaction requires it.*/ - if (trans->initiator2target_buffer_size) { - if (unlikely(!receive(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size))) { - return false; - } - } - - /* Allow any slave processing to occur. */ - if (trans->slave_callback) { - trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size, split_trans_target2initiator_buffer(trans)); - } - - /* Send transaction buffer to the master. If this transaction requires it. */ - if (trans->target2initiator_buffer_size) { - if (unlikely(!send(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size))) { - return false; - } - } - - return true; } -/** - * @brief Master specific initializations. - */ -void soft_serial_initiator_init(void) { +void serial_transport_driver_master_init(void) { usart_master_init(&serial_driver); #if defined(MCU_STM32) && defined(SERIAL_USART_PIN_SWAP) @@ -331,69 +226,3 @@ void soft_serial_initiator_init(void) { usart_driver_start(); } - -/** - * @brief Start transaction from the master half to the slave half. - * - * @param index Transaction Table index of the transaction to start. - * @return bool Indicates success of transaction. - */ -bool soft_serial_transaction(int index) { - /* Clear the receive queue, to start with a clean slate. - * Parts of failed transactions or spurious bytes could still be in it. */ - usart_clear(); - - split_shared_memory_lock(); - bool result = initiate_transaction((uint8_t)index); - split_shared_memory_unlock(); - - return result; -} - -/** - * @brief Initiate transaction to slave half. - */ -static inline bool initiate_transaction(uint8_t sstd_index) { - /* Sanity check that we are actually starting a valid transaction. */ - if (unlikely(sstd_index >= NUM_TOTAL_TRANSACTIONS)) { - dprintln("USART: Illegal transaction Id."); - return false; - } - - split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; - - /* Send transaction table index to the slave, which doubles as basic handshake token. */ - if (unlikely(!send(&sstd_index, sizeof(sstd_index)))) { - dprintln("USART: Send Handshake failed."); - return false; - } - - uint8_t sstd_index_shake = 0xFF; - - /* Which we always read back first so that we can error out correctly. - * - due to the half duplex limitations on return codes, we always have to read *something*. - * - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready. - */ - if (unlikely(!receive(&sstd_index_shake, sizeof(sstd_index_shake)) || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC)))) { - dprintln("USART: Handshake failed."); - return false; - } - - /* Send transaction buffer to the slave. If this transaction requires it. */ - if (trans->initiator2target_buffer_size) { - if (unlikely(!send(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size))) { - dprintln("USART: Send failed."); - return false; - } - } - - /* Receive transaction buffer from the slave. If this transaction requires it. */ - if (trans->target2initiator_buffer_size) { - if (unlikely(!receive(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size))) { - dprintln("USART: Receive failed."); - return false; - } - } - - return true; -} -- cgit v1.2.3 From d7173967087e022d20d1f9c812b1b668e9d3f71b Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Thu, 30 Jun 2022 13:19:27 +0200 Subject: [Core] Add Raspberry Pi RP2040 support (#14877) * Disable RESET keycode because of naming conflicts * Add Pico SDK as submodule * Add RP2040 build support to QMK * Adjust USB endpoint structs for RP2040 * Add RP2040 bootloader and double-tap reset routine * Add generic and pro micro RP2040 boards * Add RP2040 onekey keyboard * Add WS2812 PIO DMA enabled driver and documentation Supports regular and open-drain output configuration. RP2040 GPIOs are sadly not 5V tolerant, so this is a bit use-less or needs extra hardware or you take the risk to fry your hardware. * Adjust SIO Driver for RP2040 * Adjust I2C Driver for RP2040 * Adjust SPI Driver for RP2040 * Add PIO serial driver and documentation * Add general RP2040 documentation * Apply suggestions from code review Co-authored-by: Nick Brassel Co-authored-by: Nick Brassel --- platforms/chibios/drivers/serial_usart.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'platforms/chibios/drivers/serial_usart.c') diff --git a/platforms/chibios/drivers/serial_usart.c b/platforms/chibios/drivers/serial_usart.c index f76afb5db4..6581a5b6e9 100644 --- a/platforms/chibios/drivers/serial_usart.c +++ b/platforms/chibios/drivers/serial_usart.c @@ -8,12 +8,12 @@ #if defined(SERIAL_USART_CONFIG) static QMKSerialConfig serial_config = SERIAL_USART_CONFIG; -#else +#elif defined(MCU_STM32) /* STM32 MCUs */ static QMKSerialConfig serial_config = { # if HAL_USE_SERIAL - .speed = (SERIAL_USART_SPEED), /* baudrate - mandatory */ + .speed = (SERIAL_USART_SPEED), # else - .baud = (SERIAL_USART_SPEED), /* baudrate - mandatory */ + .baud = (SERIAL_USART_SPEED), # endif .cr1 = (SERIAL_USART_CR1), .cr2 = (SERIAL_USART_CR2), @@ -23,6 +23,19 @@ static QMKSerialConfig serial_config = { .cr3 = (SERIAL_USART_CR3) # endif }; +#elif defined(MCU_RP) /* Raspberry Pi MCUs */ +/* USART in 8E2 config with RX and TX FIFOs enabled. */ +// clang-format off +static QMKSerialConfig serial_config = { + .baud = (SERIAL_USART_SPEED), + .UARTLCR_H = UART_UARTLCR_H_WLEN_8BITS | UART_UARTLCR_H_PEN | UART_UARTLCR_H_STP2 | UART_UARTLCR_H_FEN, + .UARTCR = 0U, + .UARTIFLS = UART_UARTIFLS_RXIFLSEL_1_8F | UART_UARTIFLS_TXIFLSEL_1_8E, + .UARTDMACR = 0U +}; +// clang-format on +#else +# error MCU Familiy not supported by default, supply your own serial_config by defining SERIAL_USART_CONFIG in your keyboard files. #endif static QMKSerialDriver* serial_driver = (QMKSerialDriver*)&SERIAL_USART_DRIVER; @@ -156,7 +169,7 @@ inline bool serial_transport_receive_blocking(uint8_t* destination, const size_t * @brief Initiate pins for USART peripheral. Half-duplex configuration. */ __attribute__((weak)) void usart_init(void) { -# if defined(MCU_STM32) +# if defined(MCU_STM32) /* STM32 MCUs */ # if defined(USE_GPIOV1) palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE_OPENDRAIN); # else @@ -166,6 +179,8 @@ __attribute__((weak)) void usart_init(void) { # if defined(USART_REMAP) USART_REMAP; # endif +# elif defined(MCU_RP) /* Raspberry Pi MCUs */ +# error Half-duplex with the SIO driver is not supported due to hardware limitations on the RP2040, switch to the PIO driver which has half-duplex support. # else # pragma message "usart_init: MCU Familiy not supported by default, please supply your own init code by implementing usart_init() in your keyboard files." # endif @@ -177,7 +192,7 @@ __attribute__((weak)) void usart_init(void) { * @brief Initiate pins for USART peripheral. Full-duplex configuration. */ __attribute__((weak)) void usart_init(void) { -# if defined(MCU_STM32) +# if defined(MCU_STM32) /* STM32 MCUs */ # if defined(USE_GPIOV1) palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE_PUSHPULL); palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_INPUT); @@ -189,6 +204,9 @@ __attribute__((weak)) void usart_init(void) { # if defined(USART_REMAP) USART_REMAP; # endif +# elif defined(MCU_RP) /* Raspberry Pi MCUs */ + palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE_UART); + palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_ALTERNATE_UART); # else # pragma message "usart_init: MCU Familiy not supported by default, please supply your own init code by implementing usart_init() in your keyboard files." # endif -- cgit v1.2.3