Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
626f6b81
Commit
626f6b81
authored
11 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
stmhal: Add servo driver.
parent
d3116556
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
stmhal/Makefile
+1
-1
1 addition, 1 deletion
stmhal/Makefile
stmhal/main.c
+2
-2
2 additions, 2 deletions
stmhal/main.c
stmhal/pybmodule.c
+1
-3
1 addition, 3 deletions
stmhal/pybmodule.c
stmhal/servo.c
+142
-0
142 additions, 0 deletions
stmhal/servo.c
stmhal/servo.h
+5
-0
5 additions, 0 deletions
stmhal/servo.h
with
151 additions
and
6 deletions
stmhal/Makefile
+
1
−
1
View file @
626f6b81
...
@@ -89,8 +89,8 @@ SRC_C = \
...
@@ -89,8 +89,8 @@ SRC_C = \
diskio.c
\
diskio.c
\
lcd.c
\
lcd.c
\
accel.c
\
accel.c
\
servo.c
\
# servo.c \
# timer.c \
# timer.c \
# audio.c \
# audio.c \
# i2c.c \
# i2c.c \
...
...
This diff is collapsed.
Click to expand it.
stmhal/main.c
+
2
−
2
View file @
626f6b81
...
@@ -36,8 +36,8 @@
...
@@ -36,8 +36,8 @@
#include
"ff.h"
#include
"ff.h"
#include
"lcd.h"
#include
"lcd.h"
#include
"accel.h"
#include
"accel.h"
#if 0
#include
"servo.h"
#include
"servo.h"
#if 0
#include "timer.h"
#include "timer.h"
#include "pybwlan.h"
#include "pybwlan.h"
#include "pin.h"
#include "pin.h"
...
@@ -254,12 +254,12 @@ soft_reset:
...
@@ -254,12 +254,12 @@ soft_reset:
rng_init
();
rng_init
();
#endif
#endif
#if 0
#if MICROPY_HW_ENABLE_SERVO
#if MICROPY_HW_ENABLE_SERVO
// servo
// servo
servo_init
();
servo_init
();
#endif
#endif
#if 0
#if MICROPY_HW_ENABLE_TIMER
#if MICROPY_HW_ENABLE_TIMER
// timer
// timer
timer_init();
timer_init();
...
...
This diff is collapsed.
Click to expand it.
stmhal/pybmodule.c
+
1
−
3
View file @
626f6b81
...
@@ -23,8 +23,8 @@
...
@@ -23,8 +23,8 @@
#include
"storage.h"
#include
"storage.h"
#include
"sdcard.h"
#include
"sdcard.h"
#include
"accel.h"
#include
"accel.h"
#if 0
#include
"servo.h"
#include
"servo.h"
#if 0
#include "usb.h"
#include "usb.h"
#include "i2c.h"
#include "i2c.h"
#include "adc.h"
#include "adc.h"
...
@@ -231,13 +231,11 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
...
@@ -231,13 +231,11 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_rtc_info
),
(
mp_obj_t
)
&
pyb_rtc_info_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_rtc_info
),
(
mp_obj_t
)
&
pyb_rtc_info_obj
},
#endif
#endif
#if 0
#if MICROPY_HW_ENABLE_SERVO
#if MICROPY_HW_ENABLE_SERVO
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_pwm
),
(
mp_obj_t
)
&
pyb_pwm_set_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_pwm
),
(
mp_obj_t
)
&
pyb_pwm_set_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_servo
),
(
mp_obj_t
)
&
pyb_servo_set_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_servo
),
(
mp_obj_t
)
&
pyb_servo_set_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_Servo
),
(
mp_obj_t
)
&
pyb_Servo_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_Servo
),
(
mp_obj_t
)
&
pyb_Servo_obj
},
#endif
#endif
#endif
#if MICROPY_HW_HAS_SWITCH
#if MICROPY_HW_HAS_SWITCH
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_switch
),
(
mp_obj_t
)
&
pyb_switch_obj
},
{
MP_OBJ_NEW_QSTR
(
MP_QSTR_switch
),
(
mp_obj_t
)
&
pyb_switch_obj
},
...
...
This diff is collapsed.
Click to expand it.
stmhal/servo.c
0 → 100644
+
142
−
0
View file @
626f6b81
#include
<stdio.h>
#include
"stm32f4xx_hal.h"
#include
"misc.h"
#include
"mpconfig.h"
#include
"qstr.h"
#include
"obj.h"
#include
"servo.h"
// this servo driver uses hardware PWM to drive servos on PA0, PA1, PA2, PA3 = X1, X2, X3, X4
// TIM2 and TIM5 have CH1, CH2, CH3, CH4 on PA0-PA3 respectively
// they are both 32-bit counters with 16-bit prescaler
// we use TIM2
STATIC
TIM_HandleTypeDef
servo_TimHandle
;
void
servo_init
(
void
)
{
// TIM2 clock enable
__TIM2_CLK_ENABLE
();
// PWM clock configuration
servo_TimHandle
.
Instance
=
TIM2
;
servo_TimHandle
.
Init
.
Period
=
2000
;
// timer cycles at 50Hz
servo_TimHandle
.
Init
.
Prescaler
=
((
SystemCoreClock
/
2
)
/
100000
)
-
1
;
// timer runs at 100kHz
servo_TimHandle
.
Init
.
ClockDivision
=
0
;
servo_TimHandle
.
Init
.
CounterMode
=
TIM_COUNTERMODE_UP
;
HAL_TIM_PWM_Init
(
&
servo_TimHandle
);
}
STATIC
void
servo_init_channel
(
int
channel_in
)
{
uint32_t
pin
;
uint32_t
channel
;
switch
(
channel_in
)
{
case
1
:
pin
=
GPIO_PIN_0
;
channel
=
TIM_CHANNEL_1
;
break
;
case
2
:
pin
=
GPIO_PIN_1
;
channel
=
TIM_CHANNEL_2
;
break
;
case
3
:
pin
=
GPIO_PIN_2
;
channel
=
TIM_CHANNEL_3
;
break
;
case
4
:
pin
=
GPIO_PIN_3
;
channel
=
TIM_CHANNEL_4
;
break
;
default:
return
;
}
// GPIO configuration
GPIO_InitTypeDef
GPIO_InitStructure
;
GPIO_InitStructure
.
Pin
=
pin
;
GPIO_InitStructure
.
Mode
=
GPIO_MODE_AF_PP
;
GPIO_InitStructure
.
Speed
=
GPIO_SPEED_FAST
;
GPIO_InitStructure
.
Pull
=
GPIO_NOPULL
;
GPIO_InitStructure
.
Alternate
=
GPIO_AF1_TIM2
;
HAL_GPIO_Init
(
GPIOA
,
&
GPIO_InitStructure
);
// PWM mode configuration
TIM_OC_InitTypeDef
oc_init
;
oc_init
.
OCMode
=
TIM_OCMODE_PWM1
;
oc_init
.
Pulse
=
150
;
// units of 10us
oc_init
.
OCPolarity
=
TIM_OCPOLARITY_HIGH
;
oc_init
.
OCFastMode
=
TIM_OCFAST_DISABLE
;
HAL_TIM_PWM_ConfigChannel
(
&
servo_TimHandle
,
&
oc_init
,
channel
);
// start PWM
HAL_TIM_PWM_Start
(
&
servo_TimHandle
,
channel
);
}
/******************************************************************************/
// Micro Python bindings
STATIC
mp_obj_t
pyb_servo_set
(
mp_obj_t
port
,
mp_obj_t
value
)
{
int
p
=
mp_obj_get_int
(
port
);
int
v
=
mp_obj_get_int
(
value
);
if
(
v
<
50
)
{
v
=
50
;
}
if
(
v
>
250
)
{
v
=
250
;
}
switch
(
p
)
{
case
1
:
TIM2
->
CCR1
=
v
;
break
;
case
2
:
TIM2
->
CCR2
=
v
;
break
;
case
3
:
TIM2
->
CCR3
=
v
;
break
;
case
4
:
TIM2
->
CCR4
=
v
;
break
;
}
return
mp_const_none
;
}
MP_DEFINE_CONST_FUN_OBJ_2
(
pyb_servo_set_obj
,
pyb_servo_set
);
STATIC
mp_obj_t
pyb_pwm_set
(
mp_obj_t
period
,
mp_obj_t
pulse
)
{
int
pe
=
mp_obj_get_int
(
period
);
int
pu
=
mp_obj_get_int
(
pulse
);
TIM2
->
ARR
=
pe
;
TIM2
->
CCR3
=
pu
;
return
mp_const_none
;
}
MP_DEFINE_CONST_FUN_OBJ_2
(
pyb_pwm_set_obj
,
pyb_pwm_set
);
typedef
struct
_pyb_servo_obj_t
{
mp_obj_base_t
base
;
uint
servo_id
;
}
pyb_servo_obj_t
;
STATIC
void
servo_obj_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
pyb_servo_obj_t
*
self
=
self_in
;
print
(
env
,
"<Servo %lu>"
,
self
->
servo_id
);
}
STATIC
mp_obj_t
servo_obj_angle
(
mp_obj_t
self_in
,
mp_obj_t
angle
)
{
pyb_servo_obj_t
*
self
=
self_in
;
#if MICROPY_ENABLE_FLOAT
machine_int_t
v
=
152
+
85
.
0
*
mp_obj_get_float
(
angle
)
/
90
.
0
;
#else
machine_int_t
v
=
152
+
85
*
mp_obj_get_int
(
angle
)
/
90
;
#endif
if
(
v
<
65
)
{
v
=
65
;
}
if
(
v
>
210
)
{
v
=
210
;
}
switch
(
self
->
servo_id
)
{
case
1
:
TIM2
->
CCR1
=
v
;
break
;
case
2
:
TIM2
->
CCR2
=
v
;
break
;
case
3
:
TIM2
->
CCR3
=
v
;
break
;
case
4
:
TIM2
->
CCR4
=
v
;
break
;
}
return
mp_const_none
;
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_2
(
servo_obj_angle_obj
,
servo_obj_angle
);
STATIC
const
mp_method_t
servo_methods
[]
=
{
{
"angle"
,
&
servo_obj_angle_obj
},
{
NULL
,
NULL
},
};
STATIC
const
mp_obj_type_t
servo_obj_type
=
{
{
&
mp_type_type
},
.
name
=
MP_QSTR_Servo
,
.
print
=
servo_obj_print
,
.
methods
=
servo_methods
,
};
STATIC
mp_obj_t
pyb_Servo
(
mp_obj_t
servo_id
)
{
pyb_servo_obj_t
*
o
=
m_new_obj
(
pyb_servo_obj_t
);
o
->
base
.
type
=
&
servo_obj_type
;
o
->
servo_id
=
mp_obj_get_int
(
servo_id
);
servo_init_channel
(
o
->
servo_id
);
return
o
;
}
MP_DEFINE_CONST_FUN_OBJ_1
(
pyb_Servo_obj
,
pyb_Servo
);
This diff is collapsed.
Click to expand it.
stmhal/servo.h
0 → 100644
+
5
−
0
View file @
626f6b81
void
servo_init
(
void
);
MP_DECLARE_CONST_FUN_OBJ
(
pyb_servo_set_obj
);
MP_DECLARE_CONST_FUN_OBJ
(
pyb_pwm_set_obj
);
MP_DECLARE_CONST_FUN_OBJ
(
pyb_Servo_obj
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment