usbuart: Moved usbuart_set_line_coding() out into a new file

This commit is contained in:
dragonmux 2022-08-19 00:00:00 +01:00 committed by Piotr Esden-Tempski
parent 13209385f8
commit 487bcf029d
4 changed files with 74 additions and 62 deletions

View File

@ -27,3 +27,4 @@ SRC += \
usb.c \ usb.c \
usb_serial.c \ usb_serial.c \
usb_dfu_stub.c \ usb_dfu_stub.c \
aux_serial.c

View File

@ -0,0 +1,73 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2022 1BitSquared <info@1bitsquared.com>
* Written by Rachel Mant <git@dragonmux.network>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
#include <libopencm3/stm32/usart.h>
#elif defined(LM4F)
#include <libopencm3/lm4f/uart.h>
#else
#error "Unknown processor target"
#endif
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
#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;
}
}

View File

@ -192,42 +192,6 @@ void aux_serial_init(void)
usart_enable_rx_dma(USBUSART); 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. * Copy data from fifo into continuous buffer. Return copied length.
*/ */

View File

@ -76,32 +76,6 @@ void aux_serial_init(void)
nvic_enable_irq(USBUART_IRQ); 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 #ifndef ENABLE_RTT
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
{ {