This is a library to talk to STs SPI EEPROM with part number M95xxx.

Committer:
JamesStockton
Date:
Sat Aug 17 08:49:04 2019 +0000
Revision:
0:e4a388ac74a0
This is a library to talk to ST SPI EEPROM with part number M95xxx.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JamesStockton 0:e4a388ac74a0 1
JamesStockton 0:e4a388ac74a0 2
JamesStockton 0:e4a388ac74a0 3 #include "SPIMem.h"
JamesStockton 0:e4a388ac74a0 4
JamesStockton 0:e4a388ac74a0 5
JamesStockton 0:e4a388ac74a0 6 SPIMem::SPIMem(PinName _mosi, PinName _miso, PinName _sckl, PinName _cs) : spi(_mosi, _miso, _sckl), cs(_cs) {
JamesStockton 0:e4a388ac74a0 7 cs = 1;
JamesStockton 0:e4a388ac74a0 8
JamesStockton 0:e4a388ac74a0 9 spi.frequency(20000000);
JamesStockton 0:e4a388ac74a0 10 }
JamesStockton 0:e4a388ac74a0 11
JamesStockton 0:e4a388ac74a0 12 void SPIMem::writeEnable(void) {
JamesStockton 0:e4a388ac74a0 13 cs = 0;
JamesStockton 0:e4a388ac74a0 14 spi.write(WREN);
JamesStockton 0:e4a388ac74a0 15 cs = 1;
JamesStockton 0:e4a388ac74a0 16 }
JamesStockton 0:e4a388ac74a0 17
JamesStockton 0:e4a388ac74a0 18 void SPIMem::writeDisable(void) {
JamesStockton 0:e4a388ac74a0 19 cs = 0;
JamesStockton 0:e4a388ac74a0 20 spi.write(WRDI);
JamesStockton 0:e4a388ac74a0 21 cs = 1;
JamesStockton 0:e4a388ac74a0 22 }
JamesStockton 0:e4a388ac74a0 23
JamesStockton 0:e4a388ac74a0 24 void SPIMem::write(uint16_t addr, uint8_t val) {
JamesStockton 0:e4a388ac74a0 25 writeEnable();
JamesStockton 0:e4a388ac74a0 26
JamesStockton 0:e4a388ac74a0 27 cs = 0;
JamesStockton 0:e4a388ac74a0 28 spi.write(WRITE);
JamesStockton 0:e4a388ac74a0 29 spi.write((uint8_t)(addr >> 8));
JamesStockton 0:e4a388ac74a0 30 spi.write((uint8_t)addr);
JamesStockton 0:e4a388ac74a0 31 spi.write(val);
JamesStockton 0:e4a388ac74a0 32 cs = 1;
JamesStockton 0:e4a388ac74a0 33 }
JamesStockton 0:e4a388ac74a0 34
JamesStockton 0:e4a388ac74a0 35 int SPIMem::read(uint16_t addr) {
JamesStockton 0:e4a388ac74a0 36 cs = 0;
JamesStockton 0:e4a388ac74a0 37 spi.write(READ);
JamesStockton 0:e4a388ac74a0 38 spi.write((uint8_t)(addr >> 8));
JamesStockton 0:e4a388ac74a0 39 spi.write((uint8_t)addr);
JamesStockton 0:e4a388ac74a0 40 int data = spi.write(0x00);
JamesStockton 0:e4a388ac74a0 41 cs = 1;
JamesStockton 0:e4a388ac74a0 42
JamesStockton 0:e4a388ac74a0 43 return data;
JamesStockton 0:e4a388ac74a0 44 }
JamesStockton 0:e4a388ac74a0 45
JamesStockton 0:e4a388ac74a0 46 void SPIMem::burstWrite(uint16_t addr, char *data, uint16_t len) {
JamesStockton 0:e4a388ac74a0 47 writeEnable();
JamesStockton 0:e4a388ac74a0 48
JamesStockton 0:e4a388ac74a0 49 cs = 0;
JamesStockton 0:e4a388ac74a0 50 spi.write(WRITE);
JamesStockton 0:e4a388ac74a0 51 spi.write((uint8_t)(addr >> 8));
JamesStockton 0:e4a388ac74a0 52 spi.write((uint8_t)addr);
JamesStockton 0:e4a388ac74a0 53 for(int i=0; i<len; i++) {
JamesStockton 0:e4a388ac74a0 54 spi.write(data[i]);
JamesStockton 0:e4a388ac74a0 55 }
JamesStockton 0:e4a388ac74a0 56 cs = 1;
JamesStockton 0:e4a388ac74a0 57 }