diff --git a/spi.c b/spi.c index 9e0fbd2..4a3dc51 100644 --- a/spi.c +++ b/spi.c @@ -203,13 +203,6 @@ static void spiQuadTx(struct ff_spi *spi, uint8_t out) { } } -void spiCommand(struct ff_spi *spi, uint8_t cmd) { - if (spi->type == ST_QPI) - spiQuadTx(spi, cmd); - else - spiSingleTx(spi, cmd); -} - static uint8_t spiDualRx(struct ff_spi *spi) { int bit; uint8_t in = 0; @@ -276,6 +269,20 @@ uint8_t spiRx(struct ff_spi *spi) { } } +void spiCommand(struct ff_spi *spi, uint8_t cmd) { + if (spi->type == ST_QPI) + spiQuadTx(spi, cmd); + else + spiSingleTx(spi, cmd); +} + +uint8_t spiCommandRx(struct ff_spi *spi) { + if (spi->type == ST_QPI) + return spiQuadRx(spi); + else + return spiSingleRx(spi); +} + uint8_t spiReadSr(struct ff_spi *spi, int sr) { uint8_t val = 0xff; @@ -350,6 +357,51 @@ void spiWriteSr(struct ff_spi *spi, int sr, uint8_t val) { } } +struct spi_id spiId(struct ff_spi *spi) { + struct spi_id id; + memset(&id, 0xff, sizeof(id)); + + spiBegin(spi); + spiCommand(spi, 0x90); // Read manufacturer ID + spiCommand(spi, 0x00); // Dummy byte 1 + spiCommand(spi, 0x00); // Dummy byte 2 + spiCommand(spi, 0x00); // Dummy byte 3 + id.manufacturer_id = spiCommandRx(spi); + id.device_id = spiCommandRx(spi); + spiEnd(spi); + + spiBegin(spi); + spiCommand(spi, 0x9f); // Read device id + id._manufacturer_id = spiCommandRx(spi); + id.memory_type = spiCommandRx(spi); + id.memory_size = spiCommandRx(spi); + spiEnd(spi); + + spiBegin(spi); + spiCommand(spi, 0xab); // Read electronic signature + spiCommand(spi, 0x00); // Dummy byte 1 + spiCommand(spi, 0x00); // Dummy byte 2 + spiCommand(spi, 0x00); // Dummy byte 3 + id.signature = spiCommandRx(spi); + spiEnd(spi); + + spiBegin(spi); + spiCommand(spi, 0x4b); // Read unique ID + spiCommand(spi, 0x00); // Dummy byte 1 + spiCommand(spi, 0x00); // Dummy byte 2 + spiCommand(spi, 0x00); // Dummy byte 3 + spiCommand(spi, 0x00); // Dummy byte 4 + id.serial[0] = spiCommandRx(spi); + id.serial[1] = spiCommandRx(spi); + id.serial[2] = spiCommandRx(spi); + id.serial[3] = spiCommandRx(spi); + spiEnd(spi); + + return id; +} + +uint32_t spiJedecId(struct ff_spi *spi); + int spiSetType(struct ff_spi *spi, enum spi_type type) { if (spi->type == type) diff --git a/spi.h b/spi.h index 52c5f37..a1a9d02 100644 --- a/spi.h +++ b/spi.h @@ -34,6 +34,16 @@ enum spi_pin { SP_D3, }; +struct spi_id { + uint8_t manufacturer_id; // Result from 0x90 + uint8_t device_id; // Result from 0x90 + uint8_t _manufacturer_id; // Result from 0x9f + uint8_t memory_type; // Result from 0x9f + uint8_t memory_size; // Result from 0x9f + uint8_t signature; // Result from 0xab + uint8_t serial[4]; // Result from 0x4b +}; + struct ff_spi; void spiPause(struct ff_spi *spi); @@ -53,6 +63,7 @@ uint8_t spiReadSr(struct ff_spi *spi, int sr); void spiWriteSr(struct ff_spi *spi, int sr, uint8_t val); int spiSetType(struct ff_spi *spi, enum spi_type type); int spiRead(struct ff_spi *spi, uint32_t addr, uint8_t *data, unsigned int count); +struct spi_id spiId(struct ff_spi *spi); //int spi_wait_for_not_busy(struct ff_spi *spi); int spiWrite(struct ff_spi *spi, uint32_t addr, const uint8_t *data, unsigned int count); uint8_t spiReset(struct ff_spi *spi);