2017-07-19 01:29:06 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
|
|
|
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description: HAL for Arduino Due and compatible (SAM3X8E)
|
|
|
|
*
|
|
|
|
* For ARDUINO_ARCH_SAM
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_SAM
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Includes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2018-04-13 03:25:08 +02:00
|
|
|
#include "HAL.h"
|
2017-07-19 01:29:06 +02:00
|
|
|
|
|
|
|
#include "HAL_timers_Due.h"
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Externals
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Local defines
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define NUM_HARDWARE_TIMERS 9
|
|
|
|
|
|
|
|
#define PRESCALER 2
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Private Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const tTimerConfig TimerConfig [NUM_HARDWARE_TIMERS] = {
|
2018-04-24 05:05:07 +02:00
|
|
|
{ TC0, 0, TC0_IRQn, 3}, // 0 - [servo timer5]
|
2018-03-19 10:48:06 +01:00
|
|
|
{ TC0, 1, TC1_IRQn, 0}, // 1
|
|
|
|
{ TC0, 2, TC2_IRQn, 0}, // 2
|
|
|
|
{ TC1, 0, TC3_IRQn, 2}, // 3 - stepper
|
2017-07-19 01:29:06 +02:00
|
|
|
{ TC1, 1, TC4_IRQn, 15}, // 4 - temperature
|
2018-04-24 05:05:07 +02:00
|
|
|
{ TC1, 2, TC5_IRQn, 3}, // 5 - [servo timer3]
|
|
|
|
{ TC2, 0, TC6_IRQn, 14}, // 6 - tone
|
2018-03-19 10:48:06 +01:00
|
|
|
{ TC2, 1, TC7_IRQn, 0}, // 7
|
|
|
|
{ TC2, 2, TC8_IRQn, 0}, // 8
|
2017-07-19 01:29:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Function prototypes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Private functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
Timer_clock1: Prescaler 2 -> 42MHz
|
|
|
|
Timer_clock2: Prescaler 8 -> 10.5MHz
|
|
|
|
Timer_clock3: Prescaler 32 -> 2.625MHz
|
|
|
|
Timer_clock4: Prescaler 128 -> 656.25kHz
|
|
|
|
*/
|
|
|
|
|
2017-08-24 19:18:54 +02:00
|
|
|
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
|
|
|
|
Tc *tc = TimerConfig[timer_num].pTimerRegs;
|
|
|
|
IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
|
|
|
|
uint32_t channel = TimerConfig[timer_num].channel;
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2018-03-22 01:04:45 +01:00
|
|
|
// Disable interrupt, just in case it was already enabled
|
|
|
|
NVIC_DisableIRQ(irq);
|
|
|
|
|
2018-05-16 21:38:17 +02:00
|
|
|
// We NEED memory barriers to ensure Interrupts are actually disabled!
|
|
|
|
// ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
|
|
|
|
__DSB();
|
|
|
|
__ISB();
|
|
|
|
|
2018-03-22 01:04:45 +01:00
|
|
|
// Disable timer interrupt
|
|
|
|
tc->TC_CHANNEL[channel].TC_IDR = TC_IDR_CPCS;
|
|
|
|
|
|
|
|
// Stop timer, just in case, to be able to reconfigure it
|
|
|
|
TC_Stop(tc, channel);
|
|
|
|
|
2017-07-19 01:29:06 +02:00
|
|
|
pmc_set_writeprotect(false);
|
|
|
|
pmc_enable_periph_clk((uint32_t)irq);
|
2017-08-24 19:18:54 +02:00
|
|
|
NVIC_SetPriority(irq, TimerConfig [timer_num].priority);
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2018-03-19 10:48:06 +01:00
|
|
|
// wave mode, reset counter on match with RC,
|
2017-08-24 19:18:54 +02:00
|
|
|
TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2018-03-22 01:04:45 +01:00
|
|
|
// Set compare value
|
2017-08-24 19:18:54 +02:00
|
|
|
TC_SetRC(tc, channel, VARIANT_MCK / 2 / frequency);
|
2018-03-22 01:04:45 +01:00
|
|
|
|
|
|
|
// And start timer
|
2017-07-19 01:29:06 +02:00
|
|
|
TC_Start(tc, channel);
|
|
|
|
|
|
|
|
// enable interrupt on RC compare
|
|
|
|
tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
|
|
|
|
|
2018-03-22 01:04:45 +01:00
|
|
|
// Finally, enable IRQ
|
2017-07-19 01:29:06 +02:00
|
|
|
NVIC_EnableIRQ(irq);
|
|
|
|
}
|
|
|
|
|
2017-08-24 19:18:54 +02:00
|
|
|
void HAL_timer_enable_interrupt(const uint8_t timer_num) {
|
2018-05-15 10:32:40 +02:00
|
|
|
IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
|
|
|
|
NVIC_EnableIRQ(irq);
|
2017-07-19 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 19:18:54 +02:00
|
|
|
void HAL_timer_disable_interrupt(const uint8_t timer_num) {
|
2018-05-15 10:32:40 +02:00
|
|
|
IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
|
|
|
|
NVIC_DisableIRQ(irq);
|
2018-05-16 21:38:17 +02:00
|
|
|
|
|
|
|
// We NEED memory barriers to ensure Interrupts are actually disabled!
|
|
|
|
// ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
|
|
|
|
__DSB();
|
|
|
|
__ISB();
|
2018-05-15 10:32:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// missing from CMSIS: Check if interrupt is enabled or not
|
|
|
|
static bool NVIC_GetEnabledIRQ(IRQn_Type IRQn) {
|
|
|
|
return (NVIC->ISER[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F))) != 0;
|
2017-07-19 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:00:03 +01:00
|
|
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
|
2018-05-15 10:32:40 +02:00
|
|
|
IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
|
|
|
|
return NVIC_GetEnabledIRQ(irq);
|
2018-01-12 03:59:16 +01:00
|
|
|
}
|
|
|
|
|
2017-07-19 01:29:06 +02:00
|
|
|
#endif // ARDUINO_ARCH_SAM
|