Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

xDotBridge/src/main.cpp

Committer:
Matt Briggs
Date:
2017-01-11
Revision:
30:2e673a672884
Parent:
29:e05e35976cfe
Child:
31:9c535a708ae9

File content as of revision 30:2e673a672884:

#include "config.h"
#include "xdot_flash.h"
#include "dot_util.h"
#include "RadioEvent.h"
#include <math.h>


/////////////////////////////////////////////////////////////////////////////
// -------------------- DOT LIBRARY REQUIRED ------------------------------//
// * Because these example programs can be used for both mDot and xDot     //
//     devices, the LoRa stack is not included. The libmDot library should //
//     be imported if building for mDot devices. The libxDot library       //
//     should be imported if building for xDot devices.                    //
// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
// * these options must match between the two devices in   //
//   order for communication to be successful
/////////////////////////////////////////////////////////////
static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
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)

DigitalOut led1(GPIO0);

AnalogIn an1(GPIO1);
AnalogIn an2(GPIO2);

// Inputs
DigitalIn gpio3(GPIO3);
DigitalIn wake_DOUT(WAKE);
DigitalIn i2cOut1(I2C1_SCL);
DigitalIn i2cOut2(I2C1_SDA);

DigitalIn uartOut1(UART1_CTS);
DigitalIn uartOut2(UART1_RTS);

// Outputs
//DigitalOut gpio3(GPIO3);
////DigitalOut wake_DOUT(WAKE);
//DigitalOut i2cOut1(I2C1_SCL);
//DigitalOut i2cOut2(I2C1_SDA);
//
//DigitalOut uartOut1(UART1_CTS);
//DigitalOut uartOut2(UART1_RTS);

mDot* dot = NULL;

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;
    uint32_t tx_frequency;
    uint8_t tx_datarate;
    uint8_t tx_power;
    uint8_t frequency_band;

    pc.baud(115200);

    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
    
    dot = mDot::getInstance();

    logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);

    // start from a well-known state
    logInfo("defaulting Dot configuration");
    dot->resetConfig();

    // make sure library logging is turned on
    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);

    // attach the custom events handler
    dot->setEvents(&events);

    // update configuration if necessary
    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) {
            logError("failed to set network join mode to PEER_TO_PEER");
        }
    }
    frequency_band = dot->getFrequencyBand();
    switch (frequency_band) {
        case mDot::FB_EU868:
            // 250kHz channels achieve higher throughput
            // DR6 : SF7 @ 250kHz
            // DR0 - DR5 (125kHz channels) available but much slower
            tx_frequency = 869850000;
            tx_datarate = mDot::DR6;
            // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
            tx_power = 4;
            break;
        case mDot::FB_US915:
        case mDot::FB_AU915:
        default:
            // 500kHz channels achieve highest throughput
            // DR8 : SF12 @ 500kHz
            // DR9 : SF11 @ 500kHz
            // DR10 : SF10 @ 500kHz
            // DR11 : SF9 @ 500kHz
            // DR12 : SF8 @ 500kHz
            // DR13 : SF7 @ 500kHz
            // DR0 - DR3 (125kHz channels) available but much slower
            tx_frequency = 915500000;
            tx_datarate = mDot::DR13;
            // 915 bands have no duty cycle restrictions, set tx power to max
            tx_power = 20;
            break;
    }
    // in PEER_TO_PEER mode there is no join request/response transaction
    // 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 //
	///////////////////////////////
	#if BRIDGE_TX_BRUTE
    wakeMode = mDot::INTERRUPT;
    dot->setTxWait(false);
    dot->setAck(0);  // Disable Ack
	#endif

	#if BRIDGE_TX_ACK
    wakeMode = mDot::INTERRUPT;
    dot->setTxWait(true);
    dot->setAck(1);  // Enable Ack
    dot->setRepeat(1);  // No auto repeats
    dot->setRxDelay(1);
	#endif

	////////////////////////////
	// Receiver Configuration //
	////////////////////////////
	#if BRIDGE_RX_BRUTE
    wakeMode = mDot::RTC_ALARM_OR_INTERRUPT;
    dot->setTxWait(false);
    dot->setAck(0);  // Disable Ack
	#endif

    // Common Configuration
    dot->setWakePin(WAKE); // Use the wake pin as sleep interrupt
    dot->setClass("C"); // Set class C
    dot->setTxPower(20); // 20 dBm

    // save changes to configuration
    logInfo("saving configuration");
    if (!dot->saveConfig()) {
        logError("failed to save configuration");
    }

    // display configuration
    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);

    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

        // join network if not joined
        if (!dot->getNetworkJoinStatus()) {
            join_network();
        }

    	//////////////////////////////////////////
    	// Brute Protocol Transmitter main loop //
    	//////////////////////////////////////////
		#if BRIDGE_TX_BRUTE
        led1=1;
        data.push_back((seqNum >> 8) & 0xFF);
        data.push_back(seqNum & 0xFF);
        logInfo("Starting TX.  Time: %lu, seqNum: %lu", us_ticker_read(), seqNum);
        for(uint i=0;i<nTimesToTx;++i) {
        	dot->send(data);
        }
        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

		#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
        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());
			seqNum++;
			wait(0.5);
        }
		led1=0;
        logInfo("Sleeping.  Time %d", us_ticker_read());
		sleep_save_io();
		sleep_configure_io();
        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 digital outputs
//		led1 = !led1;
//		gpio3 = !gpio3;
////		wake_DOUT = !wake_DOUT;
//		i2cOut1 = !i2cOut1;
//		i2cOut2 = !i2cOut2;
//
//		uartOut1 = !uartOut1;
//		uartOut2 = !uartOut2;

        logInfo("================================");

		wait(1.0);
    }
 
    return 0;
}