|
| 1 | +#include <mruby.h> |
| 2 | +#include <mruby/data.h> |
| 3 | +#include <mruby/variable.h> |
| 4 | +#include <mruby/string.h> |
| 5 | +#include <mruby/array.h> |
| 6 | +#include <mruby/presym.h> |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#if ESP_PLATFORM |
| 12 | +#include "freertos/FreeRTOS.h" |
| 13 | +#include "freertos/task.h" |
| 14 | +#include "driver/mcpwm_prelude.h" |
| 15 | +#include "esp_log.h" |
| 16 | +#else |
| 17 | +#define ESP_LOGI(tag, fmt, ...) printf(fmt, __VA_ARGS__) |
| 18 | +#endif |
| 19 | + |
| 20 | +#define TAG ("mruby-esp32-pwm") |
| 21 | + |
| 22 | +typedef struct pwm_t { |
| 23 | + mrb_int pulse_width_ticks; |
| 24 | + mrb_int pin; |
| 25 | + mrb_int period_ticks; |
| 26 | +#if ESP_PLATFORM |
| 27 | + mcpwm_timer_handle_t *timer; |
| 28 | + mcpwm_cmpr_handle_t *comparator; |
| 29 | + mcpwm_oper_handle_t *operator; |
| 30 | + mcpwm_gen_handle_t *generator; |
| 31 | +#endif |
| 32 | + mrb_state *mrb; |
| 33 | +} pwm_t; |
| 34 | + |
| 35 | +static void mrb_pwm_free(mrb_state *mrb, void *p); |
| 36 | + |
| 37 | +static const struct mrb_data_type mrb_pwm = { |
| 38 | + "mrb_mruby_esp32_pwm", mrb_pwm_free |
| 39 | +}; |
| 40 | + |
| 41 | +static void |
| 42 | +mrb_pwm_new_mcpwm(mrb_state *mrb, pwm_t *pwm) { |
| 43 | +#if ESP_PLATFORM |
| 44 | + if((pwm->period_ticks <= 0) || (pwm->pulse_width_ticks <= 0)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + pwm->timer = mrb_malloc(mrb, sizeof(mcpwm_timer_handle_t)); |
| 49 | + mcpwm_timer_config_t timer_config = { |
| 50 | + .group_id = 0, |
| 51 | + .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, |
| 52 | + .resolution_hz = 1000000, |
| 53 | + .period_ticks = pwm->period_ticks, |
| 54 | + .count_mode = MCPWM_TIMER_COUNT_MODE_UP, |
| 55 | + }; |
| 56 | + ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, pwm->timer)); |
| 57 | + |
| 58 | + pwm->operator = mrb_malloc(mrb, sizeof(mcpwm_oper_handle_t)); |
| 59 | + mcpwm_operator_config_t operator_config = { |
| 60 | + .group_id = 0, |
| 61 | + }; |
| 62 | + ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, pwm->operator)); |
| 63 | + ESP_ERROR_CHECK(mcpwm_operator_connect_timer(*pwm->operator, *pwm->timer)); |
| 64 | + |
| 65 | + pwm->comparator = mrb_malloc(mrb, sizeof(mcpwm_cmpr_handle_t)); |
| 66 | + ESP_LOGI(TAG, "!(%p)", pwm->comparator); |
| 67 | + |
| 68 | + mcpwm_comparator_config_t comparator_config = { |
| 69 | + .flags.update_cmp_on_tez = true, |
| 70 | + }; |
| 71 | + ESP_ERROR_CHECK(mcpwm_new_comparator(*pwm->operator, &comparator_config, pwm->comparator)); |
| 72 | + |
| 73 | + pwm->generator = mrb_malloc(mrb, sizeof(mcpwm_gen_handle_t)); |
| 74 | + mcpwm_generator_config_t generator_config = { |
| 75 | + .gen_gpio_num = pwm->pin, |
| 76 | + }; |
| 77 | + ESP_ERROR_CHECK(mcpwm_new_generator(*pwm->operator, &generator_config, pwm->generator)); |
| 78 | + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(*pwm->comparator, pwm->pulse_width_ticks)); |
| 79 | + |
| 80 | + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(*pwm->generator, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); |
| 81 | + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(*pwm->generator, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, *pwm->comparator, MCPWM_GEN_ACTION_LOW))); |
| 82 | + |
| 83 | + ESP_ERROR_CHECK(mcpwm_timer_enable(*pwm->timer)); |
| 84 | + ESP_ERROR_CHECK(mcpwm_timer_start_stop(*pwm->timer, MCPWM_TIMER_START_NO_STOP)); |
| 85 | +#endif |
| 86 | +} |
| 87 | + |
| 88 | +static void |
| 89 | +mrb_pwm_del_mcpwm(mrb_state *mrb, pwm_t *pwm) { |
| 90 | +#if ESP_PLATFORM |
| 91 | + if(pwm->generator != NULL) { |
| 92 | + ESP_ERROR_CHECK(mcpwm_del_generator(*pwm->generator)); |
| 93 | + mrb_free(mrb, pwm->generator); |
| 94 | + pwm->generator = NULL; |
| 95 | + } |
| 96 | + if(pwm->comparator != NULL) { |
| 97 | + ESP_ERROR_CHECK(mcpwm_del_comparator(*pwm->comparator)); |
| 98 | + mrb_free(mrb, pwm->comparator); |
| 99 | + pwm->comparator = NULL; |
| 100 | + } |
| 101 | + if(pwm->operator != NULL) { |
| 102 | + ESP_ERROR_CHECK(mcpwm_del_operator(*pwm->operator)); |
| 103 | + mrb_free(mrb, pwm->operator); |
| 104 | + pwm->operator = NULL; |
| 105 | + } |
| 106 | + if(pwm->timer != NULL) { |
| 107 | + ESP_ERROR_CHECK(mcpwm_timer_start_stop(*pwm->timer, MCPWM_TIMER_STOP_EMPTY)); |
| 108 | + ESP_ERROR_CHECK(mcpwm_timer_disable(*pwm->timer)); |
| 109 | + ESP_ERROR_CHECK(mcpwm_del_timer(*pwm->timer)); |
| 110 | + mrb_free(mrb, pwm->timer); |
| 111 | + pwm->timer = NULL; |
| 112 | + } |
| 113 | +#endif |
| 114 | +} |
| 115 | + |
| 116 | +static void |
| 117 | +mrb_pwm_free(mrb_state *mrb, void *p) { |
| 118 | + pwm_t *pwm = (pwm_t *)p; |
| 119 | + |
| 120 | +#if ESP_PLATFORM |
| 121 | + mrb_pwm_del_mcpwm(mrb, pwm); |
| 122 | +#endif |
| 123 | + |
| 124 | + mrb_free(mrb, p); |
| 125 | +} |
| 126 | + |
| 127 | +static mrb_value |
| 128 | +mrb_pwm_init(mrb_state *mrb, mrb_value self) { |
| 129 | + pwm_t *pwm = mrb_malloc(mrb, sizeof(pwm_t)); |
| 130 | + mrb_get_args(mrb, "i", &pwm->pin); |
| 131 | + ESP_LOGI(TAG, "__initialize(pin: %d)", pwm->pin); |
| 132 | + |
| 133 | + pwm->period_ticks = 0; |
| 134 | + pwm->pulse_width_ticks = 0; |
| 135 | + |
| 136 | +#if ESP_PLATFORM |
| 137 | + pwm->timer = NULL; |
| 138 | + pwm->comparator = NULL; |
| 139 | + pwm->operator = NULL; |
| 140 | + pwm->generator = NULL; |
| 141 | + |
| 142 | + mrb_pwm_new_mcpwm(mrb, pwm); |
| 143 | +#endif |
| 144 | + |
| 145 | + mrb_data_init(self, pwm, &mrb_pwm); |
| 146 | + |
| 147 | + return self; |
| 148 | +} |
| 149 | + |
| 150 | +static mrb_value |
| 151 | +mrb_pwm_update(mrb_state *mrb, mrb_value self) { |
| 152 | + pwm_t *pwm = (pwm_t *) DATA_PTR(self); |
| 153 | + mrb_get_args(mrb, "ii", &pwm->period_ticks, &pwm->pulse_width_ticks); |
| 154 | + ESP_LOGI(TAG, "__update(%d, %d)", pwm->period_ticks, pwm->pulse_width_ticks); |
| 155 | + |
| 156 | +#if ESP_PLATFORM |
| 157 | + mrb_pwm_del_mcpwm(mrb, pwm); |
| 158 | + mrb_pwm_new_mcpwm(mrb, pwm); |
| 159 | +#endif |
| 160 | + |
| 161 | + return self; |
| 162 | +} |
| 163 | + |
| 164 | +void |
| 165 | +mrb_mruby_esp32_pwm_gem_init(mrb_state* mrb) { |
| 166 | + struct RClass *client_class = mrb_define_class(mrb, "PWM", mrb->object_class); |
| 167 | + |
| 168 | + mrb_define_method(mrb, client_class, "__initialize", mrb_pwm_init, MRB_ARGS_REQ(1)); |
| 169 | + mrb_define_method(mrb, client_class, "__update", mrb_pwm_update, MRB_ARGS_REQ(2)); |
| 170 | +} |
| 171 | + |
| 172 | +void |
| 173 | +mrb_mruby_esp32_pwm_gem_final(mrb_state* mrb) { |
| 174 | +} |
0 commit comments