Skip to content

Commit 0ac0c37

Browse files
authored
Merge pull request #2674 from hathach/add-ch32v103
Add ch32v103 (not working yet)
2 parents ba2f229 + 2be72a9 commit 0ac0c37

File tree

38 files changed

+1383
-969
lines changed

38 files changed

+1383
-969
lines changed

.github/workflows/ci_set_matrix.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
family_list = {
1616
"broadcom_32bit": ["arm-gcc"],
1717
"broadcom_64bit": ["aarch64-gcc"],
18-
"ch32v20x ch32v307 fomu gd32vf103": ["riscv-gcc"],
18+
"ch32v10x ch32v20x ch32v307 fomu gd32vf103": ["riscv-gcc"],
1919
"imxrt": ["arm-gcc", "arm-clang"],
2020
"kinetis_k kinetis_kl kinetis_k32l2": ["arm-gcc", "arm-clang"],
2121
"lpc11 lpc13 lpc15": ["arm-gcc", "arm-clang"],

examples/device/audio_4_channel_mic_freertos/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V103
12
mcu:CH32V20X
23
mcu:CH32V307
34
mcu:CXD56

examples/device/audio_test_freertos/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V103
12
mcu:CH32V20X
23
mcu:CH32V307
34
mcu:CXD56

examples/device/cdc_msc_freertos/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V103
12
mcu:CH32V20X
23
mcu:CH32V307
34
mcu:CXD56

examples/device/hid_composite_freertos/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V103
12
mcu:CH32V20X
23
mcu:CH32V307
34
mcu:CXD56

examples/device/net_lwip_webserver/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V103
12
mcu:CH32V20X
23
mcu:LPC11UXX
34
mcu:LPC13XX

examples/device/video_capture/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V103
12
mcu:CH32V20X
23
mcu:MSP430x5xx
34
mcu:NUC121

examples/device/video_capture_2ch/skip.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mcu:MSP430x5xx
22
mcu:NUC121
33
mcu:SAMD11
44
mcu:GD32VF103
5+
mcu:CH32V103
56
mcu:CH32V20X
67
mcu:CH32V307
78
mcu:STM32L0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set(LD_FLASH_SIZE 64K)
2+
set(LD_RAM_SIZE 20K)
3+
4+
function(update_board TARGET)
5+
target_compile_definitions(${TARGET} PUBLIC
6+
CFG_EXAMPLE_MSC_DUAL_READONLY
7+
)
8+
endfunction()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef BOARD_H_
2+
#define BOARD_H_
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#define LED_PORT GPIOA
9+
#define LED_PIN GPIO_Pin_10
10+
#define LED_STATE_ON 0
11+
12+
#define BUTTON_PORT GPIOA
13+
#define BUTTON_PIN GPIO_Pin_1
14+
#define BUTTON_STATE_ACTIVE 0
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CFLAGS += -DCFG_EXAMPLE_MSC_DUAL_READONLY
2+
3+
LDFLAGS += \
4+
-Wl,--defsym=__flash_size=64K \
5+
-Wl,--defsym=__ram_size=20K \

hw/bsp/ch32v10x/ch32v10x_conf.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/********************************** (C) COPYRIGHT *******************************
2+
* File Name : ch32v10x_conf.h
3+
* Author : WCH
4+
* Version : V1.0.0
5+
* Date : 2020/04/30
6+
* Description : Library configuration file.
7+
*********************************************************************************
8+
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
9+
* Attention: This software (modified or not) and binary are used for
10+
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
11+
*******************************************************************************/
12+
#ifndef __CH32V10x_CONF_H
13+
#define __CH32V10x_CONF_H
14+
15+
#include "ch32v10x_adc.h"
16+
#include "ch32v10x_bkp.h"
17+
#include "ch32v10x_crc.h"
18+
#include "ch32v10x_dbgmcu.h"
19+
#include "ch32v10x_dma.h"
20+
#include "ch32v10x_exti.h"
21+
#include "ch32v10x_flash.h"
22+
#include "ch32v10x_gpio.h"
23+
#include "ch32v10x_i2c.h"
24+
#include "ch32v10x_iwdg.h"
25+
#include "ch32v10x_pwr.h"
26+
#include "ch32v10x_rcc.h"
27+
#include "ch32v10x_rtc.h"
28+
#include "ch32v10x_spi.h"
29+
#include "ch32v10x_tim.h"
30+
#include "ch32v10x_usart.h"
31+
#include "ch32v10x_wwdg.h"
32+
#include "ch32v10x_usb.h"
33+
#include "ch32v10x_usb_host.h"
34+
#include "ch32v10x_it.h"
35+
#include "ch32v10x_misc.h"
36+
37+
#endif /* __CH32V10x_CONF_H */

hw/bsp/ch32v10x/ch32v10x_it.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/********************************** (C) COPYRIGHT *******************************
2+
* File Name : ch32v10x_it.h
3+
* Author : WCH
4+
* Version : V1.0.0
5+
* Date : 2022/08/20
6+
* Description : This file contains the headers of the interrupt handlers.
7+
*********************************************************************************
8+
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
9+
* Attention: This software (modified or not) and binary are used for
10+
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
11+
*******************************************************************************/
12+
#ifndef __CH32V10x_IT_H
13+
#define __CH32V10x_IT_H
14+
15+
#endif /* __CH32V10x_IT_H */

hw/bsp/ch32v10x/family.c

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include <stdio.h>
2+
3+
// https://github.com/openwch/ch32v307/pull/90
4+
// https://github.com/openwch/ch32v20x/pull/12
5+
#ifdef __GNUC__
6+
#pragma GCC diagnostic push
7+
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
8+
#endif
9+
10+
#include "ch32v10x.h"
11+
12+
#ifdef __GNUC__
13+
#pragma GCC diagnostic pop
14+
#endif
15+
16+
#include "bsp/board_api.h"
17+
#include "board.h"
18+
19+
__attribute__((interrupt)) __attribute__((used))
20+
void USBHD_IRQHandler(void) {
21+
#if CFG_TUD_WCH_USBIP_USBFS
22+
tud_int_handler(0);
23+
#endif
24+
}
25+
26+
__attribute__((interrupt)) __attribute__((used))
27+
void USBWakeUp_IRQHandler(void) {
28+
#if CFG_TUD_WCH_USBIP_USBFS
29+
tud_int_handler(0);
30+
#endif
31+
}
32+
33+
#if CFG_TUSB_OS == OPT_OS_NONE
34+
volatile uint32_t system_ticks = 0;
35+
36+
__attribute__((interrupt)) __attribute__((used))
37+
void SysTick_Handler(void) {
38+
SysTick->CNTL0 = SysTick->CNTL1 = SysTick->CNTL2 = SysTick->CNTL3 = 0;
39+
SysTick->CNTH0 = SysTick->CNTH1 = SysTick->CNTH2 = SysTick->CNTH3 = 0;
40+
system_ticks++;
41+
}
42+
43+
uint32_t SysTick_Config(uint32_t ticks) {
44+
NVIC_EnableIRQ(SysTicK_IRQn);
45+
SysTick->CTLR = 0;
46+
SysTick->CNTL0 = SysTick->CNTL1 = SysTick->CNTL2 = SysTick->CNTL3 = 0;
47+
SysTick->CNTH0 = SysTick->CNTH1 = SysTick->CNTH2 = SysTick->CNTH3 = 0;
48+
49+
SysTick->CMPLR0 = (u8)(ticks & 0xFF);
50+
SysTick->CMPLR1 = (u8)(ticks >> 8);
51+
SysTick->CMPLR2 = (u8)(ticks >> 16);
52+
SysTick->CMPLR3 = (u8)(ticks >> 24);
53+
54+
SysTick->CMPHR0 = SysTick->CMPHR1 = SysTick->CMPHR2 = SysTick->CMPHR3 = 0;
55+
SysTick->CTLR = 1;
56+
return 0;
57+
}
58+
59+
uint32_t board_millis(void) {
60+
return system_ticks;
61+
}
62+
#endif
63+
64+
void board_init(void) {
65+
__disable_irq();
66+
67+
#if CFG_TUSB_OS == OPT_OS_NONE
68+
SysTick_Config(SystemCoreClock / 1000);
69+
#endif
70+
71+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
72+
73+
EXTEN->EXTEN_CTR |= EXTEN_USBFS_IO_EN;
74+
uint8_t usb_div;
75+
switch (SystemCoreClock) {
76+
case 48000000: usb_div = RCC_USBCLKSource_PLLCLK_Div1; break;
77+
case 72000000: usb_div = RCC_USBCLKSource_PLLCLK_1Div5; break;
78+
default: TU_ASSERT(0,); break;
79+
}
80+
RCC_USBCLKConfig(usb_div);
81+
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USBFS, ENABLE);
82+
83+
#ifdef LED_PIN
84+
GPIO_InitTypeDef led_init = {
85+
.GPIO_Pin = LED_PIN,
86+
.GPIO_Mode = GPIO_Mode_Out_OD,
87+
.GPIO_Speed = GPIO_Speed_50MHz,
88+
};
89+
GPIO_Init(LED_PORT, &led_init);
90+
#endif
91+
92+
#ifdef BUTTON_PIN
93+
GPIO_InitTypeDef button_init = {
94+
.GPIO_Pin = BUTTON_PIN,
95+
.GPIO_Mode = GPIO_Mode_IPU,
96+
.GPIO_Speed = GPIO_Speed_50MHz,
97+
};
98+
GPIO_Init(BUTTON_PORT, &button_init);
99+
#endif
100+
101+
// UART TX is PA9
102+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
103+
GPIO_InitTypeDef usart_init = {
104+
.GPIO_Pin = GPIO_Pin_9,
105+
.GPIO_Speed = GPIO_Speed_50MHz,
106+
.GPIO_Mode = GPIO_Mode_AF_PP,
107+
};
108+
GPIO_Init(GPIOA, &usart_init);
109+
110+
USART_InitTypeDef usart = {
111+
.USART_BaudRate = 115200,
112+
.USART_WordLength = USART_WordLength_8b,
113+
.USART_StopBits = USART_StopBits_1,
114+
.USART_Parity = USART_Parity_No,
115+
.USART_Mode = USART_Mode_Tx,
116+
.USART_HardwareFlowControl = USART_HardwareFlowControl_None,
117+
};
118+
USART_Init(USART1, &usart);
119+
USART_Cmd(USART1, ENABLE);
120+
121+
__enable_irq();
122+
123+
board_led_write(true);
124+
}
125+
126+
void board_led_write(bool state) {
127+
GPIO_WriteBit(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
128+
}
129+
130+
uint32_t board_button_read(void) {
131+
return BUTTON_STATE_ACTIVE == GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN);
132+
}
133+
134+
int board_uart_read(uint8_t *buf, int len) {
135+
(void) buf;
136+
(void) len;
137+
return 0;
138+
}
139+
140+
int board_uart_write(void const *buf, int len) {
141+
const char *bufc = (const char *) buf;
142+
for (int i = 0; i < len; i++) {
143+
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
144+
USART_SendData(USART1, *bufc++);
145+
}
146+
147+
return len;
148+
}

0 commit comments

Comments
 (0)