1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "esp_log.h"
8 #include "esp_check.h"
9 #include "esp_err.h"
10 #include "driver/sigmadelta.h"
11 #include "esp_heap_caps.h"
12 #include "hal/sigmadelta_hal.h"
13 #include "hal/gpio_hal.h"
14 #include "esp_rom_gpio.h"
15
16 static const char *TAG = "sigma-delta";
17
18 #define SIGMADELTA_CHECK(a,str,ret_val) ESP_RETURN_ON_FALSE(a, ret_val, TAG, "%s", str)
19
20 typedef struct {
21 sigmadelta_hal_context_t hal; /*!< SIGMADELTA hal context*/
22 } sigmadelta_obj_t;
23
24 static sigmadelta_obj_t *p_sigmadelta_obj[SIGMADELTA_PORT_MAX] = {0};
25
26 #define SIGMADELTA_OBJ_CHECK(sigmadelta_port) { \
27 SIGMADELTA_CHECK((sigmadelta_port < SIGMADELTA_PORT_MAX), "sigmadelta port error", ESP_ERR_INVALID_ARG); \
28 SIGMADELTA_CHECK((p_sigmadelta_obj[sigmadelta_port]), "sigmadelta driver has not been initialized", ESP_ERR_INVALID_STATE); \
29 }
30
_sigmadelta_set_duty(sigmadelta_port_t sigmadelta_port,sigmadelta_channel_t channel,int8_t duty)31 static inline esp_err_t _sigmadelta_set_duty(sigmadelta_port_t sigmadelta_port, sigmadelta_channel_t channel, int8_t duty)
32 {
33 SIGMADELTA_OBJ_CHECK(sigmadelta_port);
34
35 sigmadelta_hal_set_duty(&(p_sigmadelta_obj[sigmadelta_port]->hal), channel, duty);
36 return ESP_OK;
37 }
38
_sigmadelta_set_prescale(sigmadelta_port_t sigmadelta_port,sigmadelta_channel_t channel,uint8_t prescale)39 static inline esp_err_t _sigmadelta_set_prescale(sigmadelta_port_t sigmadelta_port, sigmadelta_channel_t channel, uint8_t prescale)
40 {
41 SIGMADELTA_OBJ_CHECK(sigmadelta_port);
42
43 sigmadelta_hal_set_prescale(&(p_sigmadelta_obj[sigmadelta_port]->hal), channel, prescale);
44 return ESP_OK;
45 }
46
_sigmadelta_set_pin(sigmadelta_port_t sigmadelta_port,sigmadelta_channel_t channel,gpio_num_t gpio_num)47 static inline esp_err_t _sigmadelta_set_pin(sigmadelta_port_t sigmadelta_port, sigmadelta_channel_t channel, gpio_num_t gpio_num)
48 {
49 SIGMADELTA_OBJ_CHECK(sigmadelta_port);
50
51 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
52 gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
53 esp_rom_gpio_connect_out_signal(gpio_num, sigma_delta_periph_signals.channels[channel].sd_sig, 0, 0);
54 return ESP_OK;
55 }
56
_sigmadelta_config(sigmadelta_port_t sigmadelta_port,const sigmadelta_config_t * config)57 static inline esp_err_t _sigmadelta_config(sigmadelta_port_t sigmadelta_port, const sigmadelta_config_t *config)
58 {
59 SIGMADELTA_OBJ_CHECK(sigmadelta_port);
60
61 _sigmadelta_set_duty(sigmadelta_port, config->channel, config->sigmadelta_duty);
62 _sigmadelta_set_prescale(sigmadelta_port, config->channel, config->sigmadelta_prescale);
63 _sigmadelta_set_pin(sigmadelta_port, config->channel, config->sigmadelta_gpio);
64 return ESP_OK;
65 }
66
sigmadelta_deinit(sigmadelta_port_t sigmadelta_port)67 esp_err_t sigmadelta_deinit(sigmadelta_port_t sigmadelta_port)
68 {
69 SIGMADELTA_OBJ_CHECK(sigmadelta_port);
70
71 heap_caps_free(p_sigmadelta_obj[sigmadelta_port]);
72 p_sigmadelta_obj[sigmadelta_port] = NULL;
73 return ESP_OK;
74 }
75
sigmadelta_init(sigmadelta_port_t sigmadelta_port)76 esp_err_t sigmadelta_init(sigmadelta_port_t sigmadelta_port)
77 {
78 SIGMADELTA_CHECK((sigmadelta_port < SIGMADELTA_PORT_MAX), "sigmadelta_port error", ESP_ERR_INVALID_ARG);
79 SIGMADELTA_CHECK((p_sigmadelta_obj[sigmadelta_port]) == NULL, "sigmadelta driver already initialized", ESP_ERR_INVALID_STATE);
80
81 p_sigmadelta_obj[sigmadelta_port] = (sigmadelta_obj_t *) heap_caps_calloc(1, sizeof(sigmadelta_obj_t), MALLOC_CAP_DEFAULT);
82
83 if (p_sigmadelta_obj[sigmadelta_port] == NULL) {
84 ESP_LOGE(TAG, "TAG driver malloc error");
85 return ESP_FAIL;
86 }
87
88 sigmadelta_hal_init(&(p_sigmadelta_obj[sigmadelta_port]->hal), sigmadelta_port);
89 return ESP_OK;
90 }
91
92 // TODO: The following functions are wrappers, for compatibility with current API.
93
sigmadelta_set_duty(sigmadelta_channel_t channel,int8_t duty)94 esp_err_t sigmadelta_set_duty(sigmadelta_channel_t channel, int8_t duty)
95 {
96 SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG);
97 return _sigmadelta_set_duty(SIGMADELTA_PORT_0, channel, duty);
98 }
99
sigmadelta_set_prescale(sigmadelta_channel_t channel,uint8_t prescale)100 esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale)
101 {
102 SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG);
103 return _sigmadelta_set_prescale(SIGMADELTA_PORT_0, channel, prescale);
104 }
105
sigmadelta_set_pin(sigmadelta_channel_t channel,gpio_num_t gpio_num)106 esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num)
107 {
108 SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG);
109 SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "sigmadelta gpio num error", ESP_ERR_INVALID_ARG);
110 return _sigmadelta_set_pin(SIGMADELTA_PORT_0, channel, gpio_num);
111 }
112
sigmadelta_config(const sigmadelta_config_t * config)113 esp_err_t sigmadelta_config(const sigmadelta_config_t *config)
114 {
115 SIGMADELTA_CHECK(config->channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG);
116 SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(config->sigmadelta_gpio), "sigmadelta gpio num error", ESP_ERR_INVALID_ARG);
117 esp_err_t ret;
118
119 if ((p_sigmadelta_obj[SIGMADELTA_PORT_0]) == NULL) {
120 ret = sigmadelta_init(SIGMADELTA_PORT_0);
121 if (ret != ESP_OK) {
122 return ret;
123 }
124 }
125 return _sigmadelta_config(SIGMADELTA_PORT_0, config);
126 }
127