Skip to content

Commit 69a6616

Browse files
committed
queue and stepper init functions return bool and accept engine
1 parent 260dc21 commit 69a6616

12 files changed

+47
-37
lines changed

src/FastAccelStepper.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ FastAccelStepper* FastAccelStepperEngine::stepperConnectToPin(
119119
_stepper_cnt++;
120120

121121
FastAccelStepper* s = &fas_stepper[fas_stepper_num];
122+
bool success = s->init(this, fas_stepper_num, step_pin);
123+
if (!success) {
124+
return NULL;
125+
}
122126
_stepper[fas_stepper_num] = s;
123-
s->init(this, fas_stepper_num, step_pin);
124127
for (uint8_t i = 0; i < MAX_STEPPER; i++) {
125128
FastAccelStepper* sx = _stepper[i];
126129
if (sx) {
@@ -501,7 +504,7 @@ bool FastAccelStepper::usesAutoEnablePin(uint8_t pin) {
501504
return false;
502505
}
503506

504-
void FastAccelStepper::init(FastAccelStepperEngine* engine, uint8_t num,
507+
bool FastAccelStepper::init(FastAccelStepperEngine* engine, uint8_t num,
505508
uint8_t step_pin) {
506509
#if (TEST_MEASURE_ISR_SINGLE_FILL == 1)
507510
// For run time measurement
@@ -522,20 +525,14 @@ void FastAccelStepper::init(FastAccelStepperEngine* engine, uint8_t num,
522525
_rg.init();
523526

524527
_queue_num = num;
525-
fas_queue[_queue_num].init(_queue_num, step_pin);
526-
#if defined(SUPPORT_RP_PICO)
527-
bool ok = fas_queue[_queue_num].claim_pio_sm(engine);
528-
if (ok) {
529-
fas_queue[_queue_num].setupSM();
530-
fas_queue[_queue_num].connect();
531-
}
532-
#endif
528+
bool success = fas_queue[_queue_num].init(engine, _queue_num, step_pin);
533529
#if defined(SUPPORT_ESP32_PULSE_COUNTER) && (ESP_IDF_VERSION_MAJOR == 5)
534530
_attached_pulse_unit = NULL;
535531
#endif
536532
#if defined(SUPPORT_ESP32_PULSE_COUNTER) && (ESP_IDF_VERSION_MAJOR == 4)
537533
_attached_pulse_cnt_unit = -1;
538534
#endif
535+
return success;
539536
}
540537
uint8_t FastAccelStepper::getStepPin() { return _stepPin; }
541538
void FastAccelStepper::setDirectionPin(uint8_t dirPin, bool dirHighCountsUp,

src/FastAccelStepper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class FastAccelStepper {
270270
#else
271271
private:
272272
#endif
273-
void init(FastAccelStepperEngine* engine, uint8_t num, uint8_t step_pin);
273+
bool init(FastAccelStepperEngine* engine, uint8_t num, uint8_t step_pin);
274274

275275
public:
276276
// ## Step Pin

src/StepperISR.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class StepperQueue {
122122
struct queue_end_s queue_end;
123123
uint16_t max_speed_in_ticks;
124124

125-
void init(uint8_t queue_num, uint8_t step_pin);
125+
bool init(FastAccelStepperEngine *engine, uint8_t queue_num, uint8_t step_pin);
126126
inline uint8_t queueEntries() {
127127
fasDisableInterrupts();
128128
uint8_t rp = read_idx;
@@ -162,7 +162,7 @@ class StepperQueue {
162162

163163
#ifdef SUPPORT_ESP32_MCPWM_PCNT
164164
bool isReadyForCommands_mcpwm_pcnt();
165-
void init_mcpwm_pcnt(uint8_t channel_num, uint8_t step_pin);
165+
bool init_mcpwm_pcnt(uint8_t channel_num, uint8_t step_pin);
166166
void startQueue_mcpwm_pcnt();
167167
void forceStop_mcpwm_pcnt();
168168
uint16_t _getPerformedPulses_mcpwm_pcnt();
@@ -171,7 +171,7 @@ class StepperQueue {
171171
#endif
172172
#ifdef SUPPORT_ESP32_RMT
173173
bool isReadyForCommands_rmt();
174-
void init_rmt(uint8_t channel_num, uint8_t step_pin);
174+
bool init_rmt(uint8_t channel_num, uint8_t step_pin);
175175
void startQueue_rmt();
176176
#if ESP_IDF_VERSION_MAJOR == 4
177177
void stop_rmt(bool both);

src/StepperISR_avr.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ StepperQueue fas_queue[NUM_QUEUES];
115115
/* ensure cyclic interrupt is running */ \
116116
EnableOverflowInterrupt(T); \
117117
}
118-
void StepperQueue::init(uint8_t queue_num, uint8_t step_pin) {
118+
bool StepperQueue::init(FastAccelStepperEngine *engine, uint8_t queue_num, uint8_t step_pin) {
119119
prepareISRtimeMeasurement();
120120
_initVars();
121121
digitalWrite(step_pin, LOW);
@@ -124,16 +124,20 @@ void StepperQueue::init(uint8_t queue_num, uint8_t step_pin) {
124124
channel = channelA;
125125
AVR_INIT(FAS_TIMER_MODULE, A)
126126
}
127-
if (step_pin == stepPinStepperB) {
127+
else if (step_pin == stepPinStepperB) {
128128
channel = channelB;
129129
AVR_INIT(FAS_TIMER_MODULE, B)
130130
}
131131
#ifdef stepPinStepperC
132-
if (step_pin == stepPinStepperC) {
132+
else if (step_pin == stepPinStepperC) {
133133
channel = channelC;
134134
AVR_INIT(FAS_TIMER_MODULE, C)
135135
}
136136
#endif
137+
else {
138+
return false;
139+
}
140+
return true;
137141
}
138142

139143
// The interrupt is called on compare event, which eventually

src/StepperISR_due.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ inline uint32_t pinToChannel(uint32_t pin) {
452452
return 0xFFFFFFFF;
453453
}
454454

455-
void StepperQueue::init(uint8_t queue_num, uint8_t step_pin) {
455+
bool StepperQueue::init(FastAccelStepperEngine *engine, uint8_t queue_num, uint8_t step_pin) {
456456
_queue_num = queue_num;
457457
driver_data = (void*)&gChannelMap[queue_num];
458458
_initVars();
@@ -521,6 +521,7 @@ void StepperQueue::init(uint8_t queue_num, uint8_t step_pin) {
521521
PWM_INTERFACE->PWM_IDR1 = 0x00FFFF0F;
522522

523523
connect();
524+
return true;
524525
}
525526

526527
void StepperQueue::connect() {

src/StepperISR_esp32.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
// Here are the global variables to interface with the interrupts
66
StepperQueue fas_queue[NUM_QUEUES];
77

8-
void StepperQueue::init(uint8_t queue_num, uint8_t step_pin) {
8+
bool StepperQueue::init(FastAccelStepperEngine *engine, uint8_t queue_num, uint8_t step_pin) {
99
uint8_t channel = queue_num;
1010
#ifdef SUPPORT_ESP32_MCPWM_PCNT
1111
if (channel < QUEUES_MCPWM_PCNT) {
1212
use_rmt = false;
13-
init_mcpwm_pcnt(channel, step_pin);
14-
return;
13+
return init_mcpwm_pcnt(channel, step_pin);
1514
}
1615
channel -= QUEUES_MCPWM_PCNT;
1716
#endif
1817
#ifdef SUPPORT_ESP32_RMT
1918
use_rmt = true;
20-
init_rmt(channel, step_pin);
19+
return init_rmt(channel, step_pin);
2120
#endif
21+
return false;
2222
}
2323

2424
void StepperQueue::connect() {

src/StepperISR_idf4_esp32_mcpwm_pcnt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ static void IRAM_ATTR mcpwm1_isr_service(void *arg) {
344344
#endif
345345
}
346346

347-
void StepperQueue::init_mcpwm_pcnt(uint8_t channel_num, uint8_t step_pin) {
347+
bool StepperQueue::init_mcpwm_pcnt(uint8_t channel_num, uint8_t step_pin) {
348348
#ifdef TEST_PROBE
349349
pinMode(TEST_PROBE, OUTPUT);
350350
#endif
@@ -474,6 +474,7 @@ void StepperQueue::init_mcpwm_pcnt(uint8_t channel_num, uint8_t step_pin) {
474474

475475
// at last, link mcpwm to output pin and back into pcnt input
476476
connect();
477+
return true;
477478
}
478479

479480
void StepperQueue::connect_mcpwm_pcnt() {

src/StepperISR_idf4_esp32_rmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void IRAM_ATTR tx_intr_handler(void *arg) {
257257
#endif
258258
}
259259

260-
void StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
260+
bool StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
261261
#ifdef TEST_PROBE_1
262262
if (channel_num == 0) {
263263
pinMode(TEST_PROBE_1, OUTPUT);
@@ -359,6 +359,7 @@ void StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
359359
}
360360
}
361361
#endif
362+
return true;
362363
}
363364

364365
void StepperQueue::connect_rmt() {

src/StepperISR_idf4_esp32c3_rmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static void IRAM_ATTR tx_intr_handler(void *arg) {
318318
#endif
319319
}
320320

321-
void StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
321+
bool StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
322322
#ifdef TEST_PROBE_1
323323
if (channel_num == 0) {
324324
pinMode(TEST_PROBE_1, OUTPUT);
@@ -444,6 +444,7 @@ void StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
444444
}
445445
}
446446
#endif
447+
return true;
447448
}
448449

449450
void StepperQueue::connect_rmt() {

src/StepperISR_idf4_esp32s3_rmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static void IRAM_ATTR tx_intr_handler(void *arg) {
318318
#endif
319319
}
320320

321-
void StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
321+
bool StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
322322
#ifdef TEST_PROBE_1
323323
if (channel_num == 0) {
324324
pinMode(TEST_PROBE_1, OUTPUT);
@@ -444,6 +444,7 @@ void StepperQueue::init_rmt(uint8_t channel_num, uint8_t step_pin) {
444444
}
445445
}
446446
#endif
447+
return true;
447448
}
448449

449450
void StepperQueue::connect_rmt() {

0 commit comments

Comments
 (0)