Ejemplo n.º 1
0
int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    CAN_Start();
    
    /* Start USBFS Operation with 5V operation */
    USBFS_Start(0u, USBFS_5V_OPERATION);
    
    /* Wait for Device to enumerate */
    while(!USBFS_bGetConfiguration());

    for(;;)
    {
        /* Check for IN buffer is empty */
        if (receiveMailboxNumber == CAN_RX_MAILBOX_RX_message) {
            while(USBFS_bGetEPState(1) != USBFS_IN_BUFFER_EMPTY);
            
            buffer[0] = CAN_RX_DATA_BYTE1(CAN_RX_MAILBOX_RX_message);
        
            /* Load the IN buffer */
            USBFS_LoadInEP(1, &buffer[0], length);
        }
    }
}
Ejemplo n.º 2
0
void main()
{
    u16 tmp;
    u8 tmp8, tmp8_A;
    static tCANMsg RxMsgBuff;

    CLK_Init(CLK_HSE);
    disableInterrupts();
    GPIO_Init();
    TIM4_Init();
//	TIM2_Init();
    TIM1_Init();
    ADC_Init();
    /* Configure CAN - Interface		*/

    CAN_Init();												// init CAN - interface

    enableInterrupts();

    CAN_Start();

    CAN_TxMsg1.Length = 4;
    CAN_TxMsg1.Xtd = false;
    CAN_TxMsg1.rtr = false;
    CAN_TxMsg1.ID = 0x280;
    CAN_TxMsg1.Data[0] =0 ;
    CAN_TxMsg1.Data[1] =0;


    //PWM_SetFrequency(1);
    do {


        if (	IsCAN_MSG1_Send()) {
            tmp = ADC_GetValue(ADC_REV_CHANEL);
            tmp = tmp *24;
            if ( tmp < 100 ) {
                OIL_PRESS = false;
            } else {
                OIL_PRESS = true;
            }

            CAN_TxMsg1.Data[3] =(u8)(tmp >> 8) ;
            CAN_TxMsg1.Data[2] =(u8)(tmp & 0x00FF);
            CAN_Write(&CAN_TxMsg1);
        }

        if ( IsSpeedAdjustTime()) {
            tmp = ADC_GetValue(ADC_SPEED_CHANEL);
            tmp = tmp * 10;
            tmp = tmp/ 34;
            PWM_SetFrequency(tmp);
            // 1023 = 300 Hz
        }

        /*	if(	CAN_GetMsg(&RxMsgBuff)== RET_OK){

        		if (RxMsgBuff.Xtd ){		// EID
        			tmp8 =  (u8)(RxMsgBuff.timeStamp >>3) & 0xE0U;
        			if (RxMsgBuff.rtr) tmp8 |= 0x10;
        			tmp8 |= (RxMsgBuff.Length & 0x0F);
        			tmp8_A = (RxMsgBuff.ID>>24) & 0x001f;
        			tmp8_A |= (RxMsgBuff.FilterID << 5);
        			USART_SendBytesMessage (CAN_MSG_EXT_1, tmp8,(u8)((RxMsgBuff.timeStamp) & 0x00FF), tmp8_A ,(RxMsgBuff.ID>>16) & 0x00ffU);

        			USART_SendBytesMessage (CAN_MSG_EXT_2, (RxMsgBuff.ID>>8) & 0x00ffU,RxMsgBuff.ID & 0x00ffU,RxMsgBuff.Data[0], RxMsgBuff.Data[1] );
        			if (RxMsgBuff.Length > 2)
        				USART_SendBytesMessage (CAN_MSG_EXT_3, RxMsgBuff.Data[2], RxMsgBuff.Data[3], RxMsgBuff.Data[4], RxMsgBuff.Data[5]);
        			if (RxMsgBuff.Length > 6 )
        				USART_SendBytesMessage (CAN_MSG_EXT_4, RxMsgBuff.Data[6], RxMsgBuff.Data[7],0,0);
        		}else{									// standard ID
        			tmp8 =  (u8)(RxMsgBuff.timeStamp >>3) & 0xE0U;
        			if (RxMsgBuff.rtr) tmp8 |= 0x10;
        			tmp8 |= (RxMsgBuff.Length & 0x0F);
        			tmp8_A = (RxMsgBuff.ID>>8) & 0x007f;
        			tmp8_A |= (RxMsgBuff.FilterID << 3);
        			USART_SendBytesMessage (CAN_MSG_STD_1, tmp8,(u8)((RxMsgBuff.timeStamp) & 0x00FF), tmp8_A ,RxMsgBuff.ID & 0x00ff);
        			if (RxMsgBuff.Length > 0)
        				USART_SendBytesMessage (CAN_MSG_STD_2, RxMsgBuff.Data[0], RxMsgBuff.Data[1], RxMsgBuff.Data[2], RxMsgBuff.Data[3]);
        			if (RxMsgBuff.Length > 4 )
        				USART_SendBytesMessage (CAN_MSG_STD_3, RxMsgBuff.Data[4], RxMsgBuff.Data[5], RxMsgBuff.Data[6], RxMsgBuff.Data[7]);
        		}
        	}*/


        /** LED Flashing **/
        if (GetLedState()) {
            LED_ON;
        } else {
            LED_OFF;
        }

    } while (1);

}
Ejemplo n.º 3
0
int main() 
{
     pedal_node_state = pedal_state_neutral;
    LCD_Start();
    CAN_invertor_init();
    ADC_SAR_Start();
    ADC_SAR_StartConvert();
    EEPROM_Start();
    //isr_Start();
    //Timer_Start();
    CAN_timer_Start();
    CAN_Init();
    CAN_Start();

    isr_start_StartEx(&isr_start_handler);
   	Start_Reset_Write(1); /* source of interrupt (reset) */
    isr_start_ClearPending();

    isr_neutral_StartEx(&isr_neutral_handler);
    Neutral_Reset_Write(1);
    isr_neutral_ClearPending();
    
    isr_calibration_StartEx(&isr_calibration_handler);
    
    CyGlobalIntEnable;          //enable global interrupts

    //Initialize terminal
    terminal_init();
    monitor_init();

    pedal_restore_calibration_data();               //set min and max values
    pedal_set_CAN(); //Setup tunnel from pedal control to CAN
    pedal_set_monitor(); //Setup tunnel from pedal control to USB Monitor

    // Initialize global variables
    EEPROM_ERROR_LED_Write(0);
    should_calibrate = false;

    // terminal_registerCommand("newCmd", &newCmdRout);
    sendNMT(NMT_command_startRemoteNode);
    CyDelay(1000);
    pedal_node_state = pedal_state_driving;
    for(;;){
        pedal_fetch_data();
    }
    for(;;)
    {
        CyDelay(50);

        terminal_run(); // Refresh terminal
        if (pedal_node_state == pedal_state_neutral)
        {
            if (should_calibrate)
            {
                pedal_node_state = pedal_state_calibrating;

            } else if (should_turn_to_drive) {
                pedal_node_state = pedal_state_driving;
                //Start sending can message for invertor
                CAN_invertor_resume();
            }
        } else if (pedal_node_state == pedal_state_driving) {
            if (should_turn_to_neutral) {
                pedal_node_state = pedal_state_neutral;
                //Stop sending messages for invertor
                CAN_invertor_pause();
            }
        }
        //Clear all flags after handling
        should_calibrate = false;
        should_turn_to_drive = false;
        should_turn_to_neutral = false;

        uint8_t out_of_range_flag;
        double brake_percent = 0, throttle_percent = 0;
        double brake_percent_diff = 0, throttle_percent_diff = 0;
        uint8_t torque_plausible_flag;
        uint8_t brake_plausible_flag;
        pedal_fetch_data(); //Update ADC readings
        CAN_invertor_update_pedal_state(pedal_node_state);
        monitor_update_vechicle_state(pedal_node_state); //Update vecicle state
        monitor_status_update_vehicle_state(pedal_node_state);
        switch (pedal_node_state)
        {
            case pedal_state_neutral:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("NEUTRAL");

                break;
            case pedal_state_driving:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("DRIVING");
                //out_of_range_flag = pedal_get_out_of_range_flag();
                if (out_of_range_flag != 0)
                {
                    pedal_node_state = pedal_state_out_of_range;
                    break;
                }

                torque_plausible_flag = pedal_is_pedal_reading_matched(&brake_percent_diff, &throttle_percent_diff);
                if (torque_plausible_flag != 0)
                {
                    pedal_node_state = pedal_state_discrepency;
                    break;
                }

                brake_plausible_flag = pedal_is_brake_plausible(&brake_percent, &throttle_percent);
                if (brake_plausible_flag != 0)
                {
                    pedal_node_state = pedal_state_implausible;
                    break;
                }

                break;

            case pedal_state_calibrating:
                //clock_StopBlock();      //stop clock to disable interrupt
                pedal_calibrate();
                LCD_ClearDisplay();
                //isr_ClearPending();
                //clock_Start();
                // isr_calibration_Enable();
                pedal_node_state = pedal_state_neutral;
                break;

            case pedal_state_out_of_range:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("Pedal out of");
                LCD_Position(1,0);
                LCD_PrintString("range");

                out_of_range_flag = pedal_get_out_of_range_flag();
                if (out_of_range_flag == 0)
                {
                    pedal_node_state = pedal_state_driving;
                }
                break;

            case pedal_state_discrepency:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("Pedal discrepency");
                torque_plausible_flag = pedal_is_pedal_reading_matched(&brake_percent_diff, &throttle_percent_diff);
                if (torque_plausible_flag == 0)
                {
                    pedal_node_state = pedal_state_driving;
                }
                break;

            case pedal_state_implausible:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("Pedal implausible");
                brake_plausible_flag = pedal_is_brake_plausible(&brake_percent, &throttle_percent);
                if (throttle_percent < PEDAL_BRAKE_IMPLAUSIBLE_EXIT_THROTTLE_PERCENT)
                {
                    pedal_node_state = pedal_state_driving;
                }
                break;
        }

        // CyDelay(100);
    }

    return 0;
}