delay_us, delay_ms

This commit is contained in:
Todd Fleming 2017-01-07 23:30:48 -05:00
parent dec344fdb0
commit fdf6e31218
8 changed files with 78 additions and 45 deletions

View File

@ -40,6 +40,7 @@
// Define the Grbl system include files. NOTE: Do not alter organization. // Define the Grbl system include files. NOTE: Do not alter organization.
#include "config.h" #include "config.h"
#include "delay.h"
#include "nuts_bolts.h" #include "nuts_bolts.h"
#include "settings.h" #include "settings.h"
#include "system.h" #include "system.h"

View File

@ -88,6 +88,7 @@ volatile uint8_t sys_rt_exec_accessory_override; // Global realtime executor bit
int main(void) int main(void)
{ {
// Initialize system upon power-up. // Initialize system upon power-up.
delay_init(); // Setup delay timer
serial_init(); // Setup serial baud rate and interrupts serial_init(); // Setup serial baud rate and interrupts
settings_init(); // Load Grbl settings from EEPROM settings_init(); // Load Grbl settings from EEPROM
current_init(); // Configure stepper driver current current_init(); // Configure stepper driver current

View File

@ -121,38 +121,7 @@ void delay_sec(float seconds, uint8_t mode)
protocol_exec_rt_system(); protocol_exec_rt_system();
if (sys.suspend & SUSPEND_RESTART_RETRACT) { return; } // Bail, if safety door reopens. if (sys.suspend & SUSPEND_RESTART_RETRACT) { return; } // Bail, if safety door reopens.
} }
_delay_ms(DWELL_TIME_STEP); // Delay DWELL_TIME_STEP increment delay_ms(DWELL_TIME_STEP); // Delay DWELL_TIME_STEP increment
}
}
// Delays variable defined milliseconds. Compiler compatibility fix for _delay_ms(),
// which only accepts constants in future compiler releases.
void delay_ms(uint16_t ms)
{
while ( ms-- ) { _delay_ms(1); }
}
// Delays variable defined microseconds. Compiler compatibility fix for _delay_us(),
// which only accepts constants in future compiler releases. Written to perform more
// efficiently with larger delays, as the counter adds parasitic time in each iteration.
void delay_us(uint32_t us)
{
while (us) {
if (us < 10) {
_delay_us(1);
us--;
} else if (us < 100) {
_delay_us(10);
us -= 10;
} else if (us < 1000) {
_delay_us(100);
us -= 100;
} else {
_delay_ms(1);
us -= 1000;
}
} }
} }

View File

@ -74,12 +74,6 @@ uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr);
// Non-blocking delay function used for general operation and suspend features. // Non-blocking delay function used for general operation and suspend features.
void delay_sec(float seconds, uint8_t mode); void delay_sec(float seconds, uint8_t mode);
// Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms().
void delay_ms(uint16_t ms);
// Delays variable-defined microseconds. Compiler compatibility fix for _delay_us().
void delay_us(uint32_t us);
// Computes hypotenuse, avoiding avr-gcc's bloated version and the extra error checking. // Computes hypotenuse, avoiding avr-gcc's bloated version and the extra error checking.
float hypot_f(float x, float y); float hypot_f(float x, float y);

View File

@ -289,9 +289,9 @@
#define PLL1CFG_Val 0x00000023 #define PLL1CFG_Val 0x00000023
#define CCLKCFG_Val 0x00000003 #define CCLKCFG_Val 0x00000003
#define USBCLKCFG_Val 0x00000000 #define USBCLKCFG_Val 0x00000000
#define PCLKSEL0_Val 0x00000000 #define PCLKSEL0_Val 0x00000014
#define PCLKSEL1_Val 0x00000000 #define PCLKSEL1_Val 0x00005000
#define PCONP_Val 0x042887DE #define PCONP_Val 0x04E887DE
#define CLKOUTCFG_Val 0x00000000 #define CLKOUTCFG_Val 0x00000000
@ -321,7 +321,7 @@
// </e> // </e>
*/ */
#define FLASH_SETUP 1 #define FLASH_SETUP 1
#define FLASHCFG_Val 0x0000303A #define FLASHCFG_Val 0x0000503A
/* /*
//-------- <<< end of configuration section >>> ------------------------------ //-------- <<< end of configuration section >>> ------------------------------

32
smoother/delay.cpp Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2017 Todd Fleming
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "delay.h"
void delay_init()
{
LPC_TIM3->CTCR = 0; // timer mode
LPC_TIM3->PR = 0; // no prescale
LPC_TIM3->MCR = 0; // no MR actions
LPC_TIM3->CCR = 0; // no capture
LPC_TIM3->EMR = 0; // no external match
LPC_TIM3->TCR = 0b10; // reset
LPC_TIM3->TCR = 0b01; // enable
}

38
smoother/delay.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
// Copyright 2017 Todd Fleming
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "LPC17xx.h"
void delay_init();
inline void delay_us(uint32_t us)
{
uint32_t start = LPC_TIM3->TC;
uint32_t cycles = uint32_t(uint64_t(SystemCoreClock) * us / 1000000);
while (LPC_TIM3->TC - start < cycles)
;
}
inline void delay_ms(uint32_t ms)
{
return delay_us(ms * 1000);
}

View File

@ -1,3 +1 @@
#define F_CPU 1000000UL #define F_CPU SystemCoreClock
inline void _delay_ms(double) {}
inline void _delay_us(double) {}