Toyomasa Watarai
/
Hello-FM25W256
Hello program for the FM25W256 library
main.cpp@0:3efb18460968, 2016-03-04 (annotated)
- Committer:
- MACRUM
- Date:
- Fri Mar 04 13:48:22 2016 +0000
- Revision:
- 0:3efb18460968
- Child:
- 1:be908b1aafe6
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:3efb18460968 | 1 | #include "mbed.h" |
MACRUM | 0:3efb18460968 | 2 | |
MACRUM | 0:3efb18460968 | 3 | Serial pc(USBTX, USBRX); |
MACRUM | 0:3efb18460968 | 4 | SPI fram(dp2, dp1, dp6); |
MACRUM | 0:3efb18460968 | 5 | DigitalOut _cs(dp18); |
MACRUM | 0:3efb18460968 | 6 | |
MACRUM | 0:3efb18460968 | 7 | #define CMD_WREN 0x06 |
MACRUM | 0:3efb18460968 | 8 | #define CMD_WRDI 0x04 |
MACRUM | 0:3efb18460968 | 9 | #define CMD_RDSR 0x05 |
MACRUM | 0:3efb18460968 | 10 | #define CMD_WRSR 0x01 |
MACRUM | 0:3efb18460968 | 11 | #define CMD_READ 0x03 |
MACRUM | 0:3efb18460968 | 12 | #define CMD_WRITE 0x02 |
MACRUM | 0:3efb18460968 | 13 | |
MACRUM | 0:3efb18460968 | 14 | void write_byte(uint16_t address, uint8_t data) |
MACRUM | 0:3efb18460968 | 15 | { |
MACRUM | 0:3efb18460968 | 16 | _cs = 0; |
MACRUM | 0:3efb18460968 | 17 | fram.write(CMD_WREN); |
MACRUM | 0:3efb18460968 | 18 | _cs = 1; |
MACRUM | 0:3efb18460968 | 19 | |
MACRUM | 0:3efb18460968 | 20 | _cs = 0; |
MACRUM | 0:3efb18460968 | 21 | fram.write(CMD_WRITE); |
MACRUM | 0:3efb18460968 | 22 | fram.write(address >> 8); |
MACRUM | 0:3efb18460968 | 23 | fram.write(address & 0xFF); |
MACRUM | 0:3efb18460968 | 24 | fram.write(data & 0xFF); |
MACRUM | 0:3efb18460968 | 25 | _cs = 1; |
MACRUM | 0:3efb18460968 | 26 | } |
MACRUM | 0:3efb18460968 | 27 | |
MACRUM | 0:3efb18460968 | 28 | uint8_t read_byte(uint16_t address) |
MACRUM | 0:3efb18460968 | 29 | { |
MACRUM | 0:3efb18460968 | 30 | uint8_t data; |
MACRUM | 0:3efb18460968 | 31 | |
MACRUM | 0:3efb18460968 | 32 | _cs = 0; |
MACRUM | 0:3efb18460968 | 33 | fram.write(CMD_READ); |
MACRUM | 0:3efb18460968 | 34 | fram.write(address >> 8); |
MACRUM | 0:3efb18460968 | 35 | fram.write(address & 0xFF); |
MACRUM | 0:3efb18460968 | 36 | data = fram.write(0); |
MACRUM | 0:3efb18460968 | 37 | _cs = 1; |
MACRUM | 0:3efb18460968 | 38 | |
MACRUM | 0:3efb18460968 | 39 | return data; |
MACRUM | 0:3efb18460968 | 40 | } |
MACRUM | 0:3efb18460968 | 41 | |
MACRUM | 0:3efb18460968 | 42 | void read_bytes(uint16_t address, uint8_t *buf, uint16_t size) |
MACRUM | 0:3efb18460968 | 43 | { |
MACRUM | 0:3efb18460968 | 44 | _cs = 0; |
MACRUM | 0:3efb18460968 | 45 | fram.write(CMD_READ); |
MACRUM | 0:3efb18460968 | 46 | fram.write(address >> 8); |
MACRUM | 0:3efb18460968 | 47 | fram.write(address & 0xFF); |
MACRUM | 0:3efb18460968 | 48 | while (size--) { |
MACRUM | 0:3efb18460968 | 49 | *buf++ = fram.write(0); |
MACRUM | 0:3efb18460968 | 50 | } |
MACRUM | 0:3efb18460968 | 51 | _cs = 1; |
MACRUM | 0:3efb18460968 | 52 | } |
MACRUM | 0:3efb18460968 | 53 | |
MACRUM | 0:3efb18460968 | 54 | |
MACRUM | 0:3efb18460968 | 55 | void read_test() |
MACRUM | 0:3efb18460968 | 56 | { |
MACRUM | 0:3efb18460968 | 57 | uint16_t adrs = 0; |
MACRUM | 0:3efb18460968 | 58 | for(int i=0; i<16; i++) { |
MACRUM | 0:3efb18460968 | 59 | pc.printf("0x%04X : ", i * 16); |
MACRUM | 0:3efb18460968 | 60 | for(int j=0; j<16; j++) { |
MACRUM | 0:3efb18460968 | 61 | //pc.printf("%02X ", fram.write(0)); |
MACRUM | 0:3efb18460968 | 62 | pc.printf("%02X ", read_byte(adrs++)); |
MACRUM | 0:3efb18460968 | 63 | } |
MACRUM | 0:3efb18460968 | 64 | pc.printf("\n"); |
MACRUM | 0:3efb18460968 | 65 | } |
MACRUM | 0:3efb18460968 | 66 | } |
MACRUM | 0:3efb18460968 | 67 | |
MACRUM | 0:3efb18460968 | 68 | void write_test() |
MACRUM | 0:3efb18460968 | 69 | { |
MACRUM | 0:3efb18460968 | 70 | for(int i=0; i<256; i++) { |
MACRUM | 0:3efb18460968 | 71 | write_byte(i, i); |
MACRUM | 0:3efb18460968 | 72 | } |
MACRUM | 0:3efb18460968 | 73 | } |
MACRUM | 0:3efb18460968 | 74 | |
MACRUM | 0:3efb18460968 | 75 | int main() |
MACRUM | 0:3efb18460968 | 76 | { |
MACRUM | 0:3efb18460968 | 77 | fram.format(8, 0); |
MACRUM | 0:3efb18460968 | 78 | fram.frequency(20000000); |
MACRUM | 0:3efb18460968 | 79 | _cs = 1; |
MACRUM | 0:3efb18460968 | 80 | |
MACRUM | 0:3efb18460968 | 81 | pc.printf("\nFM25W256 test program - write test? (y/n) "); |
MACRUM | 0:3efb18460968 | 82 | if (pc.getc() == 'y') { |
MACRUM | 0:3efb18460968 | 83 | write_test(); |
MACRUM | 0:3efb18460968 | 84 | } |
MACRUM | 0:3efb18460968 | 85 | |
MACRUM | 0:3efb18460968 | 86 | pc.printf("\nFM25W256 test program - read test? (y/n) "); |
MACRUM | 0:3efb18460968 | 87 | if (pc.getc() == 'y') { |
MACRUM | 0:3efb18460968 | 88 | pc.printf("\n"); |
MACRUM | 0:3efb18460968 | 89 | read_test(); |
MACRUM | 0:3efb18460968 | 90 | } |
MACRUM | 0:3efb18460968 | 91 | |
MACRUM | 0:3efb18460968 | 92 | while(1) { |
MACRUM | 0:3efb18460968 | 93 | } |
MACRUM | 0:3efb18460968 | 94 | } |