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/
Diff: main.cpp
- Revision:
- 6:a3238e93f694
- Child:
- 7:cedbf234a089
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Dec 18 09:24:58 2019 +0000 @@ -0,0 +1,170 @@ +/* + * Mbed Application program / TYBLE-16 Data logger + * + * Copyright (c) 2019 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * https://os.mbed.com/users/kenjiArai/ + * Created: December 14th, 2019 + * Revised: December 18th, 2019 + */ + +// Include -------------------------------------------------------------------- +#include "mbed.h" +#include "TYBLE16_BASE.h" +#include "dt_logger.h" +#include "nRF51_Vdd.h" +#include "RX8025NB.h" +#include "BME280_SPI.h" +#include "MB85RSxx_SPI.h" +#include "nRF51_lowpwr.h" + +// Definition ----------------------------------------------------------------- +#define KEPP_SLEEP_TIME 1 // 0= continuos mode (every 10sec) +#define SLEEP_TIME 5 // Sleep 2 minutes +#define FRAM_ID0 (0x04) // Fijitsu 2Mbits(0x047f4803) +#define FRAM_ID1 (0x7f) +#define FRAM_ID2 (0x48) +#define FRAM_ID3 (0x03) + +// Constructor ---------------------------------------------------------------- +DigitalOut bme280_pwr(D9, 1); // BME280 sensor power on +DigitalOut fram_pwr(D5, 1); // FRAM power on +InterruptIn rtc_irq(P0_2, PullUp); +Serial pc(USBTX, USBRX); +nRF51_Vdd vdd(3.6f, 1.8f, ONLY4VDD); + +// RAM ------------------------------------------------------------------------ +uint8_t fram_id[4]; +bool fram_ready = false; + +// Global data +time_t log_sec; +float vcc_voltage; +float barometer; +float temperature; +float humidity; + +// ROM / Constant data -------------------------------------------------------- +const char *const opngmsg0 = + "\r\n---------\r\n Compiled on " __DATE__ " " __TIME__ " (UTC)\r\n"; +const char *const opngmsg1 = + " Project: TYBLE16_simple_data_logger by Kenji Arai\r\n"; + +const nRF51_LOWPWR_TypeDef lowpwr_table = + { false, // VCOM + false, // UART + false, // I2C + false, // SPI + false, // SPI Slave + false, // PWM + false // ADC +}; + +// Function prototypes -------------------------------------------------------- +static void rtc_interrupt(void); +static void goto_standby(void); +time_t w_check_rtc_time(RX8025 &ex_rtc); + +//------------------------------------------------------------------------------ +// Control Program +//------------------------------------------------------------------------------ +int main() +{ + char buf[64]; // data buffer for text + time_t seconds; + + ThisThread::sleep_for(200); + pc.puts(opngmsg0); + pc.puts(opngmsg1); + // Check TYBLE-16 configuration + cpu_sys(); + compile_condition(); + // Create constructor + // RX8025 + RX8025 ex_rtc(I2C_SDA, I2C_SCL); // RTC(RX8025) (Fixed address) + // BM280 + BME280_SPI bme280(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_25); + // FRAM + MB85RSxx_SPI fram(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, P0_6); + fram.read_device_id(fram_id); +#if 0 + if (fram_id == FRAM_ID) { + fram_ready = true; + } + pc.printf("FRAM ID = 0x%08x\r\n", fram_id); +#else + for (uint32_t i = 0; i < 4; i++) { + pc.printf("FRAM ID = 0x%08x\r\n", fram_id[i]); + } +#endif + // RTC related preparation + ex_rtc.clear_alarmD_INTA(); + w_check_rtc_time(ex_rtc); + int32_t count = 3; + while(true) { + seconds = w_check_rtc_time(ex_rtc); + log_sec = seconds; + strftime(buf, 50, "%Y/%m/%d,%H:%M:%S, ", localtime(&seconds)); + barometer = bme280.getPressure(); + temperature = bme280.getTemperature(); + humidity = bme280.getHumidity(); + vcc_voltage = vdd.read_real_value(); + if (count == 1){ + pc.printf("%s", buf); + pc.printf("%+2.2f,%2.2f,%04.2f,", + temperature, humidity, barometer); + pc.printf("%3.2f\r\n", vcc_voltage); + } + ThisThread::sleep_for(200); + if (pc.readable() == 1) { + mon(ex_rtc, fram); + } + if (--count <= 0) { + break; + } + } + dtlog_data_pack(); // Preparation for FRAM + dtlog_one_write(fram); // Write data to FRAM +#if KEPP_SLEEP_TIME + rtc_irq.fall(&rtc_interrupt); + uint16_t sleeping_time = SLEEP_TIME; + ex_rtc.set_next_alarmD_INTA(sleeping_time); + //----- SLEEP -------------------------------------------------------------- + bme280_pwr = 0; + fram_pwr = 0; + LowPwr set_lowpwr(&lowpwr_table); + ThisThread::sleep_for((sleeping_time) * 60 * 1000 + 10000); + system_reset(); +#else + bme280_pwr = 0; + fram_pwr = 0; + ThisThread::sleep_for(8000); + system_reset(); +#endif +} + +void rtc_interrupt(void) +{ + system_reset(); +} + +time_t w_check_rtc_time(RX8025 &ex_rtc) +{ + time_t seconds_1st, seconds_2nd, diff; + struct tm t; + + for (uint32_t i = 0; i < 10; i++) { + ex_rtc.get_time_rtc(&t); // read External RTC data + seconds_1st = mktime(&t); + ex_rtc.get_time_rtc(&t); // read External RTC data again (make sure) + seconds_2nd = mktime(&t); + diff = seconds_2nd - seconds_1st; + if ((diff == 0) || (diff == 1)) { + if (seconds_2nd > DATE_COUNT_START){ + return seconds_2nd; + } + } + } + // Without success + system_reset(); +}