|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Damien P. George |
| 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 "common-hal/nativeio/PulseOut.h" |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | +#include "asf/sam0/drivers/tc/tc_interrupt.h" |
| 32 | + |
| 33 | +#include "mpconfigport.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "shared-bindings/nativeio/PulseOut.h" |
| 36 | + |
| 37 | +#undef ENABLE |
| 38 | + |
| 39 | +// This timer is shared amongst all PulseOut objects under the assumption that |
| 40 | +// the code is single threaded. |
| 41 | +static struct tc_module tc_instance; |
| 42 | +static uint8_t refcount = 0; |
| 43 | + |
| 44 | +static __IO PORT_PINCFG_Type *active_pincfg = NULL; |
| 45 | +static uint16_t *pulse_buffer = NULL; |
| 46 | +static volatile uint16_t pulse_index = 0; |
| 47 | +static uint16_t pulse_length; |
| 48 | +static volatile uint32_t current_compare = 0; |
| 49 | + |
| 50 | +static void turn_on(__IO PORT_PINCFG_Type * pincfg) { |
| 51 | + pincfg->reg = PORT_PINCFG_PMUXEN; |
| 52 | +} |
| 53 | + |
| 54 | +static void turn_off(__IO PORT_PINCFG_Type * pincfg) { |
| 55 | + pincfg->reg = PORT_PINCFG_RESETVALUE; |
| 56 | +} |
| 57 | + |
| 58 | +void pulse_finish(struct tc_module *const module) { |
| 59 | + pulse_index++; |
| 60 | + |
| 61 | + if (active_pincfg == NULL) { |
| 62 | + return; |
| 63 | + } |
| 64 | + // Always turn it off. |
| 65 | + turn_off(active_pincfg); |
| 66 | + if (pulse_index >= pulse_length) { |
| 67 | + return; |
| 68 | + } |
| 69 | + current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff; |
| 70 | + tc_set_compare_value(&tc_instance, TC_COMPARE_CAPTURE_CHANNEL_0, current_compare); |
| 71 | + if (pulse_index % 2 == 0) { |
| 72 | + turn_on(active_pincfg); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void pulseout_reset() { |
| 77 | + refcount = 0; |
| 78 | + active_pincfg = NULL; |
| 79 | +} |
| 80 | + |
| 81 | +void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, |
| 82 | + const nativeio_pwmout_obj_t* carrier) { |
| 83 | + if (refcount == 0) { |
| 84 | + // Find a spare timer. |
| 85 | + Tc *t = NULL; |
| 86 | + Tc *tcs[TC_INST_NUM] = TC_INSTS; |
| 87 | + for (uint8_t i = TC_INST_NUM; i > 0; i--) { |
| 88 | + if (tcs[i - 1]->COUNT16.CTRLA.bit.ENABLE == 0) { |
| 89 | + t = tcs[i - 1]; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + if (t == NULL) { |
| 94 | + mp_raise_RuntimeError("All timers in use"); |
| 95 | + } |
| 96 | + |
| 97 | + struct tc_config config_tc; |
| 98 | + tc_get_config_defaults(&config_tc); |
| 99 | + |
| 100 | + config_tc.counter_size = TC_COUNTER_SIZE_16BIT; |
| 101 | + config_tc.clock_prescaler = TC_CTRLA_PRESCALER_DIV64; |
| 102 | + config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ; |
| 103 | + |
| 104 | + tc_init(&tc_instance, t, &config_tc); |
| 105 | + tc_register_callback(&tc_instance, pulse_finish, TC_CALLBACK_CC_CHANNEL0); |
| 106 | + tc_enable(&tc_instance); |
| 107 | + tc_stop_counter(&tc_instance); |
| 108 | + } |
| 109 | + refcount++; |
| 110 | + |
| 111 | + self->pin = carrier->pin->pin; |
| 112 | + |
| 113 | + PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin); |
| 114 | + self->pincfg = &port_base->PINCFG[self->pin % 32]; |
| 115 | + |
| 116 | + // Set the port to output a zero. |
| 117 | + port_base->OUTCLR.reg = 1 << (self->pin % 32); |
| 118 | + port_base->DIRSET.reg = 1 << (self->pin % 32); |
| 119 | + |
| 120 | + // Turn off the pinmux which should connect the port output. |
| 121 | + turn_off(self->pincfg); |
| 122 | +} |
| 123 | + |
| 124 | +void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) { |
| 125 | + PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin); |
| 126 | + port_base->DIRCLR.reg = 1 << (self->pin % 32); |
| 127 | + |
| 128 | + turn_on(self->pincfg); |
| 129 | + |
| 130 | + refcount--; |
| 131 | + if (refcount == 0) { |
| 132 | + tc_reset(&tc_instance); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { |
| 137 | + if (active_pincfg != NULL) { |
| 138 | + mp_raise_RuntimeError("Another send is already active"); |
| 139 | + } |
| 140 | + active_pincfg = self->pincfg; |
| 141 | + pulse_buffer = pulses; |
| 142 | + pulse_index = 0; |
| 143 | + pulse_length = length; |
| 144 | + |
| 145 | + current_compare = pulses[0] * 3 / 4; |
| 146 | + tc_set_compare_value(&tc_instance, TC_COMPARE_CAPTURE_CHANNEL_0, current_compare); |
| 147 | + |
| 148 | + tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0); |
| 149 | + turn_on(active_pincfg); |
| 150 | + tc_start_counter(&tc_instance); |
| 151 | + |
| 152 | + while(pulse_index < length) { |
| 153 | + // Do other things while we wait. The interrupts will handle sending the |
| 154 | + // signal. |
| 155 | + #ifdef MICROPY_VM_HOOK_LOOP |
| 156 | + MICROPY_VM_HOOK_LOOP |
| 157 | + #endif |
| 158 | + } |
| 159 | + |
| 160 | + tc_stop_counter(&tc_instance); |
| 161 | + tc_disable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0); |
| 162 | + active_pincfg = NULL; |
| 163 | +} |
0 commit comments