Skip to content

Commit

Permalink
Renamed *.c files to *.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed Mar 11, 2024
1 parent 01d3913 commit 4ef9921
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file gd32_uart0.c
* @file uart0.c
*
*/
/* Copyright (C) 2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2023-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,37 +23,46 @@
* THE SOFTWARE.
*/

#if defined (NDEBUG)
# undef NDEBUG
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

#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 {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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

Expand All @@ -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)
;

Expand All @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib-gd32/src/gd32_uart0.cpp → lib-gd32/src/uart0.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file gd32_uart0.cpp
* @file uart0.cpp
*
*/
/* Copyright (C) 2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2023-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file gd32_uart0.cpp
* @file uart0.cpp
*
*/
/* Copyright (C) 2021-2024 by Arjan van Vught mailto:[email protected]
Expand Down
24 changes: 13 additions & 11 deletions lib-hal/console/i2c/console.c → lib-hal/console/i2c/console.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file console.c
* @file console.cpp
*
*/
/* Copyright (C) 2022-2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2022-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -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() {
Expand All @@ -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<char>(nRegister);
buffer[1] = static_cast<char>(nValue);

setup();
FUNC_PREFIX(i2c_write(buffer, 2));
Expand All @@ -121,21 +121,21 @@ static uint8_t read_byte() {
setup();
FUNC_PREFIX(i2c_read(buffer, 1));

return (uint8_t) (buffer[0]);
return static_cast<uint8_t>(buffer[0]);
}

static uint8_t read_register(uint8_t nRegister) {
char buffer[1];

buffer[0] = (char) (nRegister);
buffer[0] = static_cast<char>(nRegister);

setup();
FUNC_PREFIX(i2c_write(buffer, 1));

return read_byte();
}

inline static bool is_writable() {
static bool is_writable() {
return (read_register(SC16IS7X0_TXLVL) != 0);
}

Expand All @@ -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);
Expand Down Expand Up @@ -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<uint16_t>(nColour));
console_set_bg_color(CONSOLE_BLACK);
console_puts(s);
console_putc('\n');
console_set_fg_color(CONSOLE_WHITE);
}
}
29 changes: 10 additions & 19 deletions lib-hal/console/null/console.c → lib-hal/console/null/console.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file console.c
* @file console.cpp
*
*/
/* Copyright (C) 2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2023-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,22 +23,13 @@
* THE SOFTWARE.
*/

#include <stdint.h>
#include <cstdint>

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) {}
}
Loading

0 comments on commit 4ef9921

Please sign in to comment.