Skip to content

Commit af863a3

Browse files
authored
Merge pull request adafruit#1586 from sommersoft/shared_timer_handler
Move shared_timer_handler
2 parents d218069 + a3f3872 commit af863a3

7 files changed

Lines changed: 94 additions & 1 deletion

File tree

ports/atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ SRC_C = \
245245
reset.c \
246246
supervisor/shared/memory.c \
247247
tick.c \
248+
timer_handler.c \
248249

249250

250251
ifeq ($(CIRCUITPY_NETWORK),1)

ports/atmel-samd/common-hal/audioio/AudioOut.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#endif
4848

4949
#include "audio_dma.h"
50+
#include "timer_handler.h"
5051

5152
#include "samd/dma.h"
5253
#include "samd/events.h"
@@ -251,6 +252,7 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t* self,
251252
tc_gclk = 1;
252253
#endif
253254

255+
set_timer_handler(true, tc_index, TC_HANDLER_NO_INTERRUPT);
254256
turn_on_clocks(true, tc_index, tc_gclk);
255257

256258
// Don't bother setting the period. We set it before you playback anything.

ports/atmel-samd/common-hal/pulseio/PWMOut.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "common-hal/pulseio/PWMOut.h"
3232
#include "shared-bindings/pulseio/PWMOut.h"
3333
#include "shared-bindings/microcontroller/Processor.h"
34+
#include "timer_handler.h"
3435

3536
#include "atmel_start_pins.h"
3637
#include "hal/utils/include/utils_repeat_macro.h"
@@ -234,6 +235,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
234235
}
235236
}
236237

238+
set_timer_handler(timer->is_tc, timer->index, TC_HANDLER_NO_INTERRUPT);
237239
// We use the zeroeth clock on either port to go full speed.
238240
turn_on_clocks(timer->is_tc, timer->index, 0);
239241

ports/atmel-samd/common-hal/pulseio/PulseOut.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "py/runtime.h"
3838
#include "shared-bindings/pulseio/PulseOut.h"
3939
#include "supervisor/shared/translate.h"
40+
#include "timer_handler.h"
4041

4142
// This timer is shared amongst all PulseOut objects under the assumption that
4243
// the code is single threaded.
@@ -112,6 +113,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
112113

113114
pulseout_tc_index = index;
114115

116+
set_timer_handler(true, index, TC_HANDLER_PULSEOUT);
115117
// We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run at 48mhz making our
116118
// math the same across the boards.
117119
#ifdef SAMD21

ports/atmel-samd/timer_handler.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdbool.h>
28+
#include <stdint.h>
29+
30+
#include "timer_handler.h"
31+
32+
#include "common-hal/pulseio/PulseOut.h"
33+
34+
static uint8_t tc_handler[TC_INST_NUM];
35+
36+
void set_timer_handler(bool is_tc, uint8_t index, uint8_t timer_handler) {
37+
if (is_tc) {
38+
tc_handler[index] = timer_handler;
39+
}
40+
}
41+
42+
void shared_timer_handler(bool is_tc, uint8_t index) {
43+
// Add calls to interrupt handlers for specific functionality here.
44+
// Make sure to add the handler #define to timer_handler.h
45+
if (is_tc) {
46+
uint8_t handler = tc_handler[index];
47+
if (handler == TC_HANDLER_PULSEOUT) {
48+
pulseout_interrupt_handler(index);
49+
}
50+
}
51+
}

ports/atmel-samd/timer_handler.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_TIMER_HANDLER_H
27+
#define MICROPY_INCLUDED_ATMEL_SAMD_TIMER_HANDLER_H
28+
29+
#define TC_HANDLER_NO_INTERRUPT 0x0
30+
#define TC_HANDLER_PULSEOUT 0x1
31+
32+
void set_timer_handler(bool is_tc, uint8_t index, uint8_t timer_handler);
33+
void shared_timer_handler(bool is_tc, uint8_t index);
34+
35+
#endif // MICROPY_INCLUDED_ATMEL_SAMD_TIMER_HANDLER_H

0 commit comments

Comments
 (0)