20200716 read Status Register each second
Dependencies: SDFileSystem mbed-os-example-ble-GattServer max32630fthr
Diff: main.cpp
- Revision:
- 21:51e162c130a9
- Parent:
- 0:c671a4833315
diff -r 21b02b0b2cb2 -r 51e162c130a9 main.cpp --- a/main.cpp Fri Dec 14 13:15:33 2018 +0000 +++ b/main.cpp Thu Jul 16 14:59:04 2020 +0000 @@ -28,6 +28,18 @@ #include "ble/GattServer.h" #include "BLEProcess.h" +#include "max32630fthr.h" +#include "RpcServer.h" +#include "StringInOut.h" +#include "Peripherals.h" +#include "MAX30001.h" +#include "DataLoggingService.h" +#include "PushButton.h" +#include "Streaming.h" +#include "SDFileSystem.h" +#include "version.h" + + using mbed::callback; /** @@ -45,29 +57,35 @@ typedef ClockService Self; public: + uint8_t second; + uint8_t minute; + uint8_t hour; + ClockService() : - _hour_char("485f4145-52b9-4644-af1f-7a6b9322490f", 0), - _minute_char("0a924ca7-87cd-4699-a3bd-abdcd9cf126a", 0), - _second_char("8dd6a1b7-bc75-4741-8a26-264af75807de", 0), - _clock_service( + _ecg("485f4145-52b9-4644-af1f-7a6b9322490f", 0), + _rr_bpm("0a924ca7-87cd-4699-a3bd-abdcd9cf126a", 0), + _max30001_chr("8dd6a1b7-bc75-4741-8a26-264af75807de", 0), + _ecg_service( /* uuid */ "51311102-030e-485f-b122-f8f381aa84ed", - /* characteristics */ _clock_characteristics, - /* numCharacteristics */ sizeof(_clock_characteristics) / - sizeof(_clock_characteristics[0]) + /* characteristics */ _ecg_characteristics, + /* numCharacteristics */ sizeof(_ecg_characteristics) / + sizeof(_ecg_characteristics[0]) ), _server(NULL), + _event_queue(NULL) { // update internal pointers (value, descriptors and characteristics array) - _clock_characteristics[0] = &_hour_char; - _clock_characteristics[1] = &_minute_char; - _clock_characteristics[2] = &_second_char; + _ecg_characteristics[0] = &_ecg; + _ecg_characteristics[1] = &_rr_bpm; + _ecg_characteristics[2] = &_max30001_chr; // setup authorization handlers - _hour_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); - _minute_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); - _second_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); + _ecg.setWriteAuthorizationCallback(this, &Self::authorize_client_write); + _rr_bpm.setWriteAuthorizationCallback(this, &Self::authorize_client_write); + _max30001_chr.setWriteAuthorizationCallback(this, &Self::authorize_client_write); } + @@ -82,7 +100,7 @@ // register the service printf("Adding demo service\r\n"); - ble_error_t err = _server->addService(_clock_service); + ble_error_t err = _server->addService(_ecg_service); if (err) { printf("Error %u during demo service registration.\r\n", err); @@ -101,12 +119,12 @@ // print the handles printf("clock service registered\r\n"); - printf("service handle: %u\r\n", _clock_service.getHandle()); - printf("\thour characteristic value handle %u\r\n", _hour_char.getValueHandle()); - printf("\tminute characteristic value handle %u\r\n", _minute_char.getValueHandle()); - printf("\tsecond characteristic value handle %u\r\n", _second_char.getValueHandle()); + printf("service handle: %u\r\n", _ecg_service.getHandle()); + printf("\thour characteristic value handle %u\r\n", _ecg.getValueHandle()); + printf("\tminute characteristic value handle %u\r\n", _rr_bpm.getValueHandle()); + printf("\tsecond characteristic value handle %u\r\n", _max30001_chr.getValueHandle()); - _event_queue->call_every(1000 /* ms */, callback(this, &Self::increment_second)); + _event_queue->call_every(2000 /* ms */, callback(this, &Self::increment_second)); } private: @@ -127,12 +145,15 @@ printf("data written:\r\n"); printf("\tconnection handle: %u\r\n", e->connHandle); printf("\tattribute handle: %u", e->handle); - if (e->handle == _hour_char.getValueHandle()) { + if (e->handle == _ecg.getValueHandle()) { printf(" (hour characteristic)\r\n"); - } else if (e->handle == _minute_char.getValueHandle()) { + hour = e->data[0]; + } else if (e->handle == _rr_bpm.getValueHandle()) { printf(" (minute characteristic)\r\n"); - } else if (e->handle == _second_char.getValueHandle()) { + minute = e->data[0]; + } else if (e->handle == _max30001_chr.getValueHandle()) { printf(" (second characteristic)\r\n"); + second = e->data[0]; } else { printf("\r\n"); } @@ -156,11 +177,11 @@ printf("data read:\r\n"); printf("\tconnection handle: %u\r\n", e->connHandle); printf("\tattribute handle: %u", e->handle); - if (e->handle == _hour_char.getValueHandle()) { + if (e->handle == _ecg.getValueHandle()) { printf(" (hour characteristic)\r\n"); - } else if (e->handle == _minute_char.getValueHandle()) { + } else if (e->handle == _rr_bpm.getValueHandle()) { printf(" (minute characteristic)\r\n"); - } else if (e->handle == _second_char.getValueHandle()) { + } else if (e->handle == _max30001_chr.getValueHandle()) { printf(" (second characteristic)\r\n"); } else { printf("\r\n"); @@ -222,7 +243,7 @@ } if ((e->data[0] >= 60) || - ((e->data[0] >= 24) && (e->handle == _hour_char.getValueHandle()))) { + ((e->data[0] >= 24) && (e->handle == _ecg.getValueHandle()))) { printf("Error invalid data\r\n"); e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED; return; @@ -236,16 +257,29 @@ */ void increment_second(void) { - uint8_t second = 0; - ble_error_t err = _second_char.get(*_server, second); + + ble_error_t err = _max30001_chr.get(*_server, second); if (err) { printf("read of the second value returned error %u\r\n", err); return; } second = (second + 1) % 60; - - err = _second_char.set(*_server, second); + uint32_t* mamt; + //MAX30001::MAX30001_REG_map_t reg = (MAX30001_REG_map_t)0x01; + uint8_t addr = 0x01; //STATUS + printf("reg=%d ", addr); + int res = Peripherals::max30001()->max30001_reg_read(addr, mamt); + printf("MAMT=%d, %x\r\n", res, *mamt); + + /* printf("RPC"); + char reply1[128]; + // process the RPC string + RPC_call("/System/ReadVer", reply1); + //Send reply to debug port + printf(reply1);*/ + printf("%d-%d-%d\r\n",hour, minute, second); + err = _max30001_chr.set(*_server, second); if (err) { printf("write of the second value returned error %u\r\n", err); return; @@ -261,8 +295,8 @@ */ void increment_minute(void) { - uint8_t minute = 0; - ble_error_t err = _minute_char.get(*_server, minute); + + ble_error_t err = _rr_bpm.get(*_server, minute); if (err) { printf("read of the minute value returned error %u\r\n", err); return; @@ -270,7 +304,7 @@ minute = (minute + 1) % 60; - err = _minute_char.set(*_server, minute); + err = _rr_bpm.set(*_server, minute); if (err) { printf("write of the minute value returned error %u\r\n", err); return; @@ -286,8 +320,8 @@ */ void increment_hour(void) { - uint8_t hour = 0; - ble_error_t err = _hour_char.get(*_server, hour); + + ble_error_t err = _ecg.get(*_server, hour); if (err) { printf("read of the hour value returned error %u\r\n", err); return; @@ -295,7 +329,7 @@ hour = (hour + 1) % 24; - err = _hour_char.set(*_server, hour); + err = _ecg.set(*_server, hour); if (err) { printf("write of the hour value returned error %u\r\n", err); return; @@ -378,22 +412,143 @@ uint8_t _value; }; - ReadWriteNotifyIndicateCharacteristic<uint8_t> _hour_char; - ReadWriteNotifyIndicateCharacteristic<uint8_t> _minute_char; - ReadWriteNotifyIndicateCharacteristic<uint8_t> _second_char; + ReadWriteNotifyIndicateCharacteristic<uint8_t> _ecg; + ReadWriteNotifyIndicateCharacteristic<uint8_t> _rr_bpm; + ReadWriteNotifyIndicateCharacteristic<uint8_t> _max30001_chr; // list of the characteristics of the clock service - GattCharacteristic* _clock_characteristics[3]; + GattCharacteristic* _ecg_characteristics[3]; // demo service - GattService _clock_service; + GattService _ecg_service; GattServer* _server; events::EventQueue *_event_queue; }; + +//Init PMIC on FTHR board and set logic thresholds to 3.3V +MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); + +SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); // mosi, miso, sclk, cs + +//SD card insertion detection pin +DigitalIn SDDetect(P2_2, PullUp); + + +/// DigitalOut for CS +DigitalOut cs(P5_6); +/// SPI Master 2 with SPI0_SS for use with MAX30001 +SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // used by MAX30001 +/// SPI Master 1 +QuadSpiInterface quadSpiInterface(SPI1_MOSI, SPI1_MISO, SPI1_SCK, + SPI1_SS); // used by S25FS512 + /// External Flash +S25FS512 s25fs512(&quadSpiInterface); +/// ECG device +MAX30001 max30001(&spi, &cs); +InterruptIn max30001_InterruptB(P5_5); +InterruptIn max30001_Interrupt2B(P5_4); + + +/// HSP platform LED +HspLed hspLed(LED_RED); +/// Packet TimeStamp Timer, set for 1uS +Timer timestampTimer; +/// HSP Platform push button +PushButton pushButton(SW1); + +// local input state of the RPC +int inputState; +// RPC request buffer +char request[128]; +// RPC reply buffer +char reply[128]; int main() { + + + // display start banner + printf("Maxim Integrated mbed hSensor %d.%d.%d %02d/%02d/%02d\n", + VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, + VERSION_MONTH, VERSION_DAY, VERSION_SHORT_YEAR); + + // turn on red led + printf("Init HSPLED...\n"); + fflush(stdout); + hspLed.on(); + + // set NVIC priorities for GPIO to prevent priority inversion + printf("Init NVIC Priorities...\n"); + fflush(stdout); + NVIC_SetPriority(GPIO_P0_IRQn, 5); + NVIC_SetPriority(GPIO_P1_IRQn, 5); + NVIC_SetPriority(GPIO_P2_IRQn, 5); + NVIC_SetPriority(GPIO_P3_IRQn, 5); + NVIC_SetPriority(GPIO_P4_IRQn, 5); + NVIC_SetPriority(GPIO_P5_IRQn, 5); + NVIC_SetPriority(GPIO_P6_IRQn, 5); + // used by the MAX30001 + NVIC_SetPriority(SPIM2_IRQn, 0); + + // Be able to statically reference these devices anywhere in the application + Peripherals::setS25FS512(&s25fs512); + Peripherals::setTimestampTimer(×tampTimer); + Peripherals::setHspLed(&hspLed); + Peripherals::setPushButton(&pushButton); + Peripherals::setMAX30001(&max30001); + Peripherals::setSdFS(&sd); + Peripherals::setSDDetect(&SDDetect); + + // init the S25FS256 external flash device + printf("Init S25FS512...\n"); + fflush(stdout); + s25fs512.init(); + + // start blinking led1 + printf("Init HSPLED Blink...\n"); + fflush(stdout); + + // + // MAX30001 + // + printf("Init MAX30001 callbacks, interrupts...\n"); + fflush(stdout); + max30001_InterruptB.disable_irq(); + max30001_Interrupt2B.disable_irq(); + max30001_InterruptB.mode(PullUp); + max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler); + max30001_Interrupt2B.mode(PullUp); + max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler); + max30001_InterruptB.enable_irq(); + max30001_Interrupt2B.enable_irq(); + MAX30001_AllowInterrupts(1); + max30001.max30001_sw_rst(); // Do a software reset of the MAX30001 + max30001.max30001_INT_assignment(MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_enint_loc, en_eovf_loc, en_fstint_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_dcloffint_loc, en_bint_loc, en_bovf_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_bover_loc, en_bundr_loc, en_bcgmon_loc, + MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_pint_loc, en_povf_loc, en_pedge_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, // en_lonint_loc, en_rrint_loc, en_samp_loc, + MAX30001::MAX30001_INT_ODNR, MAX30001::MAX30001_INT_ODNR); // intb_Type, int2b_Type) + max30001.onDataAvailable(&StreamPacketUint32); + + // initialize the RPC server + printf("Init RPC Server...\n"); + fflush(stdout); + RPC_init(); + // initialize the logging service + printf("Init LoggingService...\n"); + fflush(stdout); + LoggingService_Init(); + // initialize the SD disk + sd.disk_initialize(); + + // start main loop + printf("Start main loop...\n"); + fflush(stdout); + + BLE &ble_interface = BLE::Instance(); + events::EventQueue event_queue; ClockService demo_service; BLEProcess ble_process(event_queue, ble_interface); @@ -405,6 +560,7 @@ ble_process.start(); // Process the event queue. + event_queue.dispatch_forever(); return 0;