Rename to grbl-lpc

This commit is contained in:
Todd Fleming
2017-01-14 19:57:42 -05:00
parent f67c21a431
commit 4e6df9a4f9
14 changed files with 3 additions and 3 deletions

7
grbl-lpc/avr/interrupt.h Normal file

@@ -0,0 +1,7 @@
#pragma once
#include <LPC17xx.h>
#define ISR(f) void f()
inline void cli() {__disable_irq();}
inline void sei() {__enable_irq();}

71
grbl-lpc/avr/io.h Normal file

@@ -0,0 +1,71 @@
#pragma once
#include <stdint.h>
struct DummyReg {
operator uint8_t() {return 0;}
uint8_t operator=(uint8_t x) {return x;}
uint8_t operator&=(uint8_t x) {return *this = *this & x;}
uint8_t operator|=(uint8_t x) {return *this = *this | x;}
};
extern DummyReg DDRA;
extern DummyReg DDRB;
extern DummyReg DDRC;
extern DummyReg DDRD;
extern DummyReg PORTA;
extern DummyReg PORTB;
extern DummyReg PORTC;
extern DummyReg PORTD;
extern DummyReg PINA;
extern DummyReg PINB;
extern DummyReg PINC;
extern DummyReg PIND;
extern DummyReg EEAR;
extern DummyReg EECR;
extern DummyReg EEDR;
extern DummyReg EEMPE;
extern DummyReg EEPE;
extern DummyReg OCR2A;
extern DummyReg PCICR;
extern DummyReg PCIE0;
extern DummyReg PCIE1;
extern DummyReg PCMSK0;
extern DummyReg PCMSK1;
extern DummyReg TCCR2A;
extern DummyReg TCCR2B;
extern DummyReg UCSR0A;
extern DummyReg UCSR0B;
extern DummyReg U2X0;
extern DummyReg UBRR0H;
extern DummyReg UBRR0L;
extern DummyReg UDR0;
static const int COM1A0 = 0;
static const int COM1A1 = 0;
static const int COM1B0 = 0;
static const int COM1B1 = 0;
static const int COM2A0 = 0;
static const int COM2A1 = 0;
static const int COM2B0 = 0;
static const int COM2B1 = 0;
static const int CS22 = 0;
static const int EEMWE = 0;
static const int EERE = 0;
static const int EEWE = 0;
static const int WGM10 = 0;
static const int WGM11 = 0;
static const int WGM12 = 0;
static const int WGM13 = 0;
static const int WGM20 = 0;
static const int WGM21 = 0;
static const int RXEN0 = 0;
static const int TXEN0 = 0;
static const int RXCIE0 = 0;
static const int UDRIE0 = 0;

3
grbl-lpc/avr/pgmspace.h Normal file

@@ -0,0 +1,3 @@
#define PSTR(s) s
inline char pgm_read_byte_near(const char* p) {return *p;}

0
grbl-lpc/avr/wdt.h Normal file

@@ -0,0 +1,69 @@
// Current control
//
// 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 "current_control.h"
#include "grbl.h"
#include "Driver_I2C.h"
#ifdef CURRENT_I2C
extern ARM_DRIVER_I2C CURRENT_I2C;
static const uint8_t wiperRegs[] = CURRENT_WIPERS;
#endif
void current_init()
{
#ifdef CURRENT_I2C
CURRENT_I2C.Initialize(nullptr);
CURRENT_I2C.PowerControl(ARM_POWER_FULL);
CURRENT_I2C.Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_STANDARD);
CURRENT_I2C.Control(ARM_I2C_BUS_CLEAR, 0);
#ifdef CURRENT_MCP44XX_ADDR
uint8_t init1[] = {0x40, 0xff};
uint8_t init2[] = {0xA0, 0xff};
CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, init1, 2, false);
while (CURRENT_I2C.GetStatus().busy)
;
CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, init2, 2, false);
while (CURRENT_I2C.GetStatus().busy)
;
#endif
set_current(0, DEFAULT_X_CURRENT);
set_current(1, DEFAULT_Y_CURRENT);
set_current(2, DEFAULT_Z_CURRENT);
set_current(3, DEFAULT_A_CURRENT);
#endif
}
void set_current(uint8_t motor, float amps)
{
#if defined(CURRENT_I2C) && defined(CURRENT_MCP44XX_ADDR)
uint8_t command[] = {
uint8_t(wiperRegs[motor] << 4),
uint8_t(std::round(amps * CURRENT_FACTOR)),
};
CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, command, sizeof(command), false);
while (CURRENT_I2C.GetStatus().busy)
;
#endif
}

@@ -0,0 +1,26 @@
// Current control
//
// 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 <stdint.h>
void current_init();
void set_current(uint8_t motor, float amps);

45
grbl-lpc/debug.h Normal file

@@ -0,0 +1,45 @@
#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"
#include <atomic>
static const uint32_t leds = (1 << 21) | (1 << 20) | (1 << 19) | (1 << 18);
inline void debug_init()
{
LPC_GPIO1->FIODIR = leds;
LPC_GPIO1->FIOPIN = 0;
}
inline void debug_enter()
{
LPC_GPIO1->FIOPIN = (LPC_GPIO1->FIOPIN | (1 << 21)) ^ (1 << 20);
}
inline void debug_exit()
{
LPC_GPIO1->FIOPIN &= ~(1 << 21);
}
inline void xdebug_enter() {}
inline void xdebug_exit() {}

32
grbl-lpc/delay.cpp Normal 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
}

48
grbl-lpc/delay.h Normal file

@@ -0,0 +1,48 @@
#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();
// Get current time in clock cycles
inline uint32_t get_time()
{
return LPC_TIM3->TC;
}
// Wait until get_time() is numCycles past startTime. Handles timer wrap.
inline void delay_loop(uint32_t startTime, uint32_t numCycles)
{
while (get_time() - startTime < numCycles)
;
}
inline void delay_us(uint32_t us)
{
delay_loop(get_time(), uint32_t(uint64_t(SystemCoreClock) * us / 1'000'000));
}
inline void delay_ms(uint32_t ms)
{
delay_loop(get_time(), uint32_t(uint64_t(SystemCoreClock) * ms / 1000));
}

33
grbl-lpc/isr_init.cpp Normal file

@@ -0,0 +1,33 @@
// 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 "grbl.h"
void isr_init()
{
// Set all interrupts and system handlers to lowest priority
for (auto &ip : NVIC->IP)
ip = 31 << 3;
for (auto &shp : SCB->SHP)
shp = 31 << 3;
// Stepper ISR needs highest priority
NVIC->IP[TIMER1_IRQn] = 0;
}

152
grbl-lpc/pwm_driver.cpp Normal file

@@ -0,0 +1,152 @@
// PWM Driver - LPC176x - Single edge only, does not support double edge PWM
//
// Copyright 2017 Brett 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 "pwm_driver.h"
#include "LPC17xx.h"
//#include "grbl.h"
//LPC_PWM1_BASE
//LPC_PWM1 is of type LPC_PWM_TypeDef at Address LPC_PWM1_BASE
/*
Theory of operation: Initialize, start running, change width any time while running.
pwm_init(PWM1_CH1, 1000, 0);
pwm_enable(PWM1_CH1);
pwm_set_width(250);
sleep(1)
pwm_set_width(500);
sleep(1)
pwm_set_width(999);
sleep(1)
pwm_disable(PWM1_CH1);
*/
const PWM_Channel_Config PWM1_CH1 = {
&(LPC_PWM1->MR1), //Match Register
(1 << 9), //PWM Enable
(1 << 1), //Latch Enable Register
(0x2 << 4), //PINSEL3 - P1.18
(0x1 << 0) //PINSEL4 - P2.0
};
const PWM_Channel_Config PWM1_CH2 = {
&(LPC_PWM1->MR2), //Match Register
(1 << 10), //PWM Enable
(1 << 2), //Latch Enable Register
(0x2 << 8), //PINSEL3 - P1.20
(0x1 << 2) //PINSEL4 - P2.1
};
const PWM_Channel_Config PWM1_CH3 = {
&(LPC_PWM1->MR3), //Match Register
(1 << 11), //PWM Enable
(1 << 3), //Latch Enable Register
(0x2 << 10), //PINSEL3 - P1.21
(0x1 << 4) //PINSEL4 - P2.2
};
const PWM_Channel_Config PWM1_CH4 = {
&(LPC_PWM1->MR4), //Match Register
(1 << 12), //PWM Enable
(1 << 4), //Latch Enable Register
(0x2 << 14), //PINSEL3 - P1.23
(0x1 << 6) //PINSEL4 - P2.3
};
const PWM_Channel_Config PWM1_CH5 = {
&(LPC_PWM1->MR5), //Match Register
(1 << 13), //PWM Enable
(1 << 5), //Latch Enable Register
(0x2 << 16), //PINSEL3 - P1.24
(0x1 << 8) //PINSEL4 - P2.4
};
const PWM_Channel_Config PWM1_CH6 = {
&(LPC_PWM1->MR6), //Match Register
(1 << 14), //PWM Enable
(1 << 6), //Latch Enable Register
(0x2 << 20), //PINSEL3 - P1.26
(0x1 << 10) //PINSEL4 - P2.6
};
//UM10360 LPC17xx Chapter 24 - Pulse Width Modulation
//
// channel - Which of the 6 PWM channels to init.
// period - what is the overall period, in PCLK cycles, for ALL PWMs (not just the provided one)
//
void pwm_init(PWM_Channel_Config* channel, bool primaryPin, bool secondaryPin, uint32_t period, uint32_t width) {
//Power up PWM Circuitry - Defaulted to on at reset, but doesn't hurt to make sure
LPC_SC->PCONP |= 1 << 6; // Power up the PWM
//Pin mode selections
if (primaryPin) LPC_PINCON->PINSEL3 |= channel->PINSEL3_Enable_Mask;
if (secondaryPin) LPC_PINCON->PINSEL4 |= channel->PINSEL4_Enable_Mask;
//PWM Control Register - Disable output for channel
LPC_PWM1->PCR &= ~channel->PCR_Enable_Mask;
//Match Control - Continous operation (no interrupts, reset counter on MSR0 match (period))
LPC_PWM1->MCR = (1 << 1);
//PWM Control Register - Single Edge (0 for bits 2-6) mode for all PWMs
LPC_PWM1->PCR &= 0xFF00;
//Disable Capture
LPC_PWM1->CCR = 0x0000;
//PWM Timer Control Register - Counter Enable, PWM Enable
LPC_PWM1->TCR = (1 << 0) | (1 << 2);
pwm_set_period(period);
pwm_set_width(channel, width);
}
void pwm_set_period(uint32_t period) {
LPC_PWM1->MR0 = period;
//If we are running, this will make the MRx register on the next cycle
LPC_PWM1->LER = 0x00000001;
}
void pwm_set_width(PWM_Channel_Config* channel, uint32_t width) {
*(channel->MRn) = width;
//If we are running, this will make the MRx register on the next cycle
LPC_PWM1->LER = channel->LER_Enable_Mask;
}
void pwm_enable(PWM_Channel_Config* channel) {
//PWM Control Register - Enable output for channel
LPC_PWM1->PCR |= channel->PCR_Enable_Mask;
}
void pwm_disable(PWM_Channel_Config* channel) {
//PWM Control Register - Disable output for channel
LPC_PWM1->PCR &= ~channel->PCR_Enable_Mask;
}

46
grbl-lpc/pwm_driver.h Normal file

@@ -0,0 +1,46 @@
// PWM Driver - LPC176x
//
// Copyright 2017 Brett 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 <stdint.h>
typedef struct _PWM_Channel_Config {
volatile uint32_t* MRn; //LPC_PWM1->MR1 through MR6
uint32_t PCR_Enable_Mask;
uint32_t LER_Enable_Mask;
//PWM Output Mapping PWM1 PWM2 PWM3 PWM4 PWM5 PWM6
uint32_t PINSEL3_Enable_Mask; //For PWM support on (P1.18, P1.20, P1.21, P1.23, P1.24, P1.26)
uint32_t PINSEL4_Enable_Mask; //For PWM support on (P2.0, P2.1, P2.2, P2.3, P2.4, P2.6)
} const PWM_Channel_Config;
extern PWM_Channel_Config PWM1_CH1;
extern PWM_Channel_Config PWM1_CH2;
extern PWM_Channel_Config PWM1_CH3;
extern PWM_Channel_Config PWM1_CH4;
extern PWM_Channel_Config PWM1_CH5;
extern PWM_Channel_Config PWM1_CH6;
void pwm_init(PWM_Channel_Config* channel, bool primaryPin, bool secondaryPin, uint32_t period, uint32_t width);
void pwm_set_period(uint32_t period);
void pwm_set_width(PWM_Channel_Config* channel, uint32_t width);
void pwm_enable(PWM_Channel_Config* channel);
void pwm_disable(PWM_Channel_Config* channel);

1
grbl-lpc/util/delay.h Normal file

@@ -0,0 +1 @@
#define F_CPU SystemCoreClock