1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 #include "driver/periph_ctrl.h"
17 #include "driver/sigmadelta.h"
19 static const char* SIGMADELTA_TAG = "SIGMADELTA";
20 #define SIGMADELTA_CHANNEL_ERR_STR "SIGMADELTA CHANNEL ERROR"
21 #define SIGMADELTA_IO_ERR_STR "SIGMADELTA GPIO NUM ERROR"
23 #define SIGMADELTA_CHECK(a,str,ret_val) if(!(a)) { \
24 ESP_LOGE(SIGMADELTA_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
28 esp_err_t sigmadelta_config(const sigmadelta_config_t *config)
30 SIGMADELTA_CHECK(config->channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
31 SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(config->sigmadelta_gpio), SIGMADELTA_IO_ERR_STR, ESP_ERR_INVALID_ARG);
32 sigmadelta_set_duty(config->channel, config->sigmadelta_duty);
33 sigmadelta_set_prescale(config->channel, config->sigmadelta_prescale);
34 sigmadelta_set_pin(config->channel, config->sigmadelta_gpio);
38 esp_err_t sigmadelta_set_duty(sigmadelta_channel_t channel, int8_t duty)
40 SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
41 SIGMADELTA.channel[channel].duty = duty;
45 esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale)
47 SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
48 SIGMADELTA.channel[channel].prescale = prescale;
52 esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num)
54 SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
55 SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), SIGMADELTA_IO_ERR_STR, ESP_ERR_INVALID_ARG);
56 PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
57 gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
58 gpio_matrix_out(gpio_num, GPIO_SD0_OUT_IDX + channel, 0, 0);