summaryrefslogtreecommitdiff
path: root/ports/esp32s2/common-hal/pulseio/PulseOut.c
blob: e45492a8935962b2cc1fb9133730e25e72da0558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * 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/pwmio/PWMOut.h"
#include "py/runtime.h"

// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset

void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
                                            const pwmio_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"));
    }

    rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt();
    if (channel == RMT_CHANNEL_MAX) {
        mp_raise_RuntimeError(translate("All timers in use"));
    }

    // Configure Channel
    rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin->number, channel);
    config.tx_config.carrier_en = true;
    config.tx_config.carrier_duty_percent = (duty_cycle * 100) / (1<<16);
    config.tx_config.carrier_freq_hz = frequency;
    config.clk_div = 80;

    rmt_config(&config);
    rmt_driver_install(channel, 0, 0);

    self->channel = channel;
}

bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
    return (self->channel == RMT_CHANNEL_MAX);
}

void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
    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) {
    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);
    while (rmt_wait_tx_done(self->channel, 0) != ESP_OK) {
        RUN_BACKGROUND_TASKS;
    }
}