Skip to content

Commit fa9d190

Browse files
authored
Merge pull request hathach#1405 from gregdavill/ch32v307
Add WCH CH32V307 port
2 parents 4ee4c6f + b1021d5 commit fa9d190

File tree

25 files changed

+2679
-2
lines changed

25 files changed

+2679
-2
lines changed

.github/workflows/build_riscv.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
matrix:
2828
family:
2929
# Alphabetical order
30+
- 'ch32v307'
3031
- 'fomu'
3132
- 'gd32vf103'
3233
steps:

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
[submodule "hw/mcu/allwinner"]
147147
path = hw/mcu/allwinner
148148
url = https://github.com/hathach/allwinner_driver.git
149+
[submodule "hw/mcu/wch/ch32v307"]
150+
path = hw/mcu/wch/ch32v307
151+
url = https://github.com/openwch/ch32v307.git
149152
[submodule "hw/mcu/raspberry_pi/Pico-PIO-USB"]
150153
path = hw/mcu/raspberry_pi/Pico-PIO-USB
151154
url = https://github.com/sekigon-gonnoc/Pico-PIO-USB.git

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The stack supports the following MCUs:
5454
- **ST:** STM32 series: F0, F1, F2, F3, F4, F7, H7, G4, L0, L1, L4, L4+, WB
5555
- **TI:** MSP430, MSP432E4, TM4C123
5656
- **ValentyUSB:** eptri
57+
- **WCH:** CH32V307
5758

5859
Here is the list of `Supported Devices`_ that can be used with provided examples.
5960

docs/reference/supported.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Supported MCUs
106106
+--------------+-----------------------+--------+------+-----------+-------------------+--------------+
107107
| ValentyUSB | eptri |||| eptri | |
108108
+--------------+-----------------------+--------+------+-----------+-------------------+--------------+
109+
| WCH | CH32V307 || || ch32v307 | |
110+
+--------------+-----------------------+--------+------+-----------+-------------------+--------------+
109111

110112

111113
Table Legend
@@ -397,3 +399,8 @@ Tomu
397399
----
398400

399401
- `Fomu <https://www.crowdsupply.com/sutajio-kosagi/fomu>`__
402+
403+
WCH
404+
---
405+
406+
- `CH32V307V-R1-1v0 <https://lcsc.com/product-detail/Development-Boards-Kits_WCH-Jiangsu-Qin-Heng-CH32V307V-EVT-R1_C2943980.html>`

examples/device/cdc_msc_freertos/skip.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V307
12
mcu:CXD56
23
mcu:F1C100S
34
mcu:GD32VF103
@@ -8,4 +9,4 @@ mcu:SAMD11
89
mcu:SAMX7X
910
mcu:VALENTYUSB_EPTRI
1011
family:broadcom_32bit
11-
family:broadcom_64bit
12+
family:broadcom_64bit

examples/device/hid_composite_freertos/skip.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V307
12
mcu:CXD56
23
mcu:F1C100S
34
mcu:GD32VF103
@@ -8,4 +9,4 @@ mcu:SAMD11
89
mcu:SAMX7X
910
mcu:VALENTYUSB_EPTRI
1011
family:broadcom_32bit
11-
family:broadcom_64bit
12+
family:broadcom_64bit
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2023 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef BOARD_H_
26+
#define BOARD_H_
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
// LED: need to wire pin LED1 to PC0 in the J3 header
33+
#define LED_PORT GPIOC
34+
#define LED_PIN GPIO_Pin_0
35+
#define LED_STATE_ON 0
36+
#define LED_CLOCK_EN() RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE)
37+
38+
// Button: need to wire pin KEY to PC1 in the J3 header
39+
#define BUTTON_PORT GPIOC
40+
#define BUTTON_PIN GPIO_Pin_1
41+
#define BUTTON_STATE_ACTIVE 0
42+
#define BUTTON_CLOCK_EN() do { } while(0) // same as LED clock, no need to do anything
43+
44+
// TODO UART port
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif
49+
50+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LD_FILE = $(FAMILY_PATH)/ch32v307.ld
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022 Greg Davill
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#include "debug_uart.h"
28+
#include <ch32v30x.h>
29+
30+
31+
#define UART_RINGBUFFER_SIZE_TX 64
32+
#define UART_RINGBUFFER_MASK_TX (UART_RINGBUFFER_SIZE_TX-1)
33+
34+
static char tx_buf[UART_RINGBUFFER_SIZE_TX];
35+
static unsigned int tx_produce;
36+
static volatile unsigned int tx_consume;
37+
38+
void USART1_IRQHandler(void) __attribute__((naked));
39+
void USART1_IRQHandler(void) {
40+
__asm volatile ("call USART1_IRQHandler_impl; mret");
41+
}
42+
43+
__attribute__((used)) void USART1_IRQHandler_impl(void)
44+
{
45+
if(USART_GetITStatus(USART1, USART_IT_TC) != RESET)
46+
{
47+
USART_ClearITPendingBit(USART1, USART_IT_TC);
48+
49+
if(tx_consume != tx_produce) {
50+
USART_SendData(USART1, tx_buf[tx_consume]);
51+
tx_consume = (tx_consume + 1) & UART_RINGBUFFER_MASK_TX;
52+
}
53+
}
54+
55+
}
56+
57+
void uart_write(char c)
58+
{
59+
unsigned int tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX;
60+
61+
NVIC_DisableIRQ(USART1_IRQn);
62+
if((tx_consume != tx_produce) || (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)) {
63+
tx_buf[tx_produce] = c;
64+
tx_produce = tx_produce_next;
65+
} else {
66+
USART_SendData(USART1, c);
67+
}
68+
NVIC_EnableIRQ(USART1_IRQn);
69+
}
70+
71+
72+
void uart_sync(void)
73+
{
74+
while(tx_consume != tx_produce);
75+
}
76+
77+
78+
void usart_printf_init(uint32_t baudrate)
79+
{
80+
GPIO_InitTypeDef GPIO_InitStructure;
81+
USART_InitTypeDef USART_InitStructure;
82+
83+
tx_produce = 0;
84+
tx_consume = 0;
85+
86+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
87+
88+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
89+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
90+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
91+
GPIO_Init(GPIOA, &GPIO_InitStructure);
92+
93+
USART_InitStructure.USART_BaudRate = baudrate;
94+
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
95+
USART_InitStructure.USART_StopBits = USART_StopBits_1;
96+
USART_InitStructure.USART_Parity = USART_Parity_No;
97+
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
98+
USART_InitStructure.USART_Mode = USART_Mode_Tx;
99+
100+
USART_Init(USART1, &USART_InitStructure);
101+
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
102+
USART_Cmd(USART1, ENABLE);
103+
104+
NVIC_InitTypeDef NVIC_InitStructure = { 0 };
105+
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
106+
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
107+
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
108+
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
109+
NVIC_Init(&NVIC_InitStructure);
110+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022 Greg Davill
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
void uart_write(char c);
30+
void uart_sync(void);
31+
void usart_printf_init(uint32_t baudrate);

0 commit comments

Comments
 (0)