circuitpython/ports/esp32s2/common-hal/pulseio/PulseOut.c

87 lines
3.5 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* 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 "common-hal/pulseio/PulseOut.h"
#include "shared-bindings/pulseio/PWMOut.h"
#include "py/runtime.h"
2020-08-04 10:59:05 -04:00
// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset
2020-08-04 10:59:05 -04:00
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
2020-08-14 16:33:24 -04:00
const pulseio_pwmout_obj_t* carrier,
const mcu_pin_obj_t* pin,
uint32_t frequency,
uint16_t duty_cycle) {
if (carrier || !pin || !frequency) {
mp_raise_NotImplementedError(translate("Port does not accept PWM carrier. \
Pass a pin, frequency and duty cycle instead"));
}
2020-08-04 10:59:05 -04:00
rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
2020-08-04 10:59:05 -04:00
// Configure Channel
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin->number, channel);
config.tx_config.carrier_en = true;
2020-08-14 16:33:24 -04:00
config.tx_config.carrier_duty_percent = (duty_cycle * 100) / (1<<16);
2020-08-04 10:59:05 -04:00
config.tx_config.carrier_freq_hz = frequency;
config.clk_div = 80;
2020-08-04 10:59:05 -04:00
rmt_config(&config);
rmt_driver_install(channel, 0, 0);
2020-08-04 10:59:05 -04:00
self->channel = channel;
}
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
2020-08-04 10:59:05 -04:00
return (self->channel == RMT_CHANNEL_MAX);
}
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
2020-08-04 10:59:05 -04:00
esp32s2_peripherals_free_rmt(self->channel);
self->channel = RMT_CHANNEL_MAX;
}
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {
2020-08-04 10:59:05 -04:00
rmt_item32_t items[length];
// Circuitpython allows 16 bit pulse values, while ESP32 only allows 15 bits
// Thus, we use entire items for one pulse, rather than switching inside each item
for (size_t i = 0; i < length; i++) {
// Setting the RMT duration to 0 has undefined behavior, so avoid that pre-emptively.
if (pulses[i] == 0) {
pulses[i] = 1;
}
uint32_t level = (i % 2) ? 0 : 1;
const rmt_item32_t item = {{{ (pulses[i] & 0x8000 ? 0x7FFF : 1), level, (pulses[i] & 0x7FFF), level}}};
items[i] = item;
}
rmt_write_items(self->channel, items, length, true);
rmt_wait_tx_done(self->channel, pdMS_TO_TICKS(100));
}