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-30
Revision:
41:9ef4c4d77711
Parent:
40:2ec4be320961
Child:
44:ece6330e9b57

File content as of revision 41:9ef4c4d77711:

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

#include "CommProtocolPeerBrute.h"

///////////////////////
// I/O Configuration //
///////////////////////
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);
//InterruptIn intTest(GPIO2);
//DigitalIn uartOut1(UART1_CTS);
//DigitalIn uartOut2(UART1_RTS);
//DigitalIn jtag_gpio1(SWDIO);
//DigitalIn jtag_gpio2(SWCLK);

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

//unsigned int callbackCnt = 0;
//
//static void testCallback () {
////    callbackCnt = us_ticker_read();
//    callbackCnt++;
//}

int main() {
    RadioEvent events;  // Custom event handler for automatically displaying RX data

    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);

    // 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);
    // HAL_PWR_PVDCallback need to define this I think this will override the current implementation

    // TODO setup IO here

#if BRIDGE_TX_BRUTE
    CommProtocolPeerBrute protocol(true); // TX
#else
    CommProtocolPeerBrute protocol(false); // RX
#endif
    protocol.init();

    dot->setWakePin(WAKE);

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

    // display configuration
    display_config();

    uint16_t seqNum=0;

    // mbed-os Interrupt setup
//    intTest.mode(PullUp);
//    intTest.fall(&testCallback);
//    intTest.enable_irq(); // TODO check if this is useful

//    dot->setWakePin(GPIO2); // This works but disables the wake button

    // Using xdot hal libs
//int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
//    gpio_irq_init();

    // Try the STM32 way
//    //EXTI structure to init EXT
//    EXTI_InitTypeDef EXTI_InitStructure;
//    //NVIC structure to set up NVIC controller
//    NVIC_InitTypeDef NVIC_InitStructure;
//    //Connect EXTI Line to Button Pin
////    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
//    GPIO_EXTILineConfig(STM_PORT(GPIO2), STM_PIN(GPIO2));
//    //Configure Button EXTI line
//    EXTI_InitStructure.EXTI_Line = EXTI_Line0;
//    //select interrupt mode
//    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//    //generate interrupt on rising edge
//    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
//    //enable EXTI line
//    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//    //send values to registers
//    EXTI_Init(&EXTI_InitStructure);
//    //configure NVIC
//    //select NVIC channel to configure
//    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
//    //set priority to lowest
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
//    //set subpriority to lowest
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
//    //enable IRQ channel
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    //update NVIC registers
//    NVIC_Init(&NVIC_InitStructure);

    while (true) {
        std::vector<uint8_t> data;
        led1=0;

		if (protocol.isTx()) {
#if LED_FEEDBACK
            led1=1;
#endif
            // TODO check for CC_IN

            data.push_back((seqNum >> 8) & 0xFF);
            data.push_back(seqNum & 0xFF);
            protocol.send(data);
            seqNum++;

#if LED_FEEDBACK
            led1=0;
#endif
            sleep_save_io();
            sleep_configure_io();
            dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
            sleep_restore_io();
		}

		if (protocol.isRx()) {
		    bool msgPending;
		    protocol.listen(msgPending);
		    if (msgPending) {
		        protocol.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, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep until wake button
            sleep_restore_io();
		}

        //////////////
        // I/O Play //
        //////////////

//        // Check interrupt
//        logInfo("Callback last called @ %d", callbackCnt);

//        // 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;
//		gpio3 = !gpio3;
////		wake_DOUT = !wake_DOUT;
//		i2cOut1 = !i2cOut1;
//		i2cOut2 = !i2cOut2;
//
//		uartOut1 = !uartOut1;
//		uartOut2 = !uartOut2;

//        logInfo("================================");
//        wait(1.0);
//		sleep_save_io();
//		sleep_configure_io();
//        __GPIOA_CLK_ENABLE();
//        __GPIOB_CLK_ENABLE();
//        __GPIOC_CLK_ENABLE();
//        __GPIOH_CLK_ENABLE();

//        xdot_enter_stop_mode();
//        dot->sleep(2, mDot::INTERRUPT, false);  // Go to sleep until wake button
        //////////////////
        // OneWire Play //
        //////////////////

//        logInfo("Starting OneWire Play");
//        OneWire owMaster(I2C_SDA);
//        uint8_t addr[8];
//        uint8_t result;
//
//        // Search Bus
//        logInfo("Starting OneWire Search");
//        do {
//            result = owMaster.search(addr);
//            logInfo("ROM Addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x%02x\n",
//                    addr[7],addr[6],addr[5],addr[4],addr[3],addr[2],addr[1],addr[0]);
//        } while (result == 1);
//        logInfo("Finished OneWire Search");
//        wait(1.0);
//
//        uint8_t pioStateAddr[] = {0x88, 0x00};
//        uint8_t pioLatchAddr[] = {0x89, 0x00};
//        uint8_t printAddr = 0x88;
//        while (true) {
//            owMaster.reset();
//            owMaster.select(addr);
//            owMaster.write(0xF0); // Read Register Command
//            owMaster.write_bytes(pioStateAddr, 2); // Write 2 byte addr
//            printAddr = 0x88;
//            for (int i=0;i<8;i++) {
//                result = owMaster.read();
//                logInfo("%02x Reg Value: %02x\n", printAddr++, result);
//            }
//
//            owMaster.reset();
//            owMaster.select(addr);
//            owMaster.write(0xF0); // Read Register Command
//            owMaster.write_bytes(pioLatchAddr, 2); // Write 2 byte addr
//            result = owMaster.read();
//            logInfo("Latch Reg Value: %02x\n", result);
//            // TODO try reading inverted 16-bit CRC
//
//            wait(1.0);
//            // Try write
//            owMaster.reset();
//            owMaster.select(addr);
//            owMaster.write(0x5A); // Channel Access Write Command
//            uint8_t val = ~0xAA;
//            owMaster.write(val); // Pull-down all even bits
//            owMaster.write(~val); // Pull-down all even bits
//            result = owMaster.read();
//            logInfo("Confirm after write value: %02x, expected %02x\n", result, 0xAA);
//
//            // Check if the read back is just a latch reg thing or a true logic state
//            wait(1.0);
//        }
    }
 
    return 0;
}