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:
Wed Dec 18 09:24:58 2019 +0000
Revision:
6:a3238e93f694
Child:
7:cedbf234a089
1st working program

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 6:a3238e93f694 8 * Revised: December 18th, 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 #include "nRF51_lowpwr.h"
kenjiArai 6:a3238e93f694 20
kenjiArai 6:a3238e93f694 21 // Definition -----------------------------------------------------------------
kenjiArai 6:a3238e93f694 22 #define KEPP_SLEEP_TIME 1 // 0= continuos mode (every 10sec)
kenjiArai 6:a3238e93f694 23 #define SLEEP_TIME 5 // Sleep 2 minutes
kenjiArai 6:a3238e93f694 24 #define FRAM_ID0 (0x04) // Fijitsu 2Mbits(0x047f4803)
kenjiArai 6:a3238e93f694 25 #define FRAM_ID1 (0x7f)
kenjiArai 6:a3238e93f694 26 #define FRAM_ID2 (0x48)
kenjiArai 6:a3238e93f694 27 #define FRAM_ID3 (0x03)
kenjiArai 6:a3238e93f694 28
kenjiArai 6:a3238e93f694 29 // Constructor ----------------------------------------------------------------
kenjiArai 6:a3238e93f694 30 DigitalOut bme280_pwr(D9, 1); // BME280 sensor power on
kenjiArai 6:a3238e93f694 31 DigitalOut fram_pwr(D5, 1); // FRAM power on
kenjiArai 6:a3238e93f694 32 InterruptIn rtc_irq(P0_2, PullUp);
kenjiArai 6:a3238e93f694 33 Serial pc(USBTX, USBRX);
kenjiArai 6:a3238e93f694 34 nRF51_Vdd vdd(3.6f, 1.8f, ONLY4VDD);
kenjiArai 6:a3238e93f694 35
kenjiArai 6:a3238e93f694 36 // RAM ------------------------------------------------------------------------
kenjiArai 6:a3238e93f694 37 uint8_t fram_id[4];
kenjiArai 6:a3238e93f694 38 bool fram_ready = false;
kenjiArai 6:a3238e93f694 39
kenjiArai 6:a3238e93f694 40 // Global data
kenjiArai 6:a3238e93f694 41 time_t log_sec;
kenjiArai 6:a3238e93f694 42 float vcc_voltage;
kenjiArai 6:a3238e93f694 43 float barometer;
kenjiArai 6:a3238e93f694 44 float temperature;
kenjiArai 6:a3238e93f694 45 float humidity;
kenjiArai 6:a3238e93f694 46
kenjiArai 6:a3238e93f694 47 // ROM / Constant data --------------------------------------------------------
kenjiArai 6:a3238e93f694 48 const char *const opngmsg0 =
kenjiArai 6:a3238e93f694 49 "\r\n---------\r\n Compiled on " __DATE__ " " __TIME__ " (UTC)\r\n";
kenjiArai 6:a3238e93f694 50 const char *const opngmsg1 =
kenjiArai 6:a3238e93f694 51 " Project: TYBLE16_simple_data_logger by Kenji Arai\r\n";
kenjiArai 6:a3238e93f694 52
kenjiArai 6:a3238e93f694 53 const nRF51_LOWPWR_TypeDef lowpwr_table =
kenjiArai 6:a3238e93f694 54 { false, // VCOM
kenjiArai 6:a3238e93f694 55 false, // UART
kenjiArai 6:a3238e93f694 56 false, // I2C
kenjiArai 6:a3238e93f694 57 false, // SPI
kenjiArai 6:a3238e93f694 58 false, // SPI Slave
kenjiArai 6:a3238e93f694 59 false, // PWM
kenjiArai 6:a3238e93f694 60 false // ADC
kenjiArai 6:a3238e93f694 61 };
kenjiArai 6:a3238e93f694 62
kenjiArai 6:a3238e93f694 63 // Function prototypes --------------------------------------------------------
kenjiArai 6:a3238e93f694 64 static void rtc_interrupt(void);
kenjiArai 6:a3238e93f694 65 static void goto_standby(void);
kenjiArai 6:a3238e93f694 66 time_t w_check_rtc_time(RX8025 &ex_rtc);
kenjiArai 6:a3238e93f694 67
kenjiArai 6:a3238e93f694 68 //------------------------------------------------------------------------------
kenjiArai 6:a3238e93f694 69 // Control Program
kenjiArai 6:a3238e93f694 70 //------------------------------------------------------------------------------
kenjiArai 6:a3238e93f694 71 int main()
kenjiArai 6:a3238e93f694 72 {
kenjiArai 6:a3238e93f694 73 char buf[64]; // data buffer for text
kenjiArai 6:a3238e93f694 74 time_t seconds;
kenjiArai 6:a3238e93f694 75
kenjiArai 6:a3238e93f694 76 ThisThread::sleep_for(200);
kenjiArai 6:a3238e93f694 77 pc.puts(opngmsg0);
kenjiArai 6:a3238e93f694 78 pc.puts(opngmsg1);
kenjiArai 6:a3238e93f694 79 // Check TYBLE-16 configuration
kenjiArai 6:a3238e93f694 80 cpu_sys();
kenjiArai 6:a3238e93f694 81 compile_condition();
kenjiArai 6:a3238e93f694 82 // Create constructor
kenjiArai 6:a3238e93f694 83 // RX8025
kenjiArai 6:a3238e93f694 84 RX8025 ex_rtc(I2C_SDA, I2C_SCL); // RTC(RX8025) (Fixed address)
kenjiArai 6:a3238e93f694 85 // BM280
kenjiArai 6:a3238e93f694 86 BME280_SPI bme280(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_25);
kenjiArai 6:a3238e93f694 87 // FRAM
kenjiArai 6:a3238e93f694 88 MB85RSxx_SPI fram(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_6);
kenjiArai 6:a3238e93f694 89 fram.read_device_id(fram_id);
kenjiArai 6:a3238e93f694 90 #if 0
kenjiArai 6:a3238e93f694 91 if (fram_id == FRAM_ID) {
kenjiArai 6:a3238e93f694 92 fram_ready = true;
kenjiArai 6:a3238e93f694 93 }
kenjiArai 6:a3238e93f694 94 pc.printf("FRAM ID = 0x%08x\r\n", fram_id);
kenjiArai 6:a3238e93f694 95 #else
kenjiArai 6:a3238e93f694 96 for (uint32_t i = 0; i < 4; i++) {
kenjiArai 6:a3238e93f694 97 pc.printf("FRAM ID = 0x%08x\r\n", fram_id[i]);
kenjiArai 6:a3238e93f694 98 }
kenjiArai 6:a3238e93f694 99 #endif
kenjiArai 6:a3238e93f694 100 // RTC related preparation
kenjiArai 6:a3238e93f694 101 ex_rtc.clear_alarmD_INTA();
kenjiArai 6:a3238e93f694 102 w_check_rtc_time(ex_rtc);
kenjiArai 6:a3238e93f694 103 int32_t count = 3;
kenjiArai 6:a3238e93f694 104 while(true) {
kenjiArai 6:a3238e93f694 105 seconds = w_check_rtc_time(ex_rtc);
kenjiArai 6:a3238e93f694 106 log_sec = seconds;
kenjiArai 6:a3238e93f694 107 strftime(buf, 50, "%Y/%m/%d,%H:%M:%S, ", localtime(&seconds));
kenjiArai 6:a3238e93f694 108 barometer = bme280.getPressure();
kenjiArai 6:a3238e93f694 109 temperature = bme280.getTemperature();
kenjiArai 6:a3238e93f694 110 humidity = bme280.getHumidity();
kenjiArai 6:a3238e93f694 111 vcc_voltage = vdd.read_real_value();
kenjiArai 6:a3238e93f694 112 if (count == 1){
kenjiArai 6:a3238e93f694 113 pc.printf("%s", buf);
kenjiArai 6:a3238e93f694 114 pc.printf("%+2.2f,%2.2f,%04.2f,",
kenjiArai 6:a3238e93f694 115 temperature, humidity, barometer);
kenjiArai 6:a3238e93f694 116 pc.printf("%3.2f\r\n", vcc_voltage);
kenjiArai 6:a3238e93f694 117 }
kenjiArai 6:a3238e93f694 118 ThisThread::sleep_for(200);
kenjiArai 6:a3238e93f694 119 if (pc.readable() == 1) {
kenjiArai 6:a3238e93f694 120 mon(ex_rtc, fram);
kenjiArai 6:a3238e93f694 121 }
kenjiArai 6:a3238e93f694 122 if (--count <= 0) {
kenjiArai 6:a3238e93f694 123 break;
kenjiArai 6:a3238e93f694 124 }
kenjiArai 6:a3238e93f694 125 }
kenjiArai 6:a3238e93f694 126 dtlog_data_pack(); // Preparation for FRAM
kenjiArai 6:a3238e93f694 127 dtlog_one_write(fram); // Write data to FRAM
kenjiArai 6:a3238e93f694 128 #if KEPP_SLEEP_TIME
kenjiArai 6:a3238e93f694 129 rtc_irq.fall(&rtc_interrupt);
kenjiArai 6:a3238e93f694 130 uint16_t sleeping_time = SLEEP_TIME;
kenjiArai 6:a3238e93f694 131 ex_rtc.set_next_alarmD_INTA(sleeping_time);
kenjiArai 6:a3238e93f694 132 //----- SLEEP --------------------------------------------------------------
kenjiArai 6:a3238e93f694 133 bme280_pwr = 0;
kenjiArai 6:a3238e93f694 134 fram_pwr = 0;
kenjiArai 6:a3238e93f694 135 LowPwr set_lowpwr(&lowpwr_table);
kenjiArai 6:a3238e93f694 136 ThisThread::sleep_for((sleeping_time) * 60 * 1000 + 10000);
kenjiArai 6:a3238e93f694 137 system_reset();
kenjiArai 6:a3238e93f694 138 #else
kenjiArai 6:a3238e93f694 139 bme280_pwr = 0;
kenjiArai 6:a3238e93f694 140 fram_pwr = 0;
kenjiArai 6:a3238e93f694 141 ThisThread::sleep_for(8000);
kenjiArai 6:a3238e93f694 142 system_reset();
kenjiArai 6:a3238e93f694 143 #endif
kenjiArai 6:a3238e93f694 144 }
kenjiArai 6:a3238e93f694 145
kenjiArai 6:a3238e93f694 146 void rtc_interrupt(void)
kenjiArai 6:a3238e93f694 147 {
kenjiArai 6:a3238e93f694 148 system_reset();
kenjiArai 6:a3238e93f694 149 }
kenjiArai 6:a3238e93f694 150
kenjiArai 6:a3238e93f694 151 time_t w_check_rtc_time(RX8025 &ex_rtc)
kenjiArai 6:a3238e93f694 152 {
kenjiArai 6:a3238e93f694 153 time_t seconds_1st, seconds_2nd, diff;
kenjiArai 6:a3238e93f694 154 struct tm t;
kenjiArai 6:a3238e93f694 155
kenjiArai 6:a3238e93f694 156 for (uint32_t i = 0; i < 10; i++) {
kenjiArai 6:a3238e93f694 157 ex_rtc.get_time_rtc(&t); // read External RTC data
kenjiArai 6:a3238e93f694 158 seconds_1st = mktime(&t);
kenjiArai 6:a3238e93f694 159 ex_rtc.get_time_rtc(&t); // read External RTC data again (make sure)
kenjiArai 6:a3238e93f694 160 seconds_2nd = mktime(&t);
kenjiArai 6:a3238e93f694 161 diff = seconds_2nd - seconds_1st;
kenjiArai 6:a3238e93f694 162 if ((diff == 0) || (diff == 1)) {
kenjiArai 6:a3238e93f694 163 if (seconds_2nd > DATE_COUNT_START){
kenjiArai 6:a3238e93f694 164 return seconds_2nd;
kenjiArai 6:a3238e93f694 165 }
kenjiArai 6:a3238e93f694 166 }
kenjiArai 6:a3238e93f694 167 }
kenjiArai 6:a3238e93f694 168 // Without success
kenjiArai 6:a3238e93f694 169 system_reset();
kenjiArai 6:a3238e93f694 170 }