Skip to content

Commit

Permalink
Merge pull request #2674 from hathach/add-ch32v103
Browse files Browse the repository at this point in the history
Add ch32v103 (not working yet)
  • Loading branch information
hathach committed Jun 14, 2024
2 parents ba2f229 + 2be72a9 commit 0ac0c37
Show file tree
Hide file tree
Showing 38 changed files with 1,383 additions and 969 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_set_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
family_list = {
"broadcom_32bit": ["arm-gcc"],
"broadcom_64bit": ["aarch64-gcc"],
"ch32v20x ch32v307 fomu gd32vf103": ["riscv-gcc"],
"ch32v10x ch32v20x ch32v307 fomu gd32vf103": ["riscv-gcc"],
"imxrt": ["arm-gcc", "arm-clang"],
"kinetis_k kinetis_kl kinetis_k32l2": ["arm-gcc", "arm-clang"],
"lpc11 lpc13 lpc15": ["arm-gcc", "arm-clang"],
Expand Down
1 change: 1 addition & 0 deletions examples/device/audio_4_channel_mic_freertos/skip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mcu:CH32V103
mcu:CH32V20X
mcu:CH32V307
mcu:CXD56
Expand Down
1 change: 1 addition & 0 deletions examples/device/audio_test_freertos/skip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mcu:CH32V103
mcu:CH32V20X
mcu:CH32V307
mcu:CXD56
Expand Down
1 change: 1 addition & 0 deletions examples/device/cdc_msc_freertos/skip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mcu:CH32V103
mcu:CH32V20X
mcu:CH32V307
mcu:CXD56
Expand Down
1 change: 1 addition & 0 deletions examples/device/hid_composite_freertos/skip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mcu:CH32V103
mcu:CH32V20X
mcu:CH32V307
mcu:CXD56
Expand Down
1 change: 1 addition & 0 deletions examples/device/net_lwip_webserver/skip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mcu:CH32V103
mcu:CH32V20X
mcu:LPC11UXX
mcu:LPC13XX
Expand Down
1 change: 1 addition & 0 deletions examples/device/video_capture/skip.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mcu:CH32V103
mcu:CH32V20X
mcu:MSP430x5xx
mcu:NUC121
Expand Down
1 change: 1 addition & 0 deletions examples/device/video_capture_2ch/skip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mcu:MSP430x5xx
mcu:NUC121
mcu:SAMD11
mcu:GD32VF103
mcu:CH32V103
mcu:CH32V20X
mcu:CH32V307
mcu:STM32L0
Expand Down
8 changes: 8 additions & 0 deletions hw/bsp/ch32v10x/boards/ch32v103r_r1_1v0/board.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(LD_FLASH_SIZE 64K)
set(LD_RAM_SIZE 20K)

function(update_board TARGET)
target_compile_definitions(${TARGET} PUBLIC
CFG_EXAMPLE_MSC_DUAL_READONLY
)
endfunction()
20 changes: 20 additions & 0 deletions hw/bsp/ch32v10x/boards/ch32v103r_r1_1v0/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef BOARD_H_
#define BOARD_H_

#ifdef __cplusplus
extern "C" {
#endif

#define LED_PORT GPIOA
#define LED_PIN GPIO_Pin_10
#define LED_STATE_ON 0

#define BUTTON_PORT GPIOA
#define BUTTON_PIN GPIO_Pin_1
#define BUTTON_STATE_ACTIVE 0

#ifdef __cplusplus
}
#endif

#endif
5 changes: 5 additions & 0 deletions hw/bsp/ch32v10x/boards/ch32v103r_r1_1v0/board.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CFLAGS += -DCFG_EXAMPLE_MSC_DUAL_READONLY

LDFLAGS += \
-Wl,--defsym=__flash_size=64K \
-Wl,--defsym=__ram_size=20K \
37 changes: 37 additions & 0 deletions hw/bsp/ch32v10x/ch32v10x_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v10x_conf.h
* Author : WCH
* Version : V1.0.0
* Date : 2020/04/30
* Description : Library configuration file.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __CH32V10x_CONF_H
#define __CH32V10x_CONF_H

#include "ch32v10x_adc.h"
#include "ch32v10x_bkp.h"
#include "ch32v10x_crc.h"
#include "ch32v10x_dbgmcu.h"
#include "ch32v10x_dma.h"
#include "ch32v10x_exti.h"
#include "ch32v10x_flash.h"
#include "ch32v10x_gpio.h"
#include "ch32v10x_i2c.h"
#include "ch32v10x_iwdg.h"
#include "ch32v10x_pwr.h"
#include "ch32v10x_rcc.h"
#include "ch32v10x_rtc.h"
#include "ch32v10x_spi.h"
#include "ch32v10x_tim.h"
#include "ch32v10x_usart.h"
#include "ch32v10x_wwdg.h"
#include "ch32v10x_usb.h"
#include "ch32v10x_usb_host.h"
#include "ch32v10x_it.h"
#include "ch32v10x_misc.h"

#endif /* __CH32V10x_CONF_H */
15 changes: 15 additions & 0 deletions hw/bsp/ch32v10x/ch32v10x_it.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v10x_it.h
* Author : WCH
* Version : V1.0.0
* Date : 2022/08/20
* Description : This file contains the headers of the interrupt handlers.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __CH32V10x_IT_H
#define __CH32V10x_IT_H

#endif /* __CH32V10x_IT_H */
148 changes: 148 additions & 0 deletions hw/bsp/ch32v10x/family.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <stdio.h>

// https://github.com/openwch/ch32v307/pull/90
// https://github.com/openwch/ch32v20x/pull/12
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#endif

#include "ch32v10x.h"

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#include "bsp/board_api.h"
#include "board.h"

__attribute__((interrupt)) __attribute__((used))
void USBHD_IRQHandler(void) {
#if CFG_TUD_WCH_USBIP_USBFS
tud_int_handler(0);
#endif
}

__attribute__((interrupt)) __attribute__((used))
void USBWakeUp_IRQHandler(void) {
#if CFG_TUD_WCH_USBIP_USBFS
tud_int_handler(0);
#endif
}

#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;

__attribute__((interrupt)) __attribute__((used))
void SysTick_Handler(void) {
SysTick->CNTL0 = SysTick->CNTL1 = SysTick->CNTL2 = SysTick->CNTL3 = 0;
SysTick->CNTH0 = SysTick->CNTH1 = SysTick->CNTH2 = SysTick->CNTH3 = 0;
system_ticks++;
}

uint32_t SysTick_Config(uint32_t ticks) {
NVIC_EnableIRQ(SysTicK_IRQn);
SysTick->CTLR = 0;
SysTick->CNTL0 = SysTick->CNTL1 = SysTick->CNTL2 = SysTick->CNTL3 = 0;
SysTick->CNTH0 = SysTick->CNTH1 = SysTick->CNTH2 = SysTick->CNTH3 = 0;

SysTick->CMPLR0 = (u8)(ticks & 0xFF);
SysTick->CMPLR1 = (u8)(ticks >> 8);
SysTick->CMPLR2 = (u8)(ticks >> 16);
SysTick->CMPLR3 = (u8)(ticks >> 24);

SysTick->CMPHR0 = SysTick->CMPHR1 = SysTick->CMPHR2 = SysTick->CMPHR3 = 0;
SysTick->CTLR = 1;
return 0;
}

uint32_t board_millis(void) {
return system_ticks;
}
#endif

void board_init(void) {
__disable_irq();

#if CFG_TUSB_OS == OPT_OS_NONE
SysTick_Config(SystemCoreClock / 1000);
#endif

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

EXTEN->EXTEN_CTR |= EXTEN_USBFS_IO_EN;
uint8_t usb_div;
switch (SystemCoreClock) {
case 48000000: usb_div = RCC_USBCLKSource_PLLCLK_Div1; break;
case 72000000: usb_div = RCC_USBCLKSource_PLLCLK_1Div5; break;
default: TU_ASSERT(0,); break;
}
RCC_USBCLKConfig(usb_div);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USBFS, ENABLE);

#ifdef LED_PIN
GPIO_InitTypeDef led_init = {
.GPIO_Pin = LED_PIN,
.GPIO_Mode = GPIO_Mode_Out_OD,
.GPIO_Speed = GPIO_Speed_50MHz,
};
GPIO_Init(LED_PORT, &led_init);
#endif

#ifdef BUTTON_PIN
GPIO_InitTypeDef button_init = {
.GPIO_Pin = BUTTON_PIN,
.GPIO_Mode = GPIO_Mode_IPU,
.GPIO_Speed = GPIO_Speed_50MHz,
};
GPIO_Init(BUTTON_PORT, &button_init);
#endif

// UART TX is PA9
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef usart_init = {
.GPIO_Pin = GPIO_Pin_9,
.GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF_PP,
};
GPIO_Init(GPIOA, &usart_init);

USART_InitTypeDef usart = {
.USART_BaudRate = 115200,
.USART_WordLength = USART_WordLength_8b,
.USART_StopBits = USART_StopBits_1,
.USART_Parity = USART_Parity_No,
.USART_Mode = USART_Mode_Tx,
.USART_HardwareFlowControl = USART_HardwareFlowControl_None,
};
USART_Init(USART1, &usart);
USART_Cmd(USART1, ENABLE);

__enable_irq();

board_led_write(true);
}

void board_led_write(bool state) {
GPIO_WriteBit(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
}

uint32_t board_button_read(void) {
return BUTTON_STATE_ACTIVE == GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN);
}

int board_uart_read(uint8_t *buf, int len) {
(void) buf;
(void) len;
return 0;
}

int board_uart_write(void const *buf, int len) {
const char *bufc = (const char *) buf;
for (int i = 0; i < len; i++) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, *bufc++);
}

return len;
}
Loading

0 comments on commit 0ac0c37

Please sign in to comment.