TG9541 / stm8ef

STM8 eForth - a user friendly Forth for simple µCs with docs
https://github.com/TG9541/stm8ef/wiki
Other
311 stars 66 forks source link

IF .. ELSE .. THEN with relative addressing #382

Closed TG9541 closed 3 years ago

TG9541 commented 3 years ago

Some applications, e.g. ISR handlers, Flash page write, or STM8L Low-Power-Run Mode require code that doesn't depend on the STM8 eForth core. While coding in assembler or C is possible, it would be great if Forth structures like IF .. ELSE .. THEN could be used with JRxx or BTJx.

TG9541 commented 3 years ago

An example is the improved I2C driver.

Here is my first sketch but I'm not sure if that's the best way to package this:

\ STM8eForth : control structures with relative addressing         TG9541-201124
\ ------------------------------------------------------------------------------

#require >Y

: THEN ( -- ) [COMPILE] [ HERE OVER - 1- SWAP C! [COMPILE] ] ; IMMEDIATE

: >REL ( -- ) HERE 0 C, ;  \ like >MARK for rel. branch

: ELSE ( -- )  [COMPILE] [ $20 C, [COMPILE] ] >REL   \ JRA rel
    SWAP [COMPILE] THEN ; IMMEDIATE

: JREQ ( F:Z -- ) [COMPILE] [ $27 C, [COMPILE] ] >REL ; IMMEDIATE

: IF ( n -- ) COMPILE >Y [COMPILE] JREQ ; IMMEDIATE

What worked quite well is requiring >REL in a combined bit-test-and-IF word like [ addr b# ]B@IF here:

#require >REL
: ]B@IF ( -- ) 2* $7201 + , ,  ] >REL ;    \ BTJF  a,#bit

A #require ]B@IF thus redefines ELSE and THEN, and in order to avoid surprises, also IF. There is currently no protection against relative offsets larger than +/- 127.

VK6TT commented 3 years ago

Hi Thomas

I wish I had a bit more time to look at this. While my own bit bang routines have proved reliable that might be because I haven't pushed the clock speed. When I get the need I'll be sure to dig through this but as always you are well ahead of me. Great job and all the best for the coming festive season!

Regards

Richard

On 26/11/2020 2:53 am, Thomas wrote:

An example is the improved I2C driver https://gist.github.com/TG9541/5c3405320794d91ef8129734a4bfc880#improving-the-solution.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/TG9541/stm8ef/issues/382#issuecomment-733891593, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7PE25ZKYSH63VSYPY44M3SRVHELANCNFSM4UCYYSFA.

TG9541 commented 3 years ago

Hi Richard, thanks, and greetings your way!

To tell the truth this is the first time I actually use an I2C peripheral. Bit-banging I2C is the way of gravity because since, to work with a device only needs to read one datasheet, not two. I'm now moving from an 8K EEPROM to a DS1621 to learn how the I2C API should look like. Obviously there needs to be error handling and a handshake with the application.

TG9541 commented 3 years ago

I used the code above as-is. #383 contains some examples for words one can use instead of IF (e.g. ]A<IF). Other condition words can be made from any JRxx or BTJx.

Examples are in the improved I2C code here.