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.
nrflash.cpp
00001 #include <nrflash.h> 00002 #define WREN (0x06) /*Set flash write enable,FSR.WEN*/ 00003 #define WRDIS (0x04) /*Reset flash write enable, FSR.WEN*/ 00004 #define RDSR (0x05) /*1 (or more) Read Flash Status Register (FSR)*/ 00005 #define WRSR (0x01) /*1 Write Flash Status Register (FSR).*/ 00006 #define READ (0x03) /*Read data from flash*/ 00007 #define PROGRAM (0x02) /*Write data to flash*/ 00008 #define ERASE_PAGE (0x52) /*Erase addressed flash page*/ 00009 #define ERASE_ALL (0x62) /* Erase all pages of flash MainBlock*/ 00010 #define RDFPCR (0x89) /*Read Flash Protect Configuration Register (FPCR)*/ 00011 #define RDISIP (0x84) /*Set flash InfoPage read-back disable*/ 00012 #define RDISMB (0x85) /*Set flash MainBlock read-back disable*/ 00013 #define ENDEBUG (0x86) /*Write 0x00 to InfoPage byte 0x24*/ 00014 #define RDYN (1<<4) 00015 #define FLASH_LEN (32768) 00016 NRFlash::NRFlash(PinName mosi, PinName miso, PinName sclk, PinName ssel_, PinName prog_, PinName rst_): 00017 spi(mosi, miso, sclk), 00018 ssel(ssel_), 00019 prog(prog_), 00020 rst(rst_) 00021 { 00022 spi.frequency(12500000); 00023 spi.format(8, 0); 00024 rst = 1; 00025 } 00026 00027 void NRFlash::reset() 00028 { 00029 rst = 0; 00030 wait_ms(2); 00031 rst = 1; 00032 } 00033 00034 void NRFlash::enable_programming() 00035 { 00036 prog = 1; 00037 wait_ms(2); 00038 } 00039 00040 void NRFlash::disable_programming() 00041 { 00042 prog = 0; 00043 } 00044 00045 void NRFlash::write_enable() 00046 { 00047 ssel = 0; 00048 spi.write(WREN); 00049 ssel = 1; 00050 } 00051 00052 int NRFlash::read_fsr() 00053 { 00054 int fsr; 00055 ssel = 0; 00056 spi.write(RDSR); 00057 fsr = spi.write(0); 00058 ssel = 1; 00059 return fsr; 00060 } 00061 00062 void NRFlash::write_fsr(int fsr) 00063 { 00064 ssel = 0; 00065 spi.write(WRSR); 00066 spi.write(fsr); 00067 ssel = 1; 00068 } 00069 00070 void NRFlash::erase_flash() 00071 { 00072 write_enable(); 00073 ssel = 0; 00074 spi.write(ERASE_ALL); 00075 ssel = 1; 00076 while (read_fsr() & RDYN); /*wait for flash to be erased*/ 00077 } 00078 00079 int NRFlash::read_flash(const char *path) 00080 { 00081 int bytes = 0; 00082 int count = 0; 00083 char buf[1024]; 00084 00085 FILE *fp = fopen(path, "wb"); 00086 if (fp == NULL) { 00087 printf("file not found\n"); 00088 return -1; 00089 } 00090 ssel = 0; 00091 spi.write(READ); 00092 spi.write(0); 00093 spi.write(0); 00094 while (bytes++ < FLASH_LEN) { 00095 buf[count++] = spi.write(0); 00096 if (count == sizeof(buf)) { 00097 count = 0; 00098 fwrite(buf, 1, sizeof(buf), fp); 00099 } 00100 } 00101 ssel = 1; 00102 fclose(fp); 00103 return 0; 00104 } 00105 00106 int NRFlash::write_flash(const char *path) 00107 { 00108 int len; 00109 uint16_t addr = 0; 00110 char buf[256]; 00111 00112 FILE *fp = fopen(path, "rb"); 00113 if (fp == NULL) { 00114 printf("file not found\n"); 00115 return -1; 00116 } 00117 00118 while ((len = fread(buf, 1, sizeof(buf), fp)) > 0) { 00119 write_enable(); 00120 ssel = 0; 00121 spi.write(PROGRAM); /*PROGRAM command*/ 00122 spi.write(addr>>8); /*write address MSB*/ 00123 spi.write(addr&0x0F); /*write address LSB*/ 00124 addr += len; 00125 for (int i=0; i<len; i++) { 00126 spi.write(buf[i]); 00127 } 00128 ssel = 1; 00129 while (read_fsr() & RDYN); /*wait for flash to be ready*/ 00130 } 00131 fclose(fp); 00132 return 0; 00133 }
Generated on Fri Jul 15 2022 01:39:19 by
1.7.2