void setup( void ) { NVM_ResetReason_t resetReason; /* Assume data is invalid to begin with */ g_sampleDataValid = false; WDT_init(); LED_init(); /* Initialize serial UART drivers */ Serial_init(); /* Initialize sensors */ GPS_init(); OBD_init(); /* Print or initialize the reset reason */ if ( NVM_getResetReason( &resetReason ) ) { /* Valid reset reason */ if ( NVM_RESET_REASON_NORMAL != resetReason ) { Log_printf( "Reset Reason: 0x%02X\n", resetReason ); } } else { /* Invalid reset reason, initialize it */ NVM_setResetReason( NVM_RESET_REASON_NORMAL ); } /* Start the watchdog timer */ WDT_enable( WDT_TIMEOUT_MAX ); }
//------------------------------------------------------------------------------ //! Firmware start point void main(void) { // Device initialization // Initialize watchdog timer WDT_init(); // Initialize CLOCKs CLK_init(); // Initialize device system timer (used by scheduler) STIMER_init(); // Initialize task queue and schedule TASK_init(); // Enable global interrupts MCU_enableInterrupts(); // Initialize system logic LED_init(); // Start scheduler which replaces MAIN LOOP and RTOS TASK_runScheduler(); // As the scheduler has been started the firmware should never get here! while(true); }
/** * @brief Initialises IOs, Motor driver, watchdog, UART, filtered ADC * */ void init(void) { error_reg = 0x00; //! Error Register //! Defining the Outputs DDR_DRV |= DRV_EN | DRV_MODE | DRV_PHASE | DRV_SLEEP; DDR_LIMIT_A_OUT |= LIMIT_A_OUT; DDR_LIMIT_B_OUT |= LIMIT_B_OUT; DDR_DIR_A_LED |= DIR_A_LED; DDR_DIR_B_LED |= DIR_B_LED; DDR_ERR_LED |= DIR_ERR_LED; DDR_PWR_LED |= PWR_LED; SET_PWR_LED; //! Set Power LED MOTOR_BREAK; //! Engine on brake if (MCUCSR & (1<<WDRF)) //! Checking the watchdog-flag { error_reg |= ERR_WATCHDOG; //! If a watchdog reset occurs, set the bit 6 of the error register high MCUCSR = ~(1<<WDRF); //! Delete the watchdog flag } init_uart(); //! Call the UART init ADC_init (); //! Call the ADC init pRbUFuse= &rbUFuse; pRbU24 = &rbU24; pRbIDrv = &rbIDrv; rbInit(pRbUFuse); //! Init ring buffers for median rbInit(pRbU24); rbInit(pRbIDrv); Interrupt_init(); //! Call Interrupt init WDT_init (); //! Call watchdog init return; //! Return }
void Drv_Init_Phase1(void) { #if defined(DRV_GPT_GPT3) DCL_HANDLE gpt_handle; #endif //defined(DRV_GPT_GPT3) #ifndef DRV_RTC_NOT_EXIST DCL_HANDLE rtc_handler; #endif //#ifndef DRV_RTC_NOT_EXIST DCL_HANDLE uart_handle; UART_CTRL_INIT_T data_init; extern Seriport_HANDLER_T Uart_Drv_Handler; #ifdef __USB_COM_PORT_ENABLE__ DCL_HANDLE usb_dcl_handle; DCL_BOOL dcl_data; kal_bool b_is_dl_mode; usb_dcl_handle = DclUSB_DRV_Open(DCL_USB, FLAGS_NONE); DclUSB_DRV_Control(usb_dcl_handle, USB_DRV_CMD_USBDL_UPDATE_USB_DL_MODE, NULL); DclUSB_DRV_Control(usb_dcl_handle, USB_DRV_CMD_USBDL_IS_USB_DL_MODE, (DCL_CTRL_DATA_T *)&dcl_data); b_is_dl_mode = (kal_bool)dcl_data; if(b_is_dl_mode == KAL_TRUE) { UPLL_Enable(UPLL_OWNER_USB); } DclUSB_DRV_Close(usb_dcl_handle); #endif //__USB_COM_PORT_ENABLE__ #if defined(__HW_US_TIMER_SUPPORT__ ) USC_Start(); #endif #ifdef __BACKUP_DOWNLOAD_RESTORE_WITHOUT_BATTERY__ if (INT_GetSysStaByCmd(CHK_USB_META_WO_BAT, NULL)==KAL_TRUE) { DCL_HANDLE handle; DclPMU_Initialize(); handle = DclPMU_Open(DCL_PMU, FLAGS_NONE); DclPMU_Control(handle, CHR_SET_CHARGE_WITHOUT_BATTERY, NULL); DclPMU_Close(handle); } #endif //#ifdef __BACKUP_DOWNLOAD_RESTORE_WITHOUT_BATTERY__ print_bootup_trace_enter(SST_INIT_DRV1_DRVHISR); drv_hisr_init(); print_bootup_trace_exit(SST_INIT_DRV1_DRVHISR); #if defined(__DSP_FCORE4__) print_bootup_trace_enter(SST_INIT_DRV1_MDCIHW); mdci_hw_init(1,0x0); print_bootup_trace_exit(SST_INIT_DRV1_MDCIHW); #endif #if defined(__SMART_PHONE_MODEM__) print_bootup_trace_enter(SST_INIT_DRV1_CCCI); ccif_init(1,0x0); ipc_msgsvc_init(); FS_CCCI_Init(); ccci_init(CCCI_FS_CHANNEL, FS_CCCI_Callback); ccci_init(CCCI_FS_ACK_CHANNEL, FS_CCCI_Callback); uart_handle = DclSerialPort_Open(uart_port_tst_ccci, 0); DclSerialPort_RegisterCallback(uart_handle, &CCCI_Uart_Drv_Handler); uart_handle = DclSerialPort_Open(uart_port_at_ccci, 0); DclSerialPort_RegisterCallback(uart_handle, &CCCI_Uart_Drv_Handler); #if defined(__UPS_SUPPORT__) uart_handle = DclSerialPort_Open(uart_port_ccmni1_ccci, 0); DclSerialPort_RegisterCallback(uart_handle, &CCCI_Uart_Drv_Handler); uart_handle = DclSerialPort_Open(uart_port_ccmni2_ccci, 0); DclSerialPort_RegisterCallback(uart_handle, &CCCI_Uart_Drv_Handler); uart_handle = DclSerialPort_Open(uart_port_ccmni3_ccci, 0); DclSerialPort_RegisterCallback(uart_handle, &CCCI_Uart_Drv_Handler); #endif /* __UPS_SUPPORT__ */ uart_handle = DclSerialPort_Open(uart_port_gps_ccci, 0); DclSerialPort_RegisterCallback(uart_handle, &CCCI_Uart_Drv_Handler); print_bootup_trace_exit(SST_INIT_DRV1_CCCI); #endif /* __SMART_PHONE_MODEM__ */ #if defined(DRV_EMIMPU) print_bootup_trace_enter(SST_INIT_DRV1_EMIMPU); emimpu_init(); print_bootup_trace_exit(SST_INIT_DRV1_EMIMPU); #endif /* DRV_EMIMPU */ print_bootup_trace_enter(SST_INIT_DRV1_LPWR); lpwr_init(); print_bootup_trace_exit(SST_INIT_DRV1_LPWR); print_bootup_trace_enter(SST_INIT_DRV1_DRVPDN); DRVPDN_ini(); print_bootup_trace_exit(SST_INIT_DRV1_DRVPDN); print_bootup_trace_enter(SST_INIT_DRV1_PWM); DclPWM_Initialize(); print_bootup_trace_exit(SST_INIT_DRV1_PWM); /*To get all customized data*/ print_bootup_trace_enter(SST_INIT_DRV1_CUSTOM); DclSADC_Initialize(); Drv_Customize_Init(); custom_drv_init(); print_bootup_trace_exit(SST_INIT_DRV1_CUSTOM); #if defined(DRV_GPT_GPT3) print_bootup_trace_enter(SST_INIT_DRV1_GPT3); /*turn on gpt3 to count powen on period*/ DclFGPT_Initialize(); gpt_handle=DclFGPT_Open(DCL_GPT_FreeRUN3,0); DclFGPT_Control(gpt_handle,FGPT_CMD_START,0); DclFGPT_Close(gpt_handle); print_bootup_trace_exit(SST_INIT_DRV1_GPT3); #endif #if !defined(__L1_STANDALONE__) && !defined(__MAUI_BASIC__) && !defined(__SMART_PHONE_MODEM__) print_bootup_trace_enter(SST_INIT_DRV1_VISUAL); Visual_Init(); print_bootup_trace_exit(SST_INIT_DRV1_VISUAL); #endif /*!defined(__L1_STANDALONE__) && !defined(__MAUI_BASIC__) && !defined(__SMART_PHONE_MODEM__)*/ #if defined(DRV_TIMING_DEBUG) && defined(DRV_GPT_6218B_GPT3_TIMING_DEBUG) DRV_MISC_WriteReg(0x8010001c,1); DRV_MISC_WriteReg(0x80100024,0x04); //DRV_WriteReg(0x80100024,1); // (1/16K) #endif #ifdef DRV_MISC_DMA_NO_MEMCPY DRV_MEMCPY = (MEMCPY_FUNC)0x48000150; #elif defined(DRV_MISC_DMA_MEMCPY) DRV_MEMCPY_PTR = (MEMCPY_FUNC)0x48000134; #endif print_bootup_trace_enter(SST_INIT_DRV1_GPTI); DclSGPT_Initialize(); print_bootup_trace_exit(SST_INIT_DRV1_GPTI); print_bootup_trace_enter(SST_INIT_DRV1_WDT); WDT_init(); print_bootup_trace_exit(SST_INIT_DRV1_WDT); print_bootup_trace_enter(SST_INIT_DRV1_DMA); DMA_Ini(); print_bootup_trace_exit(SST_INIT_DRV1_DMA); #ifndef DRV_RTC_NOT_EXIST // need to set XOSC earlier print_bootup_trace_enter(SST_INIT_DRV1_XOSC); rtc_handler = DclRTC_Open(DCL_RTC,FLAGS_NONE); DclRTC_Control(rtc_handler, RTC_CMD_SETXOSC, (DCL_CTRL_DATA_T *)NULL); print_bootup_trace_exit(SST_INIT_DRV1_XOSC); #endif /*DRV_RTC_NOT_EXIST*/ print_bootup_trace_enter(SST_INIT_DRV1_ADC); DclHADC_Initialize( ); print_bootup_trace_exit(SST_INIT_DRV1_ADC); #ifdef __CS_FAC_DET__ print_bootup_trace_enter(SST_INIT_DRV1_CSFACDET); cs_fac_det = cs_fac_det_get_interface(); cs_fac_det->drv_init(); print_bootup_trace_exit(SST_INIT_DRV1_CSFACDET); #endif // #ifdef __CS_FAC_DET__ #ifdef __DRV_EXT_ACCESSORY_DETECTION__ #ifdef __USB_COM_PORT_ENABLE__ usb_dcl_handle = DclUSB_DRV_Open(DCL_USB, FLAGS_NONE); DclUSB_DRV_Control(usb_dcl_handle, USB_DRV_CMD_USBDL_IS_USB_DL_MODE, (DCL_CTRL_DATA_T *)&dcl_data); b_is_dl_mode = (kal_bool)dcl_data; DclUSB_DRV_Close(usb_dcl_handle); if(b_is_dl_mode == KAL_FALSE) #endif { print_bootup_trace_enter(SST_INIT_DRV1_EXTACCDET); aux_ext_acc_det = aux_custom_get_ext_accessory_det(); aux_ext_acc_det->drv_init(); print_bootup_trace_exit(SST_INIT_DRV1_EXTACCDET); } #endif // #ifdef __DRV_EXT_ACCESSORY_DETECTION__ //#if (defined(__ACCDET_SUPPORT__) && !defined(__L1_STANDALONE__)) print_bootup_trace_enter(SST_INIT_DRV1_ACCDET); DclAUX_Initialize(); print_bootup_trace_exit(SST_INIT_DRV1_ACCDET); //#endif //#if defined(__ACCDET_SUPPORT__) #if defined(__RESOURCE_MANAGER__) print_bootup_trace_enter(SST_INIT_DRV1_RM); RMInit(); print_bootup_trace_exit(SST_INIT_DRV1_RM); #endif //__RESOURCE_MANAGER__ #ifndef DRV_LCD_NOT_EXIST print_bootup_trace_enter(SST_INIT_DRV1_LCD); lcd_system_init(); print_bootup_trace_exit(SST_INIT_DRV1_LCD); #endif /*DRV_LCD_NOT_EXIST*/ #ifndef DRV_RTC_NOT_EXIST #ifdef DRV_RTC_HW_CALI print_bootup_trace_enter(SST_INIT_DRV1_RTCHW); DclRTC_Control(rtc_handler, RTC_CMD_HW_INIT, (DCL_CTRL_DATA_T *)NULL); print_bootup_trace_exit(SST_INIT_DRV1_RTCHW); #endif #endif /*DRV_RTC_NOT_EXIST*/ DclPW_Initialize(); DclPMU_Initialize(); Drv_PW_Init(); /* update the system boot mode */ /*lint -e552*/ system_boot_mode = Drv_query_boot_mode(); /*lint +e552*/ print_boot_mode(); #ifdef __DMA_UART_VIRTUAL_FIFO__ // print_bootup_trace_enter(SST_INIT_DRV1_DMAVFIFO); // DMA_Vfifo_init(); // print_bootup_trace_exit(SST_INIT_DRV1_DMAVFIFO); #if defined(__MD_STANDALONE__) // This is to configure AP side VFIFO ALT register, to avoid un-init AP side DMA setting // conflict MD side setting // This is only used in MD DVT load, when both side are ready, we shouldn't overwrite AP side setting via back door // Hack DMA channel 13 (VFIFO channel) in AP side in order to avoid conflict *(volatile kal_uint32 *)(0xF0022000 + 0xD40) = 0x3F; // Hack DMA channel 14 (VFIFO channel) in AP side in order to avoid conflict *(volatile kal_uint32 *)(0xF0022000 + 0xE40) = 0x3F; #endif // #if defined(__MD_STANDALONE__) #endif print_bootup_trace_enter(SST_INIT_DRV1_UART1); data_init.u4Flag = KAL_FALSE; uart_handle = DclSerialPort_Open(uart_port1, 0); DclSerialPort_RegisterCallback(uart_handle, &Uart_Drv_Handler); // Initialization DclSerialPort_Control(uart_handle,SIO_CMD_INIT,(DCL_CTRL_DATA_T *)&data_init); print_bootup_trace_exit(SST_INIT_DRV1_UART1); // Register the callback function print_bootup_trace_enter(SST_INIT_DRV1_UART2); uart_handle = DclSerialPort_Open(uart_port2, 0); DclSerialPort_RegisterCallback(uart_handle, &Uart_Drv_Handler); // Initialization DclSerialPort_Control(uart_handle,SIO_CMD_INIT,(DCL_CTRL_DATA_T *)&data_init); print_bootup_trace_exit(SST_INIT_DRV1_UART2); #ifdef __UART3_SUPPORT__ print_bootup_trace_enter(SST_INIT_DRV1_UART3); // Register the callback function uart_handle = DclSerialPort_Open(uart_port3, 0); DclSerialPort_RegisterCallback(uart_handle,&Uart_Drv_Handler); DclSerialPort_Control(uart_handle,SIO_CMD_INIT,(DCL_CTRL_DATA_T *)&data_init); print_bootup_trace_exit(SST_INIT_DRV1_UART3); #endif }
void main( void ) { WDT_init(); UCS_init(); TimerA1_init(); TimerB0_init(); UART_init(UCA1,115200); RotaryEncoder_init(); keyboard_init(); // GUI_init(); Motor_init(); { RP.myOutput = 0; RP.myInput = &E1_absPulseCnt; RP.mySetpoint = 0; RP.inAuto = 1; PID_setOutputLimits(&RP, (signed long)-100, (signed long)100);//(加速度) // PID_setOutputLimits(&RP, (signed long)-600, (signed long)600); RP.SampleTime = 5; PID_setControllerDirection(&RP, 1); PID_setTunings(&RP, 184, 7000, 34); //400,0,0 //184,6000,35 if (TimeBase>RP.SampleTime) RP.lastTime = TimeBase-RP.SampleTime; else RP.lastTime = 0; } _EINT(); // while(UCA1_GET_CHAR(&command)); _DINT(); E1_pulseCnt=2000; E2_pulseCnt=0; E1_pulseInLastMs=2000; _EINT(); startTime = TimeBase; // Motor_config(362); // Clear_Screen(); // TFT_Menu_StartMenu(); while(function_3()); /* while(1) { //主要任务 switch(function) { case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: if (E1_absPulseCnt==0) { function6Flag=1; function6_E2_pulseCnt=E2_pulseCnt; function6_speed=400; } if (function6Flag) { // if((!PID_compute(&RP))&&(abs(E2_pulseCnt-function6_E2_pulseCnt)<2000)) { speed+=RP.myOutput; speed-=E2_interval; if (speed>0) speedSign = 1; else if (speed<0) speedSign = -1; else speedSign = 0; if (abs(speed)>900) speed=speedSign*900; if (speedSign>0) Motor_config(speedSign*400+speed/3+5); else Motor_config(speedSign*400+speed/3+5); // } if ((abs(E1_absPulseCnt)<500)&&(abs(E1_absPulseCnt)>2)) { RP.inAuto=1; // PID_setMode(&RP,AUTOMATIC); if(!PID_compute(&RP)) { speed+=RP.myOutput; speed-=E2_interval*3; if (speed>0) speedSign = 1; else if (speed<0) speedSign = -1; else speedSign = 0; if (abs(speed)>900) speed=speedSign*900; if (speedSign>0) { Motor_config(speedSign*400+speed/3+5); } else { Motor_config(speedSign*400+speed/3+5); } } if(abs(E2_pulseCnt-function6_E2_pulseCnt)>2000) { function6Flag=0; Motor_config(0); } } } else { Motor_config(0); if ((abs(E1_absPulseCnt)>1990)&&(function6Flag)) { function6Flag=0; } } break; default: break; } }*/ }
// main loop int main(void){ uint8_t stateDS18=DS18B20_STATUS_READY; #ifdef USE_WDT WDT_init(WDTO_8S); #endif initGPIO(); LCD_init(); showLcdSplash(); setLcdInitialFields(); paramLoadFromEeprom(); USART1_config(USART1_MY_UBBRN,USART_DATA_FORMAT_8BITS|USART_STOP_BITS_1,USART_TRANSMIT_ENABLE|USART_RECEIVE_ENABLE| USART_INTERRUPT_ENABLE); USART1_sendStr("Hello"); schedulerInit(); // check for the default values #ifdef USE_WDT WDT_init(WDTO_8S); #endif sei(); //enable interrups // loop while while(1){ // cintrol zone if(flagTaskControl){ #ifdef USE_WDT WDT_reset(); #endif LED_RUN_OFF; // fire the DS if(stateDS18==DS18B20_STATUS_READY){ DS18B20_startConv(); stateDS18=DS18B20_STATUS_CONV; // check if convertion ended }else if(stateDS18==DS18B20_STATUS_CONV){ if(DS18B20_convComplete()){ stateDS18=DS18B20_STATUS_END_CONV; } // convertion ready }else if (stateDS18==DS18B20_STATUS_END_CONV){ LED_RUN_ON; currentTemp=DS18B20_getTemp(); stateDS18=DS18B20_STATUS_READY; currentStatus = checkTempError(currentTemp,currentTempSetPoint,currentHistSetPoint); // chec the out setOutputRelay(currentStatus); _delay_ms(50); LED_RUN_OFF; } flagTaskControl=0; } // user bottons area if(flagTaskReadButtons){ #ifdef USE_WDT WDT_reset(); #endif uint8_t portVal = readButtons(); uint8_t code = decodeButton(portVal); code = debounceKey(code); #ifdef MAIN_DEBUG sprintf(debugBuffer,"Key %d",code); USART1_sendStr(debugBuffer); #endif stateMachine(code); // go to machine flagTaskReadButtons=0; } // lcd update area if(flagTaskUpdateLcd){ #ifdef USE_WDT WDT_reset(); #endif //showLcdSavedMessage(); updateLcd(); // update the lcd flagTaskUpdateLcd=0; } // save to eeprom if(flagSaveParametersEeprom){ #ifdef USE_WDT WDT_reset(); #endif paramSavetoEeprom(); // save value sto eeprom showLcdSavedMessage(); setLcdInitialFields(); updateLcd(); flagSaveParametersEeprom=0; } } }
//================================================================================== //================================================================================== //================================================================================== int main() { bool ver = 0; uint8_t cnt_link = 0; PLL_init(); GPIO_init(); TIM1_init(); TIM2_init(); TIM3_init(); UART_init(); WDT_init(); EEPROM_init(); SysTick_Config(SystemCoreClock/800);//~10 ms time.t1=5; time.t2=5; time.t3=5; time.t4=5; delay_ms(100); Segment[0]=0xFF; Segment[1]=0xFF; Segment[2]=0xFF; Segment[3]=0xFF; Segment[4]=0xFF; Segment[5]=0xFF; Segment[6]=0x7F; ALARM_ON; delay_ms(100); ALARM_OFF; delay_ms(1000); // // while(1) { if( TX_st ) { TX_st = 0; if( cnt_link < LINK_COUNT ) { link_PKBA(); ++cnt_link; } else { PKDU.error = true; cnt_link = 0; } } if( RX_ok && !PKDU.error ) { uint8_t cnt_byte = 0, sum = 0; while( cnt_byte < RX_FRAME_SIZE ) { sum += ArrayRX_PKBA[cnt_byte]; ++cnt_byte; } sum += 0xAA; if( !sum ) { StatusPKBA.reg0 = ArrayRX_PKBA[0]; StatusPKBA.reg1 = ArrayRX_PKBA[1]; StatusPKBA.Error = (ArrayRX_PKBA[3]<<8) | ArrayRX_PKBA[2]; StatusPKBA.RabReg0 = ArrayRX_PKBA[4]; StatusPKBA.UGen = (ArrayRX_PKBA[6]<<8) | ArrayRX_PKBA[5]; StatusPKBA.IGen = (ArrayRX_PKBA[8]<<8) | ArrayRX_PKBA[7]; StatusPKBA.DT = ArrayRX_PKBA[9]; StatusPKBA.DM = ArrayRX_PKBA[10]; StatusPKBA.TM = ArrayRX_PKBA[11]; StatusPKBA.NDiz = (ArrayRX_PKBA[13]<<8) | ArrayRX_PKBA[12]; StatusPKBA.TBapEx = ArrayRX_PKBA[14]; StatusPKBA.TBapIn = ArrayRX_PKBA[15]; StatusPKBA.Led1 = ArrayRX_PKBA[16]; StatusPKBA.Led2 = ArrayRX_PKBA[17]; if( !ver ) { ver = true; show_ver(); delay_ms( 1500 ); Segment[ 0 ] = 0; Segment[ 1 ] = 0; Segment[ 2 ] = 0; Segment[ 3 ] = 0; Segment[ 4 ] = 0; } cnt_link=0; } RX_ok=0; } __nop(); Set_Error(); if(!PKDU.error) ShowParam(); if((PKDU.StatusKN>>4)&1) { maska_err = 0; StatusPKBA.Error=0; PKDU.error=false; cnt_link = 0; } if(((PKDU.StatusKN>>5)&1) && ((PKDU.StatusKN>>6)&1)) { Segment[0] = 0xFF; Segment[1] = 0xFF; Segment[2] = 0xFF; Segment[3] = 0xFF; Segment[4] = 0xFF; Segment[5] = 0xFF; Segment[6] = 0x7F; while(((PKDU.StatusKN>>5)&1)&&((PKDU.StatusKN>>6)&1)) IWDG_ReloadCounter(); } ControlZvonok(); write_hour(); IWDG_ReloadCounter(); } }
void Key_init() { WheelADC_init(); ButtomKey_init(); WDT_init(); }