Skip to content

Commit 7250536

Browse files
committed
Merge pull request #165 from bcostm/master
[NUCLEO_xxx] Fix issue with attach_us function
2 parents e647c0f + fda11ba commit 7250536

File tree

4 files changed

+146
-124
lines changed

4 files changed

+146
-124
lines changed

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

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@
3737

3838
static int us_ticker_inited = 0;
3939
static uint32_t SlaveCounter = 0;
40-
static uint32_t us_ticker_int_counter = 0;
41-
static uint16_t us_ticker_int_remainder = 0;
40+
static uint32_t oc_int_part = 0;
41+
static uint16_t oc_rem_part = 0;
42+
43+
void set_compare(uint16_t count) {
44+
// Set new output compare value
45+
TIM_SetCompare1(TIM_MST, count);
46+
// Enable IT
47+
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
48+
}
4249

4350
// Used to increment the slave counter
4451
static void tim_update_irq_handler(void) {
@@ -50,27 +57,30 @@ static void tim_update_irq_handler(void) {
5057

5158
// Used by interrupt system
5259
static void tim_oc_irq_handler(void) {
60+
uint16_t cval = TIM_MST->CNT;
61+
5362
// Clear interrupt flag
5463
if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) {
5564
TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
5665
}
57-
58-
if (us_ticker_int_counter > 0) {
59-
TIM_SetCompare1(TIM_MST, 0xFFFF);
60-
us_ticker_int_counter--;
61-
} else {
62-
if (us_ticker_int_remainder > 0) {
63-
TIM_SetCompare1(TIM_MST, us_ticker_int_remainder);
64-
us_ticker_int_remainder = 0;
65-
} else {
66-
// This function is going to disable the interrupts if there are
67-
// no other events in the queue
66+
67+
if (oc_rem_part > 0) {
68+
set_compare(oc_rem_part); // Finish the remaining time left
69+
oc_rem_part = 0;
70+
}
71+
else {
72+
if (oc_int_part > 0) {
73+
set_compare(0xFFFF);
74+
oc_rem_part = cval; // To finish the counter loop the next time
75+
oc_int_part--;
76+
}
77+
else {
6878
us_ticker_irq_handler();
6979
}
70-
}
80+
}
7181
}
7282

73-
void us_ticker_init(void) {
83+
void us_ticker_init(void) {
7484
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
7585

7686
if (us_ticker_inited) return;
@@ -89,13 +99,12 @@ void us_ticker_init(void) {
8999

90100
// Configure interrupts
91101
TIM_ITConfig(TIM_MST, TIM_IT_Update, ENABLE);
92-
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
93102

94-
// For 32-bit counter
103+
// Update interrupt used for 32-bit counter
95104
NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
96105
NVIC_EnableIRQ(TIM_MST_UP_IRQ);
97106

98-
// For ouput compare
107+
// Output compare interrupt used for timeout feature
99108
NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
100109
NVIC_EnableIRQ(TIM_MST_OC_IRQ);
101110

@@ -112,10 +121,10 @@ uint32_t us_ticker_read() {
112121
// value in the past. Avoid this by computing consecutive values of the timer until they
113122
// are properly ordered.
114123
counter = (uint32_t)(SlaveCounter << 16);
115-
counter += (uint32_t)TIM_GetCounter(TIM_MST);
124+
counter += TIM_MST->CNT;
116125
while (1) {
117126
counter2 = (uint32_t)(SlaveCounter << 16);
118-
counter2 += (uint32_t)TIM_GetCounter(TIM_MST);
127+
counter2 += TIM_MST->CNT;
119128
if (counter2 > counter) {
120129
break;
121130
}
@@ -126,22 +135,21 @@ uint32_t us_ticker_read() {
126135

127136
void us_ticker_set_interrupt(unsigned int timestamp) {
128137
int delta = (int)(timestamp - us_ticker_read());
138+
uint16_t cval = TIM_MST->CNT;
129139

130140
if (delta <= 0) { // This event was in the past
131141
us_ticker_irq_handler();
132-
return;
133142
}
134143
else {
135-
us_ticker_int_counter = (uint32_t)(delta >> 16);
136-
us_ticker_int_remainder = (uint16_t)(delta & 0xFFFF);
137-
if (us_ticker_int_counter > 0) { // means delta > 0xFFFF
138-
TIM_SetCompare1(TIM_MST, 0xFFFF);
139-
us_ticker_int_counter--;
144+
oc_int_part = (uint32_t)(delta >> 16);
145+
oc_rem_part = (uint16_t)(delta & 0xFFFF);
146+
if (oc_rem_part <= (0xFFFF - cval)) {
147+
set_compare(cval + oc_rem_part);
148+
oc_rem_part = 0;
140149
} else {
141-
TIM_SetCompare1(TIM_MST, us_ticker_int_remainder);
142-
us_ticker_int_remainder = 0;
150+
set_compare(0xFFFF);
151+
oc_rem_part = oc_rem_part - (0xFFFF - cval);
143152
}
144-
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
145153
}
146154
}
147155

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

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@
3737

3838
static int us_ticker_inited = 0;
3939
static uint32_t SlaveCounter = 0;
40-
static uint32_t us_ticker_int_counter = 0;
41-
static uint16_t us_ticker_int_remainder = 0;
40+
static uint32_t oc_int_part = 0;
41+
static uint16_t oc_rem_part = 0;
42+
43+
void set_compare(uint16_t count) {
44+
// Set new output compare value
45+
TIM_SetCompare1(TIM_MST, count);
46+
// Enable IT
47+
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
48+
}
4249

4350
// Used to increment the slave counter
4451
static void tim_update_irq_handler(void) {
@@ -50,27 +57,30 @@ static void tim_update_irq_handler(void) {
5057

5158
// Used by interrupt system
5259
static void tim_oc_irq_handler(void) {
60+
uint16_t cval = TIM_MST->CNT;
61+
5362
// Clear interrupt flag
5463
if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) {
5564
TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
5665
}
57-
58-
if (us_ticker_int_counter > 0) {
59-
TIM_SetCompare1(TIM_MST, 0xFFFF);
60-
us_ticker_int_counter--;
61-
} else {
62-
if (us_ticker_int_remainder > 0) {
63-
TIM_SetCompare1(TIM_MST, us_ticker_int_remainder);
64-
us_ticker_int_remainder = 0;
65-
} else {
66-
// This function is going to disable the interrupts if there are
67-
// no other events in the queue
66+
67+
if (oc_rem_part > 0) {
68+
set_compare(oc_rem_part); // Finish the remaining time left
69+
oc_rem_part = 0;
70+
}
71+
else {
72+
if (oc_int_part > 0) {
73+
set_compare(0xFFFF);
74+
oc_rem_part = cval; // To finish the counter loop the next time
75+
oc_int_part--;
76+
}
77+
else {
6878
us_ticker_irq_handler();
6979
}
70-
}
80+
}
7181
}
7282

73-
void us_ticker_init(void) {
83+
void us_ticker_init(void) {
7484
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
7585

7686
if (us_ticker_inited) return;
@@ -89,13 +99,12 @@ void us_ticker_init(void) {
8999

90100
// Configure interrupts
91101
TIM_ITConfig(TIM_MST, TIM_IT_Update, ENABLE);
92-
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
93102

94-
// For 32-bit counter
103+
// Update interrupt used for 32-bit counter
95104
NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
96105
NVIC_EnableIRQ(TIM_MST_UP_IRQ);
97106

98-
// For ouput compare
107+
// Output compare interrupt used for timeout feature
99108
NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
100109
NVIC_EnableIRQ(TIM_MST_OC_IRQ);
101110

@@ -112,10 +121,10 @@ uint32_t us_ticker_read() {
112121
// value in the past. Avoid this by computing consecutive values of the timer until they
113122
// are properly ordered.
114123
counter = (uint32_t)(SlaveCounter << 16);
115-
counter += (uint32_t)TIM_GetCounter(TIM_MST);
124+
counter += TIM_MST->CNT;
116125
while (1) {
117126
counter2 = (uint32_t)(SlaveCounter << 16);
118-
counter2 += (uint32_t)TIM_GetCounter(TIM_MST);
127+
counter2 += TIM_MST->CNT;
119128
if (counter2 > counter) {
120129
break;
121130
}
@@ -126,22 +135,21 @@ uint32_t us_ticker_read() {
126135

127136
void us_ticker_set_interrupt(unsigned int timestamp) {
128137
int delta = (int)(timestamp - us_ticker_read());
138+
uint16_t cval = TIM_MST->CNT;
129139

130140
if (delta <= 0) { // This event was in the past
131141
us_ticker_irq_handler();
132-
return;
133142
}
134143
else {
135-
us_ticker_int_counter = (uint32_t)(delta >> 16);
136-
us_ticker_int_remainder = (uint16_t)(delta & 0xFFFF);
137-
if (us_ticker_int_counter > 0) { // means delta > 0xFFFF
138-
TIM_SetCompare1(TIM_MST, 0xFFFF);
139-
us_ticker_int_counter--;
144+
oc_int_part = (uint32_t)(delta >> 16);
145+
oc_rem_part = (uint16_t)(delta & 0xFFFF);
146+
if (oc_rem_part <= (0xFFFF - cval)) {
147+
set_compare(cval + oc_rem_part);
148+
oc_rem_part = 0;
140149
} else {
141-
TIM_SetCompare1(TIM_MST, us_ticker_int_remainder);
142-
us_ticker_int_remainder = 0;
150+
set_compare(0xFFFF);
151+
oc_rem_part = oc_rem_part - (0xFFFF - cval);
143152
}
144-
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
145153
}
146154
}
147155

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

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "stm32f4xx_hal.h"
3232

3333
// Timer selection:
34-
3534
#define TIM_MST TIM1
3635
#define TIM_MST_UP_IRQ TIM1_UP_TIM10_IRQn
3736
#define TIM_MST_OC_IRQ TIM1_CC_IRQn
@@ -41,8 +40,15 @@ static TIM_HandleTypeDef TimMasterHandle;
4140

4241
static int us_ticker_inited = 0;
4342
static uint32_t SlaveCounter = 0;
44-
static uint32_t us_ticker_int_counter = 0;
45-
static uint16_t us_ticker_int_remainder = 0;
43+
static uint32_t oc_int_part = 0;
44+
static uint16_t oc_rem_part = 0;
45+
46+
void set_compare(uint16_t count) {
47+
// Set new output compare value
48+
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
49+
// Enable IT
50+
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
51+
}
4652

4753
// Used to increment the slave counter
4854
static void tim_update_irq_handler(void) {
@@ -55,24 +61,27 @@ static void tim_update_irq_handler(void) {
5561

5662
// Used by interrupt system
5763
static void tim_oc_irq_handler(void) {
64+
uint16_t cval = TIM_MST->CNT;
65+
5866
// Clear interrupt flag
5967
if (__HAL_TIM_GET_ITSTATUS(&TimMasterHandle, TIM_IT_CC1) == SET) {
6068
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
6169
}
62-
63-
if (us_ticker_int_counter > 0) {
64-
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, 0xFFFF);
65-
us_ticker_int_counter--;
66-
} else {
67-
if (us_ticker_int_remainder > 0) {
68-
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, us_ticker_int_remainder);
69-
us_ticker_int_remainder = 0;
70-
} else {
71-
// This function is going to disable the interrupts if there are
72-
// no other events in the queue
70+
71+
if (oc_rem_part > 0) {
72+
set_compare(oc_rem_part); // Finish the remaining time left
73+
oc_rem_part = 0;
74+
}
75+
else {
76+
if (oc_int_part > 0) {
77+
set_compare(0xFFFF);
78+
oc_rem_part = cval; // To finish the counter loop the next time
79+
oc_int_part--;
80+
}
81+
else {
7382
us_ticker_irq_handler();
7483
}
75-
}
84+
}
7685
}
7786

7887
void us_ticker_init(void) {
@@ -89,31 +98,20 @@ void us_ticker_init(void) {
8998
TimMasterHandle.Init.ClockDivision = 0;
9099
TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
91100
TimMasterHandle.Init.RepetitionCounter = 0;
92-
//HAL_TIM_Base_Init(&TimMasterHandle);
93101
HAL_TIM_OC_Init(&TimMasterHandle);
94-
95-
/*
96-
TIM_OC_InitTypeDef sConfig;
97-
sConfig.OCMode = TIM_OCMODE_INACTIVE;
98-
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
99-
sConfig.Pulse = 0;
100-
HAL_TIM_OC_ConfigChannel(&TimMasterHandle, &sConfig, TIM_CHANNEL_1);
101-
*/
102-
102+
103103
// Configure interrupts
104104
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE);
105-
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
106105

107-
// For 32-bit counter
106+
// Update interrupt used for 32-bit counter
108107
NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
109108
NVIC_EnableIRQ(TIM_MST_UP_IRQ);
110109

111-
// For ouput compare
110+
// Output compare interrupt used for timeout feature
112111
NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
113112
NVIC_EnableIRQ(TIM_MST_OC_IRQ);
114113

115114
// Enable timer
116-
//HAL_TIM_Base_Start(&TimMasterHandle);
117115
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
118116
}
119117

@@ -140,22 +138,21 @@ uint32_t us_ticker_read() {
140138

141139
void us_ticker_set_interrupt(unsigned int timestamp) {
142140
int delta = (int)(timestamp - us_ticker_read());
141+
uint16_t cval = TIM_MST->CNT;
143142

144143
if (delta <= 0) { // This event was in the past
145144
us_ticker_irq_handler();
146-
return;
147145
}
148146
else {
149-
us_ticker_int_counter = (uint32_t)(delta >> 16);
150-
us_ticker_int_remainder = (uint16_t)(delta & 0xFFFF);
151-
if (us_ticker_int_counter > 0) { // means delta > 0xFFFF
152-
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, 0xFFFF);
153-
us_ticker_int_counter--;
147+
oc_int_part = (uint32_t)(delta >> 16);
148+
oc_rem_part = (uint16_t)(delta & 0xFFFF);
149+
if (oc_rem_part <= (0xFFFF - cval)) {
150+
set_compare(cval + oc_rem_part);
151+
oc_rem_part = 0;
154152
} else {
155-
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, us_ticker_int_remainder);
156-
us_ticker_int_remainder = 0;
153+
set_compare(0xFFFF);
154+
oc_rem_part = oc_rem_part - (0xFFFF - cval);
157155
}
158-
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
159156
}
160157
}
161158

0 commit comments

Comments
 (0)