static void lptmr_init(void) { uint32_t extosc; /* Clock the timer */ SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK; /* Reset */ LPTMR0->CSR = 0; #if defined(TARGET_KL43Z) /* Set interrupt handler */ NVIC_SetVector(LPTMR0_IRQn, (uint32_t)lptmr_isr); NVIC_EnableIRQ(LPTMR0_IRQn); MCG->C1 |= MCG_C1_IRCLKEN_MASK; extosc = mcgirc_frequency(); #else /* Set interrupt handler */ NVIC_SetVector(LPTimer_IRQn, (uint32_t)lptmr_isr); NVIC_EnableIRQ(LPTimer_IRQn); /* Clock at (1)MHz -> (1)tick/us */ /* Check if the external oscillator can be divided to 1MHz */ extosc = extosc_frequency(); #endif if (extosc != 0) { //If external oscillator found if (extosc % 1000000u == 0) { //If it is a multiple if 1MHz extosc /= 1000000; if (extosc == 1) { //1MHz, set timerprescaler in bypass mode LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PBYP_MASK; return; } else { //See if we can divide it to 1MHz uint32_t divider = 0; extosc >>= 1; while (1) { if (extosc == 1) { LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PRESCALE(divider); return; } if (extosc % 2 != 0) //If we can't divide by two anymore break; divider++; extosc >>= 1; } } } }
void LPT_Init(int count){ /* Configure LPT */ LPTMR0_CMR = LPTMR_CMR_COMPARE(count); //Set compare value LPTMR0_PSR = LPTMR_PSR_PCS(0x1)|LPTMR_PSR_PBYP_MASK; //Use internal 1khz clock LPTMR0_CSR = LPTMR_CSR_TIE_MASK; //Enable LPT interrupt }
/******************************************************************************* * * PROCEDURE NAME: * lptmr_init - * *******************************************************************************/ void lptmr_init(int count, int clock_source) { SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; LPTMR0_PSR = ( LPTMR_PSR_PRESCALE(0) // 0000 is div 2 | LPTMR_PSR_PBYP_MASK // LPO feeds directly to LPT | LPTMR_PSR_PCS(clock_source)) ; // use the choice of clock if (clock_source== 0) printf("\n LPTMR Clock source is the MCGIRCLK \n\r"); if (clock_source== 1) printf("\n LPTMR Clock source is the LPOCLK \n\r"); if (clock_source== 2) printf("\n LPTMR Clock source is the ERCLK32 \n\r"); if (clock_source== 3) printf("\n LPTMR Clock source is the OSCERCLK \n\r"); LPTMR0_CMR = LPTMR_CMR_COMPARE(count); //Set compare value LPTMR0_CSR =( LPTMR_CSR_TCF_MASK // Clear any pending interrupt | LPTMR_CSR_TIE_MASK // LPT interrupt enabled | LPTMR_CSR_TPS(0) //TMR pin select |!LPTMR_CSR_TPP_MASK //TMR Pin polarity |!LPTMR_CSR_TFC_MASK // Timer Free running counter is reset whenever TMR counter equals compare |!LPTMR_CSR_TMS_MASK //LPTMR0 as Timer ); LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; //Turn on LPT and start counting }
/*! * brief Ungates the LPTMR clock and configures the peripheral for a basic operation. * * note This API should be called at the beginning of the application using the LPTMR driver. * * param base LPTMR peripheral base address * param config A pointer to the LPTMR configuration structure. */ void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config) { assert(config); #if defined(LPTMR_CLOCKS) #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) uint32_t instance = LPTMR_GetInstance(base); /* Ungate the LPTMR clock*/ CLOCK_EnableClock(s_lptmrClocks[instance]); #if defined(LPTMR_PERIPH_CLOCKS) CLOCK_EnableClock(s_lptmrPeriphClocks[instance]); #endif #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ #endif /* LPTMR_CLOCKS */ /* Configure the timers operation mode and input pin setup */ base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) | LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect)); /* Configure the prescale value and clock source */ base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) | LPTMR_PSR_PCS(config->prescalerClockSource)); }
/* * Counting example using the LPO clock and the prescale feature * * Sets compare value to 250. Thus when using the 1Khz LPO clock with * LPTMR0_PSR[PRESCALE]=0x4, it will take 8 seconds for Timer Compare Flag * to be set. (1Khz clock/32=31.25Hz clock) * * If prescaler was not used, then timer would only wait .25 seconds. * */ void lptmr_prescale() { int compare_value=250; //value must be less than 0xFFFF or 65535 printf("\n\n****************************\n"); printf("LPTMR Time Counting Example with Prescaler\n"); //Reset LPTMR module lptmr_clear_registers(); /* Configure LPTMR */ LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_PSR=LPTMR_PSR_PCS(0x1)|LPTMR_PSR_PRESCALE(0x4); //Use LPO clock and divide by 32 printf("LPTMR using LPO clock with PRESCALE=4 and compare value=250 (8 seconds)\n"); printf("Press a key to start counter\n"); in_char(); //wait for keyboard press LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPTMR with default settings //Wait for Timer Compare Flag to be set while((LPTMR0_CSR & LPTMR_CSR_TCF_MASK)==0) { //This may not get proper counter data if the CNR is read at the same time it is incremented printf("Current value of counter register CNR is %d\n",LPTMR0_CNR); } printf("Waited for %d counts\n",compare_value); printf("End of Time Counting Example with Prescale\n"); printf("****************************\n\n"); }
/* * LPO Clock Example (PSC=0x1) * * Test is pre-scaled to wait for 10 seconds. In reality it might be slightly off because * of the trim values for the LPO clock. */ void lptmr_lpo_input() { unsigned int compare_value=4000; //4 second delay with the 1khz LPO clock printf("\n\n****************************\n"); printf("LPO Clock Source Example\n"); //Reset LPTMR module lptmr_clear_registers(); /* Configure LPTMR */ LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_PSR=LPTMR_PSR_PCS(0x1)|LPTMR_PSR_PBYP_MASK; //Use LPO clock with bypass enabled printf("LPTMR using LPO clock with no prescale, and compare value=4000 (4 seconds)\n"); printf("Press a key to start counter\n"); in_char(); LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT with default settings printf("Counting...\n\n"); //Wait for Timer Compare Flag to be set while((LPTMR0_CSR&LPTMR_CSR_TCF_MASK)==0) { //This may not get proper counter data if the CNR is read at the same time it is incremented //printf("Current value of counter register CNR is %d\n",LPTMR0_CNR); } printf("4 seconds should have passed\n"); printf("End of LPO Clock Source Example\n"); printf("****************************\n\n"); }
/*---------------------------------------------------------------------------*/ void rtimer_arch_init(void) { #if RTIMER_CONF_USE_LPTMR /* SIM_SCGC5: LPTMR=1 */ SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; LPTMR0_CSR = (LPTMR_CSR_TCF_MASK | LPTMR_CSR_TPS(0x00) | LPTMR_CSR_TFC_MASK); /* Clear control register */ LPTMR0_CMR = LPTMR_CMR_COMPARE(LPTMR_CMR_COMPARE_MASK); /* Set up compare register */ LPTMR0_PSR = LPTMR_PSR_PRESCALE(0x00) | LPTMR_PSR_PBYP_MASK | LPTMR_PSR_PCS(0x01); /* Set up prescaler register */ LPTMR0_CSR = (LPTMR_CSR_TPS(0x00) | LPTMR_CSR_TEN_MASK); /* Set up control register */ #else /* SIM_SCGC6: TPM0=1 */ SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK; TPM0_SC = (TPM_SC_CMOD(0x00) | TPM_SC_PS(0x00)); /* Clear status and control register */ TPM0_CNT = TPM_CNT_COUNT(0x00); /* Reset counter register */ TPM0_C1SC = 0x00U; /* Clear channel status and control register */ TPM0_C2SC = 0x00U; /* Clear channel status and control register */ TPM0_C3SC = 0x00U; /* Clear channel status and control register */ TPM0_C4SC = 0x00U; /* Clear channel status and control register */ TPM0_C5SC = 0x00U; /* Clear channel status and control register */ TPM0_MOD = TPM_MOD_MOD(0xFFFF); /* Set up modulo register */ TPM0_C0SC = (TPM_CnSC_CHIE_MASK | TPM_CnSC_MSA_MASK); /* Set up channel status and control register */ TPM0_C0V = TPM_CnV_VAL(0x00); /* Set up channel value register */ TPM0_SC = (TPM_SC_CMOD(0x01) | TPM_SC_PS(0x05)); /* Set up status and control register */ #endif /* RTIMER_CONF_USE_LPTMR */ PRINTF("rtimer_arch_init done\n"); }
/* ===================================================================*/ LDD_TDeviceData* FTM_Init(LDD_TUserData *UserDataPtr) { /* Allocate device structure */ FTM_TDeviceData *DeviceDataPrv; /* {MQXLite RTOS Adapter} Driver memory allocation: Dynamic allocation is simulated by a pointer to the static object */ DeviceDataPrv = &DeviceDataPrv__DEFAULT_RTOS_ALLOC; DeviceDataPrv->UserDataPtr = UserDataPtr; /* Store the RTOS device structure */ /* Interrupt vector(s) allocation */ /* {MQXLite RTOS Adapter} Save old and set new interrupt vector (function handler and ISR parameter) */ /* Note: Exception handler for interrupt is not saved, because it is not modified */ DeviceDataPrv->SavedISRSettings_TUInterrupt.isrData = _int_get_isr_data(LDD_ivIndex_INT_LPTimer); DeviceDataPrv->SavedISRSettings_TUInterrupt.isrFunction = _int_install_isr(LDD_ivIndex_INT_LPTimer, FTM_Interrupt, DeviceDataPrv); /* LPTMR0_CSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TCF=1,TIE=0,TPS=0,TPP=0,TFC=0,TMS=0,TEN=0 */ LPTMR0_CSR = (LPTMR_CSR_TCF_MASK | LPTMR_CSR_TPS(0x00)); /* Clear control register */ /* LPTMR0_CMR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,COMPARE=0x1387 */ LPTMR0_CMR = LPTMR_CMR_COMPARE(0x1387); /* Set up compare register */ /* LPTMR0_PSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,PRESCALE=1,PBYP=0,PCS=0 */ LPTMR0_PSR = (LPTMR_PSR_PRESCALE(0x01) | LPTMR_PSR_PCS(0x00)); /* Set up prescaler register */ /* NVICIP58: PRI58=0x70 */ NVICIP58 = NVIC_IP_PRI58(0x70); /* NVICISER1: SETENA|=0x04000000 */ NVICISER1 |= NVIC_ISER_SETENA(0x04000000); /* LPTMR0_CSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TCF=0,TIE=1,TPS=0,TPP=0,TFC=0,TMS=0,TEN=1 */ LPTMR0_CSR = (LPTMR_CSR_TIE_MASK | LPTMR_CSR_TPS(0x00) | LPTMR_CSR_TEN_MASK); /* Set up control register */ /* Registration of the device structure */ PE_LDD_RegisterDeviceStructure(PE_LDD_COMPONENT_FTM_ID,DeviceDataPrv); return ((LDD_TDeviceData *)DeviceDataPrv); /* Return pointer to the device data structure */ }
/** * @brief 初始化配置LPTM模块处于计时器模式 * @code * //设置LPTM工作在计时器模式,时间间隔是500毫秒 * LPTMR_TC_InitTypeDef LPTMR_TC_InitStruct1; //申请一个结构变量 * LPTMR_TC_InitStruct1.timeInMs = 500; //设置计时时间是500ms * LPTMR_TC_Init(&LPTMR_TC_InitStruct1); * @endcode * @param LPTMR_TC_InitStruct: 工作配置结构体 * @arg timeInMs :定时时间 单位为ms * @retval None */ void LPTMR_TC_Init(LPTMR_TC_InitTypeDef* LPTMR_TC_InitStruct) { /* open clock gate */ SIM->SCGC5 |= SIM_SCGC5_LPTIMER_MASK; LPTMR0->CSR = 0x00; LPTMR0->PSR = 0x00; LPTMR0->CMR = 0x00; /* disable module first */ LPTMR0->CSR &= ~LPTMR_CSR_TEN_MASK; /* free counter will reset whenever compare register is writtened. */ LPTMR0->CSR &= ~LPTMR_CSR_TFC_MASK; /* timer counter mode */ LPTMR0->CSR &= ~LPTMR_CSR_TMS_MASK; /* bypass the prescaler, which mean we use 1KHZ LPO directly */ LPTMR0->PSR = LPTMR_PSR_PCS(1)| LPTMR_PSR_PBYP_MASK; /* set CMR(compare register) */ LPTMR0->CMR = LPTMR_CMR_COMPARE(LPTMR_TC_InitStruct->timeInMs); /* enable moudle */ LPTMR0->CSR |= LPTMR_CSR_TEN_MASK; }
/* * Initialize the low power time to provide a delay measured in ms. * * * Parameters: * count_val number of ms to delay * * Returns: * None */ void time_delay_ms(unsigned int count_val) { /* Make sure the clock to the LPTMR is enabled */ SIM_SCGC5|=SIM_SCGC5_LPTMR_MASK; /* Reset LPTMR settings */ LPTMR0_CSR=0; /* Set the compare value to the number of ms to delay */ LPTMR0_CMR = count_val; /* Set up LPTMR to use 1kHz LPO with no prescaler as its clock source */ LPTMR0_PSR = LPTMR_PSR_PCS(1)|LPTMR_PSR_PBYP_MASK; /* Start the timer */ LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; /* Wait for counter to reach compare value */ while (!(LPTMR0_CSR & LPTMR_CSR_TCF_MASK)); /* Disable counter and Clear Timer Compare Flag */ LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK; return; }
/** * @brief 初始化配置LPTM模块处于脉冲计数模式 * @code * //设置LPTM工作在脉冲计数模式,计数上限是0xFFFE * LPTMR_PC_InitTypeDef LPTMR_PC_InitStruct1; //申请一个结构变量 * LPTMR_PC_InitStruct1.timeInMs = 500; //设置计时时间是500ms * LPTMR_TC_Init(&LPTMR_TC_InitStruct1); * @endcode * @param LPTMR_PC_InitTypeDef: 工作配置结构体 * @arg counterOverflowValue :计数器计数上限,极限为0xFFFF * @arg inputSource :脉冲源选择 kLPTMR_PC_InputSource_CMP0-CMP0作为脉冲计数时钟源 kLPTMR_PC_InputSource_ALT1-外部引脚LPTMR_ALT1作为外部计数时钟源 kLPTMR_PC_InputSource_ALT2-外部引脚LPTMR_ALT2作为外部计数时钟源 * @arg pinPolarity :脉冲计数极性选择 kLPTMR_PC_PinPolarity_RigsingEdge 上升沿计数 kLPTMR_PC_PinPolarity_FallingEdge 下降沿计数 * @retval None */ void LPTMR_PC_Init(LPTMR_PC_InitTypeDef* LPTMR_PC_InitStruct) { /* open clock gate */ *(uint32_t*)SIM_LPTMRClockGateTable[0].addr |= SIM_LPTMRClockGateTable[0].mask; LPTMR0->CSR = 0x00; LPTMR0->PSR = 0x00; LPTMR0->CMR = 0x00; /* disable module first */ LPTMR0->CSR &= ~LPTMR_CSR_TEN_MASK; /* free counter will reset whenever compare register is writtened. */ LPTMR0->CSR &= ~LPTMR_CSR_TFC_MASK; /* timer counter mode */ LPTMR0->CSR |= LPTMR_CSR_TMS_MASK; /* bypass the glitch filter, which mean we use 1KHZ LPO directly */ LPTMR0->PSR = LPTMR_PSR_PCS(1)| LPTMR_PSR_PBYP_MASK; /* set CMR(compare register) */ LPTMR0->CMR = LPTMR_CMR_COMPARE(LPTMR_PC_InitStruct->counterOverflowValue); /* input source */ switch(LPTMR_PC_InitStruct->inputSource) { case kLPTMR_PC_InputSource_CMP0: LPTMR0->CSR |= LPTMR_CSR_TPS(0); break; case kLPTMR_PC_InputSource_ALT1: LPTMR0->CSR |= LPTMR_CSR_TPS(1); break; case kLPTMR_PC_InputSource_ALT2: LPTMR0->CSR |= LPTMR_CSR_TPS(2); break; default: break; } /* pin polarity */ switch(LPTMR_PC_InitStruct->pinPolarity) { case kLPTMR_PC_PinPolarity_RigsingEdge: LPTMR0->CSR &= ~LPTMR_CSR_TPP_MASK; break; case kLPTMR_PC_PinPolarity_FallingEdge: LPTMR0->CSR |= LPTMR_CSR_TPP_MASK; break; default: break; } /* enable moudle */ LPTMR0->CSR |= LPTMR_CSR_TEN_MASK; }
/*FUNCTION********************************************************************** * * Function Name : LPTMR_HAL_SetPrescalerMode * Description : Set the LPTMR prescaler mode. * *END**************************************************************************/ void LPTMR_HAL_SetPrescalerMode(LPTMR_Type * base, lptmr_prescaler_user_config_t prescaler_config) { uint32_t psr; psr = LPTMR_PSR_PCS(prescaler_config.prescalerClockSelect) | LPTMR_PSR_PBYP(prescaler_config.prescalerBypass) | LPTMR_PSR_PRESCALE(prescaler_config.prescalerValue); LPTMR_WR_PSR(base, psr); }
/* * Delay function using the LPTMR module */ void time_delay_ms(unsigned int count_val) { SIM_SCGC5|=SIM_SCGC5_LPTIMER_MASK; //Turn on clock to LPTMR module LPTMR0_CMR = count_val; //Set compare value LPTMR0_PSR = LPTMR_PSR_PCS(1)|LPTMR_PSR_PBYP_MASK; //Use 1Khz LPO clock and bypass prescaler LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; //Start counting while (!(LPTMR0_CSR & LPTMR_CSR_TCF_MASK)) {} //Wait for counter to reach compare value LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK; //Clear Timer Compare Flag return; }
void Lptmr_Init(int count, int clock_source) { _mqx_uint mqx_ret; SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; SIM_SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK; SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(2); // ERCLK32 is RTC OSC CLOCK PORTC_PCR1 &= ~PORT_PCR_MUX_MASK; PORTC_PCR1 |= PORT_PCR_MUX(1);//enable ptc1 alt1 functions to select RTC_CLKIN function /********************************************************************************* * On L2K tower board, we use external 32kHz clock instead of 32kHz crystal, so please * don't enable the 32kHz crystal oscillator **********************************************************************************/ /* RTC_CR |= RTC_CR_OSCE_MASK | RTC_CR_CLKO_MASK | RTC_CR_SC8P_MASK ; */ LPTMR0_PSR &= ~LPTMR_PSR_PRESCALE_MASK; LPTMR0_PSR |= LPTMR_PSR_PRESCALE(0); // 0000 is div 2 LPTMR0_PSR |= LPTMR_PSR_PBYP_MASK; // LPO feeds directly to LPT LPTMR0_PSR &= ~LPTMR_PSR_PCS_MASK; LPTMR0_PSR |= LPTMR_PSR_PCS(clock_source); // use the choice of clock if (clock_source== 0) APP_TRACE("\n LPTMR Clock source is the MCGIRCLK \n\r"); if (clock_source== 1) APP_TRACE("\n LPTMR Clock source is the LPOCLK \n\r"); if (clock_source== 2) APP_TRACE("\n LPTMR Clock source is the ERCLK32 \n\r"); if (clock_source== 3) APP_TRACE("\n LPTMR Clock source is the OSCERCLK \n\r"); LPTMR0_CMR = LPTMR_CMR_COMPARE(count); //Set compare value LPTMR0_CSR |=( LPTMR_CSR_TCF_MASK // Clear any pending interrupt | LPTMR_CSR_TIE_MASK // LPT interrupt enabled |!LPTMR_CSR_TPP_MASK //TMR Pin polarity |!LPTMR_CSR_TFC_MASK // Timer Free running counter is reset whenever TMR counter equals compare |!LPTMR_CSR_TMS_MASK //LPTMR0 as Timer ); enable_irq(28) ; _int_install_isr(LDD_ivIndex_INT_LPTimer, lptmr_isr, NULL); mqx_ret = _lwsem_create(&g_lptmr_int_sem, 0); ASSERT_PARAM(MQX_OK == mqx_ret); // // ready for this interrupt. // set_irq_priority(28, 2); }
/*-----------------------------------------------------------*/ void vPortInitTickTimer(void) { #if configUSE_TICKLESS_IDLE == 1 { #if TICK_NOF_BITS==32 xMaximumPossibleSuppressedTicks = 0xffffffffUL/TIMER_COUNTS_FOR_ONE_TICK; /* 32bit timer register */ #elif TICK_NOF_BITS==24 xMaximumPossibleSuppressedTicks = 0xffffffUL/TIMER_COUNTS_FOR_ONE_TICK; /* 24bit timer register */ #elif TICK_NOF_BITS==16 xMaximumPossibleSuppressedTicks = 0xffffUL/TIMER_COUNTS_FOR_ONE_TICK; /* 16bit timer register */ #elif TICK_NOF_BITS==8 xMaximumPossibleSuppressedTicks = 0xffUL/TIMER_COUNTS_FOR_ONE_TICK; /* 8bit timer register */ #else error "unknown configuration!" #endif #if configSYSTICK_USE_LOW_POWER_TIMER ulStoppedTimerCompensation = configSTOPPED_TIMER_COMPENSATION/(configCPU_CLOCK_HZ/configSYSTICK_LOW_POWER_TIMER_CLOCK_HZ); #else ulStoppedTimerCompensation = configSTOPPED_TIMER_COMPENSATION/(configCPU_CLOCK_HZ/configSYSTICK_CLOCK_HZ); #endif } #endif /* configUSE_TICKLESS_IDLE */ #if configSYSTICK_USE_LOW_POWER_TIMER SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; /* enable clock: SIM_SCGC5: LPTMR=1 */ /* LPTMR0_CSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TCF=1,TIE=0,TPS=0,TPP=0,TFC=0,TMS=0,TEN=0 */ LPTMR0_CSR = (LPTMR_CSR_TCF_MASK | LPTMR_CSR_TPS(0x00)); /* Clear control register */ /* LPTMR0_PSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,PRESCALE=0,PBYP=1,PCS=1 */ LPTMR0_PSR = LPTMR_PSR_PRESCALE(0x00) | /* prescaler value */ LPTMR_PSR_PBYP_MASK | /* prescaler bypass */ LPTMR_PSR_PCS(0x01); /* Clock source */ /* * PBYP PCS * ERCLK32 1 10 * LPO_1kHz 1 01 * ERCLK 0 00 * IRCLK 1 00 */ *(portNVIC_SYSPRI7) |= portNVIC_LP_TIMER_PRI; /* set priority of low power timer interrupt */ /* NVIC_ISER: SETENA|=0x10000000 */ NVIC_ISER |= NVIC_ISER_SETENA(0x10000000); /* 0xE000E100 <= 0x10000000 */ /* LPTMR0_CSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TCF=0,TIE=0,TPS=0,TPP=0,TFC=0,TMS=0,TEN=1 */ LPTMR0_CSR = (LPTMR_CSR_TPS(0x00) | LPTMR_CSR_TEN_MASK); /* Set up control register */ #else /* use normal SysTick Counter */ *(portNVIC_SYSPRI3) |= portNVIC_SYSTICK_PRI; /* set priority of SysTick interrupt */ #endif /* Configure timer to interrupt at the requested rate. */ SET_TICK_DURATION(TIMER_COUNTS_FOR_ONE_TICK-1UL); RESET_TICK_COUNTER_VAL(); ENABLE_TICK_COUNTER(); }
/** * @brief lptmr 定时器初始化 * * @param clock 时钟选择 * * @return E_OK 初始化成功 * @return E_INIT 初始化失败 * * @see lptmr_registers_clear() * @note 在这个定时器中,由于产生不了小于35.6ms的基准定时,所以统一选择基准为50ms的定时。 * 在移植过程中应该相应调整COMPARE寄存器和PSR寄存器中的预分频系数。 */ ER lptmr_timer_init(uint32_t clock) { switch (clock) { case LPTMR_INTERNAL_CLOCK: lptmr_registers_clear(); /* 把LPTMR寄存器清零 */ SIM_SCGC5 |= SIM_SCGC5_LPTIMER_MASK; /* 使能LPT模块时钟 */ MCG_C1 |= MCG_C1_IRCLKEN_MASK; /* 使能内部参考时钟 */ MCG_C2 |= MCG_C2_IRCS_MASK; /* MCG_C2[IRCS]=1,使能快速内部参考时钟(2MHz) */ /** * 配置 LPTMR参数 */ LPTMR0_CMR = LPTMR_CMR_COMPARE(LPTMR_TIMER_COMPARE_INTERNAL); /* 设置比较寄存器值 */ LPTMR0_PSR = LPTMR_PSR_PCS(LPTMR_INTERNAL_CLOCK) |LPTMR_PSR_PRESCALE(LPTMR_TIMER_PRESCALE8); /* 使用内部时钟,系数预分频为8*/ LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; /* 开启LPT模块设置*/ break; case LPTMR_LPO_CLOCK: lptmr_registers_clear(); /* 把LPTMR寄存器清零 */ SIM_SCGC5 |= SIM_SCGC5_LPTIMER_MASK; /*使能LPT模块时钟*/ /** * 配置 LPTMR */ LPTMR0_CMR = LPTMR_CMR_COMPARE(LPTMR_TIMER_COMPARE_LPO); /*设置比较寄存器值 */ LPTMR0_PSR = LPTMR_PSR_PCS(LPTMR_LPO_CLOCK) | LPTMR_PSR_PBYP_MASK; /*设置PBYP为1,计数器一次一次累加*/ LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; /*开启LPT模块设置*/ break; default: return E_OBJ; /*如果初始化失败*/ } return E_OK; /*如果初始化成功*/ }
void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config) { assert(config); /* Ungate the LPTMR clock*/ CLOCK_EnableClock(s_lptmrClocks[LPTMR_GetInstance(base)]); /* Configure the timers operation mode and input pin setup */ base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) | LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect)); /* Configure the prescale value and clock source */ base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) | LPTMR_PSR_PCS(config->prescalerClockSource)); }
// delay(ms) -- Spin wait delay (in ms) // Note: uses low power timer (LPTMR) void delay(unsigned int length_ms) { SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; // Make sure clock is enabled LPTMR0_CSR = 0; // Reset LPTMR settings LPTMR0_CMR = length_ms; // Set compare value (in ms) // Use 1kHz LPO with no prescaler LPTMR0_PSR = LPTMR_PSR_PCS(1) | LPTMR_PSR_PBYP_MASK; // Start the timer and wait for it to reach the compare value LPTMR0_CSR = LPTMR_CSR_TEN_MASK; while (!(LPTMR0_CSR & LPTMR_CSR_TCF_MASK)) ; LPTMR0_CSR = 0; // Turn off timer }
void Init_LPTMR(void) { SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK; // Configure LPTMR // select 1 kHz LPO clock with prescale factor 0, dividing clock by 2 // resulting in 500 Hz clock LPTMR0->PSR = LPTMR_PSR_PCS(1) | LPTMR_PSR_PRESCALE(0); LPTMR0->CSR = LPTMR_CSR_TIE_MASK; LPTMR0->CMR = 50; // Generate interrupt every 50 clock ticks or 100 ms // Configure NVIC NVIC_SetPriority(LPTimer_IRQn, 128); // 0, 64, 128 or 192 NVIC_ClearPendingIRQ(LPTimer_IRQn); NVIC_EnableIRQ(LPTimer_IRQn); }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : TimerInit * Returned Value : * Comments : Initialize timer module * * *END*----------------------------------------------------------------------*/ void TimerInit(void) { /* Enable LPT Module Clock */ #ifdef MCU_MKL25Z4 SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; #else SIM_SCGC5 |= SIM_SCGC5_LPTIMER_MASK; #endif /* Configure LPT */ LPTMR0_CMR = LPTMR_CMR_COMPARE(1); // Set compare value LPTMR0_PSR = LPTMR_PSR_PCS(0x1); //Use internal 1khz clock LPTMR0_CSR = LPTMR_CSR_TIE_MASK; //Enable LPT interrupt LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; //Turn on LPT and start counting EnableTimerInterrupt(); }
/* ===================================================================*/ LDD_TDeviceData* TU1_Init(LDD_TUserData *UserDataPtr) { TU1_TDeviceData *DeviceDataPrv; if (PE_LDD_DeviceDataList[PE_LDD_COMPONENT_TU1_ID] == NULL) { /* Allocate device structure */ /* {Default RTOS Adapter} Driver memory allocation: Dynamic allocation is simulated by a pointer to the static object */ DeviceDataPrv = &DeviceDataPrv__DEFAULT_RTOS_ALLOC; DeviceDataPrv->UserDataPtr = UserDataPtr; /* Store the RTOS device structure */ DeviceDataPrv->InitCntr = 1U; /* First initialization */ } else { /* Memory is already allocated */ DeviceDataPrv = (TU1_TDeviceDataPtr) PE_LDD_DeviceDataList[PE_LDD_COMPONENT_TU1_ID]; DeviceDataPrv->UserDataPtr = UserDataPtr; /* Store the RTOS device structure */ DeviceDataPrv->InitCntr++; /* Increment counter of initialization */ return ((LDD_TDeviceData *)DeviceDataPrv); /* Return pointer to the device data structure */ } /* Interrupt vector(s) allocation */ /* {Default RTOS Adapter} Set interrupt vector: IVT is static, ISR parameter is passed by the global variable */ INT_LPTimer__DEFAULT_RTOS_ISRPARAM = DeviceDataPrv; /* SIM_SCGC5: LPTMR=1 */ SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK; /* LPTMR0_CSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TCF=1,TIE=0,TPS=0,TPP=0,TFC=0,TMS=0,TEN=0 */ LPTMR0_CSR = (LPTMR_CSR_TCF_MASK | LPTMR_CSR_TPS(0x00)); /* Clear control register */ /* LPTMR0_CMR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,COMPARE=0xFFFF */ LPTMR0_CMR = LPTMR_CMR_COMPARE(0xFFFF); /* Set up compare register */ /* LPTMR0_PSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,PRESCALE=0,PBYP=1,PCS=0 */ LPTMR0_PSR = LPTMR_PSR_PRESCALE(0x00) | LPTMR_PSR_PBYP_MASK | LPTMR_PSR_PCS(0x00); /* Set up prescaler register */ /* NVIC_IPR7: PRI_28=0x80 */ NVIC_IPR7 = (uint32_t)((NVIC_IPR7 & (uint32_t)~(uint32_t)( NVIC_IP_PRI_28(0x7F) )) | (uint32_t)( NVIC_IP_PRI_28(0x80) )); /* NVIC_ISER: SETENA|=0x10000000 */ NVIC_ISER |= NVIC_ISER_SETENA(0x10000000); /* LPTMR0_CSR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TCF=0,TIE=1,TPS=0,TPP=0,TFC=0,TMS=0,TEN=1 */ LPTMR0_CSR = (LPTMR_CSR_TIE_MASK | LPTMR_CSR_TPS(0x00) | LPTMR_CSR_TEN_MASK); /* Set up control register */ /* Registration of the device structure */ PE_LDD_RegisterDeviceStructure(PE_LDD_COMPONENT_TU1_ID,DeviceDataPrv); return ((LDD_TDeviceData *)DeviceDataPrv); /* Return pointer to the device data structure */ }
/* * External Reference Clock Example(PSC=0x3) * * TWR-K60N512 uses a 50MHz external clock * TWR-K40X256 uses a 8MHz external clock * * Test is pre-scaled to wait for 10 seconds in both cases by adjusting * the compare value. */ void lptmr_external_clk_input() { unsigned int compare_value; printf("\n\n****************************\n"); printf("External Clock Source Example\n"); //Reset LPTMR module lptmr_clear_registers(); /* Turn on external reference clock */ MCG_C2&=~MCG_C2_EREFS_MASK; //allow extal to drive OSC_CR |= OSC_CR_ERCLKEN_MASK; // selects EXTAL to drive XOSCxERCLK //Determine compare value based on which board is being used #if (defined(K60_CLK)) compare_value=7630; //~10 seconds with prescale=0xF and 50MHz clock #else compare_value=1220; //~10 seconds with prescale=0xF and 8MHz clock #endif /* Configure LPTMR */ LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_PSR=LPTMR_PSR_PCS(0x3)|LPTMR_PSR_PRESCALE(0xF); //Use external clock divided by 65536 printf("LPTMR using external clock with PRESCALE=0xF, and compare value=%d (10 seconds)\n",compare_value); printf("Press a key to start counter\n"); in_char(); LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT with default settings printf("Counting...\n\n"); //Wait for Timer Compare Flag to be set while((LPTMR0_CSR&LPTMR_CSR_TCF_MASK)==0) { //This may not get proper counter data if the CNR is read at the same time it is incremented //printf("Current value of counter register CNR is %d\n",LPTMR0_CNR); } printf("Timer should have waited for 10 seconds\n"); printf("End of External Clock Example\n"); printf("****************************\n\n"); }
/* * 32kHz Input Clock Test (PSC=0x2) * The 32kHz clock (ERCLK32K) can come from two clock sources. * If SOPT1[OSC32KSEL]=1, then it uses 32kHz RTC crystal connected to XTAL32 * This is what the code below tests * If SOPT1[OSC32KSEL]=0, then it uses 32kHz System oscillator, and reguires * that the main system clock be a 32kHz crystal. The tower board does not * support that feature. * * Test is pre-scaled to wait for 8 seconds. */ void lptmr_32khz_input() { unsigned int compare_value=32768; //4 second delay with prescale=1 printf("\n\n****************************\n"); printf("32 Khz Clock Source Example\n"); //Reset LPTMR module lptmr_clear_registers(); /* * Configure to use 32Khz clock from RTC clock */ printf("Test using RTC OSC\n"); SIM_SCGC6|=SIM_SCGC6_RTC_MASK; //Enable RTC registers RTC_CR|=RTC_CR_OSCE_MASK; //Turn on RTC oscillator SIM_SOPT1|=SIM_SOPT1_OSC32KSEL_MASK; //Select RTC OSC as source for ERCLK32K /* Configure LPTMR */ LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_PSR=LPTMR_PSR_PCS(0x2)|LPTMR_PSR_PRESCALE(0x1); //Use 32khz clock (ERCLK32K) and divide source by 4 printf("LPTMR using 32Khz clock with PRESCALE=0x1, and compare value=32768 (4 seconds)\n"); printf("Press a key to start counter\n"); in_char(); LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT with default settings printf("Counting...\n\n"); //Wait for Timer Compare Flag to be set while((LPTMR0_CSR&LPTMR_CSR_TCF_MASK)==0) { //This may not get proper counter data if the CNR is read at the same time it is incremented //printf("Current value of counter register CNR is %d\n",LPTMR0_CNR); } printf("4 seconds should have passed\n"); printf("End of 32 Khz Clock Source Example\n"); printf("****************************\n\n"); }
/************************************************************************* * 野火嵌入式开发工作室 * * 函数名称:lptmr_counter_init * 功能说明:LPT累加捕捉 * 参数说明:LPT0_ALTn 输入管脚号 ,只能是 LPT0_ALT1、LPT0_ALT2 * count 产生中断的累加计数值 * PrescaleValue 延时滤波 * LPT_CFG 触发方式 * 函数返回:无 * 修改时间:2012-3-14 * 备 注: *************************************************************************/ void lptmr_counter_init(LPT0_ALTn altn,u16 count,u8 PrescaleValue,LPT_CFG cfg) { if(PrescaleValue > 0x0f)PrescaleValue=0x0f; //设置输入管脚 if(altn==LPT0_ALT1) { SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; //打开 PORTA 时钟 PORTA_PCR19=PORT_PCR_MUX(0x6); //在PTA19上使用 ALT6 } else if(altn==LPT0_ALT2) { SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; //使能 PORTC 时钟 PORTC_PCR5=PORT_PCR_MUX(0x4); //在PTC5上使用 ALT4 } else //不可能发生事件 { assert_failed(__FILE__, __LINE__); //设置管脚有误? } /* 开启模块时钟 */ SIM_SCGC5|=SIM_SCGC5_LPTIMER_MASK; //使能LPT模块时钟 /* 清状态寄存器 */ LPTMR0_CSR=0x00; //先关了LPT,这样才能设置时钟分频等 /* 设置累加计数值 */ LPTMR_CMR_REG(LPTMR0_BASE_PTR) = LPTMR_CMR_COMPARE(count); //设置比较值 /* 时钟选择 */ LPTMR_PSR_REG(LPTMR0_BASE_PTR) = LPTMR_PSR_PCS(0x1) | LPTMR_PSR_PBYP_MASK | LPTMR_PSR_PRESCALE(PrescaleValue); //使用 LPO clock 且 bypass glitch filter // 开启和配置脉冲滤波器:2^n个时钟上升沿才识别 /* 管脚设置、使能中断 */ LPTMR_CSR_REG(LPTMR0_BASE_PTR) = LPTMR_CSR_TPS(altn)| LPTMR_CSR_TMS_MASK | ( cfg ==LPT_Falling ? LPTMR_CSR_TPP_MASK : 0 ) | LPTMR_CSR_TEN_MASK | LPTMR_CSR_TIE_MASK ; // 选择输入管脚 选择脉冲计数 下降沿 上升沿 使能LPT // TFC = 0,即计数值等于比较值时,计数值复位 enable_irq(LPTMR_irq); //开引脚的IRQ中断 }
/* * Internal Reference Clock (PSC=0x0) * The Internal Reference Clock can come from two clock sources. * If MCG_C2[IRCS]=0, then uses slow internal clock (32kHz) * If MCG_C2[IRCS]=1, then uses fast internal clock (2Mhz) * * This example uses fast internal clock. It is pre-scaled to wait for 4 seconds. * Because of trim values, it may be slightly faster or slower than this. */ void lptmr_internal_ref_input() { unsigned int compare_value=15625; //4 seconds with prescale=8 and 2Mhz fast clock printf("\n\n****************************\n"); printf("Internal Reference Clock Example\n"); //Reset LPTMR module lptmr_clear_registers(); /* Ensure Internal Reference Clock is Enabled */ MCG_C1|=MCG_C1_IRCLKEN_MASK; //Enable fast internal ref clock by setting MCG_C2[IRCS]=1 //If wanted to use 32Khz slow mode, set MCG_C2[IRCS]=0 instead MCG_C2|=MCG_C2_IRCS_MASK; /* Configure LPTMR */ LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_PSR=LPTMR_PSR_PCS(0x0)|LPTMR_PSR_PRESCALE(0x8); //Use internal clock prescaled by 512 printf("LPTMR using fast internal ref clock with PRESCALE=0x8, and compare value=15625 (4 seconds)\n"); printf("Press a key to start counter\n"); in_char(); LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT with default settings printf("Counting...\n\n"); //Wait for Timer Compare Flag to be set while((LPTMR0_CSR&LPTMR_CSR_TCF_MASK)==0) { //This may not get proper counter data if the CNR is read at the same time it is incremented //printf("Current value of counter register CNR is %d\n",LPTMR0_CNR); } printf("4 seconds should have passed\n"); printf("End of Internal reference Clock Source Example\n"); printf("****************************\n\n"); }
/* * Timer will trigger interrupt after 5 seconds */ void lptmr_interrupt(void) { int compare_value=5000; //value must be less than 0xFFFF LPTMR_INTERRUPT=0; //Clear global variable //Reset LPTMR module //lptmr_clear_registers(); printf("\n\n****************************\n"); printf("LPTMR Interrupt Example\n"); /* Enable LPT Interrupt in NVIC*/ enable_irq(85); //LPTMR Vector is 101. IRQ# is 101-16=85 /* Configure LPT */ LPTMR0_CMR = LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_PSR = LPTMR_PSR_PCS(0x1) | LPTMR_PSR_PBYP_MASK; //Use LPO clock and bypass prescale LPTMR0_CSR = LPTMR_CSR_TIE_MASK; //Enable LPT interrupt printf("LPTMR using LPO clock with no prescale, and compare value=5000 (5 seconds)\n"); printf("Press a key to start counter\n"); in_char(); //wait for keyboard press LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; //Turn on LPTMR and start counting printf("Counting...\n\n"); /* Wait for the global variable to be set in LPTMR ISR */ while(LPTMR_INTERRUPT == 0) {} printf("Timer should have waited for 5 seconds\n"); LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK; //Turn off LPT to avoid more interrupts printf("End of LPTMR Interrupt Example\n"); printf("****************************\n\n"); //Reset LPTMR module //lptmr_clear_registers(); }
void lp_ticker_init(void) { if (lp_ticker_inited) { return; } lp_ticker_inited = 1; // RTC might be configured already, don't reset it RTC_HAL_SetSupervisorAccessCmd(RTC_BASE, true); if (!RTC_HAL_IsCounterEnabled(RTC_BASE)) { // select RTC for OSC32KSEL CLOCK_HAL_SetSource(SIM_BASE, kClockOsc32kSel, 2); // configure RTC SIM_HAL_EnableRtcClock(SIM_BASE, 0U); RTC_HAL_Init(RTC_BASE); RTC_HAL_Enable(RTC_BASE); for (volatile uint32_t wait_count = 0; wait_count < 1000000; wait_count++); RTC_HAL_SetAlarmIntCmd(RTC_BASE, false); RTC_HAL_SetSecsIntCmd(RTC_BASE, false); RTC_HAL_SetAlarmReg(RTC_BASE, 0); RTC_HAL_EnableCounter(RTC_BASE, true); } vIRQ_ClearPendingIRQ(RTC_IRQn); vIRQ_SetVector(RTC_IRQn, (uint32_t)rct_isr); vIRQ_EnableIRQ(RTC_IRQn); // configure LPTMR CLOCK_SYS_EnableLptimerClock(0); LPTMR0_CSR = 0x00; LPTMR0_PSR = 0x00; LPTMR0_CMR = 0x00; LPTMR_HAL_SetTimerModeMode(LPTMR0_BASE, kLptmrTimerModeTimeCounter); LPTMR0_PSR |= LPTMR_PSR_PCS(0x2) | LPTMR_PSR_PBYP_MASK; LPTMR_HAL_SetIntCmd(LPTMR0_BASE, 1); LPTMR_HAL_SetFreeRunningCmd(LPTMR0_BASE, 0); IRQn_Type timer_irq[] = LPTMR_IRQS; vIRQ_SetVector(timer_irq[0], (uint32_t)lptmr_isr); vIRQ_EnableIRQ(timer_irq[0]); }
static inline int lptmr_init(uint8_t dev, uint32_t freq, timer_cb_t cb, void *arg) { int32_t prescale = _lptmr_compute_prescaler(dev, freq); if (prescale < 0) { return -1; } LPTMR_Type *hw = lptmr_config[dev].dev; /* Disable IRQs to avoid race with ISR */ unsigned int mask = irq_disable(); /* Turn on module clock */ LPTMR_CLKEN(); /* Completely disable the module before messing with the settings */ hw->CSR = 0; /* select clock source and configure prescaler */ hw->PSR = LPTMR_PSR_PCS(lptmr_config[dev].src) | ((uint32_t)prescale); /* Enable IRQs on the counting channel */ NVIC_ClearPendingIRQ(lptmr_config[dev].irqn); NVIC_EnableIRQ(lptmr_config[dev].irqn); _lptmr_set_cb_config(dev, cb, arg); /* Reset state */ lptmr[dev].running = 1; lptmr[dev].cnr = 0; lptmr[dev].cmr = 0; hw->CMR = 0; hw->CSR = LPTMR_CSR_TEN_MASK | LPTMR_CSR_TFC_MASK; irq_restore(mask); return 0; }
if (extosc == 1) { LPTMR0->PSR = LPTMR_PSR_PCS(3) | LPTMR_PSR_PRESCALE(divider); return; } if (extosc % 2 != 0) //If we can't divide by two anymore break; divider++; extosc >>= 1; } } } } //No suitable external oscillator clock -> Use fast internal oscillator (4MHz / divider) MCG->C1 |= MCG_C1_IRCLKEN_MASK; MCG->C2 |= MCG_C2_IRCS_MASK; LPTMR0->PSR = LPTMR_PSR_PCS(0); switch (MCG->SC & MCG_SC_FCRDIV_MASK) { case MCG_SC_FCRDIV(0): //4MHz LPTMR0->PSR |= LPTMR_PSR_PRESCALE(1); break; case MCG_SC_FCRDIV(1): //2MHz LPTMR0->PSR |= LPTMR_PSR_PRESCALE(0); break; default: //1MHz or anything else, in which case we put it on 1MHz MCG->SC &= ~MCG_SC_FCRDIV_MASK; MCG->SC |= MCG_SC_FCRDIV(2); LPTMR0->PSR |= LPTMR_PSR_PBYP_MASK; } }
/* * Counts pulses found on LPTMR0_ALT1 and LPTMR0_ALT2. * LPTMR0_ALT3 not supported on TWR-K60N512 or TWR-K40X256 * * LPTMR0_ALT1 is pin PORTA19 (ALT6) * On TWR-K40X256, PORT19 is connected to XTAL and thus should not be * driven by an external source, as it will conflict with the crystal clock * on the board. * On TWR-K60N512, PORTA19 is conected to pin 18 on J15 * * LPTMR0_ALT2 is pin PORTC5 (ALT4). * On TWR-K40X256, PORTC5 is connected to pin 18 on J15 * On TWR-K60N512, PORTC5 is conected A70 on TWR-ELEV * * * */ void lptmr_pulse_counter(char pin_select) { unsigned int compare_value=1000; char input; printf("\n\n****************************\n"); printf("LPTMR Pulse Counting Example on LPTMR_ALT%d\n\n",pin_select); //Reset LPTMR module lptmr_clear_registers(); //Set up GPIO if(pin_select==LPTMR_ALT1) { SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; //Turn on PORTA clock PORTA_PCR19=PORT_PCR_MUX(0x6); //Use ALT6 on PTA19 printf("Testing ALT1 pin on PORTA19\n"); printf("\tTWR-K40X256: ALT1 is connected to XTAL and should not be driven\n"); printf("\tTWR-K60N512: ALT1 is conected to pin 18 on J15\n"); } else if(pin_select==LPTMR_ALT2) { SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock PORTC_PCR5=PORT_PCR_MUX(0x4); //Use ALT4 on PTC5 printf("Testing ALT2 pin on PORTC5\n"); printf("\tTWR-K40X256: ALT2 is connected to pin 18 on J15\n"); printf("\tTWR-K60N512: ALT2 is conected A70 on TWR-ELEV\n"); } else { printf("Invalid pin selected\n"); printf("****************************\n"); return; } /* Test requires external hardware. Need to confirm if want to run test or not */ printf("\nThis test requires a function generator, or another way of producing a pulse signal on the pin specified above. "); printf("Please connect that device to the specified pin (Except if using ALT1 on the TWR-K40X256)\n\n"); printf("If you would like to continue with this example, press \"y\". To skip press any other key\n"); input=in_char(); //wait for keyboard press printf("\n"); if(input!='y' && input!='Y') { printf("Exiting LPTMR Pulse Counting Example on LPTMR_ALT%d\n",pin_select); printf("****************************\n"); return; } LPTMR0_PSR=LPTMR_PSR_PCS(0x1)|LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value LPTMR0_CSR=LPTMR_CSR_TPS(pin_select)|LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default) printf("Press any key to start pulse counter\n"); in_char(); //wait for keyboard press LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT //Wait for compare flag to be set while((LPTMR0_CSR&LPTMR_CSR_TCF_MASK)==0) { //This may not get proper counter data if the CNR is read at the same time it is incremented printf("Current value of pulse count register CNR is %d\n",LPTMR0_CNR); } printf("Detected %d pulses on LPTMR_ALT%d\n",compare_value,pin_select); printf("End of Pulse Counting Example\n"); printf("****************************\n"); }