Ibrahim Abd Elkader / Mbed 2 deprecated nrflash

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
mux
Date:
Sun Mar 13 11:30:09 2011 +0000
Commit message:
1.0

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nrflash.cpp Show annotated file Show diff for this revision Revisions of this file
nrflash.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f9a5ac1b59f7 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 13 11:30:09 2011 +0000
@@ -0,0 +1,52 @@
+/**
+ * Copyright (c) 2011 Ibrahim Abd Elkader
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Program Nordic nRF24LU1+ chips using SPI.
+ * Instructions:
+ * 1)convert the ihx to binary using objcopy like this:
+ *   objcopy --gap-fill 255 -Iihex -O binary nrf.ihx nrf.bin
+ * 2)copy nrf.bin to MBED flash
+ * 3)copy nrflash to MBED flash
+ * 4)reset the MBED
+ **/
+#include <nrflash.h> 
+NRFlash nrflash(p11, p12, p13, p14, p15, p16); //mosi, miso, sck, ssel, prog, reset
+LocalFileSystem local("local");
+
+int main() 
+{   
+    nrflash.enable_programming();
+    
+    printf("FSR: %d\n", nrflash.read_fsr());
+    printf("erasing flash...\n");        
+    nrflash.erase_flash();
+        
+    printf("flashing...\n");
+    nrflash.write_flash("/local/nrf.bin");
+    
+    printf("saving flash...\n");
+    nrflash.read_flash("/local/flash.bin"); 
+    printf("all done...\n");
+    
+    nrflash.disable_programming();
+    nrflash.reset();
+    
+    return 0;
+}
diff -r 000000000000 -r f9a5ac1b59f7 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Mar 13 11:30:09 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r f9a5ac1b59f7 nrflash.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nrflash.cpp	Sun Mar 13 11:30:09 2011 +0000
@@ -0,0 +1,133 @@
+#include <nrflash.h>
+#define WREN        (0x06)    /*Set flash write enable,FSR.WEN*/             
+#define WRDIS       (0x04)    /*Reset flash write enable, FSR.WEN*/             
+#define RDSR        (0x05)    /*1 (or more) Read Flash Status Register (FSR)*/             
+#define WRSR        (0x01)    /*1 Write Flash Status Register (FSR).*/
+#define READ        (0x03)    /*Read data from flash*/                                                   
+#define PROGRAM     (0x02)    /*Write data to flash*/             
+#define ERASE_PAGE  (0x52)    /*Erase addressed flash page*/
+#define ERASE_ALL   (0x62)    /* Erase all pages of flash MainBlock*/
+#define RDFPCR      (0x89)    /*Read Flash Protect Configuration Register (FPCR)*/
+#define RDISIP      (0x84)    /*Set flash InfoPage read-back disable*/
+#define RDISMB      (0x85)    /*Set flash MainBlock read-back disable*/
+#define ENDEBUG     (0x86)    /*Write 0x00 to InfoPage byte 0x24*/
+#define RDYN        (1<<4)
+#define FLASH_LEN   (32768)
+NRFlash::NRFlash(PinName mosi, PinName miso, PinName sclk, PinName ssel_, PinName prog_, PinName rst_):
+spi(mosi, miso, sclk), 
+ssel(ssel_),
+prog(prog_),
+rst(rst_)
+{
+    spi.frequency(12500000);
+    spi.format(8, 0);
+    rst = 1;
+}
+
+void NRFlash::reset()
+{
+    rst = 0;
+    wait_ms(2);
+    rst = 1;
+}
+
+void NRFlash::enable_programming()
+{
+    prog = 1;
+    wait_ms(2);
+}
+
+void NRFlash::disable_programming()
+{
+    prog = 0;   
+}
+
+void NRFlash::write_enable()
+{
+    ssel = 0;
+    spi.write(WREN);
+    ssel = 1;
+}
+
+int NRFlash::read_fsr()
+{
+    int fsr;
+    ssel = 0;
+    spi.write(RDSR);
+    fsr = spi.write(0);
+    ssel = 1;
+    return fsr;
+}
+
+void NRFlash::write_fsr(int fsr)
+{
+    ssel = 0;   
+    spi.write(WRSR);
+    spi.write(fsr);
+    ssel = 1;
+}
+
+void NRFlash::erase_flash()
+{
+    write_enable();
+    ssel = 0;    
+    spi.write(ERASE_ALL);
+    ssel = 1;
+    while (read_fsr() & RDYN); /*wait for flash to be erased*/    
+}
+
+int NRFlash::read_flash(const char *path)
+{
+    int bytes = 0;
+    int count = 0;
+    char buf[1024];
+    
+    FILE *fp = fopen(path, "wb");
+    if (fp == NULL) {
+        printf("file not found\n");
+        return -1;
+    }        
+    ssel = 0;   
+    spi.write(READ);
+    spi.write(0);
+    spi.write(0);
+    while (bytes++ < FLASH_LEN) {
+        buf[count++] = spi.write(0);
+        if (count == sizeof(buf)) {
+            count = 0;
+            fwrite(buf, 1, sizeof(buf), fp);
+        }
+    }
+    ssel = 1;
+    fclose(fp);
+    return 0;
+}
+
+int NRFlash::write_flash(const char *path)
+{
+    int len;
+    uint16_t addr = 0;
+    char buf[256];
+        
+    FILE *fp = fopen(path, "rb");
+    if (fp == NULL) {
+        printf("file not found\n");
+        return -1;
+    }
+       
+    while ((len = fread(buf, 1, sizeof(buf), fp)) > 0) {
+        write_enable();
+        ssel = 0;
+        spi.write(PROGRAM);   /*PROGRAM command*/
+        spi.write(addr>>8);   /*write address MSB*/
+        spi.write(addr&0x0F); /*write address LSB*/    
+        addr += len;
+        for (int i=0; i<len; i++) {
+            spi.write(buf[i]);
+        }        
+        ssel = 1;               
+        while (read_fsr() & RDYN); /*wait for flash to be ready*/
+    }
+    fclose(fp);
+    return 0; 
+}
\ No newline at end of file
diff -r 000000000000 -r f9a5ac1b59f7 nrflash.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nrflash.h	Sun Mar 13 11:30:09 2011 +0000
@@ -0,0 +1,22 @@
+#ifndef __NRFLASH_H__
+#define __NRFLASH_H__
+#include <mbed.h>
+class NRFlash {
+    private:
+        SPI spi;
+        DigitalOut ssel;
+        DigitalOut prog;
+        DigitalOut rst;
+        void write_enable();
+    public:
+        NRFlash(PinName mosi, PinName miso, PinName sclk, PinName ssel, PinName prog, PinName rst);
+        void reset();
+        void enable_programming();
+        void disable_programming();
+        int  read_fsr();
+        void write_fsr(int fsr);
+        void erase_flash();
+        int read_flash(const char *path);
+        int write_flash(const char *path);
+};
+#endif //__NRFLASH_H__
\ No newline at end of file