|
| 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 <stdint.h> |
| 28 | + |
| 29 | +#include "samd/timers.h" |
| 30 | + |
| 31 | +#include "shared_timers.h" |
| 32 | + |
| 33 | +static uint8_t never_reset_tc_or_tcc[TC_INST_NUM + TCC_INST_NUM]; |
| 34 | + |
| 35 | +static void timer_refcount(int index, bool is_tc, int increment) { |
| 36 | + if (is_tc) { |
| 37 | + never_reset_tc_or_tcc[index] += increment; |
| 38 | + } else { |
| 39 | + never_reset_tc_or_tcc[TC_INST_NUM + index] += increment; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +void timer_never_reset(int index, bool is_tc) { |
| 44 | + timer_refcount(index, is_tc, 1); |
| 45 | +} |
| 46 | + |
| 47 | +void timer_reset_ok(int index, bool is_tc) { |
| 48 | + timer_refcount(index, is_tc, -1); |
| 49 | +} |
| 50 | + |
| 51 | +bool timer_ok_to_reset(int index, bool is_tc) { |
| 52 | + if (is_tc) { |
| 53 | + return never_reset_tc_or_tcc[index] == 0; |
| 54 | + } |
| 55 | + return never_reset_tc_or_tcc[TC_INST_NUM + index] == 0; |
| 56 | +} |
| 57 | + |
| 58 | +void reset_timers(void) { |
| 59 | + // Reset all timers |
| 60 | + Tcc *tccs[TCC_INST_NUM] = TCC_INSTS; |
| 61 | + for (int i = 0; i < TCC_INST_NUM; i++) { |
| 62 | + if (!timer_ok_to_reset(i, false)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + // Disable the module before resetting it. |
| 66 | + if (tccs[i]->CTRLA.bit.ENABLE == 1) { |
| 67 | + tccs[i]->CTRLA.bit.ENABLE = 0; |
| 68 | + while (tccs[i]->SYNCBUSY.bit.ENABLE == 1) { |
| 69 | + } |
| 70 | + } |
| 71 | + tccs[i]->CTRLA.bit.SWRST = 1; |
| 72 | + while (tccs[i]->CTRLA.bit.SWRST == 1) { |
| 73 | + } |
| 74 | + } |
| 75 | + Tc *tcs[TC_INST_NUM] = TC_INSTS; |
| 76 | + for (int i = 0; i < TC_INST_NUM; i++) { |
| 77 | + if (!timer_ok_to_reset(i, true)) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + tcs[i]->COUNT16.CTRLA.bit.SWRST = 1; |
| 81 | + while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) { |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments