mirror of
https://github.com/SemvdH/OBD2-car-display.git
synced 2025-12-16 12:41:05 +00:00
49 lines
1.3 KiB
C
49 lines
1.3 KiB
C
#include "sam3xa/include/sam3x8e.h"
|
|
#include "sam3xa/include/component/component_rtt.h"
|
|
// #include "sam3xa/include/component/component_rtt.h"
|
|
// #include "sam3xa/include/sam3x8e.h"
|
|
#include "obd2_timer.h"
|
|
|
|
void obd2_timer_init(void)
|
|
{
|
|
/* enable the timer: reset the RTT
|
|
RTT->RTT_MR = RTT_MR_RTTRST;
|
|
/* set the alarm value to 0*/
|
|
RTT->RTT_AR = 0;
|
|
}
|
|
|
|
void obd2_timer_enable()
|
|
{
|
|
/* enable the alarm interrupt*/
|
|
RTT->RTT_MR |= RTT_MR_ALMIEN;
|
|
|
|
/* enable the interrupt*/
|
|
NVIC_EnableIRQ(RTT_IRQn);
|
|
}
|
|
|
|
void obd2_timer_reset()
|
|
{
|
|
/* To prevent several executions of the interrupt handler,
|
|
the interrupt must be disabled in the interrupt handler
|
|
and re-enabled when the status register is cleared.
|
|
*/
|
|
/* disable interrupts */
|
|
RTT->RTT_MR &= ~RTT_MR_ALMIEN;
|
|
/* clear the status register (reading it clears it)
|
|
SAM3X datasheet section 13.4: Reading the RTT_SR status register resets the RTTINC and ALMS fields.
|
|
*/
|
|
RTT->RTT_SR;
|
|
/* set the alarm value to 0*/
|
|
RTT->RTT_AR = 0;
|
|
/* re-enable the interrupt*/
|
|
RTT->RTT_MR |= RTT_MR_ALMIEN;
|
|
/* reset the RTT*/
|
|
RTT->RTT_MR |= RTT_MR_RTTRST;
|
|
}
|
|
|
|
void obd2_timer_set_period(__UINT32_TYPE__ period)
|
|
{
|
|
/* set the prescaler value equal to the clock speed divided by how many milliseconds*/
|
|
RTT->RTT_MR |= RTT_MR_RTPRES(CLOCK_FREQ / (1000/period));
|
|
}
|