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