Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Revision:
31:9c535a708ae9
Parent:
30:2e673a672884
Child:
33:c7bb3fbc024a
--- a/xDotBridge/src/main.cpp	Wed Jan 11 12:24:18 2017 -0700
+++ b/xDotBridge/src/main.cpp	Mon Jan 23 09:02:45 2017 -0700
@@ -26,23 +26,28 @@
 static uint8_t data_session_key[] =    { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
 
 // wireless bridge protocol
-//const float dutyCycle = 0.01; // 1%
-const float rxSleepTime = 2000; // ms (one second resolution, min 2 seconds)
-const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion)
+const uint8_t TX_PWR = 20; // 20 dBm
+const float RX_SLEEP_TIME = 2000; // ms (one second resolution, min 2 seconds)
+const uint8_t TX_TIME = 30; // in ms
 
+//const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion)
+
+///////////////////////
+// I/O Configuration //
+///////////////////////
 DigitalOut led1(GPIO0);
-
 AnalogIn an1(GPIO1);
 AnalogIn an2(GPIO2);
 
 // Inputs
 DigitalIn gpio3(GPIO3);
-DigitalIn wake_DOUT(WAKE);
+//DigitalIn wake_DOUT(WAKE);
 DigitalIn i2cOut1(I2C1_SCL);
 DigitalIn i2cOut2(I2C1_SDA);
-
 DigitalIn uartOut1(UART1_CTS);
 DigitalIn uartOut2(UART1_RTS);
+DigitalIn jtag_gpio1(SWDIO);
+DigitalIn jtag_gpio2(SWCLK);
 
 // Outputs
 //DigitalOut gpio3(GPIO3);
@@ -57,36 +62,9 @@
 
 Serial pc(USBTX, USBRX);
 
-//Ticker t;
-//
-//class RxHandler {
-//	private:
-//	   uint m_dwnLink;
-//	   mDot *m_dot;
-//	public:
-//		RxHandler (mDot *dot) {
-//		   m_dot = dot;
-//			m_dwnLink = dot->getDownLinkCounter();
-//		}
-//		void listen() {
-//			std::vector<uint8_t> data;
-//			led1 = 1; // FIXME
-//			logInfo("Listening for new message current DLC: %d, time: %lu", m_dwnLink, us_ticker_read());
-//			wait(0.060); // Wait twice the time on air
-//			if (m_dwnLink < dot->getDownLinkCounter()) {
-//				m_dwnLink = dot->getDownLinkCounter();
-//				m_dot->recv(data);
-//				std::string dataStr(data.begin(), data.end());
-//				logInfo("Got msg num: -, payload: %s", dataStr.c_str());
-//				wait(0.5);
-//			}
-//			led1=0;
-//		}
-//};
-
 int main() {
-    // Custom event handler for automatically displaying RX data
-    RadioEvent events;
+    unsigned int wakeMode;
+    RadioEvent events;  // Custom event handler for automatically displaying RX data
     uint32_t tx_frequency;
     uint8_t tx_datarate;
     uint8_t tx_power;
@@ -110,7 +88,26 @@
     // attach the custom events handler
     dot->setEvents(&events);
 
+    // Setup programmable voltage detector
+    // PVD_LEVEL0 Falling 1.85
+    // PVD_LEVEL1 Falling 2.04
+    // PVD_LEVEL2 Falling 2.24
+    // PVD_LEVEL3 Falling 2.44
+    // PVD_LEVEL4 Falling 2.64
+    // PVD_LEVEL5 Falling 2.84
+    // PVD_LEVEL6 Falling 3.05
+    PWR_PVDTypeDef pvdConfig;
+    pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
+    pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
+
+    HAL_PWR_ConfigPVD(&pvdConfig);
+    HAL_PWR_EnablePVD();
+    logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
+
+    // TODO setup IO here
+
     // update configuration if necessary
+    logInfo("Setting up peer to peer configuration");
     if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
         logInfo("changing network join mode to PEER_TO_PEER");
         if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
@@ -149,9 +146,6 @@
     // as long as both Dots are configured correctly, they should be able to communicate
     update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);
 
-
-    unsigned int wakeMode;
-
     ///////////////////////////////
 	// Transmitter Configuration //
 	///////////////////////////////
@@ -181,7 +175,7 @@
     // Common Configuration
     dot->setWakePin(WAKE); // Use the wake pin as sleep interrupt
     dot->setClass("C"); // Set class C
-    dot->setTxPower(20); // 20 dBm
+    dot->setTxPower(TX_PWR);
 
     // save changes to configuration
     logInfo("saving configuration");
@@ -193,57 +187,37 @@
     display_config();
 
 
-    uint32_t txTime = 30; // in ms
-    unsigned int nTimesToTx = ceil(rxSleepTime / ((float)txTime));
-    logInfo("rxSleepTime %f, timeOnAir %lu, nTimesToTx %lu", rxSleepTime, txTime, nTimesToTx);
+    unsigned int nTimesToTx = ceil(RX_SLEEP_TIME / ((float)TX_TIME));
+    logInfo("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx);
 
     uint16_t seqNum=0;
     uint32_t cDwnLink = dot->getDownLinkCounter();
 
-    // File system Play
-    const uint32_t filesize = 164754;
-//    const uint32_t filesize = 8192;
-//    const uint32_t size = 4096; // In Bytes
-//    uint8_t buf[size];
-    const uint32_t size = 1024; // In Bytes
-    uint32_t buf[size];
-    uint32_t start = 0x0000; // Note addr is automatically offset to flash start
-	wait(0.1);
-    logInfo("Starting flash read");
-
-	int ret;
-    while (start < filesize) {
-		ret = xdot_flash_read_buf(start, (uint8_t *)buf, size);
-		if (ret < 0) {
-			logError("Flash read failed");
-		}
-		for (uint32_t i=0; i<size; i+=2) {
-	    	if ((i % 4) == 0) {
-	    		printf("\r\n%08X  ", start+i);
-	    	}
-			printf("%08X ", buf[i]);
-		}
-		start += size;
-    }
-
-    return 0;
-
-    // End File System Play
-
     while (true) {
         std::vector<uint8_t> data;
-//        led1=0; FIXME
+        led1=0;
 
         // join network if not joined
         if (!dot->getNetworkJoinStatus()) {
             join_network();
         }
 
+    	//////////////////////
+    	// Common main loop //
+    	//////////////////////
+
+        // TODO sample rotary
+        // TODO sample DIPs
+
     	//////////////////////////////////////////
     	// Brute Protocol Transmitter main loop //
     	//////////////////////////////////////////
 		#if BRIDGE_TX_BRUTE
+#if LED_FEEDBACK
         led1=1;
+#endif
+        // TODO check for CC_IN
+
         data.push_back((seqNum >> 8) & 0xFF);
         data.push_back(seqNum & 0xFF);
         logInfo("Starting TX.  Time: %lu, seqNum: %lu", us_ticker_read(), seqNum);
@@ -260,78 +234,61 @@
         sleep_restore_io();
 		#endif
 
-		#if BRIDGE_TX_ACK
-        led1=1;
-        data.push_back((seqNum >> 8) & 0xFF);
-        data.push_back(seqNum & 0xFF);
-        logInfo("Starting TX.  Time: %lu, seqNum: %lu", us_ticker_read(), seqNum);
-        uint32_t pktRtn = -1024;
-        for(uint i=0;i<nTimesToTx;++i) {
-        	pktRtn = dot->send(data);
-        	if (pktRtn == mDot::MDOT_OK) {
-				logInfo("Successful TX.  Time: %lu Try: %d", us_ticker_read(), i);
-        		break;
-			}
-        	else {
-				logInfo(" Bad TX.  Time: %lu Try: %d", us_ticker_read(), i);
-        	}
-        }
-        seqNum++;
-        led1=0;
-		logInfo("Finished TX.  Time: %lu", us_ticker_read());
-
-		sleep_save_io();
-		sleep_configure_io();
-        dot->sleep(0, wakeMode, false);  // Go to sleep until wake button
-        sleep_restore_io();
-		#endif
-
     	///////////////////////////////////////
     	// Brute Protocol Receiver main loop //
     	///////////////////////////////////////
-
 		#if BRIDGE_RX_BRUTE
         logInfo("Waiting for new message current DLC: %d, Time %d", cDwnLink, us_ticker_read());
-        wait(txTime*2.0/1000.0); // Wait twice the txTime
+        wait(TX_TIME/1000.0); // Wait TX_TIME
+
+        // TODO need to figure out what to do when DLC get resets
+
         if (cDwnLink < dot->getDownLinkCounter()) {
-			led1 = 1;
 			cDwnLink = dot->getDownLinkCounter();
 			dot->recv(data);
 			std::string dataStr(data.begin(), data.end());
 			logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str());
+			// TODO add CC_OUT code here
 			seqNum++;
+#if LED_FEEDBACK
+			led1 = 1;
 			wait(0.5);
+#endif
         }
 		led1=0;
         logInfo("Sleeping.  Time %d", us_ticker_read());
 		sleep_save_io();
 		sleep_configure_io();
+		// TODO maybe add if statement here to prevent double hits by sleeping for a longer time
         dot->sleep(2, wakeMode, false);  // Go to sleep until wake button
         sleep_restore_io();
 		#endif
-//        // Idea to Setup Ticker
-//        RxHandler rxHandler(dot);
-//        t.attach(&rxHandler, &RxHandler::listen, 1.5);
-//        wait(5.0);
-//        while (true) {
-//        	dot->sleep(0, wakeMode, false);  // Go to sleep until wake button
-//        	//sleep();
-//        }
+
         //////////////
         // I/O Play //
         //////////////
 
-        // Check Analog
-        logInfo("Read AN1/GPIO1: %f", an1.read());
-        logInfo("Read AN2/GPIO2: %f", an2.read()); // Ranges from 0.0 to 1.0
-
-        // check inputs
-        logInfo("Read GPIO3: %d", gpio3.read());
-        logInfo("Read wake_DOUT: %d", wake_DOUT.read());
-        logInfo("Read i2cOut1: %d", i2cOut1.read()); // Appears to be pulled up
-        logInfo("Read i2cOut2: %d", i2cOut2.read()); // Appears to be pulled up
-        logInfo("Read uartOut1: %d", uartOut1.read());
-        logInfo("Read uartOut2: %d", uartOut2.read());
+//        // Check Analog
+//        logInfo("Read AN1/GPIO1: %f", an1.read());
+//        logInfo("Read AN2/GPIO2: %f", an2.read()); // Ranges from 0.0 to 1.0
+//
+//        // check inputs
+//        logInfo("Read GPIO3: %d", gpio3.read());
+////        logInfo("Read wake_DOUT: %d", wake_DOUT.read());
+//        logInfo("Read i2cOut1: %d", i2cOut1.read()); // Appears to be pulled up
+//        logInfo("Read i2cOut2: %d", i2cOut2.read()); // Appears to be pulled up
+//        logInfo("Read uartOut1: %d", uartOut1.read());
+//        logInfo("Read uartOut2: %d", uartOut2.read());
+//
+//        logInfo("Read jtag_gpio1: %d", jtag_gpio1.read());
+//        logInfo("Read jtag_gpio2: %d", jtag_gpio2.read());
+//
+//        if (jtag_gpio1.read() == 0) {
+//        	led1 = 1;
+//        }
+//        else {
+//        	led1 = 0;
+//        }
 
         // check digital outputs
 //		led1 = !led1;
@@ -343,9 +300,8 @@
 //		uartOut1 = !uartOut1;
 //		uartOut2 = !uartOut2;
 
-        logInfo("================================");
-
-		wait(1.0);
+//        logInfo("================================");
+//		wait(1.0);
     }
  
     return 0;