1
1
/*
2
- * Copyright (c) 2016 Thomas Roell. All rights reserved.
2
+ * Copyright (c) 2016-2017 Thomas Roell. All rights reserved.
3
3
*
4
4
* Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
* of this software and associated documentation files (the "Software"), to
@@ -42,96 +42,62 @@ static void stm32l4_servo_event_callback(void *context, uint32_t events)
42
42
stm32l4_servo_schedule_t * active , * pending ;
43
43
unsigned int index ;
44
44
45
- if (events & TIMER_EVENT_PERIOD )
46
- {
47
- pending = servo -> pending ;
48
- servo -> pending = NULL ;
49
-
50
- if (pending )
51
- {
52
- servo -> active = pending ;
53
- }
54
-
55
- active = servo -> active ;
45
+ index = servo -> index ;
46
+ active = servo -> active ;
56
47
57
- if (active -> entries != 0 )
58
- {
59
- if (servo -> period == active -> period )
60
- {
61
- active -> slot [0 ].GPIO -> BSRR = active -> slot [0 ].mask ;
48
+ if (index != active -> entries )
49
+ {
50
+ active -> slot [index ].GPIO -> BRR = active -> slot [index ].mask ;
62
51
63
- stm32l4_timer_compare (& servo -> timer , TIMER_CHANNEL_1 , active -> slot [0 ].offset );
64
- }
65
- else
66
- {
67
- servo -> period = active -> period ;
52
+ index ++ ;
68
53
69
- stm32l4_timer_stop (& servo -> timer );
70
- stm32l4_timer_configure (& servo -> timer , servo -> prescaler - 1 , servo -> period - 1 , 0 );
71
- stm32l4_timer_channel (& servo -> timer , TIMER_CHANNEL_1 , active -> slot [0 ].offset , TIMER_CONTROL_COMPARE_TIMING );
72
- stm32l4_timer_start (& servo -> timer , false);
73
-
74
- active -> slot [0 ].GPIO -> BSRR = active -> slot [0 ].mask ;
54
+ if (index != active -> entries )
55
+ {
56
+ active -> slot [index ].GPIO -> BSRR = active -> slot [index ].mask ;
75
57
76
- servo -> state = SERVO_STATE_ACTIVE ;
77
- }
58
+ stm32l4_timer_period (& servo -> timer , active -> slot [index ].width - 1 , true);
78
59
}
79
60
else
80
61
{
81
- stm32l4_timer_stop (& servo -> timer );
82
-
83
- servo -> active = NULL ;
84
-
85
- servo -> state = SERVO_STATE_READY ;
62
+ stm32l4_timer_period (& servo -> timer , active -> sync - 1 , true);
86
63
}
87
64
88
- servo -> index = 0 ;
89
-
90
- if (pending && (servo -> events & SERVO_EVENT_UPDATE ))
91
- {
92
- (* servo -> callback )(servo -> context , SERVO_EVENT_UPDATE );
93
- }
65
+ servo -> index = index ;
94
66
}
95
67
else
96
68
{
97
- index = servo -> index ;
98
- active = servo -> active ;
69
+ servo -> index = 0 ;
99
70
100
- if (index != active -> entries )
71
+ pending = servo -> pending ;
72
+
73
+ if (pending == NULL )
101
74
{
102
- active -> slot [index ].GPIO -> BRR = active -> slot [index ].mask ;
103
-
104
- index ++ ;
75
+ active -> slot [0 ].GPIO -> BSRR = active -> slot [0 ].mask ;
105
76
106
- if (index != active -> entries )
77
+ stm32l4_timer_period (& servo -> timer , active -> slot [0 ].width - 1 , true);
78
+ }
79
+ else
80
+ {
81
+ if (pending -> entries )
107
82
{
108
- active -> slot [index ].GPIO -> BSRR = active -> slot [index ].mask ;
83
+ pending -> slot [0 ].GPIO -> BSRR = pending -> slot [0 ].mask ;
109
84
110
- stm32l4_timer_compare (& servo -> timer , TIMER_CHANNEL_1 , active -> slot [index ]. offset );
85
+ stm32l4_timer_period (& servo -> timer , pending -> slot [0 ]. width - 1 , true );
111
86
}
112
87
else
113
88
{
114
- if (active -> offset )
115
- {
116
- stm32l4_timer_compare (& servo -> timer , TIMER_CHANNEL_1 , active -> offset );
117
- }
118
- else
119
- {
120
- if (servo -> events & SERVO_EVENT_SYNC )
121
- {
122
- (* servo -> callback )(servo -> context , SERVO_EVENT_SYNC );
123
- }
124
- }
89
+ stm32l4_timer_stop (& servo -> timer );
90
+
91
+ servo -> state = SERVO_STATE_READY ;
125
92
}
126
93
127
- servo -> index = index ;
94
+ servo -> pending = NULL ;
95
+ servo -> active = pending ;
128
96
}
129
- else
97
+
98
+ if (servo -> events & SERVO_EVENT_SYNC )
130
99
{
131
- if (servo -> events & SERVO_EVENT_SYNC )
132
- {
133
- (* servo -> callback )(servo -> context , SERVO_EVENT_SYNC );
134
- }
100
+ (* servo -> callback )(servo -> context , SERVO_EVENT_SYNC );
135
101
}
136
102
}
137
103
}
@@ -150,7 +116,6 @@ bool stm32l4_servo_create(stm32l4_servo_t *servo, unsigned int instance, unsigne
150
116
151
117
servo -> index = 0 ;
152
118
servo -> prescaler = 0 ;
153
- servo -> period = 0 ;
154
119
servo -> active = NULL ;
155
120
servo -> pending = NULL ;
156
121
@@ -178,7 +143,7 @@ bool stm32l4_servo_enable(stm32l4_servo_t *servo, const stm32l4_servo_table_t *t
178
143
179
144
servo -> state = SERVO_STATE_BUSY ;
180
145
181
- if (!stm32l4_timer_enable (& servo -> timer , 0 , 0 , 0 , stm32l4_servo_event_callback , servo , ( TIMER_EVENT_PERIOD | TIMER_EVENT_CHANNEL_1 ) ))
146
+ if (!stm32l4_timer_enable (& servo -> timer , 0 , 0 , 0 , stm32l4_servo_event_callback , servo , TIMER_EVENT_PERIOD ))
182
147
{
183
148
servo -> state = SERVO_STATE_INIT ;
184
149
@@ -233,22 +198,20 @@ bool stm32l4_servo_configure(stm32l4_servo_t *servo, const stm32l4_servo_table_t
233
198
234
199
for (offset = 0 , entry = 0 , index = 0 ; entry < table -> entries ; entry ++ )
235
200
{
236
- if ((table -> slot [entry ].pin != GPIO_PIN_NONE ) && (table -> slot [index ].width >= SERVO_PULSE_THRESHOLD ))
201
+ if ((table -> slot [entry ].pin != GPIO_PIN_NONE ) && (table -> slot [index ].width >= SERVO_PULSE_WIDTH ))
237
202
{
238
- offset += table -> slot [entry ].width ;
239
-
240
203
pending -> slot [index ].GPIO = (GPIO_TypeDef * )(GPIOA_BASE + (GPIOB_BASE - GPIOA_BASE ) * ((table -> slot [entry ].pin & GPIO_PIN_GROUP_MASK ) >> GPIO_PIN_GROUP_SHIFT ));
241
204
pending -> slot [index ].mask = (1ul << ((table -> slot [entry ].pin & GPIO_PIN_INDEX_MASK ) >> GPIO_PIN_INDEX_SHIFT ));
242
- pending -> slot [index ].offset = offset ;
205
+ pending -> slot [index ].width = table -> slot [ entry ]. width ;
243
206
207
+ offset += table -> slot [entry ].width ;
244
208
index ++ ;
245
209
}
246
210
}
247
211
248
212
if (offset == 0 )
249
213
{
250
- pending -> period = 0 ;
251
- pending -> offset = 0 ;
214
+ pending -> sync = 0 ;
252
215
pending -> entries = 0 ;
253
216
}
254
217
else
@@ -257,15 +220,13 @@ bool stm32l4_servo_configure(stm32l4_servo_t *servo, const stm32l4_servo_table_t
257
220
258
221
servo -> prescaler = stm32l4_timer_clock (& servo -> timer ) / 1000000 ;
259
222
260
- if ((offset + SERVO_SYNC_MARGIN + SERVO_SYNC_WIDTH ) >= SERVO_FRAME_WIDTH )
223
+ if ((offset + SERVO_SYNC_WIDTH ) >= SERVO_FRAME_WIDTH )
261
224
{
262
- pending -> period = (offset + SERVO_SYNC_MARGIN + SERVO_SYNC_WIDTH );
263
- pending -> offset = SERVO_SYNC_MARGIN ;
225
+ pending -> sync = SERVO_SYNC_WIDTH ;
264
226
}
265
227
else
266
228
{
267
- pending -> period = SERVO_FRAME_WIDTH ;
268
- pending -> offset = SERVO_FRAME_WIDTH - SERVO_SYNC_WIDTH ;
229
+ pending -> sync = SERVO_FRAME_WIDTH - offset ;
269
230
}
270
231
}
271
232
@@ -275,20 +236,17 @@ bool stm32l4_servo_configure(stm32l4_servo_t *servo, const stm32l4_servo_table_t
275
236
}
276
237
else
277
238
{
278
- if (pending -> period )
239
+ if (pending -> entries )
279
240
{
280
- servo -> active = pending ;
281
-
282
- servo -> period = pending -> period ;
241
+ servo -> state = SERVO_STATE_ACTIVE ;
283
242
284
- stm32l4_timer_stop (& servo -> timer );
285
- stm32l4_timer_configure (& servo -> timer , servo -> prescaler - 1 , servo -> period - 1 , 0 );
286
- stm32l4_timer_channel (& servo -> timer , TIMER_CHANNEL_1 , pending -> slot [0 ].offset , TIMER_CONTROL_COMPARE_TIMING );
243
+ servo -> active = pending ;
244
+ servo -> index = 0 ;
245
+
246
+ stm32l4_timer_configure (& servo -> timer , servo -> prescaler - 1 , pending -> slot [0 ].width - 1 , 0 );
287
247
stm32l4_timer_start (& servo -> timer , false);
288
248
289
249
pending -> slot [0 ].GPIO -> BSRR = pending -> slot [0 ].mask ;
290
-
291
- servo -> state = SERVO_STATE_ACTIVE ;
292
250
}
293
251
}
294
252
0 commit comments