Skip to content

Commit db349ad

Browse files
authored
Merge pull request #18 from picoruby/stdout-to-usb
Stdout to USB
2 parents 369376f + 553e8e3 commit db349ad

File tree

20 files changed

+382
-116
lines changed

20 files changed

+382
-116
lines changed

CHANGELOG.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
# Change log
22

3+
## 2021/09/15
4+
### Useful feature 🎉
5+
- You can see debug print on a USB serial port that will be helpful if your `keymap.rb` doesn't work well
6+
- Configuration:
7+
8+
```
9+
Baud: 115200
10+
Data bits: 8
11+
Parity: None
12+
Stop bits: 1
13+
Flow control: None
14+
```
15+
16+
![](doc/images/serial_port.png)
17+
318
## 2021/09/12
419
### Small improvement
520
- Sparkfun Pro Micro RP2040 will reboot to BOOTSEL mode if you double-press RESET button without detaching USB cable!
621
7-
![](doc/images/RP2040_boards.jpg)
22+
![](doc/images/RP2040_boards.jpg)
823
924
## 2021/09/10
1025
### BIG BIG IMPROVEMENT 🍣

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(prk_firmware)
88

99
add_definitions(
1010
-DNDEBUG
11-
-DMRBC_USE_HAL_RP2040
11+
-DMRBC_USE_HAL_USER_RESERVED
1212
-DMRBC_REQUIRE_32BIT_ALIGNMENT
1313
-DMAX_REGS_SIZE=256
1414
-DMAX_SYMBOLS_COUNT=500
@@ -26,7 +26,7 @@ pico_sdk_init()
2626

2727
execute_process (COMMAND date +%Y%m%d OUTPUT_VARIABLE CMAKE_BUILDDATE OUTPUT_STRIP_TRAILING_WHITESPACE)
2828
execute_process (COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE CMAKE_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE)
29-
set (PRK_VERSION 0.9.1)
29+
set (PRK_VERSION 0.9.2)
3030
set (PRK_BUILDDATE ${CMAKE_BUILDDATE})
3131
set (PRK_REVISION ${CMAKE_REVISION})
3232
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/version.h")
@@ -127,6 +127,6 @@ pico_add_extra_outputs(${PROJECT})
127127
add_custom_command(
128128
TARGET ${PROJECT}
129129
POST_BUILD
130-
COMMAND gzip -k ${PROJECT}.uf2
130+
COMMAND gzip -kf ${PROJECT}.uf2
131131
COMMAND zip -r ${PROJECT}.uf2.zip ${PROJECT}.uf2
132132
)

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ _(left: Raspberry Pi Pico / right: Sparkfun Pro Micro RP2040)_
3333
- [ ] Media keys
3434
- [x] RGBLED. An example on [picoruby/prk_crkbd](https://github.com/picoruby/prk_crkbd/blob/main/keymap.rb#L61-L76)
3535
- [ ] OLED display
36-
- [x] Rotary encoder *new!* An example on [prk_helix_rev3/keymap.rb](https://github.com/picoruby/prk_helix_rev3/blob/master/keymap.rb#L79-L100)
36+
- [x] Rotary encoder. An example on [prk_helix_rev3/keymap.rb](https://github.com/picoruby/prk_helix_rev3/blob/master/keymap.rb#L79-L100)
3737
- [ ] Mouse / Trackball
38+
- [x] Debugging over a serial port
3839

3940
### Getting started
4041

@@ -90,12 +91,12 @@ You may not want PRK Firmware to be a mass storage device in case that your empl
9091
git clone https://github.com/picoruby/prk_meishi2.git
9192
```
9293
93-
- Edit `prk_meishi2/keymap.rb` as you wish
94+
- (Option) Edit `prk_meishi2/keymap.rb` as you wish
9495
9596
- Build with `cmake` and `make`
9697
9798
```
98-
cd prk_meishi2/build
99+
cd [path]/[to]/prk_meishi2/build
99100
cmake -DPRK_NO_MSC=1 ../../..
100101
make
101102
```
@@ -104,19 +105,38 @@ You may not want PRK Firmware to be a mass storage device in case that your empl
104105
105106
Now you should have `prk_firmware-[version]-[date]-no_msc.uf2` file in `prk_firmware/keyboards/prk_meishi2/build/` directory which includes your keymap in code.
106107
108+
- (Troubleshooting) If you got an error on `cmake -DPRK_NO_MSC=1 ../../..`
109+
- Try [this procedure](#building-uf2-of-excluding-keymap-version) once
110+
- Then, redo `cmake` again
111+
107112
- Install that `.uf2` file into RP2040
108113
109114
### What if split type keyboard?
110115
111116
- Make sure installing your setup on both side
112117
118+
### Tips
119+
120+
- You can see debug print on a "USB Serial Port" (so-called "COM Port" in Windows) that will be helpful if your `keymap.rb` doesn't work well
121+
122+
```
123+
Baud: 115200
124+
Data bits: 8
125+
Parity: None
126+
Stop bits: 1
127+
Flow control: None
128+
```
129+
130+
![](doc/images/serial_port.png)
131+
113132
### Contributing
114133
115134
#### Building uf2 of excluding-keymap-version
116135
117136
```
118-
cd prk_firmware/build
119-
cmake -DCMAKE_BUILD_TYPE=Debug ..
137+
# in the top directory of prk_firmware
138+
./setup.sh
139+
cd build
120140
make
121141
```
122142

doc/images/serial_port.png

9.29 KB
Loading

lib/CMakeLists.txt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ project(picoruby
66
LANGUAGES C
77
)
88

9+
#execute_process (
10+
# COMMAND ln -sf ../../../../../../lib/hal/hal.c
11+
# COMMAND ln -sf ../../../../../../lib/hal/hal.h
12+
# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/picoruby/src/mrubyc/src/hal_user_reserved
13+
#)
14+
915
add_library(picoruby STATIC
1016
./picoruby/src/mrubyc/src/alloc.c
1117
./picoruby/src/mrubyc/src/c_array.c
@@ -26,7 +32,7 @@ add_library(picoruby STATIC
2632
./picoruby/src/mrubyc/src/symbol.c
2733
./picoruby/src/mrubyc/src/value.c
2834
./picoruby/src/mrubyc/src/vm.c
29-
./picoruby/src/mrubyc/src/hal_rp2040/hal.c
35+
./picoruby/src/mrubyc/src/hal_user_reserved/hal.c
3036

3137
./picoruby/src/common.c
3238
./picoruby/src/compiler.c
@@ -42,19 +48,21 @@ add_library(picoruby STATIC
4248
)
4349

4450
add_custom_target(picorbc
45-
COMMAND make host_production_libc
51+
COMMAND make
4652
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/picoruby
4753
)
48-
49-
add_custom_target(ptr_size
50-
COMMAND make
51-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/picoruby/src/include
54+
add_custom_target(hal_posix
55+
COMMAND ln -sf ../../../../cli/picoshell_lib/hal_posix/hal.c
56+
COMMAND ln -sf ../../../../cli/picoshell_lib/hal_posix/hal.h
57+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/picoruby/src/mrubyc/src/hal_user_reserved
5258
)
53-
add_custom_target(tokenizer_helper
54-
COMMAND make tokenizer_helper.h
55-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/picoruby/src
59+
add_custom_target(hal_user_reserved
60+
COMMAND ln -sf ../../../../../../lib/hal/hal.c
61+
COMMAND ln -sf ../../../../../../lib/hal/hal.h
62+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/picoruby/src/mrubyc/src/hal_user_reserved
5663
)
57-
add_dependencies(picoruby picorbc ptr_size tokenizer_helper)
64+
add_dependencies(picoruby hal_user_reserved picorbc)
65+
add_dependencies(picorbc hal_posix)
5866

5967
target_link_libraries(picoruby pico_stdlib)
6068
target_compile_features(picoruby PRIVATE)
@@ -64,6 +72,13 @@ target_include_directories(picoruby INTERFACE
6472
./picoruby/src/mrubyc/src
6573
./picoruby/src/mrubyc/src/hal_rp2040
6674
)
75+
76+
# For hal.c
77+
target_include_directories(picoruby PUBLIC
78+
${PICO_SDK_PATH}/lib/tinyusb/src
79+
../include
80+
)
81+
6782
set_target_properties(picoruby
6883
PROPERTIES
6984
VERSION ${PROJECT_VERSION}

lib/hal/hal.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*! @file
2+
@brief
3+
Hardware abstraction layer
4+
for RP2040
5+
6+
<pre>
7+
Copyright (C) 2016-2021 Kyushu Institute of Technology.
8+
Copyright (C) 2016-2021 Shimane IT Open-Innovation Center.
9+
10+
This file is distributed under BSD 3-Clause License.
11+
</pre>
12+
*/
13+
14+
/***** Feature test switches ************************************************/
15+
/***** System headers *******************************************************/
16+
#include <string.h>
17+
18+
/***** Local headers ********************************************************/
19+
#include "hal.h"
20+
21+
/***** Constat values *******************************************************/
22+
/***** Macros ***************************************************************/
23+
/***** Typedefs *************************************************************/
24+
/***** Function prototypes **************************************************/
25+
/***** Local variables ******************************************************/
26+
struct repeating_timer timer;
27+
28+
/***** Global variables *****************************************************/
29+
/***** Signal catching functions ********************************************/
30+
/***** Local functions ******************************************************/
31+
/***** Global functions *****************************************************/
32+
#ifndef MRBC_NO_TIMER
33+
34+
//================================================================
35+
/*!@brief
36+
timer alarm irq
37+
38+
*/
39+
bool alarm_irq(struct repeating_timer *t) {
40+
mrbc_tick();
41+
}
42+
43+
//================================================================
44+
/*!@brief
45+
initialize
46+
47+
*/
48+
void hal_init(void){
49+
add_repeating_timer_ms(1, alarm_irq, NULL, &timer);
50+
clocks_hw->sleep_en0 = 0;
51+
clocks_hw->sleep_en1 = CLOCKS_SLEEP_EN1_CLK_SYS_TIMER_BITS
52+
| CLOCKS_SLEEP_EN1_CLK_SYS_USBCTRL_BITS
53+
| CLOCKS_SLEEP_EN1_CLK_USB_USBCTRL_BITS
54+
| CLOCKS_SLEEP_EN1_CLK_SYS_UART0_BITS
55+
| CLOCKS_SLEEP_EN1_CLK_PERI_UART0_BITS;
56+
}
57+
58+
//================================================================
59+
/*!@brief
60+
Write
61+
62+
@param fd dummy, but 1.
63+
@param buf pointer of buffer.
64+
@param nbytes output byte length.
65+
66+
Memo: Steps to use uart_putc_raw() with hal_write.
67+
1. Write in main function↓
68+
uart_init(uart0,115200);
69+
gpio_set_function(0,GPIO_FUNC_UART);
70+
gpio_set_function(1,GPIO_FUNC_UART);
71+
72+
2. Comment out the putchar for hal_write.
73+
3. Uncomment uart_putc_raw for hal_write.
74+
*/
75+
//int hal_write(int fd, const void *buf, int nbytes)
76+
//{
77+
// int i = nbytes;
78+
// const uint8_t *p = buf;
79+
//
80+
// while( --i >= 0 ) {
81+
// putchar( *p++ );
82+
// // uart_putc_raw(uart0, *p++ );
83+
// }
84+
//
85+
// return nbytes;
86+
//}
87+
/*
88+
* hal_write() using tinyusb
89+
*/
90+
#define CFG_TUSB_MCU 1
91+
#include "tusb_config.h"
92+
#include "tusb.h"
93+
int hal_write(int fd, const void *buf, int nbytes)
94+
{
95+
tud_cdc_write(buf, nbytes);
96+
tud_cdc_write_flush();
97+
return nbytes;
98+
}
99+
100+
//================================================================
101+
/*!@brief
102+
Flush write baffer
103+
104+
@param fd dummy, but 1.
105+
*/
106+
int hal_flush(int fd) {
107+
return 0;
108+
}
109+
110+
#endif /* ifndef MRBC_NO_TIMER */
111+
112+
113+
//================================================================
114+
/*!@brief
115+
abort program
116+
117+
@param s additional message.
118+
*/
119+
void hal_abort(const char *s)
120+
{
121+
if( s ) {
122+
hal_write(1, s, strlen(s));
123+
}
124+
125+
abort();
126+
}
127+

lib/hal/hal.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*! @file
2+
@brief
3+
Hardware abstraction layer
4+
for RP2040
5+
6+
<pre>
7+
Copyright (C) 2016-2021 Kyushu Institute of Technology.
8+
Copyright (C) 2016-2021 Shimane IT Open-Innovation Center.
9+
10+
This file is distributed under BSD 3-Clause License.
11+
</pre>
12+
*/
13+
14+
#ifndef MRBC_SRC_HAL_H_
15+
#define MRBC_SRC_HAL_H_
16+
17+
/***** Feature test switches ************************************************/
18+
/***** System headers *******************************************************/
19+
#include "pico/stdlib.h"
20+
#include "hardware/irq.h"
21+
#include "hardware/clocks.h"
22+
#include "hardware/sync.h"
23+
24+
/***** Local headers ********************************************************/
25+
/***** Constant values ******************************************************/
26+
#define ALARM_IRQ 0
27+
28+
/***** Macros ***************************************************************/
29+
#if !defined(MRBC_TICK_UNIT)
30+
#define MRBC_TICK_UNIT_1_MS 1
31+
#define MRBC_TICK_UNIT_2_MS 2
32+
#define MRBC_TICK_UNIT_4_MS 4
33+
#define MRBC_TICK_UNIT_10_MS 10
34+
// You may be able to reduce power consumption if you configure
35+
// MRBC_TICK_UNIT_2_MS or larger.
36+
#define MRBC_TICK_UNIT MRBC_TICK_UNIT_1_MS
37+
// Substantial timeslice value (millisecond) will be
38+
// MRBC_TICK_UNIT * MRBC_TIMESLICE_TICK_COUNT (+ Jitter).
39+
// MRBC_TIMESLICE_TICK_COUNT must be natural number
40+
// (recommended value is from 1 to 10).
41+
#define MRBC_TIMESLICE_TICK_COUNT 10
42+
#endif
43+
44+
#ifndef MRBC_NO_TIMER
45+
void hal_init(void);
46+
# define hal_enable_irq() irq_set_enabled(ALARM_IRQ, true)
47+
# define hal_disable_irq() irq_set_enabled(ALARM_IRQ, false)
48+
# define hal_idle_cpu() __wfi()
49+
#else // MRBC_NO_TIMER
50+
# define hal_init() ((void)0)
51+
# define hal_enable_irq() ((void)0)
52+
# define hal_disable_irq() ((void)0)
53+
# define hal_idle_cpu() (sleep_ms(1), mrbc_tick())
54+
55+
#endif
56+
57+
/***** Typedefs *************************************************************/
58+
/***** Global variables *****************************************************/
59+
/***** Function prototypes **************************************************/
60+
#ifdef __cplusplus
61+
extern "C" {
62+
#endif
63+
64+
int hal_write(int fd, const void *buf, int nbytes);
65+
int hal_flush(int fd);
66+
void hal_abort(const char *s);
67+
void alarm_init();
68+
69+
70+
71+
/***** Inline functions *****************************************************/
72+
73+
74+
#ifdef __cplusplus
75+
}
76+
#endif
77+
#endif // ifndef MRBC_HAL_H_

lib/picoruby

0 commit comments

Comments
 (0)