sdm.c 12 KB

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