![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
testing n-Bed SPI flash
Fork of SPIFLASH_AT45DB by
main.cpp@0:54f4cc3dfc0e, 2012-11-26 (annotated)
- Committer:
- okini3939
- Date:
- Mon Nov 26 03:14:24 2012 +0000
- Revision:
- 0:54f4cc3dfc0e
- Child:
- 1:a78f957bd042
1st build
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:54f4cc3dfc0e | 1 | /* |
okini3939 | 0:54f4cc3dfc0e | 2 | * SPI FLASH AT45DB081D (Atmel) |
okini3939 | 0:54f4cc3dfc0e | 3 | * 8Mbit |
okini3939 | 0:54f4cc3dfc0e | 4 | */ |
okini3939 | 0:54f4cc3dfc0e | 5 | #include "mbed.h" |
okini3939 | 0:54f4cc3dfc0e | 6 | #include "at45db161d.h" |
okini3939 | 0:54f4cc3dfc0e | 7 | |
okini3939 | 0:54f4cc3dfc0e | 8 | #undef PAGE_SIZE |
okini3939 | 0:54f4cc3dfc0e | 9 | #define PAGE_SIZE 264 // AT45DB081D (1MB) |
okini3939 | 0:54f4cc3dfc0e | 10 | //#define PAGE_SIZE 528 // AT45DB321D (4MB) |
okini3939 | 0:54f4cc3dfc0e | 11 | #define PAGE_NUM 4095 // AT45DB081D (1MB) |
okini3939 | 0:54f4cc3dfc0e | 12 | //#define PAGE_NUM 8192 // AT45DB321D (4MB) |
okini3939 | 0:54f4cc3dfc0e | 13 | |
okini3939 | 0:54f4cc3dfc0e | 14 | #define WRITE_BUFFER 1 |
okini3939 | 0:54f4cc3dfc0e | 15 | #define READ_BUFFER 2 |
okini3939 | 0:54f4cc3dfc0e | 16 | |
okini3939 | 0:54f4cc3dfc0e | 17 | DigitalOut myled(LED1); |
okini3939 | 0:54f4cc3dfc0e | 18 | Serial pc(USBTX, USBRX); |
okini3939 | 0:54f4cc3dfc0e | 19 | |
okini3939 | 0:54f4cc3dfc0e | 20 | SPI spi(p11, p12, p13); // mosi, miso, sclk |
okini3939 | 0:54f4cc3dfc0e | 21 | ATD45DB161D memory(spi, p19); |
okini3939 | 0:54f4cc3dfc0e | 22 | |
okini3939 | 0:54f4cc3dfc0e | 23 | void flash_write (int addr, char *buf, int len) { |
okini3939 | 0:54f4cc3dfc0e | 24 | int i; |
okini3939 | 0:54f4cc3dfc0e | 25 | memory.BufferWrite(WRITE_BUFFER, addr % PAGE_SIZE); |
okini3939 | 0:54f4cc3dfc0e | 26 | for (i = 0; i < len; i ++) { |
okini3939 | 0:54f4cc3dfc0e | 27 | spi.write(buf[i]); |
okini3939 | 0:54f4cc3dfc0e | 28 | } |
okini3939 | 0:54f4cc3dfc0e | 29 | memory.BufferToPage(WRITE_BUFFER, addr / PAGE_SIZE, 1); |
okini3939 | 0:54f4cc3dfc0e | 30 | } |
okini3939 | 0:54f4cc3dfc0e | 31 | |
okini3939 | 0:54f4cc3dfc0e | 32 | void flash_read (int addr, char *buf, int len) { |
okini3939 | 0:54f4cc3dfc0e | 33 | int i; |
okini3939 | 0:54f4cc3dfc0e | 34 | memory.PageToBuffer(addr / PAGE_SIZE, READ_BUFFER); |
okini3939 | 0:54f4cc3dfc0e | 35 | memory.BufferRead(READ_BUFFER, addr % PAGE_SIZE, 1); |
okini3939 | 0:54f4cc3dfc0e | 36 | for (i = 0; i < len; i ++) { |
okini3939 | 0:54f4cc3dfc0e | 37 | buf[i] = spi.write(0xff); |
okini3939 | 0:54f4cc3dfc0e | 38 | } |
okini3939 | 0:54f4cc3dfc0e | 39 | } |
okini3939 | 0:54f4cc3dfc0e | 40 | |
okini3939 | 0:54f4cc3dfc0e | 41 | int main() { |
okini3939 | 0:54f4cc3dfc0e | 42 | int i; |
okini3939 | 0:54f4cc3dfc0e | 43 | char buf[PAGE_SIZE]; |
okini3939 | 0:54f4cc3dfc0e | 44 | Timer t; |
okini3939 | 0:54f4cc3dfc0e | 45 | ATD45DB161D::ID id; |
okini3939 | 0:54f4cc3dfc0e | 46 | |
okini3939 | 0:54f4cc3dfc0e | 47 | pc.baud(115200); |
okini3939 | 0:54f4cc3dfc0e | 48 | spi.frequency(16000000); |
okini3939 | 0:54f4cc3dfc0e | 49 | wait_ms(500); |
okini3939 | 0:54f4cc3dfc0e | 50 | |
okini3939 | 0:54f4cc3dfc0e | 51 | memory.ReadManufacturerAndDeviceID(&id); |
okini3939 | 0:54f4cc3dfc0e | 52 | printf("RAM Manufacturer ID : %02x\r\n", id.manufacturer); |
okini3939 | 0:54f4cc3dfc0e | 53 | printf("RAM Device ID : %02x %02x\r\n", id.device[0], id.device[1]); |
okini3939 | 0:54f4cc3dfc0e | 54 | wait_ms(10); |
okini3939 | 0:54f4cc3dfc0e | 55 | |
okini3939 | 0:54f4cc3dfc0e | 56 | printf("\r\nHELLO test\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 57 | |
okini3939 | 0:54f4cc3dfc0e | 58 | printf("RAM write\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 59 | strcpy(buf, "Hello!"); |
okini3939 | 0:54f4cc3dfc0e | 60 | flash_write(0, buf, 6); |
okini3939 | 0:54f4cc3dfc0e | 61 | |
okini3939 | 0:54f4cc3dfc0e | 62 | for (i = 0; i < PAGE_SIZE; i ++) { |
okini3939 | 0:54f4cc3dfc0e | 63 | buf[i] = i; |
okini3939 | 0:54f4cc3dfc0e | 64 | } |
okini3939 | 0:54f4cc3dfc0e | 65 | flash_write(6, buf, PAGE_SIZE - 6); |
okini3939 | 0:54f4cc3dfc0e | 66 | |
okini3939 | 0:54f4cc3dfc0e | 67 | wait(1); |
okini3939 | 0:54f4cc3dfc0e | 68 | memset(buf, 0, PAGE_SIZE); |
okini3939 | 0:54f4cc3dfc0e | 69 | |
okini3939 | 0:54f4cc3dfc0e | 70 | printf("RAM read\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 71 | flash_read(0, buf, PAGE_SIZE); |
okini3939 | 0:54f4cc3dfc0e | 72 | for (i = 0; i < PAGE_SIZE; i ++) { |
okini3939 | 0:54f4cc3dfc0e | 73 | printf(" %02x", buf[i]); |
okini3939 | 0:54f4cc3dfc0e | 74 | if ((i & 0x0f) == 0x0f) |
okini3939 | 0:54f4cc3dfc0e | 75 | printf("\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 76 | } |
okini3939 | 0:54f4cc3dfc0e | 77 | |
okini3939 | 0:54f4cc3dfc0e | 78 | wait(1); |
okini3939 | 0:54f4cc3dfc0e | 79 | |
okini3939 | 0:54f4cc3dfc0e | 80 | printf("\r\nWrite/Read time\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 81 | |
okini3939 | 0:54f4cc3dfc0e | 82 | printf("RAM write\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 83 | t.reset(); |
okini3939 | 0:54f4cc3dfc0e | 84 | t.start(); |
okini3939 | 0:54f4cc3dfc0e | 85 | for (i = 0; i < 0x20000; i += PAGE_SIZE) { |
okini3939 | 0:54f4cc3dfc0e | 86 | buf[0] = (i >> 8) & 0xff; |
okini3939 | 0:54f4cc3dfc0e | 87 | flash_write(i, buf, PAGE_SIZE); |
okini3939 | 0:54f4cc3dfc0e | 88 | if ((i & 0x0fff) == 0) printf("."); |
okini3939 | 0:54f4cc3dfc0e | 89 | } |
okini3939 | 0:54f4cc3dfc0e | 90 | t.stop(); |
okini3939 | 0:54f4cc3dfc0e | 91 | printf("\r\ntime %f, %f KBytes/sec\r\n", t.read(), (float)0x20000 / 1024 / t.read()); |
okini3939 | 0:54f4cc3dfc0e | 92 | |
okini3939 | 0:54f4cc3dfc0e | 93 | wait(1); |
okini3939 | 0:54f4cc3dfc0e | 94 | |
okini3939 | 0:54f4cc3dfc0e | 95 | printf("RAM read\r\n"); |
okini3939 | 0:54f4cc3dfc0e | 96 | t.reset(); |
okini3939 | 0:54f4cc3dfc0e | 97 | t.start(); |
okini3939 | 0:54f4cc3dfc0e | 98 | for (i = 0; i < 0x20000; i += PAGE_SIZE) { |
okini3939 | 0:54f4cc3dfc0e | 99 | flash_read(i, buf, PAGE_SIZE); |
okini3939 | 0:54f4cc3dfc0e | 100 | if (buf[0] != ((i >> 8) & 0xff)) { |
okini3939 | 0:54f4cc3dfc0e | 101 | printf("error %d\r\n", i); |
okini3939 | 0:54f4cc3dfc0e | 102 | break; |
okini3939 | 0:54f4cc3dfc0e | 103 | } |
okini3939 | 0:54f4cc3dfc0e | 104 | if ((i & 0x0fff) == 0) printf("."); |
okini3939 | 0:54f4cc3dfc0e | 105 | } |
okini3939 | 0:54f4cc3dfc0e | 106 | t.stop(); |
okini3939 | 0:54f4cc3dfc0e | 107 | printf("\r\ntime %f, %f KBytes/sec\r\n", t.read(), 0x20000 / 1024 / t.read()); |
okini3939 | 0:54f4cc3dfc0e | 108 | |
okini3939 | 0:54f4cc3dfc0e | 109 | } |