![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
library for IZU2022
Dependencies: mbed mpu9250_i2c IM920 BMP180 GPS millis
main.cpp@5:b0bb11afabfd, 2016-11-22 (annotated)
- Committer:
- ryood
- Date:
- Tue Nov 22 11:25:54 2016 +0000
- Revision:
- 5:b0bb11afabfd
- Parent:
- 4:756dc42397cb
Test SDFileSystem & MCP23S17 with the same SPI bus.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:effffaacd967 | 1 | /* |
ryood | 0:effffaacd967 | 2 | * SDFileSystem Binary R/W Test |
ryood | 4:756dc42397cb | 3 | * |
ryood | 0:effffaacd967 | 4 | * Library |
ryood | 0:effffaacd967 | 5 | * SDFileSystem: https://developer.mbed.org/users/neilt6/code/SDFileSystem/ Revision:26 |
ryood | 0:effffaacd967 | 6 | * mbed: Revision: 124 |
ryood | 2:3f286265fade | 7 | * mbed-rtos: Revision: 117 |
ryood | 0:effffaacd967 | 8 | * |
ryood | 0:effffaacd967 | 9 | * 2016.11.22 created |
ryood | 0:effffaacd967 | 10 | * |
ryood | 0:effffaacd967 | 11 | */ |
ryood | 4:756dc42397cb | 12 | |
ryood | 0:effffaacd967 | 13 | #include "mbed.h" |
ryood | 2:3f286265fade | 14 | #include "rtos.h" |
ryood | 0:effffaacd967 | 15 | #include "SDFileSystem.h" |
ryood | 0:effffaacd967 | 16 | |
ryood | 4:756dc42397cb | 17 | #include "ExioMcp23s17.h" |
ryood | 4:756dc42397cb | 18 | #include "ExioInBuffer.h" |
ryood | 4:756dc42397cb | 19 | #include "ExioBufferedIn.h" |
ryood | 4:756dc42397cb | 20 | #include "ExioBufferedDebounceIn.h" |
ryood | 0:effffaacd967 | 21 | |
ryood | 2:3f286265fade | 22 | typedef struct { |
ryood | 0:effffaacd967 | 23 | uint8_t x; |
ryood | 0:effffaacd967 | 24 | uint8_t y; |
ryood | 0:effffaacd967 | 25 | uint8_t z; |
ryood | 2:3f286265fade | 26 | } DataT; |
ryood | 2:3f286265fade | 27 | |
ryood | 4:756dc42397cb | 28 | //SPI Spi(PC_12, PC_11, PC_10); // SPI3: mosi, miso, sclk |
ryood | 4:756dc42397cb | 29 | SDFileSystem sd(PC_12, PC_11, PC_10, PA_14, "sd"); // SPI3: mosi, miso, sclk, cs |
ryood | 4:756dc42397cb | 30 | |
ryood | 2:3f286265fade | 31 | void writeSD(DataT* data) |
ryood | 0:effffaacd967 | 32 | { |
ryood | 0:effffaacd967 | 33 | //Mount the filesystem |
ryood | 0:effffaacd967 | 34 | sd.mount(); |
ryood | 4:756dc42397cb | 35 | |
ryood | 0:effffaacd967 | 36 | //Perform a write test |
ryood | 4:756dc42397cb | 37 | printf("\r\nWriting binary data to SD card..."); |
ryood | 2:3f286265fade | 38 | FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); |
ryood | 4:756dc42397cb | 39 | if (file != NULL) { |
ryood | 4:756dc42397cb | 40 | if (file->write(data, sizeof(*data)) != sizeof(*data)) { |
ryood | 4:756dc42397cb | 41 | error("write error!\r\n"); |
ryood | 0:effffaacd967 | 42 | } |
ryood | 4:756dc42397cb | 43 | if (file->close()) { |
ryood | 4:756dc42397cb | 44 | printf("failed to close file!\r\n"); |
ryood | 4:756dc42397cb | 45 | } else { |
ryood | 4:756dc42397cb | 46 | printf("done!\r\n"); |
ryood | 0:effffaacd967 | 47 | } |
ryood | 4:756dc42397cb | 48 | } else { |
ryood | 4:756dc42397cb | 49 | printf("failed to create file!\r\n"); |
ryood | 0:effffaacd967 | 50 | } |
ryood | 4:756dc42397cb | 51 | |
ryood | 2:3f286265fade | 52 | //Unmount the filesystem |
ryood | 2:3f286265fade | 53 | sd.unmount(); |
ryood | 2:3f286265fade | 54 | } |
ryood | 2:3f286265fade | 55 | |
ryood | 2:3f286265fade | 56 | void readSD(DataT* data) |
ryood | 2:3f286265fade | 57 | { |
ryood | 2:3f286265fade | 58 | //Mount the filesystem |
ryood | 2:3f286265fade | 59 | sd.mount(); |
ryood | 4:756dc42397cb | 60 | |
ryood | 0:effffaacd967 | 61 | //Perform a read test |
ryood | 4:756dc42397cb | 62 | printf("\r\nReading binary data from SD card..."); |
ryood | 2:3f286265fade | 63 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); |
ryood | 4:756dc42397cb | 64 | if (file != NULL) { |
ryood | 4:756dc42397cb | 65 | if (file->read(data, sizeof(*data)) != sizeof(*data)) { |
ryood | 4:756dc42397cb | 66 | error("read error!\r\n"); |
ryood | 0:effffaacd967 | 67 | } |
ryood | 4:756dc42397cb | 68 | if (file->close()) { |
ryood | 4:756dc42397cb | 69 | printf("failed to close file!\r\n"); |
ryood | 4:756dc42397cb | 70 | } else { |
ryood | 4:756dc42397cb | 71 | printf("done!\r\n"); |
ryood | 0:effffaacd967 | 72 | } |
ryood | 4:756dc42397cb | 73 | } else { |
ryood | 4:756dc42397cb | 74 | printf("failed to open file!\r\n"); |
ryood | 0:effffaacd967 | 75 | } |
ryood | 4:756dc42397cb | 76 | |
ryood | 0:effffaacd967 | 77 | //Unmount the filesystem |
ryood | 0:effffaacd967 | 78 | sd.unmount(); |
ryood | 2:3f286265fade | 79 | } |
ryood | 4:756dc42397cb | 80 | |
ryood | 2:3f286265fade | 81 | int main() |
ryood | 2:3f286265fade | 82 | { |
ryood | 4:756dc42397cb | 83 | DataT data, rdata; |
ryood | 4:756dc42397cb | 84 | |
ryood | 2:3f286265fade | 85 | data.x = 0xff; |
ryood | 2:3f286265fade | 86 | data.y = 0x55; |
ryood | 2:3f286265fade | 87 | data.z = 0xaa; |
ryood | 4:756dc42397cb | 88 | |
ryood | 4:756dc42397cb | 89 | printf("*** Test SDFileSystem & ExioBufferedDebounceIn ***\r\n"); |
ryood | 2:3f286265fade | 90 | |
ryood | 4:756dc42397cb | 91 | // ExioMcp23s17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset); |
ryood | 4:756dc42397cb | 92 | ExioMcp23s17 Exio(0x00, *sd.SpiPointer(), PD_2, PA_13); |
ryood | 4:756dc42397cb | 93 | |
ryood | 4:756dc42397cb | 94 | // Reset MCP23S17 (初期化時にreset()が必要) |
ryood | 4:756dc42397cb | 95 | Exio.reset(); |
ryood | 4:756dc42397cb | 96 | |
ryood | 4:756dc42397cb | 97 | ExioInBuffer inBufferB(&Exio, ExioPortB); |
ryood | 4:756dc42397cb | 98 | ExioBufferedDebounceIn inB[] = { |
ryood | 4:756dc42397cb | 99 | ExioBufferedDebounceIn(&inBufferB, 0), |
ryood | 4:756dc42397cb | 100 | ExioBufferedDebounceIn(&inBufferB, 1), |
ryood | 4:756dc42397cb | 101 | ExioBufferedDebounceIn(&inBufferB, 2), |
ryood | 4:756dc42397cb | 102 | ExioBufferedDebounceIn(&inBufferB, 3), |
ryood | 4:756dc42397cb | 103 | ExioBufferedDebounceIn(&inBufferB, 4), |
ryood | 4:756dc42397cb | 104 | ExioBufferedDebounceIn(&inBufferB, 5), |
ryood | 4:756dc42397cb | 105 | ExioBufferedDebounceIn(&inBufferB, 6), |
ryood | 4:756dc42397cb | 106 | ExioBufferedDebounceIn(&inBufferB, 7) |
ryood | 4:756dc42397cb | 107 | }; |
ryood | 4:756dc42397cb | 108 | |
ryood | 4:756dc42397cb | 109 | // Start Timers |
ryood | 4:756dc42397cb | 110 | inBufferB.run(10); |
ryood | 4:756dc42397cb | 111 | for (int i = 0; i < 8; i++) { |
ryood | 4:756dc42397cb | 112 | inB[i].set_debounce_us(10000); |
ryood | 4:756dc42397cb | 113 | } |
ryood | 4:756dc42397cb | 114 | |
ryood | 4:756dc42397cb | 115 | // SDカードの読み書きの前にExioのSPIアクセスを停止 |
ryood | 4:756dc42397cb | 116 | inBufferB.stop(); |
ryood | 4:756dc42397cb | 117 | |
ryood | 2:3f286265fade | 118 | writeSD(&data); |
ryood | 2:3f286265fade | 119 | readSD(&rdata); |
ryood | 2:3f286265fade | 120 | |
ryood | 4:756dc42397cb | 121 | // ExioのSPIアクセスを再開 |
ryood | 4:756dc42397cb | 122 | inBufferB.run(10); |
ryood | 4:756dc42397cb | 123 | |
ryood | 4:756dc42397cb | 124 | printf("data: x:%02x y:%02x z:%02x\r\n", rdata.x, rdata.y, rdata.z); |
ryood | 4:756dc42397cb | 125 | |
ryood | 4:756dc42397cb | 126 | while(1) { |
ryood | 4:756dc42397cb | 127 | uint8_t x = 0; |
ryood | 4:756dc42397cb | 128 | for (int i = 0; i < 8; i++) { |
ryood | 4:756dc42397cb | 129 | int vb = inB[i].read(); |
ryood | 4:756dc42397cb | 130 | x |= (vb << i); |
ryood | 4:756dc42397cb | 131 | } |
ryood | 4:756dc42397cb | 132 | if (x != 0 && data.x != x) { |
ryood | 4:756dc42397cb | 133 | data.x = x; |
ryood | 4:756dc42397cb | 134 | printf("Write to SD: x:%02x y:%02x z:%02x\r\n", data.x, data.y, data.z); |
ryood | 4:756dc42397cb | 135 | // SDカードの読み書きの前にExioのSPIアクセスを停止 |
ryood | 4:756dc42397cb | 136 | inBufferB.stop(); |
ryood | 4:756dc42397cb | 137 | writeSD(&data); |
ryood | 4:756dc42397cb | 138 | readSD(&rdata); |
ryood | 4:756dc42397cb | 139 | // ExioのSPIアクセスを再開 |
ryood | 4:756dc42397cb | 140 | inBufferB.run(10); |
ryood | 4:756dc42397cb | 141 | printf("Read from SD: x:%02x y:%02x z:%02x\r\n", rdata.x, rdata.y, rdata.z); |
ryood | 4:756dc42397cb | 142 | } |
ryood | 4:756dc42397cb | 143 | } |
ryood | 2:3f286265fade | 144 | } |