Skip to content

Instantly share code, notes, and snippets.

@TG9541
Last active September 29, 2023 09:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TG9541/1761fa86b425a0c909b7bd1cc8017c2b to your computer and use it in GitHub Desktop.
Save TG9541/1761fa86b425a0c909b7bd1cc8017c2b to your computer and use it in GitHub Desktop.
STM8 eForth WS2812 LED Strip Demo
\ A STM8 eForth WS2812 demo with tested timing
\ for STM8S w/ 16MHz (HSI) clock
\ 3.3V MINDEV: PB4 with 1K pull-up to 5V works well
\res MCU: STM8S103
\res export PB_DDR PB_ODR PB_CR1
#require ]B!
#require ]CB
NVM
\ starting from memory a transfer n bytes to a WS2812 chain
: WS2812 ( a n -- )
OVER + ( a a1 ) SWAP
DO [
$1601 , \ LDW Y,(1,SP)
$90F6 , \ LD A,(Y)
$88 C, \ PUSH A
$A608 , \ LD A,#8
HERE \ 0$:
] [ 1 PB_ODR 4 ]B! [
$0901 , \ RLC (1,SP)
$9D C, \ NOP
$9D C, \ NOP
] [ PB_ODR 4 ]CB [
$9D C, \ NOP
$9D C, \ NOP
$9D C, \ NOP
$9D C, \ NOP
] [ 0 PB_ODR 4 ]B! [
$9D C, \ NOP
$4A C, \ DEC A
$4D C, \ TNZ A
$26 C, \ JRNE ...
HERE - 1- C, \ ... 0$
$84 C, \ POP A
]
LOOP ;
: init ( -- )
[ 0 PB_ODR 4 ]B!
[ 1 PB_DDR 4 ]B!
[ 1 PB_CR1 4 ]B! ;
RAM
\\ Example
\ 8 x WS2812B on PCB with 470µF capacitor at 5V supply
\ STM8S103F3: PB4 is pin 12 - remember the 1K pull-up resistor!
#include ws2812.fs
8 3 * CONSTANT #leds \ memory for 8 x WS2812
VARIABLE wsmem #leds 2- ALLOT
\ a very simple test with delay n
: test ( n -- )
init
255 FOR
wsmem #leds I FILL
wsmem #leds WS2812
DUP FOR ( wait ) NEXT \ pause
NEXT DROP ;
\ e.g. 10000 test
@uwu64
Copy link

uwu64 commented Aug 22, 2020

@TG9541 Thank you for replying to our question. Since we're using a generic serial terminal, entering words from library manually does indeed seem to work. You've helped us a lot, thanks again.

@rlourette
Copy link

Thanks. Did you ever consider using the SPI MOSI pin to send the wave forms to the LEDs?

@TG9541
Copy link
Author

TG9541 commented Nov 13, 2021

@rlourette yes, I did - that would certainly work but unless one uses a STM8L device with DMA I don't expect any advantage over the timing-stable loop shown in the code.

In the case of using DMA the bit coding for an RGB byte group would have to be represented as (at least) 24 bytes. As both RAM and ROM aren't exactly an abundant resources in STM8L devices the use case would be limited to (very) timing sensitive applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment