sdm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <sys/lock.h>
  8. #include "sdkconfig.h"
  9. #if CONFIG_SDM_ENABLE_DEBUG_LOG
  10. // The local log level must be defined before including esp_log.h
  11. // Set the maximum log level for this source file
  12. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  13. #endif
  14. #include "freertos/FreeRTOS.h"
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #include "esp_heap_caps.h"
  18. #include "esp_log.h"
  19. #include "esp_check.h"
  20. #include "esp_pm.h"
  21. #include "driver/gpio.h"
  22. #include "driver/sdm.h"
  23. #include "hal/gpio_hal.h"
  24. #include "hal/sdm_hal.h"
  25. #include "hal/sdm_ll.h"
  26. #include "soc/sdm_periph.h"
  27. #include "esp_private/esp_clk.h"
  28. #if CONFIG_SDM_CTRL_FUNC_IN_IRAM
  29. #define SDM_MEM_ALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
  30. #else
  31. #define SDM_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
  32. #endif
  33. #define SDM_PM_LOCK_NAME_LEN_MAX 16
  34. static const char *TAG = "sdm";
  35. typedef struct sdm_platform_t sdm_platform_t;
  36. typedef struct sdm_group_t sdm_group_t;
  37. typedef struct sdm_channel_t sdm_channel_t;
  38. struct sdm_platform_t {
  39. _lock_t mutex; // platform level mutex lock
  40. sdm_group_t *groups[SOC_SDM_GROUPS]; // sdm group pool
  41. int group_ref_counts[SOC_SDM_GROUPS]; // reference count used to protect group install/uninstall
  42. };
  43. struct sdm_group_t {
  44. int group_id; // Group ID, index from 0
  45. portMUX_TYPE spinlock; // to protect per-group register level concurrent access
  46. sdm_hal_context_t hal; // hal context
  47. sdm_channel_t *channels[SOC_SDM_CHANNELS_PER_GROUP]; // array of sdm channels
  48. sdm_clock_source_t clk_src; // Clock source
  49. };
  50. typedef enum {
  51. SDM_FSM_INIT,
  52. SDM_FSM_ENABLE,
  53. } sdm_fsm_t;
  54. struct sdm_channel_t {
  55. sdm_group_t *group; // which group the sdm channel belongs to
  56. uint32_t chan_id; // allocated channel numerical ID
  57. int gpio_num; // GPIO number
  58. uint32_t sample_rate_hz; // Sample rate, in Hz
  59. portMUX_TYPE spinlock; // to protect per-channels resources concurrently accessed by task and ISR handler
  60. esp_pm_lock_handle_t pm_lock; // PM lock, for glitch filter, as that module can only be functional under APB
  61. sdm_fsm_t fsm; // FSM state
  62. #if CONFIG_PM_ENABLE
  63. char pm_lock_name[SDM_PM_LOCK_NAME_LEN_MAX]; // pm lock name
  64. #endif
  65. };
  66. // sdm driver platform, it's always a singleton
  67. static sdm_platform_t s_platform;
  68. static sdm_group_t *sdm_acquire_group_handle(int group_id)
  69. {
  70. bool new_group = false;
  71. sdm_group_t *group = NULL;
  72. // prevent install sdm group concurrently
  73. _lock_acquire(&s_platform.mutex);
  74. if (!s_platform.groups[group_id]) {
  75. group = heap_caps_calloc(1, sizeof(sdm_group_t), SDM_MEM_ALLOC_CAPS);
  76. if (group) {
  77. new_group = true;
  78. s_platform.groups[group_id] = group; // register to platform
  79. // initialize sdm group members
  80. group->group_id = group_id;
  81. group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  82. group->clk_src = 0;
  83. // initialize HAL context
  84. sdm_hal_init(&group->hal, group_id);
  85. // enable clock
  86. // note that, this will enables all the channels' output, and channel can't be disable/enable separately
  87. sdm_ll_enable_clock(group->hal.dev, true);
  88. }
  89. } else {
  90. group = s_platform.groups[group_id];
  91. }
  92. if (group) {
  93. // someone acquired the group handle means we have a new object that refer to this group
  94. s_platform.group_ref_counts[group_id]++;
  95. }
  96. _lock_release(&s_platform.mutex);
  97. if (new_group) {
  98. ESP_LOGD(TAG, "new group (%d) at %p", group_id, group);
  99. }
  100. return group;
  101. }
  102. static void sdm_release_group_handle(sdm_group_t *group)
  103. {
  104. int group_id = group->group_id;
  105. bool do_deinitialize = false;
  106. _lock_acquire(&s_platform.mutex);
  107. s_platform.group_ref_counts[group_id]--;
  108. if (s_platform.group_ref_counts[group_id] == 0) {
  109. assert(s_platform.groups[group_id]);
  110. do_deinitialize = true;
  111. s_platform.groups[group_id] = NULL; // deregister from platform
  112. sdm_ll_enable_clock(group->hal.dev, false);
  113. }
  114. _lock_release(&s_platform.mutex);
  115. if (do_deinitialize) {
  116. free(group);
  117. ESP_LOGD(TAG, "del group (%d)", group_id);
  118. }
  119. }
  120. static esp_err_t sdm_register_to_group(sdm_channel_t *chan)
  121. {
  122. sdm_group_t *group = NULL;
  123. int chan_id = -1;
  124. for (int i = 0; i < SOC_SDM_GROUPS; i++) {
  125. group = sdm_acquire_group_handle(i);
  126. ESP_RETURN_ON_FALSE(group, ESP_ERR_NO_MEM, TAG, "no mem for group (%d)", i);
  127. // loop to search free unit in the group
  128. portENTER_CRITICAL(&group->spinlock);
  129. for (int j = 0; j < SOC_SDM_CHANNELS_PER_GROUP; j++) {
  130. if (!group->channels[j]) {
  131. chan_id = j;
  132. group->channels[j] = chan;
  133. break;
  134. }
  135. }
  136. portEXIT_CRITICAL(&group->spinlock);
  137. if (chan_id < 0) {
  138. sdm_release_group_handle(group);
  139. group = NULL;
  140. } else {
  141. chan->group = group;
  142. chan->chan_id = chan_id;
  143. break;
  144. }
  145. }
  146. ESP_RETURN_ON_FALSE(chan_id != -1, ESP_ERR_NOT_FOUND, TAG, "no free channels");
  147. return ESP_OK;
  148. }
  149. static void sdm_unregister_from_group(sdm_channel_t *chan)
  150. {
  151. sdm_group_t *group = chan->group;
  152. int chan_id = chan->chan_id;
  153. portENTER_CRITICAL(&group->spinlock);
  154. group->channels[chan_id] = NULL;
  155. portEXIT_CRITICAL(&group->spinlock);
  156. // channel has a reference on group, release it now
  157. sdm_release_group_handle(group);
  158. }
  159. static esp_err_t sdm_destory(sdm_channel_t *chan)
  160. {
  161. if (chan->pm_lock) {
  162. ESP_RETURN_ON_ERROR(esp_pm_lock_delete(chan->pm_lock), TAG, "delete pm lock failed");
  163. }
  164. if (chan->group) {
  165. sdm_unregister_from_group(chan);
  166. }
  167. free(chan);
  168. return ESP_OK;
  169. }
  170. esp_err_t sdm_new_channel(const sdm_config_t *config, sdm_channel_handle_t *ret_chan)
  171. {
  172. #if CONFIG_SDM_ENABLE_DEBUG_LOG
  173. esp_log_level_set(TAG, ESP_LOG_DEBUG);
  174. #endif
  175. esp_err_t ret = ESP_OK;
  176. sdm_channel_t *chan = NULL;
  177. ESP_GOTO_ON_FALSE(config && ret_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
  178. ESP_GOTO_ON_FALSE(GPIO_IS_VALID_OUTPUT_GPIO(config->gpio_num), ESP_ERR_INVALID_ARG, err, TAG, "invalid GPIO number");
  179. chan = heap_caps_calloc(1, sizeof(sdm_channel_t), SDM_MEM_ALLOC_CAPS);
  180. ESP_GOTO_ON_FALSE(chan, ESP_ERR_NO_MEM, err, TAG, "no mem for channel");
  181. // register channel to the group
  182. ESP_GOTO_ON_ERROR(sdm_register_to_group(chan), err, TAG, "register to group failed");
  183. sdm_group_t *group = chan->group;
  184. int group_id = group->group_id;
  185. int chan_id = chan->chan_id;
  186. ESP_GOTO_ON_FALSE(group->clk_src == 0 || group->clk_src == config->clk_src, ESP_ERR_INVALID_ARG, err, TAG, "clock source conflict");
  187. uint32_t src_clk_hz = 0;
  188. switch (config->clk_src) {
  189. case SDM_CLK_SRC_APB:
  190. src_clk_hz = esp_clk_apb_freq();
  191. #if CONFIG_PM_ENABLE
  192. sprintf(chan->pm_lock_name, "sdm_%d_%d", group->group_id, chan_id); // e.g. sdm_0_0
  193. ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, chan->pm_lock_name, &chan->pm_lock);
  194. ESP_RETURN_ON_ERROR(ret, TAG, "create APB_FREQ_MAX lock failed");
  195. #endif
  196. break;
  197. default:
  198. ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "clock source %d is not support", config->clk_src);
  199. break;
  200. }
  201. group->clk_src = config->clk_src;
  202. // GPIO configuration
  203. gpio_config_t gpio_conf = {
  204. .intr_type = GPIO_INTR_DISABLE,
  205. // also enable the input path is `io_loop_back` is on, this is useful for debug
  206. .mode = GPIO_MODE_OUTPUT | (config->flags.io_loop_back ? GPIO_MODE_INPUT : 0),
  207. .pull_down_en = false,
  208. .pull_up_en = true,
  209. .pin_bit_mask = 1ULL << config->gpio_num,
  210. };
  211. ESP_GOTO_ON_ERROR(gpio_config(&gpio_conf), err, TAG, "config GPIO failed");
  212. esp_rom_gpio_connect_out_signal(config->gpio_num, sigma_delta_periph_signals.channels[chan_id].sd_sig, config->flags.invert_out, false);
  213. chan->gpio_num = config->gpio_num;
  214. // set prescale based on sample rate
  215. uint32_t prescale = src_clk_hz / config->sample_rate_hz;
  216. sdm_ll_set_prescale(group->hal.dev, chan_id, prescale);
  217. chan->sample_rate_hz = src_clk_hz / prescale;
  218. // preset the duty cycle to zero
  219. sdm_ll_set_duty(group->hal.dev, chan_id, 0);
  220. // initialize other members of timer
  221. chan->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  222. chan->fsm = SDM_FSM_INIT; // put the channel into init state
  223. ESP_LOGD(TAG, "new sdm channel (%d,%d) at %p, gpio=%d, sample rate=%"PRIu32"Hz", group_id, chan_id, chan, chan->gpio_num, chan->sample_rate_hz);
  224. *ret_chan = chan;
  225. return ESP_OK;
  226. err:
  227. if (chan) {
  228. sdm_destory(chan);
  229. }
  230. return ret;
  231. }
  232. esp_err_t sdm_del_channel(sdm_channel_handle_t chan)
  233. {
  234. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  235. ESP_RETURN_ON_FALSE(chan->fsm == SDM_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "channel not in init state");
  236. sdm_group_t *group = chan->group;
  237. int group_id = group->group_id;
  238. int chan_id = chan->chan_id;
  239. ESP_LOGD(TAG, "del channel (%d,%d)", group_id, chan_id);
  240. // recycle memory resource
  241. ESP_RETURN_ON_ERROR(sdm_destory(chan), TAG, "destory channel failed");
  242. return ESP_OK;
  243. }
  244. esp_err_t sdm_channel_enable(sdm_channel_handle_t chan)
  245. {
  246. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  247. ESP_RETURN_ON_FALSE(chan->fsm == SDM_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "channel not in init state");
  248. // acquire power manager lock
  249. if (chan->pm_lock) {
  250. ESP_RETURN_ON_ERROR(esp_pm_lock_acquire(chan->pm_lock), TAG, "acquire pm_lock failed");
  251. }
  252. chan->fsm = SDM_FSM_ENABLE;
  253. return ESP_OK;
  254. }
  255. esp_err_t sdm_channel_disable(sdm_channel_handle_t chan)
  256. {
  257. ESP_RETURN_ON_FALSE(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  258. ESP_RETURN_ON_FALSE(chan->fsm == SDM_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "channel not in enable state");
  259. // release power manager lock
  260. if (chan->pm_lock) {
  261. ESP_RETURN_ON_ERROR(esp_pm_lock_release(chan->pm_lock), TAG, "release pm_lock failed");
  262. }
  263. chan->fsm = SDM_FSM_INIT;
  264. return ESP_OK;
  265. }
  266. esp_err_t sdm_channel_set_duty(sdm_channel_handle_t chan, int8_t duty)
  267. {
  268. ESP_RETURN_ON_FALSE_ISR(chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  269. sdm_group_t *group = chan->group;
  270. int chan_id = chan->chan_id;
  271. portENTER_CRITICAL_SAFE(&chan->spinlock);
  272. sdm_ll_set_duty(group->hal.dev, chan_id, duty);
  273. portEXIT_CRITICAL_SAFE(&chan->spinlock);
  274. return ESP_OK;
  275. }