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/

Committer:
kenjiArai
Date:
Sun Dec 22 06:33:18 2019 +0000
Revision:
9:8c9e6e270b67
Parent:
8:7e42f8dc42a2
reduce current consumption

Who changed what in which revision?

UserRevisionLine numberNew 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 9:8c9e6e270b67 8 * Revised: December 22nd, 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 9:8c9e6e270b67 28 DigitalInOut x0(I2C_SDA, PIN_OUTPUT, PullUp, 1); // I2C for dummy
kenjiArai 9:8c9e6e270b67 29 DigitalInOut x1(I2C_SCL, PIN_OUTPUT, PullUp, 1); // I2C for dummy
kenjiArai 6:a3238e93f694 30 InterruptIn rtc_irq(P0_2, PullUp);
kenjiArai 6:a3238e93f694 31 Serial pc(USBTX, USBRX);
kenjiArai 8:7e42f8dc42a2 32 nRF51_Vdd vdd(3.6f, 1.6f, ONLY4VDD);
kenjiArai 6:a3238e93f694 33
kenjiArai 6:a3238e93f694 34 // RAM ------------------------------------------------------------------------
kenjiArai 6:a3238e93f694 35 uint8_t fram_id[4];
kenjiArai 6:a3238e93f694 36 bool fram_ready = false;
kenjiArai 6:a3238e93f694 37
kenjiArai 6:a3238e93f694 38 // Global data
kenjiArai 6:a3238e93f694 39 time_t log_sec;
kenjiArai 6:a3238e93f694 40 float vcc_voltage;
kenjiArai 6:a3238e93f694 41 float barometer;
kenjiArai 6:a3238e93f694 42 float temperature;
kenjiArai 6:a3238e93f694 43 float humidity;
kenjiArai 8:7e42f8dc42a2 44
kenjiArai 6:a3238e93f694 45 // ROM / Constant data --------------------------------------------------------
kenjiArai 6:a3238e93f694 46 const char *const opngmsg0 =
kenjiArai 7:cedbf234a089 47 "\r\n---------\r\nCompiled on " __DATE__ " " __TIME__ " (UTC)\r\n";
kenjiArai 6:a3238e93f694 48 const char *const opngmsg1 =
kenjiArai 7:cedbf234a089 49 "Project: TYBLE16_simple_data_logger by Kenji Arai\r\n";
kenjiArai 8:7e42f8dc42a2 50
kenjiArai 6:a3238e93f694 51 // Function prototypes --------------------------------------------------------
kenjiArai 6:a3238e93f694 52 static void rtc_interrupt(void);
kenjiArai 6:a3238e93f694 53 static void goto_standby(void);
kenjiArai 9:8c9e6e270b67 54 static void priparation_for_sleep(void);
kenjiArai 6:a3238e93f694 55 time_t w_check_rtc_time(RX8025 &ex_rtc);
kenjiArai 6:a3238e93f694 56
kenjiArai 6:a3238e93f694 57 //------------------------------------------------------------------------------
kenjiArai 6:a3238e93f694 58 // Control Program
kenjiArai 6:a3238e93f694 59 //------------------------------------------------------------------------------
kenjiArai 6:a3238e93f694 60 int main()
kenjiArai 6:a3238e93f694 61 {
kenjiArai 6:a3238e93f694 62 char buf[64]; // data buffer for text
kenjiArai 6:a3238e93f694 63 time_t seconds;
kenjiArai 6:a3238e93f694 64
kenjiArai 9:8c9e6e270b67 65 // open message
kenjiArai 6:a3238e93f694 66 pc.puts(opngmsg0);
kenjiArai 6:a3238e93f694 67 pc.puts(opngmsg1);
kenjiArai 6:a3238e93f694 68 // Check TYBLE-16 configuration
kenjiArai 6:a3238e93f694 69 cpu_sys();
kenjiArai 6:a3238e93f694 70 compile_condition();
kenjiArai 6:a3238e93f694 71 // Create constructor
kenjiArai 6:a3238e93f694 72 // BM280
kenjiArai 6:a3238e93f694 73 BME280_SPI bme280(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_25);
kenjiArai 6:a3238e93f694 74 // FRAM
kenjiArai 6:a3238e93f694 75 MB85RSxx_SPI fram(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_6);
kenjiArai 6:a3238e93f694 76 fram.read_device_id(fram_id);
kenjiArai 7:cedbf234a089 77 uint32_t id = 0, num = 24;
kenjiArai 7:cedbf234a089 78 for (uint32_t i = 0; i < 4; i++) {
kenjiArai 7:cedbf234a089 79 id += fram_id[i] << num;
kenjiArai 7:cedbf234a089 80 num -= 8;
kenjiArai 7:cedbf234a089 81 }
kenjiArai 7:cedbf234a089 82 pc.printf("FRAM ID=0x%08x", id);
kenjiArai 7:cedbf234a089 83 if (id == FRAM_ID) {
kenjiArai 6:a3238e93f694 84 fram_ready = true;
kenjiArai 6:a3238e93f694 85 }
kenjiArai 8:7e42f8dc42a2 86 if (fram_ready == true) {
kenjiArai 7:cedbf234a089 87 pc.printf("(looks like Fujitsu MB85RS2M)\r\n");
kenjiArai 7:cedbf234a089 88 } else {
kenjiArai 7:cedbf234a089 89 pc.printf("(unknown device)\r\n");
kenjiArai 6:a3238e93f694 90 }
kenjiArai 7:cedbf234a089 91 // RX8025
kenjiArai 7:cedbf234a089 92 RX8025 ex_rtc(I2C_SDA, I2C_SCL); // RTC(RX8025) (Fixed address)
kenjiArai 9:8c9e6e270b67 93 ThisThread::sleep_for(20);
kenjiArai 6:a3238e93f694 94 ex_rtc.clear_alarmD_INTA();
kenjiArai 6:a3238e93f694 95 w_check_rtc_time(ex_rtc);
kenjiArai 6:a3238e93f694 96 int32_t count = 3;
kenjiArai 6:a3238e93f694 97 while(true) {
kenjiArai 6:a3238e93f694 98 seconds = w_check_rtc_time(ex_rtc);
kenjiArai 6:a3238e93f694 99 log_sec = seconds;
kenjiArai 6:a3238e93f694 100 strftime(buf, 50, "%Y/%m/%d,%H:%M:%S, ", localtime(&seconds));
kenjiArai 6:a3238e93f694 101 barometer = bme280.getPressure();
kenjiArai 6:a3238e93f694 102 temperature = bme280.getTemperature();
kenjiArai 6:a3238e93f694 103 humidity = bme280.getHumidity();
kenjiArai 6:a3238e93f694 104 vcc_voltage = vdd.read_real_value();
kenjiArai 8:7e42f8dc42a2 105 if (count == 1) {
kenjiArai 6:a3238e93f694 106 pc.printf("%s", buf);
kenjiArai 6:a3238e93f694 107 pc.printf("%+2.2f,%2.2f,%04.2f,",
kenjiArai 6:a3238e93f694 108 temperature, humidity, barometer);
kenjiArai 6:a3238e93f694 109 pc.printf("%3.2f\r\n", vcc_voltage);
kenjiArai 6:a3238e93f694 110 }
kenjiArai 6:a3238e93f694 111 ThisThread::sleep_for(200);
kenjiArai 6:a3238e93f694 112 if (pc.readable() == 1) {
kenjiArai 6:a3238e93f694 113 mon(ex_rtc, fram);
kenjiArai 6:a3238e93f694 114 }
kenjiArai 6:a3238e93f694 115 if (--count <= 0) {
kenjiArai 6:a3238e93f694 116 break;
kenjiArai 6:a3238e93f694 117 }
kenjiArai 6:a3238e93f694 118 }
kenjiArai 7:cedbf234a089 119 dtlog_data_pack(); // Data preparation for FRAM
kenjiArai 6:a3238e93f694 120 dtlog_one_write(fram); // Write data to FRAM
kenjiArai 6:a3238e93f694 121 #if KEPP_SLEEP_TIME
kenjiArai 6:a3238e93f694 122 rtc_irq.fall(&rtc_interrupt);
kenjiArai 6:a3238e93f694 123 uint16_t sleeping_time = SLEEP_TIME;
kenjiArai 7:cedbf234a089 124 pc.printf("Enter sleep mode and wake up after %d minutes\r\n",
kenjiArai 7:cedbf234a089 125 sleeping_time);
kenjiArai 6:a3238e93f694 126 ex_rtc.set_next_alarmD_INTA(sleeping_time);
kenjiArai 6:a3238e93f694 127 //----- SLEEP --------------------------------------------------------------
kenjiArai 8:7e42f8dc42a2 128 // FRAM & BME280 power off
kenjiArai 6:a3238e93f694 129 bme280_pwr = 0;
kenjiArai 6:a3238e93f694 130 fram_pwr = 0;
kenjiArai 9:8c9e6e270b67 131 priparation_for_sleep();
kenjiArai 9:8c9e6e270b67 132 // Enter sleep mode
kenjiArai 9:8c9e6e270b67 133 ThisThread::sleep_for((sleeping_time) * 60 * 1000 + 10000);
kenjiArai 9:8c9e6e270b67 134 system_reset();
kenjiArai 9:8c9e6e270b67 135 #else
kenjiArai 9:8c9e6e270b67 136 // FRAM & BME280 power off
kenjiArai 9:8c9e6e270b67 137 bme280_pwr = 0;
kenjiArai 9:8c9e6e270b67 138 fram_pwr = 0;
kenjiArai 9:8c9e6e270b67 139 priparation_for_sleep();
kenjiArai 9:8c9e6e270b67 140 ThisThread::sleep_for(8000);
kenjiArai 9:8c9e6e270b67 141 system_reset();
kenjiArai 9:8c9e6e270b67 142 #endif
kenjiArai 9:8c9e6e270b67 143 }
kenjiArai 9:8c9e6e270b67 144
kenjiArai 9:8c9e6e270b67 145 void priparation_for_sleep(void)
kenjiArai 9:8c9e6e270b67 146 {
kenjiArai 8:7e42f8dc42a2 147 // SPI output keeps low
kenjiArai 8:7e42f8dc42a2 148 DigitalOut p0(SPIS_PSELMOSI, 0);
kenjiArai 8:7e42f8dc42a2 149 DigitalOut p1(SPIS_PSELSCK, 0);
kenjiArai 8:7e42f8dc42a2 150 DigitalOut p2(P0_25, 0);
kenjiArai 8:7e42f8dc42a2 151 DigitalOut p3(P0_6, 0);
kenjiArai 8:7e42f8dc42a2 152 // SPI MISO sets pulldown
kenjiArai 8:7e42f8dc42a2 153 DigitalIn p4(SPIS_PSELMISO, PullDown);
kenjiArai 8:7e42f8dc42a2 154 // Periperal power off
kenjiArai 9:8c9e6e270b67 155 NRF_UART0->ENABLE = 0;
kenjiArai 9:8c9e6e270b67 156 NRF_UART0->POWER = 0;
kenjiArai 9:8c9e6e270b67 157 NRF_TWI1->ENABLE = 0;
kenjiArai 9:8c9e6e270b67 158 NRF_TWI1->POWER = 0;
kenjiArai 9:8c9e6e270b67 159 NRF_SPI0->ENABLE = 0;
kenjiArai 9:8c9e6e270b67 160 NRF_SPI0->POWER = 0;
kenjiArai 9:8c9e6e270b67 161 NRF_SPIS1->ENABLE = 0;
kenjiArai 9:8c9e6e270b67 162 NRF_SPIS1->POWER = 0;
kenjiArai 9:8c9e6e270b67 163 NRF_ADC->ENABLE = 0;
kenjiArai 9:8c9e6e270b67 164 NRF_ADC->POWER = 0;
kenjiArai 6:a3238e93f694 165 }
kenjiArai 6:a3238e93f694 166
kenjiArai 6:a3238e93f694 167 void rtc_interrupt(void)
kenjiArai 6:a3238e93f694 168 {
kenjiArai 6:a3238e93f694 169 system_reset();
kenjiArai 6:a3238e93f694 170 }
kenjiArai 6:a3238e93f694 171
kenjiArai 6:a3238e93f694 172 time_t w_check_rtc_time(RX8025 &ex_rtc)
kenjiArai 6:a3238e93f694 173 {
kenjiArai 6:a3238e93f694 174 time_t seconds_1st, seconds_2nd, diff;
kenjiArai 6:a3238e93f694 175 struct tm t;
kenjiArai 8:7e42f8dc42a2 176
kenjiArai 6:a3238e93f694 177 for (uint32_t i = 0; i < 10; i++) {
kenjiArai 6:a3238e93f694 178 ex_rtc.get_time_rtc(&t); // read External RTC data
kenjiArai 6:a3238e93f694 179 seconds_1st = mktime(&t);
kenjiArai 6:a3238e93f694 180 ex_rtc.get_time_rtc(&t); // read External RTC data again (make sure)
kenjiArai 6:a3238e93f694 181 seconds_2nd = mktime(&t);
kenjiArai 6:a3238e93f694 182 diff = seconds_2nd - seconds_1st;
kenjiArai 6:a3238e93f694 183 if ((diff == 0) || (diff == 1)) {
kenjiArai 8:7e42f8dc42a2 184 if (seconds_2nd > DATE_COUNT_START) {
kenjiArai 6:a3238e93f694 185 return seconds_2nd;
kenjiArai 6:a3238e93f694 186 }
kenjiArai 6:a3238e93f694 187 }
kenjiArai 6:a3238e93f694 188 }
kenjiArai 6:a3238e93f694 189 // Without success
kenjiArai 6:a3238e93f694 190 system_reset();
kenjiArai 6:a3238e93f694 191 }