-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedcStepper.cpp
More file actions
356 lines (307 loc) · 11.3 KB
/
LedcStepper.cpp
File metadata and controls
356 lines (307 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "LedcStepper.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "driver/ledc.h"
#include "driver/pulse_cnt.h"
#include "soc/pcnt_periph.h"
#include "soc/gpio_sig_map.h"
#include <rom/gpio.h>
static const char *TAG = "LedcStepper";
#define PCNT_HIGH_LIMIT 32000
#define PCNT_LOW_LIMIT -32000
// Why the hell, does espressif think, that the unit and channel id are not
// needed ? Without unit/channel ID, the needed parameter for
// gpio_matrix_in/gpio_iomux_in cannot be derived.
//
// Here we declare the private pcnt_chan_t structure, which is not save.
struct pcnt_unit_t
{
/*pcnt_group_t*/ void *group;
portMUX_TYPE spinlock;
int unit_id;
// remainder of struct not needed
};
struct pcnt_chan_t
{
pcnt_unit_t *unit;
int channel_id;
// remainder of struct not needed
};
uint8_t LedcStepper::_next_timer_channel = 0;
SemaphoreHandle_t LedcStepper::_pcnt_mutex = xSemaphoreCreateMutex();
bool LedcStepper::_fade_func_installed = false;
#define HALF_DUTY 128
bool IRAM_ATTR pcnt_on_reach(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx)
{
LedcStepper *stepper = static_cast<LedcStepper *>(user_ctx);
vTaskNotifyGiveFromISR(stepper->_task_to_notify_handle, NULL);
return false;
}
void stop_task(void *pvParameter)
{
LedcStepper *stepper = static_cast<LedcStepper *>(pvParameter);
while (1)
{
xTaskNotifyWait(0x00, ULONG_MAX, NULL, portMAX_DELAY);
stepper->stop(true, true);
ESP_LOGI(TAG, "stopTaskWatermark: %d", uxTaskGetStackHighWaterMark(NULL));
}
}
LedcStepper::LedcStepper(uint8_t step_pin, uint8_t dir_pin, uint8_t en_pin, bool invert_dir_pin, UBaseType_t task_priority, UBaseType_t task_stop_priority)
{
_step_pin = step_pin;
_dir_pin = (gpio_num_t)dir_pin;
_invert_dir_pin = invert_dir_pin;
_en_pin = (gpio_num_t)en_pin;
if (_next_timer_channel >= LEDC_TIMER_MAX)
ESP_LOGE(TAG, "No more timers available");
if (_next_timer_channel >= LEDC_CHANNEL_MAX)
ESP_LOGE(TAG, "No more channels available");
_timer = (ledc_timer_t)_next_timer_channel;
_channel = (ledc_channel_t)_next_timer_channel;
++_next_timer_channel;
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_8_BIT,
.timer_num = _timer,
.freq_hz = 100,
.clk_cfg = LEDC_AUTO_CLK,
.deconfigure = false};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.gpio_num = _step_pin,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = _channel,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = _timer,
.duty = 0,
.hpoint = 0,
.flags = {.output_invert = 1}};
ledc_channel_config(&ledc_channel);
if (!_fade_func_installed)
{
ledc_fade_func_install(0);
_fade_func_installed = true;
}
pcnt_unit_config_t unit_config = {
.low_limit = PCNT_LOW_LIMIT,
.high_limit = PCNT_HIGH_LIMIT,
.intr_priority = 0,
.flags = {.accum_count = 1}};
ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &_pcnt_unit));
pcnt_chan_config_t chan_config = {
.edge_gpio_num = _step_pin,
.level_gpio_num = _dir_pin,
.flags = {
.invert_edge_input = 0,
.invert_level_input = 0,
.virt_edge_io_level = 0,
.virt_level_io_level = 0,
.io_loop_back = 0,
}};
ESP_ERROR_CHECK(pcnt_new_channel(_pcnt_unit, &chan_config, &_pcnt_chan));
ESP_ERROR_CHECK(pcnt_channel_set_edge_action(_pcnt_chan, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD));
if (_invert_dir_pin)
ESP_ERROR_CHECK(pcnt_channel_set_level_action(_pcnt_chan, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_INVERSE));
else
ESP_ERROR_CHECK(pcnt_channel_set_level_action(_pcnt_chan, PCNT_CHANNEL_LEVEL_ACTION_INVERSE, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
pcnt_event_callbacks_t cbs = {
.on_reach = pcnt_on_reach,
};
ESP_ERROR_CHECK(pcnt_unit_register_event_callbacks(_pcnt_unit, &cbs, this));
ESP_ERROR_CHECK(pcnt_unit_add_watch_point(_pcnt_unit, PCNT_HIGH_LIMIT));
ESP_ERROR_CHECK(pcnt_unit_add_watch_point(_pcnt_unit, PCNT_LOW_LIMIT));
ESP_ERROR_CHECK(pcnt_unit_enable(_pcnt_unit));
ESP_ERROR_CHECK(pcnt_unit_start(_pcnt_unit));
int unit_id = _pcnt_unit->unit_id;
int channel_id = _pcnt_chan->channel_id;
// int signal = pcnt_periph_signals.groups[0]
// .units[unit_id]
// .channels[channel_id]
// .pulse_sig;
// gpio_matrix_in(step_pin, signal, 0);
// gpio_iomux_in(step_pin, signal);
gpio_reset_pin(_dir_pin);
gpio_set_direction(_dir_pin, GPIO_MODE_OUTPUT);
int control = pcnt_periph_signals.groups[0]
.units[unit_id]
.channels[channel_id]
.control_sig;
gpio_iomux_out(dir_pin, 0x100, false);
gpio_matrix_in(dir_pin, control, 0);
gpio_iomux_in(dir_pin, control);
gpio_set_direction((gpio_num_t)_step_pin, GPIO_MODE_INPUT_OUTPUT);
gpio_matrix_out(_step_pin, LEDC_LS_SIG_OUT0_IDX + _channel, 0, 0);
gpio_reset_pin(_en_pin);
gpio_set_direction(_en_pin, GPIO_MODE_OUTPUT);
_task_priority = task_priority;
_task_stop_priority = task_stop_priority;
disable();
xTaskCreate(stop_task, "ledc_stepperStopTask", 8192 * 4, (void *)this, _task_priority, &_stop_task_handle);
_task_to_notify_handle = _stop_task_handle; // default value
}
void LedcStepper::step(int32_t position, uint32_t speed, bool wait, bool reset)
{
if (position == 0)
return;
UBaseType_t prvPriority = uxTaskPriorityGet(NULL);
vTaskPrioritySet(NULL, _task_priority);
if (!_enabled)
ESP_LOGE(TAG, "Stepper not enabled !");
if (_running)
{
if ((!reset) && _pcnt_objective)
{
position += _pcnt_objective;
}
stop(false);
}
gpio_set_level(_dir_pin, (position < 0) ? !_invert_dir_pin : _invert_dir_pin);
_pcnt_objective = position;
if (position > 0)
_pcnt_watch_point = (position < PCNT_HIGH_LIMIT) ? position : PCNT_HIGH_LIMIT;
else
_pcnt_watch_point = (position > PCNT_LOW_LIMIT) ? position : PCNT_LOW_LIMIT;
if (_pcnt_watch_point != PCNT_HIGH_LIMIT && _pcnt_watch_point != PCNT_LOW_LIMIT)
{
xSemaphoreTake(_pcnt_mutex, portMAX_DELAY);
pcnt_unit_add_watch_point(_pcnt_unit, _pcnt_watch_point);
pcnt_unit_clear_count(_pcnt_unit);
xSemaphoreGive(_pcnt_mutex);
}
ledc_set_freq(LEDC_LOW_SPEED_MODE, _timer, speed);
_task_to_notify_handle = wait ? xTaskGetCurrentTaskHandle() : _stop_task_handle;
_running = true;
ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE, _channel, HALF_DUTY, 0);
ESP_LOGI(TAG, "activated ledc output");
if (wait)
{
do
{
ESP_LOGI(TAG, "waiting");
xTaskNotifyWait(0x00, ULONG_MAX, NULL, portMAX_DELAY);
ESP_LOGI(TAG, "stopped waiting");
stop(true, true);
ESP_LOGI(TAG, "stopped ledc output");
} while (_running);
}
vTaskPrioritySet(NULL, prvPriority);
}
void LedcStepper::go_to(int32_t position, uint32_t speed, bool wait)
{
UBaseType_t prvPriority = uxTaskPriorityGet(NULL);
vTaskPrioritySet(NULL, _task_priority);
if (!_enabled)
ESP_LOGE(TAG, "Stepper not enabled !");
if (_running)
stop(false);
step(position - _position, speed, wait, true);
vTaskPrioritySet(NULL, prvPriority);
}
void LedcStepper::step_speed(int32_t speed)
{
UBaseType_t prvPriority = uxTaskPriorityGet(NULL);
vTaskPrioritySet(NULL, _task_priority);
if (!_enabled)
ESP_LOGE(TAG, "Stepper not enabled !");
if (_running)
stop(false);
_running = true;
_last_speed = speed;
gpio_set_level(_dir_pin, (speed < 0) ? !_invert_dir_pin : _invert_dir_pin);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_LOW_SPEED_MODE, _timer, abs(speed)));
ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE, _channel, HALF_DUTY, 0);
vTaskPrioritySet(NULL, prvPriority);
}
void LedcStepper::stop(bool stop_waiting, bool from_ISR) // ISSUE : the stop_waiting mechanism does not work for goto wait
{
UBaseType_t prvPriority = uxTaskPriorityGet(NULL);
vTaskPrioritySet(NULL, _task_stop_priority);
xSemaphoreTake(_pcnt_mutex, portMAX_DELAY);
if ((_pcnt_watch_point && _pcnt_watch_point == _pcnt_objective) || !from_ISR)
{
ledc_set_duty_and_update(LEDC_LOW_SPEED_MODE, _channel, 0, 0);
_running = false;
} // else because counter overflown while speed target
ESP_LOGI(TAG, "Calling stop() - _pcnt_unit: %p, group: %p", _pcnt_unit, _pcnt_unit->group);
int count;
pcnt_unit_get_count(_pcnt_unit, &count);
pcnt_unit_clear_count(_pcnt_unit);
_position += count;
ESP_LOGI(TAG, "Stopping - count:%d, position:%d, pcntwatchpoint:%d, pcnt_objective:%d", count, _position, _pcnt_watch_point, _pcnt_objective);
if (_pcnt_watch_point)
{
// account for possible overflow
if (_pcnt_watch_point != PCNT_HIGH_LIMIT && _pcnt_watch_point != PCNT_LOW_LIMIT)
pcnt_unit_remove_watch_point(_pcnt_unit, _pcnt_watch_point);
if (!(_pcnt_watch_point == _pcnt_objective) && from_ISR)
{
_pcnt_objective -= _pcnt_watch_point;
if (_pcnt_objective > 0)
_pcnt_watch_point = (_pcnt_objective < PCNT_HIGH_LIMIT) ? _pcnt_objective : PCNT_HIGH_LIMIT;
else
_pcnt_watch_point = (_pcnt_objective > PCNT_LOW_LIMIT) ? _pcnt_objective : PCNT_LOW_LIMIT;
ESP_LOGD(TAG, "Trying to add watchpoint: %d", _pcnt_watch_point);
if (_pcnt_watch_point != PCNT_HIGH_LIMIT && _pcnt_watch_point != PCNT_LOW_LIMIT)
{
pcnt_unit_add_watch_point(_pcnt_unit, _pcnt_watch_point);
// pcnt_unit_get_count(_pcnt_unit, &count);
pcnt_unit_clear_count(_pcnt_unit); // necessary to apply the watch point
// Warning : We might loose a couple steps between the two clear_counts !
}
vTaskPrioritySet(NULL, prvPriority);
xSemaphoreGive(_pcnt_mutex);
return;
}
_pcnt_watch_point = 0;
}
if (stop_waiting && _waiting)
{
xTaskNotifyGive(_wait_task_handle);
}
xSemaphoreGive(_pcnt_mutex);
vTaskPrioritySet(NULL, prvPriority);
}
void LedcStepper::wait()
{
if (_running)
{
_wait_task_handle = xTaskGetCurrentTaskHandle();
_waiting = true;
if (_running) // make sure we are still running and it did not change at the wrong time
xTaskNotifyWait(0x00, ULONG_MAX, NULL, portMAX_DELAY);
_waiting = false;
}
}
void LedcStepper::disable()
{
if (_enabled)
{
gpio_set_level(_en_pin, 1);
_enabled = false;
}
}
void LedcStepper::enable()
{
if (!_enabled)
{
gpio_set_level(_en_pin, 0);
_enabled = true;
}
}
int LedcStepper::get_position()
{
if (_running)
{
int count;
xSemaphoreTake(_pcnt_mutex, portMAX_DELAY);
pcnt_unit_get_count(_pcnt_unit, &count);
xSemaphoreGive(_pcnt_mutex);
return _position + count;
}
return _position;
}
void LedcStepper::reset_position()
{
_position = 0;
}