Skip to content

Commit 8a26a8e

Browse files
committed
Add rv003usb example
1 parent 63ba03b commit 8a26a8e

File tree

10 files changed

+483
-0
lines changed

10 files changed

+483
-0
lines changed

.github/workflows/examples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- "examples/blinky-none-os-ch6xx"
2222
- "examples/ch32fun-blink"
2323
- "examples/ch32fun-oled"
24+
- "examples/ch32fun-rv003usb-composite-hid"
2425
- "examples/uart-printf-none-os"
2526
- "examples/webserver-ch32v307-none-os"
2627
- "examples/blinky-freertos"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pio
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
How to build PlatformIO based project
2+
=====================================
3+
4+
See <https://github.com/cnlohr/rv003usb/tree/master/demo_composite_hid>
5+
6+
1. [Install PlatformIO Core](https://docs.platformio.org/page/core.html)
7+
2. Download [development platform with examples](https://github.com/Community-PIO-CH32V/platform-ch32v/archive/develop.zip)
8+
3. Extract ZIP archive
9+
4. Run these commands:
10+
11+
```shell
12+
# Change directory to example
13+
$ cd platform-ch32v/examples/ch32fun-rv003usb-composite-hid
14+
15+
# Build project
16+
$ pio run
17+
18+
# Upload firmware
19+
$ pio run --target upload
20+
21+
# Upload firmware for the specific environment
22+
$ pio run -e ch32v003f4p6_evt_r0 --target upload
23+
24+
# Clean build files
25+
$ pio run --target clean
26+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter, extra scripting
4+
; Upload options: custom port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
;
7+
; Please visit documentation for the other options and examples
8+
; https://docs.platformio.org/page/projectconf.html
9+
10+
[env]
11+
platform = ch32v
12+
framework = ch32v003fun
13+
monitor_speed = 115200
14+
;upload_protocol = wlink
15+
16+
[env:genericCH32V003F4U6]
17+
board = genericCH32V003F4U6
18+
; prevent ch32fun from recognizing the wrong chip series
19+
; not needed anymore in latest ch32v platform versions, left for reference.
20+
;build_unflags = -DCH32V00x
21+
lib_deps = https://github.com/maxgerhardt/rv003usb.git#136e2d107c6c8c08c52acd5ae24e93e5b4af0a15
22+
extra_scripts =
23+
; convenience for showing USB descriptors in intellisense
24+
pre:$PROJECT_LIBDEPS_DIR/$PIOENV/rv003usb/.github/show_descriptors.py
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "ch32fun.h"
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include "rv003usb.h"
5+
6+
int main()
7+
{
8+
SystemInit();
9+
// funGpioInitC();
10+
// funPinMode(PC5, GPIO_CFGLR_OUT_10Mhz_PP);
11+
Delay_Ms(1); // Ensures USB re-enumeration after bootloader or reset; Spec demand >2.5µs ( TDDIS )
12+
usb_setup();
13+
while(1)
14+
{
15+
// funDigitalWrite(PC5, 1);
16+
// Delay_Ms(1000);
17+
// funDigitalWrite(PC5, 0);
18+
// Delay_Ms(1000);
19+
#if RV003USB_EVENT_DEBUGGING
20+
uint32_t * ue = GetUEvent();
21+
if( ue )
22+
{
23+
printf( "%lu %lx %lx %lx\n", ue[0], ue[1], ue[2], ue[3] );
24+
}
25+
#endif
26+
}
27+
}
28+
29+
void usb_handle_user_data( struct usb_endpoint * e, int current_endpoint, uint8_t * data, int len, struct rv003usb_internal * ist )
30+
{
31+
if (len > 0) {
32+
LogUEvent(1139, data[0], 0, current_endpoint);
33+
}
34+
}
35+
36+
void usb_handle_user_in_request( struct usb_endpoint * e, uint8_t * scratchpad, int endp, uint32_t sendtok, struct rv003usb_internal * ist )
37+
{
38+
if( endp == 1 )
39+
{
40+
// Mouse (4 bytes)
41+
static int i;
42+
static uint8_t tsajoystick[4] = { 0x00, 0x00, 0x00, 0x00 };
43+
i++;
44+
int mode = i >> 5;
45+
46+
// Move the mouse right, down, left and up in a square.
47+
switch( mode & 3 )
48+
{
49+
case 0: tsajoystick[1] = 1; tsajoystick[2] = 0; break;
50+
case 1: tsajoystick[1] = 0; tsajoystick[2] = 1; break;
51+
case 2: tsajoystick[1] = -1; tsajoystick[2] = 0; break;
52+
case 3: tsajoystick[1] = 0; tsajoystick[2] =-1; break;
53+
}
54+
usb_send_data( tsajoystick, 4, 0, sendtok );
55+
}
56+
else if( endp == 2 )
57+
{
58+
// Keyboard (8 bytes)
59+
static int i;
60+
static uint8_t tsajoystick[8] = { 0x00 };
61+
usb_send_data( tsajoystick, 8, 0, sendtok );
62+
63+
i++;
64+
65+
// Press a Key every second or so.
66+
if( (i & 0x7f) == 0 )
67+
{
68+
tsajoystick[4] = 0x05; // 0x05 = "b"; 0x53 = NUMLOCK; 0x39 = CAPSLOCK;
69+
}
70+
else
71+
{
72+
tsajoystick[4] = 0;
73+
}
74+
}
75+
else
76+
{
77+
// If it's a control transfer, empty it.
78+
usb_send_empty( sendtok );
79+
}
80+
}
81+
82+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _FUNCONFIG_H
2+
#define _FUNCONFIG_H
3+
4+
#define FUNCONF_USE_DEBUGPRINTF 1
5+
#define CH32V003 1
6+
#define FUNCONF_SYSTICK_USE_HCLK 1
7+
8+
#endif
9+

0 commit comments

Comments
 (0)