2017-03-10 13:17:54 -05:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2017-03-10 13:17:54 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "common-hal/pulseio/PulseOut.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "asf/sam0/drivers/tc/tc_interrupt.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "asf/sam0/drivers/port/port.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
#include "mpconfigport.h"
|
2017-04-21 17:43:03 -04:00
|
|
|
#include "py/gc.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
#include "py/runtime.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "samd21_pins.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/pulseio/PulseOut.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
#undef ENABLE
|
|
|
|
|
|
|
|
// This timer is shared amongst all PulseOut objects under the assumption that
|
2017-05-04 14:57:51 -04:00
|
|
|
// the code is single threaded. Its stored in MICROPY_PORT_ROOT_POINTERS so it
|
|
|
|
// doesn't get garbage collected.
|
2017-03-10 13:17:54 -05:00
|
|
|
static uint8_t refcount = 0;
|
|
|
|
|
|
|
|
static __IO PORT_PINCFG_Type *active_pincfg = NULL;
|
|
|
|
static uint16_t *pulse_buffer = NULL;
|
|
|
|
static volatile uint16_t pulse_index = 0;
|
|
|
|
static uint16_t pulse_length;
|
|
|
|
static volatile uint32_t current_compare = 0;
|
|
|
|
|
|
|
|
static void turn_on(__IO PORT_PINCFG_Type * pincfg) {
|
|
|
|
pincfg->reg = PORT_PINCFG_PMUXEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void turn_off(__IO PORT_PINCFG_Type * pincfg) {
|
|
|
|
pincfg->reg = PORT_PINCFG_RESETVALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pulse_finish(struct tc_module *const module) {
|
|
|
|
pulse_index++;
|
|
|
|
|
|
|
|
if (active_pincfg == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Always turn it off.
|
|
|
|
turn_off(active_pincfg);
|
|
|
|
if (pulse_index >= pulse_length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff;
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_set_compare_value(MP_STATE_VM(pulseout_tc_instance), TC_COMPARE_CAPTURE_CHANNEL_0, current_compare);
|
2017-03-10 13:17:54 -05:00
|
|
|
if (pulse_index % 2 == 0) {
|
|
|
|
turn_on(active_pincfg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pulseout_reset() {
|
|
|
|
refcount = 0;
|
2017-05-04 14:57:51 -04:00
|
|
|
MP_STATE_VM(pulseout_tc_instance) = NULL;
|
2017-03-10 13:17:54 -05:00
|
|
|
active_pincfg = NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
|
|
|
|
const pulseio_pwmout_obj_t* carrier) {
|
2017-03-10 13:17:54 -05:00
|
|
|
if (refcount == 0) {
|
|
|
|
// Find a spare timer.
|
|
|
|
Tc *t = NULL;
|
|
|
|
Tc *tcs[TC_INST_NUM] = TC_INSTS;
|
|
|
|
for (uint8_t i = TC_INST_NUM; i > 0; i--) {
|
|
|
|
if (tcs[i - 1]->COUNT16.CTRLA.bit.ENABLE == 0) {
|
|
|
|
t = tcs[i - 1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t == NULL) {
|
|
|
|
mp_raise_RuntimeError("All timers in use");
|
|
|
|
}
|
2017-05-04 14:57:51 -04:00
|
|
|
MP_STATE_VM(pulseout_tc_instance) = gc_alloc(sizeof(struct tc_module), false);
|
2017-04-21 17:43:03 -04:00
|
|
|
if (t == NULL) {
|
|
|
|
mp_raise_msg(&mp_type_MemoryError, "");
|
|
|
|
}
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
struct tc_config config_tc;
|
|
|
|
tc_get_config_defaults(&config_tc);
|
|
|
|
|
|
|
|
config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
|
|
|
|
config_tc.clock_prescaler = TC_CTRLA_PRESCALER_DIV64;
|
|
|
|
config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ;
|
|
|
|
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_init(MP_STATE_VM(pulseout_tc_instance), t, &config_tc);
|
|
|
|
tc_register_callback(MP_STATE_VM(pulseout_tc_instance), pulse_finish, TC_CALLBACK_CC_CHANNEL0);
|
|
|
|
tc_enable(MP_STATE_VM(pulseout_tc_instance));
|
|
|
|
tc_stop_counter(MP_STATE_VM(pulseout_tc_instance));
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
refcount++;
|
|
|
|
|
|
|
|
self->pin = carrier->pin->pin;
|
|
|
|
|
|
|
|
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
|
|
|
|
self->pincfg = &port_base->PINCFG[self->pin % 32];
|
|
|
|
|
|
|
|
// Set the port to output a zero.
|
|
|
|
port_base->OUTCLR.reg = 1 << (self->pin % 32);
|
|
|
|
port_base->DIRSET.reg = 1 << (self->pin % 32);
|
|
|
|
|
|
|
|
// Turn off the pinmux which should connect the port output.
|
|
|
|
turn_off(self->pincfg);
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
|
|
|
|
return self->pin == NO_PIN;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_pulseio_pulseout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-10 13:17:54 -05:00
|
|
|
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
|
|
|
|
port_base->DIRCLR.reg = 1 << (self->pin % 32);
|
|
|
|
|
|
|
|
turn_on(self->pincfg);
|
|
|
|
|
|
|
|
refcount--;
|
|
|
|
if (refcount == 0) {
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_reset(MP_STATE_VM(pulseout_tc_instance));
|
|
|
|
gc_free(MP_STATE_VM(pulseout_tc_instance));
|
|
|
|
MP_STATE_VM(pulseout_tc_instance) = NULL;
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
2017-10-02 20:49:40 -04:00
|
|
|
self->pin = NO_PIN;
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
|
2017-03-10 13:17:54 -05:00
|
|
|
if (active_pincfg != NULL) {
|
|
|
|
mp_raise_RuntimeError("Another send is already active");
|
|
|
|
}
|
|
|
|
active_pincfg = self->pincfg;
|
|
|
|
pulse_buffer = pulses;
|
|
|
|
pulse_index = 0;
|
|
|
|
pulse_length = length;
|
|
|
|
|
|
|
|
current_compare = pulses[0] * 3 / 4;
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_set_compare_value(MP_STATE_VM(pulseout_tc_instance), TC_COMPARE_CAPTURE_CHANNEL_0, current_compare);
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_enable_callback(MP_STATE_VM(pulseout_tc_instance), TC_CALLBACK_CC_CHANNEL0);
|
2017-03-10 13:17:54 -05:00
|
|
|
turn_on(active_pincfg);
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_start_counter(MP_STATE_VM(pulseout_tc_instance));
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
while(pulse_index < length) {
|
|
|
|
// Do other things while we wait. The interrupts will handle sending the
|
|
|
|
// signal.
|
|
|
|
#ifdef MICROPY_VM_HOOK_LOOP
|
|
|
|
MICROPY_VM_HOOK_LOOP
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-04 14:57:51 -04:00
|
|
|
tc_stop_counter(MP_STATE_VM(pulseout_tc_instance));
|
|
|
|
tc_disable_callback(MP_STATE_VM(pulseout_tc_instance), TC_CALLBACK_CC_CHANNEL0);
|
2017-03-10 13:17:54 -05:00
|
|
|
active_pincfg = NULL;
|
|
|
|
}
|