summaryrefslogtreecommitdiff
path: root/stmhal/timer.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-04-09 23:56:15 +0100
committerDamien George <damien.p.george@gmail.com>2015-04-16 14:30:16 +0000
commit7f9d1d6ab923096582622b700bedb6a571518eac (patch)
treef97a0d56ba0279dd4ef2a44f00676193c4d49d8b /stmhal/timer.c
parent56beb01724d4f0027babc5d23f016efbde4c4190 (diff)
py: Overhaul and simplify printf/pfenv mechanism.
Previous to this patch the printing mechanism was a bit of a tangled mess. This patch attempts to consolidate printing into one interface. All (non-debug) printing now uses the mp_print* family of functions, mainly mp_printf. All these functions take an mp_print_t structure as their first argument, and this structure defines the printing backend through the "print_strn" function of said structure. Printing from the uPy core can reach the platform-defined print code via two paths: either through mp_sys_stdout_obj (defined pert port) in conjunction with mp_stream_write; or through the mp_plat_print structure which uses the MP_PLAT_PRINT_STRN macro to define how string are printed on the platform. The former is only used when MICROPY_PY_IO is defined. With this new scheme printing is generally more efficient (less layers to go through, less arguments to pass), and, given an mp_print_t* structure, one can call mp_print_str for efficiency instead of mp_printf("%s", ...). Code size is also reduced by around 200 bytes on Thumb2 archs.
Diffstat (limited to 'stmhal/timer.c')
-rw-r--r--stmhal/timer.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/stmhal/timer.c b/stmhal/timer.c
index 606852659..0180cf442 100644
--- a/stmhal/timer.c
+++ b/stmhal/timer.c
@@ -35,7 +35,7 @@
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/gc.h"
-#include "py/pfenv.h"
+#include MICROPY_HAL_H
#include "timer.h"
#include "servo.h"
#include "pin.h"
@@ -468,17 +468,17 @@ STATIC void config_deadtime(pyb_timer_obj_t *self, mp_int_t ticks) {
HAL_TIMEx_ConfigBreakDeadTime(&self->tim, &deadTimeConfig);
}
-STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
+STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_timer_obj_t *self = self_in;
if (self->tim.State == HAL_TIM_STATE_RESET) {
- print(env, "Timer(%u)", self->tim_id);
+ mp_printf(print, "Timer(%u)", self->tim_id);
} else {
uint32_t prescaler = self->tim.Instance->PSC & 0xffff;
uint32_t period = __HAL_TIM_GetAutoreload(&self->tim) & TIMER_CNT_MASK(self);
// for efficiency, we compute and print freq as an int (not a float)
uint32_t freq = timer_get_source_freq(self->tim_id) / ((prescaler + 1) * (period + 1));
- print(env, "Timer(%u, freq=%u, prescaler=%u, period=%u, mode=%s, div=%u",
+ mp_printf(print, "Timer(%u, freq=%u, prescaler=%u, period=%u, mode=%s, div=%u",
self->tim_id,
freq,
prescaler,
@@ -488,9 +488,10 @@ STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void
self->tim.Init.ClockDivision == TIM_CLOCKDIVISION_DIV4 ? 4 :
self->tim.Init.ClockDivision == TIM_CLOCKDIVISION_DIV2 ? 2 : 1);
if (IS_TIM_ADVANCED_INSTANCE(self->tim.Instance)) {
- print(env, ", deadtime=%u", compute_ticks_from_dtg(self->tim.Instance->BDTR & TIM_BDTR_DTG));
+ mp_printf(print, ", deadtime=%u",
+ compute_ticks_from_dtg(self->tim.Instance->BDTR & TIM_BDTR_DTG));
}
- print(env, ")");
+ mp_print_str(print, ")");
}
}
@@ -1166,10 +1167,10 @@ const mp_obj_type_t pyb_timer_type = {
/// Timer channels are used to generate/capture a signal using a timer.
///
/// TimerChannel objects are created using the Timer.channel() method.
-STATIC void pyb_timer_channel_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
+STATIC void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_timer_channel_obj_t *self = self_in;
- print(env, "TimerChannel(timer=%u, channel=%u, mode=%s)",
+ mp_printf(print, "TimerChannel(timer=%u, channel=%u, mode=%s)",
self->timer->tim_id,
self->channel,
qstr_str(channel_mode_info[self->mode].name));
@@ -1308,7 +1309,7 @@ STATIC void timer_handle_irq_channel(pyb_timer_obj_t *tim, uint8_t channel, mp_o
} else {
printf("uncaught exception in Timer(%u) channel %u interrupt handler\n", tim->tim_id, channel);
}
- mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val);
+ mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
}
gc_unlock();
}