💾 Archived View for gem.librehacker.com › gemlog › tech › 20220727-0.gmi captured on 2023-11-04 at 11:57:43. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

Update on Forth RP2040 I2C Driver Project

I am still working on the I2C driver project during my lunch breaks, to utilize the built-in circuitry for I2C communication on my Rpi Pico, for Mecrisp Stellaris Forth. The code (WIP) is available on Codeberg:

mf-rp2040

In the process, I wrote diagnostic words to display...

- the GPIO function settings (from the GPIO_CTRL register)

- the GPIO pad settings

- the settings in the I2C0 control register

And I wrote words to configure the GPIO pins appropriately, and to set the speed mode for I2C0. Now I am working on a word to selected master or slave mode. At the present, I am focusing just on I2C0, the first I2C module the RP2040, although there is also an I2C1 available. I keep the diagnostic words compiled into flash, so they are always available after a reset, and then I just load the I2C driver code into RAM for testing and troubleshooting.

8 9 init_i2c0_pins  ok.
std_md set_i2c0_spd  ok.
cr gpio_fns 
0 U0T
1 U0R
2 SIO
3 SIO
4 SIO
5 SIO
6 SIO
7 SIO
8 I0D
9 I0C
10 SIO
11 SIO
12 SIO
13 SIO
14 SIO
15 SIO
16 SIO
17 SIO
18 SIO
19 SIO
20 SIO
21 SIO
22 SIO
23 SIO
24 SIO
25 SIO
26 SIO
27 SIO
28 SIO
29 SIO
 ok.
cr pad_ctrl 
0 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
1 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
2 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
3 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
4 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
5 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
6 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
7 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
8 SF:F SC:T PD:F PU:T 4mA IE:T OD:F 
9 SF:F SC:T PD:F PU:T 4mA IE:T OD:F 
10 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
11 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
12 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
13 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
14 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
15 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
16 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
17 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
18 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
19 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
20 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
21 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
22 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
23 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
24 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
25 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
26 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
27 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
28 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
29 SF:F SC:T PD:T PU:F 4mA IE:T OD:F 
 ok.
cr i2c_ctrl 
I2C0 MAMO:T SPED:S 10AL:F 10AM:F RSEN:T SLDI:T SDAD:F TXEC:F RXFH:F STMA:F 
I2C1 MAMO:T SPED:F 10AL:F 10AM:F RSEN:T SLDI:T SDAD:F TXEC:F RXFH:F STMA:F 
 ok.

As I've mentioned, (Mecrisp Stellaris) Forth makes for a pleasant microcontroller development experience, because you get a very interactive experience, while still being able to write directly to all the mapped memory, including all the memory-mapped registers. I find Forth code to be straightfoward to troubleshoot, because most of the time you can just layout the words on a piece of paper (or text editor) along-side notes tracking the stack effects, and you have a pretty clear view of what is going on. Also, since the syntax is so simple, you can just run all the words a word-at-a-time in the interpreter while checking the stack, except of course that you have to handle manually the effect of compile-only words like conditionals.

I got hung up for quite-a-while troubleshooting the word which sets the the I2C speed — my code seemed to be having no effect on the register. In the end I figured out that it was not actually my code, but rather something I didn't understand in the microcontroller itself. Per the datasheet, you can set the RP2040 to standard, fast, or high speed I2C mode. However, if you look at the fine print, there is some kind of configuration setting (hardwired? burned?) into the chip (or the microcontroller board...?) such that it has a MAX_SPEED_MODE, which apparently on my Pico is set to fast speed mode. If you try to set the register to a higher speed setting, the hardware will automatically set it back down to the highest speed actually supported. So, I was trying to set it to high speed mode, but I was only seeing fast mode, so it looked like nothing was happening.

Anyway, it has been fun working on this, though sometimes I wish could just focus on this one project and get it done in a day or two, rather than having to spread it out among a few dozen lunch breaks.