Skip to content

Commit bef6fdf

Browse files
committed
[NUCLEO_F103RB] Code cleanup, correction in pins definition
1 parent 7d145c8 commit bef6fdf

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/PinNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ typedef enum {
102102
PC_14 = 0x2E,
103103
PC_15 = 0x2F,
104104

105+
PD_0 = 0x30,
106+
PD_1 = 0x31,
105107
PD_2 = 0x32,
106-
PD_8 = 0x38,
107108

108109
// Arduino connector namings
109110
A0 = PA_0,

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/us_ticker.c

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929
#include "us_ticker_api.h"
3030
#include "PeripheralNames.h"
3131

32+
// Timers selection:
33+
// The Master timer clocks the Slave timer
34+
35+
#define TIM_MST TIM1
36+
#define TIM_MST_IRQ TIM1_CC_IRQn
37+
#define TIM_MST_RCC RCC_APB2Periph_TIM1
38+
39+
#define TIM_SLV TIM4
40+
#define TIM_SLV_IRQ TIM4_IRQn
41+
#define TIM_SLV_RCC RCC_APB1Periph_TIM4
42+
43+
#define MST_SLV_ITR TIM_TS_ITR0
44+
3245
int us_ticker_inited = 0;
3346

3447
void us_ticker_init(void) {
@@ -40,78 +53,79 @@ void us_ticker_init(void) {
4053
us_ticker_inited = 1;
4154

4255
// Enable Timers clock
43-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
44-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
56+
RCC_APB2PeriphClockCmd(TIM_MST_RCC, ENABLE);
57+
RCC_APB1PeriphClockCmd(TIM_SLV_RCC, ENABLE);
4558

46-
// Time base configuration
47-
// TIM1 is used as "master", "TIM4" as "slave". TIM4 is clocked by TIM1.
59+
// Master and Slave timers time base configuration
4860
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
4961
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
5062
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
5163
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
5264
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
53-
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
65+
TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure);
5466
TIM_TimeBaseStructure.TIM_Prescaler = 0;
55-
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
67+
TIM_TimeBaseInit(TIM_SLV, &TIM_TimeBaseStructure);
5668

5769
// Master timer configuration
5870
TIM_OCStructInit(&TIM_OCInitStructure);
5971
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
6072
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
6173
TIM_OCInitStructure.TIM_Pulse = 0;
6274
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
63-
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
64-
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
65-
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
75+
TIM_OC1Init(TIM_MST, &TIM_OCInitStructure);
76+
TIM_SelectMasterSlaveMode(TIM_MST, TIM_MasterSlaveMode_Enable);
77+
TIM_SelectOutputTrigger(TIM_MST, TIM_TRGOSource_Update);
6678

6779
// Slave timer configuration
68-
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);
69-
TIM_SelectInputTrigger(TIM4, TIM_TS_ITR0);
80+
TIM_SelectSlaveMode(TIM_SLV, TIM_SlaveMode_External1);
81+
// The connection between Master and Slave is done here
82+
TIM_SelectInputTrigger(TIM_SLV, MST_SLV_ITR);
7083

7184
// Enable timers
72-
TIM_Cmd(TIM4, ENABLE);
73-
TIM_Cmd(TIM1, ENABLE);
85+
TIM_Cmd(TIM_SLV, ENABLE);
86+
TIM_Cmd(TIM_MST, ENABLE);
7487
}
7588

7689
uint32_t us_ticker_read() {
7790
uint32_t counter, counter2;
7891
if (!us_ticker_inited) us_ticker_init();
79-
// A situation might appear when TIM1 overflows right after TIM4 is read and before the
80-
// new (overflowed) value of TIM1 is read, which would make the code below consider the
81-
// previous (incorrect) value of TIM4 and the new value of TIM1, which would return a
92+
// A situation might appear when Master overflows right after Slave is read and before the
93+
// new (overflowed) value of Master is read. Which would make the code below consider the
94+
// previous (incorrect) value of Slave and the new value of Master, which would return a
8295
// value in the past. Avoid this by computing consecutive values of the timer until they
8396
// are properly ordered.
84-
counter = counter2 = (uint32_t)((uint32_t)TIM_GetCounter(TIM4) << 16) + (uint32_t)TIM_GetCounter(TIM1);
97+
counter = counter2 = (uint32_t)((uint32_t)TIM_GetCounter(TIM_SLV) << 16) + (uint32_t)TIM_GetCounter(TIM_MST);
8598
while (1) {
86-
counter2 = (uint32_t)((uint32_t)TIM_GetCounter(TIM4) << 16) + (uint32_t)TIM_GetCounter(TIM1);
87-
if (counter2 > counter)
99+
counter2 = (uint32_t)((uint32_t)TIM_GetCounter(TIM_SLV) << 16) + (uint32_t)TIM_GetCounter(TIM_MST);
100+
if (counter2 > counter) {
88101
break;
102+
}
89103
counter = counter2;
90104
}
91105
return counter2;
92106
}
93107

94108
void us_ticker_set_interrupt(unsigned int timestamp) {
95109
if (timestamp > 0xFFFF) {
96-
TIM_SetCompare1(TIM4, (uint16_t)((timestamp >> 16) & 0xFFFF));
97-
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
98-
NVIC_SetVector(TIM4_IRQn, (uint32_t)us_ticker_irq_handler);
99-
NVIC_EnableIRQ(TIM4_IRQn);
110+
TIM_SetCompare1(TIM_SLV, (uint16_t)((timestamp >> 16) & 0xFFFF));
111+
TIM_ITConfig(TIM_SLV, TIM_IT_CC1, ENABLE);
112+
NVIC_SetVector(TIM_SLV_IRQ, (uint32_t)us_ticker_irq_handler);
113+
NVIC_EnableIRQ(TIM_SLV_IRQ);
100114
}
101115
else {
102-
TIM_SetCompare1(TIM1, (uint16_t)timestamp);
103-
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
104-
NVIC_SetVector(TIM1_CC_IRQn, (uint32_t)us_ticker_irq_handler);
105-
NVIC_EnableIRQ(TIM1_CC_IRQn);
116+
TIM_SetCompare1(TIM_MST, (uint16_t)timestamp);
117+
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
118+
NVIC_SetVector(TIM_MST_IRQ, (uint32_t)us_ticker_irq_handler);
119+
NVIC_EnableIRQ(TIM_MST_IRQ);
106120
}
107121
}
108122

109123
void us_ticker_disable_interrupt(void) {
110-
TIM_ITConfig(TIM1, TIM_IT_CC1, DISABLE);
111-
TIM_ITConfig(TIM4, TIM_IT_CC1, DISABLE);
124+
TIM_ITConfig(TIM_MST, TIM_IT_CC1, DISABLE);
125+
TIM_ITConfig(TIM_SLV, TIM_IT_CC1, DISABLE);
112126
}
113127

114128
void us_ticker_clear_interrupt(void) {
115-
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
116-
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
129+
TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
130+
TIM_ClearITPendingBit(TIM_SLV, TIM_IT_CC1);
117131
}

0 commit comments

Comments
 (0)