1 /*
2  * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/cdefs.h>
10 #include "sdkconfig.h"
11 #if CONFIG_ETM_ENABLE_DEBUG_LOG
12 // The local log level must be defined before including esp_log.h
13 // Set the maximum log level for this source file
14 #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
15 #endif
16 #include "freertos/FreeRTOS.h"
17 #include "driver/gpio.h"
18 #include "driver/gpio_etm.h"
19 #include "esp_heap_caps.h"
20 #include "esp_log.h"
21 #include "esp_check.h"
22 #include "soc/soc_caps.h"
23 #include "hal/gpio_ll.h"
24 #include "hal/gpio_etm_ll.h"
25 #include "esp_private/etm_interface.h"
26 
27 #define ETM_MEM_ALLOC_CAPS   MALLOC_CAP_DEFAULT
28 
29 static const char *TAG = "gpio-etm";
30 
31 typedef struct gpio_etm_task_t gpio_etm_task_t;
32 typedef struct gpio_etm_event_t gpio_etm_event_t;
33 
34 typedef struct gpio_etm_group_t {
35     portMUX_TYPE spinlock;
36     gpio_etm_dev_t *dev;
37     gpio_etm_task_t *tasks[SOC_GPIO_ETM_TASKS_PER_GROUP];
38     gpio_etm_event_t *events[SOC_GPIO_ETM_EVENTS_PER_GROUP];
39 } gpio_etm_group_t;
40 
41 struct gpio_etm_event_t {
42     esp_etm_event_t base;
43     int chan_id;
44     gpio_etm_group_t *group;
45 };
46 
47 struct gpio_etm_task_t {
48     esp_etm_task_t base;
49     int chan_id;
50     gpio_etm_group_t *group;
51     size_t num_of_gpios;     // record the number of GPIOs that are bound to the etm task
52 };
53 
54 static gpio_etm_group_t s_gpio_etm_group = {
55     .dev = &GPIO_ETM,
56     .spinlock = portMUX_INITIALIZER_UNLOCKED,
57 };
58 
gpio_etm_event_register_to_group(gpio_etm_event_t * event)59 static esp_err_t gpio_etm_event_register_to_group(gpio_etm_event_t *event)
60 {
61     gpio_etm_group_t *group = &s_gpio_etm_group;
62     int chan_id = -1;
63     // loop to search free one in the group
64     portENTER_CRITICAL(&group->spinlock);
65     for (int j = 0; j < SOC_GPIO_ETM_EVENTS_PER_GROUP; j++) {
66         if (!group->events[j]) {
67             chan_id = j;
68             group->events[j] = event;
69             break;
70         }
71     }
72     portEXIT_CRITICAL(&group->spinlock);
73 
74     ESP_RETURN_ON_FALSE(chan_id != -1, ESP_ERR_NOT_FOUND, TAG, "no free event channel");
75     event->group = group;
76     event->chan_id = chan_id;
77     return ESP_OK;
78 }
79 
gpio_etm_task_register_to_group(gpio_etm_task_t * task)80 static esp_err_t gpio_etm_task_register_to_group(gpio_etm_task_t *task)
81 {
82     gpio_etm_group_t *group = &s_gpio_etm_group;
83     int chan_id = -1;
84     // loop to search free one in the group
85     portENTER_CRITICAL(&group->spinlock);
86     for (int j = 0; j < SOC_GPIO_ETM_TASKS_PER_GROUP; j++) {
87         if (!group->tasks[j]) {
88             chan_id = j;
89             group->tasks[j] = task;
90             break;
91         }
92     }
93     portEXIT_CRITICAL(&group->spinlock);
94 
95     ESP_RETURN_ON_FALSE(chan_id != -1, ESP_ERR_NOT_FOUND, TAG, "no free task channel");
96     task->group = group;
97     task->chan_id = chan_id;
98     return ESP_OK;
99 }
100 
gpio_etm_event_unregister_from_group(gpio_etm_event_t * event)101 static void gpio_etm_event_unregister_from_group(gpio_etm_event_t *event)
102 {
103     gpio_etm_group_t *group = event->group;
104     int chan_id = event->chan_id;
105     portENTER_CRITICAL(&group->spinlock);
106     group->events[chan_id] = NULL;
107     portEXIT_CRITICAL(&group->spinlock);
108 }
109 
gpio_etm_task_unregister_from_group(gpio_etm_task_t * task)110 static void gpio_etm_task_unregister_from_group(gpio_etm_task_t *task)
111 {
112     gpio_etm_group_t *group = task->group;
113     int chan_id = task->chan_id;
114     portENTER_CRITICAL(&group->spinlock);
115     group->tasks[chan_id] = NULL;
116     portEXIT_CRITICAL(&group->spinlock);
117 }
118 
gpio_etm_event_destroy(gpio_etm_event_t * event)119 static esp_err_t gpio_etm_event_destroy(gpio_etm_event_t *event)
120 {
121     if (event->group) {
122         gpio_etm_event_unregister_from_group(event);
123     }
124     free(event);
125     return ESP_OK;
126 }
127 
gpio_etm_task_destroy(gpio_etm_task_t * task)128 static esp_err_t gpio_etm_task_destroy(gpio_etm_task_t *task)
129 {
130     if (task->group) {
131         gpio_etm_task_unregister_from_group(task);
132     }
133     free(task);
134     return ESP_OK;
135 }
136 
gpio_del_etm_event(esp_etm_event_t * event)137 static esp_err_t gpio_del_etm_event(esp_etm_event_t *event)
138 {
139     gpio_etm_event_t *gpio_event = __containerof(event, gpio_etm_event_t, base);
140     gpio_etm_group_t *group = gpio_event->group;
141     // disable event channel
142     gpio_ll_etm_enable_event_channel(group->dev, gpio_event->chan_id, false);
143     gpio_etm_event_destroy(gpio_event);
144     return ESP_OK;
145 }
146 
gpio_del_etm_task(esp_etm_task_t * task)147 static esp_err_t gpio_del_etm_task(esp_etm_task_t *task)
148 {
149     gpio_etm_task_t *gpio_task = __containerof(task, gpio_etm_task_t, base);
150     // make sure user has called `gpio_etm_task_rm_gpio` to clean the etm task channel
151     ESP_RETURN_ON_FALSE(gpio_task->num_of_gpios == 0, ESP_ERR_INVALID_STATE, TAG, "some GPIO till bounded to the etm task");
152     gpio_etm_task_destroy(gpio_task);
153     return ESP_OK;
154 }
155 
gpio_new_etm_event(const gpio_etm_event_config_t * config,esp_etm_event_handle_t * ret_event)156 esp_err_t gpio_new_etm_event(const gpio_etm_event_config_t *config, esp_etm_event_handle_t *ret_event)
157 {
158 #if CONFIG_ETM_ENABLE_DEBUG_LOG
159     esp_log_level_set(TAG, ESP_LOG_DEBUG);
160 #endif
161     esp_err_t ret = ESP_OK;
162     gpio_etm_event_t *event = NULL;
163     ESP_GOTO_ON_FALSE(config && ret_event, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
164 
165     event = heap_caps_calloc(1, sizeof(gpio_etm_event_t), ETM_MEM_ALLOC_CAPS);
166     ESP_GOTO_ON_FALSE(event, ESP_ERR_NO_MEM, err, TAG, "no mem for event channel");
167     // register the event channel to the group
168     ESP_GOTO_ON_ERROR(gpio_etm_event_register_to_group(event), err, TAG, "register event channel to group failed");
169     int chan_id = event->chan_id;
170 
171     uint32_t event_id = 0;
172     switch (config->edge) {
173     case GPIO_ETM_EVENT_EDGE_ANY:
174         event_id = GPIO_LL_ETM_EVENT_ID_ANY_EDGE(chan_id);
175         break;
176     case GPIO_ETM_EVENT_EDGE_POS:
177         event_id = GPIO_LL_ETM_EVENT_ID_POS_EDGE(chan_id);
178         break;
179     case GPIO_ETM_EVENT_EDGE_NEG:
180         event_id = GPIO_LL_ETM_EVENT_ID_NEG_EDGE(chan_id);
181         break;
182     default:
183         ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "invalid edge");
184     }
185 
186     event->base.del = gpio_del_etm_event;
187     event->base.event_id = event_id;
188     event->base.trig_periph = ETM_TRIG_PERIPH_GPIO;
189     ESP_LOGD(TAG, "new event @%p, event_id=%"PRIu32", chan_id=%d", event, event_id, chan_id);
190     *ret_event = &event->base;
191     return ESP_OK;
192 
193 err:
194     if (event) {
195         gpio_etm_event_destroy(event);
196     }
197     return ret;
198 }
199 
gpio_new_etm_task(const gpio_etm_task_config_t * config,esp_etm_task_handle_t * ret_task)200 esp_err_t gpio_new_etm_task(const gpio_etm_task_config_t *config, esp_etm_task_handle_t *ret_task)
201 {
202 #if CONFIG_ETM_ENABLE_DEBUG_LOG
203     esp_log_level_set(TAG, ESP_LOG_DEBUG);
204 #endif
205     esp_err_t ret = ESP_OK;
206     gpio_etm_task_t *task = NULL;
207     ESP_GOTO_ON_FALSE(config && ret_task, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
208 
209     task = heap_caps_calloc(1, sizeof(gpio_etm_task_t), ETM_MEM_ALLOC_CAPS);
210     ESP_GOTO_ON_FALSE(task, ESP_ERR_NO_MEM, err, TAG, "no mem for task channel");
211     // register the task channel to the group
212     ESP_GOTO_ON_ERROR(gpio_etm_task_register_to_group(task), err, TAG, "register task channel to group failed");
213     int chan_id = task->chan_id;
214 
215     uint32_t task_id = 0;
216     switch (config->action) {
217     case GPIO_ETM_TASK_ACTION_SET:
218         task_id = GPIO_LL_ETM_TASK_ID_SET(chan_id);
219         break;
220     case GPIO_ETM_TASK_ACTION_CLR:
221         task_id = GPIO_LL_ETM_TASK_ID_CLR(chan_id);
222         break;
223     case GPIO_ETM_TASK_ACTION_TOG:
224         task_id = GPIO_LL_ETM_TASK_ID_TOG(chan_id);
225         break;
226     default:
227         ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "invalid action");
228     }
229 
230     task->base.del = gpio_del_etm_task;
231     task->base.task_id = task_id;
232     task->base.trig_periph = ETM_TRIG_PERIPH_GPIO;
233     ESP_LOGD(TAG, "new task @%p, task_id=%"PRIu32", chan_id=%d", task, task_id, chan_id);
234     *ret_task = &task->base;
235     return ESP_OK;
236 
237 err:
238     if (task) {
239         gpio_etm_task_destroy(task);
240     }
241     return ret;
242 }
243 
gpio_etm_event_bind_gpio(esp_etm_event_handle_t event,int gpio_num)244 esp_err_t gpio_etm_event_bind_gpio(esp_etm_event_handle_t event, int gpio_num)
245 {
246     ESP_RETURN_ON_FALSE(event, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
247     ESP_RETURN_ON_FALSE(event->trig_periph == ETM_TRIG_PERIPH_GPIO, ESP_ERR_INVALID_ARG, TAG, "not a gpio etm event");
248     ESP_RETURN_ON_FALSE(GPIO_IS_VALID_GPIO(gpio_num), ESP_ERR_INVALID_ARG, TAG, "gpio is not input capable");
249     gpio_etm_event_t *gpio_event = __containerof(event, gpio_etm_event_t, base);
250     gpio_etm_group_t *group = gpio_event->group;
251     // disable gpio etm event channel first
252     gpio_ll_etm_enable_event_channel(group->dev, gpio_event->chan_id, false);
253     // then set the gpio number
254     gpio_ll_etm_event_channel_set_gpio(group->dev, gpio_event->chan_id, gpio_num);
255     // enable gpio etm event channel again
256     gpio_ll_etm_enable_event_channel(group->dev, gpio_event->chan_id, true);
257     return ESP_OK;
258 }
259 
gpio_etm_task_add_gpio(esp_etm_task_handle_t task,int gpio_num)260 esp_err_t gpio_etm_task_add_gpio(esp_etm_task_handle_t task, int gpio_num)
261 {
262     ESP_RETURN_ON_FALSE(task, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
263     ESP_RETURN_ON_FALSE(task->trig_periph == ETM_TRIG_PERIPH_GPIO, ESP_ERR_INVALID_ARG, TAG, "not a gpio etm task");
264     ESP_RETURN_ON_FALSE(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), ESP_ERR_INVALID_ARG, TAG, "gpio is not output capable");
265     gpio_etm_task_t *gpio_task = __containerof(task, gpio_etm_task_t, base);
266     gpio_etm_group_t *group = gpio_task->group;
267     bool gpio_not_enabled = true;
268     // use spinlock as this function may be called with different task object in different threads
269     // and the gpio_num might reside in the same register
270     portENTER_CRITICAL(&group->spinlock);
271     // check if the gpio has been enabled
272     if (!gpio_ll_etm_is_task_gpio_enabled(group->dev, gpio_num)) {
273         gpio_ll_etm_gpio_set_task_channel(group->dev, gpio_num, gpio_task->chan_id);
274         gpio_ll_etm_enable_task_gpio(group->dev, gpio_num, true);
275     } else {
276         gpio_not_enabled = false;
277     }
278     portEXIT_CRITICAL(&group->spinlock);
279     ESP_RETURN_ON_FALSE(gpio_not_enabled, ESP_ERR_INVALID_STATE, TAG, "gpio already enabled by other task channel");
280     gpio_task->num_of_gpios++;
281     return ESP_OK;
282 }
283 
gpio_etm_task_rm_gpio(esp_etm_task_handle_t task,int gpio_num)284 esp_err_t gpio_etm_task_rm_gpio(esp_etm_task_handle_t task, int gpio_num)
285 {
286     ESP_RETURN_ON_FALSE(task, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
287     ESP_RETURN_ON_FALSE(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), ESP_ERR_INVALID_ARG, TAG, "gpio is not output capable");
288     gpio_etm_task_t *gpio_task = __containerof(task, gpio_etm_task_t, base);
289     gpio_etm_group_t *group = gpio_task->group;
290     bool gpio_enabled_by_this_task = true;
291     // use spinlock as this function may be called with different task object in different threads
292     // and the gpio_num might reside in the same register
293     portENTER_CRITICAL(&group->spinlock);
294     // check if the gpio is managed by this etm task channel
295     if (gpio_ll_etm_is_task_gpio_enabled(group->dev, gpio_num) &&
296             (gpio_ll_etm_gpio_get_task_channel(group->dev, gpio_num) == gpio_task->chan_id)) {
297         gpio_ll_etm_enable_task_gpio(group->dev, gpio_num, false);
298     } else {
299         gpio_enabled_by_this_task = false;
300     }
301     portEXIT_CRITICAL(&group->spinlock);
302     ESP_RETURN_ON_FALSE(gpio_enabled_by_this_task, ESP_ERR_INVALID_STATE, TAG, "gpio is not enabled by this task channel");
303     gpio_task->num_of_gpios--;
304     return ESP_OK;
305 }
306