From 487bcf029d35d1eed90072246769cb2c26b8c2e6 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Fri, 19 Aug 2022 00:00:00 +0100 Subject: [PATCH] usbuart: Moved usbuart_set_line_coding() out into a new file --- src/platforms/common/Makefile.inc | 1 + src/platforms/common/aux_serial.c | 73 +++++++++++++++++++++++++++++++ src/platforms/stm32/usbuart.c | 36 --------------- src/platforms/tm4c/usbuart.c | 26 ----------- 4 files changed, 74 insertions(+), 62 deletions(-) create mode 100644 src/platforms/common/aux_serial.c diff --git a/src/platforms/common/Makefile.inc b/src/platforms/common/Makefile.inc index ee6a5bc..fcf4dcd 100644 --- a/src/platforms/common/Makefile.inc +++ b/src/platforms/common/Makefile.inc @@ -27,3 +27,4 @@ SRC += \ usb.c \ usb_serial.c \ usb_dfu_stub.c \ + aux_serial.c diff --git a/src/platforms/common/aux_serial.c b/src/platforms/common/aux_serial.c new file mode 100644 index 0000000..72fcb87 --- /dev/null +++ b/src/platforms/common/aux_serial.c @@ -0,0 +1,73 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2022 1BitSquared + * Written by Rachel Mant + * + * 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 . + */ + +#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) +#include +#elif defined(LM4F) +#include +#else +#error "Unknown processor target" +#endif +#include +#include + +#include "general.h" +#include "usbuart.h" +#include "usb.h" + +void usbuart_set_line_coding(struct usb_cdc_line_coding *coding) +{ + usart_set_baudrate(USBUSART, coding->dwDTERate); + +#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) + if (coding->bParityType) + usart_set_databits(USBUSART, (coding->bDataBits + 1 <= 8 ? 8 : 9)); + else + usart_set_databits(USBUSART, (coding->bDataBits <= 8 ? 8 : 9)); +#elif defined(LM4F) + uart_set_databits(USBUART, coding->bDataBits); +#endif + + switch(coding->bCharFormat) { + case 0: + usart_set_stopbits(USBUSART, USART_STOPBITS_1); + break; + case 1: + usart_set_stopbits(USBUSART, USART_STOPBITS_1_5); + break; + case 2: + default: + usart_set_stopbits(USBUSART, USART_STOPBITS_2); + break; + } + + switch(coding->bParityType) { + case 0: + usart_set_parity(USBUSART, USART_PARITY_NONE); + break; + case 1: + usart_set_parity(USBUSART, USART_PARITY_ODD); + break; + case 2: + default: + usart_set_parity(USBUSART, USART_PARITY_EVEN); + break; + } +} diff --git a/src/platforms/stm32/usbuart.c b/src/platforms/stm32/usbuart.c index 1f37f86..0afeac5 100644 --- a/src/platforms/stm32/usbuart.c +++ b/src/platforms/stm32/usbuart.c @@ -192,42 +192,6 @@ void aux_serial_init(void) usart_enable_rx_dma(USBUSART); } -void usbuart_set_line_coding(struct usb_cdc_line_coding *coding) -{ - usart_set_baudrate(USBUSART, coding->dwDTERate); - - if (coding->bParityType) - usart_set_databits(USBUSART, (coding->bDataBits + 1 <= 8 ? 8 : 9)); - else - usart_set_databits(USBUSART, (coding->bDataBits <= 8 ? 8 : 9)); - - switch(coding->bCharFormat) { - case 0: - usart_set_stopbits(USBUSART, USART_STOPBITS_1); - break; - case 1: - usart_set_stopbits(USBUSART, USART_STOPBITS_1_5); - break; - case 2: - default: - usart_set_stopbits(USBUSART, USART_STOPBITS_2); - break; - } - - switch(coding->bParityType) { - case 0: - usart_set_parity(USBUSART, USART_PARITY_NONE); - break; - case 1: - usart_set_parity(USBUSART, USART_PARITY_ODD); - break; - case 2: - default: - usart_set_parity(USBUSART, USART_PARITY_EVEN); - break; - } -} - /* * Copy data from fifo into continuous buffer. Return copied length. */ diff --git a/src/platforms/tm4c/usbuart.c b/src/platforms/tm4c/usbuart.c index f598363..2e2574a 100644 --- a/src/platforms/tm4c/usbuart.c +++ b/src/platforms/tm4c/usbuart.c @@ -76,32 +76,6 @@ void aux_serial_init(void) nvic_enable_irq(USBUART_IRQ); } -void usbuart_set_line_coding(struct usb_cdc_line_coding *coding) -{ - uart_set_baudrate(USBUART, coding->dwDTERate); - uart_set_databits(USBUART, coding->bDataBits); - switch(coding->bCharFormat) { - case 0: - case 1: - uart_set_stopbits(USBUART, 1); - break; - case 2: - uart_set_stopbits(USBUART, 2); - break; - } - switch(coding->bParityType) { - case 0: - uart_set_parity(USBUART, UART_PARITY_NONE); - break; - case 1: - uart_set_parity(USBUART, UART_PARITY_ODD); - break; - case 2: - uart_set_parity(USBUART, UART_PARITY_EVEN); - break; - } -} - #ifndef ENABLE_RTT void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) {