something_i_am_sure

This commit is contained in:
2026-10-30 10:37:42 +01:00
parent 9615b5d42f
commit aef25a9d47
24 changed files with 4401 additions and 13 deletions
+8 -2
View File
@@ -10,11 +10,11 @@
#
# This build does NOT use the WCH EVT SDK, WCH's GCC, libprintf, or IQmath.
TARGET ?= firmware
TARGET ?= ch32v003_GPIO_USB
TARGET_MCU ?= CH32V003
# Root of the ch32fun checkout (the directory containing ch32fun/ and minichlink/).
CH32FUN_ROOT ?= $(HOME)/code/ch32fun
CH32FUN_ROOT ?= ./rv003usb/ch32fun
CH32FUN ?= $(CH32FUN_ROOT)/ch32fun
MINICHLINK ?= $(CH32FUN_ROOT)/minichlink
@@ -27,6 +27,12 @@ NEWLIB ?= /usr/riscv64-elf/include
# make PREFIX=riscv32-unknown-elf
PREFIX ?= riscv64-elf
EXTRA_CFLAGS:=-I./rv003usb/lib -I./rv003usb/rv003usb -I../include -O3
ADDITIONAL_C_FILES+=$(wildcard ./src/*.c)
ADDITIONAL_C_FILES+=$(wildcard ./rv003usb/rv003usb/*.c)
ifeq ($(wildcard $(CH32FUN)/ch32fun.mk),)
$(error ch32fun not found at $(CH32FUN). Set CH32FUN_ROOT=/path/to/ch32fun)
endif
+3 -11
View File
@@ -1,16 +1,8 @@
/* SPDX-License-Identifier: MIT */
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H
/* CH32V003: 48 MHz from the internal HSI + PLL. */
#define FUNCONF_USE_HSI 1
#define FUNCONF_USE_PLL 1
#define FUNCONF_SYSTEM_CORE_CLOCK 48000000
/*
* printf() goes to the SWIO debug channel by default.
* Use `make monitor` (minichlink -T) to view it.
*/
#define FUNCONF_USE_DEBUGPRINTF 1
#define FUNCONF_USE_DEBUGPRINTF 1
#define FUNCONF_SYSTICK_USE_HCLK 1
#endif
+43
View File
@@ -0,0 +1,43 @@
#include <stdint.h>
#include "ch32v003hw.h"
#include "rv003usb.h"
#include "ch32fun.h"
#include "usb.h"
static uint8_t ctl_buf[4] __attribute__((aligned(4)));
const uint8_t pin_lookup[] = {PA1,PA2,PC0,PC1,PC2,PC3,PC4,PC5,PC6,PC7,PD0,PD1,PD2,PD7};
void usb_handle_other_control_message(struct usb_endpoint *e, struct usb_urb *s,
struct rv003usb_internal *ist) {
uint8_t type = s->wRequestTypeLSBRequestMSB & 0xff;
uint8_t req = s->wRequestTypeLSBRequestMSB >> 8;
uint16_t wValue = s->lValueLSBIndexMSB & 0xffff; // driver: (op << 8) | pin
int len = 0;
if (type != 0xC0) return; // vendor, device to host
if (req == 0 /* GET_INFO */) {
struct microcontroller_capabilities* capabs = (struct microcontroller_capabilities*)ctl_buf;
capabs->num_gpio = sizeof(pin_lookup) / sizeof(typeof(pin_lookup[0]));
capabs->num_uart = 1;
capabs->num_spi = 1;
capabs->num_adc = 4 ;
len = 4;
} else if (req == 1 /* CONTROL_PINS */) {
switch (ctl_buf[0]) {
case (OP_GPIO_GET):
ctl_buf[0] = funDigitalRead(pin_lookup[ctl_buf[1]]);
len = 1;
break;
}
}
e->opaque = ctl_buf;
e->max_len = (s->wLength < len) ? s->wLength : len;
}
void usb_handle_user_data(struct usb_endpoint *e, int ep, uint8_t *data, int len,
struct rv003usb_internal *ist) {
if (ep != 1 || len < 1) return;
/* data[0] = OP_GPIO_SET / OP_GPIO_SET_DIR ... */
}
+102
View File
@@ -0,0 +1,102 @@
#ifndef _USB_CONFIG_H
#define _USB_CONFIG_H
#define ENDPOINTS 2
#define USB_PORT D
#define USB_PIN_DP 3
#define USB_PIN_DM 4
// #define USB_PIN_DPU 2
#define RV003USB_OPTIMIZE_FLASH 1
#define RV003USB_DEBUG_TIMING 0
#define RV003USB_EVENT_DEBUGGING 0
#define RV003USB_HANDLE_IN_REQUEST 0
#define RV003USB_OTHER_CONTROL 1
#define RV003USB_HANDLE_USER_DATA 1
#define RV003USB_HID_FEATURES 0
#define RV003USB_USB_TERMINAL 0
#define RV003USB_USE_REBOOT_FEATURE_REPORT 0
#ifndef __ASSEMBLER__
#include <stdint.h>
#ifdef INSTANCE_DESCRIPTORS
static const uint8_t device_descriptor[] = {
18, 1,
0x10, 0x01, // USB 1.1
0x00, 0x00, 0x00,
0x08, // EP0 max packet
0xd0, 0xfa, // idVendor 0xfad0
0x00, 0x90, // idProduct 0x9000
0x02, 0x00,
1, 2, 3,
1,
};
static const uint8_t config_descriptor[] = {
9, 2,
0x20, 0x00, // wTotalLength = 9 + 9 + 7 + 7
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00,
0x80,
0x64,
9, 4,
0, // bInterfaceNumber
0, // bAlternateSetting
2, // bNumEndpoints
0xff, // vendor specific class
0x00,
0x00,
0,
7, 0x05,
0x01, // EP1 OUT
0x03, // interrupt
0x08, 0x00, // max packet 8
10, // interval ms
7, 0x05,
0x81, // EP1 IN
0x03,
0x08, 0x00,
10,
};
#define STR_MANUFACTURER u"That real manufacturer"
#define STR_PRODUCT u"CH32V003 USB GPIO module"
#ifndef STR_SERIAL
#define STR_SERIAL u"deadbeef"
#endif
struct usb_string_descriptor_struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wString[];
};
const static struct usb_string_descriptor_struct string0 __attribute__((section(".rodata"))) = { 4, 3, {0x0409} };
const static struct usb_string_descriptor_struct string1 __attribute__((section(".rodata"))) = { sizeof(STR_MANUFACTURER), 3, STR_MANUFACTURER };
const static struct usb_string_descriptor_struct string2 __attribute__((section(".rodata"))) = { sizeof(STR_PRODUCT), 3, STR_PRODUCT };
const static struct usb_string_descriptor_struct string3 __attribute__((section(".rodata"))) = { sizeof(STR_SERIAL), 3, STR_SERIAL };
const static struct descriptor_list_struct {
uint32_t lIndexValue;
const uint8_t *addr;
uint8_t length;
} descriptor_list[] = {
{0x00000100, device_descriptor, sizeof(device_descriptor)},
{0x00000200, config_descriptor, sizeof(config_descriptor)},
{0x00000300, (const uint8_t *)&string0, 4},
{0x04090301, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
{0x04090302, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
{0x04090303, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
};
#define DESCRIPTOR_LIST_ENTRIES ((sizeof(descriptor_list))/(sizeof(struct descriptor_list_struct)))
#endif
#endif
#endif