Skip to content

Commit f05f81e

Browse files
committed
add button support
1 parent 65f7a80 commit f05f81e

File tree

2 files changed

+92
-29
lines changed

2 files changed

+92
-29
lines changed
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

hw/bsp/ch32v307/family.c

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828
#include "debug_uart.h"
2929
#include "ch32v30x.h"
3030

31-
#include "../board.h"
31+
#include "bsp/board.h"
32+
#include "board.h"
3233

3334
//--------------------------------------------------------------------+
3435
// Forward USB interrupt events to TinyUSB IRQ Handler
3536
//--------------------------------------------------------------------+
3637

37-
void USBHS_IRQHandler(void) __attribute__((naked));
38-
void USBHS_IRQHandler(void) {
39-
__asm volatile ("call USBHS_IRQHandler_impl; mret");
38+
void USBHS_IRQHandler (void) __attribute__((naked));
39+
void USBHS_IRQHandler (void)
40+
{
41+
__asm volatile ("call USBHS_IRQHandler_impl; mret");
4042
}
4143

42-
__attribute__ ((used)) void USBHS_IRQHandler_impl(void) {
43-
tud_int_handler(0);
44+
__attribute__ ((used)) void USBHS_IRQHandler_impl (void)
45+
{
46+
tud_int_handler(0);
4447
}
4548

4649
//--------------------------------------------------------------------+
@@ -49,21 +52,20 @@ __attribute__ ((used)) void USBHS_IRQHandler_impl(void) {
4952

5053
uint32_t SysTick_Config(uint32_t ticks)
5154
{
52-
NVIC_EnableIRQ(SysTicK_IRQn);
53-
SysTick->CTLR=0;
54-
SysTick->SR=0;
55-
SysTick->CNT=0;
56-
SysTick->CMP=ticks-1;
57-
SysTick->CTLR=0xF;
58-
return 0;
55+
NVIC_EnableIRQ(SysTicK_IRQn);
56+
SysTick->CTLR=0;
57+
SysTick->SR=0;
58+
SysTick->CNT=0;
59+
SysTick->CMP=ticks-1;
60+
SysTick->CTLR=0xF;
61+
return 0;
5962
}
6063

6164
void board_init(void) {
6265

6366
/* Disable interrupts during init */
6467
__disable_irq();
6568

66-
6769
#if CFG_TUSB_OS == OPT_OS_NONE
6870
SysTick_Config(SystemCoreClock / 1000);
6971
#endif
@@ -79,11 +81,19 @@ void board_init(void) {
7981

8082
GPIO_InitTypeDef GPIO_InitStructure = {0};
8183

82-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
83-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
84+
// LED
85+
LED_CLOCK_EN();
86+
GPIO_InitStructure.GPIO_Pin = LED_PIN;
8487
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
8588
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
86-
GPIO_Init(GPIOC, &GPIO_InitStructure);
89+
GPIO_Init(LED_PORT, &GPIO_InitStructure);
90+
91+
// Button
92+
BUTTON_CLOCK_EN();
93+
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
94+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
95+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
96+
GPIO_Init(BUTTON_PORT, &GPIO_InitStructure);
8797

8898
/* Enable interrupts globally */
8999
__enable_irq();
@@ -114,26 +124,29 @@ uint32_t board_millis(void) { return system_ticks; }
114124
// Board porting API
115125
//--------------------------------------------------------------------+
116126

117-
void board_led_write(bool state) {
118-
(void) state;
119-
120-
GPIO_WriteBit(GPIOC, GPIO_Pin_0, state);
127+
void board_led_write (bool state)
128+
{
129+
GPIO_WriteBit(LED_PORT, LED_PIN, state);
121130
}
122131

123-
uint32_t board_button_read(void) {
124-
return false;
132+
uint32_t board_button_read (void)
133+
{
134+
return BUTTON_STATE_ACTIVE == GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN);
125135
}
126136

127-
int board_uart_read(uint8_t* buf, int len) {
128-
(void)buf;
129-
(void)len;
137+
int board_uart_read (uint8_t *buf, int len)
138+
{
139+
(void) buf;
140+
(void) len;
130141
return 0;
131142
}
132143

133-
int board_uart_write(void const* buf, int len) {
144+
int board_uart_write (void const *buf, int len)
145+
{
134146
int txsize = len;
135-
while (txsize--) {
136-
uart_write(*(uint8_t const*)buf);
147+
while ( txsize-- )
148+
{
149+
uart_write(*(uint8_t const*) buf);
137150
buf++;
138151
}
139152
return len;

0 commit comments

Comments
 (0)