From 35d6adc236f54e94231b1959eb2d16b785c7733a Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sat, 12 Jan 2013 20:44:43 +0100 Subject: native/usbuart: Move platform dependant parts to platform.h --- src/Makefile | 1 + src/platforms/native/Makefile.inc | 4 ++- src/platforms/native/platform.c | 2 +- src/platforms/native/platform.h | 13 ++++++-- src/platforms/native/usbuart.c | 64 +++++++++++++++++++++------------------ src/platforms/native/usbuart.h | 34 --------------------- src/platforms/stm32/usbuart.h | 33 ++++++++++++++++++++ 7 files changed, 83 insertions(+), 68 deletions(-) delete mode 100644 src/platforms/native/usbuart.h create mode 100644 src/platforms/stm32/usbuart.h diff --git a/src/Makefile b/src/Makefile index 6699191..d9f3e9e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -47,6 +47,7 @@ blackmagic: $(OBJ) clean: host_clean $(RM) *.o *.d *~ blackmagic $(HOSTFILES) + $(RM) platforms/*/*.o platforms/*/*.d mapfile -include *.d diff --git a/src/platforms/native/Makefile.inc b/src/platforms/native/Makefile.inc index dc0e743..585c50d 100644 --- a/src/platforms/native/Makefile.inc +++ b/src/platforms/native/Makefile.inc @@ -3,7 +3,9 @@ CC = $(CROSS_COMPILE)gcc OBJCOPY = $(CROSS_COMPILE)objcopy CFLAGS += -Istm32/include -mcpu=cortex-m3 -mthumb \ - -DSTM32F1 -I../libopencm3/include + -DSTM32F1 -DBLACKMAGIC -I../libopencm3/include -Iplatforms/native \ + -Iplatforms/stm32 + LDFLAGS_BOOT = -lopencm3_stm32f1 -Wl,--defsym,_stack=0x20005000 \ -Wl,-T,platforms/stm32/blackmagic.ld -nostartfiles -lc -lnosys \ -Wl,-Map=mapfile -mthumb -mcpu=cortex-m3 -Wl,-gc-sections \ diff --git a/src/platforms/native/platform.c b/src/platforms/native/platform.c index 5b90976..0f8b78a 100644 --- a/src/platforms/native/platform.c +++ b/src/platforms/native/platform.c @@ -32,7 +32,7 @@ #include "platform.h" #include "jtag_scan.h" -#include "usbuart.h" +#include #include diff --git a/src/platforms/native/platform.h b/src/platforms/native/platform.h index 17841ea..30f063d 100644 --- a/src/platforms/native/platform.h +++ b/src/platforms/native/platform.h @@ -90,6 +90,7 @@ extern usbd_device *usbdev; #define USB_VBUS_IRQ NVIC_EXTI15_10_IRQ #define LED_PORT GPIOB +#define LED_PORT_UART GPIOB #define LED_UART GPIO2 #define LED_IDLE_RUN GPIO10 #define LED_ERROR GPIO11 @@ -99,10 +100,19 @@ extern usbd_device *usbdev; * TIM3 is used for traceswo capture and must be highest priority. */ #define IRQ_PRI_USB (2 << 4) -#define IRQ_PRI_USART1 (1 << 4) +#define IRQ_PRI_USBUSART (1 << 4) #define IRQ_PRI_USB_VBUS (14 << 4) #define IRQ_PRI_TIM3 (0 << 4) +#define USBUSART USART1 +#define USBUSART_CR1 USART1_CR1 +#define USBUSART_IRQ NVIC_USART1_IRQ +#define USBUSART_APB_ENR RCC_APB2ENR +#define USBUSART_CLK_ENABLE RCC_APB2ENR_USART1EN +#define USBUSART_PORT GPIOA +#define USBUSART_TX_PIN GPIO9 +#define USBUSART_ISR usart1_isr + #define DEBUG(...) extern uint8_t running_status; @@ -174,4 +184,3 @@ static inline u16 _gpio_get(u32 gpioport, u16 gpios) #endif #endif - diff --git a/src/platforms/native/usbuart.c b/src/platforms/native/usbuart.c index 35a7d7d..3d07b33 100644 --- a/src/platforms/native/usbuart.c +++ b/src/platforms/native/usbuart.c @@ -26,62 +26,64 @@ #include #include -#include "platform.h" +#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(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); + rcc_peripheral_enable_clock(&USBUSART_APB_ENR, USBUSART_CLK_ENABLE); - /* UART1 TX to 'alternate function output push-pull' */ - gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9); + /* 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(USART1, 38400); - usart_set_databits(USART1, 8); - usart_set_stopbits(USART1, USART_STOPBITS_1); - usart_set_mode(USART1, USART_MODE_TX_RX); - usart_set_parity(USART1, USART_PARITY_NONE); - usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); + 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(USART1); + usart_enable(USBUSART); /* Enable interrupts */ - USART1_CR1 |= USART_CR1_RXNEIE; - nvic_set_priority(NVIC_USART1_IRQ, IRQ_PRI_USART1); - nvic_enable_irq(NVIC_USART1_IRQ); + 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(USART1, coding->dwDTERate); - usart_set_databits(USART1, coding->bDataBits); + usart_set_baudrate(USBUSART, coding->dwDTERate); + usart_set_databits(USBUSART, coding->bDataBits); switch(coding->bCharFormat) { case 0: - usart_set_stopbits(USART1, USART_STOPBITS_1); + usart_set_stopbits(USBUSART, USART_STOPBITS_1); break; case 1: - usart_set_stopbits(USART1, USART_STOPBITS_1_5); + usart_set_stopbits(USBUSART, USART_STOPBITS_1_5); break; case 2: - usart_set_stopbits(USART1, USART_STOPBITS_2); + usart_set_stopbits(USBUSART, USART_STOPBITS_2); break; } switch(coding->bParityType) { case 0: - usart_set_parity(USART1, USART_PARITY_NONE); + usart_set_parity(USBUSART, USART_PARITY_NONE); break; case 1: - usart_set_parity(USART1, USART_PARITY_ODD); + usart_set_parity(USBUSART, USART_PARITY_ODD); break; case 2: - usart_set_parity(USART1, USART_PARITY_EVEN); + usart_set_parity(USBUSART, USART_PARITY_EVEN); break; } } @@ -94,16 +96,18 @@ void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) 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, LED_UART); + gpio_set(LED_PORT_UART, LED_UART); for(int i = 0; i < len; i++) - usart_send_blocking(USART1, buf[i]); - gpio_clear(LED_PORT, LED_UART); + usart_send_blocking(USBUSART, buf[i]); + gpio_clear(LED_PORT_UART, LED_UART); } static uint8_t uart_usb_buf[CDCACM_PACKET_SIZE]; @@ -112,7 +116,7 @@ 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, LED_UART); + gpio_clear(LED_PORT_UART, LED_UART); return; } @@ -120,11 +124,11 @@ void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep) uart_usb_buf_size = 0; } -void usart1_isr(void) +void USBUSART_ISR(void) { - char c = usart_recv(USART1); + char c = usart_recv(USBUSART); - gpio_set(LED_PORT, LED_UART); + gpio_set(LED_PORT_UART, LED_UART); /* Try to send now */ if (usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, &c, 1) == 1) diff --git a/src/platforms/native/usbuart.h b/src/platforms/native/usbuart.h deleted file mode 100644 index 10cb60e..0000000 --- a/src/platforms/native/usbuart.h +++ /dev/null @@ -1,34 +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 - diff --git a/src/platforms/stm32/usbuart.h b/src/platforms/stm32/usbuart.h new file mode 100644 index 0000000..39f7dcd --- /dev/null +++ b/src/platforms/stm32/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 -- cgit v1.2.3