Beispiel #1
0
/* SUMMARY:
 * Main function
 * INFO:
 * [1] Start by init the watchdog, see wdt.h for macro options. 
 * [2] Init the current state to the entry state and the collection of return 
 * codes. 
 * [3] Setup the function pointer for the diffrent states. 
 * [4] Init the serial communication and enable global interrupts and 
 * init timer0. 
 * [5] Redirect the stdout and the stdin for serial streams through uart.
 * MAIN LOOP:
 * [1] Assign the function pointer a current state
 * [2] Call the pointed function, and collect it's return code
 * [3] Reset the watchdog
 * [4] Check if the current state is exit and break(This will terminate)
 * [5] If not, then move to the next state, declared in the transition table.
 * [6] Repeat
 */
int
main(void) 
{
    enum state_codes cur_state = ENTRY_STATE;
    enum ret_codes rc;
    uint8_t (* state_fun)(void);

    UART0Init();
    sei();
    timer0_init();

    stdin = stdout = &uart0_str;
    
    for (;;) {
        state_fun = state[cur_state];
        rc = state_fun();
        
        if (EXIT_STATE == cur_state)
            break;
        cur_state = lookup_transitions(cur_state, rc);
    }
    return 0;
}
Beispiel #2
0
/*******************************************************************************
main

Software entry point

Purpose of this software is to listen & inject Z-Wave packet
Is essential a bidirectional Serial to Z-Wave bridge
Can be used to either be part of a home automation system (my case),
or create havoc. USE RESPONSIBLY.

Requires a CC1110DK
Compiled with Keil c51 v952

 *******************************************************************************/
void main(void)
{
    unsigned char Idx = 0; /* General For Loop Counter */

    CLKCON = 0x00; /* Select the High speed crystal oscillator */
    while((CLKCON & OSC) != 0)
    {
        /* Wait for change to take effect */
    }

    /* Configure Pins */
    P0SEL = (PX_5_PERIPHERAL | PX_4_PERIPHERAL | PX_3_PERIPHERAL | PX_2_PERIPHERAL); /* Schematics of the mini devkit user guide p25 shows the serial port uses: P0_3 as Tx, P0_2 as Rx */
    P1DIR = (PX_1_OUT | PX_0_OUT); /* Schematics of the mini devkit user guide p25 show Green LED uses P1_0 and the Red LED P1_1 */

    UART0Init(BAUD_115200); /* Configures UART0 */
    InfoBanner();

    RadioInit(); /* Configure the TI chip Radio */

    /* UART RX Test */
    //UARTRxByteCount = 5; //wait for 5 bytes
    //UART0Rx();
    //for(Idx = 0; Idx < UARTRxByteCount ; Idx++)
    //{
    //    UARTTxMessageBuild(UARTRxBytes[Idx] + 1); /* Print the next char for a laugth */
    //}
    //UART0Tx();

    /* Dummy Data to test Radio Tx */
    RadioTxMessageBuild(0x01);
    RadioTxMessageBuild(0x23);
    RadioTxMessageBuild(0x45);
    RadioTxMessageBuild(0x67);
    RadioTxMessageBuild(0x89);
    /*
    RadioTxMessageBuild(0xAB);
    RadioTxMessageBuild(0xCD);
    RadioTxMessageBuild(0xEF);
    RadioTxMessageBuild(0x01);
    RadioTxMessageBuild(0x23);
    RadioTxMessageBuild(0x45);
    RadioTxMessageBuild(0x67);
    RadioTxMessageBuild(0x89);
    RadioTxMessageBuild(0xAB);
    RadioTxMessageBuild(0xCD);
    RadioTxMessageBuild(0xEF);
    RadioTxMessageBuild(0x01);
    RadioTxMessageBuild(0x23);
    RadioTxMessageBuild(0x45);
    RadioTxMessageBuild(0x67);
    RadioTxMessageBuild(0x89);
    */

    while(1) /* Forever Loop */
    {
        MARCSTATECurrent = MARCSTATE;

        /* LED Controls */
        PinLEDGreen = (MARCSTATECurrent == RX) ? 1 : 0 ; /* When radio receives, Green LED should be lit */
        PinLEDRed =   (MARCSTATECurrent == TX) ? 1 : 0 ; /* When radio trasmits,   Red LED should be lit */

        switch(MARCSTATECurrent)
        {
            case IDLE:
                if(RadioRxBytesCount > 0)
                {
                    /* Print the number of radio bytes received */
                    UARTTxMessageBuild('R');
                    UARTTxMessageBuild('x');
                    UARTTxMessageBuild('B');
                    UARTTxMessageBuild(' ');
                    UARTTxMessageBuild('=');
                    UARTTxMessageBuild(' ');
                    UART0Tx();

                    UtilU8ToHumanDec(RadioRxBytesCount);

                    UARTTxMessageBuild('\r');
                    UARTTxMessageBuild('\n');
                    UART0Tx();

                    /* Print the received radio bytes */
                    UARTTxMessageBuild('R');
                    UARTTxMessageBuild('x');
                    UARTTxMessageBuild('D');
                    UARTTxMessageBuild(' ');
                    UARTTxMessageBuild('=');
                    UARTTxMessageBuild(' ');
                    UART0Tx();

                    for(Idx = 0; Idx < RadioRxBytesCount; Idx++)
                    {
                        UtilU8ToHumanHex(RadioRxBytes[Idx]);
                    }

                    RadioRxBytesCount = 0; /* Reset Radio Data IN counter */
                    UARTTxMessageBuild('\r');
                    UARTTxMessageBuild('\n');
                    UART0Tx();
                }

                RadioTxBytesCountOut = 0; /* Reset Radio Data OUT counter */

                /* Always go back to Rx Mode by Default, unless master switch is pushed */
                if(PinSwitchMaster == 0)
                {
                    PKTLEN = RadioTxBytesCountIn;
                    RFST = STX; /* Enter Radio TX state by issuing an SRX strobe command */
                }
                else
                {
                    PKTLEN = 25;
                    RFST = SRX; /* Enter Radio RX state by issuing an SRX strobe command */
                }

                break;

            case RX:
                if(RFTXRXIF != 0)
                {
                    RFTXRXIF = 0; /* Specs say to clear f*g  BEFORE reading (p188) */
                    RadioRxBytes[RadioRxBytesCount] = ~RFD;
                    RadioRxBytesCount++;
                }
                break;

            case RX_OVERFLOW:
                RFST = SIDLE; /* Return to Idle state to continue */
                break;

            case TX:
                /* Print Radio State Info */
                UARTTxMessageBuild('S');
                UARTTxMessageBuild('t');
                UARTTxMessageBuild('M');
                UARTTxMessageBuild(' ');
                UARTTxMessageBuild('=');
                UARTTxMessageBuild(' ');
                UART0Tx();

                UtilU8ToHumanHex(MARCSTATECurrent);

                UARTTxMessageBuild('\r');
                UARTTxMessageBuild('\n');
                UART0Tx();

                while(RadioTxBytesCountOut < RadioTxBytesCountIn)
                {
                    if(RFTXRXIF != 0)
                    {
                        RFD = ~RadioTxBytes[RadioTxBytesCountOut];
                        RFTXRXIF = 0;
                        RadioTxBytesCountOut++;
                    }
                }
                //RadioTxBytesCountIn = 0; /* Reset the Radio TX Data IN index */
                RFST = SIDLE; /* Return to Idle state to continue */
                break;

            case TX_UNDERFLOW:
                    UARTTxMessageBuild('S');
                    UARTTxMessageBuild('t');
                    UARTTxMessageBuild('M');
                    UARTTxMessageBuild(' ');
                    UARTTxMessageBuild('=');
                    UARTTxMessageBuild(' ');
                    UART0Tx();

                    UtilU8ToHumanHex(MARCSTATECurrent);

                    UARTTxMessageBuild('\r');
                    UARTTxMessageBuild('\n');
                    UART0Tx();
                RFST = SIDLE; /* Return to Idle state to continue */
                break;

            default:
                /* Push both buttons at the same time to leave a locked unknown current state without a reset */
                if((PinSwitchMaster == 0) && (PinSwitchSlave == 0))
                {
                    /* Print Radio State Info */
                    UARTTxMessageBuild('S');
                    UARTTxMessageBuild('t');
                    UARTTxMessageBuild('M');
                    UARTTxMessageBuild(' ');
                    UARTTxMessageBuild('=');
                    UARTTxMessageBuild(' ');
                    UART0Tx();

                    UtilU8ToHumanHex(MARCSTATECurrent);

                    UARTTxMessageBuild('\r');
                    UARTTxMessageBuild('\n');
                    UART0Tx();
                    RFST = SIDLE; /* Return to Idle state to continue */
                }
                break;
        }

        MARCSTATEPrevious = MARCSTATECurrent;
    }
}
Beispiel #3
0
void UBloxInit(void)
{
  SCU_APBPeriphClockConfig(__GPIO6, ENABLE);  // Enable the GPIO6 Clock
  SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART0 Clock

  GPIO_InitTypeDef gpio_init;

  // Configure pin GPIO6.6 to be UART0 Rx
  gpio_init.GPIO_Direction = GPIO_PinInput;
  gpio_init.GPIO_Pin = GPIO_Pin_6;
  gpio_init.GPIO_Type = GPIO_Type_PushPull;
  gpio_init.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
  gpio_init.GPIO_Alternate = GPIO_InputAlt1;  // UART0 Rx
  GPIO_Init(GPIO6, &gpio_init);

  // Configure pin GPIO6.6 to be UART0 Tx
  gpio_init.GPIO_Direction = GPIO_PinOutput;
  gpio_init.GPIO_Pin = GPIO_Pin_7;
  gpio_init.GPIO_Type = GPIO_Type_PushPull;
  gpio_init.GPIO_Alternate = GPIO_OutputAlt3;  // UART0 Tx
  GPIO_Init(GPIO6, &gpio_init);

  UART0Init(UBLOX_INITIAL_BAUD);

  // Enable UART Rx interrupt.
  UART_ITConfig(UART0, UART_IT_Receive, ENABLE);
  VIC_Config(UART0_ITLine, VIC_IRQ, IRQ_PRIORITY_UART0);
  VIC_ITCmd(UART0_ITLine, ENABLE);

  {
    // Set the port to UART UBX @ 57600.
    const uint8_t tx_buffer[28] = { 0xb5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01,
      0x00, 0x00, 0x00, 0xd0, 0x08, 0x00, 0x00, 0x00, 0xe1, 0x00, 0x00, 0x01,
      0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd6, 0x8d };
    UBloxTxBuffer(tx_buffer, 28);
  }

  Wait(150);
  UART0Init(UBLOX_OPERATING_BAUD);

  {  // Configure USB for UBX input with no output.
    const uint8_t tx_buffer[28] = { 0xb5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x03,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x8c };
    UBloxTxBuffer(tx_buffer, 28);
  }
  {  // Set antenna flags to 0x0b and pins to 0x380f.
    const uint8_t tx_buffer[12] = { 0xb5, 0x62, 0x06, 0x13, 0x04, 0x00, 0x0b,
      0x00, 0x0f, 0x38, 0x6f, 0x4f };
    UBloxTxBuffer(tx_buffer, 12);
  }
  {  // Set measurement period to 200ms (5Hz) with UTC reference.
    const uint8_t tx_buffer[14] = { 0xb5, 0x62, 0x06, 0x08, 0x06, 0x00, 0xc8,
      0x00, 0x01, 0x00, 0x00, 0x00, 0xdd, 0x68 };
    UBloxTxBuffer(tx_buffer, 14);
  }
  {  // Configure TimPulse.
    const uint8_t tx_buffer[28] = { 0xb5, 0x62, 0x06, 0x07, 0x14, 0x00, 0x40,
      0x42, 0x0f, 0x00, 0x90, 0x86, 0x03, 0x00, 0xff, 0x01, 0x00, 0x00, 0x32,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x70 };
    UBloxTxBuffer(tx_buffer, 28);
  }
  {  // Configure SBAS.
    const uint8_t tx_buffer[16] = { 0xb5, 0x62, 0x06, 0x16, 0x08, 0x00, 0x03,
      0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2b, 0xbd };
    UBloxTxBuffer(tx_buffer, 16);
  }
  {  // Configure navigation engine.
    const uint8_t tx_buffer[44] = { 0xb5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xff,
      0xff, 0x06, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x08,
      0x3c, 0x50, 0x00, 0x32, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97,
      0xfa };
    UBloxTxBuffer(tx_buffer, 44);
  }
  {  // Configure navigation engine expert settings.
    const uint8_t tx_buffer[48] = { 0xb5, 0x62, 0x06, 0x23, 0x28, 0x00, 0x00,
      0x00, 0x4c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x10, 0x14,
      0x00, 0x01, 0x00, 0x00, 0x00, 0xf8, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0xc9, 0xea };
    UBloxTxBuffer(tx_buffer, 48);
  }
  {  // Request NAV-POSLLH message to be output every measurement cycle.
    const uint8_t tx_buffer[11] = { 0xb5, 0x62, 0x06, 0x01, 0x03, 0x00, 0x01,
      0x02, 0x01, 0x0e, 0x47 };
    UBloxTxBuffer(tx_buffer, 11);
  }
  {  // Request NAV-VELNED message to be output every measurement cycle.
    const uint8_t tx_buffer[11] = { 0xb5, 0x62, 0x06, 0x01, 0x03, 0x00, 0x01,
      0x12, 0x01, 0x1e, 0x67 };
    UBloxTxBuffer(tx_buffer, 11);
  }
/*
  {  // Request NAV-SOL message to be output every measurement cycle.
    const uint8_t tx_buffer[11] = { 0xb5, 0x62, 0x06, 0x01, 0x03, 0x00, 0x01,
      0x06, 0x01, 0x12, 0x4f };
    UBloxTxBuffer(tx_buffer, 11);
  }
*/
  {  // Request Time-UTC message to be output every 5 measurement cycles.
    const uint8_t tx_buffer[11] = { 0xb5, 0x62, 0x06, 0x01, 0x03, 0x00, 0x01,
      0x21, 0x05, 0x31, 0x89 };
    UBloxTxBuffer(tx_buffer, 11);
  }
}