Let's test the #NuttX SPI Driver for #BL602
https://nuttx.apache.org/docs/latest/components/drivers/special/spi.html
We create the "spi_test" Demo App in #NuttX ... By copying the "hello" Demo App
https://github.com/lupyuen/incubator-nuttx-apps/commit/9af4ad6cab225d333ce0dae98c65a2a48621b3b4
Fixing our "spi_test" app for #NuttX ... Rename "hello_main.c" to "spi_test_main.c"
https://github.com/lupyuen/incubator-nuttx-apps/commit/a4f884c67dc4c1042831d0554aed1d55a0e28b40
In our #NuttX App "spi_test", change all "hello" to "spi_test" ... Remember to Preserve Case!
https://github.com/lupyuen/incubator-nuttx-apps/commit/0e19613b3059882f002eee948c0a79f622eccb74
1️⃣ make distclean 2️⃣ configure.sh 3️⃣ make menuconfig ... Our #NuttX App "spi_test" magically appears!
https://github.com/lupyuen/incubator-nuttx-apps/tree/newapp/examples/spi_test
Our #NuttX Demo App "spi_test" ... Runs OK on #BL602
https://github.com/lupyuen/incubator-nuttx-apps/blob/newapp/examples/spi_test/spi_test_main.c
#NuttX SPI Interface is defined here ... Let's call it from our "spi_test" app
https://github.com/apache/incubator-nuttx/blob/master/include/nuttx/spi/spi.h
Build, Flash and Run #NuttX OS on #BL602 ... Here's the script I use for macOS
https://gist.github.com/lupyuen/cc21385ecc66b5c02d15affd776a64af
Can our #NuttX App directly call the SPI Interface? Let's find out! 🤔
https://github.com/lupyuen/incubator-nuttx-apps/blob/spi_test/examples/spi_test/spi_test_main.c
#NuttX SPI Interface needs an SPI Device "spi_dev_s" ... How do we get an SPI Device? 🤔
https://github.com/lupyuen/incubator-nuttx/blob/master/arch/risc-v/src/bl602/bl602_spi.c#L932-L967
Tracing thru #NuttX Virtual File System ... We see that ioctl() maps the File Descriptor to a File Struct
https://github.com/lupyuen/incubator-nuttx/blob/master/fs/vfs/fs_ioctl.c#L118-L138
#NuttX File Struct contains a Private Pointer to the SPI Driver "spi_driver_s"
https://github.com/lupyuen/incubator-nuttx/blob/master/drivers/spi/spi_driver.c#L112-L147
Instead we copy an existing #NuttX SPI Device Driver to test the SPI Interface ... We pick the simplest smallest SPI Device Driver: dat-31r5-sp
https://docs.google.com/spreadsheets/d/1MDps5cPe7tIgCL1Cz98iVccJAUJq1lgctpKgg9OwztI/edit#gid=0
In our SPI Test Driver: Change all "dat31r5sp" to "spi_test_driver" ... Remember to Preserve Case!
https://github.com/lupyuen/incubator-nuttx/commit/8fee69215163180b77dc9d5b9e7449ebe00ac1cc
Do the same to create the Header File for our #NuttX Driver: spi_test_driver.h
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/include/nuttx/rf/spi_test_driver.h
Add our SPI Test Driver to #NuttX Kconfig
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/drivers/rf/Kconfig#L22-L27
Our SPI Test Driver for #NuttX appears in "make menuconfig"!
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/drivers/rf/Kconfig#L22-L27
Remember to enable "SPI0" and "SPI Character Driver" in #NuttX ... Or our SPI Test Driver won't start
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/drivers/rf/spi_test_driver.c
Here's what happens when we make a boo-boo and #NuttX won't start
https://gist.github.com/lupyuen/ccfd90125f9a180b4cfb459e8a57b323
Update the Makefile "Make.defs" ... So that #NuttX will build our SPI Test Driver
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/drivers/rf/Make.defs#L33-L37
Build, flash and run #NuttX ... Our SPI Test Driver appears as "/dev/spitest0"! 🎉
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/drivers/rf/spi_test_driver.c
Back to our #NuttX SPI Test App ... Here's how we open the SPI Test Driver and write data
https://github.com/lupyuen/incubator-nuttx-apps/blob/spi_test/examples/spi_test/spi_test_main.c
This appears when we run our #NuttX SPI Test App ... Let's study our SPI Test Driver
https://github.com/lupyuen/incubator-nuttx-apps/blob/spi_test/examples/spi_test/spi_test_main.c
Every #NuttX Device Driver defines the File Operations for the device ... Here are the open(), close(), read(), write() and ioctl() operations for our SPI Test Driver
https://github.com/lupyuen/incubator-nuttx/blob/spi_test/drivers/rf/spi_test_driver.c#L80-L89
In the write() operation for our #NuttX SPI Test Driver, we 1️⃣ Lock the SPI Bus 2️⃣ Config the SPI Interface 3️⃣ Select the SPI Device 4️⃣ Transfer SPI Data 5️⃣ Deselect and Unlock
https://github.com/lupyuen/incubator-nuttx/blob/spi_test/drivers/rf/spi_test_driver.c#L182-L239
Here's how we configure the #NuttX SPI Interface
https://github.com/lupyuen/incubator-nuttx/blob/spi_test/drivers/rf/spi_test_driver.c#L95-L117
To watch what happens inside #NuttX's SPI Driver for #BL602 ... Turn on SPI Debug Logging
https://github.com/lupyuen/incubator-nuttx/blob/spi_test/arch/risc-v/src/bl602/bl602_spi.c
Now we see every byte transferred by #NuttX's SPI Driver for #BL602!
https://github.com/lupyuen/incubator-nuttx/blob/spi_test/drivers/rf/spi_test_driver.c
How to verify the #NuttX SPI Output? We sniff the #BL602 SPI Bus with a Logic Analyser
https://lupyuen.github.io/articles/spi#appendix-troubleshoot-bl602-spi-with-logic-analyser
@lupyuen -- If I'm understanding this code correctly, then it is very clean and straight forward. Good job!!! 👍
@TikToc Thanks! 🙂
We create a new #NuttX SPI Device Driver ... By copying "dat-31r5-sp.c" to "spi_test_driver.c"
https://github.com/lupyuen/incubator-nuttx/blob/newdriver/drivers/rf/spi_test_driver.c