/* * 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"); }
/*---------------------------------------------------------------------------*/ 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"); }
/* * 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"); }
/* ===================================================================*/ 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 */ }
/*---------------------------------------------------------------------------*/ void rtimer_arch_schedule(rtimer_clock_t t) { PRINTF("rtimer_arch_schedule time 0x%4x\n",(unsigned int)t); #if RTIMER_CONF_USE_LPTMR RT_DISABLE(); /* Disable LPTMR to write to CMR register */ LPTMR0_CMR = LPTMR_CMR_COMPARE(t); /* enable LPTMR interrupts and clear interrupt flag */ LPTMR0_CSR |= (LPTMR_CSR_TCF_MASK | LPTMR_CSR_TIE_MASK); RT_ENABLE(); /* Re-enable LPTMR */ #else RT_MODE_CAPTURE(); TPM0_C0V = TPM_CnV_VAL(t); RT_MODE_COMPARE(); /* enable tmp0 channel 0 interrupts */ TPM0_C0SC |= TPM_CnSC_CHIE_MASK; TPM0_STATUS = (TPM_STATUS_CH0F_MASK | TPM_STATUS_CH1F_MASK | TPM_STATUS_CH2F_MASK | TPM_STATUS_CH3F_MASK | TPM_STATUS_CH4F_MASK | TPM_STATUS_CH5F_MASK | TPM_STATUS_TOF_MASK); #endif /* RTIMER_CONF_USE_LPTMR */ }
ER lptmr_pulse_capture_init(PIN pin) { SIM_SCGC5 |= SIM_SCGC5_LPTIMER_MASK; /*使能LPT模块时钟*/ lptmr_registers_clear(); switch (pin) { case PTA19: SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; /*打开 PORTA 时钟*/ PORTA_PCR19 = PORT_PCR_MUX(0x6); /*在PTA19上使用 ALT6*/ LPTMR0_CSR = LPTMR_CSR_TPS(0x01); /*设置LPT使用选择的引脚*/ break; case PTC5: SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; /*打开 PORTC 时钟*/ PORTC_PCR5 = PORT_PCR_MUX(0x4); /*在PTC5上使用 ALT4*/ LPTMR0_CSR = LPTMR_CSR_TPS(0x02); /*设置LPT使用选择的引脚*/ break; default: return E_OBJ; } LPTMR0_PSR |= LPTMR_PSR_PBYP_MASK; /*设定PBYP为1即每捕捉到一次上升沿计数器累加一次*/ LPTMR0_CMR = LPTMR_CMR_COMPARE(LPTMR_PULSE_COMPARE); /*设置比较值*/ LPTMR0_CSR |= LPTMR_CSR_TMS_MASK; /*进入脉冲累加模式,上升沿捕捉*/ LPTMR0_CSR |= LPTMR_CSR_TEN_MASK; /*开启 LPT模块*/ return E_OK; }
/******************************************************************************* * * 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 初始化配置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; }
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 }
/** * @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; }
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); }
/** * @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; /*如果初始化成功*/ }
/*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"); }
/* ** =================================================================== ** Method : LPTmr1_Init (component Init_LPTMR) ** ** Description : ** This method initializes registers of the LPTMR module ** according to the Peripheral Initialization settings. ** Call this method in user code to initialize the module. By ** default, the method is called by PE automatically; see "Call ** Init method" property of the component for more details. ** Parameters : None ** Returns : Nothing ** =================================================================== */ void LPTmr1_Init(void) { /* 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=0,TIE=0,TPS=0,TPP=0,TFC=0,TMS=0,TEN=0 */ LPTMR0_CSR = 0x00U; /* LPTMR0_CMR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,COMPARE=0xC8 */ LPTMR0_CMR = LPTMR_CMR_COMPARE(0xC8); /* 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; /* 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=7,PBYP=0,PCS=0 */ LPTMR0_PSR = LPTMR_PSR_PRESCALE(0x07); /* LPTMR0_CSR: TCF=0,TEN=1 */ LPTMR0_CSR = (uint32_t)((LPTMR0_CSR & (uint32_t)~(uint32_t)( LPTMR_CSR_TCF_MASK )) | (uint32_t)( LPTMR_CSR_TEN_MASK )); }
/*---------------------------------------------------------------------------*/ void LPTMR_IRQHandler(void) { ENERGEST_ON(ENERGEST_TYPE_IRQ); watchdog_start(); PRINTF("rtimer_arch_init compare event at 0x%4x.\n", rtimer_arch_now()); LPTMR0_CMR = LPTMR_CMR_COMPARE(LPTMR_CMR_COMPARE_MASK); LPTMR0_CSR = (uint32_t) ((LPTMR0_CSR & (uint32_t) ~(uint32_t) (LPTMR_CSR_TIE_MASK)) | (uint32_t) (LPTMR_CSR_TCF_MASK)); // Clear interrupt flag and disable interrupt rtimer_run_next(); watchdog_stop(); ENERGEST_OFF(ENERGEST_TYPE_IRQ); }
/************************************************************************* * 野火嵌入式开发工作室 * * 函数名称: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中断 }
/* * 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"); }
/* * 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(); }
/* * 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"); }