void stkEvaluateRxMessage(void) /* not static to prevent inlining */
{
    _u8       i, cmd;
    utilWord_t  len = {2};  /* defaults to cmd + error code */
    void        *param;

    //
    LED_RX_ON();

    /*
        NOTICE: Please do NOT forget to set the following values
    
        txBuffer[STK_TXMSG_START]     : cmd
        &txBuffer[STK_TXMSG_START+1]  : payload data
        len.word                      : sizeof(cmd) + sizeof(payload data)
        len.byte[0]                   : len.word & 0xFF
    */

    cmd = rxBuffer[STK_TXMSG_START];
    param = &rxBuffer[STK_TXMSG_START + 1];
    
    // The default return value
    txBuffer[STK_TXMSG_START] = cmd;
    txBuffer[STK_TXMSG_START + 1] = STK_STATUS_CMD_OK;
    
    
    // reduce the code size by removing the switch-case jump table
    if (cmd == RPINFRA_STK_CMD_ECHO){
        // simple echo back
    }

    else if (cmd == RPINFRA_STK_CMD_GET_ADC){
        *(_u16 *)&txBuffer[STK_TXMSG_START+1] = analogRead(A_ADC0);
        len.bytes[0] = sizeof(unsigned long) + 1;
    }else if (cmd == RPINFRA_STK_CMD_SET_PWM){
        PWM_ENABLE( IO_PWM0 );
        PWM_SET( IO_PWM0, rxBuffer[STK_TXMSG_START + 1] );
        
    }else if (cmd == SCANNER_CMD_SET_HEADING) {
        // set heading
        onSetHeading(*(_u16 *)&rxBuffer[STK_TXMSG_START + 1]);
    }else if (cmd == SCANNER_CMD_ENABLE_LASER) {
        onEnableLaser();
    }else if (cmd == SCANNER_CMD_DISABLE_LASER) {
        onDisableLaser();
    }

    LED_RX_OFF();

    //    
    stkSetTxMessage(len.word);
}
void ST_RadioReceiveIsrCallback(u8 *packet,
                                  boolean ackFramePendingSet,
                                  u32 time,
                                  u16 errors,
                                  s8 rssi)
{
  LED_RX_ON();
  receiving_packet = 0;
  /* Copy packet into the buffer. It is better to do this here. */
  if(add_to_rxbuf(packet)){
    process_poll(&stm32w_radio_process);
    last_rssi = rssi;
  }
  LED_RX_OFF();
}
Beispiel #3
0
/*---------------------------------------------------------------------------*/
void
ST_RadioReceiveIsrCallback(uint8_t *packet,
                           boolean ackFramePendingSet,
                           uint32_t time, uint16_t errors, int8_t rssi)
{
  LED_RX_ON();
  PRINTF("stm32w: incomming packet received\n");
  receiving_packet = 0;

  /* Copy packet into the buffer. It is better to do this here. */
  if(add_to_rxbuf(packet)) {
    process_poll(&stm32w_radio_process);
    last_rssi = rssi;
  }
  LED_RX_OFF();
  GET_LOCK();
  is_transmit_ack = 1;
  /* Wait for sending ACK */
  BUSYWAIT_UNTIL(!is_transmit_ack, RTIMER_SECOND / 1500);
  RELEASE_LOCK();
}
Beispiel #4
0
void UserInit(void) {
    byte i;

#if defined(USE_USB_BUS_SENSE_IO)
    tris_usb_bus_sense = INPUT_PIN; // See io_cfg.h
#endif

#if defined(USE_SELF_POWER_SENSE_IO)
    tris_self_power = INPUT_PIN;
#endif


    CS_2515_HIGH(); //Drive high
    tris_CS = 0; //Output
    OpenSPI(SPI_FOSC_16, MODE_00, SMPMID);

    TRISBbits.TRISB0 = 1; //SDI
    TRISBbits.TRISB1 = 0; //SCK

    //-------------------------
    // initialize variables
    //-------------------------
    for (i = 0; i < BUF_SIZE; i++) // initialize input and output buffer to 0
    {
        inbuffer[i] = 0;
        outbuffer[i] = 0;
    }

    //Timer 0
    TMR0H = 0; //clear timer
    TMR0L = 0; //clear timer
    T0CONbits.PSA = 0; //Assign prescaler to Timer 0
    T0CONbits.T0PS2 = 1; //Setup prescaler
    T0CONbits.T0PS1 = 1; //Will time out every 51 us based on
    T0CONbits.T0PS0 = 1; //20 MHz Fosc
    T0CONbits.T0CS = 0; //Increment on instuction cycle

    INTCON2 = 0xFF; //INT2 on rising edge
    INTCON3bits.INT2IP = 0; //Low priority
    INTCON3bits.INT2IF = 0; //Clear flag
    INTCON3bits.INT2IE = 1; //Enable INT2 interrupt

    //Outputs for the LEDs
    ADCON1 = 0x0F; //Digital pins
    CMCON = 0x07; //Digital pins

    LED_25PCT_OFF();
    LED_50PCT_OFF();
    LED_75PCT_OFF();
    LED_100PCT_OFF();

    TRISBbits.TRISB6 = INPUT_PIN;
    TRISBbits.TRISB7 = INPUT_PIN;

    tris_LED_25PCT = OUTPUT_PIN;
    tris_LED_50PCT = OUTPUT_PIN;
    tris_LED_75PCT = OUTPUT_PIN;
    tris_LED_100PCT = OUTPUT_PIN;

    UserFlag.CANLoading = OFF;

    LED_RX_OFF();
    LED_TX_OFF();

    tris_LED_TX = OUTPUT_PIN;
    tris_LED_RX = OUTPUT_PIN;

    tris_SW_LOAD = INPUT_PIN;

    //RTS Pin Outputs
    RTS0_2515_HIGH();
    tris_RTS0_pin = OUTPUT_PIN;

    RTS1_2515_HIGH();
    tris_RTS1_pin = OUTPUT_PIN;

    RTS2_2515_HIGH();
    tris_RTS2_pin = OUTPUT_PIN;

    tris_CAN_RES = OUTPUT_PIN;
    CAN_RES_ON(); //JM
    //  CAN_RES_OFF();  //Disconnect the termination resistor by default

    UserFlag.MCP_RXBn = 0; //clear flag
    UserFlag.USBsend = 0; //clear flag
    UserFlag.USBQueue = 0; //Clear message queue

    //Need to set up MCP2515 before enabling interrupts
    CANInit(); // See BusMon.c & .h

    RCONbits.IPEN = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
}//end UserInit
Beispiel #5
0
/******************************************************************************
 * Function:        void ProcessIO
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void) {
    unsigned char n, b, c, y;
    int a;
    // User Application USB tasks

    if (!(isUsbPowered)) //Only generate traffic if NOT connected to USB
    {
        CheckLoadButton();
        CANLoadTX();
        return;
    }

    if ((usb_device_state < CONFIGURED_STATE) || (UCONbits.SUSPND == 1)) return;

    //----- Read USB buffer if it has data from the host -----
    if (HIDRxReport(inbuffer, 64)) // USB receive buffer has data
    {
        LED_RX_ON(); //Turn LED on
        T0CONbits.TMR0ON = 1; //Start timer for TX LED on time
        gTimeout = 0; //Reset timout

        //---- CANmsgs: Check if host has requested CAN messages to be transmited
        switch (inbuffer[u_CANmsgs]) // interpret command
        {
            case 0x00: // No messages are available
                break;

            case 0x01: // Message 1 is available
                GetUSBData(m1_SIDH, 13, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, 13, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x02: // Message 1 and 2 are available
                //Message 1
                GetUSBData(m1_SIDH, m1_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 2
                GetUSBData(m2_SIDH, m2_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x03: // Message 1, 2, and 3 are available
                //Message 1
                GetUSBData(m1_SIDH, m1_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 2
                GetUSBData(m2_SIDH, m2_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 3
                GetUSBData(m3_SIDH, m3_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m3_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x04: //--FUTURE-- Message 1, 2, 3, and 4 are available
                break;

            default: // unrecognized or null command
                ;
        }// END switch: inbuffer[u_CANmsgs]

        //---- CANCTRL: Write to the CANCTRL register if changed
        if (inbuffer[u_CANCTRL] != old_CANCTRL) //If host sent new CANCTRL value
        {
            SPIByteWrite(CANCTRL, inbuffer[u_CANCTRL]); //Write to CANCTRL
            EEPROMBYTEWrite(CANCTRL, inbuffer[u_CANCTRL]);
            EEPROMCRCWrite(0, 128);
            old_CANCTRL = inbuffer[u_CANCTRL]; //
            outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT);
            while ((outbuffer[u_CANSTAT] & 0xE0) != (inbuffer[u_CANCTRL] & 0xE0))//if didn't change modes yet
            {
                outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT);
            }
            UserFlag.USBsend = 1; //Set flag so will send USB message
        }

        //---- SPI: SPI command from host
        if (inbuffer[u_SPI]) //If host sent SPI command (non-zero)
        {
            switch (inbuffer[u_SPI]) {
                case CAN_RESET: //
                    SPIReset();
                    CANInit();

                    break;

                case CAN_READ: //
                    if (!UserFlag.USBQueue) // If previous message is queued
                    {
                        outbuffer[u_SPI] = CAN_READ; //Send back to host
                        outbuffer[u_REG] = inbuffer[u_REG]; //Send back to host
                        outbuffer[u_DATA] = SPIByteRead(inbuffer[u_REG]); //Send back to host
                    }
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    UserFlag.USBQueue = 1; //Indicates msg is queued, but not sent
                    break;

                case CAN_WRITE: //
                    //outbuffer[u_SPI] = 0;        //Send back to host //JM
                    SPIByteWrite(inbuffer[u_REG], inbuffer[u_DATA]);
                    EEPROMBYTEWrite(inbuffer[u_REG], inbuffer[u_DATA]);
                    EEPROMCRCWrite(0, 128);
                    break;

                case CAN_RTS: //
                    SPI_RTS(inbuffer[u_DATA]);
                    break;

                case CAN_RD_STATUS: //
                    outbuffer[u_DATA] = SPIReadStatus();
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    break;
                case FIRMWARE_VER_RD:
                    memmove(&outbuffer[u_STATUS], firmware_version, sizeof (firmware_version));
                    outbuffer[u_STATUS + sizeof (firmware_version)] = 0;
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    break;

                default: // unrecognized or null command
                    ;
            }// END switch: inbuffer[u_SPI]
        }
    }//END if (HIDRxReport(inbuffer, 1)

    //---- Check RXnBF pins and service messages as needed ---
    switch (CheckCANRX()) // Check if CAN message received
    {
        case 0x01: // Message in RXB0 (Msgs in this buffer are Standard)
            SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        case 0x02: // Message in RXB1 (Msgs in this buffer are Extended)
            SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        case 0x03: // Message in RXB0 and RXB1
            SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        default: // unrecognized or null command
            ;
    }// END switch: CheckCANRX()

    //---- The following turns off the TX and RX USB indicator LEDs after some time
    //Inst. cycle = 200 ns; TMR0IF sets every 51 us
    if (INTCONbits.TMR0IF) {
        TimerCounter++;
        if (!TimerCounter) //if rolled over, set flag. User code will handle the rest.
        {
            LED_TX_OFF(); //Turn LED off
            LED_RX_OFF(); //Turn LED off
            T0CONbits.TMR0ON = 0; //Start timer for TX LED on time
            TimerCounter = 0xFE;
            gTimeout = 1; //Reset timout
        }
        INTCONbits.TMR0IF = 0;
    }

    //------ Load USB Data to be transmitted to the host --------
    if (UserFlag.MCP_RXBn | UserFlag.USBsend) {
        if (!mHIDTxIsBusy()) {
            HIDTxReport(outbuffer, 64);

            outbuffer[0] = 0x00; //PKR$$$ Need this??

            UserFlag.MCP_RXBn = 0; //clear flag
            UserFlag.USBsend = 0; //clear flag
            UserFlag.USBQueue = 0; //Clear message queue

            LED_TX_ON(); //Turn LED on
            T0CONbits.TMR0ON = 1; //Start timer for TX LED on time
            gTimeout = 0; //Reset timout

            outbuffer[u_SPI] = 0x00; //clear back to 00h so host doesn't detect "SPI response"
            USB_ptr = 0xFF; //Point to location 0xFF
            outbuffer[u_CANmsgs] = 0x00; //Clear message status
        }
    }
}//end ProcessIO