Test SDFileSystem & MCP23S17 with the same SPI bus.
Dependencies: ExioBufferdController MCP23S17 SDFileSystem mbed-rtos mbed
main.cpp
- Committer:
- ryood
- Date:
- 2016-11-22
- Revision:
- 5:b0bb11afabfd
- Parent:
- 4:756dc42397cb
File content as of revision 5:b0bb11afabfd:
/* * SDFileSystem Binary R/W Test * * Library * SDFileSystem: https://developer.mbed.org/users/neilt6/code/SDFileSystem/ Revision:26 * mbed: Revision: 124 * mbed-rtos: Revision: 117 * * 2016.11.22 created * */ #include "mbed.h" #include "rtos.h" #include "SDFileSystem.h" #include "ExioMcp23s17.h" #include "ExioInBuffer.h" #include "ExioBufferedIn.h" #include "ExioBufferedDebounceIn.h" typedef struct { uint8_t x; uint8_t y; uint8_t z; } DataT; //SPI Spi(PC_12, PC_11, PC_10); // SPI3: mosi, miso, sclk SDFileSystem sd(PC_12, PC_11, PC_10, PA_14, "sd"); // SPI3: mosi, miso, sclk, cs void writeSD(DataT* data) { //Mount the filesystem sd.mount(); //Perform a write test printf("\r\nWriting binary data to SD card..."); FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); if (file != NULL) { if (file->write(data, sizeof(*data)) != sizeof(*data)) { error("write error!\r\n"); } if (file->close()) { printf("failed to close file!\r\n"); } else { printf("done!\r\n"); } } else { printf("failed to create file!\r\n"); } //Unmount the filesystem sd.unmount(); } void readSD(DataT* data) { //Mount the filesystem sd.mount(); //Perform a read test printf("\r\nReading binary data from SD card..."); FileHandle* file = sd.open("Test File.bin", O_RDONLY); if (file != NULL) { if (file->read(data, sizeof(*data)) != sizeof(*data)) { error("read error!\r\n"); } if (file->close()) { printf("failed to close file!\r\n"); } else { printf("done!\r\n"); } } else { printf("failed to open file!\r\n"); } //Unmount the filesystem sd.unmount(); } int main() { DataT data, rdata; data.x = 0xff; data.y = 0x55; data.z = 0xaa; printf("*** Test SDFileSystem & ExioBufferedDebounceIn ***\r\n"); // ExioMcp23s17(int hardwareaddress, SPI& spi, PinName nCs, PinName nReset); ExioMcp23s17 Exio(0x00, *sd.SpiPointer(), PD_2, PA_13); // Reset MCP23S17 (初期化時にreset()が必要) Exio.reset(); ExioInBuffer inBufferB(&Exio, ExioPortB); ExioBufferedDebounceIn inB[] = { ExioBufferedDebounceIn(&inBufferB, 0), ExioBufferedDebounceIn(&inBufferB, 1), ExioBufferedDebounceIn(&inBufferB, 2), ExioBufferedDebounceIn(&inBufferB, 3), ExioBufferedDebounceIn(&inBufferB, 4), ExioBufferedDebounceIn(&inBufferB, 5), ExioBufferedDebounceIn(&inBufferB, 6), ExioBufferedDebounceIn(&inBufferB, 7) }; // Start Timers inBufferB.run(10); for (int i = 0; i < 8; i++) { inB[i].set_debounce_us(10000); } // SDカードの読み書きの前にExioのSPIアクセスを停止 inBufferB.stop(); writeSD(&data); readSD(&rdata); // ExioのSPIアクセスを再開 inBufferB.run(10); printf("data: x:%02x y:%02x z:%02x\r\n", rdata.x, rdata.y, rdata.z); while(1) { uint8_t x = 0; for (int i = 0; i < 8; i++) { int vb = inB[i].read(); x |= (vb << i); } if (x != 0 && data.x != x) { data.x = x; printf("Write to SD: x:%02x y:%02x z:%02x\r\n", data.x, data.y, data.z); // SDカードの読み書きの前にExioのSPIアクセスを停止 inBufferB.stop(); writeSD(&data); readSD(&rdata); // ExioのSPIアクセスを再開 inBufferB.run(10); printf("Read from SD: x:%02x y:%02x z:%02x\r\n", rdata.x, rdata.y, rdata.z); } } }