Skip to content

Commit

Permalink
Merge pull request #482 from paulscottrobson/sdfix
Browse files Browse the repository at this point in the history
Fix SD Card / SPI not working
  • Loading branch information
paulscottrobson authored May 2, 2024
2 parents 396f0b9 + 74dab4f commit 31eb950
Showing 1 changed file with 63 additions and 10 deletions.
73 changes: 63 additions & 10 deletions firmware/sources/hardware/ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@

#include "common.h"

// ***************************************************************************************
//
// GPIO Pins
//
// ***************************************************************************************

#define I2C_SCL_PIN (23)
#define I2C_SDA_PIN (22)
#define I2C_DEVICE (i2c1)

#define SPI_MISO_PIN (24)
#define SPI_MOSI_PIN (27)
#define SPI_SCK_PIN (26)
#define SPI_CS_PIN (25)

#define SPI_DEVICE (spi1)

// ***************************************************************************************
//
// Check if SPI in use, or SPI pin in use
//
// ***************************************************************************************

#if STORAGE_TYPE==SDCARD
//
// SD Card ; don't use the SPI pins or SPI function
//
#define INUSE(p) ((p) == SPI_MISO_PIN || (p) == SPI_MOSI_PIN || (p) == SPI_SCK_PIN || (p) == SPI_CS_PIN)
#define SPI_BLOCKED
#else
//
// USB Storage
//
#define INUSE(p) (false)
#endif

// ***************************************************************************************
//
// Set GPIO Direction
Expand All @@ -21,6 +57,9 @@
static bool isADCInitialised = false;

int UEXTSetGPIODirection(int gpio,int pinType) {

if (INUSE(gpio)) return 1;

gpio_init(gpio);
if (pinType == UEXT_ANALOGUE) {
if (!isADCInitialised) adc_init();
Expand All @@ -39,6 +78,7 @@ int UEXTSetGPIODirection(int gpio,int pinType) {
// ***************************************************************************************

int UEXTSetGPIO(int gpio,bool isOn) {
if (INUSE(gpio)) return 1;
gpio_put(gpio,isOn ? 1 : 0);
return 0;
}
Expand All @@ -50,6 +90,10 @@ int UEXTSetGPIO(int gpio,bool isOn) {
// ***************************************************************************************

int UEXTGetGPIO(int gpio,bool *pIsHigh) {
if (INUSE(gpio)) {
*pIsHigh = 0;
return 1;
}
*pIsHigh = gpio_get(gpio);
return 0;
}
Expand All @@ -61,6 +105,10 @@ int UEXTGetGPIO(int gpio,bool *pIsHigh) {
// ***************************************************************************************

int UEXTGetGPIOAnalogue(int gpio,uint16_t *pLevel) {
if (INUSE(gpio)) {
*pLevel = 0;
return 1;
}
adc_select_input(gpio-26);
*pLevel = adc_read();
return 0;
Expand All @@ -72,10 +120,6 @@ int UEXTGetGPIOAnalogue(int gpio,uint16_t *pLevel) {
//
// ***************************************************************************************

#define I2C_SCL_PIN (23)
#define I2C_SDA_PIN (22)

#define I2C_DEVICE (i2c1)

int UEXTI2CInitialise(void) {
i2c_init(I2C_DEVICE, 400 * 1000);
Expand Down Expand Up @@ -115,6 +159,7 @@ int UEXTI2CReadBlock(uint8_t device, uint8_t *data,size_t size) {
//
// ***************************************************************************************

#ifndef SPI_BLOCKED
static int reg_read(spi_inst_t *spi,const uint cs,const uint8_t reg,uint8_t *buf,const uint8_t nbytes) {
int num_bytes_read = 0;
uint8_t mb = 0;
Expand All @@ -132,21 +177,19 @@ static int reg_read(spi_inst_t *spi,const uint cs,const uint8_t reg,uint8_t *buf
gpio_put(cs, 1);
return num_bytes_read;
}
#endif

// ***************************************************************************************
//
// UEXT SPI Initialise
//
// ***************************************************************************************

#define SPI_MISO_PIN (24)
#define SPI_MOSI_PIN (27)
#define SPI_SCK_PIN (26)
#define SPI_CS_PIN (25)

#define SPI_DEVICE (spi1)

int UEXTSPIInitialise(void) {
#ifdef SPI_BLOCKED
return 1;
#else
gpio_init(SPI_CS_PIN); // Set SPI pin high
gpio_set_dir(SPI_CS_PIN, GPIO_OUT);
gpio_put(SPI_CS_PIN, 1);
Expand All @@ -167,6 +210,7 @@ int UEXTSPIInitialise(void) {
uint8_t data;
reg_read(SPI_DEVICE, SPI_CS_PIN, 0, &data, 1); // Workaround: perform throw-away read to make SCK idle high
return 0;
#endif
}

// ***************************************************************************************
Expand All @@ -176,10 +220,14 @@ int UEXTSPIInitialise(void) {
// ***************************************************************************************

int UEXTSPIWriteBlock(uint8_t *data,size_t size) {
#ifdef SPI_BLOCKED
return 1;
#else
gpio_put(SPI_CS_PIN, 0);
size_t nWritten = spi_write_blocking(SPI_DEVICE, data,size);
gpio_put(SPI_CS_PIN, 1);
return (nWritten == size) ? 0 : 1;
#endif
}

// ***************************************************************************************
Expand All @@ -189,15 +237,20 @@ int UEXTSPIWriteBlock(uint8_t *data,size_t size) {
// ***************************************************************************************

int UEXTSPIReadBlock(uint8_t *data,size_t size) {
#ifdef SPI_BLOCKED
return 1;
#else
gpio_put(SPI_CS_PIN, 0);
size_t nRead = spi_read_blocking(SPI_DEVICE, 0, data,size);
gpio_put(SPI_CS_PIN, 1);
return (nRead == size) ? 0 : 1;
#endif
}

// ***************************************************************************************
//
// Date Revision
// ==== ========
// 02/05/24 Added code to stop interfering with SPI pins.
//
// ***************************************************************************************

0 comments on commit 31eb950

Please sign in to comment.