Created
March 24, 2018 15:16
-
-
Save kosso/f59e6cd6eb92a24ef76a4eb4632dcbce to your computer and use it in GitHub Desktop.
Experimental code to use two timers to create and control two fast pulses for eventual use with stepper motor drivers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Trying and dynamically set, start and stop the timers. | |
// so we can set the pulse delay at that point, rather than within the timer ISR | |
#include <Arduino.h> | |
// ESP32 Registers | |
// https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf | |
// Serial Tests and Timers | |
// type '1go' or '2go' in Serial monitor to start 10us timer counter. | |
// type '1stop' or '2stop' to stop each pulse. | |
// See : https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf | |
// 'Register Summary' - Section 4.12 - page 59. | |
// For now using two LEDs to show the pulses. | |
// The idea would be to eventually use this for step pulses to a stepper motor driver like a DRV.., TMC.., etc. | |
#define PIN_14_HIGH (*((volatile uint32_t *) (0x3ff44000 + 0x8 ))) ^= 1 << 14; // 'Write 1 To Set' : GPIO_OUT_W1TS_REG (0x3FF44008) | |
#define PIN_14_LOW (*((volatile uint32_t *) (0x3ff44000 + 0xC ))) ^= 1 << 14; // 'Write 1 To Clear' : GPIO_OUT_W1TC_REG 0x3FF4400C) | |
#define PIN_27_HIGH (*((volatile uint32_t *) (0x3ff44000 + 0x8 ))) ^= 1 << 27; // 'Write 1 To Set' : GPIO_OUT_W1TS_REG (0x3FF44008) | |
#define PIN_27_LOW (*((volatile uint32_t *) (0x3ff44000 + 0xC ))) ^= 1 << 27; // 'Write 1 To Clear' : GPIO_OUT_W1TC_REG 0x3FF4400C) | |
// Serial comms. | |
int c = 0; | |
String rString; | |
String oString; | |
volatile int interruptCounter_1; | |
int totalInterruptCounter_1; | |
volatile int interruptCounter_2; | |
int totalInterruptCounter_2; | |
portMUX_TYPE timerMux_1 = portMUX_INITIALIZER_UNLOCKED; | |
portMUX_TYPE timerMux_2 = portMUX_INITIALIZER_UNLOCKED; | |
hw_timer_t * timer_1 = NULL; | |
int pulse_delay_1 = 5000; | |
// give it a bit longer. | |
int timer_delay_1 = pulse_delay_1 * 2 + pulse_delay_1 * 0.2; | |
int steps_1 = 0; | |
hw_timer_t * timer_2 = NULL; | |
int pulse_delay_2 = 5000; | |
// give it a bit longer. | |
int timer_delay_2 = pulse_delay_2 * 2 + pulse_delay_2 * 0.2; | |
int steps_2 = 0; | |
void IRAM_ATTR onTimer_1() { | |
portENTER_CRITICAL_ISR(&timerMux_1); | |
PIN_14_HIGH; | |
interruptCounter_1++; | |
portEXIT_CRITICAL_ISR(&timerMux_1); | |
delayMicroseconds(pulse_delay_1); | |
Serial.println('1'); | |
portENTER_CRITICAL_ISR(&timerMux_1); | |
PIN_14_LOW; | |
portEXIT_CRITICAL_ISR(&timerMux_1); | |
delayMicroseconds(pulse_delay_1); | |
} | |
void IRAM_ATTR onTimer_2() { | |
portENTER_CRITICAL_ISR(&timerMux_2); | |
PIN_27_HIGH; | |
interruptCounter_2++; | |
portEXIT_CRITICAL_ISR(&timerMux_2); | |
delayMicroseconds(pulse_delay_2); | |
Serial.println('2'); | |
portENTER_CRITICAL_ISR(&timerMux_2); | |
PIN_27_LOW; | |
portEXIT_CRITICAL_ISR(&timerMux_2); | |
delayMicroseconds(pulse_delay_2); | |
} | |
void start_timer_1(int steps, int pulse_delay){ | |
pulse_delay_1 = pulse_delay; | |
timer_delay_1 = (pulse_delay * 2 + pulse_delay * 0.2); | |
steps_1 = steps; | |
timer_1 = timerBegin(0, 80, true); | |
timerAttachInterrupt(timer_1, &onTimer_1, true); | |
timerAlarmWrite(timer_1, timer_delay_1, true); | |
timerAlarmEnable(timer_1); | |
timerStart(timer_1); | |
} | |
void start_timer_2(int steps, int pulse_delay){ | |
pulse_delay_2 = pulse_delay; | |
timer_delay_2 = (pulse_delay * 2 + pulse_delay * 0.2); | |
steps_2 = steps; | |
timer_2 = timerBegin(0, 80, true); | |
timerAttachInterrupt(timer_2, &onTimer_2, true); | |
timerAlarmWrite(timer_2, timer_delay_2, true); | |
timerAlarmEnable(timer_2); | |
timerStart(timer_2); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
pinMode(14, OUTPUT); | |
pinMode(27, OUTPUT); | |
// flash an led to show starting up | |
PIN_27_HIGH; | |
delay(250); | |
PIN_27_LOW; | |
delay(250); | |
PIN_27_HIGH; | |
delay(250); | |
PIN_27_LOW; | |
delay(250); | |
PIN_27_HIGH; | |
delay(250); | |
PIN_27_LOW; | |
delay(250); | |
PIN_14_LOW; | |
PIN_27_LOW; | |
// Now moved to their own functions... | |
// timer_1 = timerBegin(0, 80, true); | |
// timerAttachInterrupt(timer_1, &onTimer_1, true); | |
// timerAlarmWrite(timer_1, timer_delay_1, true); // give it enough time | |
// timer_2 = timerBegin(0, 80, true); | |
// timerAttachInterrupt(timer_2, &onTimer_2, true); | |
// timerAlarmWrite(timer_2, timer_delay_2, true); // give it enough time | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
// read the incoming serial | |
c = Serial.read(); | |
if(c != '\n' && c != '\r'){ | |
rString += (char)c; | |
if(rString == "1go"){ | |
start_timer_1(6400, 50000); | |
Serial.println("starting timer_1 delay: " + String(timer_delay_1) + "us : pulse delay:" + String(pulse_delay_1)+"us"); | |
interruptCounter_1 = 0; | |
totalInterruptCounter_1 = 0; | |
timerAlarmEnable(timer_1); | |
// PIN_14_HIGH | |
return; | |
} else if(rString == "1stop"){ | |
Serial.println("stopping timer_1"); | |
timerAlarmDisable(timer_1); | |
timerStop(timer_1); | |
timerEnd(timer_1); | |
PIN_14_LOW | |
PIN_27_LOW | |
return; | |
} else if(rString == "2go"){ | |
start_timer_2(6400, 100000); | |
Serial.println("starting timer_2 delay: " + String(timer_delay_2) + "us : pulse delay:" + String(pulse_delay_2)+"us"); | |
interruptCounter_2 = 0; | |
totalInterruptCounter_2 = 0; | |
timerAlarmEnable(timer_2); | |
// PIN_14_HIGH | |
return; | |
} else if(rString == "2stop"){ | |
Serial.println("stopping timer_2"); | |
timerAlarmDisable(timer_2); | |
timerStop(timer_2); | |
timerEnd(timer_2); | |
PIN_14_LOW | |
PIN_27_LOW | |
return; | |
} | |
} else { | |
oString = rString; | |
Serial.println("received: " + oString); | |
rString = ""; | |
} | |
} | |
if (interruptCounter_1 > 0 && timerAlarmEnabled(timer_1)) { | |
totalInterruptCounter_1++; | |
PIN_14_HIGH; | |
} else if(interruptCounter_1 > 0 && !timerAlarmEnabled(timer_1)){ | |
Serial.println("final_1: main:"+ String(totalInterruptCounter_1) +" - timed pulse count:" + String(interruptCounter_1)); | |
interruptCounter_1 = 0; | |
totalInterruptCounter_1 = 0; | |
} | |
if (interruptCounter_2 > 0 && timerAlarmEnabled(timer_2)) { | |
totalInterruptCounter_2++; | |
PIN_14_HIGH; | |
} else if(interruptCounter_2 > 0 && !timerAlarmEnabled(timer_2)){ | |
Serial.println("final_2: main:"+ String(totalInterruptCounter_2) +" - timed pulse count:" + String(interruptCounter_2)); | |
interruptCounter_2 = 0; | |
totalInterruptCounter_2 = 0; | |
} | |
//delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment