|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Lucian Copeland for Adafruit Industries |
| 7 | + * Uses code from Micropython, Copyright (c) 2013-2016 Damien P. George |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | + |
| 29 | +#include "common-hal/pulseio/PWMOut.h" |
| 30 | +#include "shared-bindings/pulseio/PWMOut.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "driver/ledc.h" |
| 33 | + |
| 34 | +#define LEDC_LS_TIMER LEDC_TIMER_1 |
| 35 | +#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE |
| 36 | +#define LEDC_LS_CH0_GPIO (18) |
| 37 | +#define LEDC_LS_CH0_CHANNEL LEDC_CHANNEL_0 |
| 38 | +#define LEDC_LS_CH1_GPIO (19) |
| 39 | +#define LEDC_LS_CH1_CHANNEL LEDC_CHANNEL_1 |
| 40 | +#define LEDC_LS_CH2_GPIO (4) |
| 41 | +#define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2 |
| 42 | +#define LEDC_LS_CH3_GPIO (5) |
| 43 | +#define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3 |
| 44 | + |
| 45 | +#define LEDC_TEST_CH_NUM (4) |
| 46 | +#define LEDC_TEST_DUTY (4000) |
| 47 | +#define LEDC_TEST_FADE_TIME (3000) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +void pwmout_reset(void) { |
| 52 | +} |
| 53 | + |
| 54 | +pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, |
| 55 | + const mcu_pin_obj_t* pin, |
| 56 | + uint16_t duty, |
| 57 | + uint32_t frequency, |
| 58 | + bool variable_frequency) { |
| 59 | + ledc_timer_config_t ledc_timer = { |
| 60 | + .duty_resolution = LEDC_TIMER_16_BIT, // resolution of PWM duty |
| 61 | + .freq_hz = frequency, // frequency of PWM signal |
| 62 | + .speed_mode = LEDC_LS_MODE, // timer mode |
| 63 | + .timer_num = LEDC_LS_TIMER, // timer index |
| 64 | + .clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock |
| 65 | + }; |
| 66 | + // Set configuration of timer0 for high speed channels |
| 67 | + ledc_timer_config(&ledc_timer); |
| 68 | + |
| 69 | + ledc_channel_config_t pwm_channel = { |
| 70 | + .channel = LEDC_LS_CH0_CHANNEL, |
| 71 | + .duty = 0, |
| 72 | + .gpio_num = pin->number, |
| 73 | + .speed_mode = LEDC_LS_MODE, |
| 74 | + .hpoint = 0, |
| 75 | + .timer_sel = LEDC_LS_TIMER |
| 76 | + }; |
| 77 | + |
| 78 | + // Set LED Controller with previously prepared configuration |
| 79 | + for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { |
| 80 | + ledc_channel_config(&ledc_channel[ch]); |
| 81 | + } |
| 82 | + |
| 83 | + for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { |
| 84 | + ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, duty*2); |
| 85 | + ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel); |
| 86 | + } |
| 87 | + |
| 88 | + return PWMOUT_OK; |
| 89 | +} |
| 90 | + |
| 91 | +void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { |
| 92 | +} |
| 93 | + |
| 94 | +void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { |
| 95 | +} |
| 96 | + |
| 97 | +bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) { |
| 98 | + return false; |
| 99 | +} |
| 100 | + |
| 101 | +void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { |
| 102 | +} |
| 103 | + |
| 104 | +void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) { |
| 105 | +} |
| 106 | + |
| 107 | +uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) { |
| 108 | + return false; |
| 109 | +} |
| 110 | + |
| 111 | +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) { |
| 112 | +} |
| 113 | + |
| 114 | +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) { |
| 115 | + return false; |
| 116 | +} |
| 117 | + |
| 118 | +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) { |
| 119 | + return false; |
| 120 | +} |
0 commit comments