Skip to content

Commit 0119fc7

Browse files
committed
stmhal: Servo driver can move at a given speed.
1 parent 626f6b8 commit 0119fc7

6 files changed

Lines changed: 147 additions & 51 deletions

File tree

stmhal/servo.c

Lines changed: 107 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "stm32f4xx_hal.h"
44

5+
#include "nlr.h"
56
#include "misc.h"
67
#include "mpconfig.h"
78
#include "qstr.h"
@@ -13,25 +14,85 @@
1314
// they are both 32-bit counters with 16-bit prescaler
1415
// we use TIM2
1516

16-
STATIC TIM_HandleTypeDef servo_TimHandle;
17+
#define PYB_SERVO_NUM (4)
18+
19+
typedef struct _pyb_servo_obj_t {
20+
mp_obj_base_t base;
21+
uint16_t servo_id;
22+
uint16_t time_left;
23+
int16_t pulse_accum;
24+
uint16_t pulse_cur;
25+
uint16_t pulse_dest;
26+
} pyb_servo_obj_t;
27+
28+
STATIC const mp_obj_type_t servo_obj_type;
29+
30+
STATIC pyb_servo_obj_t pyb_servo_obj[PYB_SERVO_NUM];
31+
32+
TIM_HandleTypeDef servo_TIM2_Handle;
1733

1834
void servo_init(void) {
1935
// TIM2 clock enable
2036
__TIM2_CLK_ENABLE();
2137

38+
// set up and enable interrupt
39+
HAL_NVIC_SetPriority(TIM2_IRQn, 6, 0);
40+
HAL_NVIC_EnableIRQ(TIM2_IRQn);
41+
2242
// PWM clock configuration
23-
servo_TimHandle.Instance = TIM2;
24-
servo_TimHandle.Init.Period = 2000; // timer cycles at 50Hz
25-
servo_TimHandle.Init.Prescaler = ((SystemCoreClock / 2) / 100000) - 1; // timer runs at 100kHz
26-
servo_TimHandle.Init.ClockDivision = 0;
27-
servo_TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
28-
HAL_TIM_PWM_Init(&servo_TimHandle);
43+
servo_TIM2_Handle.Instance = TIM2;
44+
servo_TIM2_Handle.Init.Period = 2000; // timer cycles at 50Hz
45+
servo_TIM2_Handle.Init.Prescaler = ((SystemCoreClock / 2) / 100000) - 1; // timer runs at 100kHz
46+
servo_TIM2_Handle.Init.ClockDivision = 0;
47+
servo_TIM2_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
48+
HAL_TIM_PWM_Init(&servo_TIM2_Handle);
49+
50+
// reset servo objects
51+
for (int i = 0; i < PYB_SERVO_NUM; i++) {
52+
pyb_servo_obj[i].base.type = &servo_obj_type;
53+
pyb_servo_obj[i].servo_id = i + 1;
54+
pyb_servo_obj[i].time_left = 0;
55+
pyb_servo_obj[i].pulse_cur = 150; // units of 10us
56+
pyb_servo_obj[i].pulse_dest = 0;
57+
}
58+
}
59+
60+
#include "led.h"
61+
void servo_timer_irq_callback(void) {
62+
led_toggle(1);
63+
bool need_it = false;
64+
for (int i = 0; i < PYB_SERVO_NUM; i++) {
65+
pyb_servo_obj_t *s = &pyb_servo_obj[i];
66+
if (s->pulse_cur != s->pulse_dest) {
67+
if (s->time_left <= 1) {
68+
s->pulse_cur = s->pulse_dest;
69+
s->time_left = 0;
70+
} else {
71+
s->pulse_accum += s->pulse_dest - s->pulse_cur;
72+
s->pulse_cur += s->pulse_accum / s->time_left;
73+
s->pulse_accum %= s->time_left;
74+
s->time_left--;
75+
need_it = true;
76+
}
77+
switch (s->servo_id) {
78+
case 1: TIM2->CCR1 = s->pulse_cur; break;
79+
case 2: TIM2->CCR2 = s->pulse_cur; break;
80+
case 3: TIM2->CCR3 = s->pulse_cur; break;
81+
case 4: TIM2->CCR4 = s->pulse_cur; break;
82+
}
83+
}
84+
}
85+
if (need_it) {
86+
__HAL_TIM_ENABLE_IT(&servo_TIM2_Handle, TIM_IT_UPDATE);
87+
} else {
88+
__HAL_TIM_DISABLE_IT(&servo_TIM2_Handle, TIM_IT_UPDATE);
89+
}
2990
}
3091

31-
STATIC void servo_init_channel(int channel_in) {
92+
STATIC void servo_init_channel(pyb_servo_obj_t *s) {
3293
uint32_t pin;
3394
uint32_t channel;
34-
switch (channel_in) {
95+
switch (s->servo_id) {
3596
case 1: pin = GPIO_PIN_0; channel = TIM_CHANNEL_1; break;
3697
case 2: pin = GPIO_PIN_1; channel = TIM_CHANNEL_2; break;
3798
case 3: pin = GPIO_PIN_2; channel = TIM_CHANNEL_3; break;
@@ -51,13 +112,13 @@ STATIC void servo_init_channel(int channel_in) {
51112
// PWM mode configuration
52113
TIM_OC_InitTypeDef oc_init;
53114
oc_init.OCMode = TIM_OCMODE_PWM1;
54-
oc_init.Pulse = 150; // units of 10us
115+
oc_init.Pulse = s->pulse_cur; // units of 10us
55116
oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
56117
oc_init.OCFastMode = TIM_OCFAST_DISABLE;
57-
HAL_TIM_PWM_ConfigChannel(&servo_TimHandle, &oc_init, channel);
118+
HAL_TIM_PWM_ConfigChannel(&servo_TIM2_Handle, &oc_init, channel);
58119

59120
// start PWM
60-
HAL_TIM_PWM_Start(&servo_TimHandle, channel);
121+
HAL_TIM_PWM_Start(&servo_TIM2_Handle, channel);
61122
}
62123

63124
/******************************************************************************/
@@ -89,35 +150,39 @@ STATIC mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) {
89150

90151
MP_DEFINE_CONST_FUN_OBJ_2(pyb_pwm_set_obj, pyb_pwm_set);
91152

92-
typedef struct _pyb_servo_obj_t {
93-
mp_obj_base_t base;
94-
uint servo_id;
95-
} pyb_servo_obj_t;
96-
97153
STATIC void servo_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
98154
pyb_servo_obj_t *self = self_in;
99-
print(env, "<Servo %lu>", self->servo_id);
155+
print(env, "<Servo %lu at %lu>", self->servo_id, self->pulse_cur);
100156
}
101157

102-
STATIC mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) {
103-
pyb_servo_obj_t *self = self_in;
158+
STATIC mp_obj_t servo_obj_angle(uint n_args, const mp_obj_t *args) {
159+
pyb_servo_obj_t *self = args[0];
160+
if (n_args == 1) {
161+
// get angle
162+
return mp_obj_new_int((self->pulse_cur - 152) * 90 / 85);
163+
} else {
104164
#if MICROPY_ENABLE_FLOAT
105-
machine_int_t v = 152 + 85.0 * mp_obj_get_float(angle) / 90.0;
165+
machine_int_t v = 152 + 85.0 * mp_obj_get_float(args[1]) / 90.0;
106166
#else
107-
machine_int_t v = 152 + 85 * mp_obj_get_int(angle) / 90;
167+
machine_int_t v = 152 + 85 * mp_obj_get_int(args[1]) / 90;
108168
#endif
109-
if (v < 65) { v = 65; }
110-
if (v > 210) { v = 210; }
111-
switch (self->servo_id) {
112-
case 1: TIM2->CCR1 = v; break;
113-
case 2: TIM2->CCR2 = v; break;
114-
case 3: TIM2->CCR3 = v; break;
115-
case 4: TIM2->CCR4 = v; break;
169+
if (v < 65) { v = 65; }
170+
if (v > 210) { v = 210; }
171+
self->pulse_dest = v;
172+
if (n_args == 2) {
173+
// set angle immediately
174+
self->time_left = 0;
175+
} else {
176+
// set angle over a given time (given in milli seconds)
177+
self->time_left = mp_obj_get_int(args[2]) / 20;
178+
self->pulse_accum = 0;
179+
}
180+
servo_timer_irq_callback();
181+
return mp_const_none;
116182
}
117-
return mp_const_none;
118183
}
119184

120-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle);
185+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(servo_obj_angle_obj, 1, 3, servo_obj_angle);
121186

122187
STATIC const mp_method_t servo_methods[] = {
123188
{ "angle", &servo_obj_angle_obj },
@@ -131,12 +196,17 @@ STATIC const mp_obj_type_t servo_obj_type = {
131196
.methods = servo_methods,
132197
};
133198

134-
STATIC mp_obj_t pyb_Servo(mp_obj_t servo_id) {
135-
pyb_servo_obj_t *o = m_new_obj(pyb_servo_obj_t);
136-
o->base.type = &servo_obj_type;
137-
o->servo_id = mp_obj_get_int(servo_id);
138-
servo_init_channel(o->servo_id);
139-
return o;
199+
STATIC mp_obj_t pyb_Servo(mp_obj_t servo_id_o) {
200+
machine_int_t servo_id = mp_obj_get_int(servo_id_o) - 1;
201+
if (0 <= servo_id && servo_id < PYB_SERVO_NUM) {
202+
pyb_servo_obj_t *s = &pyb_servo_obj[servo_id];
203+
s->pulse_dest = s->pulse_cur;
204+
s->time_left = 0;
205+
servo_init_channel(s);
206+
return s;
207+
} else {
208+
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Servo %d does not exist", servo_id));
209+
}
140210
}
141211

142212
MP_DEFINE_CONST_FUN_OBJ_1(pyb_Servo_obj, pyb_Servo);

stmhal/servo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
extern TIM_HandleTypeDef servo_TIM2_Handle;
2+
13
void servo_init(void);
4+
void servo_timer_irq_callback(void);
25

36
MP_DECLARE_CONST_FUN_OBJ(pyb_servo_set_obj);
47
MP_DECLARE_CONST_FUN_OBJ(pyb_pwm_set_obj);

stmhal/stm32f4xx_hal_msp.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
#include "usbd_cdc_msc.h"
5151
#include "usbd_cdc_interface.h"
5252

53+
#include "misc.h"
54+
#include "mpconfig.h"
55+
#include "qstr.h"
56+
#include "obj.h"
57+
#include "servo.h"
58+
5359
/** @addtogroup STM32F4xx_HAL_Driver
5460
* @{
5561
*/
@@ -155,6 +161,14 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
155161
__HAL_RCC_RTC_DISABLE();
156162
}
157163

164+
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
165+
if (htim == &USBD_CDC_TIM3_Handle) {
166+
USBD_CDC_HAL_TIM_PeriodElapsedCallback();
167+
} else if (htim == &servo_TIM2_Handle) {
168+
servo_timer_irq_callback();
169+
}
170+
}
171+
158172
/**
159173
* @}
160174
*/

stmhal/stm32f4xx_it.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@
4242

4343
#include "stm32f4xx_it.h"
4444
#include "stm32f4xx_hal.h"
45+
#include "usbd_cdc_msc.h"
46+
#include "usbd_cdc_interface.h"
4547

4648
#include "misc.h"
4749
#include "mpconfig.h"
4850
#include "qstr.h"
4951
#include "obj.h"
5052
#include "exti.h"
53+
#include "servo.h"
5154

5255
/** @addtogroup STM32F4xx_HAL_Examples
5356
* @{
@@ -64,7 +67,6 @@
6467

6568
extern void fatality();
6669
extern PCD_HandleTypeDef hpcd;
67-
extern TIM_HandleTypeDef USBD_CDC_TimHandle;
6870

6971
/* Private function prototypes -----------------------------------------------*/
7072
/* Private functions ---------------------------------------------------------*/
@@ -349,9 +351,14 @@ void RTC_WKUP_IRQHandler(void) {
349351
Handle_EXTI_Irq(EXTI_RTC_WAKEUP);
350352
}
351353

354+
void TIM2_IRQHandler(void) {
355+
// servo timer is TIM2
356+
HAL_TIM_IRQHandler(&servo_TIM2_Handle);
357+
}
358+
352359
void TIM3_IRQHandler(void) {
353360
// USBD CDC timer is TIM3
354-
HAL_TIM_IRQHandler(&USBD_CDC_TimHandle);
361+
HAL_TIM_IRQHandler(&USBD_CDC_TIM3_Handle);
355362
}
356363

357364
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

stmhal/usbd_cdc_interface.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int user_interrupt_char = VCP_CHAR_NONE;
6666
static void *user_interrupt_data = NULL;
6767

6868
/* TIM handler declaration */
69-
TIM_HandleTypeDef USBD_CDC_TimHandle;
69+
TIM_HandleTypeDef USBD_CDC_TIM3_Handle;
7070
/* USB handler declaration */
7171
extern USBD_HandleTypeDef hUSBDDevice;
7272

@@ -132,7 +132,7 @@ static int8_t CDC_Itf_Init(void)
132132

133133
/*##-4- Start the TIM Base generation in interrupt mode ####################*/
134134
/* Start Channel1 */
135-
if(HAL_TIM_Base_Start_IT(&USBD_CDC_TimHandle) != HAL_OK)
135+
if(HAL_TIM_Base_Start_IT(&USBD_CDC_TIM3_Handle) != HAL_OK)
136136
{
137137
/* Starting Error */
138138
}
@@ -250,8 +250,7 @@ static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
250250
* @param htim: TIM handle
251251
* @retval None
252252
*/
253-
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
254-
{
253+
void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
255254
uint32_t buffptr;
256255
uint32_t buffsize;
257256

@@ -388,19 +387,19 @@ int USBD_CDC_RxGet(void) {
388387
static void TIM_Config(void)
389388
{
390389
/* Set TIMx instance */
391-
USBD_CDC_TimHandle.Instance = USBD_CDC_TIMx;
390+
USBD_CDC_TIM3_Handle.Instance = USBD_CDC_TIMx;
392391

393392
/* Initialize TIM3 peripheral as follow:
394393
+ Period = 10000 - 1
395394
+ Prescaler = ((SystemCoreClock/2)/10000) - 1
396395
+ ClockDivision = 0
397396
+ Counter direction = Up
398397
*/
399-
USBD_CDC_TimHandle.Init.Period = (USBD_CDC_POLLING_INTERVAL*1000) - 1;
400-
USBD_CDC_TimHandle.Init.Prescaler = 84-1;
401-
USBD_CDC_TimHandle.Init.ClockDivision = 0;
402-
USBD_CDC_TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
403-
if(HAL_TIM_Base_Init(&USBD_CDC_TimHandle) != HAL_OK)
398+
USBD_CDC_TIM3_Handle.Init.Period = (USBD_CDC_POLLING_INTERVAL*1000) - 1;
399+
USBD_CDC_TIM3_Handle.Init.Prescaler = 84-1;
400+
USBD_CDC_TIM3_Handle.Init.ClockDivision = 0;
401+
USBD_CDC_TIM3_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
402+
if(HAL_TIM_Base_Init(&USBD_CDC_TIM3_Handle) != HAL_OK)
404403
{
405404
/* Initialization Error */
406405
}

stmhal/usbd_cdc_interface.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
/* Definition for TIMx clock resources */
3737
#define USBD_CDC_TIMx TIM3
3838
#define USBD_CDC_TIMx_CLK_ENABLE __TIM3_CLK_ENABLE
39-
#define USBD_CDC_TIMx_FORCE_RESET() __USART3_FORCE_RESET()
40-
#define USBD_CDC_TIMx_RELEASE_RESET() __USART3_RELEASE_RESET()
39+
#define USBD_CDC_TIMx_FORCE_RESET() __TIM3_FORCE_RESET()
40+
#define USBD_CDC_TIMx_RELEASE_RESET() __TIM3_RELEASE_RESET()
4141

4242
/* Definition for TIMx's NVIC */
4343
#define USBD_CDC_TIMx_IRQn TIM3_IRQn
@@ -47,8 +47,11 @@
4747
The period depends on USBD_CDC_POLLING_INTERVAL */
4848
#define USBD_CDC_POLLING_INTERVAL 10 /* in ms. The max is 65 and the min is 1 */
4949

50+
extern TIM_HandleTypeDef USBD_CDC_TIM3_Handle;
5051
extern const USBD_CDC_ItfTypeDef USBD_CDC_fops;
5152

53+
void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void);
54+
5255
int USBD_CDC_IsConnected(void);
5356
void USBD_CDC_SetInterrupt(int chr, void *data);
5457
void USBD_CDC_Tx(const char *str, uint32_t len);

0 commit comments

Comments
 (0)