Use SysTick instead of TIM3 for printing

This commit is contained in:
fruchti 2018-08-05 13:04:30 +02:00
parent d47ef99119
commit 8247add832
2 changed files with 40 additions and 48 deletions

View file

@ -1 +1 @@
233 323

View file

@ -24,11 +24,13 @@ static volatile State_t State = {State_Idle};
static void InitStepper(void) static void InitStepper(void)
{ {
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
// PA15 is used for stepper control, so JTAG has to be disabled // PA15 is used for stepper control, so JTAG has to be disabled
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_1; AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_1;
GPIOA->BRR = (1 << PIN_STEPPER_AM) | (1 << PIN_STEPPER_AP)
| (1 << PIN_STEPPER_BM) | (1 << PIN_STEPPER_BP);
GPIOA->CRH = (GPIOA->CRH GPIOA->CRH = (GPIOA->CRH
& ~(0x0f << (4 * PIN_STEPPER_AM - 32)) & ~(0x0f << (4 * PIN_STEPPER_AM - 32))
& ~(0x0f << (4 * PIN_STEPPER_AP - 32)) & ~(0x0f << (4 * PIN_STEPPER_AP - 32))
@ -40,13 +42,8 @@ static void InitStepper(void)
| (0x01 << (4 * PIN_STEPPER_BP - 32)) // Output, max. 10 MHz | (0x01 << (4 * PIN_STEPPER_BP - 32)) // Output, max. 10 MHz
; ;
TIM3->PSC = 48000000 / 100 / LTP1245_MAX_DRIVE_FREQ - 1; // The SysTick is clocked by AHB / 8
TIM3->ARR = 100; SysTick_Config(48000000 / 8 / LTP1245_MAX_DRIVE_FREQ - 1);
TIM3->DIER = TIM_DIER_UIE;
TIM3->CR1 = TIM_CR1_CEN;
NVIC_SetPriority(TIM3_IRQn, 1);
NVIC_EnableIRQ(TIM3_IRQn);
} }
static void InitSensors(void) static void InitSensors(void)
@ -380,54 +377,49 @@ void AdvanceStateMachine(void)
State = State.fn(); State = State.fn();
} }
void TIM3_IRQHandler(void) void SysTick_Handler(void)
{ {
if(TIM3->SR & TIM_SR_UIF) static int substep = 0;
static bool off;
const int TABLE[] = {0, 1, 3, 2};
const int GPIO_MASK = ((1 << PIN_STEPPER_AM) | (1 << PIN_STEPPER_AP)
| (1 << PIN_STEPPER_BM) | (1 << PIN_STEPPER_BP));
if(Stepper_Delta != 0)
{ {
static int substep = 0; off = false;
static bool off; if(Stepper_Delta > 0)
const int TABLE[] = {0, 1, 3, 2};
const int GPIO_MASK = ((1 << PIN_STEPPER_AM) | (1 << PIN_STEPPER_AP)
| (1 << PIN_STEPPER_BM) | (1 << PIN_STEPPER_BP));
if(Stepper_Delta != 0)
{ {
off = false; substep++;
if(Stepper_Delta > 0) if(substep > 3)
{ substep = 0;
substep++; Stepper_Delta--;
if(substep > 3)
substep = 0;
Stepper_Delta--;
}
else
{
substep--;
if(substep < 0)
substep = 3;
Stepper_Delta++;
}
GPIOA->ODR = (GPIOA->ODR & ~GPIO_MASK)
| ((TABLE[substep] & 1) ? (1 << PIN_STEPPER_AP)
: (1 << PIN_STEPPER_AM))
| ((TABLE[substep] & 2) ? (1 << PIN_STEPPER_BP)
: (1 << PIN_STEPPER_BM));
} }
else else
{ {
if(off) substep--;
{ if(substep < 0)
GPIOA->ODR = (GPIOA->ODR & ~GPIO_MASK); substep = 3;
substep = 0; Stepper_Delta++;
}
off = true;
} }
AdvanceStateMachine(); GPIOA->ODR = (GPIOA->ODR & ~GPIO_MASK)
| ((TABLE[substep] & 1) ? (1 << PIN_STEPPER_AP)
TIM3->SR &= ~TIM_SR_UIF; : (1 << PIN_STEPPER_AM))
| ((TABLE[substep] & 2) ? (1 << PIN_STEPPER_BP)
: (1 << PIN_STEPPER_BM));
} }
else
{
if(off)
{
GPIOA->ODR = (GPIOA->ODR & ~GPIO_MASK);
substep = 0;
}
off = true;
}
AdvanceStateMachine();
} }
void ADC1_2_IRQHandler(void) void ADC1_2_IRQHandler(void)