Skip to content

Commit 796cbc2

Browse files
committed
chore(hardwaretimer): harden get API using uint32_t
As all get API's use error handler if an error occur, then no need to use int as return type. Moreover, all HAL API uses uint32_t for those value. This avoid cast issue. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 339e2b9 commit 796cbc2

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

libraries/SrcWrapper/inc/HardwareTimer.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ class HardwareTimer {
177177

178178
// The following function(s) are available for more advanced timer options
179179
TIM_HandleTypeDef *getHandle(); // return the handle address for HAL related configuration
180-
int getChannel(uint32_t channel);
181-
int getLLChannel(uint32_t channel);
182-
int getIT(uint32_t channel);
183-
int getAssociatedChannel(uint32_t channel);
180+
uint32_t getChannel(uint32_t channel);
181+
uint32_t getLLChannel(uint32_t channel);
182+
uint32_t getIT(uint32_t channel);
183+
uint32_t getAssociatedChannel(uint32_t channel);
184184

185185
private:
186186
// Store for each channel if regular, complementary or both are used

libraries/SrcWrapper/src/HardwareTimer.cpp

+50-49
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ void HardwareTimer::pauseChannel(uint32_t channel)
181181
return;
182182
}
183183

184-
int timAssociatedInputChannel;
185-
int LLChannel = getLLChannel(channel);
186-
int interrupt = getIT(channel);
184+
uint32_t timAssociatedInputChannel;
185+
uint32_t LLChannel = getLLChannel(channel);
186+
uint32_t interrupt = getIT(channel);
187187

188188
// Disable channel and corresponding interrupt
189189
__HAL_TIM_DISABLE_IT(&(_timerObj.handle), interrupt);
@@ -238,123 +238,123 @@ void HardwareTimer::resume(void)
238238
* @param Arduino channel [1..4]
239239
* @retval HAL channel. Error handler called if arduino channel is invalid
240240
*/
241-
int HardwareTimer::getChannel(uint32_t channel)
241+
uint32_t HardwareTimer::getChannel(uint32_t channel)
242242
{
243-
int return_value = -1;
243+
uint32_t timChannel = -1;
244244

245245
switch (channel) {
246246
case 1:
247-
return_value = TIM_CHANNEL_1;
247+
timChannel = TIM_CHANNEL_1;
248248
break;
249249
case 2:
250-
return_value = TIM_CHANNEL_2;
250+
timChannel = TIM_CHANNEL_2;
251251
break;
252252
case 3:
253-
return_value = TIM_CHANNEL_3;
253+
timChannel = TIM_CHANNEL_3;
254254
break;
255255
case 4:
256-
return_value = TIM_CHANNEL_4;
256+
timChannel = TIM_CHANNEL_4;
257257
break;
258258
default:
259259
Error_Handler();
260260
}
261-
return return_value;
261+
return timChannel;
262262
}
263263

264264
/**
265265
* @brief Convert arduino channel into LL channels used (regular and/or complementary)
266266
* @param Arduino channel [1..4]
267267
* @retval LL channel. Error handler called if arduino channel is invalid
268268
*/
269-
int HardwareTimer::getLLChannel(uint32_t channel)
269+
uint32_t HardwareTimer::getLLChannel(uint32_t channel)
270270
{
271-
int return_value = 0;
271+
bool error = false;
272+
uint32_t ll_channel = 0;
272273

273274
#if defined(TIM_CCER_CC1NE)
274275
if (__ChannelsUsed[channel - 1] & COMPLEMENTARY_CHAN_MASK) {
275276
// Complementary channel
276277
switch (channel) {
277278
case 1:
278-
return_value = LL_TIM_CHANNEL_CH1N;
279+
ll_channel = LL_TIM_CHANNEL_CH1N;
279280
break;
280281
case 2:
281-
return_value = LL_TIM_CHANNEL_CH2N;
282+
ll_channel = LL_TIM_CHANNEL_CH2N;
282283
break;
283284
case 3:
284-
return_value = LL_TIM_CHANNEL_CH3N;
285+
ll_channel = LL_TIM_CHANNEL_CH3N;
285286
break;
286287
#if defined(LL_TIM_CHANNEL_CH4N)
287288
case 4:
288-
return_value = LL_TIM_CHANNEL_CH4N;
289+
ll_channel = LL_TIM_CHANNEL_CH4N;
289290
break;
290291
#endif
291292
default:
292-
return_value = -1;
293+
error = true;
293294
}
294295
}
295296
#endif
296-
if ((return_value != -1) && (__ChannelsUsed[channel - 1] & REGULAR_CHAN_MASK)) {
297+
if ((!error) && (__ChannelsUsed[channel - 1] & REGULAR_CHAN_MASK)) {
297298
// Regular channel not complementary
298299
switch (channel) {
299300
case 1:
300-
return_value |= LL_TIM_CHANNEL_CH1;
301+
ll_channel |= LL_TIM_CHANNEL_CH1;
301302
break;
302303
case 2:
303-
return_value |= LL_TIM_CHANNEL_CH2;
304+
ll_channel |= LL_TIM_CHANNEL_CH2;
304305
break;
305306
case 3:
306-
return_value |= LL_TIM_CHANNEL_CH3;
307+
ll_channel |= LL_TIM_CHANNEL_CH3;
307308
break;
308309
case 4:
309-
return_value |= LL_TIM_CHANNEL_CH4;
310+
ll_channel |= LL_TIM_CHANNEL_CH4;
310311
break;
311312
default:
312-
return_value = -1;
313+
error = true;
313314
}
314315
}
315-
if (return_value == -1) {
316+
if (error) {
316317
Error_Handler();
317318
}
318-
return return_value;
319+
return ll_channel;
319320
}
320321

321322
/**
322323
* @brief Convert arduino channel into HAL Interrupt ID
323324
* @param Arduino channel [1..4]
324325
* @retval HAL channel. Error handler called if arduino channel is invalid
325326
*/
326-
int HardwareTimer::getIT(uint32_t channel)
327+
uint32_t HardwareTimer::getIT(uint32_t channel)
327328
{
328-
int return_value = -1;
329-
329+
uint32_t interrupt = 0;
330330
switch (channel) {
331331
case 1:
332-
return_value = TIM_IT_CC1;
332+
interrupt = TIM_IT_CC1;
333333
break;
334334
case 2:
335-
return_value = TIM_IT_CC2;
335+
interrupt = TIM_IT_CC2;
336336
break;
337337
case 3:
338-
return_value = TIM_IT_CC3;
338+
interrupt = TIM_IT_CC3;
339339
break;
340340
case 4:
341-
return_value = TIM_IT_CC4;
341+
interrupt = TIM_IT_CC4;
342342
break;
343343
default:
344344
Error_Handler();
345345
}
346-
return return_value;
346+
return interrupt;
347347
}
348348

349349
/**
350350
* @brief Get input associated channel
351351
* Channel 1 and 2 are associated; channel 3 and 4 are associated
352352
* @param Arduino channel [1..4]
353-
* @retval HAL channel. return -1 if arduino channel is invalid
353+
* @retval HAL channel. Error handler called if arduino channel is invalid
354354
*/
355-
int HardwareTimer::getAssociatedChannel(uint32_t channel)
355+
uint32_t HardwareTimer::getAssociatedChannel(uint32_t channel)
356356
{
357-
int timAssociatedInputChannel = -1;
357+
uint32_t timAssociatedInputChannel = 0;
358358
switch (channel) {
359359
case 1:
360360
timAssociatedInputChannel = 2;
@@ -369,6 +369,7 @@ int HardwareTimer::getAssociatedChannel(uint32_t channel)
369369
timAssociatedInputChannel = 3;
370370
break;
371371
default:
372+
Error_Handler();
372373
break;
373374
}
374375
return timAssociatedInputChannel;
@@ -381,9 +382,9 @@ int HardwareTimer::getAssociatedChannel(uint32_t channel)
381382
*/
382383
void HardwareTimer::resumeChannel(uint32_t channel)
383384
{
384-
int timChannel = getChannel(channel);
385-
int timAssociatedInputChannel;
386-
int interrupt = getIT(channel);
385+
uint32_t timChannel = getChannel(channel);
386+
uint32_t timAssociatedInputChannel;
387+
uint32_t interrupt = getIT(channel);
387388

388389
// Clear flag and enable IT
389390
if (callbacks[channel]) {
@@ -631,8 +632,8 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, uint32_t pin, C
631632
*/
632633
void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, ChannelInputFilter_t filter)
633634
{
634-
int timChannel = getChannel(channel);
635-
int timAssociatedInputChannel;
635+
uint32_t timChannel = getChannel(channel);
636+
uint32_t timAssociatedInputChannel;
636637
TIM_OC_InitTypeDef channelOC;
637638
TIM_IC_InitTypeDef channelIC;
638639

@@ -739,7 +740,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, Ch
739740
_ChannelMode[channel - 1] = mode;
740741

741742
if (pin != NC) {
742-
if ((int)getTimerChannel(pin) == timChannel) {
743+
if (getTimerChannel(pin) == timChannel) {
743744
/* Configure PWM GPIO pins */
744745
pinmap_pinout(pin, PinMap_TIM);
745746
#if defined(STM32F1xx)
@@ -807,7 +808,7 @@ void HardwareTimer::setPreloadEnable(bool value)
807808
*/
808809
void HardwareTimer::setCaptureCompare(uint32_t channel, uint32_t compare, TimerCompareFormat_t format)
809810
{
810-
int timChannel = getChannel(channel);
811+
uint32_t timChannel = getChannel(channel);
811812
uint32_t Prescalerfactor = LL_TIM_GetPrescaler(_timerObj.handle.Instance) + 1;
812813
uint32_t CCR_RegisterValue;
813814

@@ -869,7 +870,7 @@ void HardwareTimer::setCaptureCompare(uint32_t channel, uint32_t compare, TimerC
869870
*/
870871
uint32_t HardwareTimer::getCaptureCompare(uint32_t channel, TimerCompareFormat_t format)
871872
{
872-
int timChannel = getChannel(channel);
873+
uint32_t timChannel = getChannel(channel);
873874
uint32_t CCR_RegisterValue = __HAL_TIM_GET_COMPARE(&(_timerObj.handle), timChannel);
874875
uint32_t Prescalerfactor = LL_TIM_GetPrescaler(_timerObj.handle.Instance) + 1;
875876
uint32_t return_value;
@@ -1010,7 +1011,7 @@ void HardwareTimer::detachInterrupt()
10101011
*/
10111012
void HardwareTimer::attachInterrupt(uint32_t channel, callback_function_t callback)
10121013
{
1013-
int interrupt = getIT(channel);
1014+
uint32_t interrupt = getIT(channel);
10141015

10151016
if ((channel == 0) || (channel > (TIMER_CHANNELS + 1))) {
10161017
Error_Handler(); // only channel 1..4 have an interrupt
@@ -1036,7 +1037,7 @@ void HardwareTimer::attachInterrupt(uint32_t channel, callback_function_t callba
10361037
*/
10371038
void HardwareTimer::detachInterrupt(uint32_t channel)
10381039
{
1039-
int interrupt = getIT(channel);
1040+
uint32_t interrupt = getIT(channel);
10401041

10411042
if ((channel == 0) || (channel > (TIMER_CHANNELS + 1))) {
10421043
Error_Handler(); // only channel 1..4 have an interrupt
@@ -1169,14 +1170,14 @@ bool HardwareTimer::isRunning()
11691170
*/
11701171
bool HardwareTimer::isRunningChannel(uint32_t channel)
11711172
{
1172-
int LLChannel = getLLChannel(channel);
1173-
int interrupt = getIT(channel);
1173+
uint32_t LLChannel = getLLChannel(channel);
1174+
uint32_t interrupt = getIT(channel);
11741175
bool ret;
11751176

11761177
// channel is running if: timer is running, and either output channel is
11771178
// enabled or interrupt is set
11781179
ret = LL_TIM_CC_IsEnabledChannel(_timerObj.handle.Instance, LLChannel)
1179-
|| (__HAL_TIM_GET_IT_SOURCE(&(_timerObj.handle), (uint32_t)interrupt) == SET);
1180+
|| (__HAL_TIM_GET_IT_SOURCE(&(_timerObj.handle), interrupt) == SET);
11801181
return (isRunning() && ret);
11811182
}
11821183

0 commit comments

Comments
 (0)