Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:819f21483c01, committed 2013-11-28
- Comitter:
- embeddedartists
- Date:
- Thu Nov 28 12:17:45 2013 +0000
- Parent:
- 0:aa38ad0bf51d
- Commit message:
- Renamed the SPI24LC080 class to SPI25LC080 as it is an interface to the 25LC080 chip.
Changed in this revision
--- a/SPI24LC080.cpp Sun Nov 24 11:24:40 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,117 +0,0 @@
-#include "mbed.h"
-#include "SPI24LC080.h"
-
-/* SPI E2PROM command set */
-#define INST_WREN 0x06 /* MSB A8 is set to 0, simplifying test */
-#define INST_WRDI 0x04
-#define INST_RDSR 0x05
-#define INST_WRSR 0x01
-#define INST_READ 0x03
-#define INST_WRITE 0x02
-
-/* RDSR status bit definition */
-#define RDSR_RDY 0x01
-#define RDSR_WEN 0x02
-
-/* Page size of E2PROM */
-#define PAGE_SIZE 16
-
-#define LOCAL_MIN(__a, __b) ( ((__a) < (__b)) ? (__a) : (__b) )
-
-SPI24LC080::SPI24LC080(PinName mosi, PinName sclk, PinName miso, PinName ssel) :
- m_spi(mosi, miso, sclk), m_ssel(ssel)
-{
-}
-
-void SPI24LC080::read(uint16_t address, uint8_t* pData, uint32_t length)
-{
- //pull SSEL/CS low
- m_ssel = 0;
-
- //output read command and address
- m_spi.write(INST_READ);
- m_spi.write((address >> 8) & 0xff);
- m_spi.write(address & 0xff);
-
- //read bytes from E2PROM
- for (uint32_t i = 0; i < length; i++) {
- pData[i] = m_spi.write(0x00); // write dummy byte to get read next value
- }
-
- //pull SSEL/CS high
- m_ssel = 1;
-}
-
-uint8_t SPI24LC080::status()
-{
- //pull SSEL/CS low
- m_ssel = 0;
-
- uint8_t status = m_spi.write(INST_RDSR);
-
- //pull SSEL/CS high
- m_ssel = 1;
-
- return status;
-}
-
-void SPI24LC080::write_enable()
-{
- //pull SSEL/CS low
- m_ssel = 0;
-
- //output write command and address
- m_spi.write(INST_WREN);
-
- //pull SSEL/CS high
- m_ssel = 1;
-}
-
-void SPI24LC080::write(uint16_t address, uint8_t* pData, uint32_t length)
-{
- uint32_t left = length;
- uint32_t offset = address;
- uint32_t len = 0;
-
- // find length of first page write
- if ((address / PAGE_SIZE) != ((address + length) / PAGE_SIZE)) {
- //spans across at least one boundary
- len = PAGE_SIZE - (address % PAGE_SIZE);
- } else {
- // ends inside same page => use normal length
- len = length % PAGE_SIZE;
- }
-
- //insert code here to break up large write operation into several
- //page write operations (16 or 32 byte pages)
- while (left > 0) {
-
- //Enable write latch
- write_enable();
-
- //pull SSEL/CS low
- m_ssel = 0;
-
- //output write command and address
- m_spi.write(INST_WRITE);
- m_spi.write((offset >> 8) & 0xff);
- m_spi.write(offset & 0xff);
-
- //send bytes to write E2PROM
- for (uint32_t i = 0; i < len; i++) {
- m_spi.write(pData[offset + i]);
- }
-
- //pull SSEL/CS high
- m_ssel = 1;
-
- offset += len;
- left -= len;
- len = LOCAL_MIN(left, PAGE_SIZE);
-
- //wait until write operations is done
- //for now, just wait. A more sophisticated method is to poll the status register
- wait_ms(5);
- }
-}
-
--- a/SPI24LC080.h Sun Nov 24 11:24:40 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-#ifndef SPI24LC080_H
-#define SPI24LC080_H
-
-/**
- * Interface to the SPI24LC080 chip (http://ww1.microchip.com/downloads/en/DeviceDoc/22151b.pdf)
- *
- * @code
- * #include "mbed.h"
- *
- * SPI24LC080 e2prom;
- *
- * int main(void) {
- * uint8_t buff[20];
- * uint16_t addr = ...
- *
- * while(1) {
- * // read from e2prom
- * e2prom.read(addr, buff, 20);
- *
- * // write to e2prom
- * e2prom.write(addr, buff, 20);
- * }
- * }
- * @endcode
- */
-class SPI24LC080 {
-public:
-
- /** Create an interface to the SPI24LC080 chip
- *
- * @param mosi the SPI MOSI pin
- * @param sclk the SPI SCLK pin
- * @param miso the SPI MISO pin
- */
- SPI24LC080(PinName mosi = P0_14, PinName sclk = P0_12, PinName miso = P0_15, PinName ssel = P0_13);
-
- /** Reads from the E2PROM
- *
- * @param address the address to read from
- * @param pBuf where to store the read data
- * @param length the number of bytes to read
- */
- void read(uint16_t address, uint8_t* pData, uint32_t length);
-
- /** Writes to the E2PROM
- *
- * @param address the address to write to
- * @param pBuf the data to write
- * @param length the number of bytes to write
- */
- void write(uint16_t address, uint8_t* pData, uint32_t length);
-
- /** Reads the status register
- */
- uint8_t status();
-
-private:
- SPI m_spi;
- DigitalOut m_ssel;
-
- /** Sets the write enable latch/bit
- */
- void write_enable();
-};
-
-#endif
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SPI25LC080.cpp Thu Nov 28 12:17:45 2013 +0000
@@ -0,0 +1,117 @@
+#include "mbed.h"
+#include "SPI25LC080.h"
+
+/* SPI E2PROM command set */
+#define INST_WREN 0x06 /* MSB A8 is set to 0, simplifying test */
+#define INST_WRDI 0x04
+#define INST_RDSR 0x05
+#define INST_WRSR 0x01
+#define INST_READ 0x03
+#define INST_WRITE 0x02
+
+/* RDSR status bit definition */
+#define RDSR_RDY 0x01
+#define RDSR_WEN 0x02
+
+/* Page size of E2PROM */
+#define PAGE_SIZE 16
+
+#define LOCAL_MIN(__a, __b) ( ((__a) < (__b)) ? (__a) : (__b) )
+
+SPI25LC080::SPI25LC080(PinName mosi, PinName sclk, PinName miso, PinName ssel) :
+ m_spi(mosi, miso, sclk), m_ssel(ssel)
+{
+}
+
+void SPI25LC080::read(uint16_t address, uint8_t* pData, uint32_t length)
+{
+ //pull SSEL/CS low
+ m_ssel = 0;
+
+ //output read command and address
+ m_spi.write(INST_READ);
+ m_spi.write((address >> 8) & 0xff);
+ m_spi.write(address & 0xff);
+
+ //read bytes from E2PROM
+ for (uint32_t i = 0; i < length; i++) {
+ pData[i] = m_spi.write(0x00); // write dummy byte to get read next value
+ }
+
+ //pull SSEL/CS high
+ m_ssel = 1;
+}
+
+uint8_t SPI25LC080::status()
+{
+ //pull SSEL/CS low
+ m_ssel = 0;
+
+ uint8_t status = m_spi.write(INST_RDSR);
+
+ //pull SSEL/CS high
+ m_ssel = 1;
+
+ return status;
+}
+
+void SPI25LC080::write_enable()
+{
+ //pull SSEL/CS low
+ m_ssel = 0;
+
+ //output write command and address
+ m_spi.write(INST_WREN);
+
+ //pull SSEL/CS high
+ m_ssel = 1;
+}
+
+void SPI25LC080::write(uint16_t address, uint8_t* pData, uint32_t length)
+{
+ uint32_t left = length;
+ uint32_t offset = address;
+ uint32_t len = 0;
+
+ // find length of first page write
+ if ((address / PAGE_SIZE) != ((address + length) / PAGE_SIZE)) {
+ //spans across at least one boundary
+ len = PAGE_SIZE - (address % PAGE_SIZE);
+ } else {
+ // ends inside same page => use normal length
+ len = length % PAGE_SIZE;
+ }
+
+ //insert code here to break up large write operation into several
+ //page write operations (16 or 32 byte pages)
+ while (left > 0) {
+
+ //Enable write latch
+ write_enable();
+
+ //pull SSEL/CS low
+ m_ssel = 0;
+
+ //output write command and address
+ m_spi.write(INST_WRITE);
+ m_spi.write((offset >> 8) & 0xff);
+ m_spi.write(offset & 0xff);
+
+ //send bytes to write E2PROM
+ for (uint32_t i = 0; i < len; i++) {
+ m_spi.write(pData[offset + i]);
+ }
+
+ //pull SSEL/CS high
+ m_ssel = 1;
+
+ offset += len;
+ left -= len;
+ len = LOCAL_MIN(left, PAGE_SIZE);
+
+ //wait until write operations is done
+ //for now, just wait. A more sophisticated method is to poll the status register
+ wait_ms(5);
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SPI25LC080.h Thu Nov 28 12:17:45 2013 +0000
@@ -0,0 +1,67 @@
+#ifndef SPI25LC080_H
+#define SPI25LC080_H
+
+/**
+ * Interface to the 25LC080 chip (http://ww1.microchip.com/downloads/en/DeviceDoc/22151b.pdf)
+ *
+ * @code
+ * #include "mbed.h"
+ *
+ * SPI25LC080 e2prom;
+ *
+ * int main(void) {
+ * uint8_t buff[20];
+ * uint16_t addr = ...
+ *
+ * while(1) {
+ * // read from e2prom
+ * e2prom.read(addr, buff, 20);
+ *
+ * // write to e2prom
+ * e2prom.write(addr, buff, 20);
+ * }
+ * }
+ * @endcode
+ */
+class SPI25LC080 {
+public:
+
+ /** Create an interface to the 25LC080 chip
+ *
+ * @param mosi the SPI MOSI pin
+ * @param sclk the SPI SCLK pin
+ * @param miso the SPI MISO pin
+ */
+ SPI25LC080(PinName mosi = P0_14, PinName sclk = P0_12, PinName miso = P0_15, PinName ssel = P0_13);
+
+ /** Reads from the E2PROM
+ *
+ * @param address the address to read from
+ * @param pBuf where to store the read data
+ * @param length the number of bytes to read
+ */
+ void read(uint16_t address, uint8_t* pData, uint32_t length);
+
+ /** Writes to the E2PROM
+ *
+ * @param address the address to write to
+ * @param pBuf the data to write
+ * @param length the number of bytes to write
+ */
+ void write(uint16_t address, uint8_t* pData, uint32_t length);
+
+ /** Reads the status register
+ */
+ uint8_t status();
+
+private:
+ SPI m_spi;
+ DigitalOut m_ssel;
+
+ /** Sets the write enable latch/bit
+ */
+ void write_enable();
+};
+
+#endif
+
--- a/main.cpp Sun Nov 24 11:24:40 2013 +0000 +++ b/main.cpp Thu Nov 28 12:17:45 2013 +0000 @@ -1,9 +1,9 @@ #include "mbed.h" -#include "SPI24LC080.h" +#include "SPI25LC080.h" Serial pc(USBTX, USBRX); -SPI24LC080 e2prom; +SPI25LC080 e2prom; #define E2PROM_START_ADDR 0 #define BUF_SIZE 20