Initial Release

This commit is contained in:
graham sanderson
2021-01-20 10:49:34 -06:00
commit 46078742c7
245 changed files with 21157 additions and 0 deletions

6
spi/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
if (NOT PICO_NO_HARDWARE)
add_subdirectory(spi_flash)
add_subdirectory(mpu9250_spi)
add_subdirectory(bme280_spi)
add_subdirectory(spi_dma)
endif ()

View File

@@ -0,0 +1,12 @@
add_executable(bme280_spi
bme280_spi.c
)
# Pull in our (to be renamed) simple get you started dependencies
target_link_libraries(bme280_spi pico_stdlib hardware_spi)
# create map/bin/hex file etc.
pico_add_extra_outputs(bme280_spi)
# add url via pico_set_program_url
example_auto_set_url(bme280_spi)

View File

@@ -0,0 +1,50 @@
= Attaching a BME280 temperature/humidity/pressure sensor via SPI
This example code shows how to interface the Raspberry Pi Pico to a BME280 temperature/humidity/pressure. The particular device used can be interfaced via I2C or SPI, we are using SPI, and interfacing at 3.3v.
This examples reads the data from the sensor, and runs it through the appropriate compensation routines (see the chip datasheet for details https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf). At startup the compensation parameters required by the compensation routines are read from the chip.
)
== Wiring information
Wiring up the device requires 6 jumpers as follows:
* GPIO 16 (pin 21) MISO/spi0_rx-> SDO/SDO on bme280 board
* GPIO 17 (pin 22) Chip select -> CSB/!CS on bme280 board
* GPIO 18 (pin 24) SCK/spi0_sclk -> SCL/SCK on bme280 board
* GPIO 19 (pin 25) MOSI/spi0_tx -> SDA/SDI on bme280 board
* 3.3v (pin 3;6) -> VCC on bme280 board
* GND (pin 38) -> GND on bme280 board
The example here uses SPI port 0. Power is supplied from the 3.3V pin.
[NOTE]
======
There are many different manufacturers who sell boards with the BME280. Whilst they all appear slightly different, they all have, at least, the same 6 pins required to power and communicate. When wiring up a board that is different to the one in the diagram, ensure you connect up as described in the previous paragraph.
======
[[BME280_spi_wiring]]
[pdfwidth=75%]
.Wiring Diagram for bme280.
image::bme280_spi_bb.png[]
== List of Files
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
bme280_spi.c:: The example code.
== Bill of Materials
.A list of materials required for the example
[[BME280-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
| BME280 board| 1 | generic part
| M/M Jumper wires | 6 | generic part
|===

233
spi/bme280_spi/bme280_spi.c Normal file
View File

@@ -0,0 +1,233 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
/* Example code to talk to a bme280 humidity/temperature/pressure sensor.
NOTE: Ensure the device is capable of being driven at 3.3v NOT 5v. The Pico
GPIO (and therefor SPI) cannot be used at 5v.
You will need to use a level shifter on the SPI lines if you want to run the
board at 5v.
Connections on Raspberry Pi Pico board and a generic bme280 board, other
boards may vary.
GPIO 16 (pin 21) MISO/spi0_rx-> SDO/SDO on bme280 board
GPIO 17 (pin 22) Chip select -> CSB/!CS on bme280 board
GPIO 18 (pin 24) SCK/spi0_sclk -> SCL/SCK on bme280 board
GPIO 19 (pin 25) MOSI/spi0_tx -> SDA/SDI on bme280 board
3.3v (pin 3;6) -> VCC on bme280 board
GND (pin 38) -> GND on bme280 board
Note: SPI devices can have a number of different naming schemes for pins. See
the Wikipedia page at https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
for variations.
This code uses a bunch of register definitions, and some compensation code derived
from the Bosch datasheet which can be found here.
https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
*/
#define PIN_MISO 16
#define PIN_CS 17
#define PIN_SCK 18
#define PIN_MOSI 19
#define SPI_PORT spi0
#define READ_BIT 0x80
int32_t t_fine;
uint16_t dig_T1;
int16_t dig_T2, dig_T3;
uint16_t dig_P1;
int16_t dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
uint8_t dig_H1, dig_H3;
int8_t dig_H6;
int16_t dig_H2, dig_H4, dig_H5;
/* The following compensation functions are required to convert from the raw ADC
data from the chip to something usable. Each chip has a different set of
compensation parameters stored on the chip at point of manufacture, which are
read from the chip at startup and used inthese routines.
*/
int32_t compensate_temp(int32_t adc_T) {
int32_t var1, var2, T;
var1 = ((((adc_T >> 3) - ((int32_t) dig_T1 << 1))) * ((int32_t) dig_T2)) >> 11;
var2 = (((((adc_T >> 4) - ((int32_t) dig_T1)) * ((adc_T >> 4) - ((int32_t) dig_T1))) >> 12) * ((int32_t) dig_T3))
>> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}
uint32_t compensate_pressure(int32_t adc_P) {
int32_t var1, var2;
uint32_t p;
var1 = (((int32_t) t_fine) >> 1) - (int32_t) 64000;
var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * ((int32_t) dig_P6);
var2 = var2 + ((var1 * ((int32_t) dig_P5)) << 1);
var2 = (var2 >> 2) + (((int32_t) dig_P4) << 16);
var1 = (((dig_P3 * (((var1 >> 2) * (var1 >> 2)) >> 13)) >> 3) + ((((int32_t) dig_P2) * var1) >> 1)) >> 18;
var1 = ((((32768 + var1)) * ((int32_t) dig_P1)) >> 15);
if (var1 == 0)
return 0;
p = (((uint32_t) (((int32_t) 1048576) - adc_P) - (var2 >> 12))) * 3125;
if (p < 0x80000000)
p = (p << 1) / ((uint32_t) var1);
else
p = (p / (uint32_t) var1) * 2;
var1 = (((int32_t) dig_P9) * ((int32_t) (((p >> 3) * (p >> 3)) >> 13))) >> 12;
var2 = (((int32_t) (p >> 2)) * ((int32_t) dig_P8)) >> 13;
p = (uint32_t) ((int32_t) p + ((var1 + var2 + dig_P7) >> 4));
return p;
}
uint32_t compensate_humidity(int32_t adc_H) {
int32_t v_x1_u32r;
v_x1_u32r = (t_fine - ((int32_t) 76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t) dig_H4) << 20) - (((int32_t) dig_H5) * v_x1_u32r)) +
((int32_t) 16384)) >> 15) * (((((((v_x1_u32r * ((int32_t) dig_H6)) >> 10) * (((v_x1_u32r *
((int32_t) dig_H3))
>> 11) + ((int32_t) 32768))) >> 10) + ((int32_t) 2097152)) *
((int32_t) dig_H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t) dig_H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
return (uint32_t) (v_x1_u32r >> 12);
}
static inline void cs_select() {
asm volatile("nop \n nop \n nop");
gpio_put(PIN_CS, 0); // Active low
asm volatile("nop \n nop \n nop");
}
static inline void cs_deselect() {
asm volatile("nop \n nop \n nop");
gpio_put(PIN_CS, 1);
asm volatile("nop \n nop \n nop");
}
static void write_register(uint8_t reg, uint8_t data) {
uint8_t buf[2];
buf[0] = reg & 0x7f; // remove read bit as this is a write
buf[1] = data;
cs_select();
spi_write_blocking(SPI_PORT, buf, 2);
cs_deselect();
sleep_ms(10);
}
static void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) {
// For this particular device, we send the device the register we want to read
// first, then subsequently read from the device. The register is auto incrementing
// so we don't need to keep sending the register we want, just the first.
reg |= READ_BIT;
cs_select();
spi_write_blocking(SPI_PORT, &reg, 1);
sleep_ms(10);
spi_read_blocking(SPI_PORT, 0, buf, len);
cs_deselect();
sleep_ms(10);
}
/* This function reads the manufacturing assigned compensation parameters from the device */
void read_compensation_parameters() {
uint8_t buffer[26];
read_registers(0x88, buffer, 24);
dig_T1 = buffer[0] | (buffer[1] << 8);
dig_T2 = buffer[2] | (buffer[3] << 8);
dig_T3 = buffer[4] | (buffer[5] << 8);
dig_P1 = buffer[6] | (buffer[7] << 8);
dig_P2 = buffer[8] | (buffer[9] << 8);
dig_P3 = buffer[10] | (buffer[11] << 8);
dig_P4 = buffer[12] | (buffer[13] << 8);
dig_P5 = buffer[14] | (buffer[15] << 8);
dig_P6 = buffer[16] | (buffer[17] << 8);
dig_P7 = buffer[18] | (buffer[19] << 8);
dig_P8 = buffer[20] | (buffer[21] << 8);
dig_P9 = buffer[22] | (buffer[23] << 8);
dig_H1 = buffer[25];
read_registers(0xE1, buffer, 8);
dig_H2 = buffer[0] | (buffer[1] << 8);
dig_H3 = (int8_t) buffer[2];
dig_H4 = buffer[3] << 4 | (buffer[4] & 0xf);
dig_H5 = (buffer[5] >> 4) | (buffer[6] << 4);
dig_H6 = (int8_t) buffer[7];
}
static void bme280_read_raw(int32_t *humidity, int32_t *pressure, int32_t *temperature) {
uint8_t buffer[8];
read_registers(0xF7, buffer, 8);
*pressure = ((uint32_t) buffer[0] << 12) | ((uint32_t) buffer[1] << 4) | (buffer[2] >> 4);
*temperature = ((uint32_t) buffer[3] << 12) | ((uint32_t) buffer[4] << 4) | (buffer[5] >> 4);
*humidity = (uint32_t) buffer[6] << 8 | buffer[7];
}
int main() {
stdio_init_all();
printf("Hello, bme280! Reading raw data from registers via SPI...\n");
// This example will use SPI0 at 0.5MHz.
spi_init(SPI_PORT, 500 * 1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Chip select is active-low, so we'll initialise it to a driven-high state
gpio_init(PIN_CS);
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1);
// See if SPI is working - interrograte the device for its I2C ID number, should be 0x60
uint8_t id;
read_registers(0xD0, &id, 1);
printf("Chip ID is 0x%x\n", id);
read_compensation_parameters();
write_register(0xF2, 0x1); // Humidity oversampling register - going for x1
write_register(0xF4, 0x27);// Set rest of oversampling modes and run mode to normal
int32_t humidity, pressure, temperature;
while (1) {
bme280_read_raw(&humidity, &pressure, &temperature);
// These are the raw numbers from the chip, so we need to run through the
// compensations to get human understandable numbers
pressure = compensate_pressure(pressure);
temperature = compensate_temp(temperature);
humidity = compensate_humidity(humidity);
printf("Humidity = %.2f%%\n", humidity / 1024.0);
printf("Pressure = %dPa\n", pressure);
printf("Temp. = %.2fC\n", temperature / 100.0);
sleep_ms(1000);
}
return 0;
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 KiB

View File

@@ -0,0 +1,12 @@
add_executable(mpu9250_spi
mpu9250_spi.c
)
# Pull in our (to be renamed) simple get you started dependencies
target_link_libraries(mpu9250_spi pico_stdlib hardware_spi)
# create map/bin/hex file etc.
pico_add_extra_outputs(mpu9250_spi)
# add url via pico_set_program_url
example_auto_set_url(mpu9250_spi)

View File

@@ -0,0 +1,52 @@
= Attaching a MPU9250 accelerometer/gyroscope via SPI
This example code shows how to interface the Raspberry Pi Pico to the MPU9250 accelerometer/gyroscope board. The particular device used can be interfaced via I2C or SPI, we are using SPI, and interfacing at 3.3v.
[NOTE]
======
This is a very basic example, and only recovers raw data from the sensor. There are various calibration options available that should be used to ensure that the final results are accurate. It is also possible to wire up the interrupt pin to a GPIO and read data only when it is ready, rather than using the polling approach in the example.
======
== Wiring information
Wiring up the device requires 6 jumpers as follows:
* GPIO 4 (pin 6) MISO/spi0_rx-> ADO on MPU9250 board
* GPIO 5 (pin 7) Chip select -> NCS on MPU9250 board
* GPIO 6 (pin 9) SCK/spi0_sclk -> SCL on MPU9250 board
* GPIO 7 (pin 10) MOSI/spi0_tx -> SDA on MPU9250 board
* 3.3v (pin 36) -> VCC on MPU9250 board
* GND (pin 38) -> GND on MPU9250 board
The example here uses SPI port 0. Power is supplied from the 3.3V pin.
[NOTE]
======
There are many different manufacturers who sell boards with the MPU9250. Whilst they all appear slightly different, they all have, at least, the same 6 pins required to power and communicate. When wiring up a board that is different to the one in the diagram, ensure you connect up as described in the previous paragraph.
======
[[mpu9250_spi_wiring]]
[pdfwidth=75%]
.Wiring Diagram for MPU9250.
image::mpu9250_spi_bb.png[]
== List of Files
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
mpu9250_spi.c:: The example code.
== Bill of Materials
.A list of materials required for the example
[[MPU9250-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
| MPU9250 board| 1 | generic part
| M/M Jumper wires | 6 | generic part
|===

View File

@@ -0,0 +1,150 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
/* Example code to talk to a MPU9250 MEMS accelerometer and gyroscope.
Ignores the magnetometer, that is left as a exercise for the reader.
This is taking to simple approach of simply reading registers. It's perfectly
possible to link up an interrupt line and set things up to read from the
inbuilt FIFO to make it more useful.
NOTE: Ensure the device is capable of being driven at 3.3v NOT 5v. The Pico
GPIO (and therefor SPI) cannot be used at 5v.
You will need to use a level shifter on the I2C lines if you want to run the
board at 5v.
Connections on Raspberry Pi Pico board and a generic MPU9250 board, other
boards may vary.
GPIO 4 (pin 6) MISO/spi0_rx-> ADO on MPU9250 board
GPIO 5 (pin 7) Chip select -> NCS on MPU9250 board
GPIO 6 (pin 9) SCK/spi0_sclk -> SCL on MPU9250 board
GPIO 7 (pin 10) MOSI/spi0_tx -> SDA on MPU9250 board
3.3v (pin 36) -> VCC on MPU9250 board
GND (pin 38) -> GND on MPU9250 board
Note: SPI devices can have a number of different naming schemes for pins. See
the Wikipedia page at https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
for variations.
The particular device used here uses the same pins for I2C and SPI, hence the
using of I2C names
*/
#define PIN_MISO 4
#define PIN_CS 5
#define PIN_SCK 6
#define PIN_MOSI 7
#define SPI_PORT spi0
#define READ_BIT 0x80
static inline void cs_select() {
asm volatile("nop \n nop \n nop");
gpio_put(PIN_CS, 0); // Active low
asm volatile("nop \n nop \n nop");
}
static inline void cs_deselect() {
asm volatile("nop \n nop \n nop");
gpio_put(PIN_CS, 1);
asm volatile("nop \n nop \n nop");
}
static void mpu9250_reset() {
// Two byte reset. First byte register, second byte data
// There are a load more options to set up the device in different ways that could be added here
uint8_t buf[] = {0x6B, 0x00};
cs_select();
spi_write_blocking(SPI_PORT, buf, 2);
cs_deselect();
}
static void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) {
// For this particular device, we send the device the register we want to read
// first, then subsequently read from the device. The register is auto incrementing
// so we don't need to keep sending the register we want, just the first.
reg |= READ_BIT;
cs_select();
spi_write_blocking(SPI_PORT, &reg, 1);
sleep_ms(10);
spi_read_blocking(SPI_PORT, 0, buf, len);
cs_deselect();
sleep_ms(10);
}
static void mpu9250_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
uint8_t buffer[6];
// Start reading acceleration registers from register 0x3B for 6 bytes
read_registers(0x3B, buffer, 6);
for (int i = 0; i < 3; i++) {
accel[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);
}
// Now gyro data from reg 0x43 for 6 bytes
read_registers(0x43, buffer, 6);
for (int i = 0; i < 3; i++) {
gyro[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);;
}
// Now temperature from reg 0x41 for 2 bytes
read_registers(0x41, buffer, 2);
*temp = buffer[0] << 8 | buffer[1];
}
int main() {
stdio_init_all();
printf("Hello, MPU9250! Reading raw data from registers via SPI...\n");
// This example will use SPI0 at 0.5MHz.
spi_init(SPI_PORT, 500 * 1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Chip select is active-low, so we'll initialise it to a driven-high state
gpio_init(PIN_CS);
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1);
mpu9250_reset();
// See if SPI is working - interrograte the device for its I2C ID number, should be 0x71
uint8_t id;
read_registers(0x75, &id, 1);
printf("I2C address is 0x%x\n", id);
int16_t acceleration[3], gyro[3], temp;
while (1) {
mpu9250_read_raw(acceleration, gyro, &temp);
// These are the raw numbers from the chip, so will need tweaking to be really useful.
// See the datasheet for more information
printf("Acc. X = %d, Y = %d, Z = %d\n", acceleration[0], acceleration[1], acceleration[2]);
printf("Gyro. X = %d, Y = %d, Z = %d\n", gyro[0], gyro[1], gyro[2]);
// Temperature is simple so use the datasheet calculation to get deg C.
// Note this is chip temperature.
printf("Temp. = %f\n", (temp / 340.0) + 36.53);
sleep_ms(100);
}
return 0;
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

View File

@@ -0,0 +1,11 @@
add_executable(spi_dma
spi_dma.c
)
target_link_libraries(spi_dma pico_stdlib hardware_spi hardware_dma)
# create map/bin/hex file etc.
pico_add_extra_outputs(spi_dma)
# add url via pico_set_program_url
example_auto_set_url(spi_dma)

103
spi/spi_dma/spi_dma.c Normal file
View File

@@ -0,0 +1,103 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// Example of writing via DMA to the SPI interface and similarly reading it back via a loopback.
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "hardware/dma.h"
#define PIN_MISO 16
#define PIN_CS 17
#define PIN_SCK 18
#define PIN_MOSI 19
#define SPI_INST spi0
#define TEST_SIZE 1024
int main() {
// Enable UART so we can print status output
stdio_init_all();
printf("SPI DMA example\n");
// Enable SPI at 1 MHz and connect to GPIOs
spi_init(SPI_INST, 1000 * 1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_init(PIN_CS);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Grab some unused dma channels
const uint dma_tx = dma_claim_unused_channel(true);
const uint dma_rx = dma_claim_unused_channel(true);
// Force loopback for testing (I don't have an SPI device handy)
hw_set_bits(&spi_get_hw(SPI_INST)->cr1, SPI_SSPCR1_LBM_BITS);
static uint8_t txbuf[TEST_SIZE];
static uint8_t rxbuf[TEST_SIZE];
for (uint i = 0; i < TEST_SIZE; ++i) {
txbuf[i] = rand();
}
// We set the outbound DMA to transfer from a memory buffer to the SPI transmit FIFO paced by the SPI TX FIFO DREQ
// The default is for the read address to increment every element (in this case 1 byte - DMA_SIZE_8)
// and for the write address to remain unchanged.
printf("Configure TX DMA\n");
dma_channel_config c = dma_channel_get_default_config(dma_tx);
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_dreq(&c, spi_get_index(SPI_INST) ? DREQ_SPI1_TX : DREQ_SPI0_TX);
dma_channel_configure(dma_tx, &c,
&spi_get_hw(SPI_INST)->dr, // write address
txbuf, // read address
TEST_SIZE, // element count (each element is of size transfer_data_size)
false); // don't start yet
printf("Configure RX DMA\n");
// We set the inbound DMA to transfer from the SPI receive FIFO to a memory buffer paced by the SPI RX FIFO DREQ
// We coinfigure the read address to remain unchanged for each element, but the write
// address to increment (so data is written throughout the buffer)
c = dma_channel_get_default_config(dma_rx);
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_dreq(&c, spi_get_index(SPI_INST) ? DREQ_SPI1_RX : DREQ_SPI0_RX);
channel_config_set_read_increment(&c, false);
channel_config_set_write_increment(&c, true);
dma_channel_configure(dma_rx, &c,
rxbuf, // write address
&spi_get_hw(SPI_INST)->dr, // read address
TEST_SIZE, // element count (each element is of size transfer_data_size)
false); // don't start yet
printf("Starting DMAs...\n");
// start them exactly simultaneously to avoid races (in extreme cases the FIFO could overflow)
dma_start_channel_mask((1u << dma_tx) | (1u << dma_rx));
printf("Wait for RX complete...\n");
dma_channel_wait_for_finish_blocking(dma_rx);
if (dma_channel_is_busy(dma_tx)) {
panic("RX completed before TX");
}
printf("Done. Checking...");
for (uint i = 0; i < TEST_SIZE; ++i) {
if (rxbuf[i] != txbuf[i]) {
panic("Mismatch at %d/%d: expected %02x, got %02x",
i, TEST_SIZE, txbuf[i], rxbuf[i]
);
}
}
printf("All good\n");
dma_channel_unclaim(dma_tx);
dma_channel_unclaim(dma_rx);
return 0;
}

View File

@@ -0,0 +1,12 @@
add_executable(spi_flash
spi_flash.c
)
# Pull in basic dependencies
target_link_libraries(spi_flash pico_stdlib hardware_spi)
# create map/bin/hex file etc.
pico_add_extra_outputs(spi_flash)
# add url via pico_set_program_url
example_auto_set_url(spi_flash)

154
spi/spi_flash/spi_flash.c Normal file
View File

@@ -0,0 +1,154 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// Example of reading/writing an external serial flash using the PL022 SPI interface
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
#define FLASH_PAGE_SIZE 256
#define FLASH_SECTOR_SIZE 4096
#define FLASH_CMD_PAGE_PROGRAM 0x02
#define FLASH_CMD_READ 0x03
#define FLASH_CMD_STATUS 0x05
#define FLASH_CMD_WRITE_EN 0x06
#define FLASH_CMD_SECTOR_ERASE 0x20
#define FLASH_STATUS_BUSY_MASK 0x01
#define PIN_MISO 4
#define PIN_CS 5
#define PIN_SCK 6
#define PIN_MOSI 7
static inline void cs_select(uint cs_pin) {
asm volatile("nop \n nop \n nop"); // FIXME
gpio_put(cs_pin, 0);
asm volatile("nop \n nop \n nop"); // FIXME
}
static inline void cs_deselect(uint cs_pin) {
asm volatile("nop \n nop \n nop"); // FIXME
gpio_put(cs_pin, 1);
asm volatile("nop \n nop \n nop"); // FIXME
}
void __not_in_flash_func(flash_read)(spi_inst_t *spi, uint cs_pin, uint32_t addr, uint8_t *buf, size_t len) {
cs_select(cs_pin);
uint8_t cmdbuf[4] = {
FLASH_CMD_READ,
addr >> 16,
addr >> 8,
addr
};
spi_write_blocking(spi, cmdbuf, 4);
spi_read_blocking(spi, 0, buf, len);
cs_deselect(cs_pin);
}
void __not_in_flash_func(flash_write_enable)(spi_inst_t *spi, uint cs_pin) {
cs_select(cs_pin);
uint8_t cmd = FLASH_CMD_WRITE_EN;
spi_write_blocking(spi, &cmd, 1);
cs_deselect(cs_pin);
}
void __not_in_flash_func(flash_wait_done)(spi_inst_t *spi, uint cs_pin) {
uint8_t status;
do {
cs_select(cs_pin);
uint8_t buf[2] = {FLASH_CMD_STATUS, 0};
spi_write_read_blocking(spi, buf, buf, 2);
cs_deselect(cs_pin);
status = buf[1];
} while (status & FLASH_STATUS_BUSY_MASK);
}
void __not_in_flash_func(flash_sector_erase)(spi_inst_t *spi, uint cs_pin, uint32_t addr) {
uint8_t cmdbuf[4] = {
FLASH_CMD_SECTOR_ERASE,
addr >> 16,
addr >> 8,
addr
};
flash_write_enable(spi, cs_pin);
cs_select(cs_pin);
spi_write_blocking(spi, cmdbuf, 4);
cs_deselect(cs_pin);
flash_wait_done(spi, cs_pin);
}
void __not_in_flash_func(flash_page_program)(spi_inst_t *spi, uint cs_pin, uint32_t addr, uint8_t data[]) {
uint8_t cmdbuf[4] = {
FLASH_CMD_PAGE_PROGRAM,
addr >> 16,
addr >> 8,
addr
};
flash_write_enable(spi, cs_pin);
cs_select(cs_pin);
spi_write_blocking(spi, cmdbuf, 4);
spi_write_blocking(spi, data, FLASH_PAGE_SIZE);
cs_deselect(cs_pin);
flash_wait_done(spi, cs_pin);
}
void printbuf(uint8_t buf[FLASH_PAGE_SIZE]) {
for (int i = 0; i < FLASH_PAGE_SIZE; ++i) {
if (i % 16 == 15)
printf("%02x\n", buf[i]);
else
printf("%02x ", buf[i]);
}
}
int main() {
// Enable UART so we can print status output
stdio_init_all();
printf("SPI flash example\n");
// Enable SPI 0 at 1 MHz and connect to GPIOs
spi_init(spi0, 1000 * 1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Chip select is active-low, so we'll initialise it to a driven-high state
gpio_init(PIN_CS);
gpio_put(PIN_CS, 1);
gpio_set_dir(PIN_CS, GPIO_OUT);
printf("SPI initialised, let's goooooo\n");
uint8_t page_buf[FLASH_PAGE_SIZE];
const uint32_t target_addr = 0;
flash_sector_erase(spi0, PIN_CS, target_addr);
flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE);
printf("After erase:\n");
printbuf(page_buf);
for (int i = 0; i < FLASH_PAGE_SIZE; ++i)
page_buf[i] = i;
flash_page_program(spi0, PIN_CS, target_addr, page_buf);
flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE);
printf("After program:\n");
printbuf(page_buf);
flash_sector_erase(spi0, PIN_CS, target_addr);
flash_read(spi0, PIN_CS, target_addr, page_buf, FLASH_PAGE_SIZE);
printf("Erase again:\n");
printbuf(page_buf);
return 0;
}