Line: 1 to 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Board Support Package for the STM32WB Feather Development Board
Intro
The board support package for the STM32WB Feather Development Board (see https://www.reclaimerlabs.com/stm32wb-feather) is restricted to the JP1 and JP3 pin header, the onboard LEDs and switches (buttons), and the onboard 16 MiB serial NOR flash. The STM32WB55 has much more capabilities than 11 digital I/O pins, 6 analog input pins, UART, SPI, and I2C interfaces. But if you want to use the more advanced features you can use the CubeMX to create source code for the internal peripherals. This project wants to show how to use the Cube Ecosystem for a Forth system (or vice versa) and can't implement all features and possibilities the STM32WB has. It is a good starting point for your project.
Mecrisp-Cube for the STM32WB55 Feather Development Board
Contents
Overview
Board Support Wordsled1! ( n -- ) sets LED1 (red) led1@ ( -- n ) gets LED1 (red) neopixel! ( rgb -- ) sets the neopixel RGB led ($ff0000 red, $00ff00 green, $0000ff blue) neopixel@ ( -- rgb ) gets the neopixel RGB led ($ff0000 red, $00ff00 green, $0000ff blue) switch1? ( -- flag ) gets switch1 (button A), closed=TRUE switch2? ( -- flag ) gets switch2 (button B), closed=TRUE switch3? ( -- flag ) gets switch3 (button C), closed=TRUE switchuser? ( -- flag ) gets user button, closed=TRUE dport! ( n -- ) sets the digital output port (D0=bit0 .. D15=bit15). dport@ ( -- n ) gets the digital input/output port (D0=bit0 .. D15=bit15). dpin! ( n a -- ) sets the digital output port pin a (D0=0 .. D15=15, A0=16 .. A6=22) dpin@ ( a -- n ) gets the digital input/output port pin a dmod ( u a -- ) sets the pin mode: 0 in, 1 in pull-up, 2 in pull-down, 3 out push pull, 4 out open drain, 5 out push pull PWM, 6 input capture, 7 output compare, 8 I2C, 9 USART, 10 analog EXTImod ( u a -- ) Sets for pin a (D5, D6, D11, D13) the EXTI mode u: 0 rising, 1 falling, 2 both edges, 3 none EXTIwait ( u a -- ) Wait for EXTI interrupt on pin a (D5, D6, D11, D13), timeout u in [ms] pwmpin! ( u a -- ) sets the digital output port pin a (D0=0, D1=1, A4=20) to a PWM value u (0..1000). Default frequency is 1 kHz, TIMER1 pwmprescale ( u -- ) Sets the PWM prescale for TIMER1. 32 kHz / prescale, default 32 -> PWM frequency 1 kHz ICOCprescale ( u -- ) Sets the input capture / output compare prescale for TIMER2. default 32 -> 32 MHz / 32 = 1 MHz, timer resolution 1 us ICOCperiod! ( u -- ) Sets the input capture / output compare (TIMER2) period. default $FFFFFFFF (4'294'967'295). When the up counter reaches the period, the counter is set to 0. For prescale 32 the maximum time is about 1 h 11 m ICOCcount! ( -- u ) Sets the input capture / output compare counter for TIMER2 ICOCcount@ ( u -- ) Gets the input capture / output compare counter for TIMER2 ICOCstart ( -- ) Starts the ICOC period ICOCstop ( -- ) Stops the ICOC period OCmod ( u a -- ) Sets for pin a (D5, D6, D13) the Output Compare mode u: 0 frozen, 1 active level on match, 2 inactive level on match, 3 toggle on match, 4 forced active, 5 forced inactive OCstart ( u a -- ) Starts the output compare mode for pin a with pulse u OCstop ( a -- ) Stops output compare for pin a ICstart ( u -- ) Starts input capture u for pin A5: 0 rising edge, 1 falling edge, 2 both edges ICstop ( -- ) Stops input capture waitperiod ( -- ) wait for the end of the TIMER2 period OCwait ( a -- ) wait for the end of output capture on pin a ICwait ( u -- u ) wait for the end of input capture with timeout u, returns counter u apin@ ( a -- u ) gets the analog input port pin (A0=0 .. A5=5). Returns a 12 bit value (0..4095) Using the Digital Port Pins (Input and Output)Set 8 port pins to push/pull output3 15 dmod \ set D15 (SCL) to Output 3 5 dmod \ set D5 to Output 3 6 dmod \ set D6 to Output 3 9 dmod \ set D9 to Output 3 10 dmod \ set D10 to Output 3 11 dmod \ set D11 to output 3 12 dmod \ set D12 to output 3 13 dmod \ set D13 to outputremap D15, D5, .. D13 create port-map 15 , 5 , 6 , 9 , 10 , 11 , 12 , 13 , : pin ( n -- n ) \ gets the Dx pin number cells port-map + @ ;
3 16 dmod \ set A0 to Output 3 17 dmod \ set A1 to Output 3 18 dmod \ set A2 to Output 3 19 dmod \ set A3 to Output 3 20 dmod \ set A4 to Output 3 21 dmod \ set A5 to output 3 2 dmod \ set D2 (SCK) to output 3 4 dmod \ set D4 (MOSI) to outputremap D15, D5, .. D13 create port-map 16 , 17 , 18 , 19 , 20 , 21 , 2 , 4 , Using the ADC (Analog Input Pins)Control the Neopixelapin@ ( a -- u ) returns the ADC value (12 bit, 0 .. 4095) from one of the analog pins A0 to A5 (0 .. 5). Here I use the A0 to control the neopixel blue led brightness.
: neo-blue ( -- ) begin 0 apin@ 16 / neopixel! 10 osDelay drop key? until key drop ; Control the Knightrider Paceapin@ ( a -- u ) returns the ADC value (12 bit, 0 .. 4095) from one of the analog pins A0 to A5 (0 .. 5). Here I use the A0 to control the delay.
Using the PWM (Analog Output Pins)Three port pins are supported so far. The 16 bit timer TIM1 (D0, D1, A4) is used for the timebase, time resolution is 1 us (32 MHz SysClk divided by 32). The PWM scale is from 0 (0 % duty cycle) to 1000 (100 % duty cycle), this results in a PWM frequency of 1 kHz. If you need higher PWM frequencies, decrease the divider and/or the scale. PWM port pins: D0 (TIM1CH3), D1 (TIM1CH2), and A4 (TIM1CH1). Simple test program to set brightness of a LED on pin D0 with a potentiometer on A0. Default PWM frequency is 1 kHz (prescaler set to 32). You can set the prescale with the wordpwmprescale from 32 kHz (value 1) down to 0.5 Hz (64000).
5 0 dmod \ set D0 to PWM : pwm ( -- ) begin 0 apin@ 4 / 0 pwmpin! 10 osDelay drop key? until key drop ; Control an RC Servohttps://en.wikipedia.org/wiki/Servo_(radio_control): The control signal is a digital PWM signal with a 50 Hz frame rate. Within each 20 ms timeframe, an active-high digital pulse controls the position. The pulse nominally ranges from 1.0 ms to 2.0 ms with 1.5 ms always being center of range. Pulse widths outside this range can be used for "overtravel" - moving the servo beyond its normal range. A servo pulse of 1.5 ms width will typically set the servo to its "neutral" position (typically half of the specified full range), a pulse of 1.0 ms will set it to 0°, and a pulse of 2.0 ms to 90° (for a 90° servo). The physical limits and timings of the servo hardware varies between brands and models, but a general servo's full angular motion will travel somewhere in the range of 90° – 180° and the neutral position (45° or 90°) is almost always at 1.5 ms. This is the "standard pulse servo mode" used by all hobby analog servos. The BSPs default PWM frequency is 1 kHz, 50 Hz is 20 times slower. The divider is therefore 32 * 20 = 640.
640 pwmprescale 5 0 dmod \ set D0 to PWM : servo ( -- ) begin 130 40 do i 0 pwmpin! i neopixel! i 40 = if 1000 \ give some more time to get back else 200 then osDelay drop 10 +loop key? until key drop ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Added: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> > | 640 pwmprescale 5 3 dmod \ set D3 to PWM : slowservo ( -- ) begin 100 50 do i 3 pwmpin! 50 osDelay drop 1 +loop 50 100 do i 3 pwmpin! 50 osDelay drop -1 +loop key? until key drop ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Using Input Capture and Output CompareTime BaseDefault timer resolution is 1 us. The 32 bit TIMER2 is used as time base for Input Capture / Output Compare. For a 5 s period 5'000'000 cycles are needed. All channels (input capture / output compare) use the same time base.: period ( -- ) 5000000 ICOCperiod! \ 5 s period ICOCstart begin waitperiod cr .time key? until key drop ; Output CompareOutput compare TIM2: D5, D6, and D137 5 dmod \ output compare for D5 7 6 dmod \ output compare for D6 7 13 dmod \ output compate for D13 : oc-toggle ( -- ) 5000000 ICOCperiod! \ 5 s period ICOCstart 3 5 OCmod 1000000 5 OCstart \ toggle D5 after 1 s 3 6 OCmod 2000000 5 OCstart \ toggle D6 after 2 s 3 13 OCmod 3000000 13 OCstart \ toggle D13 after 3 s begin waitperiod cr .time key? until key drop ;When you abort (hit any key) the program, the timer still runs and controls the port pins. To stop the port pins: 5 OCstop 5 OCstop 13 OCstopOr change the prescale to make it faster or slower: 1 ICOCprescale Input CaptureThis sample program measures the time between the edges on port A5. if no event occurs within 2 seconds, "timeout" is issued. Hit any key to abort program.: ic-test ( -- ) 6 21 dmod \ input capture on A5 ICOCstart 2 ICstart \ both edges ICOCcount@ ( -- count ) begin 2000 \ 2 s timeout ICwait ( -- old-capture capture ) cr dup 0= if ." timeout" drop else dup rot ( -- capture capture old-capture ) - 1000 / . ." ms" then key? until key drop drop ICstop ; Use Ouput Compare for PWMUsing EXTI lineD5, D6, D11 and D13 can be used as an EXTI line. EXTIs are external interrupt lines, D5 uses EXTI2 (EXTI Line2 interrupt), D6 EXTI3, D11 EXIT8, and D13 EXTI1.: exti-test ( -- ) 2 5 EXTImod \ both edges on D5 begin 2000 5 EXTIwait \ wait for edge on D5 (button C) with 2 s timeout cr 0= if 5 dpin@ if ." rising edge" else ." falling edge" then else ." timeout" then key? until key drop ; PinoutsThis BSP is using the standard Feather numbering scheme and not the Argon numbering scheme. The red LED should be connected to D13, but it is connected to D12 (D7 in Argon numbering scheme).SCL D15, SDA D14 SCK D2, MO D4, MI D3, RX D0, TX D1The anlog pins can be used as digital pins too: A0 D16, A1 D17, A2 D18, A3 D19, A4 D20, A5 D21
Digital PinsJP1.1NCRESET/ JP1.16UART_RX / GPIO D0 / PA10 / JP1.3
UART_TX / GPIO D1 / PA9 / JP1.2
SCK / GPIO D2 / PB3 / JP1.6
MISO / GPIO D3 / PB4 / JP1.4
MOSI / GPIO D4 / PB1 / JP1.5
GPIO D5 / Button C / PA2 / JP3.10
GPIO D6 / Button B / PB8 / JP3.9
GPIO D7
GPIO D8
GPIO D9 / Button A / PB1 / JP3.8GPIO D10 / PB9 / JP3.7
GPIO D11 / PB8 / JP3.6
GPIO D12 / red LED / PB2 / JP3.5
GPIO D13 / (red LED) / PA1 / JP3.4
SDA / GPIO D14 / PB7 / JP3.12SCL / GPIO D15 / PB6 / JP3.11Analog PinsA0 / GPIO 16 / PA4 / JP1.12
A1 / GPIO 17 / PA5 / JP1.11
A2 / GPIO18 / PA6 / JP1.10
A3 / GPIO19 / PA7 / JP1.9
A4 / GPIO20 / PA8 / JP1.8
A5 / GPIO21 / PA0 / JP1.7
SPI FlashFLASH_SCK / PB3FLASH_MISO / PB4FLASH_MOSI / PB5FLASH_CS / PA15USBUSB_DP / PA12USB_DM / PA11AntennaCTRL_FE1 / PE4Low chip antenna, high UFL connectotCTRL_FE2 / PB0VDD for HF switch.This work by Peter Schmid is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
|