From 4ef9921e5b8799638c525b55955977560dcb16e9 Mon Sep 17 00:00:00 2001 From: Arjan van Vught Date: Mon, 11 Mar 2024 16:04:32 +0100 Subject: [PATCH] Renamed *.c files to *.cpp --- .../softuart0/{gd32_uart0.cpp => uart0.cpp} | 98 +++++++++++-------- lib-gd32/src/{gd32_uart0.cpp => uart0.cpp} | 4 +- .../src/uart0/{gd32_uart0.cpp => uart0.cpp} | 2 +- .../console/i2c/{console.c => console.cpp} | 24 ++--- .../console/null/{console.c => console.cpp} | 29 ++---- .../console/uart0/{console.c => console.cpp} | 36 +++---- lib-hal/include/console.h | 2 +- 7 files changed, 104 insertions(+), 91 deletions(-) rename lib-gd32/src/softuart0/{gd32_uart0.cpp => uart0.cpp} (68%) rename lib-gd32/src/{gd32_uart0.cpp => uart0.cpp} (94%) rename lib-gd32/src/uart0/{gd32_uart0.cpp => uart0.cpp} (98%) rename lib-hal/console/i2c/{console.c => console.cpp} (93%) rename lib-hal/console/null/{console.c => console.cpp} (67%) rename lib-hal/console/uart0/{console.c => console.cpp} (84%) diff --git a/lib-gd32/src/softuart0/gd32_uart0.cpp b/lib-gd32/src/softuart0/uart0.cpp similarity index 68% rename from lib-gd32/src/softuart0/gd32_uart0.cpp rename to lib-gd32/src/softuart0/uart0.cpp index 3b3d6ab..60aa09b 100644 --- a/lib-gd32/src/softuart0/gd32_uart0.cpp +++ b/lib-gd32/src/softuart0/uart0.cpp @@ -1,8 +1,8 @@ /** - * @file gd32_uart0.c + * @file uart0.c * */ -/* Copyright (C) 2023 by Arjan van Vught mailto:info@gd32-dmx.org +/* Copyright (C) 2023-2024 by Arjan van Vught mailto:info@gd32-dmx.org * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,37 +23,46 @@ * THE SOFTWARE. */ -#if defined (NDEBUG) -# undef NDEBUG -#endif - #include #include #include #include "gd32.h" -#if !defined (SOFTUART_TX_PINx) -# define SOFTUART_TX_PINx GPIO_PIN_9 -# define SOFTUART_TX_GPIOx GPIOA -# define SOFTUART_TX_RCU_GPIOx RCU_GPIOA +#include "debug.h" + +#if defined (GD32H7XX) +# define TIMERx TIMER15 +# define RCU_TIMERx RCU_TIMER15 +# define TIMERx_IRQHandler TIMER15_IRQHandler +# define TIMERx_IRQn TIMER15_IRQn +#else +# define TIMERx TIMER9 +# define RCU_TIMERx RCU_TIMER9 +# define TIMERx_IRQHandler TIMER0_UP_TIMER9_IRQHandler +# define TIMERx_IRQn TIMER0_UP_TIMER9_IRQn #endif -#if defined (GD32F4XX) -# define TIMER_CLOCK (APB2_CLOCK_FREQ * 2) +#if defined (GD32H7XX) +# define TIMER_CLOCK_FREQ (AHB_CLOCK_FREQ) +#elif defined (GD32F4XX) +# define TIMER_CLOCK_FREQ (APB2_CLOCK_FREQ * 2) #else -# define TIMER_CLOCK (APB2_CLOCK_FREQ) +# define TIMER_CLOCK_FREQ (APB2_CLOCK_FREQ) #endif -#define BAUD_RATE (115200U) -#define TIMER_PERIOD ((TIMER_CLOCK / BAUD_RATE) - 1U) -#define BUFFER_SIZE (128U) +#if !defined (SOFTUART_TX_PINx) +# define SOFTUART_TX_PINx GPIO_PIN_9 +# define SOFTUART_TX_GPIOx GPIOA +# define SOFTUART_TX_RCU_GPIOx RCU_GPIOA +#endif + +#define BAUD_RATE (115200U) +#define TIMER_PERIOD ((TIMER_CLOCK_FREQ / BAUD_RATE) - 1U) +#define BUFFER_SIZE (128U) -typedef enum { - SOFTUART_IDLE, - SOFTUART_START_BIT, - SOFTUART_DATA, - SOFTUART_STOP_BIT, +typedef enum { + SOFTUART_IDLE, SOFTUART_START_BIT, SOFTUART_DATA, SOFTUART_STOP_BIT, } softuart_state; struct circular_buffer { @@ -73,8 +82,8 @@ static bool is_circular_buffer_empty() { } extern "C" { -void TIMER0_UP_TIMER9_IRQHandler() { - if ((TIMER_INTF(TIMER9) & TIMER_INT_FLAG_UP) == TIMER_INT_FLAG_UP) { +void TIMERx_IRQHandler() { + if ((TIMER_INTF(TIMERx) & TIMER_INT_FLAG_UP) == TIMER_INT_FLAG_UP) { #if defined (LED3_GPIO_PINx) GPIO_BOP(LED3_GPIOx) = LED3_GPIO_PINx; #endif @@ -109,7 +118,7 @@ void TIMER0_UP_TIMER9_IRQHandler() { if (is_circular_buffer_empty()) { s_state = SOFTUART_IDLE; - timer_disable(TIMER9); + timer_disable(TIMERx); } else { s_state = SOFTUART_START_BIT; } @@ -123,17 +132,22 @@ void TIMER0_UP_TIMER9_IRQHandler() { #endif } - timer_interrupt_flag_clear(TIMER9, ~0); + timer_interrupt_flag_clear(TIMERx, ~0); } void uart0_init() { + s_state = SOFTUART_IDLE; + s_circular_buffer.head = 0; + s_circular_buffer.tail = 0; + s_circular_buffer.full = false; + #if defined (LED3_GPIO_PINx) rcu_periph_clock_enable (LED3_RCU_GPIOx); -# if !defined (GD32F4XX) +# if defined (GPIO_INIT) gpio_init(LED3_GPIOx, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED3_GPIO_PINx); # else gpio_mode_set(LED3_GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, LED3_GPIO_PINx); - gpio_output_options_set(LED3_GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED3_GPIO_PINx); + gpio_output_options_set(LED3_GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED, LED3_GPIO_PINx); gpio_af_set(LED3_GPIOx, GPIO_AF_0, LED3_GPIO_PINx); # endif @@ -142,39 +156,43 @@ void uart0_init() { rcu_periph_clock_enable (SOFTUART_TX_RCU_GPIOx); -#if !defined (GD32F4XX) +#if defined (GPIO_INIT) gpio_init(SOFTUART_TX_GPIOx, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SOFTUART_TX_PINx); #else gpio_mode_set(SOFTUART_TX_GPIOx, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, SOFTUART_TX_PINx); - gpio_output_options_set(SOFTUART_TX_GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SOFTUART_TX_PINx); + gpio_output_options_set(SOFTUART_TX_GPIOx, GPIO_OTYPE_PP, GPIO_OSPEED, SOFTUART_TX_PINx); gpio_af_set(SOFTUART_TX_GPIOx, GPIO_AF_0, SOFTUART_TX_PINx); #endif GPIO_BOP(SOFTUART_TX_GPIOx) = SOFTUART_TX_PINx; - rcu_periph_clock_enable(RCU_TIMER9); + rcu_periph_clock_enable(RCU_TIMERx); - timer_deinit(TIMER9); + timer_deinit(TIMERx); timer_parameter_struct timer_initpara; + timer_struct_para_init(&timer_initpara); + timer_initpara.prescaler = 0; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = TIMER_PERIOD; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; + timer_initpara.repetitioncounter = 0; - timer_init(TIMER9, &timer_initpara); + timer_init(TIMERx, &timer_initpara); - timer_flag_clear(TIMER9, ~0); - timer_interrupt_flag_clear(TIMER9, ~0); + timer_flag_clear(TIMERx, ~0); + timer_interrupt_flag_clear(TIMERx, ~0); - timer_interrupt_enable(TIMER9, TIMER_INT_UP); + timer_interrupt_enable(TIMERx, TIMER_INT_UP); - NVIC_SetPriority(TIMER0_UP_TIMER9_IRQn, 2); - NVIC_EnableIRQ(TIMER0_UP_TIMER9_IRQn); + NVIC_SetPriority(TIMERx_IRQn, 2); + NVIC_EnableIRQ(TIMERx_IRQn); } -static void _putc(int c) { +static void _putc(const int c) { + //FIXME deadlock when timer is not running while (s_circular_buffer.full) ; @@ -183,8 +201,8 @@ static void _putc(int c) { s_circular_buffer.full = s_circular_buffer.head == s_circular_buffer.tail; if (s_state == SOFTUART_IDLE) { - timer_counter_value_config(TIMER9, 0); - timer_enable(TIMER9); + timer_counter_value_config(TIMERx, 0); + timer_enable(TIMERx); s_state = SOFTUART_START_BIT; } } diff --git a/lib-gd32/src/gd32_uart0.cpp b/lib-gd32/src/uart0.cpp similarity index 94% rename from lib-gd32/src/gd32_uart0.cpp rename to lib-gd32/src/uart0.cpp index 50d04b5..02b35aa 100644 --- a/lib-gd32/src/gd32_uart0.cpp +++ b/lib-gd32/src/uart0.cpp @@ -1,8 +1,8 @@ /** - * @file gd32_uart0.cpp + * @file uart0.cpp * */ -/* Copyright (C) 2023 by Arjan van Vught mailto:info@gd32-dmx.org +/* Copyright (C) 2023-2024 by Arjan van Vught mailto:info@gd32-dmx.org * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/lib-gd32/src/uart0/gd32_uart0.cpp b/lib-gd32/src/uart0/uart0.cpp similarity index 98% rename from lib-gd32/src/uart0/gd32_uart0.cpp rename to lib-gd32/src/uart0/uart0.cpp index b52679b..a5a8b06 100644 --- a/lib-gd32/src/uart0/gd32_uart0.cpp +++ b/lib-gd32/src/uart0/uart0.cpp @@ -1,5 +1,5 @@ /** - * @file gd32_uart0.cpp + * @file uart0.cpp * */ /* Copyright (C) 2021-2024 by Arjan van Vught mailto:info@gd32-dmx.org diff --git a/lib-hal/console/i2c/console.c b/lib-hal/console/i2c/console.cpp similarity index 93% rename from lib-hal/console/i2c/console.c rename to lib-hal/console/i2c/console.cpp index d33e8a7..5d3f055 100644 --- a/lib-hal/console/i2c/console.c +++ b/lib-hal/console/i2c/console.cpp @@ -1,8 +1,8 @@ /** - * @file console.c + * @file console.cpp * */ -/* Copyright (C) 2022-2023 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2022-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -97,7 +97,7 @@ static bool is_connected(const uint8_t address, const uint32_t baudrate) { } /* This is known to corrupt the Atmel AT24RF08 EEPROM */ - return FUNC_PREFIX(i2c_write(NULL, 0)) == 0; + return FUNC_PREFIX(i2c_write(nullptr, 0)) == 0; } static void setup() { @@ -108,8 +108,8 @@ static void setup() { static void write_register(uint8_t nRegister, uint8_t nValue) { char buffer[2]; - buffer[0] = (char)(nRegister); - buffer[1] = (char)(nValue); + buffer[0] = static_cast(nRegister); + buffer[1] = static_cast(nValue); setup(); FUNC_PREFIX(i2c_write(buffer, 2)); @@ -121,13 +121,13 @@ static uint8_t read_byte() { setup(); FUNC_PREFIX(i2c_read(buffer, 1)); - return (uint8_t) (buffer[0]); + return static_cast(buffer[0]); } static uint8_t read_register(uint8_t nRegister) { char buffer[1]; - buffer[0] = (char) (nRegister); + buffer[0] = static_cast(nRegister); setup(); FUNC_PREFIX(i2c_write(buffer, 1)); @@ -135,7 +135,7 @@ static uint8_t read_register(uint8_t nRegister) { return read_byte(); } -inline static bool is_writable() { +static bool is_writable() { return (read_register(SC16IS7X0_TXLVL) != 0); } @@ -157,7 +157,8 @@ static void set_baud(uint32_t nBaud) { write_register(SC16IS7X0_LCR, nRegisterLCR); } -void __attribute__((cold)) console_init(void) { +extern "C" { +void __attribute__((cold)) console_init() { FUNC_PREFIX(i2c_begin()); s_isConnected = is_connected(CONSOLE_I2C_ADDRESS, 100000); @@ -303,14 +304,15 @@ void console_set_bg_color(uint16_t bg) { } } -void console_status(uint32_t color, const char *s) { +void console_status(uint32_t nColour, const char *s) { if (!s_isConnected) { return; } - console_set_fg_color((uint16_t) color); + console_set_fg_color(static_cast(nColour)); console_set_bg_color(CONSOLE_BLACK); console_puts(s); console_putc('\n'); console_set_fg_color(CONSOLE_WHITE); } +} diff --git a/lib-hal/console/null/console.c b/lib-hal/console/null/console.cpp similarity index 67% rename from lib-hal/console/null/console.c rename to lib-hal/console/null/console.cpp index 49df711..11b185b 100755 --- a/lib-hal/console/null/console.c +++ b/lib-hal/console/null/console.cpp @@ -1,8 +1,8 @@ /** - * @file console.c + * @file console.cpp * */ -/* Copyright (C) 2023 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2023-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,22 +23,13 @@ * THE SOFTWARE. */ -#include +#include -void console_init(void) { -} - -void console_putc(__attribute__((unused)) int i) { -} - -void console_puts(__attribute__((unused)) const char *p) { -} - -void console_write(__attribute__((unused)) const char *p, __attribute__((unused)) unsigned int i) { -} - -void console_status(__attribute__((unused)) uint32_t i, __attribute__((unused)) const char *p) { -} - -void console_error(__attribute__((unused)) const char *p) { +extern "C" { +void console_puts([[maybe_unused]] const char *p) {} +void console_write([[maybe_unused]] const char *p, [[maybe_unused]] unsigned int i) {} +void console_status([[maybe_unused]] uint32_t i, [[maybe_unused]] const char *p) {} +void console_error([[maybe_unused]] const char *p) {} +void console_init() {} +void console_putc([[maybe_unused]] int i) {} } diff --git a/lib-hal/console/uart0/console.c b/lib-hal/console/uart0/console.cpp similarity index 84% rename from lib-hal/console/uart0/console.c rename to lib-hal/console/uart0/console.cpp index 9fca1a3..cc0a873 100644 --- a/lib-hal/console/uart0/console.c +++ b/lib-hal/console/uart0/console.cpp @@ -1,8 +1,8 @@ /** - * @file console.c + * @file console.cpp * */ -/* Copyright (C) 2018-2023 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,15 +23,16 @@ * THE SOFTWARE. */ -#include +#include #include "console.h" -extern void uart0_init(void); -extern void uart0_putc(int); -extern void uart0_puts(const char *); +extern "C" { +void uart0_init(); +void uart0_putc(int); +void uart0_puts(const char *); -void __attribute__((cold)) console_init(void) { +void __attribute__((cold)) console_init() { uart0_init(); console_set_fg_color(CONSOLE_WHITE); @@ -46,6 +47,12 @@ void console_puts(const char *s) { uart0_puts(s); } +void console_error(const char *s) { + uart0_puts("\x1b[31m"); + uart0_puts(s); + uart0_puts("\x1b[37m"); +} + void console_set_fg_color(uint16_t fg) { switch (fg) { case CONSOLE_BLACK: @@ -89,19 +96,13 @@ void console_set_bg_color(uint16_t bg) { void console_write(const char *s, unsigned int n) { char c; - while (((c = *s++) != (char) 0) && (n-- != 0)) { - console_putc((int) c); + while (((c = *s++) != 0) && (n-- != 0)) { + console_putc(static_cast(c)); } } -void console_error(const char *s) { - uart0_puts("\x1b[31m"); - uart0_puts(s); - uart0_puts("\x1b[37m"); -} - -void console_status(uint32_t color, const char *s) { - console_set_fg_color((uint16_t) color); +void console_status(uint32_t nColour, const char *s) { + console_set_fg_color(static_cast(nColour)); console_set_bg_color(CONSOLE_BLACK); uart0_puts(s); @@ -109,3 +110,4 @@ void console_status(uint32_t color, const char *s) { console_set_fg_color(CONSOLE_WHITE); } +} diff --git a/lib-hal/include/console.h b/lib-hal/include/console.h index 38aab85..3435451 100644 --- a/lib-hal/include/console.h +++ b/lib-hal/include/console.h @@ -2,7 +2,7 @@ * @file console.h * */ -/* Copyright (C) 2018-2023 by Arjan van Vught mailto:info@orangepi-dmx.nl +/* Copyright (C) 2018-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal