Compare commits

...

40 Commits

Author SHA1 Message Date
dragonmux af4025ef04
aux_serial: Renamed buf_tx_act_sz -> aux_serial_transmit_buffer_consumed 2 years ago
dragonmux 22f6a098df
aux_serial: Moved all the transmit logic into aux_serial.c and cleaned up in usb_serial.c 2 years ago
dragonmux 8028107178
usb_serial: Begun bringin more commonality to debug_uart_receive_callback 2 years ago
dragonmux eb4c85b766
usb_serial: Begun refactoring debug_uart_receive_callback 2 years ago
dragonmux 732567a41a
aux_serial: Redefined the DMA transmit buffers as two actual buffers and simplified 2 years ago
dragonmux 6030872c53
aux_serial: Refined how flip-flopping between the two transmit buffers is achieved 2 years ago
dragonmux d5a1d5b909
usbuart: Further refined the buffer sizing definition process 2 years ago
dragonmux 9f72cbca04
usbuart: Moved all the STM32 buffer sizing logic into one header and one location 2 years ago
dragonmux 7550d0c2c1
aux_serial: tx_buf -> aux_serial_transmit_buffer 2 years ago
dragonmux 8b87546a01
usb_serial: Begun cleaning up in debug_uart_receive_callback() 2 years ago
dragonmux 43f8b397a1
aux_serial: Begun moving globals into their proper private TU scopes 2 years ago
dragonmux 5a25269333
aux_serial: Moved the transmit buffer swap function declaration into aux_serial.h and implemented a function to get the current buffer 2 years ago
dragonmux 2746bd7bcc
usb_serial: Renamed usbuart_usb_out_cb() -> debug_uart_receive_callback() 2 years ago
dragonmux d4d0900fb9
aux_serial: Renamed usbuart_change_dma_tx_buf() -> aux_serial_switch_transmit_buffers() 2 years ago
dragonmux f053c2a219
usbuart: Moved usbuart_change_dma_tx_buf() into aux_serial.c 2 years ago
dragonmux 811c643128
usbuart: Moved usbuart_usb_out_cb() into usb_serial.c 2 years ago
dragonmux 3282087db6
usb_serial: Renamed usbuart_usb_in_cb() -> debug_uart_send_callback() 2 years ago
dragonmux 7497f1d441
usbuart: Moved usbuart_usb_in_cb into usb_serial.c 2 years ago
dragonmux 1ac6de276e
usbuart: Moved usbuart_usb_in_cb into usb_serial.c 2 years ago
dragonmux 8936069244
usb_serial: Renamed usbuart_send_rx_packet() -> debug_uart_send_rx_packet() 2 years ago
dragonmux c9ba465549
usbuart: Moved usbuart_send_rx_packet to usb_serial.c 2 years ago
dragonmux e7bd8135b9
usb_serial: Renamed usbuart_run() -> debug_uart_run() 2 years ago
dragonmux c132d08981
usbuart: Moved usbuart_run() into usb_serial.c 2 years ago
dragonmux 9b2eb0caad
platform: Moved the aux serial functions into their own header 2 years ago
dragonmux f491aff745
usb_serial: clang-foramt cleanup of debug_uart_write() 2 years ago
dragonmux fe4e5fbbbb
usb_serial: Renamed usbuart_debug_write() -> debug_uart_write() 2 years ago
dragonmux 41c7760261
usbuart: Moved the SemiHosting implementation to usb_serial.c 2 years ago
dragonmux 3d5018b1e2
usbuart: Moved usbuart_debug_write() to usb_serial.c 2 years ago
dragonmux c492fa7948
usbuart: Moved some important definitions to the header temporarily 2 years ago
dragonmux bf164edf43
aux_serial: Renamed usbuart_set_line_coding() -> aux_serial_set_encoding() 2 years ago
dragonmux 3faf4221e3
usbuart: Moved usbuart_set_line_coding() out into a new file 2 years ago
dragonmux e07cde4302
usb_serial: Refactored debug_uart_send_stdout() and cleaned up 2 years ago
dragonmux d6c1aee96a
usb_serial: Renamed usbuart_send_stdout() -> debug_uart_send_stdout() 2 years ago
dragonmux 1b42438855
usb_serial: Moved usbuart_send_stdout() into the usb_serial code 2 years ago
dragonmux a4ba390d2d
usb_serial: Moved the "SemiHosting" init into a more suitable spot in bringup 2 years ago
dragonmux 3e1985e961
usbuart: Begun refactoring the physical UART code to seperate out the mixed interests 2 years ago
dragonmux 3c2683b6d9
usbuart: Replaced most of the debug monitor code with newlib hooks and documented everything 2 years ago
dragonmux cefae28c27
platform: Removed usbuart_debug_write() from the headers and fixed its return type 2 years ago
dragonmux 038299db3c
native/usbdfu: clang-format for part of the DFU bootloader 2 years ago
dragonmux e7ecda6faf
native: Done a little more cleanup in the platform with clang-format 2 years ago
  1. 2
      src/include/general.h
  2. 3
      src/platforms/96b_carbon/platform.c
  3. 3
      src/platforms/blackpillv2/platform.c
  4. 1
      src/platforms/common/Makefile.inc
  5. 142
      src/platforms/common/aux_serial.c
  6. 41
      src/platforms/common/aux_serial.h
  7. 267
      src/platforms/common/usb_serial.c
  8. 52
      src/platforms/common/usbuart.h
  9. 3
      src/platforms/f072/platform.c
  10. 3
      src/platforms/f072/platform.h
  11. 3
      src/platforms/f3/platform.c
  12. 1
      src/platforms/f3/platform.h
  13. 3
      src/platforms/f4discovery/platform.c
  14. 3
      src/platforms/hydrabus/platform.c
  15. 3
      src/platforms/launchpad-icdi/platform.c
  16. 15
      src/platforms/native/platform.c
  17. 1
      src/platforms/native/platform.h
  18. 9
      src/platforms/native/usbdfu.c
  19. 9
      src/platforms/stlink/platform.c
  20. 1
      src/platforms/stlink/platform.h
  21. 4
      src/platforms/stm32/rtt_if.c
  22. 339
      src/platforms/stm32/usbuart.c
  23. 7
      src/platforms/swlink/platform.c
  24. 1
      src/platforms/swlink/platform.h
  25. 72
      src/platforms/tm4c/usbuart.c
  26. 2
      src/target/target.c

2
src/include/general.h

@ -78,7 +78,7 @@ enum BMP_DEBUG {
# define DEBUG_WIRE(...) do {} while(0)
# define DEBUG_GDB_WIRE(...) do {} while(0)
void usbuart_send_stdout(const uint8_t *data, uint32_t len);
void debug_uart_send_stdout(const uint8_t *data, size_t len);
#else
# include <stdarg.h>
extern int cl_debuglevel;

3
src/platforms/96b_carbon/platform.c

@ -25,6 +25,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/f4/rcc.h>
@ -69,7 +70,7 @@ void platform_init(void)
platform_timing_init();
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
}
void platform_nrst_set_val(bool assert)

3
src/platforms/blackpillv2/platform.c

@ -25,6 +25,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/rcc.h>
@ -108,7 +109,7 @@ void platform_init(void)
platform_timing_init();
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
// https://github.com/libopencm3/libopencm3/pull/1256#issuecomment-779424001
OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS | OTG_GCCFG_PWRDWN;

1
src/platforms/common/Makefile.inc

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

142
src/platforms/common/aux_serial.c

@ -0,0 +1,142 @@
/*
* 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>
#include <libopencm3/stm32/dma.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"
#include "aux_serial.h"
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
static char aux_serial_transmit_buffer[2U][TX_BUF_SIZE];
static uint8_t aux_serial_transmit_buffer_index;
extern uint8_t aux_serial_transmit_buffer_consumed;
#elif defined(LM4F)
static char aux_serial_transmit_buffer[FIFO_SIZE];
#endif
void aux_serial_set_encoding(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;
}
}
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
char *aux_serial_current_transmit_buffer(void)
{
return aux_serial_transmit_buffer[aux_serial_transmit_buffer_index];
}
size_t aux_serial_transmit_buffer_fullness(void)
{
return aux_serial_transmit_buffer_consumed;
}
/*
* Changes USBUSART TX buffer in which data is accumulated from USB.
* Filled buffer is submitted to DMA for transfer.
*/
void aux_serial_switch_transmit_buffers(void)
{
/* Make the buffer we've been filling the active DMA buffer, and swap to the other */
dma_set_memory_address(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, (uintptr_t)aux_serial_current_transmit_buffer());
dma_set_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, aux_serial_transmit_buffer_consumed);
dma_enable_channel(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN);
/* Change active buffer */
aux_serial_transmit_buffer_consumed = 0;
aux_serial_transmit_buffer_index ^= 1;
}
void aux_serial_send(const size_t len)
{
aux_serial_transmit_buffer_consumed += len;
/* If DMA is idle, schedule new transfer */
if (len && tx_trfr_cplt)
{
tx_trfr_cplt = false;
aux_serial_switch_transmit_buffers();
/* Enable LED */
usbuart_set_led_state(TX_LED_ACT, true);
}
}
#elif defined(LM4F)
char *aux_serial_current_transmit_buffer(void)
{
return aux_serial_transmit_buffer;
}
size_t aux_serial_transmit_buffer_fullness(void)
{
return 0;
}
void aux_serial_send(const size_t len)
{
for(size_t i = 0; i < len; ++i)
uart_send_blocking(USBUART, aux_serial_transmit_buffer[i]);
}
#endif

41
src/platforms/common/aux_serial.h

@ -0,0 +1,41 @@
/*
* 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/>.
*/
#ifndef AUX_SERIAL_H
#define AUX_SERIAL_H
#include <stddef.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
void aux_serial_init(void);
void aux_serial_set_encoding(struct usb_cdc_line_coding *coding);
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
void aux_serial_switch_transmit_buffers(void);
#endif
/* Get the current transmit buffer to stage data into */
char *aux_serial_current_transmit_buffer(void);
/* Get how full the current transmit buffer is */
size_t aux_serial_transmit_buffer_fullness(void);
/* Send a number of bytes staged into the current transmit bufer */
void aux_serial_send(size_t len);
#endif /*AUX_SERIAL_H*/

267
src/platforms/common/usb_serial.c

@ -1,8 +1,10 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <gareth@blacksphere.co.nz>
* 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
@ -41,17 +43,37 @@
#include "general.h"
#include "gdb_if.h"
#include "usb_serial.h"
#if defined(PLATFORM_HAS_TRACESWO)
# include "traceswo.h"
#ifdef PLATFORM_HAS_TRACESWO
#include "traceswo.h"
#endif
#include "usbuart.h"
#include "aux_serial.h"
#include <libopencm3/cm3/cortex.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/usb/cdc.h>
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/dma.h>
#endif
static bool gdb_uart_dtr = true;
static void usb_serial_set_state(usbd_device *dev, uint16_t iface, uint8_t ep);
static void debug_uart_send_callback(usbd_device *dev, uint8_t ep);
static void debug_uart_receive_callback(usbd_device *dev, uint8_t ep);
#ifdef ENABLE_DEBUG
/*
* This call initialises "SemiHosting", only we then do our own SVC interrupt things to
* route all output through to the debug USB serial interface if debug_bmp is true.
*
* https://github.com/mirror/newlib-cygwin/blob/master/newlib/libc/sys/arm/syscalls.c#L115
*/
void initialise_monitor_handles(void);
#endif
static enum usbd_request_return_codes gdb_uart_control_request(usbd_device *dev, struct usb_setup_data *req,
uint8_t **buf, uint16_t *const len, void (**complete)(usbd_device *dev, struct usb_setup_data *req))
{
@ -95,7 +117,7 @@ static enum usbd_request_return_codes debug_uart_control_request(usbd_device *de
case USB_CDC_REQ_SET_LINE_CODING:
if (*len < sizeof(struct usb_cdc_line_coding))
return USBD_REQ_NOTSUPP;
usbuart_set_line_coding((struct usb_cdc_line_coding *)*buf);
aux_serial_set_encoding((struct usb_cdc_line_coding *)*buf);
return USBD_REQ_HANDLED;
}
return USBD_REQ_NOTSUPP;
@ -135,12 +157,12 @@ void usb_serial_set_config(usbd_device *dev, uint16_t value)
usbd_ep_setup(dev, (CDCACM_GDB_ENDPOINT + 1) | USB_REQ_TYPE_IN, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
/* Serial interface */
usbd_ep_setup(dev, CDCACM_UART_ENDPOINT, USB_ENDPOINT_ATTR_BULK, CDCACM_PACKET_SIZE / 2, usbuart_usb_out_cb);
usbd_ep_setup(dev, CDCACM_UART_ENDPOINT, USB_ENDPOINT_ATTR_BULK, CDCACM_PACKET_SIZE / 2, debug_uart_receive_callback);
usbd_ep_setup(
dev, CDCACM_UART_ENDPOINT | USB_REQ_TYPE_IN, USB_ENDPOINT_ATTR_BULK, CDCACM_PACKET_SIZE, usbuart_usb_in_cb);
dev, CDCACM_UART_ENDPOINT | USB_REQ_TYPE_IN, USB_ENDPOINT_ATTR_BULK, CDCACM_PACKET_SIZE, debug_uart_send_callback);
usbd_ep_setup(dev, (CDCACM_UART_ENDPOINT + 1) | USB_REQ_TYPE_IN, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
#if defined(PLATFORM_HAS_TRACESWO)
#ifdef PLATFORM_HAS_TRACESWO
/* Trace interface */
usbd_ep_setup(dev, TRACE_ENDPOINT | USB_REQ_TYPE_IN, USB_ENDPOINT_ATTR_BULK, 64, trace_buf_drain);
#endif
@ -155,4 +177,235 @@ void usb_serial_set_config(usbd_device *dev, uint16_t value)
*/
usb_serial_set_state(dev, GDB_IF_NO, CDCACM_GDB_ENDPOINT);
usb_serial_set_state(dev, UART_IF_NO, CDCACM_UART_ENDPOINT);
#ifdef ENABLE_DEBUG
initialise_monitor_handles();
#endif
}
void debug_uart_send_stdout(const uint8_t *const data, const size_t len)
{
for (size_t offset = 0; offset < len; offset += CDCACM_PACKET_SIZE) {
const size_t count = MIN(len - offset, CDCACM_PACKET_SIZE);
nvic_disable_irq(USB_IRQ);
/* XXX: Do we actually care if this fails? Possibly not.. */
usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, data + offset, count);
nvic_enable_irq(USB_IRQ);
}
}
#ifdef ENABLE_DEBUG
size_t debug_uart_write(const char *buf, const size_t len)
{
if (nvic_get_active_irq(USB_IRQ) || nvic_get_active_irq(USBUSART_IRQ) || nvic_get_active_irq(USBUSART_DMA_RX_IRQ))
return 0;
CM_ATOMIC_CONTEXT();
for (size_t i = 0; i < len && (usb_dbg_in + 1) % RX_FIFO_SIZE != usb_dbg_out; ++i) {
if (buf[i] == '\n') {
usb_dbg_buf[usb_dbg_in++] = '\r';
usb_dbg_in %= RX_FIFO_SIZE;
if ((usb_dbg_in + 1) % RX_FIFO_SIZE == usb_dbg_out)
break;
}
usb_dbg_buf[usb_dbg_in++] = buf[i];
usb_dbg_in %= RX_FIFO_SIZE;
}
debug_uart_run();
return len;
}
/*
* Copy data from fifo into continuous buffer. Return copied length.
*/
static uint32_t copy_from_fifo(char *dst, const char *src, uint32_t start, uint32_t end, uint32_t len, uint32_t fifo_sz)
{
uint32_t out_len = 0;
for (uint32_t buf_out = start; buf_out != end && out_len < len; buf_out %= fifo_sz)
dst[out_len++] = src[buf_out++];
return out_len;
}
/*
* Runs deferred processing for USBUSART RX, draining RX FIFO by sending
* characters to host PC via CDCACM. Allowed to write to FIFO OUT pointer.
*/
static void debug_uart_send_rx_packet(void)
{
rx_usb_trfr_cplt = false;
/* Calculate writing position in the FIFO */
const uint32_t buf_rx_in = (RX_FIFO_SIZE - dma_get_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN)) % RX_FIFO_SIZE;
/* Forcibly empty fifo if no USB endpoint.
* If fifo empty, nothing further to do. */
if (usb_get_config() != 1 || (buf_rx_in == buf_rx_out
#ifdef ENABLE_DEBUG
&& usb_dbg_in == usb_dbg_out
#endif
))
{
#ifdef ENABLE_DEBUG
usb_dbg_out = usb_dbg_in;
#endif
buf_rx_out = buf_rx_in;
/* Turn off LED */
usbuart_set_led_state(RX_LED_ACT, false);
rx_usb_trfr_cplt = true;
}
else
{
/* To avoid the need of sending ZLP don't transmit full packet.
* Also reserve space for copy function overrun.
*/
char packet_buf[CDCACM_PACKET_SIZE - 1 + sizeof(uint64_t)];
uint32_t packet_size;
#ifdef ENABLE_DEBUG
/* Copy data from DEBUG FIFO into local usb packet buffer */
packet_size = copy_from_fifo(packet_buf, usb_dbg_buf, usb_dbg_out, usb_dbg_in, CDCACM_PACKET_SIZE - 1, RX_FIFO_SIZE);
/* Send if buffer not empty */
if (packet_size)
{
const uint16_t written = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, packet_buf, packet_size);
usb_dbg_out = (usb_dbg_out + written) % RX_FIFO_SIZE;
return;
}
#endif
/* Copy data from uart RX FIFO into local usb packet buffer */
packet_size = copy_from_fifo(packet_buf, buf_rx, buf_rx_out, buf_rx_in, CDCACM_PACKET_SIZE - 1, RX_FIFO_SIZE);
/* Advance fifo out pointer by amount written */
const uint16_t written = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, packet_buf, packet_size);
buf_rx_out = (buf_rx_out + written) % RX_FIFO_SIZE;
}
}
void debug_uart_run(void)
{
nvic_disable_irq(USB_IRQ);
/* Enable LED */
usbuart_set_led_state(RX_LED_ACT, true);
/* Try to send a packet if usb is idle */
if (rx_usb_trfr_cplt)
debug_uart_send_rx_packet();
nvic_enable_irq(USB_IRQ);
}
static void debug_uart_send_callback(usbd_device *dev, uint8_t ep)
{
(void) ep;
(void) dev;
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
debug_uart_send_rx_packet();
#endif
}
#ifndef ENABLE_RTT
static void debug_uart_receive_callback(usbd_device *dev, uint8_t ep)
{
char *const transmit_buffer = aux_serial_current_transmit_buffer() + aux_serial_transmit_buffer_fullness();
const uint16_t len = usbd_ep_read_packet(dev, ep, transmit_buffer, 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) && !(RCC_APB1ENR & RCC_APB1ENR_USART2EN))
return;
#endif
aux_serial_send(len);
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
/* Disable USBUART TX packet reception if buffer does not have enough space */
if (TX_BUF_SIZE - aux_serial_transmit_buffer_fullness() < CDCACM_PACKET_SIZE)
usbd_ep_nak_set(dev, ep, 1);
#endif
}
#endif
/*
* newlib defines _write as a weak link'd function for user code to override.
*
* This function forms the root of the implementation of a variety of functions
* that can write to stdout/stderr, including printf().
*
* The result of this function is the number of bytes written.
*/
/* NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
int _write(const int file, const void *const ptr, const size_t len)
{
(void)file;
#ifdef PLATFORM_HAS_DEBUG
if (debug_bmp)
return debug_uart_write(ptr, len);
#endif
return len;
}
/*
* newlib defines isatty as a weak link'd function for user code to override.
*
* The result of this function is always 'true'.
*/
int isatty(const int file)
{
(void)file;
return true;
}
enum {
RDI_SYS_OPEN = 0x01,
};
typedef struct ex_frame {
uint32_t r0;
const uint32_t *params;
uint32_t r2;
uint32_t r3;
uint32_t r12;
uintptr_t lr;
uintptr_t return_address;
} ex_frame_s;
void debug_monitor_handler(void) __attribute__((used)) __attribute__((naked));
/*
* This implements the other half of the newlib syscall puzzle.
* When newlib is built for ARM, various calls that do file IO
* such as printf end up calling [_swiwrite](https://github.com/mirror/newlib-cygwin/blob/master/newlib/libc/sys/arm/syscalls.c#L317)
* and other similar low-level implementation functions. These
* generate `swi` instructions for the "RDI Monitor" and that lands us.. here.
*
* The RDI calling convention sticks the file number in r0, the buffer pointer in r1, and length in r2.
* ARMv7-M's SWI (SVC) instruction then takes all that and maps it into an exception frame on the stack.
*/
void debug_monitor_handler(void)
{
ex_frame_s *frame;
__asm__(
"mov %[frame], sp" :
[frame] "=r" (frame)
);
/* Make sure to return to the instruction after the SWI/BKPT */
frame->return_address += 2U;
switch (frame->r0) {
case RDI_SYS_OPEN:
frame->r0 = 1;
break;
default:
frame->r0 = UINT32_MAX;
}
__asm__("bx lr");
}
#endif

52
src/platforms/common/usbuart.h

@ -20,13 +20,53 @@
#ifndef __USBUART_H
#define __USBUART_H
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
#include "general.h"
void usbuart_init(void);
void usbuart_set_led_state(uint8_t ledn, bool state);
void debug_uart_run(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);
#define TX_LED_ACT (1 << 0)
#define RX_LED_ACT (1 << 1)
#if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
/* XXX: Does the st_usbfs_v2_usb_driver work on F3 with 128 byte buffers? */
#if defined(STM32F1) || defined(STM32F3) || defined(STM32F4)
#define USART_DMA_BUF_SHIFT 7U
#elif defined(STM32F0)
/* The st_usbfs_v2_usb_driver only works with up to 64-byte buffers on the F0 parts */
#define USART_DMA_BUF_SHIFT 6U
#endif
#define USART_DMA_BUF_SIZE (1U << USART_DMA_BUF_SHIFT)
#define RX_FIFO_SIZE (USART_DMA_BUF_SIZE)
#define TX_BUF_SIZE (USART_DMA_BUF_SIZE)
/* TX transfer complete */
extern bool tx_trfr_cplt;
/* RX Fifo buffer with space for copy fn overrun */
extern char buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)];
/* RX Fifo out pointer, writes assumed to be atomic */
extern uint8_t buf_rx_out;
/* RX usb transfer complete */
extern bool rx_usb_trfr_cplt;
#ifdef ENABLE_DEBUG
/* Debug Fifo buffer with space for copy fn overrun */
extern char usb_dbg_buf[RX_FIFO_SIZE + sizeof(uint64_t)];
/* Debug Fifo in pointer */
extern uint8_t usb_dbg_in;
/* Debug Fifo out pointer */
extern uint8_t usb_dbg_out;
#endif
#elif defined(LM4F)
#define FIFO_SIZE 128
/* RX Fifo buffer */
char buf_rx[FIFO_SIZE];
/* Fifo in pointer, writes assumed to be atomic, should be only incremented within RX ISR */
uint8_t buf_rx_in;
/* Fifo out pointer, writes assumed to be atomic, should be only incremented outside RX ISR */
uint8_t buf_rx_out;
#endif
#endif

3
src/platforms/f072/platform.c

@ -24,6 +24,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/rcc.h>
@ -85,7 +86,7 @@ void platform_init(void)
GPIO_OSPEED_2MHZ, NRST_PIN);
platform_timing_init();
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
}
void platform_nrst_set_val(bool assert)

3
src/platforms/f072/platform.h

@ -90,8 +90,6 @@
#define SWDIO_MODE_DRIVE() \
gpio_mode_setup(SWDIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, SWDIO_PIN);
/* FIXME: Does st_usbfs_v2_usb_driver work on F3 with 128 Bytes buffer*/
#define USART_DMA_BUF_SIZE 64
#define USB_DRIVER st_usbfs_v2_usb_driver
#define USB_IRQ NVIC_USB_IRQ
#define USB_ISR(x) usb_isr(x)
@ -145,7 +143,6 @@
#ifdef ENABLE_DEBUG
extern bool debug_bmp;
int usbuart_debug_write(const char *buf, size_t len);
# define DEBUG printf
#else
# define DEBUG(...)

3
src/platforms/f3/platform.c

@ -24,6 +24,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/f3/rcc.h>
@ -89,7 +90,7 @@ void platform_init(void)
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
gpio_set_af(GPIOA, GPIO_AF14, GPIO11 | GPIO12);
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
}
void platform_nrst_set_val(bool assert)

1
src/platforms/f3/platform.h

@ -138,7 +138,6 @@
#ifdef ENABLE_DEBUG
extern bool debug_bmp;
int usbuart_debug_write(const char *buf, size_t len);
# define DEBUG printf
#else
# define DEBUG(...)

3
src/platforms/f4discovery/platform.c

@ -25,6 +25,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/rcc.h>
@ -107,7 +108,7 @@ void platform_init(void)
platform_timing_init();
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
}
void platform_nrst_set_val(bool assert) { (void)assert; }

3
src/platforms/hydrabus/platform.c

@ -25,6 +25,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/rcc.h>
@ -77,7 +78,7 @@ void platform_init(void)
platform_timing_init();
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
}
void platform_nrst_set_val(bool assert) { (void)assert; }

3
src/platforms/launchpad-icdi/platform.c

@ -18,6 +18,7 @@
#include "gdb_if.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include <libopencm3/lm4f/rcc.h>
#include <libopencm3/lm4f/nvic.h>
@ -82,7 +83,7 @@ void platform_init(void)
__asm__("nop"); __asm__("nop"); __asm__("nop");
gpio_mode_setup(GPIOD_BASE, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO4|GPIO5);
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
usb_enable_interrupts(USB_INT_RESET | USB_INT_DISCON |
USB_INT_RESUME | USB_INT_SUSPEND, 0xff, 0xff);

15
src/platforms/native/platform.c

@ -25,6 +25,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include "morse.h"
#include <libopencm3/stm32/rcc.h>
@ -138,10 +139,6 @@ int platform_hwversion(void)
void platform_init(void)
{
SCS_DEMCR |= SCS_DEMCR_VC_MON_EN;
#ifdef ENABLE_DEBUG
void initialise_monitor_handles(void);
initialise_monitor_handles();
#endif
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
@ -221,14 +218,14 @@ void platform_init(void)
/* On hardware version 1 and 2, UART and SWD share connector pins.
* Don't enable UART if we're being debugged. */
if (platform_hwversion() == 0 || platform_hwversion() >= 3 || !(SCS_DEMCR & SCS_DEMCR_TRCENA))
usbuart_init();
aux_serial_init();
setup_vbus_irq();
}
void platform_nrst_set_val(bool assert)
{
gpio_set_val(TMS_PORT, TMS_PIN, 1);
gpio_set(TMS_PORT, TMS_PIN);
if ((platform_hwversion() == 0) ||
(platform_hwversion() >= 3)) {
gpio_set_val(NRST_PORT, NRST_PIN, assert);
@ -270,8 +267,7 @@ static void adc_init(void)
{
rcc_periph_clock_enable(RCC_ADC1);
gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_ANALOG, GPIO0);
gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0);
adc_power_off(ADC1);
adc_disable_scan_mode(ADC1);
@ -333,8 +329,7 @@ void platform_request_boot(void)
gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT, 0, USB_PU_PIN);
/* Drive boot request pin */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
gpio_clear(GPIOB, GPIO12);
}

1
src/platforms/native/platform.h

@ -36,7 +36,6 @@
# define PLATFORM_HAS_DEBUG
# define USBUART_DEBUG
extern bool debug_bmp;
int usbuart_debug_write(const char *buf, size_t len);
#endif
#define PLATFORM_IDENT " "

9
src/platforms/native/usbdfu.c

@ -39,7 +39,7 @@ int main(void)
{
/* Check the force bootloader pin*/
rcc_periph_clock_enable(RCC_GPIOB);
if(gpio_get(GPIOB, GPIO12))
if (gpio_get(GPIOB, GPIO12))
dfu_jump_app_if_valid();
dfu_protect(false);
@ -57,15 +57,13 @@ int main(void)
systick_counter_enable();
/* Configure the LED pins. */
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, LED_0 | LED_1 | LED_2);
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_0 | LED_1 | LED_2);
dfu_init(&st_usbfs_v1_usb_driver);
/* Configure the USB pull up pin. */
gpio_set(GPIOA, GPIO8);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
dfu_main();
}
@ -116,4 +114,3 @@ void sys_tick_handler(void)
}
}
}

9
src/platforms/stlink/platform.c

@ -25,6 +25,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/scb.h>
@ -48,10 +49,6 @@ void platform_init(void)
{
rev = detect_rev();
SCS_DEMCR |= SCS_DEMCR_VC_MON_EN;
#ifdef ENABLE_DEBUG
void initialise_monitor_handles(void);
initialise_monitor_handles();
#endif
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
if (rev == 0) {
led_idle_run = GPIO8;
@ -89,8 +86,8 @@ void platform_init(void)
/* Don't enable UART if we're being debugged. */
if (!(SCS_DEMCR & SCS_DEMCR_TRCENA))
usbuart_init();
adc_init();
aux_serial_init();
adc_init();
}
void platform_nrst_set_val(bool assert)

1
src/platforms/stlink/platform.h

@ -36,7 +36,6 @@
# define PLATFORM_HAS_DEBUG
# define USBUART_DEBUG
extern bool debug_bmp;
int usbuart_debug_write(const char *buf, size_t len);
#endif
#define PLATFORM_HAS_USBUART

4
src/platforms/stm32/rtt_if.c

@ -59,10 +59,10 @@ inline static bool recv_set_nak()
return recv_bytes_free() < 2 * CDCACM_PACKET_SIZE;
}
/* usbuart_usb_out_cb is called when usb uart has received new data for target.
/* debug_uart_receive_callback is called when usb uart has received new data for target.
this routine has to be fast */
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
void debug_uart_receive_callback(usbd_device *dev, uint8_t ep)
{
(void)dev;
(void)ep;

339
src/platforms/stm32/usbuart.c

@ -29,7 +29,9 @@
#include <libopencm3/usb/cdc.h>
#include "general.h"
#include "usbuart.h"
#include "usb.h"
#include "aux_serial.h"
#ifdef DMA_STREAM0
#define dma_channel_reset(dma, channel) dma_stream_reset(dma, channel)
@ -47,46 +49,30 @@
#define DMA_CGIF DMA_IFCR_CGIF_BIT
#endif
#define TX_LED_ACT (1 << 0)
#define RX_LED_ACT (1 << 1)
/* F072 with st_usbfs_v2_usb_drive drops characters at the 64 byte boundary!*/
#if !defined(USART_DMA_BUF_SIZE)
# define USART_DMA_BUF_SIZE 128
#endif
#define RX_FIFO_SIZE (USART_DMA_BUF_SIZE)
#define TX_BUF_SIZE (USART_DMA_BUF_SIZE)
/* TX double buffer */
static uint8_t buf_tx[TX_BUF_SIZE * 2];
/* Active buffer part idx */
static uint8_t buf_tx_act_idx;
/* Active buffer part used capacity */
static uint8_t buf_tx_act_sz;
uint8_t aux_serial_transmit_buffer_consumed;
/* TX transfer complete */
static bool tx_trfr_cplt = true;
bool tx_trfr_cplt = true;
/* RX Fifo buffer with space for copy fn overrun */
static uint8_t buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)];
char buf_rx[RX_FIFO_SIZE + sizeof(uint64_t)];
/* RX Fifo out pointer, writes assumed to be atomic */
static uint8_t buf_rx_out;
uint8_t buf_rx_out;
/* RX usb transfer complete */
static bool rx_usb_trfr_cplt = true;
bool rx_usb_trfr_cplt = true;
#ifdef USBUART_DEBUG
#ifdef ENABLE_DEBUG
/* Debug Fifo buffer with space for copy fn overrun */
static uint8_t usb_dbg_buf[RX_FIFO_SIZE + sizeof(uint64_t)];
char usb_dbg_buf[RX_FIFO_SIZE + sizeof(uint64_t)];
/* Debug Fifo in pointer */
static uint8_t usb_dbg_in;
uint8_t usb_dbg_in;
/* Debug Fifo out pointer */
static uint8_t usb_dbg_out;
uint8_t usb_dbg_out;
#endif
static void usbuart_run(void);
/*
* Update led state atomically respecting RX anb TX states.
*/
static void usbuart_set_led_state(uint8_t ledn, bool state)
void usbuart_set_led_state(uint8_t ledn, bool state)
{
CM_ATOMIC_CONTEXT();
@ -105,7 +91,7 @@ static void usbuart_set_led_state(uint8_t ledn, bool state)
}
}
void usbuart_init(void)
void aux_serial_init(void)
{
/* Enable clocks */
rcc_periph_clock_enable(USBUSART_CLK);
@ -192,237 +178,6 @@ void usbuart_init(void)
usart_enable_rx_dma(USBUSART);
}
void usbuart_send_stdout(const uint8_t *data, uint32_t len)
{
while (len) {
uint32_t cnt = CDCACM_PACKET_SIZE;
if (cnt > len)
cnt = len;
nvic_disable_irq(USB_IRQ);
cnt = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, data, cnt);
nvic_enable_irq(USB_IRQ);
data += cnt;
len -= cnt;
}
}
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.
*/
static uint32_t copy_from_fifo(uint8_t *dst, const uint8_t *src, uint32_t start, uint32_t end, uint32_t len, uint32_t fifo_sz)
{
uint32_t out_len = 0;
for (uint32_t buf_out = start; buf_out != end && out_len < len; buf_out %= fifo_sz)
dst[out_len++] = src[buf_out++];
return out_len;
}
/*
* Changes USBUSART TX buffer in which data is accumulated from USB.
* Filled buffer is submitted to DMA for transfer.
*/
static void usbuart_change_dma_tx_buf(void)
{
/* Select buffer for transmission */
uint8_t *const tx_buf_ptr = &buf_tx[buf_tx_act_idx * TX_BUF_SIZE];
/* Configure DMA */
dma_set_memory_address(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, (uint32_t)tx_buf_ptr);
dma_set_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN, buf_tx_act_sz);
dma_enable_channel(USBUSART_DMA_BUS, USBUSART_DMA_TX_CHAN);
/* Change active buffer */
buf_tx_act_sz = 0;
buf_tx_act_idx ^= 1;
}
#ifndef ENABLE_RTT
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
{
(void)ep;
usbd_ep_nak_set(dev, CDCACM_UART_ENDPOINT, 1);
/* Read new packet directly into TX buffer */
uint8_t *const tx_buf_ptr = &buf_tx[buf_tx_act_idx * TX_BUF_SIZE];
const uint16_t len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT,
tx_buf_ptr + buf_tx_act_sz, 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) &&
!(RCC_APB1ENR & RCC_APB1ENR_USART2EN))
{
usbd_ep_nak_set(dev, CDCACM_UART_ENDPOINT, 0);
return;
}
#endif
if (len)
{
buf_tx_act_sz += len;
/* If DMA is idle, schedule new transfer */
if (tx_trfr_cplt)
{
tx_trfr_cplt = false;
usbuart_change_dma_tx_buf();
/* Enable LED */
usbuart_set_led_state(TX_LED_ACT, true);
}
}
/* Enable USBUART TX packet reception if buffer has enough space */
if (TX_BUF_SIZE - buf_tx_act_sz >= CDCACM_PACKET_SIZE)
usbd_ep_nak_set(dev, CDCACM_UART_ENDPOINT, 0);
}
#endif
#ifdef USBUART_DEBUG
int usbuart_debug_write(const char *buf, size_t len)
{
if (nvic_get_active_irq(USB_IRQ) || nvic_get_active_irq(USBUSART_IRQ) || nvic_get_active_irq(USBUSART_DMA_RX_IRQ))
return 0;
CM_ATOMIC_CONTEXT();
for (size_t i = 0; i < len && (usb_dbg_in + 1) % RX_FIFO_SIZE != usb_dbg_out; i++)
{
if (buf[i] == '\n')
{
usb_dbg_buf[usb_dbg_in++] = '\r';
usb_dbg_in %= RX_FIFO_SIZE;
if ((usb_dbg_in + 1) % RX_FIFO_SIZE == usb_dbg_out)
break;
}
usb_dbg_buf[usb_dbg_in++] = buf[i];
usb_dbg_in %= RX_FIFO_SIZE;
}
usbuart_run();
return len;
}
#endif
/*
* Runs deferred processing for USBUSART RX, draining RX FIFO by sending
* characters to host PC via CDCACM. Allowed to write to FIFO OUT pointer.
*/
static void usbuart_send_rx_packet(void)
{
rx_usb_trfr_cplt = false;
/* Calculate writing position in the FIFO */
const uint32_t buf_rx_in = (RX_FIFO_SIZE - dma_get_number_of_data(USBUSART_DMA_BUS, USBUSART_DMA_RX_CHAN)) % RX_FIFO_SIZE;
/* Forcibly empty fifo if no USB endpoint.
* If fifo empty, nothing further to do. */
if (usb_get_config() != 1 || (buf_rx_in == buf_rx_out
#ifdef USBUART_DEBUG
&& usb_dbg_in == usb_dbg_out
#endif
))
{
#ifdef USBUART_DEBUG
usb_dbg_out = usb_dbg_in;
#endif
buf_rx_out = buf_rx_in;
/* Turn off LED */
usbuart_set_led_state(RX_LED_ACT, false);
rx_usb_trfr_cplt = true;
}
else
{
/* To avoid the need of sending ZLP don't transmit full packet.
* Also reserve space for copy function overrun.
*/
uint8_t packet_buf[CDCACM_PACKET_SIZE - 1 + sizeof(uint64_t)];
uint32_t packet_size;
#ifdef USBUART_DEBUG
/* Copy data from DEBUG FIFO into local usb packet buffer */
packet_size = copy_from_fifo(packet_buf, usb_dbg_buf, usb_dbg_out, usb_dbg_in, CDCACM_PACKET_SIZE - 1, RX_FIFO_SIZE);
/* Send if buffer not empty */
if (packet_size)
{
const uint16_t written = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, packet_buf, packet_size);
usb_dbg_out = (usb_dbg_out + written) % RX_FIFO_SIZE;
return;
}
#endif
/* Copy data from uart RX FIFO into local usb packet buffer */
packet_size = copy_from_fifo(packet_buf, buf_rx, buf_rx_out, buf_rx_in, CDCACM_PACKET_SIZE - 1, RX_FIFO_SIZE);
/* Advance fifo out pointer by amount written */
const uint16_t written = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, packet_buf, packet_size);
buf_rx_out = (buf_rx_out + written) % RX_FIFO_SIZE;
}
}
void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep)
{
(void) ep;
(void) dev;
usbuart_send_rx_packet();
}
static void usbuart_run(void)
{
nvic_disable_irq(USB_IRQ);
/* Enable LED */
usbuart_set_led_state(RX_LED_ACT, true);
/* Try to send a packet if usb is idle */
if (rx_usb_trfr_cplt)
usbuart_send_rx_packet();
nvic_enable_irq(USB_IRQ);
}
#if defined(USART_ICR)
#define USBUSART_ISR_TEMPLATE(USART, DMA_IRQ) do { \
nvic_disable_irq(DMA_IRQ); \
@ -434,7 +189,7 @@ static void usbuart_run(void)
/* If line is now idle, then transmit a packet */ \
if (isIdle) { \
USART_ICR(USART) = USART_ICR_IDLECF; \
usbuart_run(); \
debug_uart_run(); \
} \
\
nvic_enable_irq(DMA_IRQ); \
@ -452,7 +207,7 @@ static void usbuart_run(void)
/* On the older uarts, the sequence "read flags", */ \
/* "read DR" clears the flags */ \
\
usbuart_run(); \
debug_uart_run(); \
} \
\
nvic_enable_irq(DMA_IRQ); \
@ -502,9 +257,9 @@ void USBUSART2_ISR(void)
/* If new buffer is ready, continue transmission. \
* Otherwise report transfer completion. \
*/ \
if (buf_tx_act_sz) \
if (aux_serial_transmit_buffer_consumed) \
{ \
usbuart_change_dma_tx_buf(); \
aux_serial_switch_transmit_buffers(); \
usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); \
} \
else \
@ -543,7 +298,7 @@ void USBUSART2_DMA_TX_ISR(void)
/* Clear flags */ \
dma_clear_interrupt_flags(USBUSART_DMA_BUS, DMA_RX_CHAN, DMA_CGIF); \
/* Transmit a packet */ \
usbuart_run(); \
debug_uart_run(); \
\
nvic_enable_irq(USART_IRQ); \
} while(0)
@ -578,61 +333,3 @@ void USBUSART_DMA_RXTX_ISR(void)
USBUSART_DMA_TX_ISR();
}
#endif
#ifdef ENABLE_DEBUG
enum {
RDI_SYS_OPEN = 0x01,
RDI_SYS_WRITE = 0x05,
RDI_SYS_ISTTY = 0x09,
};
int rdi_write(int fn, const char *buf, size_t len)
{
(void)fn;
#if defined(PLATFORM_HAS_DEBUG)
if (debug_bmp)
return len - usbuart_debug_write(buf, len);
#else
(void)buf;
(void)len;
#endif
return 0;
}
struct ex_frame {
union {
int syscall;
int retval;
};
const int *params;
uint32_t r2, r3, r12, lr, pc;
};
void debug_monitor_handler_c(struct ex_frame *sp)
{
/* Return to after breakpoint instruction */
sp->pc += 2;
switch (sp->syscall) {
case RDI_SYS_OPEN:
sp->retval = 1;
break;
case RDI_SYS_WRITE:
sp->retval = rdi_write(sp->params[0], (void*)sp->params[1], sp->params[2]);
break;
case RDI_SYS_ISTTY:
sp->retval = 1;
break;
default:
sp->retval = -1;
}
}
__asm__(".globl debug_monitor_handler\n"
".thumb_func\n"
"debug_monitor_handler: \n"
" mov r0, sp\n"
" b debug_monitor_handler_c\n");
#endif

7
src/platforms/swlink/platform.c

@ -26,6 +26,7 @@
#include "general.h"
#include "usb.h"
#include "usbuart.h"
#include "aux_serial.h"
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/scb.h>
@ -50,10 +51,6 @@ void platform_init(void)
{
uint32_t data;
SCS_DEMCR |= SCS_DEMCR_VC_MON_EN;
#ifdef ENABLE_DEBUG
void initialise_monitor_handles(void);
initialise_monitor_handles();
#endif
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
rev = detect_rev();
/* Enable peripherals */
@ -110,7 +107,7 @@ void platform_init(void)
platform_timing_init();
blackmagic_usb_init();
usbuart_init();
aux_serial_init();
}
void platform_nrst_set_val(bool assert)

1
src/platforms/swlink/platform.h

@ -33,7 +33,6 @@
# define PLATFORM_HAS_DEBUG
# define USBUART_DEBUG
extern bool debug_bmp;
int usbuart_debug_write(const char *buf, size_t len);
#endif
#define PLATFORM_HAS_USBUART

72
src/platforms/tm4c/usbuart.c

@ -33,18 +33,20 @@
#define FIFO_SIZE 128
/* RX Fifo buffer */
static uint8_t buf_rx[FIFO_SIZE];
char buf_rx[FIFO_SIZE];
/* Fifo in pointer, writes assumed to be atomic, should be only incremented within RX ISR */
static uint8_t buf_rx_in;
uint8_t buf_rx_in;
/* Fifo out pointer, writes assumed to be atomic, should be only incremented outside RX ISR */
static uint8_t buf_rx_out;
uint8_t buf_rx_out;
void usbuart_init(void)
void aux_serial_init(void)
{
UART_PIN_SETUP();
periph_clock_enable(USBUART_CLK);
__asm__("nop"); __asm__("nop"); __asm__("nop");
__asm__("nop");
__asm__("nop");
__asm__("nop");
uart_disable(USBUART);
@ -74,66 +76,6 @@ void usbuart_init(void)
nvic_enable_irq(USBUART_IRQ);
}
void usbuart_send_stdout(const uint8_t *data, uint32_t len)
{
while (len) {
uint32_t cnt = CDCACM_PACKET_SIZE;
if (cnt > len)
cnt = len;
nvic_disable_irq(USBUART_IRQ);
cnt = usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, data, cnt);
nvic_enable_irq(USBUART_IRQ);
data += cnt;
len -= cnt;
}
}
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)
{
(void)ep;
char buf[CDCACM_PACKET_SIZE];
int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT,
buf, CDCACM_PACKET_SIZE);
for(int i = 0; i < len; i++)
uart_send_blocking(USBUART, buf[i]);
}
#endif
void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep)
{
(void) dev;
(void) ep;
}
/*
* Read a character from the UART RX and stuff it in a software FIFO.
* Allowed to read from FIFO out pointer, but not write to it.

2
src/target/target.c

@ -702,7 +702,7 @@ int tc_write(target *t, int fd, target_addr buf, unsigned int count)
if (cnt > count)
cnt = count;
target_mem_read(t, tmp, buf, cnt);
usbuart_send_stdout(tmp, cnt);
debug_uart_send_stdout(tmp, cnt);
count -= cnt;
buf += cnt;
}

Loading…
Cancel
Save