BOOL Meter_Init(void) { Meter_TotalEnergy = 0; Meter_TotalEnergyCost = 0; Meter_PowerFactor = 0; Meter_VoltageRMS = 0; Meter_CurrentRMS = 0; Meter_Time = 0; PerformingCalculations = bFALSE; SamplesReady = OS_SemaphoreCreate(0); RunCalculations = OS_SemaphoreCreate(0); return (Sampler_Init(sampleCallBack, 0) && Tariff_Init()); }
BOOL RTC_Init() { //enable the clock gate to the RTC module SIM_SCGC6 |= SIM_SCGC6_RTC_MASK; //configure the required load capacitance (18pf) RTC_CR |= (RTC_CR_SC2P_MASK | RTC_CR_SC16P_MASK); //enable the 32.768 KHZ crystal oscillator RTC_CR |= RTC_CR_OSCE_MASK; //lock the control register RTC_LR |= RTC_LR_CRL_MASK; //enable the time counter and set the time second register start from 0 RTC_SR |= RTC_SR_TCE_MASK; // RTC_TSR = 0; //enable the time second interrupt RTC_IER |= RTC_IER_TSIE_MASK; // initialize NVIC // Vector 0x53, IRQ = 67, location = IRQ mod 32 = 3 // NVIC non-IPR=2 IPR=16 // clear pending interrupts NVICICPR2 = NVIC_ICPR_CLRPEND(1 << 3); //Enable interrupts NVICISER2 = NVIC_ISER_SETENA(1 << 3); RTCCallbackSemaphore = OS_SemaphoreCreate(0); return bTRUE; }
/*! * @brief The entry point into the program. */ int main(void) /*lint -restore Enable MISRA rule (6.3) checking. */ { /* Write your local variable definition here */ /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ PE_low_level_init(); /*** End of Processor Expert internal initialization. ***/ /* Write your code here */ OS_Init(CPU_CORE_CLK_HZ); //Important semaphores. Do it now! EventSemaphore = OS_SemaphoreCreate(0); InitSemaphore = OS_SemaphoreCreate(1); RtcSemaphore = OS_SemaphoreCreate(0); //Make some threads. CREATE_THREAD(InitThread, NULL, InitThreadStack, TP_INITTHREAD); CREATE_THREAD(MainThread, NULL, MainThreadStack, TP_MAINTHREAD); CREATE_THREAD(EventThread, NULL, EventThreadStack, TP_EVENTTHREAD); CREATE_THREAD(PacketThread, NULL, PacketThreadStack, TP_PACKETTHREAD); CREATE_THREAD(RtcThread, NULL, RtcThreadStack, TP_RTCTHREAD); //GOGOGOGOOGOGOGO OS_Start(); //We don't go here. /*** Don't write any code pass this line, or it will be deleted during code generation. ***/ /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/ #ifdef PEX_RTOS_START PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */ #endif /*** End of RTOS startup code. ***/ /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/ for(;;){} /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/ } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
OS_Mutex_t *OS_MutexCreate(const char *name) { OS_Mutex_t *mutex; mutex = (OS_Mutex_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Mutex_t)); if(mutex == NULL) return NULL; mutex->semaphore = OS_SemaphoreCreate(name, 1); if(mutex->semaphore == NULL) return NULL; mutex->thread = NULL; mutex->count = 0; return mutex; }
void OS_Init(uint32 *heapStorage, uint32 bytes) { int i; OS_AsmInterruptInit(); //Patch interrupt vector OS_InterruptMaskClear(0xffffffff); //Disable interrupts HeapArray[0] = OS_HeapCreate("Default", heapStorage, bytes); HeapArray[1] = HeapArray[0]; SemaphoreSleep = OS_SemaphoreCreate("Sleep", 0); SemaphoreRelease = OS_SemaphoreCreate("Release", 1); SemaphoreLock = OS_SemaphoreCreate("Lock", 1); for(i = 0; i < OS_CPU_COUNT; ++i) OS_ThreadCreate("Idle", OS_IdleThread, NULL, 0, 256); #ifndef DISABLE_IRQ_SIM if((OS_InterruptStatus() & (IRQ_COUNTER18 | IRQ_COUNTER18_NOT)) == 0) { //Detected that running in simulator so create SimIsr thread UartPrintfCritical("SimIsr\n"); OS_ThreadCreate("SimIsr", OS_IdleSimulateIsr, NULL, 1, 0); } #endif //DISABLE_IRQ_SIM //Plasma hardware dependent OS_InterruptRegister(IRQ_COUNTER18 | IRQ_COUNTER18_NOT, OS_ThreadTickToggle); OS_InterruptMaskSet(IRQ_COUNTER18 | IRQ_COUNTER18_NOT); }
OS_Heap_t *OS_HeapCreate(const char *name, void *memory, uint32 size) { OS_Heap_t *heap; assert(((uint32)memory & 3) == 0); heap = (OS_Heap_t*)memory; heap->magic = HEAP_MAGIC; heap->name = name; heap->semaphore = OS_SemaphoreCreate(name, 1); heap->available = (HeapNode_t*)(heap + 1); heap->available->next = &heap->base; heap->available->size = (size - sizeof(OS_Heap_t)) / sizeof(HeapNode_t); heap->base.next = heap->available; heap->base.size = 0; return heap; }
OS_MQueue_t *OS_MQueueCreate(const char *name, int messageCount, int messageBytes) { OS_MQueue_t *queue; int size; size = messageBytes / sizeof(uint32); queue = (OS_MQueue_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_MQueue_t) + messageCount * size * 4); if(queue == NULL) return queue; queue->name = name; queue->semaphore = OS_SemaphoreCreate(name, 0); if(queue->semaphore == NULL) return NULL; queue->count = messageCount; queue->size = size; queue->used = 0; queue->read = 0; queue->write = 0; return queue; }
OS_Timer_t *OS_TimerCreate(const char *name, OS_MQueue_t *mQueue, uint32 info) { OS_Timer_t *timer; OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER); if(SemaphoreTimer == NULL) { SemaphoreTimer = OS_SemaphoreCreate("Timer", 0); OS_ThreadCreate("Timer", OS_TimerThread, NULL, 250, 2000); } OS_SemaphorePost(SemaphoreLock); timer = (OS_Timer_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Timer_t)); if(timer == NULL) return NULL; timer->name = name; timer->callback = NULL; timer->mqueue = mQueue; timer->next = NULL; timer->prev = NULL; timer->info = info; timer->active = 0; return timer; }
void QSYS_Init(void) { u8 *ptr1; u8 *ptr2; // FIL *ptr3; OS_CPU_SysTickInit();//Initialize the SysTick. #if OS_USE_UCOS CPU_IntSrcPrioSet(CPU_INT_PENDSV,15); CPU_IntSrcPrioSet(CPU_INT_SYSTICK,15); #endif SetupHardware(); Debug("sizeof(INPUT_EVENT)=%d\n\r",sizeof(INPUT_EVENT));//for debug by karlno Debug("sizeof(PAGE_ATTRIBUTE)=%d\n\r",sizeof(PAGE_ATTRIBUTE));//for debug by karlno Debug("sizeof(IMG_BUTTON_OBJ)=%d\n\r",sizeof(IMG_BUTTON_OBJ));//for debug by karlno Debug("sizeof(CHAR_BUTTON_OBJ)=%d\n\r",sizeof(CHAR_BUTTON_OBJ));//for debug by karlno Debug("sizeof(MUSIC_EVENT)=%d\n\r",sizeof(MUSIC_EVENT));//for debug by karlno //Debug("sizeof(QSYS_MSG_BOX)=%d\n\r",sizeof(QSYS_MSG_BOX));//for debug by karlno if(SysEvt_MaxNum>32) { Debug("Error:SysEvt_MaxNum(%d) is too big!!!\n\r",SysEvt_MaxNum); Q_ErrorStopScreen("Error:SysEvt_MaxNum is too big!!!\n\r"); } gLCD_Mutex=OS_MutexCreate(); //创建触摸输入中断发生信号量 gTouchHandler_Sem=OS_SemaphoreCreate(0); gAllowTchHandler_Sem=OS_SemaphoreCreate(0); gVsDreq_Sem=OS_SemaphoreCreate(0); gRfRecvHandler_Sem=OS_SemaphoreCreate(0); //创建事件传递数据 gInputHandler_Queue=OS_MsgBoxCreate("Input Event",sizeof(INPUT_EVENT),16); gMusicHandler_Queue=OS_MsgBoxCreate("MusicKV Event",sizeof(MUSIC_EVENT),8); #if 0//debug OS_TaskCreate((void (*) (void *)) T1_Task,(void *) 0, (OS_STK *) &T1_TaskStack[OS_MINIMAL_STACK_SIZE - 1],T1_TASK_pRIORITY); //OS_TaskCreate(T2_Task , ( signed OS_CHAR * ) "T2 Handler", OS_MINI_STACK_SIZE, NULL, MUSIC_HANDLER_TASK_PRIORITY, NULL ); OS_TaskDelete(SYSTEM_TASK_PRIORITY); #endif #if 1 //首次下载完成后可关闭此处代码,防止误下载 Debug("\n\r-------------------SPI FLASH DOWNLOAD FROM SD------------------\n\r"); ptr1=(u8 *)Q_Mallco(CfgFileSize); ptr2=(u8 *)Q_Mallco(SPI_FLASH_PAGE_SIZE); SpiFlashDownFromSD(FALSE,"System/Down.cfg",ptr1,ptr2); Q_Free(ptr2); Q_Free(ptr1); Debug("-------------------SPI FLASH DOWNLOAD FROM SD------------------\n\r\n\r"); #endif Debug("----------------DATABASE SETTING INITIALIZATION----------------\n\r"); DB_Init(); Debug("----------------DATABASE SETTING INITIALIZATION----------------\n\r\n\r"); RTC_SetUp(); Gui_Init(); //图像库初始化 Gui_SetBgLight(Q_DB_GetValue(Setting_BgLightScale,NULL));//设置背光亮度 OS_TaskCreate(MusicHandler_Task,"Music",OS_MINIMAL_STACK_SIZE*8,NULL,MUSIC_TASK_PRIORITY,&MusicHandler_Task_Handle); OS_TaskCreate(TouchHandler_Task,"Touch",OS_MINIMAL_STACK_SIZE*3,NULL,TOUCH_TASK_PRIORITY,&TouchHandler_Task_Handle); OS_TaskCreate(KeysHandler_Task,"Keys",OS_MINIMAL_STACK_SIZE*2,NULL,KEYS_TASK_PRIORITY,&KeysHandler_Task_Handle); OS_TaskCreate(QWebHandler_Task,"QWeb",OS_MINIMAL_STACK_SIZE*8,NULL,RF_DATA_TASK_PRIORITY,&QWebHandler_Task_Handle); OS_TaskDelay(100); OS_TaskStkCheck(FALSE); if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0) //如果没按下Key-PE2,就正常启动串口中断 { #if !QXW_RELEASE_VER//for debug USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//release版本请关掉此句,免得不懂的用户说板卡老死机。 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); #endif } }