1st working program
Dependencies: mbed-os_TYBLE16 BME280_SPI RX8025NB nRF51_Vdd MB85RSxx_SPI
see /users/kenjiArai/notebook/tyble16-module-as-mbed-os-5-board-mbedlization/
main.cpp@7:cedbf234a089, 2019-12-19 (annotated)
- Committer:
- kenjiArai
- Date:
- Thu Dec 19 01:43:46 2019 +0000
- Revision:
- 7:cedbf234a089
- Parent:
- 6:a3238e93f694
- Child:
- 8:7e42f8dc42a2
small modification
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 6:a3238e93f694 | 1 | /* |
kenjiArai | 6:a3238e93f694 | 2 | * Mbed Application program / TYBLE-16 Data logger |
kenjiArai | 6:a3238e93f694 | 3 | * |
kenjiArai | 6:a3238e93f694 | 4 | * Copyright (c) 2019 Kenji Arai / JH1PJL |
kenjiArai | 6:a3238e93f694 | 5 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 6:a3238e93f694 | 6 | * https://os.mbed.com/users/kenjiArai/ |
kenjiArai | 6:a3238e93f694 | 7 | * Created: December 14th, 2019 |
kenjiArai | 7:cedbf234a089 | 8 | * Revised: December 19th, 2019 |
kenjiArai | 6:a3238e93f694 | 9 | */ |
kenjiArai | 6:a3238e93f694 | 10 | |
kenjiArai | 6:a3238e93f694 | 11 | // Include -------------------------------------------------------------------- |
kenjiArai | 6:a3238e93f694 | 12 | #include "mbed.h" |
kenjiArai | 6:a3238e93f694 | 13 | #include "TYBLE16_BASE.h" |
kenjiArai | 6:a3238e93f694 | 14 | #include "dt_logger.h" |
kenjiArai | 6:a3238e93f694 | 15 | #include "nRF51_Vdd.h" |
kenjiArai | 6:a3238e93f694 | 16 | #include "RX8025NB.h" |
kenjiArai | 6:a3238e93f694 | 17 | #include "BME280_SPI.h" |
kenjiArai | 6:a3238e93f694 | 18 | #include "MB85RSxx_SPI.h" |
kenjiArai | 6:a3238e93f694 | 19 | |
kenjiArai | 6:a3238e93f694 | 20 | // Definition ----------------------------------------------------------------- |
kenjiArai | 6:a3238e93f694 | 21 | #define KEPP_SLEEP_TIME 1 // 0= continuos mode (every 10sec) |
kenjiArai | 7:cedbf234a089 | 22 | #define SLEEP_TIME 2 // Sleep 2 minutes |
kenjiArai | 7:cedbf234a089 | 23 | #define FRAM_ID (0x047f4803) // Fijitsu 2Mbits |
kenjiArai | 6:a3238e93f694 | 24 | |
kenjiArai | 6:a3238e93f694 | 25 | // Constructor ---------------------------------------------------------------- |
kenjiArai | 6:a3238e93f694 | 26 | DigitalOut bme280_pwr(D9, 1); // BME280 sensor power on |
kenjiArai | 6:a3238e93f694 | 27 | DigitalOut fram_pwr(D5, 1); // FRAM power on |
kenjiArai | 6:a3238e93f694 | 28 | InterruptIn rtc_irq(P0_2, PullUp); |
kenjiArai | 6:a3238e93f694 | 29 | Serial pc(USBTX, USBRX); |
kenjiArai | 6:a3238e93f694 | 30 | nRF51_Vdd vdd(3.6f, 1.8f, ONLY4VDD); |
kenjiArai | 6:a3238e93f694 | 31 | |
kenjiArai | 6:a3238e93f694 | 32 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 6:a3238e93f694 | 33 | uint8_t fram_id[4]; |
kenjiArai | 6:a3238e93f694 | 34 | bool fram_ready = false; |
kenjiArai | 6:a3238e93f694 | 35 | |
kenjiArai | 6:a3238e93f694 | 36 | // Global data |
kenjiArai | 6:a3238e93f694 | 37 | time_t log_sec; |
kenjiArai | 6:a3238e93f694 | 38 | float vcc_voltage; |
kenjiArai | 6:a3238e93f694 | 39 | float barometer; |
kenjiArai | 6:a3238e93f694 | 40 | float temperature; |
kenjiArai | 6:a3238e93f694 | 41 | float humidity; |
kenjiArai | 6:a3238e93f694 | 42 | |
kenjiArai | 6:a3238e93f694 | 43 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 6:a3238e93f694 | 44 | const char *const opngmsg0 = |
kenjiArai | 7:cedbf234a089 | 45 | "\r\n---------\r\nCompiled on " __DATE__ " " __TIME__ " (UTC)\r\n"; |
kenjiArai | 6:a3238e93f694 | 46 | const char *const opngmsg1 = |
kenjiArai | 7:cedbf234a089 | 47 | "Project: TYBLE16_simple_data_logger by Kenji Arai\r\n"; |
kenjiArai | 6:a3238e93f694 | 48 | |
kenjiArai | 6:a3238e93f694 | 49 | // Function prototypes -------------------------------------------------------- |
kenjiArai | 6:a3238e93f694 | 50 | static void rtc_interrupt(void); |
kenjiArai | 6:a3238e93f694 | 51 | static void goto_standby(void); |
kenjiArai | 6:a3238e93f694 | 52 | time_t w_check_rtc_time(RX8025 &ex_rtc); |
kenjiArai | 6:a3238e93f694 | 53 | |
kenjiArai | 6:a3238e93f694 | 54 | //------------------------------------------------------------------------------ |
kenjiArai | 6:a3238e93f694 | 55 | // Control Program |
kenjiArai | 6:a3238e93f694 | 56 | //------------------------------------------------------------------------------ |
kenjiArai | 6:a3238e93f694 | 57 | int main() |
kenjiArai | 6:a3238e93f694 | 58 | { |
kenjiArai | 6:a3238e93f694 | 59 | char buf[64]; // data buffer for text |
kenjiArai | 6:a3238e93f694 | 60 | time_t seconds; |
kenjiArai | 6:a3238e93f694 | 61 | |
kenjiArai | 7:cedbf234a089 | 62 | ThisThread::sleep_for(100); |
kenjiArai | 6:a3238e93f694 | 63 | pc.puts(opngmsg0); |
kenjiArai | 6:a3238e93f694 | 64 | pc.puts(opngmsg1); |
kenjiArai | 6:a3238e93f694 | 65 | // Check TYBLE-16 configuration |
kenjiArai | 6:a3238e93f694 | 66 | cpu_sys(); |
kenjiArai | 6:a3238e93f694 | 67 | compile_condition(); |
kenjiArai | 6:a3238e93f694 | 68 | // Create constructor |
kenjiArai | 6:a3238e93f694 | 69 | // BM280 |
kenjiArai | 6:a3238e93f694 | 70 | BME280_SPI bme280(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_25); |
kenjiArai | 6:a3238e93f694 | 71 | // FRAM |
kenjiArai | 6:a3238e93f694 | 72 | MB85RSxx_SPI fram(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_6); |
kenjiArai | 6:a3238e93f694 | 73 | fram.read_device_id(fram_id); |
kenjiArai | 7:cedbf234a089 | 74 | uint32_t id = 0, num = 24; |
kenjiArai | 7:cedbf234a089 | 75 | for (uint32_t i = 0; i < 4; i++) { |
kenjiArai | 7:cedbf234a089 | 76 | id += fram_id[i] << num; |
kenjiArai | 7:cedbf234a089 | 77 | num -= 8; |
kenjiArai | 7:cedbf234a089 | 78 | } |
kenjiArai | 7:cedbf234a089 | 79 | pc.printf("FRAM ID=0x%08x", id); |
kenjiArai | 7:cedbf234a089 | 80 | if (id == FRAM_ID) { |
kenjiArai | 6:a3238e93f694 | 81 | fram_ready = true; |
kenjiArai | 6:a3238e93f694 | 82 | } |
kenjiArai | 7:cedbf234a089 | 83 | if (fram_ready == true){ |
kenjiArai | 7:cedbf234a089 | 84 | pc.printf("(looks like Fujitsu MB85RS2M)\r\n"); |
kenjiArai | 7:cedbf234a089 | 85 | } else { |
kenjiArai | 7:cedbf234a089 | 86 | pc.printf("(unknown device)\r\n"); |
kenjiArai | 6:a3238e93f694 | 87 | } |
kenjiArai | 7:cedbf234a089 | 88 | // RX8025 |
kenjiArai | 7:cedbf234a089 | 89 | RX8025 ex_rtc(I2C_SDA, I2C_SCL); // RTC(RX8025) (Fixed address) |
kenjiArai | 7:cedbf234a089 | 90 | ThisThread::sleep_for(500); |
kenjiArai | 6:a3238e93f694 | 91 | ex_rtc.clear_alarmD_INTA(); |
kenjiArai | 6:a3238e93f694 | 92 | w_check_rtc_time(ex_rtc); |
kenjiArai | 6:a3238e93f694 | 93 | int32_t count = 3; |
kenjiArai | 6:a3238e93f694 | 94 | while(true) { |
kenjiArai | 6:a3238e93f694 | 95 | seconds = w_check_rtc_time(ex_rtc); |
kenjiArai | 6:a3238e93f694 | 96 | log_sec = seconds; |
kenjiArai | 6:a3238e93f694 | 97 | strftime(buf, 50, "%Y/%m/%d,%H:%M:%S, ", localtime(&seconds)); |
kenjiArai | 6:a3238e93f694 | 98 | barometer = bme280.getPressure(); |
kenjiArai | 6:a3238e93f694 | 99 | temperature = bme280.getTemperature(); |
kenjiArai | 6:a3238e93f694 | 100 | humidity = bme280.getHumidity(); |
kenjiArai | 6:a3238e93f694 | 101 | vcc_voltage = vdd.read_real_value(); |
kenjiArai | 6:a3238e93f694 | 102 | if (count == 1){ |
kenjiArai | 6:a3238e93f694 | 103 | pc.printf("%s", buf); |
kenjiArai | 6:a3238e93f694 | 104 | pc.printf("%+2.2f,%2.2f,%04.2f,", |
kenjiArai | 6:a3238e93f694 | 105 | temperature, humidity, barometer); |
kenjiArai | 6:a3238e93f694 | 106 | pc.printf("%3.2f\r\n", vcc_voltage); |
kenjiArai | 6:a3238e93f694 | 107 | } |
kenjiArai | 6:a3238e93f694 | 108 | ThisThread::sleep_for(200); |
kenjiArai | 6:a3238e93f694 | 109 | if (pc.readable() == 1) { |
kenjiArai | 6:a3238e93f694 | 110 | mon(ex_rtc, fram); |
kenjiArai | 6:a3238e93f694 | 111 | } |
kenjiArai | 6:a3238e93f694 | 112 | if (--count <= 0) { |
kenjiArai | 6:a3238e93f694 | 113 | break; |
kenjiArai | 6:a3238e93f694 | 114 | } |
kenjiArai | 6:a3238e93f694 | 115 | } |
kenjiArai | 7:cedbf234a089 | 116 | dtlog_data_pack(); // Data preparation for FRAM |
kenjiArai | 6:a3238e93f694 | 117 | dtlog_one_write(fram); // Write data to FRAM |
kenjiArai | 6:a3238e93f694 | 118 | #if KEPP_SLEEP_TIME |
kenjiArai | 6:a3238e93f694 | 119 | rtc_irq.fall(&rtc_interrupt); |
kenjiArai | 6:a3238e93f694 | 120 | uint16_t sleeping_time = SLEEP_TIME; |
kenjiArai | 7:cedbf234a089 | 121 | pc.printf("Enter sleep mode and wake up after %d minutes\r\n", |
kenjiArai | 7:cedbf234a089 | 122 | sleeping_time); |
kenjiArai | 6:a3238e93f694 | 123 | ex_rtc.set_next_alarmD_INTA(sleeping_time); |
kenjiArai | 6:a3238e93f694 | 124 | //----- SLEEP -------------------------------------------------------------- |
kenjiArai | 6:a3238e93f694 | 125 | bme280_pwr = 0; |
kenjiArai | 6:a3238e93f694 | 126 | fram_pwr = 0; |
kenjiArai | 6:a3238e93f694 | 127 | ThisThread::sleep_for((sleeping_time) * 60 * 1000 + 10000); |
kenjiArai | 6:a3238e93f694 | 128 | system_reset(); |
kenjiArai | 6:a3238e93f694 | 129 | #else |
kenjiArai | 6:a3238e93f694 | 130 | bme280_pwr = 0; |
kenjiArai | 6:a3238e93f694 | 131 | fram_pwr = 0; |
kenjiArai | 6:a3238e93f694 | 132 | ThisThread::sleep_for(8000); |
kenjiArai | 6:a3238e93f694 | 133 | system_reset(); |
kenjiArai | 6:a3238e93f694 | 134 | #endif |
kenjiArai | 6:a3238e93f694 | 135 | } |
kenjiArai | 6:a3238e93f694 | 136 | |
kenjiArai | 6:a3238e93f694 | 137 | void rtc_interrupt(void) |
kenjiArai | 6:a3238e93f694 | 138 | { |
kenjiArai | 6:a3238e93f694 | 139 | system_reset(); |
kenjiArai | 6:a3238e93f694 | 140 | } |
kenjiArai | 6:a3238e93f694 | 141 | |
kenjiArai | 6:a3238e93f694 | 142 | time_t w_check_rtc_time(RX8025 &ex_rtc) |
kenjiArai | 6:a3238e93f694 | 143 | { |
kenjiArai | 6:a3238e93f694 | 144 | time_t seconds_1st, seconds_2nd, diff; |
kenjiArai | 6:a3238e93f694 | 145 | struct tm t; |
kenjiArai | 6:a3238e93f694 | 146 | |
kenjiArai | 6:a3238e93f694 | 147 | for (uint32_t i = 0; i < 10; i++) { |
kenjiArai | 6:a3238e93f694 | 148 | ex_rtc.get_time_rtc(&t); // read External RTC data |
kenjiArai | 6:a3238e93f694 | 149 | seconds_1st = mktime(&t); |
kenjiArai | 6:a3238e93f694 | 150 | ex_rtc.get_time_rtc(&t); // read External RTC data again (make sure) |
kenjiArai | 6:a3238e93f694 | 151 | seconds_2nd = mktime(&t); |
kenjiArai | 6:a3238e93f694 | 152 | diff = seconds_2nd - seconds_1st; |
kenjiArai | 6:a3238e93f694 | 153 | if ((diff == 0) || (diff == 1)) { |
kenjiArai | 6:a3238e93f694 | 154 | if (seconds_2nd > DATE_COUNT_START){ |
kenjiArai | 6:a3238e93f694 | 155 | return seconds_2nd; |
kenjiArai | 6:a3238e93f694 | 156 | } |
kenjiArai | 6:a3238e93f694 | 157 | } |
kenjiArai | 6:a3238e93f694 | 158 | } |
kenjiArai | 6:a3238e93f694 | 159 | // Without success |
kenjiArai | 6:a3238e93f694 | 160 | system_reset(); |
kenjiArai | 6:a3238e93f694 | 161 | } |