Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HardwarePulseCounter.cpp@3:2b2c973f1549, 2019-10-07 (annotated)
- Committer:
- hermano
- Date:
- Mon Oct 07 21:21:06 2019 +0000
- Revision:
- 3:2b2c973f1549
- Parent:
- 1:2bbb30dc3ed3
- Child:
- 4:437833c86cdd
1.3
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hermano | 0:baad428cbcc7 | 1 | #include "HardwarePulseCounter.h" |
| hermano | 0:baad428cbcc7 | 2 | |
| hermano | 0:baad428cbcc7 | 3 | PulseCounter::PulseCounter(TIM_TypeDef *TIMx, GPIO_TypeDef *GPIOx, uint16_t PINx){ |
| hermano | 0:baad428cbcc7 | 4 | this->TIM = TIMx; |
| hermano | 0:baad428cbcc7 | 5 | this->GPIO = GPIOx; |
| hermano | 0:baad428cbcc7 | 6 | this->PIN = PINx; |
| hermano | 0:baad428cbcc7 | 7 | }; |
| hermano | 0:baad428cbcc7 | 8 | |
| hermano | 0:baad428cbcc7 | 9 | |
| hermano | 0:baad428cbcc7 | 10 | void PulseCounter::CounterConfig(){ |
| hermano | 0:baad428cbcc7 | 11 | |
| hermano | 0:baad428cbcc7 | 12 | if(this->TIM == TIM1){ |
| hermano | 0:baad428cbcc7 | 13 | __HAL_RCC_TIM1_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 14 | } |
| hermano | 0:baad428cbcc7 | 15 | |
| hermano | 0:baad428cbcc7 | 16 | else if(this->TIM == TIM2){ |
| hermano | 0:baad428cbcc7 | 17 | __HAL_RCC_TIM2_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 18 | } |
| hermano | 0:baad428cbcc7 | 19 | |
| hermano | 0:baad428cbcc7 | 20 | else if(this->TIM == TIM3){ |
| hermano | 0:baad428cbcc7 | 21 | __HAL_RCC_TIM3_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 22 | } |
| hermano | 0:baad428cbcc7 | 23 | |
| hermano | 0:baad428cbcc7 | 24 | else if(this->TIM == TIM4){ |
| hermano | 0:baad428cbcc7 | 25 | __HAL_RCC_TIM4_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 26 | } |
| hermano | 0:baad428cbcc7 | 27 | |
| hermano | 0:baad428cbcc7 | 28 | this->htim.Instance = this->TIM; |
| hermano | 0:baad428cbcc7 | 29 | this->htim.Init.Period = 1000; |
| hermano | 0:baad428cbcc7 | 30 | this->htim.Init.Prescaler = 1; |
| hermano | 0:baad428cbcc7 | 31 | this->htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
| hermano | 0:baad428cbcc7 | 32 | this->htim.Init.CounterMode = TIM_COUNTERMODE_UP; |
| hermano | 0:baad428cbcc7 | 33 | this->htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
| hermano | 0:baad428cbcc7 | 34 | } |
| hermano | 0:baad428cbcc7 | 35 | |
| hermano | 0:baad428cbcc7 | 36 | |
| hermano | 0:baad428cbcc7 | 37 | void PulseCounter::GpioConfig(void){ |
| hermano | 0:baad428cbcc7 | 38 | |
| hermano | 0:baad428cbcc7 | 39 | if(this->GPIO == GPIOA){ |
| hermano | 0:baad428cbcc7 | 40 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 41 | } |
| hermano | 0:baad428cbcc7 | 42 | |
| hermano | 0:baad428cbcc7 | 43 | else if(this->GPIO == GPIOB){ |
| hermano | 0:baad428cbcc7 | 44 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 45 | } |
| hermano | 0:baad428cbcc7 | 46 | |
| hermano | 0:baad428cbcc7 | 47 | else if(this->GPIO == GPIOC){ |
| hermano | 0:baad428cbcc7 | 48 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 49 | } |
| hermano | 0:baad428cbcc7 | 50 | |
| hermano | 0:baad428cbcc7 | 51 | else if(this->GPIO == GPIOD){ |
| hermano | 0:baad428cbcc7 | 52 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
| hermano | 0:baad428cbcc7 | 53 | } |
| hermano | 0:baad428cbcc7 | 54 | |
| hermano | 0:baad428cbcc7 | 55 | |
| hermano | 0:baad428cbcc7 | 56 | this->GPIO_InitStruct.Pin = this->PIN; |
| hermano | 0:baad428cbcc7 | 57 | this->GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;; |
| hermano | 0:baad428cbcc7 | 58 | this->GPIO_InitStruct.Pull = GPIO_NOPULL; |
| hermano | 0:baad428cbcc7 | 59 | this->GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| hermano | 0:baad428cbcc7 | 60 | HAL_GPIO_Init(this->GPIO, &this->GPIO_InitStruct); |
| hermano | 0:baad428cbcc7 | 61 | } |
| hermano | 0:baad428cbcc7 | 62 | |
| hermano | 0:baad428cbcc7 | 63 | |
| hermano | 0:baad428cbcc7 | 64 | void PulseCounter::SlaveConfig(){ |
| hermano | 0:baad428cbcc7 | 65 | |
| hermano | 0:baad428cbcc7 | 66 | this->sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1; |
| hermano | 0:baad428cbcc7 | 67 | this->sSlaveConfig.InputTrigger = TIM_TS_TI1FP1; |
| hermano | 0:baad428cbcc7 | 68 | this->sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING; |
| hermano | 0:baad428cbcc7 | 69 | this->sSlaveConfig.TriggerFilter = 15; |
| hermano | 0:baad428cbcc7 | 70 | HAL_TIM_SlaveConfigSynchronization(&this->htim, &this->sSlaveConfig); |
| hermano | 0:baad428cbcc7 | 71 | } |
| hermano | 0:baad428cbcc7 | 72 | |
| hermano | 0:baad428cbcc7 | 73 | |
| hermano | 0:baad428cbcc7 | 74 | void PulseCounter::MasterConfig(){ |
| hermano | 0:baad428cbcc7 | 75 | |
| hermano | 0:baad428cbcc7 | 76 | this->sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
| hermano | 0:baad428cbcc7 | 77 | this->sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
| hermano | 0:baad428cbcc7 | 78 | HAL_TIMEx_MasterConfigSynchronization(&this->htim, &this->sMasterConfig); |
| hermano | 0:baad428cbcc7 | 79 | } |
| hermano | 0:baad428cbcc7 | 80 | |
| hermano | 0:baad428cbcc7 | 81 | |
| hermano | 0:baad428cbcc7 | 82 | void PulseCounter::sConfig(){ |
| hermano | 0:baad428cbcc7 | 83 | |
| hermano | 0:baad428cbcc7 | 84 | this->sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; |
| hermano | 0:baad428cbcc7 | 85 | this->sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; |
| hermano | 0:baad428cbcc7 | 86 | this->sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; |
| hermano | 0:baad428cbcc7 | 87 | this->sConfigIC.ICFilter = 0; |
| hermano | 0:baad428cbcc7 | 88 | } |
| hermano | 0:baad428cbcc7 | 89 | |
| hermano | 0:baad428cbcc7 | 90 | |
| hermano | 0:baad428cbcc7 | 91 | void PulseCounter::TIM_Start(){ |
| hermano | 0:baad428cbcc7 | 92 | |
| hermano | 0:baad428cbcc7 | 93 | if(this->TIM == TIM2){ |
| hermano | 0:baad428cbcc7 | 94 | RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; |
| hermano | 0:baad428cbcc7 | 95 | } |
| hermano | 0:baad428cbcc7 | 96 | |
| hermano | 0:baad428cbcc7 | 97 | else if(this->TIM == TIM3){ |
| hermano | 0:baad428cbcc7 | 98 | RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; |
| hermano | 0:baad428cbcc7 | 99 | } |
| hermano | 0:baad428cbcc7 | 100 | |
| hermano | 0:baad428cbcc7 | 101 | else if(this->TIM == TIM4){ |
| hermano | 0:baad428cbcc7 | 102 | RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; |
| hermano | 0:baad428cbcc7 | 103 | } |
| hermano | 0:baad428cbcc7 | 104 | |
| hermano | 0:baad428cbcc7 | 105 | this->TIM->CR1 |= TIM_CR1_CEN; |
| hermano | 0:baad428cbcc7 | 106 | } |
| hermano | 0:baad428cbcc7 | 107 | |
| hermano | 0:baad428cbcc7 | 108 | void PulseCounter::Start_Configurations(){ |
| hermano | 0:baad428cbcc7 | 109 | |
| hermano | 0:baad428cbcc7 | 110 | this->GpioConfig(); |
| hermano | 0:baad428cbcc7 | 111 | this->CounterConfig(); |
| hermano | 0:baad428cbcc7 | 112 | this->SlaveConfig(); |
| hermano | 0:baad428cbcc7 | 113 | this->MasterConfig(); |
| hermano | 0:baad428cbcc7 | 114 | this->sConfig(); |
| hermano | 0:baad428cbcc7 | 115 | this->TIM_Start(); |
| hermano | 0:baad428cbcc7 | 116 | |
| hermano | 0:baad428cbcc7 | 117 | HAL_TIM_Base_Start(&this->htim); |
| hermano | 0:baad428cbcc7 | 118 | } |
| hermano | 0:baad428cbcc7 | 119 | |
| hermano | 0:baad428cbcc7 | 120 | uint32_t PulseCounter::getTimerValue(){ |
| hermano | 0:baad428cbcc7 | 121 | |
| hermano | 0:baad428cbcc7 | 122 | this->count = this->TIM->CNT; |
| hermano | 0:baad428cbcc7 | 123 | return this->count; |
| hermano | 0:baad428cbcc7 | 124 | } |
| hermano | 0:baad428cbcc7 | 125 | |
| hermano | 0:baad428cbcc7 | 126 | void PulseCounter::clearTimerValue(){ |
| hermano | 0:baad428cbcc7 | 127 | |
| hermano | 0:baad428cbcc7 | 128 | this->TIM->CNT = 0; |
| hermano | 0:baad428cbcc7 | 129 | } |