From 533608a2f4b2e0c19cb2f696fc491908ec980812 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sun, 13 Jan 2013 18:37:32 +0100 Subject: Move usbuart to the stm32 directory --- src/include/usbuart.h | 33 +++++++++ src/platforms/native/Makefile.inc | 3 +- src/platforms/native/usbuart.c | 145 -------------------------------------- src/platforms/stlink/Makefile.inc | 4 +- src/platforms/stm32/usbuart.c | 144 +++++++++++++++++++++++++++++++++++++ src/platforms/stm32/usbuart.h | 33 --------- 6 files changed, 180 insertions(+), 182 deletions(-) create mode 100644 src/include/usbuart.h delete mode 100644 src/platforms/native/usbuart.c create mode 100644 src/platforms/stm32/usbuart.c delete mode 100644 src/platforms/stm32/usbuart.h (limited to 'src') diff --git a/src/include/usbuart.h b/src/include/usbuart.h new file mode 100644 index 0000000..39f7dcd --- /dev/null +++ b/src/include/usbuart.h @@ -0,0 +1,33 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2012 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ +#ifndef __USBUART_H +#define __USBUART_H + +#include +#include +#include "general.h" + +void usbuart_init(void); + +void usbuart_set_line_coding(struct usb_cdc_line_coding *coding); +void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep); +void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep); + +#endif diff --git a/src/platforms/native/Makefile.inc b/src/platforms/native/Makefile.inc index 585c50d..36ee01c 100644 --- a/src/platforms/native/Makefile.inc +++ b/src/platforms/native/Makefile.inc @@ -3,8 +3,7 @@ CC = $(CROSS_COMPILE)gcc OBJCOPY = $(CROSS_COMPILE)objcopy CFLAGS += -Istm32/include -mcpu=cortex-m3 -mthumb \ - -DSTM32F1 -DBLACKMAGIC -I../libopencm3/include -Iplatforms/native \ - -Iplatforms/stm32 + -DSTM32F1 -DBLACKMAGIC -I../libopencm3/include LDFLAGS_BOOT = -lopencm3_stm32f1 -Wl,--defsym,_stack=0x20005000 \ -Wl,-T,platforms/stm32/blackmagic.ld -nostartfiles -lc -lnosys \ diff --git a/src/platforms/native/usbuart.c b/src/platforms/native/usbuart.c deleted file mode 100644 index 3d07b33..0000000 --- a/src/platforms/native/usbuart.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * This file is part of the Black Magic Debug project. - * - * Copyright (C) 2012 Black Sphere Technologies Ltd. - * Written by Gareth McMullin - * - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include - -#include - -void usbuart_init(void) -{ -#if defined(BLACKMAGIC) - /* On mini hardware, UART and SWD share connector pins. - * Don't enable UART if we're being debugged. */ - if ((platform_hwversion() == 1) && (SCS_DEMCR & SCS_DEMCR_TRCENA)) - return; -#endif - - rcc_peripheral_enable_clock(&USBUSART_APB_ENR, USBUSART_CLK_ENABLE); - - /* UART TX to 'alternate function output push-pull' */ - gpio_set_mode(USBUSART_PORT, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USBUSART_TX_PIN); - - /* Setup UART parameters. */ - usart_set_baudrate(USBUSART, 38400); - usart_set_databits(USBUSART, 8); - usart_set_stopbits(USBUSART, USART_STOPBITS_1); - usart_set_mode(USBUSART, USART_MODE_TX_RX); - usart_set_parity(USBUSART, USART_PARITY_NONE); - usart_set_flow_control(USBUSART, USART_FLOWCONTROL_NONE); - - /* Finally enable the USART. */ - usart_enable(USBUSART); - - /* Enable interrupts */ - USBUSART_CR1 |= USART_CR1_RXNEIE; - nvic_set_priority(USBUSART_IRQ, IRQ_PRI_USBUSART); - nvic_enable_irq(USBUSART_IRQ); -} - -void usbuart_set_line_coding(struct usb_cdc_line_coding *coding) -{ - usart_set_baudrate(USBUSART, coding->dwDTERate); - usart_set_databits(USBUSART, coding->bDataBits); - 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: - 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: - usart_set_parity(USBUSART, USART_PARITY_EVEN); - break; - } -} - -void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) -{ - (void)ep; - - char buf[CDCACM_PACKET_SIZE]; - int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT, - buf, CDCACM_PACKET_SIZE); - -#if defined(BLACKMAGIC) - /* Don't bother if uart is disabled. - * This will be the case on mini while we're being debugged. - */ - if(!(RCC_APB2ENR & RCC_APB2ENR_USART1EN)) - return; -#endif - - gpio_set(LED_PORT_UART, LED_UART); - for(int i = 0; i < len; i++) - usart_send_blocking(USBUSART, buf[i]); - gpio_clear(LED_PORT_UART, LED_UART); -} - -static uint8_t uart_usb_buf[CDCACM_PACKET_SIZE]; -static uint8_t uart_usb_buf_size; - -void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep) -{ - if (!uart_usb_buf_size) { - gpio_clear(LED_PORT_UART, LED_UART); - return; - } - - usbd_ep_write_packet(dev, ep, uart_usb_buf, uart_usb_buf_size); - uart_usb_buf_size = 0; -} - -void USBUSART_ISR(void) -{ - char c = usart_recv(USBUSART); - - gpio_set(LED_PORT_UART, LED_UART); - - /* Try to send now */ - if (usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, &c, 1) == 1) - return; - - /* We failed, so queue for later */ - if (uart_usb_buf_size == CDCACM_PACKET_SIZE) { - /* Drop if the buffer's full: we have no flow control */ - return; - } - - uart_usb_buf[uart_usb_buf_size++] = c; -} - diff --git a/src/platforms/stlink/Makefile.inc b/src/platforms/stlink/Makefile.inc index 7cacb0e..0f784f5 100644 --- a/src/platforms/stlink/Makefile.inc +++ b/src/platforms/stlink/Makefile.inc @@ -3,7 +3,7 @@ CC = $(CROSS_COMPILE)gcc OBJCOPY = $(CROSS_COMPILE)objcopy CFLAGS += -mcpu=cortex-m3 -mthumb \ - -DSTM32F1 -DDISCOVERY_STLINK -I../libopencm3/include -Iplatforms/stlink -Iplatforms/stm32 + -DSTM32F1 -DDISCOVERY_STLINK -I../libopencm3/include LDFLAGS_BOOT = -lopencm3_stm32f1 -Wl,--defsym,_stack=0x20005000 \ -Wl,-T,platforms/stm32/stlink.ld -nostartfiles -lc -lnosys \ -Wl,-Map=mapfile -mthumb -mcpu=cortex-m3 -Wl,-gc-sections \ @@ -14,7 +14,7 @@ VPATH += platforms/stm32 SRC += cdcacm.c \ platform.c \ - platforms/native/usbuart.c \ + usbuart.c \ all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex diff --git a/src/platforms/stm32/usbuart.c b/src/platforms/stm32/usbuart.c new file mode 100644 index 0000000..27dbaa2 --- /dev/null +++ b/src/platforms/stm32/usbuart.c @@ -0,0 +1,144 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2012 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +void usbuart_init(void) +{ +#if defined(BLACKMAGIC) + /* On mini hardware, UART and SWD share connector pins. + * Don't enable UART if we're being debugged. */ + if ((platform_hwversion() == 1) && (SCS_DEMCR & SCS_DEMCR_TRCENA)) + return; +#endif + + rcc_peripheral_enable_clock(&USBUSART_APB_ENR, USBUSART_CLK_ENABLE); + + /* UART TX to 'alternate function output push-pull' */ + gpio_set_mode(USBUSART_PORT, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USBUSART_TX_PIN); + + /* Setup UART parameters. */ + usart_set_baudrate(USBUSART, 38400); + usart_set_databits(USBUSART, 8); + usart_set_stopbits(USBUSART, USART_STOPBITS_1); + usart_set_mode(USBUSART, USART_MODE_TX_RX); + usart_set_parity(USBUSART, USART_PARITY_NONE); + usart_set_flow_control(USBUSART, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USBUSART); + + /* Enable interrupts */ + USBUSART_CR1 |= USART_CR1_RXNEIE; + nvic_set_priority(USBUSART_IRQ, IRQ_PRI_USBUSART); + nvic_enable_irq(USBUSART_IRQ); +} + +void usbuart_set_line_coding(struct usb_cdc_line_coding *coding) +{ + usart_set_baudrate(USBUSART, coding->dwDTERate); + usart_set_databits(USBUSART, coding->bDataBits); + 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: + 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: + usart_set_parity(USBUSART, USART_PARITY_EVEN); + break; + } +} + +void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) +{ + (void)ep; + + char buf[CDCACM_PACKET_SIZE]; + int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT, + buf, CDCACM_PACKET_SIZE); + +#if defined(BLACKMAGIC) + /* Don't bother if uart is disabled. + * This will be the case on mini while we're being debugged. + */ + if(!(RCC_APB2ENR & RCC_APB2ENR_USART1EN)) + return; +#endif + + gpio_set(LED_PORT_UART, LED_UART); + for(int i = 0; i < len; i++) + usart_send_blocking(USBUSART, buf[i]); + gpio_clear(LED_PORT_UART, LED_UART); +} + +static uint8_t uart_usb_buf[CDCACM_PACKET_SIZE]; +static uint8_t uart_usb_buf_size; + +void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep) +{ + if (!uart_usb_buf_size) { + gpio_clear(LED_PORT_UART, LED_UART); + return; + } + + usbd_ep_write_packet(dev, ep, uart_usb_buf, uart_usb_buf_size); + uart_usb_buf_size = 0; +} + +void USBUSART_ISR(void) +{ + char c = usart_recv(USBUSART); + + gpio_set(LED_PORT_UART, LED_UART); + + /* Try to send now */ + if (usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, &c, 1) == 1) + return; + + /* We failed, so queue for later */ + if (uart_usb_buf_size == CDCACM_PACKET_SIZE) { + /* Drop if the buffer's full: we have no flow control */ + return; + } + + uart_usb_buf[uart_usb_buf_size++] = c; +} diff --git a/src/platforms/stm32/usbuart.h b/src/platforms/stm32/usbuart.h deleted file mode 100644 index 39f7dcd..0000000 --- a/src/platforms/stm32/usbuart.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the Black Magic Debug project. - * - * Copyright (C) 2012 Black Sphere Technologies Ltd. - * Written by Gareth McMullin - * - * 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 . - */ -#ifndef __USBUART_H -#define __USBUART_H - -#include -#include -#include "general.h" - -void usbuart_init(void); - -void usbuart_set_line_coding(struct usb_cdc_line_coding *coding); -void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep); -void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep); - -#endif -- cgit v1.2.3