Skip to content

Commit

Permalink
SPI demo w/W25Q32 chip
Browse files Browse the repository at this point in the history
  • Loading branch information
jamisonderek committed Nov 15, 2023
1 parent 3d0aed8 commit 6934ea5
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
Binary file added gpio/spi_demo/Hello-World.sal
Binary file not shown.
20 changes: 20 additions & 0 deletions gpio/spi_demo/README.md
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions gpio/spi_demo/app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <furi.h>
#include <furi_hal.h>

void spi_demo();
void spi_demo_v2();

int32_t main_learn_spi(void* _p) {
UNUSED(_p);
spi_demo();
spi_demo_v2();
return 0;
}
Binary file added gpio/spi_demo/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions gpio/spi_demo/application.fam
Original file line number Diff line number Diff line change
@@ -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.",
)
39 changes: 39 additions & 0 deletions gpio/spi_demo/spi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Demo reading from a w25q32 chip using SPI.
*/

#include <furi.h>
#include <furi_hal.h>

#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);
}
42 changes: 42 additions & 0 deletions gpio/spi_demo/spi_v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Improved demo reading from a w25q32 chip using SPI.
*/

#include <furi.h>
#include <furi_hal.h>

#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);
}

0 comments on commit 6934ea5

Please sign in to comment.