|
| 1 | +#include "mbed.h" |
| 2 | +#include "SDBlockDevice.h" |
| 3 | + |
| 4 | +// Instantiate the SDBlockDevice by specifying the SPI pins connected to the SDCard |
| 5 | +// socket. The PINS are: |
| 6 | +// MOSI (Master Out Slave In) |
| 7 | +// MISO (Master In Slave Out) |
| 8 | +// SCLK (Serial Clock) |
| 9 | +// CS (Chip Select) |
| 10 | +SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); |
| 11 | +uint8_t block[512] = "Hello World!\n"; |
| 12 | + |
| 13 | +int main() |
| 14 | +{ |
| 15 | + // Call the SDBlockDevice instance initialisation method |
| 16 | + if ( 0 != sd.init()) { |
| 17 | + printf("Init failed \n"); |
| 18 | + return -1; |
| 19 | + } |
| 20 | + printf("sd size: %llu\n", sd.size()); |
| 21 | + printf("sd read size: %llu\n", sd.get_read_size()); |
| 22 | + printf("sd program size: %llu\n", sd.get_program_size()); |
| 23 | + printf("sd erase size: %llu\n", sd.get_erase_size()); |
| 24 | + |
| 25 | + // Set the frequency |
| 26 | + if ( 0 != sd.frequency(5000000)) { |
| 27 | + printf("Error setting frequency \n"); |
| 28 | + } |
| 29 | + |
| 30 | + if ( 0 != sd.erase(0, sd.get_erase_size())) { |
| 31 | + printf("Error Erasing block \n"); |
| 32 | + } |
| 33 | + |
| 34 | + // Write data block to the device |
| 35 | + if ( 0 == sd.program(block, 0, 512)) { |
| 36 | + // Read the data block from the device |
| 37 | + if ( 0 == sd.read(block, 0, 512)) { |
| 38 | + // Print the contents of the block |
| 39 | + printf("%s", block); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Call the SDBlockDevice instance de-initialisation method |
| 44 | + sd.deinit(); |
| 45 | +} |
0 commit comments