init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Revision 37:505ef618f06c, committed 2019-01-16
- Comitter:
- pathfindr
- Date:
- Wed Jan 16 21:27:28 2019 +0000
- Parent:
- 36:8e359069192b
- Child:
- 38:476a9b5629a1
- Commit message:
- r
Changed in this revision
--- a/NRFuart.cpp Tue Jan 15 11:19:41 2019 +0000
+++ b/NRFuart.cpp Wed Jan 16 21:27:28 2019 +0000
@@ -1,9 +1,7 @@
-//bool NRFuart_enabled = false;
#include "NRFuart.h"
void NRFuart_init_nohwfc() {
if(NRF_UART0->ENABLE == UART_ENABLE_ENABLE_Disabled) {
- //Configure UART0 pins.
nrf_gpio_cfg_output(PN_UART_TX);
nrf_gpio_cfg_input(PN_UART_RX, NRF_GPIO_PIN_NOPULL);
NRF_UART0->PSELTXD = PN_UART_TX;
@@ -27,7 +25,34 @@
//NVIC_EnableIRQ(UART0_IRQn);
//NVIC_SetVector(UART0_IRQn, (uint32_t) UART0_IRQHandler);
}
- //NRFuart_enabled = true;
+};
+void NRFuart_init_hwfc() {
+ if(NRF_UART0->ENABLE == UART_ENABLE_ENABLE_Disabled) {
+ nrf_gpio_cfg_output(PN_UART_TX);
+ nrf_gpio_cfg_input(PN_UART_RX, NRF_GPIO_PIN_NOPULL);
+ NRF_UART0->PSELTXD = PN_UART_TX;
+ NRF_UART0->PSELRXD = PN_UART_RX;
+ NRF_UART0->PSELCTS = PN_UART_CTS;
+ NRF_UART0->PSELRTS = PN_UART_RTS;
+ NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
+ NRF_UART0->BAUDRATE = NRF_UART_BAUDRATE_115200;
+ NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
+ NRF_UART0->EVENTS_RXDRDY = 0;
+ NRF_UART0->EVENTS_TXDRDY = 0;
+ NRF_UART0->EVENTS_ERROR = 0;
+ NRF_UART0->EVENTS_TXDRDY = 0;
+ NRF_UART0->TASKS_STARTRX = 1;
+ NRF_UART0->TASKS_STARTTX = 1;
+ //NRF_UART0->INTENCLR = 0xffffffffUL;
+ //NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Msk; //or
+ /*NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) |
+ (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) |
+ (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos);*/
+ //NVIC_ClearPendingIRQ(UART0_IRQn);
+ //NVIC_SetPriority(UART0_IRQn, 1); //3
+ //NVIC_EnableIRQ(UART0_IRQn);
+ //NVIC_SetVector(UART0_IRQn, (uint32_t) UART0_IRQHandler);
+ }
};
void NRFuart_uninit() {
if (NRF_UART0->ENABLE == UART_ENABLE_ENABLE_Enabled) {
@@ -40,7 +65,6 @@
NRF_UART0->PSELRXD = 0xFFFFFFFF;
NRF_UART0->PSELRTS = 0xFFFFFFFF;
NRF_UART0->PSELCTS = 0xFFFFFFFF;
- //NRFuart_enabled = false;
}
};
void NRFuart_putc(char byte) {
@@ -54,19 +78,25 @@
};
void NRFuart_puts(char* bytes) {
if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
- for(int i = 0; bytes[i] != '\0'; i++) {
+ uint32_t safetycounter = 0;
+ for(int i = 0; bytes[i] != 0x00; i++) {
NRFuart_putc(bytes[i]);
+ safetycounter ++;
+ if (safetycounter > 10000) break;
}
};
void NRFuart_puts_debug(char* bytes) {
if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
- for(int i = 0; bytes[i] != '\0'; i++) {
+ uint32_t safetycounter = 0;
+ for(int i = 0; bytes[i] != 0x00; i++) {
NRFuart_putc(bytes[i]);
+ safetycounter ++;
+ if (safetycounter > 10000) break;
}
NRFuart_putc('\n');
};
void debug_prep(){
- memset(GLOBAL_debug_buffer, '\0', sizeof(GLOBAL_debug_buffer));
+ memset(GLOBAL_debug_buffer, 0x00, sizeof(GLOBAL_debug_buffer));
}
void debug_exe(){
NRFuart_puts_debug(GLOBAL_debug_buffer);
@@ -85,8 +115,10 @@
static char buffer[200];
int charindex = 0;
memset(buffer,0x00,sizeof(buffer));
- while(1) {
+ uint32_t safetycounter = 0;
+ while(1 && safetycounter < 10000) {
if (NRF_UART0->EVENTS_RXDRDY == 0) {
+ safetycounter ++;
//Nothing available from the UART.
continue;
} else {
--- a/WatchdogTimer.lib Tue Jan 15 11:19:41 2019 +0000 +++ b/WatchdogTimer.lib Wed Jan 16 21:27:28 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/jcady92/code/WatchdogTimer/#dca9710f08ba +https://os.mbed.com/users/jcady92/code/WatchdogTimer/#912a54b2dd7b
--- a/main.cpp Tue Jan 15 11:19:41 2019 +0000
+++ b/main.cpp Wed Jan 16 21:27:28 2019 +0000
@@ -3,7 +3,9 @@
//------------------------------------------------------------------------------
//DEFINES
//------------------------------------------------------------------------------
-#define FW_VERSION 2
+#define FW_VERSION 2
+#define SKU "GPSPLUS"
+#define HW_MAJORREVISION "001"
//MODES
#define USE_NRF_TEMP_SENSOR 1
@@ -24,7 +26,7 @@
#define DEFAULT_BEACON_INTERVAL_SECONDS 10
//------------------------------------------------------------------------------
-//FUNCTION PROTOTYPES
+//FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES
//------------------------------------------------------------------------------
void mainStateEngine(void);
void selftest(void);
@@ -109,12 +111,12 @@
//PERIPHERALS
//------------------------------------------------------------------------------
//BLE myble;
-WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice
+WatchdogTimer watchdog; //Do not set to less than 4500ms or can cause issues with softdevice
void watchdogKick() {watchdog.kick();}
SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
-LowPowerTicker RTCticker; //no impact on power consumption
+LowPowerTicker RTCticker;
//------------------------------------------------------------------------------
//THREAD SEMAPHORES
@@ -202,14 +204,12 @@
}
float getTemperature() {
float temperature;
-
if (USE_NRF_TEMP_SENSOR) {
//INTERNAL NRF52 TEMP SENSOR
temperature = nrfTemperature();
} else {
temperature = si7060.getTemperature(); //currently disabled because its causing a high current 450ua sleep, most likely due to sensor not sleeping correctly, or i2c sleep issue
}
-
return temperature;
}
void addToExceptionString(char* value) {
@@ -276,7 +276,6 @@
GLOBAL_accel_healthy = false;
GLOBAL_motionStopFlagTriggered = false;
memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
- if (DEBUG_ON) {memset(GLOBAL_debug_buffer,0x00,sizeof(GLOBAL_debug_buffer));}
}
void setDefaults() {
//IDENTITY
@@ -480,6 +479,16 @@
RET_haveSettings = true;
GLOBAL_needToConfigureLis3dh = true;
RET_SettingsGotAt = RET_RTCunixtime;
+
+ //check for firmware update
+ if (RET_setting_firmware != FW_VERSION) {
+ read_app_data_from_flash(&app_data);
+ app_data.target_firmware_version = RET_setting_firmware;
+ write_app_data_to_flash(&app_data);
+ ThisThread::wait(500);
+ system_reset();
+ }
+
return true;
} else {
if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
@@ -723,12 +732,8 @@
break;
}
if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
- if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n",false); event_location_tx(); }
+ if (run_location_tx) { event_location_tx(); }
- //if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
- //Location Failsafe timer catchall
- //if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
- //if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
//ACTIVITY EVENT
bool run_activity_tx = false;
@@ -784,8 +789,8 @@
{
DEBUG("STATE:BUTTONHOLD\n",false);
if (RET_state_prev == STATE_NORMAL) {
+ RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (72*3600)); //72hrs
setState(STATE_DORMANT);
- RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
DEBUG("TURNING OFF\n",false);
DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
LED1on(5000);
@@ -815,6 +820,7 @@
LED1on(500);
//INIT
+ watchdog.configure(300.0);
LED1off();
RTCticker.attach(&RTCtick, 1.0);
button.fall(&buttonPress);
@@ -862,12 +868,15 @@
watchdogKick(); //only need this if we're in while loop
//INIT
- resetGlobals(); // could move this into state engine switches to save actions on each loop
+ resetGlobals(); // could move this into state switches to save actions on each loop
GLOBAL_wakeTime = RET_RTCunixtime;
//MAIN LOGIC
- DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
- DEBUG("ACTIVITY:%s\n",RET_activityData);
+ //DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
+ //DEBUG("ACTIVITY:%s\n",RET_activityData);
+ if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();}
+ if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACTIVITY:%s\n",RET_activityData);debug_exe();}
+
mainStateEngine();
//LOG FIRST RUN - BOOTLOADER COMMS
--- a/modem.cpp Tue Jan 15 11:19:41 2019 +0000
+++ b/modem.cpp Wed Jan 16 21:27:28 2019 +0000
@@ -126,7 +126,6 @@
void Modem::off(bool soft)
{
- _w_disable = 0; //enable airplane mode
if (soft) {
//ATsendCMD("AT+QPOWD");
//_pwrkey = 0;
@@ -136,6 +135,7 @@
}
GLOBAL_modemOn = false;
GLOBAL_registeredOnNetwork = false;
+ _w_disable = 0; //enable airplane mode
_pwrkey = 0;
_vreg_en = 0;
}