8000 Add pulse_width_percent to teensy. · comfuture/micropython@53d5fa6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53d5fa6

Browse files
committed
Add pulse_width_percent to teensy.
Fix stmhal and teensy print routines to report actual prescaler an period. Fix teensy build to use soft-float Add USE_ARDUINO_TOOLCHAIN option to teensy build
1 parent 2c180f7 commit 53d5fa6

File tree

6 files changed

+176
-69
lines changed

6 files changed

+176
-69
lines changed

stmhal/qstrdefsport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Q(BOTH)
175175
// for TimerChannel class
176176
Q(TimerChannel)
177177
Q(pulse_width)
178-
Q(pulse_width_ratio)
178+
Q(pulse_width_percent)
179179
Q(compare)
180180
Q(capture)
181181
Q(polarity)

stmhal/timer.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void
276276
} else {
277277
print(env, "Timer(%u, prescaler=%u, period=%u, mode=%s, div=%u)",
278278
self->tim_id,
279-
self->tim.Init.Prescaler,
280-
self->tim.Init.Period,
279+
self->tim.Instance->PSC & 0xffff,
280+
__HAL_TIM_GetAutoreload(&self->tim) & TIMER_CNT_MASK(self),
281281
self->tim.Init.CounterMode == TIM_COUNTERMODE_UP ? "UP" :
282282
self->tim.Init.CounterMode == TIM_COUNTERMODE_DOWN ? "DOWN" : "CENTER",
283283
self->tim.Init.ClockDivision == TIM_CLOCKDIVISION_DIV4 ? 4 :
@@ -543,7 +543,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
543543
/// Keyword arguments for Timer.PWM modes:
544544
///
545545
/// - `pulse_width` - determines the initial pulse width value to use.
546-
/// - `pulse_width_ratio` - determines the initial pulse width ratio to use.
546+
/// - `pulse_width_percent` - determines the initial pulse width percentage to use.
547547
///
548548
/// Keyword arguments for Timer.OC modes:
549549
///
@@ -566,12 +566,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
566566
/// ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=210000)
567567
/// ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=420000)
568568
STATIC const mp_arg_t pyb_timer_channel_args[] = {
569-
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
570-
{ MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
571-
{ MP_QSTR_pulse_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
572-
{ MP_QSTR_pulse_width_ratio, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
573-
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
574-
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
569+
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
570+
{ MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
571+
{ MP_QSTR_pulse_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
572+
{ MP_QSTR_pulse_width_percent, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
573+
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
574+
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
575575
};
576576
#define PYB_TIMER_CHANNEL_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_channel_args)
577577

@@ -667,9 +667,17 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
667667
// absolute pulse width value given
668668
oc_config.Pulse = vals[2].u_int;
669669
} else if (vals[3].u_obj != mp_const_none) {
670-
// pulse width ratio given
670+
// pulse width percent given
671671
uint32_t period = (__HAL_TIM_GetAutoreload(&self->tim) & TIMER_CNT_MASK(self)) + 1;
672-
uint32_t cmp = mp_obj_get_float(vals[3].u_obj) * period;
672+
uint32_t cmp;
673+
#if MICROPY_PY_BUILTINS_FLOAT
674+
if (MP_OBJ_IS_TYPE(vals[3].u_obj, &mp_type_float)) {
675+
cmp = mp_obj_get_float(vals[3].u_obj) * period / 100.0;
676+
} else
677+
#endif
678+
{
679+
cmp = mp_obj_get_int(vals[3].u_obj) * period / 100;
680+
}
673681
if (cmp < 0) {
674682
cmp = 0;
675683
} else if (cmp > period) {
@@ -891,9 +899,6 @@ STATIC void pyb_timer_channel_print(void (*print)(void *env, const char *fmt, ..
891899
/// pulse_width is the logical name to use when the channel is in PWM mode.
892900
STATIC mp_obj_t pyb_timer_channel_capture_compare(mp_uint_t n_args, const mp_obj_t *args) {
893901
pyb_timer_channel_obj_t *self = args[0];
894-
if (self->channel == 0) {
895-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d no channel specified", self->timer->tim_id));
896-
}
897902
if (n_args == 1) {
898903
// get
899904
return mp_obj_new_int(__HAL_TIM_GetCompare(&self->timer->tim, TIMER_CHANNEL(self)) & TIMER_CNT_MASK(self->timer));
@@ -910,19 +915,28 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_capture_compare_obj
910915
/// a floating-point number between 0.0 and 1.0, and is relative to the period
911916
/// of the timer associated with this channel. For example, a ratio of 0.5
912917
/// would be a 50% duty cycle.
913-
STATIC mp_obj_t pyb_timer_channel_pulse_width_ratio(mp_uint_t n_args, const mp_obj_t *args) {
918+
STATIC mp_obj_t pyb_timer_channel_pulse_width_percent(mp_uint_t n_args, const mp_obj_t *args) {
914919
pyb_timer_channel_obj_t *self = args[0];
915-
if (self->channel == 0) {
916-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d no channel specified", self->timer->tim_id));
917-
}
918920
uint32_t period = (__HAL_TIM_GetAutoreload(&self->timer->tim) & TIMER_CNT_MASK(self->timer)) + 1;
919921
if (n_args == 1) {
920922
// get
921923
uint32_t cmp = __HAL_TIM_GetCompare(&self->timer->tim, TIMER_CHANNEL(self)) & TIMER_CNT_MASK(self->timer);
922-
return mp_obj_new_float((float)cmp / (float)period);
924+
#if MICROPY_PY_BUILTINS_FLOAT
925+
return mp_obj_new_float((float)cmp * 100.0 / (float)period);
926+
#else
927+
return mp_obj_new_int(cmp * 100 / period);
928+
#endif
923929
} else {
924930
// set
925-
uint32_t cmp = mp_obj_get_float(args[1]) * period;
931+
uint32_t cmp;
932+
#if MICROPY_PY_BUILTINS_FLOAT
933+
if (MP_OBJ_IS_TYPE(args[1], &mp_type_float)) {
934+
cmp = mp_obj_get_float(args[1]) * period / 100.0;
935+
} else
936+
#endif
937+
{
938+
cmp = mp_obj_get_int(args[1]) * period / 100;
939+
}
926940
if (cmp < 0) {
927941
cmp = 0;
928942
} else if (cmp > period) {
@@ -932,7 +946,7 @@ STATIC mp_obj_t pyb_timer_channel_pulse_width_ratio(mp_uint_t n_args, const mp_o
932946
return mp_const_none;
933947
}
934948
}
935-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_pulse_width_ratio_obj, 1, 2, pyb_timer_channel_pulse_width_ratio);
949+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_pulse_width_percent_obj, 1, 2, pyb_timer_channel_pulse_width_percent);
936950

937951
/// \method callback(fun)
938952
/// Set the function to be called when the timer channel triggers.
@@ -976,7 +990,7 @@ STATIC const mp_map_elem_t pyb_timer_channel_locals_dict_table[] = {
976990
// instance methods
977991
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_timer_channel_callback_obj },
978992
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width), (mp_obj_t)&pyb_timer_channel_capture_compare_obj },
979-
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width_ratio), (mp_obj_t)&pyb_timer_channel_pulse_width_ratio_obj },
993+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width_percent), (mp_obj_t)&pyb_timer_channel_pulse_width_percent_obj },
980994
{ MP_OBJ_NEW_QSTR(MP_QSTR_capture), (mp_obj_t)&pyb_timer_channel_capture_compare_obj },
981995
{ MP_OBJ_NEW_QSTR(MP_QSTR_compare), (mp_obj_t)&pyb_timer_channel_capture_compare_obj },
982996
};

teensy/Makefile

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,28 @@ QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h
66
# include py core make definitions
77
include ../py/py.mk
88

9+
# If you set USE_ARDUINO_TOOLCHAIN=1 then this makefile will attempt to use
10+
# the toolchain that comes with Teensyduino
11+
ifeq ($(USE_ARDUINO_TOOLCHAIN),)
12+
USE_ARDUINO_TOOLCHAIN = 0
13+
endif
14+
15+
ifeq ($(USE_ARDUINO_TOOLCHAIN),1)
16+
ifeq ($(ARDUINO),)
17+
$(error USE_ARDUINO_TOOLCHAIN requires that ARDUINO be set)
18+
endif
19+
endif
20+
21+
ifeq ($(USE_ARDUINO_TOOLCHAIN),1)
22+
$(info Using ARDUINO toolchain)
23+
CROSS_COMPILE = $(ARDUINO)/hardware/tools/arm-none-eabi/bin/arm-none-eabi-
24+
else
25+
$(info Using toolchain from PATH)
926
CROSS_COMPILE = arm-none-eabi-
27+
endif
1028

1129
CFLAGS_TEENSY = -DF_CPU=96000000 -DUSB_SERIAL -D__MK20DX256__
12-
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -fsingle-precision-constant -Wdouble-promotion $(CFLAGS_TEENSY)
30+
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -mfloat-abi=soft -fsingle-precision-constant -Wdouble-promotion $(CFLAGS_TEENSY)
1331

1432
INC = -I.
1533
INC += -I$(PY_SRC)
@@ -18,11 +36,21 @@ INC += -I$(BUILD)
1836
INC += -Icore
1937

2038
CFLAGS = $(INC) -Wall -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4)
21-
LDFLAGS = -nostdlib -T mk20dx256.ld
39+
LDFLAGS = -nostdlib -T mk20dx256.ld -msoft-float -mfloat-abi=soft
40+
41+
ifeq ($(USE_ARDUINO_TOOLCHAIN),1)
2242

23-
LIBGCC_FILE_NAME = $(shell $(CC) -print-libgcc-file-name)
24-
LIBM_FILE_NAME = $(shell $(CC) -print-file-name=libm.a)
25-
LIBC_FILE_NAME = $(shell $(CC) -print-file-name=libc.a)
43+
LIBGCC_FILE_NAME = $(ARDUINO)/hardware/tools/arm-none-eabi/lib/gcc/arm-none-eabi/4.7.2/thumb2/libgcc.a
44+
LIBM_FILE_NAME = $(ARDUINO)/hardware/tools/arm-none-eabi/arm-none-eabi/lib/thumb2/libm.a
45+
LIBC_FILE_NAME = $(ARDUINO)/hardware/tools/arm-none-eabi/arm-none-eabi/lib/thumb2/libc.a
46+
47+
else
48+
49+
LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
50+
LIBM_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-file-name=libm.a)
51+
LIBC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-file-name=libc.a)
52+
53+
endif
2654

2755
#$(info %%%%% LIBGCC_FILE_NAME = $(LIBGCC_FILE_NAME))
2856
#$(info %%%%% LIBM_FILE_NAME = $(LIBM_FILE_NAME))

teensy/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Q(BOTH)
119119
// for TimerChannel class
120120
Q(TimerChannel)
121121
Q(pulse_width)
122+
Q(pulse_width_percent)
122123
Q(compare)
123124
Q(capture)
124125
Q(polarity)

0 commit comments

Comments
 (0)
0