FORCE_INLINE void GeneralInit(void){ wdt_enable(WDTO_2S); ACSR = 1<<ACD; // Disable analog comparator TimerInit(); // LED init LED_DDR |= (1<<LED_P); // CC init TimerResetDelay(&CCSrv.Timer); CC.TX_Pkt.PacketID = 1; CC_Init(); CCSrv.Pwr = HiPwr; CC_SetChannel(45); // Set needed channel // Battery BatteryInit(); sei(); }
static void hal_initialising_state_enter(void) { DEBUG(("hal initialising state enter...\n")); /** init pio **/ pioInit(&hal.pio_state, getHalTask()); /** init battery lib, set 0 to read once **/ /*BatteryInit(&hal.battery_state, getHalTask(), BATTERY_READING_SOURCE, BATTERY_POLLING_PERIOD);*/ BatteryInit(&hal.battery_state, getHalTask(), BATTERY_READING_SOURCE, 0); /** floating, if user release button and power cable unplugged, system stop **/ disableLDO(); update_indication(); }
uint16_t GetBatteryStatus(uint16_t *value) { BatteryInit(); ADCON0 = 1; //enable a/d module ADCON0bits.GO = 1; while(ADCON0bits.DONE); while(BusyADC()); uint16_t adc_val = ADRES*3.3/1024*100; //printf("Battery: %d \n", adc_val); ADCON0 = 0; //disable a/d module //printf("BAT: %d\n", adc_val); *value = adc_val; return adc_val; }
/* user main function, called by AppFramework after system init done && wifi * station on in user_main thread. */ OSStatus user_main( app_context_t * const app_context ) { user_log_trace(); OSStatus err = kUnknownErr; unsigned char rdata[64]; unsigned char sdata[64]; uint16_t datalen; require(app_context, exit); // platform initialize AaSysComInit(); AaSysLogInit(); // application initialize ControllerBusInit(); OuterTriggerInit(NULL); TemperatureInit(); // will be support in release 2 BatteryInit(); // reset f411 and wait until it startup ResetF411(); #if 1 MOInit(); #endif err = SntpInit(app_context); if(kNoErr != err) { AaSysLogPrint(LOGLEVEL_ERR, "SntpInit finished with err code %d", err); } else { AaSysLogPrint(LOGLEVEL_INF, "SntpInit success"); } #if 1 DeviceInit(app_context); HealthInit(app_context); LightsInit(app_context); MusicInit(app_context); // start the downstream thread to handle user command err = mico_rtos_create_thread(&user_downstrem_thread_handle, MICO_APPLICATION_PRIORITY, "user_downstream", user_downstream_thread, STACK_SIZE_USER_DOWNSTREAM_THREAD, app_context ); require_noerr_action( err, exit, user_log("ERROR: create user_downstream thread failed!") ); #endif user_log("[DBG]net_main: Appilcation Initialize success @"SOFTWAREVERSION); // user_main loop, update oled display every 1s while(1){ #if 1 mico_thread_sleep(MICO_WAIT_FOREVER); #else mico_thread_sleep(5); datalen = user_uartRecv(rdata, 5); if(datalen) { user_log("[DBG]user_main: Usart recevice datalen %d", datalen); user_log("[DBG]user_main: receive %.*s", datalen, rdata); } else { user_log("[DBG]user_main: Usart didn't recevice data"); } mico_thread_sleep(2); sprintf(sdata, "hello, world!\r\n"); user_uartSend(sdata, strlen(sdata)); #endif } exit: if(kNoErr != err){ user_log("[ERR]user_main: user_main thread exit with err=%d", err); } mico_rtos_delete_thread(NULL); // delete current thread return err; }
int main( void ) { SysInit(); LedInit(); BatteryInit(); ComInit(); TimerInit(); INTEN while(1) { if(TimerGetTimeFlag() > 10) { TimerClearTimeFlag(); BattertSleep(); } if(TimerGetBatteryFlag() > 0x01) { TimerClearBatteryFlag(); if(BatterGetFlag() == 0) { BatterSetFlag(1); BattertGet(); ComWatchSendCmd(dce_powe,BattertGetLevel(),0x00,0x00); } } LedTimeService(); if(ComWatchGetFlag() == 0x80) { ComWatchClearFlag(); TimerClearTimeFlag(); BattertOpen(); BatterSetFlag(0);//可以获取电量 if(ComWatchGetData(0) != 0x00) { BattertOpen(); } LedSetModeFlicker(1); if(BattertGetLevel() > 0) { switch(ComWatchGetData(0)) {//电量不足 case front: switch(ComWatchGetData(1)) { case add_stal:ComRearSendCmd(add_stal,0x00,0x00,0x00);break; case sub_stal:ComRearSendCmd(sub_stal,0x00,0x00,0x00);break; case add_setp:ComRearSendCmd(add_setp,0x00,0x00,0x00);break; case sub_setp:ComRearSendCmd(sub_setp,0x00,0x00,0x00);break; case set_inti:ComRearSendCmd(set_inti,0x00,0x00,0x00);break; } break; case behind: switch(ComWatchGetData(1)) { case exchange_stal: ComBehindSendCmd(exchange_stal,0x00,0x00,0x00);break; case add_setp:ComBehindSendCmd(add_setp,0x00,0x00,0x00);break; case sub_setp:ComBehindSendCmd(sub_setp,0x00,0x00,0x00);break; case set_inti:ComBehindSendCmd(set_inti,0x00,0x00,0x00);break; } break; default:break; } } else { } } if(ComRearGetFlag() == 0x80) { ComRearClearFlag(); switch(ComRearGetData(0)) { case dce_gear: ComBehindSendCmd(ask_rear,ComRearGetData(1),0x00,0x00); ComWatchSendCmd(behind,dce_gear,ComRearGetData(1),BattertGetLevel()); break; case stuck: ComWatchSendCmd(stuck,dce_gear,ComRearGetData(1),BattertGetLevel()); break; default:break; } } if(ComBehindGetFlag() == 0x80) { ComBehindClearFlag(); switch(ComBehindGetData(0)) { case dce_gear: ComWatchSendCmd(front,dce_gear,ComBehindGetData(1),BattertGetLevel()); break; case stuck: ComWatchSendCmd(stuck,dce_gear,ComBehindGetData(1),BattertGetLevel()); break; default:break; } } } }
static void hal_initialising_handler(Task task, MessageId id, Message message) { switch (id) { case PIO_RAW: { DEBUG(("hal initialising state, PIO_RAW message arrived...\n")); /** update charging state, and no check, even battery low we have nothing to do **/ pio_raw_handler(message); update_indication(); } break; case BATTERY_READING_MESSAGE: { DEBUG(("hal warming-up state, BATTERY_READING_MESSAGE message arrived...\n")); /** update battery reading and no check, even battery low we have nothing to do **/ if (hal.voltage == K_VoltageInit) { BatteryInit(&hal.battery_state, getHalTask(), BATTERY_READING_SOURCE, BATTERY_POLLING_PERIOD); } battery_message_handler(message); update_indication(); } break; case HAL_POWER_BUTTON_HELD_SHORT: { DEBUG(("hal initialising state, POWER_BUTTON_HELD_SHORT message arrived...\n")); /** let error mananger check initialization result, and determing the control flow **/ initialisationFinished(); hal_set_state(ACTIVATING); } break; case POWER_BUTTON_PRESS: DEBUG(("hal initialising state, POWER_BUTTON_PRESS message arrived...\n")); MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT, 0, 2000); MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_LONG, 0, 8000); break; case POWER_BUTTON_RELEASE: DEBUG(("hal initialising state, POWER_BUTTON_RELEASE message arrived...\n")); MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT); MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_LONG); break; case FUNCTION_BUTTON_DFU: DEBUG(("hal initialising state, FUNCTION_BUTTON_DFU message arrived...\n")); ledsPlay(BEEP_ONCE);/*if need beep we should add a timeout message then to enter DFU mode*/ MessageSendLater(getHalTask(), HAL_FUNCTION_BUTTON_DFU_TIMEOUT, 0, BEEP_ONCE_DURATION + 100); break; case HAL_FUNCTION_BUTTON_DFU_TIMEOUT: DEBUG(("hal initialising state, HAL_FUNCTION_BUTTON_DFU_TIMEOUT message arrived...\n")); BootSetMode(0); break; default: break; } }