timer.c 37.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>

#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include MICROPY_HAL_H
#include "gc.h"
#include "pin.h"
#include "reg.h"

#include "timer.h"

typedef enum {
    CHANNEL_MODE_PWM_NORMAL,
    CHANNEL_MODE_PWM_INVERTED,
    CHANNEL_MODE_OC_TIMING,
    CHANNEL_MODE_OC_ACTIVE,
    CHANNEL_MODE_OC_INACTIVE,
    CHANNEL_MODE_OC_TOGGLE,
//    CHANNEL_MODE_OC_FORCED_ACTIVE,
//    CHANNEL_MODE_OC_FORCED_INACTIVE,
    CHANNEL_MODE_IC,
} pyb_channel_mode;

STATIC const struct {
    qstr        name;
    uint32_t    oc_mode;
60
} channel_mode_info[] = {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    { MP_QSTR_PWM,                FTM_OCMODE_PWM1 },
    { MP_QSTR_PWM_INVERTED,       FTM_OCMODE_PWM2 },
    { MP_QSTR_OC_TIMING,          FTM_OCMODE_TIMING },
    { MP_QSTR_OC_ACTIVE,          FTM_OCMODE_ACTIVE },
    { MP_QSTR_OC_INACTIVE,        FTM_OCMODE_INACTIVE },
    { MP_QSTR_OC_TOGGLE,          FTM_OCMODE_TOGGLE },
//    { MP_QSTR_OC_FORCED_ACTIVE,   FTM_OCMODE_FORCED_ACTIVE },
//    { MP_QSTR_OC_FORCED_INACTIVE, FTM_OCMODE_FORCED_INACTIVE },
    { MP_QSTR_IC,                 0 },
};

struct _pyb_timer_obj_t;

typedef struct _pyb_timer_channel_obj_t {
    mp_obj_base_t base;
    struct _pyb_timer_obj_t *timer;
    uint8_t channel;
    uint8_t mode;
    mp_obj_t callback;
    struct _pyb_timer_channel_obj_t *next;
} pyb_timer_channel_obj_t;

typedef struct _pyb_timer_obj_t {
    mp_obj_base_t base;
    uint8_t tim_id;
    uint8_t irqn;
    mp_obj_t callback;
    FTM_HandleTypeDef ftm;
    pyb_timer_channel_obj_t *channel;
} pyb_timer_obj_t;

// Used to do callbacks to Python code on interrupt
STATIC pyb_timer_obj_t *pyb_timer_obj_all[3];
#define PYB_TIMER_OBJ_ALL_NUM MP_ARRAY_SIZE(pyb_timer_obj_all)

STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in);
STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback);
STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback);

void timer_init0(void) {
    for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) {
        pyb_timer_obj_all[i] = NULL;
    }
}

// unregister all interrupt sources
void timer_deinit(void) {
    for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) {
        pyb_timer_obj_t *tim = pyb_timer_obj_all[i];
        if (tim != NULL) {
            pyb_timer_deinit(tim);
        }
    }
}

mp_uint_t get_prescaler_shift(mp_int_t prescaler) {
    mp_uint_t prescaler_shift;
    for (prescaler_shift = 0; prescaler_shift < 8; prescaler_shift++) {
        if (prescaler == (1 << prescaler_shift)) {
            return prescaler_shift;
        }
    }
    nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "prescaler must be a power of 2 between 1 and 128, not %d", prescaler));
}

/******************************************************************************/
/* Micro Python bindings                                                      */

129
130
STATIC const mp_obj_type_t pyb_timer_channel_type;

Dave Hylands's avatar
Dave Hylands committed
131
132
133
134
135
136
137
138
139
140
141
142
143
// Helper function for determining the period used for calculating percent
STATIC uint32_t compute_period(pyb_timer_obj_t *self) {
    // In center mode,  compare == period corresponds to 100%
    // In edge mode, compare == (period + 1) corresponds to 100%
    FTM_TypeDef *FTMx = self->ftm.Instance;
    uint32_t period = (FTMx->MOD & 0xffff);
    if ((FTMx->SC & FTM_SC_CPWMS) == 0) {
        // Edge mode
        period++;
    }
    return period;
}

144
145
146
// Helper function to compute PWM value from timer period and percent value.
// 'val' can be an int or a float between 0 and 100 (out of range values are
// clamped).
Dave Hylands's avatar
Dave Hylands committed
147
STATIC uint32_t compute_pwm_value_from_percent(uint32_t period, mp_obj_t percent_in) {
148
149
150
    uint32_t cmp;
    if (0) {
    #if MICROPY_PY_BUILTINS_FLOAT
Dave Hylands's avatar
Dave Hylands committed
151
152
153
154
155
156
157
158
159
    } else if (MP_OBJ_IS_TYPE(percent_in, &mp_type_float)) {
        float percent = mp_obj_get_float(percent_in);
        if (percent <= 0.0) {
            cmp = 0;
        } else if (percent >= 100.0) {
            cmp = period;
        } else {
            cmp = percent / 100.0 * ((float)period);
        }
160
161
    #endif
    } else {
Dave Hylands's avatar
Dave Hylands committed
162
163
164
165
166
        mp_int_t percent = mp_obj_get_int(percent_in);
        if (percent <= 0) {
            cmp = 0;
        } else if (percent >= 100) {
            cmp = period;
167
        } else {
Dave Hylands's avatar
Dave Hylands committed
168
            cmp = ((uint32_t)percent * period) / 100;
169
170
171
172
173
        }
    }
    return cmp;
}

Dave Hylands's avatar
Dave Hylands committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Helper function to compute percentage from timer perion and PWM value.
STATIC mp_obj_t compute_percent_from_pwm_value(uint32_t period, uint32_t cmp) {
    #if MICROPY_PY_BUILTINS_FLOAT
    float percent = (float)cmp * 100.0 / (float)period;
    if (cmp > period) {
        percent = 100.0;
    } else {
        percent = (float)cmp * 100.0 / (float)period;
    }
    return mp_obj_new_float(percent);
    #else
    mp_int_t percent;
    if (cmp > period) {
        percent = 100;
    } else {
        percent = cmp * 100 / period;
    }
    return mp_obj_new_int(percent);
    #endif
}

195
196
197
198
199
200
201
202
STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
    pyb_timer_obj_t *self = self_in;

    if (self->ftm.State == HAL_FTM_STATE_RESET) {
        print(env, "Timer(%u)", self->tim_id);
    } else {
        print(env, "Timer(%u, prescaler=%u, period=%u, mode=%s)",
            self->tim_id,
203
204
            1 << (self->ftm.Instance->SC & 7),
            self->ftm.Instance->MOD & 0xffff,
Dave Hylands's avatar
Dave Hylands committed
205
            self->ftm.Init.CounterMode == FTM_COUNTERMODE_UP ? "UP" : "CENTER");
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    }
}

/// \method init(*, freq, prescaler, period)
/// Initialise the timer.  Initialisation must be either by frequency (in Hz)
/// or by prescaler and period:
///
///     tim.init(freq=100)                  # set the timer to trigger at 100Hz
///     tim.init(prescaler=83, period=999)  # set the prescaler and period directly
///
/// Keyword arguments:
///
///   - `freq` - specifies the periodic frequency of the timer. You migh also
///              view this as the frequency with which the timer goes through
///              one complete cycle.
///
///   - `prescaler` 1, 2, 4, 8 16 32, 64 or 128 - specifies the value to be loaded into the
///                 timer's prescaler. The timer clock source is divided by
///     (`prescaler`) to arrive at the timer clock.
///
///   - `period` [0-0xffff] - Specifies the value to be loaded into the timer's
///     Modulo Register (MOD). This determines the period of the timer (i.e.
///     when the counter cycles). The timer counter will roll-over after
Dave Hylands's avatar
Dave Hylands committed
229
230
///     `period` timer clock cycles. In center mode, a compare register > 0x7fff
///     doesn't seem to work properly, so keep this in mind.
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
///
///   - `mode` can be one of:
///     - `Timer.UP` - configures the timer to count from 0 to MOD (default)
///     - `Timer.CENTER` - confgures the timer to count from 0 to MOD and
///       then back down to 0.
///
///   - `callback` - as per Timer.callback()
///
///  You must either specify freq or both of period and prescaler.
STATIC const mp_arg_t pyb_timer_init_args[] = {
    { MP_QSTR_freq,             MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
    { MP_QSTR_prescaler,        MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
    { MP_QSTR_period,           MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
    { MP_QSTR_mode,             MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = FTM_COUNTERMODE_UP} },
    { MP_QSTR_callback,         MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
#define PYB_TIMER_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_init_args)

STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    // parse args
    mp_arg_val_t vals[PYB_TIMER_INIT_NUM_ARGS];
    mp_arg_parse_all(n_args, args, kw_args, PYB_TIMER_INIT_NUM_ARGS, pyb_timer_init_args, vals);

    FTM_HandleTypeDef *ftm = &self->ftm;

    // set the TIM configuration values
    FTM_Base_InitTypeDef *init = &ftm->Init;

    if (vals[0].u_int != 0xffffffff) {
        // set prescaler and period from frequency

        if (vals[0].u_int == 0) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't have 0 frequency"));
        }

        uint32_t period = MAX(1, F_BUS / vals[0].u_int);
        uint32_t prescaler_shift = 0;
Dave Hylands's avatar
Dave Hylands committed
268
        while (period > 0xffff && prescaler_shift < 7) {
269
270
271
            period >>= 1;
            prescaler_shift++;
        }
Dave Hylands's avatar
Dave Hylands committed
272
273
        if (period > 0xffff) {
            period = 0xffff;
274
275
        }
        init->PrescalerShift = prescaler_shift;
Dave Hylands's avatar
Dave Hylands committed
276
        init->Period = period;
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    } else if (vals[1].u_int != 0xffffffff && vals[2].u_int != 0xffffffff) {
        // set prescaler and period directly
        init->PrescalerShift = get_prescaler_shift(vals[1].u_int);
        init->Period = vals[2].u_int;
        if (!IS_FTM_PERIOD(init->Period)) {
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "period must be between 0 and 65535, not %d", init->Period));
        }
    } else {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "must specify either freq, or prescaler and period"));
    }

    init->CounterMode = vals[3].u_int;
    if (!IS_FTM_COUNTERMODE(init->CounterMode)) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "invalid counter mode: %d", init->CounterMode));
    }

    // Currently core/mk20dx128.c sets SIM_SCGC6_FTM0, SIM_SCGC6_FTM1, SIM_SCGC3_FTM2
    // so we don't need to do it here.

    NVIC_SET_PRIORITY(self->irqn, 0xe); // next-to lowest priority

    HAL_FTM_Base_Init(ftm);
    if (vals[4].u_obj == mp_const_none) {
        HAL_FTM_Base_Start(ftm);
    } else {
        pyb_timer_callback(self, vals[4].u_obj);
    }

    return mp_const_none;
}

/// \classmethod \constructor(id, ...)
/// Construct a new timer object of the given id.  If additional
/// arguments are given, then the timer is initialised by `init(...)`.
/// `id` can be 1 to 14, excluding 3.
312
STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    // check arguments
    mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);

    // create new Timer object
    pyb_timer_obj_t *tim = m_new_obj(pyb_timer_obj_t);
    memset(tim, 0, sizeof(*tim));

    tim->base.type = &pyb_timer_type;
    tim->callback = mp_const_none;
    tim->channel = NULL;

    // get FTM number
    tim->tim_id = mp_obj_get_int(args[0]);

    switch (tim->tim_id) {
        case 0: tim->ftm.Instance = FTM0; tim->irqn = IRQ_FTM0; break;
        case 1: tim->ftm.Instance = FTM1; tim->irqn = IRQ_FTM1; break;
        case 2: tim->ftm.Instance = FTM2; tim->irqn = IRQ_FTM2; break;
        default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d does not exist", tim->tim_id));
    }

    if (n_args > 1 || n_kw > 0) {
        // start the peripheral
        mp_map_t kw_args;
        mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
        pyb_timer_init_helper(tim, n_args - 1, args + 1, &kw_args);
    }

    // set the global variable for interrupt callbacks
    if (tim->tim_id < PYB_TIMER_OBJ_ALL_NUM) {
        pyb_timer_obj_all[tim->tim_id] = tim;
    }

    return (mp_obj_t)tim;
}

349
STATIC mp_obj_t pyb_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
350
351
352
353
354
355
356
357
    return pyb_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init);

/// \method deinit()
/// Deinitialises the timer.
///
/// Disables the callback (and the associated irq).
358
/// Disables any channel callbacks (and the associated irq).
359
360
361
362
/// Stops the timer, and disables the timer peripheral.
STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
    pyb_timer_obj_t *self = self_in;

363
    // Disable the base interrupt
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    pyb_timer_callback(self_in, mp_const_none);

    pyb_timer_channel_obj_t *chan = self->channel;
    self->channel = NULL;

    // Disable the channel interrupts
    while (chan != NULL) {
        pyb_timer_channel_callback(chan, mp_const_none);
        pyb_timer_channel_obj_t *prev_chan = chan;
        chan = chan->next;
        prev_chan->next = NULL;
    }

    HAL_FTM_Base_DeInit(&self->ftm);
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);

382
/// \method channel(channel, mode, ...)
383
///
384
385
/// If only a channel number is passed, then a previously initialized channel
/// object is returned (or `None` if there is no previous channel).
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
///
/// Othwerwise, a TimerChannel object is initialized and returned.
///
/// Each channel can be configured to perform pwm, output compare, or
/// input capture. All channels share the same underlying timer, which means
/// that they share the same timer clock.
///
/// Keyword arguments:
///
///   - `mode` can be one of:
///     - `Timer.PWM` - configure the timer in PWM mode (active high).
///     - `Timer.PWM_INVERTED` - configure the timer in PWM mode (active low).
///     - `Timer.OC_TIMING` - indicates that no pin is driven.
///     - `Timer.OC_ACTIVE` - the pin will be made active when a compare
///        match occurs (active is determined by polarity)
///     - `Timer.OC_INACTIVE` - the pin will be made inactive when a compare
///        match occurs.
///     - `Timer.OC_TOGGLE` - the pin will be toggled when an compare match occurs.
///     - `Timer.IC` - configure the timer in Input Capture mode.
///
///   - `callback` - as per TimerChannel.callback()
///
///   - `pin` None (the default) or a Pin object. If specified (and not None)
///           this will cause the alternate function of the the indicated pin
///      to be configured for this timer channel. An error will be raised if
///      the pin doesn't support any alternate functions for this timer channel.
///
/// Keyword arguments for Timer.PWM modes:
///
415
416
///   - `pulse_width` - determines the initial pulse width value to use.
///   - `pulse_width_percent` - determines the initial pulse width percentage to use.
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
///
/// Keyword arguments for Timer.OC modes:
///
///   - `compare` - determines the initial value of the compare register.
///
///   - `polarity` can be one of:
///     - `Timer.HIGH` - output is active high
///     - `Timer.LOW` - output is acive low
///
/// Optional keyword arguments for Timer.IC modes:
///
///   - `polarity` can be one of:
///     - `Timer.RISING` - captures on rising edge.
///     - `Timer.FALLING` - captures on falling edge.
///     - `Timer.BOTH` - captures on both edges.
///
/// PWM Example:
///
///     timer = pyb.Timer(0, prescaler=128, period=37500, counter_mode=pyb.Timer.COUNTER_MODE_CENTER)
///     ch0 = t0.channel(0, pyb.Timer.PWM, pin=pyb.Pin.board.D22, pulse_width=(t0.period() + 1) // 4)
///     ch1 = t0.channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.D23, pulse_width=(t0.period() + 1) // 2)
STATIC const mp_arg_t pyb_timer_channel_args[] = {
439
440
    { MP_QSTR_callback,            MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
    { MP_QSTR_pin,                 MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
441
    { MP_QSTR_pulse_width,         MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
442
443
444
    { MP_QSTR_pulse_width_percent, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
    { MP_QSTR_compare,             MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
    { MP_QSTR_polarity,            MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
};
#define PYB_TIMER_CHANNEL_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_channel_args)

STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    pyb_timer_obj_t *self = args[0];
    mp_int_t channel = mp_obj_get_int(args[1]);

    if (channel < 0 || channel > 7) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid channel (%d)", channel));
    }

    pyb_timer_channel_obj_t *chan = self->channel;
    pyb_timer_channel_obj_t *prev_chan = NULL;

    while (chan != NULL) {
        if (chan->channel == channel) {
            break;
        }
        prev_chan = chan;
        chan = chan->next;
    }
466
467
468
469

    // If only the channel number is given return the previously allocated
    // channel (or None if no previous channel).
    if (n_args == 2) {
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
        if (chan) {
            return chan;
        }
        return mp_const_none;
    }

    // If there was already a channel, then remove it from the list. Note that
    // the order we do things here is important so as to appear atomic to
    // the IRQ handler.
    if (chan) {
        // Turn off any IRQ associated with the channel.
        pyb_timer_channel_callback(chan, mp_const_none);

        // Unlink the channel from the list.
        if (prev_chan) {
            prev_chan->next = chan->next;
        }
        self->channel = chan->next;
        chan->next = NULL;
    }

    // Allocate and initialize a new channel
    mp_arg_val_t vals[PYB_TIMER_CHANNEL_NUM_ARGS];
    mp_arg_parse_all(n_args - 3, args + 3, kw_args, PYB_TIMER_CHANNEL_NUM_ARGS, pyb_timer_channel_args, vals);

    chan = m_new_obj(pyb_timer_channel_obj_t);
    memset(chan, 0, sizeof(*chan));
    chan->base.type = &pyb_timer_channel_type;
    chan->timer = self;
    chan->channel = channel;
    chan->mode = mp_obj_get_int(args[2]);
    chan->callback = vals[0].u_obj;

    mp_obj_t pin_obj = vals[1].u_obj;
    if (pin_obj != mp_const_none) {
        if (!MP_OBJ_IS_TYPE(pin_obj, &pin_type)) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "pin argument needs to be be a Pin type"));
        }
        const pin_obj_t *pin = pin_obj;
        const pin_af_obj_t *af = pin_find_af(pin, AF_FN_FTM, self->tim_id);
        if (af == NULL) {
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %s doesn't have an af for TIM%d", qstr_str(pin->name), self->tim_id));
        }
        // pin.init(mode=AF_PP, af=idx)
        const mp_obj_t args[6] = {
            (mp_obj_t)&pin_init_obj,
            pin_obj,
            MP_OBJ_NEW_QSTR(MP_QSTR_mode),  MP_OBJ_NEW_SMALL_INT(GPIO_MODE_AF_PP),
            MP_OBJ_NEW_QSTR(MP_QSTR_af),    MP_OBJ_NEW_SMALL_INT(af->idx)
        };
        mp_call_method_n_kw(0, 2, args);
    }

    // Link the channel to the timer before we turn the channel on.
    // Note that this needs to appear atomic to the IRQ handler (the write
    // to self->channel is atomic, so we're good, but I thought I'd mention
    // in case this was ever changed in the future).
    chan->next = self->channel;
    self->channel = chan;

    switch (chan->mode) {

        case CHANNEL_MODE_PWM_NORMAL:
        case CHANNEL_MODE_PWM_INVERTED: {
            FTM_OC_InitTypeDef oc_config;
535
            oc_config.OCMode = channel_mode_info[chan->mode].oc_mode;
536
            if (vals[3].u_obj != mp_const_none) {
537
                // pulse width ratio given
Dave Hylands's avatar
Dave Hylands committed
538
                uint32_t period = compute_period(self);
539
                oc_config.Pulse = compute_pwm_value_from_percent(period, vals[3].u_obj);
540
            } else {
541
542
                // use absolute pulse width value (defaults to 0 if nothing given)
                oc_config.Pulse = vals[2].u_int;
543
            }
Dave Hylands's avatar
Dave Hylands committed
544
            oc_config.OCPolarity = FTM_OCPOLARITY_HIGH;
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

            HAL_FTM_PWM_ConfigChannel(&self->ftm, &oc_config, channel);
            if (chan->callback == mp_const_none) {
                HAL_FTM_PWM_Start(&self->ftm, channel);
            } else {
                HAL_FTM_PWM_Start_IT(&self->ftm, channel);
            }
            break;
        }

        case CHANNEL_MODE_OC_TIMING:
        case CHANNEL_MODE_OC_ACTIVE:
        case CHANNEL_MODE_OC_INACTIVE:
        case CHANNEL_MODE_OC_TOGGLE: {
            FTM_OC_InitTypeDef oc_config;
560
561
562
            oc_config.OCMode       = channel_mode_info[chan->mode].oc_mode;
            oc_config.Pulse        = vals[4].u_int;
            oc_config.OCPolarity   = vals[5].u_int;
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
            if (oc_config.OCPolarity == 0xffffffff) {
                oc_config.OCPolarity = FTM_OCPOLARITY_HIGH;
            }

            if (!IS_FTM_OC_POLARITY(oc_config.OCPolarity)) {
                nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid polarity (%d)", oc_config.OCPolarity));
            }
            HAL_FTM_OC_ConfigChannel(&self->ftm, &oc_config, channel);
            if (chan->callback == mp_const_none) {
                HAL_FTM_OC_Start(&self->ftm, channel);
            } else {
                HAL_FTM_OC_Start_IT(&self->ftm, channel);
            }
            break;
        }

        case CHANNEL_MODE_IC: {
            FTM_IC_InitTypeDef ic_config;

582
            ic_config.ICPolarity  = vals[5].u_int;
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
            if (ic_config.ICPolarity == 0xffffffff) {
                ic_config.ICPolarity = FTM_ICPOLARITY_RISING;
            }

            if (!IS_FTM_IC_POLARITY(ic_config.ICPolarity)) {
                nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid polarity (%d)", ic_config.ICPolarity));
            }
            HAL_FTM_IC_ConfigChannel(&self->ftm, &ic_config, chan->channel);
            if (chan->callback == mp_const_none) {
                HAL_FTM_IC_Start(&self->ftm, channel);
            } else {
                HAL_FTM_IC_Start_IT(&self->ftm, channel);
            }
            break;
        }

        default:
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid mode (%d)", chan->mode));
    }
602

603
604
    return chan;
}
605
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_channel_obj, 2, pyb_timer_channel);
606
607
608

/// \method counter([value])
/// Get or set the timer counter.
609
STATIC mp_obj_t pyb_timer_counter(mp_uint_t n_args, const mp_obj_t *args) {
610
611
612
613
614
615
616
617
618
619
620
621
622
623
    pyb_timer_obj_t *self = args[0];
    if (n_args == 1) {
        // get
        return mp_obj_new_int(self->ftm.Instance->CNT);
    }
    // set - In order to write to CNT we need to set CNTIN
    self->ftm.Instance->CNTIN = mp_obj_get_int(args[1]);
    self->ftm.Instance->CNT = 0;    // write any value to load CNTIN into CNT
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_counter_obj, 1, 2, pyb_timer_counter);

/// \method prescaler([value])
/// Get or set the prescaler for the timer.
624
STATIC mp_obj_t pyb_timer_prescaler(mp_uint_t n_args, const mp_obj_t *args) {
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    pyb_timer_obj_t *self = args[0];
    if (n_args == 1) {
        // get
        return mp_obj_new_int(1 << (self->ftm.Instance->SC & 7));
    }

    // set
    mp_uint_t prescaler_shift = get_prescaler_shift(mp_obj_get_int(args[1]));

    mp_uint_t sc = self->ftm.Instance->SC;
    sc &= ~7;
    sc |= FTM_SC_PS(prescaler_shift);
    self->ftm.Instance->SC = sc;

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_prescaler_obj, 1, 2, pyb_timer_prescaler);

/// \method period([value])
/// Get or set the period of the timer.
645
STATIC mp_obj_t pyb_timer_period(mp_uint_t n_args, const mp_obj_t *args) {
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
    pyb_timer_obj_t *self = args[0];
    if (n_args == 1) {
        // get
        return mp_obj_new_int(self->ftm.Instance->MOD & 0xffff);
    }

    // set
    mp_int_t period = mp_obj_get_int(args[1]) & 0xffff;
    self->ftm.Instance->CNT = 0;
    self->ftm.Instance->MOD = period;
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_period_obj, 1, 2, pyb_timer_period);

/// \method callback(fun)
/// Set the function to be called when the timer triggers.
/// `fun` is passed 1 argument, the timer object.
/// If `fun` is `None` then the callback will be disabled.
STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback) {
    pyb_timer_obj_t *self = self_in;
    if (callback == mp_const_none) {
        // stop interrupt (but not timer)
        __HAL_FTM_DISABLE_TOF_IT(&self->ftm);
        self->callback = mp_const_none;
    } else if (mp_obj_is_callable(callback)) {
        self->callback = callback;
        HAL_NVIC_EnableIRQ(self->irqn);
        // start timer, so that it interrupts on overflow
        HAL_FTM_Base_Start_IT(&self->ftm);
    } else {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "callback must be None or a callable object"));
    }
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_timer_callback_obj, pyb_timer_callback);

#if MICROPY_TIMER_REG
reg_t timer_reg[] = {
    REG_ENTRY(FTM_TypeDef, SC),
    REG_ENTRY(FTM_TypeDef, CNT),
    REG_ENTRY(FTM_TypeDef, MOD),
    REG_ENTRY(FTM_TypeDef, CNTIN),
    REG_ENTRY(FTM_TypeDef, STATUS),
    REG_ENTRY(FTM_TypeDef, MODE),
    REG_ENTRY(FTM_TypeDef, SYNC),
    REG_ENTRY(FTM_TypeDef, OUTINIT),
    REG_ENTRY(FTM_TypeDef, OUTMASK),
    REG_ENTRY(FTM_TypeDef, COMBINE),
    REG_ENTRY(FTM_TypeDef, DEADTIME),
    REG_ENTRY(FTM_TypeDef, EXTTRIG),
    REG_ENTRY(FTM_TypeDef, POL),
    REG_ENTRY(FTM_TypeDef, FMS),
    REG_ENTRY(FTM_TypeDef, FILTER),
    REG_ENTRY(FTM_TypeDef, FLTCTRL),
    REG_ENTRY(FTM_TypeDef, QDCTRL),
    REG_ENTRY(FTM_TypeDef, CONF),
    REG_ENTRY(FTM_TypeDef, FLTPOL),
    REG_ENTRY(FTM_TypeDef, SYNCONF),
    REG_ENTRY(FTM_TypeDef, INVCTRL),
    REG_ENTRY(FTM_TypeDef, SWOCTRL),
    REG_ENTRY(FTM_TypeDef, PWMLOAD),
};

mp_obj_t pyb_timer_reg(uint n_args, const mp_obj_t *args) {
    pyb_timer_obj_t *self = args[0];
    return reg_cmd(self->ftm.Instance, timer_reg, MP_ARRAY_SIZE(timer_reg), n_args - 1, args + 1);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_reg_obj, 1, 3, pyb_timer_reg);
#endif // MICROPY_TIMER_REG

STATIC const mp_map_elem_t pyb_timer_locals_dict_table[] = {
    // instance methods
    { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_timer_init_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_timer_deinit_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_channel), (mp_obj_t)&pyb_timer_channel_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_counter), (mp_obj_t)&pyb_timer_counter_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_prescaler), (mp_obj_t)&pyb_timer_prescaler_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_period), (mp_obj_t)&pyb_timer_period_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_timer_callback_obj },
#if MICROPY_TIMER_REG
    { MP_OBJ_NEW_QSTR(MP_QSTR_reg), (mp_obj_t)&pyb_timer_reg_obj },
#endif
    { MP_OBJ_NEW_QSTR(MP_QSTR_UP),              MP_OBJ_NEW_SMALL_INT(FTM_COUNTERMODE_UP) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_CENTER),          MP_OBJ_NEW_SMALL_INT(FTM_COUNTERMODE_CENTER) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_PWM),             MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_PWM_NORMAL) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_PWM_INVERTED),    MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_PWM_INVERTED) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_OC_TIMING),       MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_OC_TIMING) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_OC_ACTIVE),       MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_OC_ACTIVE) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_OC_INACTIVE),     MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_OC_INACTIVE) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_OC_TOGGLE),       MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_OC_TOGGLE) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_IC),              MP_OBJ_NEW_SMALL_INT(CHANNEL_MODE_IC) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_HIGH),            MP_OBJ_NEW_SMALL_INT(FTM_OCPOLARITY_HIGH) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_LOW),             MP_OBJ_NEW_SMALL_INT(FTM_OCPOLARITY_LOW) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_RISING),          MP_OBJ_NEW_SMALL_INT(FTM_ICPOLARITY_RISING) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_FALLING),         MP_OBJ_NEW_SMALL_INT(FTM_ICPOLARITY_FALLING) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_BOTH),            MP_OBJ_NEW_SMALL_INT(FTM_ICPOLARITY_BOTH) },
};
STATIC MP_DEFINE_CONST_DICT(pyb_timer_locals_dict, pyb_timer_locals_dict_table);

const mp_obj_type_t pyb_timer_type = {
    { &mp_type_type },
    .name = MP_QSTR_Timer,
    .print = pyb_timer_print,
    .make_new = pyb_timer_make_new,
    .locals_dict = (mp_obj_t)&pyb_timer_locals_dict,
};

/// \moduleref pyb
/// \class TimerChannel - setup a channel for a timer.
///
/// 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) {
    pyb_timer_channel_obj_t *self = self_in;

762
    print(env, "TimerChannel(timer=%u, channel=%u, mode=%s)",
763
764
          self->timer->tim_id,
          self->channel,
765
          qstr_str(channel_mode_info[self->mode].name));
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
}

/// \method capture([value])
/// Get or set the capture value associated with a channel.
/// capture, compare, and pulse_width are all aliases for the same function.
/// capture is the logical name to use when the channel is in input capture mode.

/// \method compare([value])
/// Get or set the compare value associated with a channel.
/// capture, compare, and pulse_width are all aliases for the same function.
/// compare is the logical name to use when the channel is in output compare mode.

/// \method pulse_width([value])
/// Get or set the pulse width value associated with a channel.
/// capture, compare, and pulse_width are all aliases for the same function.
/// pulse_width is the logical name to use when the channel is in PWM mode.
Dave Hylands's avatar
Dave Hylands committed
782
783
784
/// 
/// In edge aligned mode, a pulse_width of `period + 1` corresponds to a duty cycle of 100%
/// In center aligned mode, a pulse width of `period` corresponds to a duty cycle of 100%
785
STATIC mp_obj_t pyb_timer_channel_capture_compare(mp_uint_t n_args, const mp_obj_t *args) {
786
787
788
789
    pyb_timer_channel_obj_t *self = args[0];
    FTM_TypeDef *FTMx = self->timer->ftm.Instance;
    if (n_args == 1) {
        // get
790
        return mp_obj_new_int(FTMx->channel[self->channel].CV & 0xffff);
791
792
793
794
795
    }

    mp_int_t pw = mp_obj_get_int(args[1]);

    // set
796
    FTMx->channel[self->channel].CV = pw & 0xffff;
797
798
799
800
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_capture_compare_obj, 1, 2, pyb_timer_channel_capture_compare);

801
/// \method pulse_width_percent([value])
802
803
804
805
806
/// Get or set the pulse width percentage associated with a channel.  The value
/// is a number between 0 and 100 and sets the percentage of the timer period
/// for which the pulse is active.  The value can be an integer or
/// floating-point number for more accuracy.  For example, a value of 25 gives
/// a duty cycle of 25%.
807
808
809
STATIC mp_obj_t pyb_timer_channel_pulse_width_percent(mp_uint_t n_args, const mp_obj_t *args) {
    pyb_timer_channel_obj_t *self = args[0];
    FTM_TypeDef *FTMx = self->timer->ftm.Instance;
Dave Hylands's avatar
Dave Hylands committed
810
    uint32_t period = compute_period(self->timer);
811
812
813
    if (n_args == 1) {
        // get
        uint32_t cmp = FTMx->channel[self->channel].CV & 0xffff;
Dave Hylands's avatar
Dave Hylands committed
814
        return compute_percent_from_pwm_value(period, cmp);
815
816
    } else {
        // set
817
        uint32_t cmp = compute_pwm_value_from_percent(period, args[1]);
818
819
820
821
822
823
        FTMx->channel[self->channel].CV = cmp & 0xffff;
        return mp_const_none;
    }
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_pulse_width_percent_obj, 1, 2, pyb_timer_channel_pulse_width_percent);

824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/// \method callback(fun)
/// Set the function to be called when the timer channel triggers.
/// `fun` is passed 1 argument, the timer object.
/// If `fun` is `None` then the callback will be disabled.
STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback) {
    pyb_timer_channel_obj_t *self = self_in;
    if (callback == mp_const_none) {
        // stop interrupt (but not timer)
        __HAL_FTM_DISABLE_CH_IT(&self->timer->ftm, self->channel);
        self->callback = mp_const_none;
    } else if (mp_obj_is_callable(callback)) {
        self->callback = callback;
        HAL_NVIC_EnableIRQ(self->timer->irqn);
        // start timer, so that it interrupts on overflow
        switch (self->mode) {
            case CHANNEL_MODE_PWM_NORMAL:
            case CHANNEL_MODE_PWM_INVERTED:
                HAL_FTM_PWM_Start_IT(&self->timer->ftm, self->channel);
                break;
            case CHANNEL_MODE_OC_TIMING:
            case CHANNEL_MODE_OC_ACTIVE:
            case CHANNEL_MODE_OC_INACTIVE:
            case CHANNEL_MODE_OC_TOGGLE:
                HAL_FTM_OC_Start_IT(&self->timer->ftm, self->channel);
                break;
            case CHANNEL_MODE_IC:
                HAL_FTM_IC_Start_IT(&self->timer->ftm, self->channel);
                break;
        }
    } else {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "callback must be None or a callable object"));
    }
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_timer_channel_callback_obj, pyb_timer_channel_callback);

#if MICROPY_TIMER_REG
reg_t timer_channel_reg[] = {
    REG_ENTRY(FTM_ChannelTypeDef, CSC),
    REG_ENTRY(FTM_ChannelTypeDef, CV),
};

mp_obj_t pyb_timer_channel_reg(uint n_args, const mp_obj_t *args) {
    pyb_timer_channel_obj_t *self = args[0];
    return reg_cmd(&self->timer->ftm.Instance->channel[self->channel],
                   timer_channel_reg, MP_ARRAY_SIZE(timer_channel_reg),
                   n_args - 1, args + 1);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_reg_obj, 1, 3, pyb_timer_channel_reg);
#endif

STATIC const mp_map_elem_t pyb_timer_channel_locals_dict_table[] = {
    // instance methods
    { MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_timer_channel_callback_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width), (mp_obj_t)&pyb_timer_channel_capture_compare_obj },
879
    { MP_OBJ_NEW_QSTR(MP_QSTR_pulse_width_percent), (mp_obj_t)&pyb_timer_channel_pulse_width_percent_obj },
880
881
882
883
884
885
886
887
    { MP_OBJ_NEW_QSTR(MP_QSTR_capture), (mp_obj_t)&pyb_timer_channel_capture_compare_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_compare), (mp_obj_t)&pyb_timer_channel_capture_compare_obj },
#if MICROPY_TIMER_REG
    { MP_OBJ_NEW_QSTR(MP_QSTR_reg), (mp_obj_t)&pyb_timer_channel_reg_obj },
#endif
};
STATIC MP_DEFINE_CONST_DICT(pyb_timer_channel_locals_dict, pyb_timer_channel_locals_dict_table);

888
STATIC const mp_obj_type_t pyb_timer_channel_type = {
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
    { &mp_type_type },
    .name = MP_QSTR_TimerChannel,
    .print = pyb_timer_channel_print,
    .locals_dict = (mp_obj_t)&pyb_timer_channel_locals_dict,
};

STATIC bool ftm_handle_irq_callback(pyb_timer_obj_t *self, mp_uint_t channel, mp_obj_t callback) {
    // execute callback if it's set
    if (callback == mp_const_none) {
        return false;
    }
    bool handled = false;

    // When executing code within a handler we must lock the GC to prevent
    // any memory allocations.  We must also catch any exceptions.
    gc_lock();
    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        mp_call_function_1(callback, self);
        nlr_pop();
        handled = true;
    } else {
        // Uncaught exception; disable the callback so it doesn't run again.
        self->callback = mp_const_none;
        if (channel == 0xffffffff) {
            printf("Uncaught exception in Timer(" UINT_FMT
                   ") interrupt handler\n", self->tim_id);
        } else {
            printf("Uncaught exception in Timer(" UINT_FMT ") channel "
                   UINT_FMT " interrupt handler\n", self->tim_id, channel);
        }
        mp_obj_print_exception((mp_obj_t)nlr.ret_val);
    }
    gc_unlock();
    return handled;
}

STATIC void ftm_irq_handler(uint tim_id) {
    if (tim_id >= PYB_TIMER_OBJ_ALL_NUM) {
        return;
    }
    // get the timer object
    pyb_timer_obj_t *self = pyb_timer_obj_all[tim_id];
    if (self == NULL) {
        // timer object has not been set, so we can't do anything
        printf("No timer object for id=%d\n", tim_id);
        return;
    }
    FTM_HandleTypeDef *hftm = &self->ftm;

    bool handled = false;

    // Check for timer (versus timer channel) interrupt.
    if (__HAL_FTM_GET_TOF_IT(hftm) && __HAL_FTM_GET_TOF_FLAG(hftm)) {
        __HAL_FTM_CLEAR_TOF_FLAG(hftm);
        if (ftm_handle_irq_callback(self, 0xffffffff, self->callback)) {
            handled = true;
        } else {
            __HAL_FTM_DISABLE_TOF_IT(&self->ftm);
            printf("No callback for Timer %d TOF (now disabled)\n", tim_id);
        }
    }

    uint32_t processed = 0;

    // Check to see if a timer channel interrupt is pending
    pyb_timer_channel_obj_t *chan = self->channel;
    while (chan != NULL) {
        processed |= (1 << chan->channel);
        if (__HAL_FTM_GET_CH_IT(&self->ftm, chan->channel) && __HAL_FTM_GET_CH_FLAG(&self->ftm, chan->channel)) {
            __HAL_FTM_CLEAR_CH_FLAG(&self->ftm, chan->channel);
            if (ftm_handle_irq_callback(self, chan->channel, chan->callback)) {
                handled = true;
            } else {
                __HAL_FTM_DISABLE_CH_IT(&self->ftm, chan->channel);
                printf("No callback for Timer %d channel %u (now disabled)\n",
                       self->tim_id, chan->channel);
            }
        }
        chan = chan->next;
    }

    if (!handled) {
        // An interrupt occurred for a channel we didn't process. Find it and
        // turn it off.
        for (mp_uint_t channel = 0; channel < 8; channel++) {
            if ((processed & (1 << channel)) == 0) {
                if (__HAL_FTM_GET_CH_FLAG(&self->ftm, channel) != 0) {
                    __HAL_FTM_CLEAR_CH_FLAG(&self->ftm, channel);
                    __HAL_FTM_DISABLE_CH_IT(&self->ftm, channel);
                    printf("Unhandled interrupt Timer %d channel %u (now disabled)\n",
                           tim_id, channel);
                }
            }
        }
    }
}

void ftm0_isr(void) {
    ftm_irq_handler(0);
}

void ftm1_isr(void) {
    ftm_irq_handler(1);
}

void ftm2_isr(void) {
    ftm_irq_handler(2);
}