From b02e305a7049c4a026bb9054b636e0a3941b6e35 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Sun, 21 Aug 2022 18:48:38 +0100 Subject: [PATCH] aux_serial: Redone the LED status system and cleaned up the last of the old usbuart stuff --- src/platforms/common/aux_serial.c | 47 ++++++++++++------------------- src/platforms/common/aux_serial.h | 8 +++++- src/platforms/common/usb_serial.c | 5 +--- src/platforms/common/usbuart.h | 28 ------------------ 4 files changed, 26 insertions(+), 62 deletions(-) delete mode 100644 src/platforms/common/usbuart.h diff --git a/src/platforms/common/aux_serial.c b/src/platforms/common/aux_serial.c index ca44140..d4a5bc5 100644 --- a/src/platforms/common/aux_serial.c +++ b/src/platforms/common/aux_serial.c @@ -34,22 +34,23 @@ #include #include "general.h" -#include "usbuart.h" #include "usb_serial.h" #include "aux_serial.h" static char aux_serial_receive_buffer[AUX_UART_BUFFER_SIZE]; /* Fifo in pointer, writes assumed to be atomic, should be only incremented within RX ISR */ -static uint8_t aux_serial_receive_write_index; +static uint8_t aux_serial_receive_write_index = 0; /* Fifo out pointer, writes assumed to be atomic, should be only incremented outside RX ISR */ -static uint8_t aux_serial_receive_read_index; +static uint8_t aux_serial_receive_read_index = 0; #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) static char aux_serial_transmit_buffer[2U][AUX_UART_BUFFER_SIZE]; -static uint8_t aux_serial_transmit_buffer_index; -static uint8_t aux_serial_transmit_buffer_consumed; +static uint8_t aux_serial_transmit_buffer_index = 0; +static uint8_t aux_serial_transmit_buffer_consumed = 0; static bool aux_serial_transmit_complete = true; +static volatile uint8_t aux_serial_led_state = 0; + #ifdef DMA_STREAM0 #define dma_channel_reset(dma, channel) dma_stream_reset(dma, channel) #define dma_enable_channel(dma, channel) dma_enable_stream(dma, channel) @@ -248,26 +249,17 @@ void aux_serial_set_encoding(struct usb_cdc_line_coding *coding) } #if defined(STM32F0) || defined(STM32F1) || defined(STM32F3) || defined(STM32F4) -/* - * Update led state atomically respecting RX anb TX states. - */ -void usbuart_set_led_state(uint8_t ledn, bool state) +void aux_serial_set_led(const aux_serial_led_e led) { - CM_ATOMIC_CONTEXT(); + aux_serial_led_state |= led; + gpio_set(LED_PORT_UART, LED_UART); +} - static uint8_t led_state = 0; - - if (state) - { - led_state |= ledn; - gpio_set(LED_PORT_UART, LED_UART); - } - else - { - led_state &= ~ledn; - if (!led_state) - gpio_clear(LED_PORT_UART, LED_UART); - } +void aux_serial_clear_led(const aux_serial_led_e led) +{ + aux_serial_led_state &= ~led; + if (!aux_serial_led_state) + gpio_clear(LED_PORT_UART, LED_UART); } char *aux_serial_current_transmit_buffer(void) @@ -305,9 +297,7 @@ void aux_serial_send(const size_t len) { aux_serial_transmit_complete = false; aux_serial_switch_transmit_buffers(); - - /* Enable LED */ - usbuart_set_led_state(TX_LED_ACT, true); + aux_serial_set_led(AUX_SERIAL_LED_TX); } } @@ -326,8 +316,7 @@ bool aux_serial_receive_buffer_empty(void) void aux_serial_drain_receive_buffer(void) { aux_serial_receive_read_index = aux_serial_receive_write_index; - /* Turn off LED */ - usbuart_set_led_state(RX_LED_ACT, false); + aux_serial_clear_led(AUX_SERIAL_LED_RX); } void aux_serial_stage_receive_buffer(void) @@ -371,7 +360,7 @@ static void aux_serial_dma_transmit_isr(const uint8_t dma_tx_channel) aux_serial_switch_transmit_buffers(); usbd_ep_nak_set(usbdev, CDCACM_UART_ENDPOINT, 0); } else { - usbuart_set_led_state(TX_LED_ACT, false); + aux_serial_clear_led(AUX_SERIAL_LED_TX); aux_serial_transmit_complete = true; } diff --git a/src/platforms/common/aux_serial.h b/src/platforms/common/aux_serial.h index 36e164c..934c447 100644 --- a/src/platforms/common/aux_serial.h +++ b/src/platforms/common/aux_serial.h @@ -43,7 +43,13 @@ 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 usbuart_set_led_state(uint8_t ledn, bool state); +typedef enum aux_serial_led { + AUX_SERIAL_LED_TX = (1U << 0U), + AUX_SERIAL_LED_RX = (1U << 1U) +} aux_serial_led_e; + +void aux_serial_set_led(aux_serial_led_e led); +void aux_serial_clear_led(aux_serial_led_e led); void aux_serial_switch_transmit_buffers(void); #endif diff --git a/src/platforms/common/usb_serial.c b/src/platforms/common/usb_serial.c index 92c3bd4..d129d7e 100644 --- a/src/platforms/common/usb_serial.c +++ b/src/platforms/common/usb_serial.c @@ -46,7 +46,6 @@ #ifdef PLATFORM_HAS_TRACESWO #include "traceswo.h" #endif -#include "usbuart.h" #include "aux_serial.h" #include @@ -261,9 +260,7 @@ static void debug_serial_send_data(void) void debug_serial_run(void) { nvic_disable_irq(USB_IRQ); - - /* Enable LED */ - usbuart_set_led_state(RX_LED_ACT, true); + aux_serial_set_led(AUX_SERIAL_LED_RX); /* Try to send a packet if usb is idle */ if (debug_serial_send_complete) diff --git a/src/platforms/common/usbuart.h b/src/platforms/common/usbuart.h deleted file mode 100644 index 5de5fca..0000000 --- a/src/platforms/common/usbuart.h +++ /dev/null @@ -1,28 +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 "general.h" - -#define TX_LED_ACT (1 << 0) -#define RX_LED_ACT (1 << 1) - -#endif