Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
32:6c0f43fda460
Parent:
31:010a44d53627
Child:
33:6a2301a89e92
--- a/main.cpp	Wed Aug 24 13:11:10 2016 +0000
+++ b/main.cpp	Thu Aug 25 00:07:21 2016 +0000
@@ -265,6 +265,23 @@
 	timer1_interrupted = true;
 }
 
+class WatchDog {
+	static const uint32_t RELOAD_VALUE = 0x6E524635;
+	static const uint8_t WDT_TIMEOUT = 2; // sec
+public:
+	static void init() {
+		// timeout [s] = (CRV + 1) / 32768;
+		// crv = 32768 * timeout - 1
+		NRF_WDT->CRV = 32768 * WDT_TIMEOUT - 1;
+		NRF_WDT->RREN = WDT_RREN_RR0_Enabled << WDT_RREN_RR0_Pos;
+		NRF_WDT->CONFIG = WDT_CONFIG_SLEEP_Pause << WDT_CONFIG_SLEEP_Pos;
+		NRF_WDT->TASKS_START = 1;
+	}
+	
+	static void reload() {
+		NRF_WDT->RR[0] = RELOAD_VALUE;
+	}
+};
 
 int main(void) {
 	printf("init\r\n");
@@ -294,6 +311,7 @@
 	NVIC_ClearPendingIRQ(TIMER1_IRQn);
 	NVIC_EnableIRQ(TIMER1_IRQn);
 
+	WatchDog::init();
 			    		
 	// 100kHz
 	i2c.frequency(100000);
@@ -312,7 +330,8 @@
 	// Disable TWI by default.
 	NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
 	
-	while (1) {	
+	while (1) {
+		WatchDog::reload();
 		if (pollCount > 0) {
 			printf("scan keys\r\n");
   		    
@@ -358,7 +377,7 @@
 			printf("disable int\r\n");
 			NRF_TIMER1->TASKS_SHUTDOWN = 1;
 		} else {
-			printf("[%s] wait for events...\r\n", HIDController::connected() ? "connected" : "disconnected");
+			printf("[%d:%s] wait for events...\r\n", HIDController::status(), HIDController::statusString());
     
     		// allow printf in some interrupts while connecting
     		if (HIDController::connected()) {