11/*
2- * Copyright (c) 2016 Thomas Roell. All rights reserved.
2+ * Copyright (c) 2016-2017 Thomas Roell. All rights reserved.
33 *
44 * Permission is hereby granted, free of charge, to any person obtaining a copy
55 * 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)
4242 stm32l4_servo_schedule_t * active , * pending ;
4343 unsigned int index ;
4444
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 ;
5647
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 ;
6251
63- stm32l4_timer_compare (& servo -> timer , TIMER_CHANNEL_1 , active -> slot [0 ].offset );
64- }
65- else
66- {
67- servo -> period = active -> period ;
52+ index ++ ;
6853
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 ;
7557
76- servo -> state = SERVO_STATE_ACTIVE ;
77- }
58+ stm32l4_timer_period (& servo -> timer , active -> slot [index ].width - 1 , true);
7859 }
7960 else
8061 {
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);
8663 }
8764
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 ;
9466 }
9567 else
9668 {
97- index = servo -> index ;
98- active = servo -> active ;
69+ servo -> index = 0 ;
9970
100- if (index != active -> entries )
71+ pending = servo -> pending ;
72+
73+ if (pending == NULL )
10174 {
102- active -> slot [index ].GPIO -> BRR = active -> slot [index ].mask ;
103-
104- index ++ ;
75+ active -> slot [0 ].GPIO -> BSRR = active -> slot [0 ].mask ;
10576
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 )
10782 {
108- active -> slot [index ].GPIO -> BSRR = active -> slot [index ].mask ;
83+ pending -> slot [0 ].GPIO -> BSRR = pending -> slot [0 ].mask ;
10984
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 );
11186 }
11287 else
11388 {
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 ;
12592 }
12693
127- servo -> index = index ;
94+ servo -> pending = NULL ;
95+ servo -> active = pending ;
12896 }
129- else
97+
98+ if (servo -> events & SERVO_EVENT_SYNC )
13099 {
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 );
135101 }
136102 }
137103}
@@ -150,7 +116,6 @@ bool stm32l4_servo_create(stm32l4_servo_t *servo, unsigned int instance, unsigne
150116
151117 servo -> index = 0 ;
152118 servo -> prescaler = 0 ;
153- servo -> period = 0 ;
154119 servo -> active = NULL ;
155120 servo -> pending = NULL ;
156121
@@ -178,7 +143,7 @@ bool stm32l4_servo_enable(stm32l4_servo_t *servo, const stm32l4_servo_table_t *t
178143
179144 servo -> state = SERVO_STATE_BUSY ;
180145
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 ))
182147 {
183148 servo -> state = SERVO_STATE_INIT ;
184149
@@ -233,22 +198,20 @@ bool stm32l4_servo_configure(stm32l4_servo_t *servo, const stm32l4_servo_table_t
233198
234199 for (offset = 0 , entry = 0 , index = 0 ; entry < table -> entries ; entry ++ )
235200 {
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 ))
237202 {
238- offset += table -> slot [entry ].width ;
239-
240203 pending -> slot [index ].GPIO = (GPIO_TypeDef * )(GPIOA_BASE + (GPIOB_BASE - GPIOA_BASE ) * ((table -> slot [entry ].pin & GPIO_PIN_GROUP_MASK ) >> GPIO_PIN_GROUP_SHIFT ));
241204 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 ;
243206
207+ offset += table -> slot [entry ].width ;
244208 index ++ ;
245209 }
246210 }
247211
248212 if (offset == 0 )
249213 {
250- pending -> period = 0 ;
251- pending -> offset = 0 ;
214+ pending -> sync = 0 ;
252215 pending -> entries = 0 ;
253216 }
254217 else
@@ -257,15 +220,13 @@ bool stm32l4_servo_configure(stm32l4_servo_t *servo, const stm32l4_servo_table_t
257220
258221 servo -> prescaler = stm32l4_timer_clock (& servo -> timer ) / 1000000 ;
259222
260- if ((offset + SERVO_SYNC_MARGIN + SERVO_SYNC_WIDTH ) >= SERVO_FRAME_WIDTH )
223+ if ((offset + SERVO_SYNC_WIDTH ) >= SERVO_FRAME_WIDTH )
261224 {
262- pending -> period = (offset + SERVO_SYNC_MARGIN + SERVO_SYNC_WIDTH );
263- pending -> offset = SERVO_SYNC_MARGIN ;
225+ pending -> sync = SERVO_SYNC_WIDTH ;
264226 }
265227 else
266228 {
267- pending -> period = SERVO_FRAME_WIDTH ;
268- pending -> offset = SERVO_FRAME_WIDTH - SERVO_SYNC_WIDTH ;
229+ pending -> sync = SERVO_FRAME_WIDTH - offset ;
269230 }
270231 }
271232
@@ -275,20 +236,17 @@ bool stm32l4_servo_configure(stm32l4_servo_t *servo, const stm32l4_servo_table_t
275236 }
276237 else
277238 {
278- if (pending -> period )
239+ if (pending -> entries )
279240 {
280- servo -> active = pending ;
281-
282- servo -> period = pending -> period ;
241+ servo -> state = SERVO_STATE_ACTIVE ;
283242
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 );
287247 stm32l4_timer_start (& servo -> timer , false);
288248
289249 pending -> slot [0 ].GPIO -> BSRR = pending -> slot [0 ].mask ;
290-
291- servo -> state = SERVO_STATE_ACTIVE ;
292250 }
293251 }
294252
0 commit comments