PICO C-code - (part 2) MicroPython and then down into C-Code for sdcard integration

Following on from my previous post about PICO, VGA and using the PICO DEMO board....

I had concluded that I was able to use the pico demo board to get a VGA output - awesome!

I had concluded that I was able to build the MCUME emulator code and that it was failing to read the SDCard.....with a "Card not ready yet" error message (3).

As I'm a stubborn old-git, I refused to let something like a simple error message prevent me from proceeding.

I tried a few more things, even use a bit of MicroPython in an attempt to read the card - that failed too.

However, I then noticed on the website for the product that it states:

Please note that VGA Demo Base only currently works with the C/C++ Pico SDK!

At which point I did a Homer Simpson and face-palmed my forehead.


Okay, time to get blowing off the C code cobwebs properly then....

I then tried a few variants of other people C code and found that didn't work either.

I kept coming back to the PIN diagram:



and the reference from the product PIN code layout.


As well as going back to the PDF document about the SDCard wiring.


I got a bit distracted and found this github repo:


MAKE SURE TO SELECT THE VGABOARD BRANCH!  The other master branch is for the PICO Display.

Which, although it doesn't read from the sdcard, it does output to the VGA nicely and also shows how to get the Buttons A,B,C to work from code - that is going to be useful / handy later on.




I even raised a Support query on the PIMORONI support forum, asking if anyone else had figured this out.  Apparently not.  I guess I was going to go off into "unknown territory" again, where's my hat?



Then just to tease me, I then found this github repo where a person had used a variant of the pico demo board (with HDMI instead of VGA) and had gotten it to work - clearly it was reading the ROM files from the sd-card.


Hmmm.... so I thought, you know what - I'll just rip out the C code from his repo. strip it down to the bare essentials and make my own project that just does the SD Card access.

Before I did that however, I kept coming back to this github repo as this was a nice tool for accessing SDCards and performing tasks etc..etc.. via the UART serial connection.

What I mistakenly was doing here was assuming that, like all the other LCD screens that I have, that the pico demo vga board was using SPI to communicate with the sdcard.  This led me into a rabbit-hole for a few more days, after all, why write C code from scratch when I can use this elegantly written code?  I tried and eventually failed with this....

So, back to writing my own code time.

I did write the code.  I even lifted the library of code from the REPO mentioned earlier about the NES - as that person clearly had it working.

My first fault was the usage of the sdcard.h file - I took what was there and ass-u-me'd that it was clearly configured correctly.  That came back to bite me - see later*

Anyway, I wrote my own code, I even uploaded it to GITHUB

https://github.com/pi-gram/pico_c/tree/main/picovga

I even put in printf() commands that showed what file and where the code was executing so I could fully trace what was going on.

SDCARD_TEST.C: system init response=0
SDCARD_TEST.C: SDCARD_SPI_BUS=1073987584
SDCARD_TEST.C: SDCARD_PIN_SPI0_CS=22
SDCARD_TEST.C: SDCARD_PIN_SPI0_SCK=18
SDCARD_TEST.C: SDCARD_PIN_SPI0_MOSI=19
SDCARD_TEST.C: SDCARD_PIN_SPI0_MISO=20
SDCARD_TEST.C: SDCARD_PIO=1344274432
SDCARD_TEST.C: SDCARD_PIO_SM=0
SDCARD_TEST.C: Let’s try to mount the SDcard
FF.C: inside f_mount about to call mount_volume
FF.C: about to call disk_initialize from within mount_volume
SDCARD.C: about to call init_spi() here, I think I was called by FF.C: mount_volume
SDCARD.C: we should be executing the PIO code here
SDCARD_TEST.C: SD card mount error: 3
SDCARD_TEST.C: sdcard_success=0
SDCARD_TEST.C: Bugger an error happened…again…

My second fault was the usage of the CMakeLists.txt file for setting some variable values.  This came back to bite me - see later*

I thought that maybe the issue was the references to spi1 or pio1 and they should be spi0 or pio0, I changed through all the combinations, nothing same error as always.

This is where I posted back to the PIMORONI support forum saying what I had tried and was anyone else the wiser....

...and then, a simple reply was made - I had been using the GPIO Pin values from the NES example and I was also putting those values in 2 places, one in main.h and the other in the CMakeLists.txt file - I noticed when I was compiling these messages shoot up the screen real fast:


and that was also where I found fault number three!  The sdcard library had it's own sdcard.h values setting the GPIO pins also - and these were the NES github versions.  Anyway, turns out that as the warning message says, it only uses one of the values - but this was getting messy as to what was actually being used.

In the end I just made them all the SAME values.

Then the PIMORONI Support forum post pointed it out for me - I was not using PIN 5 and 18 correctly! D'oh!  Right back at the start I was using the correct values and during the journey I had become influenced to change values and that might be the problem.

I changed the settings to be:

#define SDCARD_SPI_BUS spi1
#define SDCARD_PIN_SPI0_CS      22
#define SDCARD_PIN_SPI0_SCK    5
#define SDCARD_PIN_SPI0_MOSI    18
#define SDCARD_PIN_SPI0_MISO    19
#define SDCARD_PIO pio1
#define SDCARD_PIO_SM 0

Recompiled with:

cd build
rm -r *
cmake -D"PICO_BOARD=vgaboard" ..
make


Dragged the .uf2 file onto the PICO board..... and then waited and watched the tio output.

Oh yeah, I found a better monitoring tool called "tio", just start it with:

tio -m ODELBS /dev/ttyACM0

and then sit back and watch the output:

[tio 23:04:15] tio v1.32
[tio 23:04:15] Press ctrl-t q to quit
[tio 23:04:24] Connected

SDCARD_TEST.C: system init response=0
SDCARD_TEST.C: SDCARD_SPI_BUS=1074003968
SDCARD_TEST.C: SDCARD_PIN_SPI0_CS=22
SDCARD_TEST.C: SDCARD_PIN_SPI0_SCK=5
SDCARD_TEST.C: SDCARD_PIN_SPI0_MOSI=18
SDCARD_TEST.C: SDCARD_PIN_SPI0_MISO=19
SDCARD_TEST.C: SDCARD_PIO=1345323008
SDCARD_TEST.C: SDCARD_PIO_SM=0
SDCARD_TEST.C: Let's try to mount the SDcard
FF.C: inside f_mount about to call mount_volume
FF.C: about to call disk_initialize from within mount_volume
SDCARD.C: about to call init_spi() here, I think I was called by FF.C: mount_volume
SDCARD.C: we should be executing the PIO code here
SDCARD.C: pio_spi_init executed
SDCARD_TEST.C: Mounted OKAY!
SDCARD_TEST.C: Current directory is: /
SDCARD_TEST.C: sdcard_success=1
SDCARD_TEST.C: Woo!hoo! party time


and there we have it.  "Woo!hoo! party time"





As the DEBUG code output states, it drops through into the FF.C code and because I have the SDCARD library linked together this overrides the disk_initialize() and also because we have set the variable to use PIO instead of SPI, it executes the following code:


Note - we are executing the PIO_SPI_INIT() code and NOT the SPI_INIT() / SPI_SET_FORMAT() code and that is why the previous SDCard repo was not working as it was assuming we were using SPIO and this is not how we access this sdcard on this particular board.

...and obviously, the SDCARD_PIN_SPIO_* values have to be set correctly!  Once they have been - job done.  all working as it should,

Well, that was a fun 7-10days and a lot fo midnight, 1am, 2am, 3am... stubbornness - but got there in the end.

Hopefully, this C code will now get cleaned up and made publicly available via PIMORONI so other people can now benefit from this - I mean I didn't actually write the SPI/PIO code, that was done by previous genius people, I just leveraged what they did and made it work for the pico demo vga board.




Right... what was I doing this all again now?

Oh yeah - back to the MCUME emulator and to graft this code into that and get this working so it reads the sdcard on boot up and then get it to work with the VGA output too....

...or, just write my own emulator code app!  we'll see...


Comments