Skip to content

Commit 8ae54f1

Browse files
authored
Merge pull request #532 from fpistm/Timer_issue_531
Fix Timer issues
2 parents 2cfddac + 3e7a9ea commit 8ae54f1

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

Diff for: cores/arduino/stm32/timer.c

+28-18
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,17 @@ void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
185185
*/
186186
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
187187
{
188-
uint32_t channel = 0;
189188
stimer_t *obj = get_timer_obj(htim);
190189
switch (htim->Channel) {
191190
case HAL_TIM_ACTIVE_CHANNEL_1:
192-
channel = TIM_CHANNEL_1 / 4;
193191
if (obj->irqHandleOC_CH1 != NULL) {
194192
obj->irqHandleOC_CH1();
195193
}
196194
if (obj->irqHandleOC != NULL) {
197-
obj->irqHandleOC(obj, channel);
195+
obj->irqHandleOC(obj, TIM_CHANNEL_1);
198196
}
199197
break;
200198
case HAL_TIM_ACTIVE_CHANNEL_2:
201-
channel = TIM_CHANNEL_2 / 4;
202199
if (obj->irqHandleOC_CH2 != NULL) {
203200
obj->irqHandleOC_CH2();
204201
}
@@ -207,13 +204,11 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
207204
if (obj->irqHandleOC_CH3 != NULL) {
208205
obj->irqHandleOC_CH3();
209206
}
210-
channel = TIM_CHANNEL_3 / 4;
211207
break;
212208
case HAL_TIM_ACTIVE_CHANNEL_4:
213209
if (obj->irqHandleOC_CH4 != NULL) {
214210
obj->irqHandleOC_CH4();
215211
}
216-
channel = TIM_CHANNEL_4 / 4;
217212
break;
218213
default:
219214
break;
@@ -679,7 +674,7 @@ void TimerPulseInit(stimer_t *obj, uint16_t period, uint16_t pulseWidth, void (*
679674
#endif
680675
obj->irqHandleOC = irqHandle;
681676

682-
attachIntHandleOC(obj, NULL, 1, pulseWidth);
677+
attachIntHandleOC(obj, NULL, TIM_CHANNEL_1, pulseWidth);
683678
}
684679

685680
/**
@@ -732,7 +727,12 @@ void setTimerCounter(stimer_t *obj, uint32_t value)
732727
/**
733728
* @brief Set the TIM Capture Compare Register value.
734729
* @param timer_id : id of the timer
735-
* @param channel : TIM Channels to be configured.
730+
* @param channel : TIM Channel associated with the capture compare register.
731+
* This parameter can be one of the following values:
732+
* @arg TIM_CHANNEL_1: TIM Channel 1 selected
733+
* @arg TIM_CHANNEL_2: TIM Channel 2 selected
734+
* @arg TIM_CHANNEL_3: TIM Channel 3 selected
735+
* @arg TIM_CHANNEL_4: TIM Channel 4 selected
736736
* @retval CRR value.
737737
*/
738738
uint32_t getCCRRegister(stimer_t *obj, uint32_t channel)
@@ -744,12 +744,17 @@ uint32_t getCCRRegister(stimer_t *obj, uint32_t channel)
744744
* @brief Set the TIM Capture Compare Register value.
745745
* @param timer_id : id of the timer
746746
* @param channel : TIM Channels to be configured.
747+
* This parameter can be one of the following values:
748+
* @arg TIM_CHANNEL_1: TIM Channel 1 selected
749+
* @arg TIM_CHANNEL_2: TIM Channel 2 selected
750+
* @arg TIM_CHANNEL_3: TIM Channel 3 selected
751+
* @arg TIM_CHANNEL_4: TIM Channel 4 selected
747752
* @param value : register new register.
748753
* @retval None
749754
*/
750755
void setCCRRegister(stimer_t *obj, uint32_t channel, uint32_t value)
751756
{
752-
__HAL_TIM_SET_COMPARE(&(obj->handle), channel * 4, value);
757+
__HAL_TIM_SET_COMPARE(&(obj->handle), channel, value);
753758
}
754759

755760
/**
@@ -1129,11 +1134,16 @@ void attachIntHandle(stimer_t *obj, void (*irqHandle)(stimer_t *))
11291134
* @brief This function will attach timer interrupt to with a particular duty cycle on channel x
11301135
* @param timer_id : timer_id_e
11311136
* @param irqHandle : interrupt routine to call
1132-
* @param timChannel : timer channel
1137+
* @param timChannel : TIM Channel to use
1138+
* This parameter can be one of the following values:
1139+
* @arg TIM_CHANNEL_1: TIM Channel 1 selected
1140+
* @arg TIM_CHANNEL_2: TIM Channel 2 selected
1141+
* @arg TIM_CHANNEL_3: TIM Channel 3 selected
1142+
* @arg TIM_CHANNEL_4: TIM Channel 4 selected
11331143
* @param pulseWidth : phase of the timer where the callback will happen
11341144
* @retval None
11351145
*/
1136-
void attachIntHandleOC(stimer_t *obj, void (*irqHandle)(void), uint16_t timChannel, uint16_t pulseWidth)
1146+
void attachIntHandleOC(stimer_t *obj, void (*irqHandle)(void), uint32_t timChannel, uint16_t pulseWidth)
11371147
{
11381148
TIM_OC_InitTypeDef sConfig = {};
11391149
TIM_HandleTypeDef *handle = &(obj->handle);
@@ -1154,27 +1164,27 @@ void attachIntHandleOC(stimer_t *obj, void (*irqHandle)(void), uint16_t timChann
11541164
return;
11551165
}
11561166
switch (timChannel) {
1157-
case 1:
1167+
case TIM_CHANNEL_1:
11581168
obj->irqHandleOC_CH1 = irqHandle;
11591169
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_1) == HAL_OK) {
11601170
HAL_TIM_OC_Start_IT(handle, TIM_CHANNEL_1);
11611171
}
11621172
break;
1163-
case 2:
1173+
case TIM_CHANNEL_2:
11641174
obj->irqHandleOC_CH2 = irqHandle;
1165-
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_2) != HAL_OK) {
1175+
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_2) == HAL_OK) {
11661176
HAL_TIM_OC_Start_IT(handle, TIM_CHANNEL_2);
11671177
}
11681178
break;
1169-
case 3:
1179+
case TIM_CHANNEL_3:
11701180
obj->irqHandleOC_CH3 = irqHandle;
1171-
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_3) != HAL_OK) {
1181+
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_3) == HAL_OK) {
11721182
HAL_TIM_OC_Start_IT(handle, TIM_CHANNEL_3);
11731183
}
11741184
break;
1175-
case 4:
1185+
case TIM_CHANNEL_4:
11761186
obj->irqHandleOC_CH4 = irqHandle;
1177-
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_4) != HAL_OK) {
1187+
if (HAL_TIM_OC_ConfigChannel(handle, &sConfig, TIM_CHANNEL_4) == HAL_OK) {
11781188
HAL_TIM_OC_Start_IT(handle, TIM_CHANNEL_4);
11791189
}
11801190
break;

Diff for: cores/arduino/stm32/timer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ uint8_t getTimerClkSrc(TIM_TypeDef *tim);
233233
uint32_t getTimerClkFreq(TIM_TypeDef *tim);
234234

235235
void attachIntHandle(stimer_t *obj, void (*irqHandle)(stimer_t *));
236-
void attachIntHandleOC(stimer_t *obj, void (*irqHandle)(void), uint16_t timChannel, uint16_t pulseWidth);
236+
void attachIntHandleOC(stimer_t *obj, void (*irqHandle)(void), uint32_t timChannel, uint16_t pulseWidth);
237237
#endif /* HAL_TIM_MODULE_ENABLED */
238238

239239
#ifdef __cplusplus

0 commit comments

Comments
 (0)