/*! * @cond DOXYGEN_PRIVATE * * @brief Atomically captures current time into HWTIMER_TIME_STRUCT structure * * Corrects/normalizes the values if necessary (interrupt pending, etc.) * * @param hwtimer[in] Pointer to hwtimer structure. * @param time[out] Pointer to time structure. This value is filled with current value of the timer. * * @return kHwtimerSuccess Success. * * @see HWTIMER_SYS_PitInit * @see HWTIMER_SYS_PitDeinit * @see HWTIMER_SYS_PitSetDiv * @see HWTIMER_SYS_PitStart * @see HWTIMER_SYS_PitStop * @see HWTIMER_SYS_PitIsr * @see HWTIMER_SYS_PitIsrShared */ static _hwtimer_error_code_t HWTIMER_SYS_PitGetTime(hwtimer_t *hwtimer, hwtimer_time_t *time) { uint32_t pitChannel; uint32_t tempCval; uint32_t baseAddr = g_pitBaseAddr[0]; assert(NULL != hwtimer); assert(NULL != time); pitChannel = hwtimer->llContext[0U]; assert(pitChannel < FSL_FEATURE_PIT_TIMER_COUNT); /* Enter critical section to avoid disabling interrupt from pit for very long time */ OSA_EnterCritical(kCriticalDisableInt); PIT_HAL_SetIntCmd(baseAddr, pitChannel, false); time->ticks = hwtimer->ticks; tempCval = PIT_HAL_ReadTimerCount(baseAddr, pitChannel); /* Check pending interrupt flag */ if (PIT_HAL_IsIntPending(baseAddr, pitChannel)) { PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); OSA_ExitCritical(kCriticalDisableInt); time->subTicks = hwtimer->modulo - 1U; } else { PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); OSA_ExitCritical(kCriticalDisableInt); /* todo: following line should be updated when HAL will be updated with this functionality. */ time->subTicks = HW_PIT_LDVALn_RD(baseAddr, pitChannel) - tempCval; } return kHwtimerSuccess; }
/*! * @cond DOXYGEN_PRIVATE * * @brief Initialization of pit timer module * * Called by hwtimer_deinit. Disables the peripheral. Unregisters ISR. * * @param hwtimer[in] Pointer to hwtimer structure. * * @return kHwtimerSuccess Success. * @return kHwtimerLockError When Locking failed. * @return kHwtimerRegisterHandlerError When un-registration of the interrupt service routine failed. * * @see HWTIMER_SYS_PitInit * @see HWTIMER_SYS_PitSetDiv * @see HWTIMER_SYS_PitStart * @see HWTIMER_SYS_PitStop * @see HWTIMER_SYS_PitGetTime * @see HWTIMER_SYS_PitIsr * @see HWTIMER_SYS_PitIsrShared */ static _hwtimer_error_code_t HWTIMER_SYS_PitDeinit(hwtimer_t * hwtimer) { /* We belive that if isr is shared ,than is shared for every chanells */ uint32_t baseAddr = g_pitBaseAddr[0]; uint32_t pitChannel; int i; assert(NULL != hwtimer); pitChannel = hwtimer->llContext[0U]; assert(pitChannel < FSL_FEATURE_PIT_TIMER_COUNT); /* Remove Hwtimer from global array and disable interrupt on this channel */ PIT_HAL_StopTimer(baseAddr, pitChannel); PIT_HAL_SetIntCmd(baseAddr, pitChannel, false); PIT_HAL_ClearIntFlag(baseAddr, pitChannel); /* Critical section. Access to global variable */ if (kStatus_OSA_Success != OSA_MutexLock(g_lock, OSA_WAIT_FOREVER)) { return kHwtimerLockError; } /* Pit can have shared interrupt vectors. We need un-register interrupt only when all hwtimers are de-inited(set to NULL) */ g_hwtimersPit[pitChannel] = NULL; if (kStatus_OSA_Success != OSA_MutexUnlock(g_lock)) { return kHwtimerLockError; } /* Check if this is last hwtimer in pit_hwtimers_array */ for (i = 0U; i < FSL_FEATURE_PIT_TIMER_COUNT; i++) { if (NULL != g_hwtimersPit[i]) { break; } } if (i == FSL_FEATURE_PIT_TIMER_COUNT) { if(kStatus_OSA_Success != OSA_InstallIntHandler(kpitIrqIds[pitChannel], NULL)) { return kHwtimerRegisterHandlerError; } } /* Release lock if does not exists*/ /* Enter critical section to avoid interrupt release locking */ OSA_EnterCritical(kCriticalDisableInt); if (kStatus_OSA_Success != OSA_MutexUnlock(g_lock)) { return kHwtimerLockError; } g_lock = NULL; OSA_ExitCritical(kCriticalDisableInt); return kHwtimerSuccess; }
void panic( panicId_t id, uint32_t location, uint32_t extra1, uint32_t extra2 ) { #if gUsePanic_c /* Save the Link Register */ volatile uint32_t savedLR; // __asm("str r14, [SP]"); __asm("push {r2} "); __asm("push {LR} "); __asm("pop {r2} "); __asm("str r2, [SP, #4]"); __asm("pop {r2}"); panic_data.id = id; panic_data.location = location; panic_data.extra1 = extra1; panic_data.extra2 = extra2; panic_data.linkRegister = savedLR; panic_data.cpsr_contents = 0; #if 0 OSA_ExitCritical(kCriticalDisableInt); /* enable interrupts */ OSA_TimeDelay(100); /* allowing datalogs to get out */ #endif OSA_EnterCritical(kCriticalDisableInt); /* disable interrupts */ /* cause processor to halt */ #ifdef __IAR_SYSTEMS_ICC__ __asm("BKPT #0xF"); __asm("NOP"); __asm("NOP"); __asm("NOP"); #endif #ifdef RVDS __breakpoint(); #endif /* infinite loop just to ensure this routine never returns */ for(;;) { __asm("NOP"); } #endif }
uint8_t setWakeUpTimeOut(wakeUpSource_t wus) { uint8_t val0; uint8_t val1; while(1) { PRINTF("Select the wake up timeout in format DD. Possible decimal value is from range 01 - 60 seconds. Eg. 05 means 5 seconds delay"); PRINTF("\n\rWaiting for key press..\n\r\n\r"); val0 = getInput(); PRINTF("You pressed: '%c", val0); if( (val0 >= '0') && (val0 <= '6') ) { val1 = getInput(); PRINTF("%c'\r\n", val1); if( (val1 >= '0') && (val1 <= '9') ) { val0 = (val0-'0')*10 + (val1-'0'); if( (val0!=0) && (val0<=60) ) { OSA_EnterCritical(kCriticalDisableInt); if((wus & wakeUpSourceRtc) != 0) { LLWU_HAL_SetInternalModuleCmd(LLWU_BASE_PTR,PM_RTOS_DEMO_RTC_LLWU_WAKEUP_MODULE,true); } if((wus & wakeUpSourceLptmr) != 0) { LLWU_HAL_SetInternalModuleCmd(LLWU_BASE_PTR,PM_RTOS_DEMO_LPTMR_LLWU_WAKEUP_MODULE,true); } OSA_ExitCritical(kCriticalDisableInt); cmdAlarm(wus, val0); return val0; } } } PRINTF("Wrong value!\n\r"); } }
static void CommonService_SendNotifications(deviceId_t deviceId, uint16_t handle) { uint16_t handleCccd; bool_t isNotifActive; /* Get handle of CCCD */ if (GattDb_FindCccdHandleForCharValueHandle(handle, &handleCccd) != gBleSuccess_c) return; if (deviceId == gInvalidDeviceId_c) return; if ( (gBleSuccess_c == Gap_CheckNotificationStatus(deviceId, handleCccd, &isNotifActive)) && (TRUE == isNotifActive) ) { OSA_EnterCritical(kCriticalDisableInt); GattServer_SendNotification(deviceId, handle); OSA_ExitCritical(kCriticalDisableInt); } }
/*! * @cond DOXYGEN_PRIVATE * * @brief This function initializes caller allocated structure according to given * numerical identifier of the timer. * * Called by hwtimer_init(). * Initializes the HWTIMER structure. * Sets interrupt priority and registers ISR. * * @param hwtimer[in] Returns initialized hwtimer structure handle. * @param pitId[in] Determines PIT module and pit channel. * @param isrPrior[in] Interrupt priority for PIT * @param data[in] Specific data. Not used in this timer. * * @return kHwtimerSuccess Success. * @return kHwtimerInvalidInput When channel number does not exist in pit module. * @return kHwtimerLockError When Locking failed. * @return kHwtimerRegisterHandlerError When registration of the interrupt service routine failed. * * @see HWTIMER_SYS_PitDeinit * @see HWTIMER_SYS_PitSetDiv * @see HWTIMER_SYS_PitStart * @see HWTIMER_SYS_PitStop * @see HWTIMER_SYS_PitGetTime * @see HWTIMER_SYS_PitIsr * @see HWTIMER_SYS_PitIsrShared */ static _hwtimer_error_code_t HWTIMER_SYS_PitInit(hwtimer_t * hwtimer, uint32_t pitId, uint32_t isrPrior, void *data) { uint32_t pitChannel; uint32_t baseAddr = g_pitBaseAddr[0]; if (FSL_FEATURE_PIT_TIMER_COUNT < pitId) { return kHwtimerInvalidInput; } assert(NULL != hwtimer); /* We need to store pitId of timer in context struct */ hwtimer->llContext[0U] = pitId; pitChannel = hwtimer->llContext[0U]; /* Un-gate pit clock */ CLOCK_SYS_EnablePitClock(0U); /* Enable PIT module clock */ PIT_HAL_Enable(baseAddr); /* Allows the timers to be stopped when the device enters the Debug mode. */ PIT_HAL_SetTimerRunInDebugCmd(baseAddr, false); /* Disable timer and interrupt */ PIT_HAL_StopTimer(baseAddr, pitChannel); PIT_HAL_SetIntCmd(baseAddr, pitChannel, false); /* Clear any pending interrupt */ PIT_HAL_ClearIntFlag(baseAddr, pitChannel); /* Create lock if does not exists*/ /* Enter critical section to avoid interrupt create locking */ OSA_EnterCritical(kCriticalDisableInt); if (g_lock == NULL) { /* Initialize synchronization object */ if (kStatus_OSA_Success != OSA_MutexCreate(&g_lock_data)) { return kHwtimerLockError; } g_lock = &g_lock_data; } OSA_ExitCritical(kCriticalDisableInt); assert(g_lock == &g_lock_data); /* Critical section. Access to global variable */ if (kStatus_OSA_Success != OSA_MutexLock(g_lock, OSA_WAIT_FOREVER)) { return kHwtimerLockError; } /* Store hwtimer in global array */ g_hwtimersPit[pitChannel] = hwtimer; if (kStatus_OSA_Success != OSA_MutexUnlock(g_lock)) { return kHwtimerLockError; } /* Enable PIT interrupt.*/ if (kStatus_OSA_Success != OSA_InstallIntHandler(kpitIrqIds[pitChannel], HWTIMER_SYS_PitIsr)) { return kHwtimerRegisterHandlerError; } PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); INT_SYS_EnableIRQ(kpitIrqIds[pitChannel]); return kHwtimerSuccess; }
/*FUNCTION********************************************************************** * * Function Name : TSI_DRV_Init * Description : Initialize whole the TSI peripheral to be ready to read capacitance changes * To initialize the TSI driver, the configuration structure should be handled. * *END**************************************************************************/ tsi_status_t TSI_DRV_Init(uint32_t instance, tsi_state_t * tsiState, const tsi_user_config_t * tsiUserConfig) { assert(instance < TSI_INSTANCE_COUNT); TSI_Type * base = g_tsiBase[instance]; tsi_state_t * tsiSt = g_tsiStatePtr[instance]; /* Critical section. */ OSA_EnterCritical(kCriticalDisableInt); /* Exit if current instance is already initialized. */ if(tsiSt) { /* End of critical section. */ OSA_ExitCritical(kCriticalDisableInt); return kStatus_TSI_Initialized; } /* Save runtime structure pointer.*/ tsiSt = g_tsiStatePtr[instance] = tsiState; /* Clear the state structure for this instance. */ memset(tsiSt, 0, sizeof(tsi_state_t)); /* Create the mutex used by whole driver. */ OSA_MutexCreate(&tsiSt->lock); /* Create the mutex used by change mode function. */ OSA_MutexCreate(&tsiSt->lockChangeMode); /* Critical section. Access to global variable */ if (kStatus_OSA_Success != OSA_MutexLock(&tsiSt->lock, OSA_WAIT_FOREVER)) { /* End of critical section. */ OSA_ExitCritical(kCriticalDisableInt); return kStatus_TSI_Error; } /* End of critical section. */ OSA_ExitCritical(kCriticalDisableInt); tsiSt->opMode = tsi_OpModeNormal; tsiSt->opModesData[tsiSt->opMode].config = *tsiUserConfig->config; /* Store the hardware configuration. */ tsiSt->pCallBackFunc = tsiUserConfig->pCallBackFunc; tsiSt->usrData = tsiUserConfig->usrData; tsiSt->isBlockingMeasure = false; /* Un-gate TSI module clock */ CLOCK_SYS_EnableTsiClock(instance); /* Initialize the interrupt sync object. */ OSA_SemaCreate(&tsiSt->irqSync, 0); TSI_HAL_Init(base); TSI_HAL_SetConfiguration(base, &tsiSt->opModesData[tsiSt->opMode].config); TSI_HAL_EnableInterrupt(base); TSI_HAL_EnableEndOfScanInterrupt(base); TSI_HAL_EnableSoftwareTriggerScan(base); /* Disable all electrodes */ tsiState->opModesData[tsiState->opMode].enabledElectrodes = 0; /* Enable TSI interrupt on NVIC level. */ INT_SYS_EnableIRQ(g_tsiIrqId[instance]); tsiSt->status = kStatus_TSI_Initialized; /* End of critical section. */ OSA_MutexUnlock(&tsiSt->lock); return kStatus_TSI_Success; }
/*FUNCTION********************************************************************** * * Function Name : CLOCK_SYS_UpdateConfiguration * Description : Send notification and change system clock configuration. * This function sends the notification to all callback functions, if all * callbacks return OK or forceful policy is used, this function will change * system clock configuration. * *END**************************************************************************/ clock_manager_error_code_t CLOCK_SYS_UpdateConfiguration(uint8_t targetConfigIndex, clock_manager_policy_t policy) { uint8_t callbackIdx; clock_manager_error_code_t ret = kClockManagerSuccess; clock_manager_callback_user_config_t* callbackConfig; clock_notify_struct_t notifyStruct; notifyStruct.targetClockConfigIndex = targetConfigIndex; notifyStruct.policy = policy; /* Clock configuration index is out of range. */ if (targetConfigIndex >= g_clockState.clockConfigNum) { return kClockManagerErrorOutOfRange; } OSA_EnterCritical(kCriticalLockSched); /* Set errorcallbackindex as callbackNum, which means no callback error now.*/ g_clockState.errorCallbackIndex = g_clockState.callbackNum; /* First step: Send "BEFORE" notification. */ notifyStruct.notifyType = kClockManagerNotifyBefore; /* Send notification to all callback. */ for (callbackIdx=0; callbackIdx<g_clockState.callbackNum; callbackIdx++) { callbackConfig = g_clockState.callbackConfig[callbackIdx]; if ((NULL != callbackConfig) && ((uint8_t)callbackConfig->callbackType & (uint8_t)kClockManagerNotifyBefore)) { if (kClockManagerSuccess != (*callbackConfig->callback)(¬ifyStruct, callbackConfig->callbackData)) { g_clockState.errorCallbackIndex = callbackIdx; /* Save the error callback index. */ ret = kClockManagerErrorNotificationBefore; if (kClockManagerPolicyAgreement == policy) { break; } } } } /* If all callback success or forceful policy is used. */ if ((kClockManagerSuccess == ret) || (policy == kClockManagerPolicyForcible)) { /* clock mode switch. */ OSA_EnterCritical(kCriticalDisableInt); CLOCK_SYS_SetConfiguration(g_clockState.configTable[targetConfigIndex]); g_clockState.curConfigIndex = targetConfigIndex; OSA_ExitCritical(kCriticalDisableInt); notifyStruct.notifyType = kClockManagerNotifyAfter; for (callbackIdx=0; callbackIdx<g_clockState.callbackNum; callbackIdx++) { callbackConfig = g_clockState.callbackConfig[callbackIdx]; if ((NULL != callbackConfig) && ((uint8_t)callbackConfig->callbackType & (uint8_t)kClockManagerNotifyAfter)) { if (kClockManagerSuccess != (*callbackConfig->callback)(¬ifyStruct, callbackConfig->callbackData)) { g_clockState.errorCallbackIndex = callbackIdx; /* Save the error callback index. */ ret = kClockManagerErrorNotificationAfter; if (kClockManagerPolicyAgreement == policy) { break; } } } } } else /* Error occurs, need to send "RECOVER" notification. */ { notifyStruct.notifyType = kClockManagerNotifyRecover; while (callbackIdx--) { callbackConfig = g_clockState.callbackConfig[callbackIdx]; if (NULL != callbackConfig) { (*callbackConfig->callback)(¬ifyStruct, callbackConfig->callbackData); } } } OSA_ExitCritical(kCriticalLockSched); return ret; }