Suga koubou
/
SPIFLASH_AT45DB
SPI Flash AT45DB080D (Atmel) see: http://mbed.org/users/okini3939/notebook/extend-memory/
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 * SPI FLASH AT45DB081D (Atmel) 00003 * 8Mbit 00004 */ 00005 #include "mbed.h" 00006 #include "at45db161d.h" 00007 00008 #undef PAGE_SIZE 00009 #define PAGE_SIZE 264 // AT45DB081D (1MB) 00010 //#define PAGE_SIZE 528 // AT45DB321D (4MB) 00011 #define PAGE_NUM 4095 // AT45DB081D (1MB) 00012 //#define PAGE_NUM 8192 // AT45DB321D (4MB) 00013 00014 #define WRITE_BUFFER 1 00015 #define READ_BUFFER 2 00016 00017 DigitalOut myled(LED1); 00018 Serial pc(USBTX, USBRX); 00019 00020 SPI spi(p11, p12, p13); // mosi, miso, sclk 00021 ATD45DB161D memory(spi, p19); 00022 00023 void flash_write (int addr, char *buf, int len) { 00024 int i; 00025 memory.BufferWrite(WRITE_BUFFER, addr % PAGE_SIZE); 00026 for (i = 0; i < len; i ++) { 00027 spi.write(buf[i]); 00028 } 00029 memory.BufferToPage(WRITE_BUFFER, addr / PAGE_SIZE, 1); 00030 } 00031 00032 void flash_read (int addr, char *buf, int len) { 00033 int i; 00034 memory.PageToBuffer(addr / PAGE_SIZE, READ_BUFFER); 00035 memory.BufferRead(READ_BUFFER, addr % PAGE_SIZE, 1); 00036 for (i = 0; i < len; i ++) { 00037 buf[i] = spi.write(0xff); 00038 } 00039 } 00040 00041 int main() { 00042 int i; 00043 char buf[PAGE_SIZE]; 00044 Timer t; 00045 ATD45DB161D::ID id; 00046 00047 pc.baud(115200); 00048 spi.frequency(16000000); 00049 wait_ms(500); 00050 00051 memory.ReadManufacturerAndDeviceID(&id); 00052 printf("RAM Manufacturer ID : %02x\r\n", id.manufacturer); 00053 printf("RAM Device ID : %02x %02x\r\n", id.device[0], id.device[1]); 00054 wait_ms(10); 00055 00056 printf("\r\nHELLO test\r\n"); 00057 00058 printf("RAM write\r\n"); 00059 strcpy(buf, "Hello!"); 00060 flash_write(0, buf, 6); 00061 00062 for (i = 0; i < PAGE_SIZE; i ++) { 00063 buf[i] = i; 00064 } 00065 flash_write(6, buf, PAGE_SIZE - 6); 00066 00067 wait(1); 00068 memset(buf, 0, PAGE_SIZE); 00069 00070 printf("RAM read\r\n"); 00071 flash_read(0, buf, PAGE_SIZE); 00072 for (i = 0; i < PAGE_SIZE; i ++) { 00073 printf(" %02x", buf[i]); 00074 if ((i & 0x0f) == 0x0f) 00075 printf("\r\n"); 00076 } 00077 00078 wait(1); 00079 00080 printf("\r\nWrite/Read time\r\n"); 00081 00082 printf("RAM write\r\n"); 00083 t.reset(); 00084 t.start(); 00085 for (i = 0; i < 0x20000; i += PAGE_SIZE) { 00086 buf[0] = (i >> 8) & 0xff; 00087 flash_write(i, buf, PAGE_SIZE); 00088 if ((i & 0x0fff) == 0) printf("."); 00089 } 00090 t.stop(); 00091 printf("\r\ntime %f, %f KBytes/sec\r\n", t.read(), (float)0x20000 / 1024 / t.read()); 00092 00093 wait(1); 00094 00095 printf("RAM read\r\n"); 00096 t.reset(); 00097 t.start(); 00098 for (i = 0; i < 0x20000; i += PAGE_SIZE) { 00099 flash_read(i, buf, PAGE_SIZE); 00100 if (buf[0] != ((i >> 8) & 0xff)) { 00101 printf("error %d\r\n", i); 00102 break; 00103 } 00104 if ((i & 0x0fff) == 0) printf("."); 00105 } 00106 t.stop(); 00107 printf("\r\ntime %f, %f KBytes/sec\r\n", t.read(), 0x20000 / 1024 / t.read()); 00108 00109 }
Generated on Fri Jul 15 2022 14:38:05 by 1.7.2