Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
30:f9ebc769118d
Parent:
29:ec548c473d50
Child:
31:010a44d53627
--- a/main.cpp	Wed Aug 24 01:27:46 2016 +0000
+++ b/main.cpp	Wed Aug 24 12:31:15 2016 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-// ignore load us_ticker_api
+#include <cmath>
 #include "mbed.h"
 
 #include "HIDController_BLE.h"
@@ -128,6 +128,7 @@
 
 	}
 
+	// __attribute__((used, long_call, section(".data")))
 	void scanKeyboard(uint8_t* keys) {
 		int ok;
 		
@@ -257,18 +258,29 @@
     statusLed = !statusLed;
 }
 
+static volatile bool timer2_interrupted = false;
+void TIMER2_IRQHandler(void) {
+	NRF_TIMER2->TASKS_CLEAR = 1;
+	NRF_TIMER2->EVENTS_COMPARE[0] = 0;
+	timer2_interrupted = true;
+}
+
 
 int main(void) {
 	printf("init\r\n");
 	
-	NRF_POWER->RESET = 1; // Enable Pin-reset on DEBUG mode
+ 	// Enable Pin-reset on DEBUG mode
+ 	// This makes possiable booting without normal mode easily.
+	NRF_POWER->RESET = 1;
+	// Disable Internal DC/DC step down converter surely
 	NRF_POWER->DCDCEN = 0;
+	// Enable 2.1V brown out detection for avoiding over discharge of NiMH
 	NRF_POWER->POFCON = 
 		POWER_POFCON_POF_Enabled << POWER_POFCON_POF_Pos |
 		POWER_POFCON_THRESHOLD_V21 << POWER_POFCON_THRESHOLD_Pos;
     
 	// mbed's Serial of TARGET_RBLAB_BLENANO sucks
-	// DO NOT CONNECT RTS/CTS AUTOMATICALY!
+	// DO NOT CONNECT RTS/CTS WITHOUT PRIOR CONSENT!
 	NRF_UART0->PSELRTS = 0xFFFFFFFFUL;
 	NRF_UART0->PSELCTS = 0xFFFFFFFFUL;
 
@@ -286,23 +298,29 @@
 	// STOP UART RX for power consumption
 	NRF_UART0->TASKS_STOPRX = 1;
 	
+	// Disable TWI by default.
 	NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
 	
-	
-	// ticker.attach(tickerStatus, 0.5);
-	while (1) {
-		/*
-		ticker.detach();
-		switch (HIDController::status()) {
-			case DISCONNECTED: ticker.attach(tickerStatus, 1); break;
-			case CONNECTING: ticker.attach(tickerStatus, 2); break;
-			case CONNECTED: ticker.attach(tickerStatus, 60);break;
-		}
-		*/
-		
-		
+	while (1) {	
 		if (pollCount > 0) {
 			printf("scan keys\r\n");
+			
+			/*
+			// Setup timer for just wakeup from sleep
+			NRF_TIMER2->TASKS_STOP = 1;
+			NRF_TIMER2->PRESCALER = 4; // f = HFCLK / 2^N => 1MHz timer
+			// TIMER2 is 16bit timer
+			NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
+			NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
+			NRF_TIMER2->CC[0] = 1e6 / 200; // 5ms
+			NRF_TIMER2->EVENTS_COMPARE[0] = 0;
+			NRF_TIMER2->TASKS_CLEAR = 1;
+			NRF_TIMER2->INTENSET = 1;
+			NVIC_SetPriority(TIMER2_IRQn, 3);
+			NVIC_ClearPendingIRQ(TIMER2_IRQn);
+    		NVIC_EnableIRQ(TIMER2_IRQn);
+    		*/
+    
 			while (pollCount -- > 0) {
 				uint8_t (&keysCurr)[COLS] = state ? keysA : keysB;
 				uint8_t (&keysPrev)[COLS] = state ? keysB : keysA;
@@ -328,12 +346,27 @@
 				
 	
 				if (queue) HIDController::queueCurrentReportData();
+				
 				wait_ms(5);
+				
+				/*
+				while (!timer2_interrupted) sleep();
+				timer2_interrupted = false;
+				*/
 			}
+			
+			/*
+			NVIC_DisableIRQ(TIMER2_IRQn);
+			NRF_TIMER2->TASKS_STOP = 1;
+			NRF_TIMER2->TASKS_SHUTDOWN = 1;
+			*/
 		} else {
 			printf("[%s] wait for events...\r\n", HIDController::connected() ? "connected" : "disconnected");
     
+    		// allow printf in some interrupts while connecting
     		if (HIDController::connected()) {
+    			// disable internal HFCLK RC Clock surely. It consume 1mA constantly
+    			// TWI / SPI / UART must be disabled and boot without debug mode
 				while (NRF_UART0->EVENTS_TXDRDY != 1);
 				
 				uint32_t tx = NRF_UART0->PSELTXD;
@@ -345,6 +378,7 @@
 							
 				NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
 				NRF_UART0->TASKS_STARTTX = 1;
+				// dummy send to wakeup...
 				NRF_UART0->PSELTXD = 0xFFFFFFFF;
 			    NRF_UART0->EVENTS_TXDRDY = 0;
 			    NRF_UART0->TXD = 0;