diff --git a/gpio/spi_demo/Hello-World.sal b/gpio/spi_demo/Hello-World.sal new file mode 100644 index 0000000..67954a5 Binary files /dev/null and b/gpio/spi_demo/Hello-World.sal differ diff --git a/gpio/spi_demo/README.md b/gpio/spi_demo/README.md new file mode 100644 index 0000000..f8810ef --- /dev/null +++ b/gpio/spi_demo/README.md @@ -0,0 +1,20 @@ +# SPI_DEMO + +This is Not intended to be a full demo, but a simple example of how to use the SPI interface on the Flipper Zero. + +Please see [https://youtu.be/W6bGYZ5PgIc](https://youtu.be/W6bGYZ5PgIc) for a video demo. + +My video this Saturday, Nov 18th 2023, will cover how to wire the device between the Flipper Zero and the chip that supports SPI, such as the W25Q32. + +The connections are: +- Flipper Pin 2 - MOSI (D0 on the W25Q32) +- Flipper Pin 3 - MISO (D1 on the W25Q32) +- Flipper Pin 4 - CS (CS on the W25Q32) +- Flipper Pin 5 - SCK (SLK on the W25Q32) +- Flipper Pin 8 - GND (GND on the W25Q32) +- Flipper Pin 9 - 3V3 (VCC on the W25Q32) + + +The `Hellow-World.sal` file can be viewed using [https://www.saleae.com/downloads/](https://www.saleae.com/downloads/) software. This is a capture of my W25Q32 chip being read by the Flipper Zero. The commands were sent using the `SPI Mem Manager` application. + +You can find the `SPI Mem Manager` application at [https://github.com/flipperdevices/flipperzero-good-faps/tree/dev/spi_mem_manager](https://github.com/flipperdevices/flipperzero-good-faps) \ No newline at end of file diff --git a/gpio/spi_demo/app.c b/gpio/spi_demo/app.c new file mode 100644 index 0000000..dd76cfd --- /dev/null +++ b/gpio/spi_demo/app.c @@ -0,0 +1,12 @@ +#include +#include + +void spi_demo(); +void spi_demo_v2(); + +int32_t main_learn_spi(void* _p) { + UNUSED(_p); + spi_demo(); + spi_demo_v2(); + return 0; +} \ No newline at end of file diff --git a/gpio/spi_demo/app.png b/gpio/spi_demo/app.png new file mode 100644 index 0000000..ac29149 Binary files /dev/null and b/gpio/spi_demo/app.png differ diff --git a/gpio/spi_demo/application.fam b/gpio/spi_demo/application.fam new file mode 100644 index 0000000..6f734c1 --- /dev/null +++ b/gpio/spi_demo/application.fam @@ -0,0 +1,14 @@ +App( + appid="learn_spi_app", + name="[WIP] Learn SPI", + apptype=FlipperAppType.EXTERNAL, + entry_point="main_learn_spi", + stack_size=4 * 1024, + requires=[ + "gui", + ], + order=10, + fap_icon="app.png", + fap_category="GPIO", + fap_description="Demo of communicating with W25Q32 IC.", +) diff --git a/gpio/spi_demo/spi.c b/gpio/spi_demo/spi.c new file mode 100644 index 0000000..db87531 --- /dev/null +++ b/gpio/spi_demo/spi.c @@ -0,0 +1,39 @@ +/* +Demo reading from a w25q32 chip using SPI. +*/ + +#include +#include + +#define TAG "LearnSPI-SPI" + +static uint32_t timeout = 1000; +static FuriHalSpiBusHandle* spi = &furi_hal_spi_bus_handle_external; + +void read_w25q32_spi() { + uint8_t command_read_memory[10] = {0x03, 0x0, 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; + uint8_t data_response[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Extra space for NULL termination + + furi_hal_spi_acquire(spi); + + if(furi_hal_spi_bus_trx(spi, command_read_memory, data_response, 9, timeout)) { + char* data_response_str = (char*)data_response + 4; + FURI_LOG_E( + TAG, + "MESSAGE STORED IN CHIP AT ADDRESS 0x%02X%02X%02X IS: %s", + command_read_memory[1], + command_read_memory[2], + command_read_memory[3], + data_response_str); + } else { + FURI_LOG_E(TAG, "FAILED - read_id_spi failed."); + } + + furi_hal_spi_release(spi); +} + +void spi_demo() { + furi_hal_spi_bus_handle_init(spi); + read_w25q32_spi(); + furi_hal_spi_bus_handle_deinit(spi); +} diff --git a/gpio/spi_demo/spi_v2.c b/gpio/spi_demo/spi_v2.c new file mode 100644 index 0000000..da1ad39 --- /dev/null +++ b/gpio/spi_demo/spi_v2.c @@ -0,0 +1,42 @@ +/* +Improved demo reading from a w25q32 chip using SPI. +*/ + +#include +#include + +#define TAG "LearnSPI-SPI-V2" + +static uint32_t timeout = 1000; +static FuriHalSpiBusHandle* spi = &furi_hal_spi_bus_handle_external; + +void read_w25q32_spi_v2() { + uint8_t command_read_memory[1] = {0x03}; // Read command + uint8_t read_memory_address[3] = {0x00, 0x01, 0x00}; // Memory address is 0x000100 + uint8_t data_response[6] = {0, 0, 0, 0, 0, 0}; // Read 5 bytes + NULL terminator. + + furi_hal_spi_acquire(spi); + + if(furi_hal_spi_bus_tx(spi, command_read_memory, 1, timeout) && + furi_hal_spi_bus_tx(spi, read_memory_address, 3, timeout) && + (furi_hal_spi_bus_rx(spi, data_response, 5, timeout))) { + char* data_response_str = (char*)data_response; + FURI_LOG_E( + TAG, + "MESSAGE STORED IN CHIP AT ADDRESS 0x%02X%02X%02X IS: %s", + read_memory_address[0], + read_memory_address[1], + read_memory_address[2], + data_response_str); + } else { + FURI_LOG_E(TAG, "FAILED - read_id_spi failed."); + } + + furi_hal_spi_release(spi); +} + +void spi_demo_v2() { + furi_hal_spi_bus_handle_init(spi); + read_w25q32_spi_v2(); + furi_hal_spi_bus_handle_deinit(spi); +}