Skip to content

Commit ebadf62

Browse files
authored
Ch32v support (#59)
* Add support for function pointers in UART configuration and implement ChannelUart class * Implement ChannelCh32Uart class and update ChannelUart for buffer handling; add tests for sendBuffer functionality * Add unit tests for CRC16 calculation and UART configuration; update CMakeLists for test integration * Refactor UART configuration and implementation for STM8 support; update related structures and tests * Reorder CRC16 sending in v2styxlib_uart_send function; update tests for expected CRC16 frame * Fix comments * Fix STM8 build issues * Add filter_src.py for STM8 source filtering in build process * Add filter_src.py and library.json for CH32V support in build process * Implement CH32V support: add UART configuration and send functions, update README, and modify source filtering * Enhance CH32V UART support: update V2styxlibUartConfigBase structure, modify setup function, and adjust send logic for SOF markers * Refactor ChannelUartConfig: remove sofMarkers member and update constructor to use baseConfig for SOF markers * Refactor CH32V UART setup and send functions: streamline GPIO and USART initialization using direct register manipulation * Fix comments
1 parent dbf3f39 commit ebadf62

27 files changed

Lines changed: 910 additions & 224 deletions

c/l4/CMakeLists.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
22

33
add_library(module_l4_c STATIC)
44

5+
set(MODULE_L4C_SRC
6+
src/arch.c
7+
src/Channel_c.c
8+
)
9+
510
target_sources(module_l4_c
611
PRIVATE
7-
src/Channel_c.c
12+
${MODULE_L4C_SRC}
813
)
914

1015
target_include_directories(module_l4_c PUBLIC
1116
$<INSTALL_INTERFACE:include>
1217
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
1318
)
19+
20+
21+
if (ENABLE_CATCH2)
22+
add_executable(module_l4_c_tests
23+
test/test_Channel_c.cpp
24+
${MODULE_L4C_SRC}
25+
)
26+
target_compile_definitions(module_l4_c_tests PRIVATE TARGET_CATCH2)
27+
target_link_libraries(module_l4_c_tests PUBLIC Catch2::Catch2WithMain)
28+
target_include_directories(module_l4_c_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
29+
30+
catch_discover_tests(module_l4_c_tests
31+
PROPERTIES
32+
TIMEOUT 10)
33+
endif()

c/l4/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ Enables the use of Software UART for TX on STM8. This is particularly useful whe
3232
you need additional ADC channels and want to free up the **UART1_TX / AIN5 / (HS) PD5** pin for analog input.
3333

3434
**Note:** This implementation covers TX only, as software-based RX is generally
35-
unstable on this architecture due to timing constraints.
35+
unstable on this architecture due to timing constraints.
36+
37+
## `V2STYXLIB_TARGET_STM8`
38+
39+
Target build for STM8.
40+
41+
## `V2STYXLIB_TARGET_CH32V`
42+
43+
Target build for CH32V.

c/l4/filter_src.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Import("env")
2+
from os.path import join, realpath
3+
4+
is_stm8 = env.get("PIOPLATFORM") == "ststm8" or "stm8" in env.get("BOARD_MCU", "").lower()
5+
is_ch32v = env.get("PIOPLATFORM") == "ch32v" or "ch32v" in env.get("BOARD_MCU", "").lower()
6+
7+
sources = ["+<arch.c>", "+<Channel_c.c>"]
8+
9+
if is_stm8:
10+
sources.append("+<ChannelUartStm8.c>")
11+
if is_ch32v:
12+
sources.append("+<ChannelUartCh32v.c>")
13+
14+
env.Replace(SRC_FILTER=sources)

c/l4/include/ChannelUartCh32v.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include "defines.h"
4+
#include "structs.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
typedef struct {
11+
V2styxlibUartConfigBase baseConfig;
12+
uint32_t baudrate;
13+
} V2styxlibUartCh32vConfig;
14+
15+
/**
16+
* Configure USART1 peripheral (PD5 TX, PD6 RX) with the given baud rate.
17+
* Must be called once before sending data.
18+
*/
19+
void v2styxlib_uart_ch32v_setup(const V2styxlibUartCh32vConfig* config);
20+
21+
/**
22+
* Send multiple bytes over USART1.
23+
*/
24+
void v2styxlib_uart_ch32v_send_bytes(
25+
const V2styxlibUartCh32vConfig* config,
26+
const uint8_t* buffer,
27+
BufferSize_t length);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif

c/l4/include/ChannelUartStm8.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#pragma once
22

3-
#include <stdint.h>
4-
#include "stm8s.h"
5-
6-
#include "Channel_c.h"
3+
#include "defines.h"
4+
#include "structs.h"
75

86
typedef struct {
9-
V2styxlibUartConfig baseConfig;
10-
#ifdef V2STYXLIB_SOFTUART
7+
V2styxlibUartConfigBase baseConfig;
8+
#ifdef V2STYXLIB_SOFTUART_TX
119
GPIO_TypeDef * softUartPort;
1210
uint8_t softUartTxPinMask;
1311
#endif
@@ -22,18 +20,12 @@ typedef struct {
2220
* baudRateDivider = 16000000 / 115200 ≈ 138.89
2321
* In this case, baudRateDivider will be approximately 139 (rounded to the nearest integer).
2422
*/
25-
void v2styxlib_uart_setup(
23+
void v2styxlib_uart_stm8_setup(
2624
const V2styxlibUartStm8Config* config,
2725
uint16_t baudRateDivider
2826
);
2927

30-
/**
31-
* buffer - is a pointer to the data buffer that contains the data to be sent over UART.
32-
* length - is the number of bytes to be sent from the buffer.
33-
* This function is responsible for sending a specified number of bytes from the provided buffer over UART.
34-
*/
35-
void v2styxlib_uart_send(
36-
const V2styxlibUartStm8Config* config,
37-
const uint8_t *buffer,
38-
BufferSize_t length
39-
);
28+
void v2styxlib_uart_stm8_send_bytes(
29+
const V2styxlibUartStm8Config* config,
30+
const uint8_t* buffer,
31+
BufferSize_t length);

c/l4/include/Channel_c.h

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
#pragma once
22

3-
#include <stdint.h>
4-
#include <stdbool.h>
5-
6-
#ifndef BufferSize_t
7-
#define BufferSize_t uint8_t
8-
#endif
9-
10-
#ifndef V2STYXLIB_CRC16_POLY
11-
#define V2STYXLIB_CRC16_POLY 0x1021
12-
#endif
13-
#ifndef V2STYXLIB_CRC16_INITIAL_VALUE
14-
#define V2STYXLIB_CRC16_INITIAL_VALUE 0xFFFF
15-
#endif
16-
17-
#define V2STYXLIB_CONFIG_STREAMING_MODE 0x01
18-
#define V2STYXLIB_CONFIG_SEND_CRC16 0x02
19-
#define V2STYXLIB_CONFIG_SOFT_UART_TX 0x04
20-
21-
#ifndef V2STYXLIB_SOF_MARKER_1
22-
#define V2STYXLIB_SOF_MARKER_1 0x55
23-
#define V2STYXLIB_SOF_MARKER_2 0xAA
24-
#endif
3+
#include "defines.h"
4+
#include "arch.h"
255

266
#ifdef __cplusplus
277
extern "C" {
288
#endif
299

30-
typedef struct {
31-
uint8_t config;
32-
} V2styxlibUartConfig;
33-
3410
/**
3511
* useStreamingMode - is a boolean flag that indicates whether to
3612
* use streaming mode for UART communication.
@@ -59,7 +35,20 @@ void v2styxlib_uart_configure_proto(
5935
*
6036
* Returns the calculated CRC16 checksum.
6137
*/
62-
uint16_t v2styxlib_crc16_calculate(const uint8_t *data, BufferSize_t length);
38+
uint16_t v2styxlib_crc16_calculate(
39+
const uint8_t *data,
40+
BufferSize_t length
41+
);
42+
43+
/**
44+
* buffer - is a pointer to the data buffer that contains the data to be sent over UART.
45+
* length - is the number of bytes to be sent from the buffer.
46+
* This function is responsible for sending a specified number of bytes from the provided buffer over UART.
47+
*/
48+
void v2styxlib_uart_send(
49+
const V2styxlibUartConfig* config,
50+
const uint8_t *buffer,
51+
BufferSize_t length);
6352

6453
#ifdef __cplusplus
6554
}

c/l4/include/arch.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef ARCH_H
2+
#define ARCH_H
3+
4+
#include "structs.h"
5+
6+
#ifdef V2STYXLIB_TARGET_STM8
7+
#include "ChannelUartStm8.h"
8+
9+
typedef V2styxlibUartStm8Config V2styxlibUartConfig;
10+
#define v2styxlib_uart_send_bytes v2styxlib_uart_stm8_send_bytes
11+
#elif defined(V2STYXLIB_TARGET_CH32V)
12+
#include "ChannelUartCh32v.h"
13+
14+
typedef V2styxlibUartCh32vConfig V2styxlibUartConfig;
15+
#define v2styxlib_uart_send_bytes v2styxlib_uart_ch32v_send_bytes
16+
#else
17+
// target to host system, use generic config
18+
typedef struct {
19+
struct V2styxlibUartConfigBase baseConfig;
20+
} V2styxlibUartConfig;
21+
22+
void v2styxlib_uart_send_bytes(
23+
const V2styxlibUartConfig* config,
24+
const uint8_t* buffer,
25+
BufferSize_t length
26+
);
27+
#endif
28+
29+
#endif // ARCH_H

c/l4/include/defines.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef DEFINES_H
2+
#define DEFINES_H
3+
4+
#ifdef V2STYXLIB_TARGET_STM8
5+
#include "stm8s.h"
6+
#else
7+
#include <stdint.h>
8+
#include <stdbool.h>
9+
#endif
10+
11+
#ifndef BufferSize_t
12+
#define BufferSize_t uint8_t
13+
#endif
14+
15+
#ifndef V2STYXLIB_CRC16_POLY
16+
#define V2STYXLIB_CRC16_POLY 0x1021
17+
#endif
18+
#ifndef V2STYXLIB_CRC16_INITIAL_VALUE
19+
#define V2STYXLIB_CRC16_INITIAL_VALUE 0xFFFF
20+
#endif
21+
22+
#define V2STYXLIB_CONFIG_STREAMING_MODE 0x01
23+
#define V2STYXLIB_CONFIG_SEND_CRC16 0x02
24+
#define V2STYXLIB_CONFIG_SOFT_UART_TX 0x04
25+
26+
#ifndef V2STYXLIB_SOF_MARKER_1
27+
#define V2STYXLIB_SOF_MARKER_1 0x55
28+
#define V2STYXLIB_SOF_MARKER_2 0xAA
29+
#endif
30+
31+
#endif // DEFINES_H

c/l4/include/structs.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef STRUCTS_H
2+
#define STRUCTS_H
3+
4+
#include "defines.h"
5+
6+
/**
7+
* V2styxlibUartConfigBase - is a structure that holds the
8+
* configuration for UART communication. And hardware layer
9+
* specific send function.
10+
*/
11+
typedef struct V2styxlibUartConfigBase {
12+
uint8_t config;
13+
uint8_t sof[2];
14+
} V2styxlibUartConfigBase;
15+
16+
#endif // STRUCTS_H

c/l4/library.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
"name": "V2StyxLibL4_C",
33
"version": "1.0.0",
44
"build": {
5-
"srcFilter": [
6-
"+<Channel_c.c>",
7-
"+<ChannelUartStm8.c>"
8-
]
5+
"extraScript": "filter_src.py"
96
}
107
}

0 commit comments

Comments
 (0)