Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MPU9250_SPI
Fork of WearableDevice_Nucleo by
main.cpp@19:0a3ae902722e, 2017-10-18 (annotated)
- Committer:
- Muglug
- Date:
- Wed Oct 18 15:27:31 2017 +0000
- Revision:
- 19:0a3ae902722e
- Parent:
- 18:7f9c2b8541e1
- Child:
- 20:197c69b38adf
SD Card Testing added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gusteibolt | 1:a6dc717fb060 | 1 | /* |
gusteibolt | 1:a6dc717fb060 | 2 | * Los Putacos |
gusteibolt | 1:a6dc717fb060 | 3 | * Copyright (C) 2017, All rights reserved. |
Muglug | 18:7f9c2b8541e1 | 4 | * ________________________________________ |
gusteibolt | 1:a6dc717fb060 | 5 | * |
gusteibolt | 1:a6dc717fb060 | 6 | * Created by: Gustavo Campana, Michael Schmidt, Miguel Lopez |
gusteibolt | 1:a6dc717fb060 | 7 | * Date: 11-Oct-2017 |
gusteibolt | 1:a6dc717fb060 | 8 | * Version: V0.1 |
gusteibolt | 1:a6dc717fb060 | 9 | */ |
gusteibolt | 1:a6dc717fb060 | 10 | |
gusteibolt | 1:a6dc717fb060 | 11 | //----------------------------------------------------------------- |
gusteibolt | 1:a6dc717fb060 | 12 | // Board: NUCLEO - F401RE |
gusteibolt | 1:a6dc717fb060 | 13 | // Version: MR1136 rev C |
gusteibolt | 1:a6dc717fb060 | 14 | //----------------------------------------------------------------- |
gusteibolt | 1:a6dc717fb060 | 15 | |
gusteibolt | 1:a6dc717fb060 | 16 | //----------------------------------------------------------------- |
gusteibolt | 1:a6dc717fb060 | 17 | // Includes |
gusteibolt | 0:73cd0cb02330 | 18 | #include "mbed.h" |
gusteibolt | 0:73cd0cb02330 | 19 | #include "rtos.h" |
gusteibolt | 3:26aeff25f610 | 20 | #include "mbed_events.h" |
gusteibolt | 3:26aeff25f610 | 21 | |
gusteibolt | 3:26aeff25f610 | 22 | #include "configuration.h" |
gusteibolt | 10:577e1fc4453e | 23 | #include "event.h" |
gusteibolt | 7:ebaaff27840b | 24 | #include "pin.h" |
gusteibolt | 3:26aeff25f610 | 25 | #include "MPU9250.h" |
Muglug | 17:f98597cd2efc | 26 | |
Muglug | 17:f98597cd2efc | 27 | #include "XBeeLib.h" |
Muglug | 19:0a3ae902722e | 28 | #include "SDFileSystem.h" |
gusteibolt | 1:a6dc717fb060 | 29 | //----------------------------------------------------------------- |
gusteibolt | 0:73cd0cb02330 | 30 | |
gusteibolt | 1:a6dc717fb060 | 31 | //----------------------------------------------------------------- |
gusteibolt | 1:a6dc717fb060 | 32 | // Declarations |
Muglug | 18:7f9c2b8541e1 | 33 | Timer time_stamp; //Timer µS time-stamp |
Muglug | 18:7f9c2b8541e1 | 34 | EventQueue queue(32 * EVENTS_EVENT_SIZE); //Event |
gusteibolt | 3:26aeff25f610 | 35 | |
Muglug | 18:7f9c2b8541e1 | 36 | Serial PC(USBTX, USBRX); // PC Serial (Debug) |
Muglug | 18:7f9c2b8541e1 | 37 | // Serial XBeeee(PA_11, PA_12); // Xbee |
gusteibolt | 3:26aeff25f610 | 38 | |
Muglug | 18:7f9c2b8541e1 | 39 | // MPU9250 imu; // IMU MPU-9255 by Kris Winer |
gusteibolt | 1:a6dc717fb060 | 40 | //----------------------------------------------------------------- |
gusteibolt | 1:a6dc717fb060 | 41 | |
gusteibolt | 1:a6dc717fb060 | 42 | //----------------------------------------------------------------- |
gusteibolt | 1:a6dc717fb060 | 43 | // Global Variables |
gusteibolt | 3:26aeff25f610 | 44 | // Your code here! |
gusteibolt | 1:a6dc717fb060 | 45 | //----------------------------------------------------------------- |
gusteibolt | 10:577e1fc4453e | 46 | /* |
gusteibolt | 1:a6dc717fb060 | 47 | //----------------------------------------------------------------- |
gusteibolt | 8:ba93a973f967 | 48 | void print_event(void const *argv, int i) { |
gusteibolt | 10:577e1fc4453e | 49 | pc.printf("%d ",i); |
gusteibolt | 8:ba93a973f967 | 50 | pc.printf((const char*)argv); |
gusteibolt | 10:577e1fc4453e | 51 | pc.printf("\r\n"); |
gusteibolt | 8:ba93a973f967 | 52 | } |
gusteibolt | 8:ba93a973f967 | 53 | //----------------------------------------------------------------- |
gusteibolt | 10:577e1fc4453e | 54 | */ |
gusteibolt | 8:ba93a973f967 | 55 | //----------------------------------------------------------------- |
Muglug | 18:7f9c2b8541e1 | 56 | /*void blink_event(DigitalOut pin){ |
gusteibolt | 10:577e1fc4453e | 57 | pin = !pin; |
gusteibolt | 8:ba93a973f967 | 58 | } |
gusteibolt | 8:ba93a973f967 | 59 | //----------------------------------------------------------------- |
gusteibolt | 8:ba93a973f967 | 60 | |
gusteibolt | 8:ba93a973f967 | 61 | //----------------------------------------------------------------- |
gusteibolt | 8:ba93a973f967 | 62 | void println_event(){ |
gusteibolt | 8:ba93a973f967 | 63 | pc.printf("Arroz, feijao e batata\r\n"); |
gusteibolt | 8:ba93a973f967 | 64 | } |
gusteibolt | 8:ba93a973f967 | 65 | //----------------------------------------------------------------- |
Muglug | 18:7f9c2b8541e1 | 66 | */ |
gusteibolt | 8:ba93a973f967 | 67 | //----------------------------------------------------------------- |
Muglug | 18:7f9c2b8541e1 | 68 | void Setup() |
Muglug | 18:7f9c2b8541e1 | 69 | { |
gusteibolt | 3:26aeff25f610 | 70 | // PC Serial (Debug) baudrate |
Muglug | 18:7f9c2b8541e1 | 71 | PC.baud(9600); |
Muglug | 18:7f9c2b8541e1 | 72 | PC.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); |
Muglug | 18:7f9c2b8541e1 | 73 | |
Muglug | 18:7f9c2b8541e1 | 74 | // XBeeee.baud(9600); |
Muglug | 18:7f9c2b8541e1 | 75 | |
gusteibolt | 3:26aeff25f610 | 76 | // Task queue |
Muglug | 18:7f9c2b8541e1 | 77 | // int task1_id = queue.call_every(1000, printf, "called every 1 seconds\n"); |
Muglug | 18:7f9c2b8541e1 | 78 | // int task2_id = queue.call_every(2500, printf, "called every 2.5 seconds\n"); |
Muglug | 18:7f9c2b8541e1 | 79 | // int task3_id = queue.call_every(3000, &print_event, (void *)"called every 3 seconds\n", (int) time_stamp.read()); |
Muglug | 18:7f9c2b8541e1 | 80 | // int task4_id = queue.call_every(50, blink_event, led1); |
Muglug | 18:7f9c2b8541e1 | 81 | // int task5_id = queue.call_every(500, println_event); |
Muglug | 18:7f9c2b8541e1 | 82 | // int task6_id = queue.call_every(500, readIMU); |
Muglug | 18:7f9c2b8541e1 | 83 | // int task7_id = queue.call_every(500, gayEvent); |
gusteibolt | 0:73cd0cb02330 | 84 | } |
gusteibolt | 1:a6dc717fb060 | 85 | //----------------------------------------------------------------- |
gusteibolt | 0:73cd0cb02330 | 86 | |
gusteibolt | 1:a6dc717fb060 | 87 | //----------------------------------------------------------------- |
Muglug | 18:7f9c2b8541e1 | 88 | /*void initMPU9250() |
Muglug | 18:7f9c2b8541e1 | 89 | { |
Muglug | 18:7f9c2b8541e1 | 90 | // Initialize MPU9250 device |
Muglug | 18:7f9c2b8541e1 | 91 | // wake up device |
Muglug | 18:7f9c2b8541e1 | 92 | imu.writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors |
Muglug | 18:7f9c2b8541e1 | 93 | }*/ |
Muglug | 18:7f9c2b8541e1 | 94 | |
Mister_Lopez | 4:56e903769e94 | 95 | //----------------------------------------------------------------- |
Mister_Lopez | 4:56e903769e94 | 96 | |
Muglug | 17:f98597cd2efc | 97 | using namespace XBeeLib; |
Muglug | 17:f98597cd2efc | 98 | |
Muglug | 18:7f9c2b8541e1 | 99 | /** Callback function, invoked at packet reception */ |
Muglug | 18:7f9c2b8541e1 | 100 | static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len) |
Muglug | 19:0a3ae902722e | 101 | { |
Muglug | 18:7f9c2b8541e1 | 102 | for (int i = 0; i < len; i++) |
Muglug | 18:7f9c2b8541e1 | 103 | PC.printf("%c", data[i]); |
Muglug | 19:0a3ae902722e | 104 | |
Muglug | 18:7f9c2b8541e1 | 105 | PC.printf("\r\n"); |
Muglug | 18:7f9c2b8541e1 | 106 | } |
Muglug | 19:0a3ae902722e | 107 | // Create an SDFileSystem object |
Muglug | 19:0a3ae902722e | 108 | SDFileSystem SD(SD_MOSI, SD_MISO, SD_CLK, SD_CS, "SD"); |
Muglug | 18:7f9c2b8541e1 | 109 | |
Mister_Lopez | 4:56e903769e94 | 110 | //----------------------------------------------------------------- |
gusteibolt | 0:73cd0cb02330 | 111 | int main() |
gusteibolt | 0:73cd0cb02330 | 112 | { |
Muglug | 18:7f9c2b8541e1 | 113 | Setup(); // Initial Setups |
Muglug | 18:7f9c2b8541e1 | 114 | const uint8_t data[] = "Hello Master!"; |
Muglug | 18:7f9c2b8541e1 | 115 | const uint16_t data_len = sizeof data / sizeof data[0] - 1; |
Muglug | 18:7f9c2b8541e1 | 116 | |
Muglug | 18:7f9c2b8541e1 | 117 | XBeeZB XBee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 115200); |
Muglug | 18:7f9c2b8541e1 | 118 | XBee.init(); |
Muglug | 19:0a3ae902722e | 119 | |
Muglug | 18:7f9c2b8541e1 | 120 | /* Register callbacks */ |
Muglug | 18:7f9c2b8541e1 | 121 | XBee.register_receive_cb(&receive_cb); |
Muglug | 19:0a3ae902722e | 122 | time_stamp.start(); // Start Timer |
Muglug | 18:7f9c2b8541e1 | 123 | |
Muglug | 19:0a3ae902722e | 124 | // Mount the filesystem |
Muglug | 19:0a3ae902722e | 125 | SD.mount(); |
Muglug | 19:0a3ae902722e | 126 | |
Muglug | 19:0a3ae902722e | 127 | if (SD.card_present()) |
Muglug | 19:0a3ae902722e | 128 | PC.printf("Good"); |
Muglug | 19:0a3ae902722e | 129 | else |
Muglug | 19:0a3ae902722e | 130 | PC.printf("Meh"); |
Muglug | 18:7f9c2b8541e1 | 131 | |
Muglug | 18:7f9c2b8541e1 | 132 | // Reading the analog input (Microphone) |
Muglug | 18:7f9c2b8541e1 | 133 | // array = micro.read_u16(); |
Muglug | 18:7f9c2b8541e1 | 134 | |
Muglug | 19:0a3ae902722e | 135 | // Performing Write Test |
Muglug | 19:0a3ae902722e | 136 | PC.printf("\nWriting to SD card..."); |
Muglug | 19:0a3ae902722e | 137 | FILE *fp = fopen("sdtest.txt", "w"); |
Muglug | 19:0a3ae902722e | 138 | if (fp != NULL) { |
Muglug | 19:0a3ae902722e | 139 | fclose(fp); |
Muglug | 19:0a3ae902722e | 140 | PC.printf(" Success!\n"); |
Muglug | 19:0a3ae902722e | 141 | } else { |
Muglug | 19:0a3ae902722e | 142 | PC.printf(" Failed!\n"); |
Muglug | 19:0a3ae902722e | 143 | } |
Muglug | 19:0a3ae902722e | 144 | |
Muglug | 19:0a3ae902722e | 145 | // Performing Read Test |
Muglug | 19:0a3ae902722e | 146 | PC.printf("Reading from SD card..."); |
Muglug | 19:0a3ae902722e | 147 | fp = fopen("/sd/sdtest.txt", "r"); |
Muglug | 19:0a3ae902722e | 148 | if (fp != NULL) { |
Muglug | 19:0a3ae902722e | 149 | char c = fgetc(fp); |
Muglug | 19:0a3ae902722e | 150 | if (c == 'W') |
Muglug | 19:0a3ae902722e | 151 | PC.printf(" Success!\n"); |
Muglug | 19:0a3ae902722e | 152 | else |
Muglug | 19:0a3ae902722e | 153 | PC.printf("Incorrect Character! (%c)!\n", c); |
Muglug | 19:0a3ae902722e | 154 | fclose(fp); |
Muglug | 19:0a3ae902722e | 155 | } else { |
Muglug | 19:0a3ae902722e | 156 | PC.printf(" Failed!\n"); |
Muglug | 19:0a3ae902722e | 157 | } |
Muglug | 18:7f9c2b8541e1 | 158 | |
Muglug | 18:7f9c2b8541e1 | 159 | led1 = 1; |
gusteibolt | 0:73cd0cb02330 | 160 | while (true) { |
Muglug | 18:7f9c2b8541e1 | 161 | // queue.dispatch(); |
Muglug | 18:7f9c2b8541e1 | 162 | XBee.process_rx_frames(); |
Muglug | 18:7f9c2b8541e1 | 163 | |
Muglug | 18:7f9c2b8541e1 | 164 | TxStatus txStatus = XBee.send_data_to_coordinator(data, data_len); |
Muglug | 18:7f9c2b8541e1 | 165 | if (txStatus != TxStatusSuccess) { |
Muglug | 18:7f9c2b8541e1 | 166 | PC.printf("send_data_broadcast() failed with error %d\n", (int)txStatus); |
Muglug | 18:7f9c2b8541e1 | 167 | } |
gusteibolt | 0:73cd0cb02330 | 168 | } |
Muglug | 19:0a3ae902722e | 169 | |
Muglug | 19:0a3ae902722e | 170 | // Unmount the Filesystem |
Muglug | 19:0a3ae902722e | 171 | SD.unmount(); |
gusteibolt | 0:73cd0cb02330 | 172 | } |
Muglug | 18:7f9c2b8541e1 | 173 | //----------------------------------------------------------------- |