Skip to content

Commit

Permalink
avr: use port pin toggle feature instead read-modify-write by xor por…
Browse files Browse the repository at this point in the history
…t bit
  • Loading branch information
gin66 committed Apr 28, 2024
1 parent 5af160b commit 6d7ae50
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
16 changes: 8 additions & 8 deletions extras/tests/simavr_based/test_sd_04_timing_2560/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
EnableA: 1*L->H, 1*H->L
EnableB: 2*L->H, 1*H->L
EnableC: 2*L->H, 1*H->L
StepA: 64000*L->H, 64000*H->L, Max High=31us Total High=588641us
StepB: 64000*L->H, 64000*H->L, Max High=32us Total High=414769us
StepC: 64000*L->H, 64000*H->L, Max High=40us Total High=431717us
StepA: 64000*L->H, 64000*H->L, Max High=29us Total High=380000us
StepB: 64000*L->H, 64000*H->L, Max High=34us Total High=507758us
StepC: 64000*L->H, 64000*H->L, Max High=40us Total High=629247us
Position[A]=64000

Position[B]=64000
Expand All @@ -17,13 +17,13 @@ Time in EnableA max=233598 us, total=233598 us

Time in EnableB max=246080 us, total=246080 us

Time in EnableC max=254235 us, total=254235 us
Time in EnableC max=254236 us, total=254236 us

Time in FillISR max=2804 us, total=2134900 us
Time in FillISR max=2849 us, total=2127254 us

Time in StepA max=31 us, total=588641 us
Time in StepA max=29 us, total=380000 us

Time in StepB max=32 us, total=414769 us
Time in StepB max=34 us, total=507758 us

Time in StepC max=40 us, total=431717 us
Time in StepC max=40 us, total=629247 us

12 changes: 6 additions & 6 deletions extras/tests/simavr_based/test_sd_04_timing_328p/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
DirB: 1*L->H, 0*H->L
EnableA: 1*L->H, 1*H->L
EnableB: 2*L->H, 1*H->L
StepA: 1000*L->H, 1000*H->L, Max High=14us Total High=5340us
StepB: 1000*L->H, 1000*H->L, Max High=16us Total High=5816us
StepA: 1000*L->H, 1000*H->L, Max High=14us Total High=5335us
StepB: 1000*L->H, 1000*H->L, Max High=16us Total High=5809us
Position[A]=1000

Position[B]=1000
Expand All @@ -12,11 +12,11 @@ Time in EnableA max=225398 us, total=225398 us

Time in EnableB max=238116 us, total=238116 us

Time in FillISR max=2642 us, total=47691 us
Time in FillISR max=2642 us, total=47687 us

Time in StepA max=14 us, total=5340 us
Time in StepA max=14 us, total=5335 us

Time in StepB max=16 us, total=5816 us
Time in StepB max=16 us, total=5809 us

Time in StepISR max=7 us, total=9363 us
Time in StepISR max=7 us, total=9300 us

12 changes: 10 additions & 2 deletions src/StepperISR.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ class StepperQueue {
bool bufferContainsSteps[2];
#endif
#if defined(SUPPORT_DIR_PIN_MASK)
// avr uses uint8_t and sam needs uint32_t
// so make the SUPPORT_DIR_PIN_MASK dual use
volatile SUPPORT_DIR_PIN_MASK* _dirPinPort;
SUPPORT_DIR_PIN_MASK _dirPinMask;
#endif
#if defined(SUPPORT_DIR_TOGGLE_PIN_MASK)
volatile SUPPORT_DIR_TOGGLE_PIN_MASK* _dirTogglePinPort;
SUPPORT_DIR_TOGGLE_PIN_MASK _dirTogglePinMask;
#endif
#if defined(SUPPORT_AVR)
volatile bool _prepareForStop;
volatile bool _isRunning;
Expand Down Expand Up @@ -180,6 +182,12 @@ class StepperQueue {
_dirPinPort = portOutputRegister(digitalPinToPort(dir_pin));
_dirPinMask = digitalPinToBitMask(dir_pin);
}
#endif
#if defined(SUPPORT_DIR_TOGGLE_PIN_MASK)
if ((dir_pin != PIN_UNDEFINED) && ((dir_pin & PIN_EXTERNAL_FLAG) == 0)) {
_dirTogglePinPort = portInputRegister(digitalPinToPort(dir_pin));
_dirTogglePinMask = digitalPinToBitMask(dir_pin);
}
#endif
}
void adjustSpeedToStepperCount(uint8_t steppers);
Expand Down
11 changes: 5 additions & 6 deletions src/StepperISR_avr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#define Stepper_IsDisconnected(T, X) \
((TCCR##T##A & (_BV(COM##T##X##0) | _BV(COM##T##X##1))) == 0)
#define Stepper_IsOneIfOutput(T, X) ((TCCR##T##A & _BV(COM##T##X##0)) != 0)
#define Stepper_ToggleDirection(CHANNEL) *fas_queue_##CHANNEL._dirPinPort ^= fas_queue_##CHANNEL._dirPinMask
#define Stepper_ToggleDirection(CHANNEL) *fas_queue_##CHANNEL._dirTogglePinPort = fas_queue_##CHANNEL._dirTogglePinMask
#define PREPARE_DIRECTION_PIN(CHANNEL) \
if (e->toggle_dir) { \
Stepper_ToggleDirection(CHANNEL); \
}

#ifdef SIMAVR_TIME_MEASUREMENT
#define prepareISRtimeMeasurement() DDRB |= 0x18
Expand Down Expand Up @@ -238,11 +242,6 @@ AVR_CYCLIC_ISR_GEN(FAS_TIMER_MODULE)
rp = fas_queue_##CHANNEL.read_idx; \
e = &fas_queue_##CHANNEL.entry[rp & QUEUE_LEN_MASK];

#define PREPARE_DIRECTION_PIN(CHANNEL) \
if (e->toggle_dir) { \
*fas_queue_##CHANNEL._dirPinPort ^= fas_queue_##CHANNEL._dirPinMask; \
}

#define AVR_START_QUEUE(T, CHANNEL) \
_isRunning = true; \
_prepareForStop = false; \
Expand Down
2 changes: 1 addition & 1 deletion src/fas_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ struct queue_end_s {

#define noop_or_wait

#define SUPPORT_DIR_PIN_MASK uint8_t
#define SUPPORT_DIR_TOGGLE_PIN_MASK uint8_t

#define SUPPORT_QUEUE_ENTRY_END_POS_U16

Expand Down

0 comments on commit 6d7ae50

Please sign in to comment.