![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Adds POST test for BMP280 sensor board. Includes driver
Dependencies: BMP280 ELEC350-Practicals-FZ429 BME280
Fork of Task690-mbed-os-FZ429ZI by
main.cpp@6:4b449e2d4ad1, 2019-11-22 (annotated)
- Committer:
- noutram
- Date:
- Fri Nov 22 15:58:42 2019 +0000
- Revision:
- 6:4b449e2d4ad1
- Parent:
- 5:0fe69963f1dd
2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martinsimpson | 0:1ce5a958aaf8 | 1 | /* Access an SD Card using SPI */ |
martinsimpson | 0:1ce5a958aaf8 | 2 | |
martinsimpson | 0:1ce5a958aaf8 | 3 | #include "mbed.h" |
martinsimpson | 0:1ce5a958aaf8 | 4 | #include "SDBlockDevice.h" |
martinsimpson | 0:1ce5a958aaf8 | 5 | #include "FATFileSystem.h" |
noutram | 2:fad34c30dcc4 | 6 | #include "sample_hardware.hpp" |
martinsimpson | 0:1ce5a958aaf8 | 7 | |
noutram | 2:fad34c30dcc4 | 8 | //SD Card Object |
noutram | 5:0fe69963f1dd | 9 | SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs |
martinsimpson | 0:1ce5a958aaf8 | 10 | |
martinsimpson | 0:1ce5a958aaf8 | 11 | uint8_t block[512] = "Hello World!\n"; |
martinsimpson | 0:1ce5a958aaf8 | 12 | int main() |
martinsimpson | 0:1ce5a958aaf8 | 13 | { |
noutram | 2:fad34c30dcc4 | 14 | //POWER ON SELF TEST |
noutram | 2:fad34c30dcc4 | 15 | post(); |
noutram | 2:fad34c30dcc4 | 16 | |
noutram | 2:fad34c30dcc4 | 17 | printf("Initialise\n"); |
martinsimpson | 0:1ce5a958aaf8 | 18 | //FileSystemLike(*sd); |
martinsimpson | 0:1ce5a958aaf8 | 19 | |
martinsimpson | 0:1ce5a958aaf8 | 20 | // call the SDBlockDevice instance initialisation method. |
noutram | 2:fad34c30dcc4 | 21 | if ( sd.init() != 0) { |
martinsimpson | 0:1ce5a958aaf8 | 22 | printf("Init failed \n"); |
noutram | 2:fad34c30dcc4 | 23 | errorCode(FATAL); |
noutram | 2:fad34c30dcc4 | 24 | } |
martinsimpson | 0:1ce5a958aaf8 | 25 | |
noutram | 2:fad34c30dcc4 | 26 | //Create a filing system for SD Card |
martinsimpson | 0:1ce5a958aaf8 | 27 | FATFileSystem fs("sd", &sd); |
martinsimpson | 0:1ce5a958aaf8 | 28 | |
noutram | 2:fad34c30dcc4 | 29 | // ************* |
noutram | 2:fad34c30dcc4 | 30 | // Open to WRITE |
noutram | 2:fad34c30dcc4 | 31 | // ************* |
noutram | 2:fad34c30dcc4 | 32 | printf("Write to a file\n"); |
noutram | 3:22e5460a8b42 | 33 | FILE* fp = fopen("/sd/test.txt","a"); |
noutram | 2:fad34c30dcc4 | 34 | //Check file handle (stream) |
noutram | 2:fad34c30dcc4 | 35 | if (fp == NULL) { |
martinsimpson | 0:1ce5a958aaf8 | 36 | error("Could not open file for write\n"); |
noutram | 2:fad34c30dcc4 | 37 | errorCode(FATAL); |
martinsimpson | 0:1ce5a958aaf8 | 38 | } |
martinsimpson | 0:1ce5a958aaf8 | 39 | |
martinsimpson | 0:1ce5a958aaf8 | 40 | //Put some text in the file... |
noutram | 2:fad34c30dcc4 | 41 | fprintf(fp, "Welcome to ELEC350\n"); |
noutram | 2:fad34c30dcc4 | 42 | |
noutram | 2:fad34c30dcc4 | 43 | //Close the file |
noutram | 2:fad34c30dcc4 | 44 | fclose(fp); |
martinsimpson | 0:1ce5a958aaf8 | 45 | |
noutram | 2:fad34c30dcc4 | 46 | // ************ |
noutram | 2:fad34c30dcc4 | 47 | // Open to READ |
noutram | 2:fad34c30dcc4 | 48 | // ************ |
noutram | 2:fad34c30dcc4 | 49 | printf("Read a file\n"); |
noutram | 2:fad34c30dcc4 | 50 | fp = fopen("/sd/test.txt","r"); |
noutram | 2:fad34c30dcc4 | 51 | if (fp == NULL) { |
noutram | 2:fad34c30dcc4 | 52 | error("Could not open file for read\n"); |
noutram | 2:fad34c30dcc4 | 53 | errorCode(FATAL); |
noutram | 2:fad34c30dcc4 | 54 | } |
noutram | 2:fad34c30dcc4 | 55 | |
noutram | 3:22e5460a8b42 | 56 | //Read back all strings |
noutram | 3:22e5460a8b42 | 57 | char s1[64]; |
noutram | 3:22e5460a8b42 | 58 | while (fscanf(fp, "%s", s1) == 1) { |
noutram | 3:22e5460a8b42 | 59 | printf("READ BACK: %s\n", s1); |
noutram | 3:22e5460a8b42 | 60 | } |
noutram | 2:fad34c30dcc4 | 61 | //To read a whole line, use: fgets(s1, sizeof(s1), fp); |
noutram | 3:22e5460a8b42 | 62 | |
noutram | 2:fad34c30dcc4 | 63 | |
noutram | 2:fad34c30dcc4 | 64 | //Close File |
martinsimpson | 0:1ce5a958aaf8 | 65 | fclose(fp); |
noutram | 2:fad34c30dcc4 | 66 | |
noutram | 2:fad34c30dcc4 | 67 | //Close down |
martinsimpson | 0:1ce5a958aaf8 | 68 | sd.deinit(); |
martinsimpson | 0:1ce5a958aaf8 | 69 | printf("All done...\n"); |
noutram | 2:fad34c30dcc4 | 70 | errorCode(OK); |
martinsimpson | 0:1ce5a958aaf8 | 71 | |
noutram | 2:fad34c30dcc4 | 72 | //Flash to indicate goodness |
noutram | 2:fad34c30dcc4 | 73 | while(true) { |
noutram | 2:fad34c30dcc4 | 74 | greenLED = 1; |
noutram | 2:fad34c30dcc4 | 75 | wait(0.5); |
noutram | 2:fad34c30dcc4 | 76 | greenLED = 0; |
noutram | 2:fad34c30dcc4 | 77 | wait(0.1); |
noutram | 2:fad34c30dcc4 | 78 | } |
noutram | 2:fad34c30dcc4 | 79 | } |