From e62b831867f49550a03ee02afa3b8d06b80b8585 Mon Sep 17 00:00:00 2001 From: xiongyu Date: Mon, 15 Jul 2019 14:44:15 +0800 Subject: [PATCH] refactor(sigmadelta): add hal sigmadelta driver --- components/driver/include/driver/sigmadelta.h | 42 ++---- components/driver/sigmadelta.c | 131 ++++++++++++++---- components/soc/CMakeLists.txt | 1 + .../soc/esp32/include/hal/sigmadelta_ll.h | 73 ++++++++++ .../soc/esp32/include/soc/sigmadelta_caps.h | 37 +++++ .../esp32s2beta/include/hal/sigmadelta_ll.h | 73 ++++++++++ .../esp32s2beta/include/soc/sigmadelta_caps.h | 37 +++++ components/soc/include/hal/sigmadelta_hal.h | 71 ++++++++++ components/soc/include/hal/sigmadelta_types.h | 45 ++++++ components/soc/src/hal/sigmadelta_hal.c | 26 ++++ docs/Doxyfile | 1 + 11 files changed, 477 insertions(+), 60 deletions(-) create mode 100644 components/soc/esp32/include/hal/sigmadelta_ll.h create mode 100644 components/soc/esp32/include/soc/sigmadelta_caps.h create mode 100644 components/soc/esp32s2beta/include/hal/sigmadelta_ll.h create mode 100644 components/soc/esp32s2beta/include/soc/sigmadelta_caps.h create mode 100644 components/soc/include/hal/sigmadelta_hal.h create mode 100644 components/soc/include/hal/sigmadelta_types.h create mode 100644 components/soc/src/hal/sigmadelta_hal.c diff --git a/components/driver/include/driver/sigmadelta.h b/components/driver/include/driver/sigmadelta.h index 93388f0de..783baa142 100644 --- a/components/driver/include/driver/sigmadelta.h +++ b/components/driver/include/driver/sigmadelta.h @@ -1,9 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at - // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software @@ -12,41 +11,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef __DRIVER_SIGMADELTA_H__ -#define __DRIVER_SIGMADELTA_H__ +#pragma once + #include #include "soc/sigmadelta_periph.h" +#include "soc/sigmadelta_caps.h" #include "driver/gpio.h" +#include "hal/sigmadelta_types.h" #ifdef _cplusplus extern "C" { #endif -/** - * @brief Sigma-delta channel list - */ -typedef enum{ - SIGMADELTA_CHANNEL_0 = 0, /*!< Sigma-delta channel 0 */ - SIGMADELTA_CHANNEL_1 = 1, /*!< Sigma-delta channel 1 */ - SIGMADELTA_CHANNEL_2 = 2, /*!< Sigma-delta channel 2 */ - SIGMADELTA_CHANNEL_3 = 3, /*!< Sigma-delta channel 3 */ - SIGMADELTA_CHANNEL_4 = 4, /*!< Sigma-delta channel 4 */ - SIGMADELTA_CHANNEL_5 = 5, /*!< Sigma-delta channel 5 */ - SIGMADELTA_CHANNEL_6 = 6, /*!< Sigma-delta channel 6 */ - SIGMADELTA_CHANNEL_7 = 7, /*!< Sigma-delta channel 7 */ - SIGMADELTA_CHANNEL_MAX, -} sigmadelta_channel_t; - -/** - * @brief Sigma-delta configure struct - */ -typedef struct { - sigmadelta_channel_t channel; /*!< Sigma-delta channel number */ - int8_t sigmadelta_duty; /*!< Sigma-delta duty, duty ranges from -128 to 127. */ - uint8_t sigmadelta_prescale; /*!< Sigma-delta prescale, prescale ranges from 0 to 255. */ - uint8_t sigmadelta_gpio; /*!< Sigma-delta output io number, refer to gpio.h for more details. */ -} sigmadelta_config_t; - /** * @brief Configure Sigma-delta channel * @@ -54,6 +30,7 @@ typedef struct { * * @return * - ESP_OK Success + * - ESP_ERR_INVALID_STATE sigmadelta driver already initialized * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t sigmadelta_config(const sigmadelta_config_t *config); @@ -69,9 +46,10 @@ esp_err_t sigmadelta_config(const sigmadelta_config_t *config); * @param channel Sigma-delta channel number * @param duty Sigma-delta duty of one channel, the value ranges from -128 to 127, recommended range is -90 ~ 90. * The waveform is more like a random one in this range. - * + * * @return * - ESP_OK Success + * - ESP_ERR_INVALID_STATE sigmadelta driver has not been initialized * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t sigmadelta_set_duty(sigmadelta_channel_t channel, int8_t duty); @@ -85,6 +63,7 @@ esp_err_t sigmadelta_set_duty(sigmadelta_channel_t channel, int8_t duty); * * @return * - ESP_OK Success + * - ESP_ERR_INVALID_STATE sigmadelta driver has not been initialized * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale); @@ -97,6 +76,7 @@ esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale * * @return * - ESP_OK Success + * - ESP_ERR_INVALID_STATE sigmadelta driver has not been initialized * - ESP_ERR_INVALID_ARG Parameter error */ esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num); @@ -104,5 +84,3 @@ esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num); #ifdef _cplusplus } #endif - -#endif diff --git a/components/driver/sigmadelta.c b/components/driver/sigmadelta.c index bc2981315..339c86c62 100644 --- a/components/driver/sigmadelta.c +++ b/components/driver/sigmadelta.c @@ -1,9 +1,9 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at - +// // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software @@ -11,49 +11,124 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -#include "esp_log.h" -#include "driver/sigmadelta.h" -static const char* SIGMADELTA_TAG = "SIGMADELTA"; -#define SIGMADELTA_CHANNEL_ERR_STR "SIGMADELTA CHANNEL ERROR" -#define SIGMADELTA_IO_ERR_STR "SIGMADELTA GPIO NUM ERROR" +#include "esp_log.h" +#include "esp_err.h" +#include "driver/sigmadelta.h" +#include "esp_heap_caps.h" +#include "hal/sigmadelta_hal.h" + +static const char *TAG = "SIGMADELTA"; #define SIGMADELTA_CHECK(a,str,ret_val) if(!(a)) { \ - ESP_LOGE(SIGMADELTA_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ - return (ret_val); \ - } + ESP_LOGE(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ + return (ret_val); \ + } -esp_err_t sigmadelta_config(const sigmadelta_config_t *config) +typedef struct { + sigmadelta_hal_context_t hal; /*!< SIGMADELTA hal context*/ +} sigmadelta_obj_t; + +static sigmadelta_obj_t *p_sigmadelta_obj[SIGMADELTA_PORT_MAX] = {0}; + +#define SIGMADELTA_OBJ_CHECK(sigmadelta_port) { \ + SIGMADELTA_CHECK((sigmadelta_port < SIGMADELTA_PORT_MAX), "sigmadelta port error", ESP_ERR_INVALID_ARG); \ + SIGMADELTA_CHECK((p_sigmadelta_obj[sigmadelta_port]), "sigmadelta driver has not been initialized", ESP_ERR_INVALID_STATE); \ +} + +static inline esp_err_t _sigmadelta_set_duty(sigmadelta_port_t sigmadelta_port, sigmadelta_channel_t channel, int8_t duty) { - SIGMADELTA_CHECK(config->channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG); - SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(config->sigmadelta_gpio), SIGMADELTA_IO_ERR_STR, ESP_ERR_INVALID_ARG); - sigmadelta_set_duty(config->channel, config->sigmadelta_duty); - sigmadelta_set_prescale(config->channel, config->sigmadelta_prescale); - sigmadelta_set_pin(config->channel, config->sigmadelta_gpio); + SIGMADELTA_OBJ_CHECK(sigmadelta_port); + + sigmadelta_hal_set_duty(&(p_sigmadelta_obj[sigmadelta_port]->hal), channel, duty); return ESP_OK; } -esp_err_t sigmadelta_set_duty(sigmadelta_channel_t channel, int8_t duty) +static inline esp_err_t _sigmadelta_set_prescale(sigmadelta_port_t sigmadelta_port, sigmadelta_channel_t channel, uint8_t prescale) { - SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG); - SIGMADELTA.channel[channel].duty = duty; + SIGMADELTA_OBJ_CHECK(sigmadelta_port); + + sigmadelta_hal_set_prescale(&(p_sigmadelta_obj[sigmadelta_port]->hal), channel, prescale); return ESP_OK; } -esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale) +static inline esp_err_t _sigmadelta_set_pin(sigmadelta_port_t sigmadelta_port, sigmadelta_channel_t channel, gpio_num_t gpio_num) { - SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG); - SIGMADELTA.channel[channel].prescale = prescale; - return ESP_OK; -} + SIGMADELTA_OBJ_CHECK(sigmadelta_port); -esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num) -{ - SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, SIGMADELTA_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG); - SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), SIGMADELTA_IO_ERR_STR, ESP_ERR_INVALID_ARG); PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO); gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT); gpio_matrix_out(gpio_num, GPIO_SD0_OUT_IDX + channel, 0, 0); return ESP_OK; } +static inline esp_err_t _sigmadelta_config(sigmadelta_port_t sigmadelta_port, const sigmadelta_config_t *config) +{ + SIGMADELTA_OBJ_CHECK(sigmadelta_port); + + _sigmadelta_set_duty(sigmadelta_port, config->channel, config->sigmadelta_duty); + _sigmadelta_set_prescale(sigmadelta_port, config->channel, config->sigmadelta_prescale); + _sigmadelta_set_pin(sigmadelta_port, config->channel, config->sigmadelta_gpio); + return ESP_OK; +} + +esp_err_t sigmadelta_deinit(sigmadelta_port_t sigmadelta_port) +{ + SIGMADELTA_OBJ_CHECK(sigmadelta_port); + + heap_caps_free(p_sigmadelta_obj[sigmadelta_port]); + p_sigmadelta_obj[sigmadelta_port] = NULL; + return ESP_OK; +} + +esp_err_t sigmadelta_init(sigmadelta_port_t sigmadelta_port) +{ + SIGMADELTA_CHECK((sigmadelta_port < SIGMADELTA_PORT_MAX), "sigmadelta_port error", ESP_ERR_INVALID_ARG); + SIGMADELTA_CHECK((p_sigmadelta_obj[sigmadelta_port]) == NULL, "sigmadelta driver already initialized", ESP_ERR_INVALID_STATE); + + p_sigmadelta_obj[sigmadelta_port] = (sigmadelta_obj_t *) heap_caps_calloc(1, sizeof(sigmadelta_obj_t), MALLOC_CAP_DEFAULT); + + if (p_sigmadelta_obj[sigmadelta_port] == NULL) { + ESP_LOGE(TAG, "TAG driver malloc error"); + return ESP_FAIL; + } + + sigmadelta_hal_init(&(p_sigmadelta_obj[sigmadelta_port]->hal), sigmadelta_port); + return ESP_OK; +} + +// TODO: The following functions are wrappers, for compatibility with current API. + +esp_err_t sigmadelta_set_duty(sigmadelta_channel_t channel, int8_t duty) +{ + SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG); + return _sigmadelta_set_duty(SIGMADELTA_PORT_0, channel, duty); +} + +esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale) +{ + SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG); + return _sigmadelta_set_prescale(SIGMADELTA_PORT_0, channel, prescale); +} + +esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num) +{ + SIGMADELTA_CHECK(channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG); + SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "sigmadelta gpio num error", ESP_ERR_INVALID_ARG); + return _sigmadelta_set_pin(SIGMADELTA_PORT_0, channel, gpio_num); +} + +esp_err_t sigmadelta_config(const sigmadelta_config_t *config) +{ + SIGMADELTA_CHECK(config->channel < SIGMADELTA_CHANNEL_MAX, "sigmadelta channel error", ESP_ERR_INVALID_ARG); + SIGMADELTA_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(config->sigmadelta_gpio), "sigmadelta gpio num error", ESP_ERR_INVALID_ARG); + esp_err_t ret; + + if ((p_sigmadelta_obj[SIGMADELTA_PORT_0]) == NULL) { + ret = sigmadelta_init(SIGMADELTA_PORT_0); + if (ret != ESP_OK) { + return ret; + } + } + return _sigmadelta_config(SIGMADELTA_PORT_0, config); +} diff --git a/components/soc/CMakeLists.txt b/components/soc/CMakeLists.txt index dff818fbb..391c46e0f 100644 --- a/components/soc/CMakeLists.txt +++ b/components/soc/CMakeLists.txt @@ -21,6 +21,7 @@ list(APPEND srcs "src/soc_include_legacy_warn.c" "src/hal/pcnt_hal.c" "src/hal/i2s_hal.c" + "src/hal/sigmadelta_hal.c" ) # TODO: SPI Flash HAL for ESP32S2Beta also diff --git a/components/soc/esp32/include/hal/sigmadelta_ll.h b/components/soc/esp32/include/hal/sigmadelta_ll.h new file mode 100644 index 000000000..922cbc85e --- /dev/null +++ b/components/soc/esp32/include/hal/sigmadelta_ll.h @@ -0,0 +1,73 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/******************************************************************************* + * NOTICE + * The hal is not public api, don't use in application code. + * See readme.md in soc/include/hal/readme.md + ******************************************************************************/ + +// The LL layer for ESP32 SIGMADELTA register operations + +#pragma once + +#include +#include "soc/sigmadelta_periph.h" +#include "hal/sigmadelta_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Get SIGMADELTA hardware instance with giving sigmadelta num +#define SIGMADELTA_LL_GET_HW(num) (((num) == 0) ? (&SIGMADELTA) : NULL) + +/** + * @brief Set Sigma-delta enable + * + * @param hw Peripheral SIGMADELTA hardware instance address. + * @param en Sigma-delta enable value + */ +static inline void sigmadelta_ll_set_en(gpio_sd_dev_t *hw, bool en) +{ + // The clk enable register does not exist on ESP32. +} + +/** + * @brief Set Sigma-delta channel duty. + * + * @param hw Peripheral SIGMADELTA hardware instance address. + * @param channel Sigma-delta channel number + * @param duty Sigma-delta duty of one channel, the value ranges from -128 to 127, recommended range is -90 ~ 90. + * The waveform is more like a random one in this range. + */ +static inline void sigmadelta_ll_set_duty(gpio_sd_dev_t *hw, sigmadelta_channel_t channel, int8_t duty) +{ + hw->channel[channel].duty = duty; +} + +/** + * @brief Set Sigma-delta channel's clock pre-scale value. + * + * @param hw Peripheral SIGMADELTA hardware instance address. + * @param channel Sigma-delta channel number + * @param val The divider of source clock, ranges from 0 to 255 + */ +static inline void sigmadelta_ll_set_prescale(gpio_sd_dev_t *hw, sigmadelta_channel_t channel, uint8_t prescale) +{ + hw->channel[channel].prescale = prescale; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/components/soc/esp32/include/soc/sigmadelta_caps.h b/components/soc/esp32/include/soc/sigmadelta_caps.h new file mode 100644 index 000000000..53035d185 --- /dev/null +++ b/components/soc/esp32/include/soc/sigmadelta_caps.h @@ -0,0 +1,37 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +// ESP32 have 1 SIGMADELTA peripheral. +#define SIGMADELTA_PORT_0 (0) /*!< SIGMADELTA port 0 */ +#define SIGMADELTA_PORT_MAX (1) /*!< SIGMADELTA port max */ +#define SOC_SIGMADELTA_NUM (SIGMADELTA_PORT_MAX) + +#define SIGMADELTA_CHANNEL_0 (0) /*!< Sigma-delta channel 0 */ +#define SIGMADELTA_CHANNEL_1 (1) /*!< Sigma-delta channel 1 */ +#define SIGMADELTA_CHANNEL_2 (2) /*!< Sigma-delta channel 2 */ +#define SIGMADELTA_CHANNEL_3 (3) /*!< Sigma-delta channel 3 */ +#define SIGMADELTA_CHANNEL_4 (4) /*!< Sigma-delta channel 4 */ +#define SIGMADELTA_CHANNEL_5 (5) /*!< Sigma-delta channel 5 */ +#define SIGMADELTA_CHANNEL_6 (6) /*!< Sigma-delta channel 6 */ +#define SIGMADELTA_CHANNEL_7 (7) /*!< Sigma-delta channel 7 */ +#define SIGMADELTA_CHANNEL_MAX (8) + +#ifdef __cplusplus +} +#endif diff --git a/components/soc/esp32s2beta/include/hal/sigmadelta_ll.h b/components/soc/esp32s2beta/include/hal/sigmadelta_ll.h new file mode 100644 index 000000000..71a1190e2 --- /dev/null +++ b/components/soc/esp32s2beta/include/hal/sigmadelta_ll.h @@ -0,0 +1,73 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/******************************************************************************* + * NOTICE + * The hal is not public api, don't use in application code. + * See readme.md in soc/include/hal/readme.md + ******************************************************************************/ + +// The LL layer for ESP32-S2 SIGMADELTA register operations + +#pragma once + +#include +#include "soc/sigmadelta_periph.h" +#include "hal/sigmadelta_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Get SIGMADELTA hardware instance with giving sigmadelta num +#define SIGMADELTA_LL_GET_HW(num) (((num) == 0) ? (&SIGMADELTA) : NULL) + +/** + * @brief Set Sigma-delta enable + * + * @param hw Peripheral SIGMADELTA hardware instance address. + * @param en Sigma-delta enable value + */ +static inline void sigmadelta_ll_set_en(gpio_sd_dev_t *hw, bool en) +{ + hw->misc.function_clk_en = en; +} + +/** + * @brief Set Sigma-delta channel duty. + * + * @param hw Peripheral SIGMADELTA hardware instance address. + * @param channel Sigma-delta channel number + * @param duty Sigma-delta duty of one channel, the value ranges from -128 to 127, recommended range is -90 ~ 90. + * The waveform is more like a random one in this range. + */ +static inline void sigmadelta_ll_set_duty(gpio_sd_dev_t *hw, sigmadelta_channel_t channel, int8_t duty) +{ + hw->channel[channel].duty = duty; +} + +/** + * @brief Set Sigma-delta channel's clock pre-scale value. + * + * @param hw Peripheral SIGMADELTA hardware instance address. + * @param channel Sigma-delta channel number + * @param prescale The divider of source clock, ranges from 0 to 255 + */ +static inline void sigmadelta_ll_set_prescale(gpio_sd_dev_t *hw, sigmadelta_channel_t channel, uint8_t prescale) +{ + hw->channel[channel].prescale = prescale; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/components/soc/esp32s2beta/include/soc/sigmadelta_caps.h b/components/soc/esp32s2beta/include/soc/sigmadelta_caps.h new file mode 100644 index 000000000..5ae88424a --- /dev/null +++ b/components/soc/esp32s2beta/include/soc/sigmadelta_caps.h @@ -0,0 +1,37 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +// ESP32-S2 have 1 SIGMADELTA peripheral. +#define SIGMADELTA_PORT_0 (0) /*!< SIGMADELTA port 0 */ +#define SIGMADELTA_PORT_MAX (1) /*!< SIGMADELTA port max */ +#define SOC_SIGMADELTA_NUM (SIGMADELTA_PORT_MAX) + +#define SIGMADELTA_CHANNEL_0 (0) /*!< Sigma-delta channel 0 */ +#define SIGMADELTA_CHANNEL_1 (1) /*!< Sigma-delta channel 1 */ +#define SIGMADELTA_CHANNEL_2 (2) /*!< Sigma-delta channel 2 */ +#define SIGMADELTA_CHANNEL_3 (3) /*!< Sigma-delta channel 3 */ +#define SIGMADELTA_CHANNEL_4 (4) /*!< Sigma-delta channel 4 */ +#define SIGMADELTA_CHANNEL_5 (5) /*!< Sigma-delta channel 5 */ +#define SIGMADELTA_CHANNEL_6 (6) /*!< Sigma-delta channel 6 */ +#define SIGMADELTA_CHANNEL_7 (7) /*!< Sigma-delta channel 7 */ +#define SIGMADELTA_CHANNEL_MAX (8) + +#ifdef __cplusplus +} +#endif diff --git a/components/soc/include/hal/sigmadelta_hal.h b/components/soc/include/hal/sigmadelta_hal.h new file mode 100644 index 000000000..55363fdcc --- /dev/null +++ b/components/soc/include/hal/sigmadelta_hal.h @@ -0,0 +1,71 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/******************************************************************************* + * NOTICE + * The hal is not public api, don't use in application code. + * See readme.md in soc/include/hal/readme.md + ******************************************************************************/ + +// The HAL layer for SIGMADELTA. +// There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters. + +#pragma once + +#include "soc/sigmadelta_periph.h" +#include "hal/sigmadelta_types.h" +#include "hal/sigmadelta_ll.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Context that should be maintained by both the driver and the HAL + */ + +typedef struct { + gpio_sd_dev_t *dev; +} sigmadelta_hal_context_t; + +/** + * @brief Set Sigma-delta channel duty. + * + * @param hal Context of the HAL layer + * @param channel Sigma-delta channel number + * @param duty Sigma-delta duty of one channel, the value ranges from -128 to 127, recommended range is -90 ~ 90. + * The waveform is more like a random one in this range. + */ +#define sigmadelta_hal_set_duty(hal, channel, duty) sigmadelta_ll_set_duty((hal)->dev, channel, duty) + +/** + * @brief Set Sigma-delta channel's clock pre-scale value. + * + * @param hal Context of the HAL layer + * @param channel Sigma-delta channel number + * @param prescale The divider of source clock, ranges from 0 to 255 + */ +#define sigmadelta_hal_set_prescale(hal, channel, prescale) sigmadelta_ll_set_prescale((hal)->dev, channel, prescale) + +/** + * @brief Init the SIGMADELTA hal and set the SIGMADELTA to the default configuration. This function should be called first before other hal layer function is called + * + * @param hal Context of the HAL layer + * @param sigmadelta_num The uart port number, the max port number is (SIGMADELTA_NUM_MAX -1) + */ +void sigmadelta_hal_init(sigmadelta_hal_context_t *hal, int sigmadelta_num); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/components/soc/include/hal/sigmadelta_types.h b/components/soc/include/hal/sigmadelta_types.h new file mode 100644 index 000000000..e6ab0b473 --- /dev/null +++ b/components/soc/include/hal/sigmadelta_types.h @@ -0,0 +1,45 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "soc/sigmadelta_caps.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief SIGMADELTA port number, the max port number is (SIGMADELTA_NUM_MAX -1). + */ +typedef int sigmadelta_port_t; + +/** + * @brief Sigma-delta channel list + */ +typedef int sigmadelta_channel_t; + +/** + * @brief Sigma-delta configure struct + */ +typedef struct { + sigmadelta_channel_t channel; /*!< Sigma-delta channel number */ + int8_t sigmadelta_duty; /*!< Sigma-delta duty, duty ranges from -128 to 127. */ + uint8_t sigmadelta_prescale; /*!< Sigma-delta prescale, prescale ranges from 0 to 255. */ + uint8_t sigmadelta_gpio; /*!< Sigma-delta output io number, refer to gpio.h for more details. */ +} sigmadelta_config_t; + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/components/soc/src/hal/sigmadelta_hal.c b/components/soc/src/hal/sigmadelta_hal.c new file mode 100644 index 000000000..01559cf58 --- /dev/null +++ b/components/soc/src/hal/sigmadelta_hal.c @@ -0,0 +1,26 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// The HAL layer for SIGMADELTA (common part) + +#include +#include "soc/soc.h" +#include "hal/sigmadelta_hal.h" + +void sigmadelta_hal_init(sigmadelta_hal_context_t *hal, int sigmadelta_num) +{ + //Get hardware instance. + hal->dev = SIGMADELTA_LL_GET_HW(sigmadelta_num); + sigmadelta_ll_set_en(hal->dev, 1); +} \ No newline at end of file diff --git a/docs/Doxyfile b/docs/Doxyfile index 3009d4b44..2a7d801fa 100644 --- a/docs/Doxyfile +++ b/docs/Doxyfile @@ -108,6 +108,7 @@ INPUT = \ ../../components/soc/include/hal/pcnt_types.h \ ../../components/soc/include/hal/i2s_types.h \ ../../components/soc/include/hal/rtc_io_types.h \ + ../../components/soc/include/hal/sigmadelta_types.h \ ../../components/soc/esp32/include/soc/adc_channel.h \ ../../components/soc/esp32/include/soc/dac_channel.h \ ../../components/soc/esp32/include/soc/touch_channel.h \