Add test code for using RTT

This commit is contained in:
Sem van der Hoeven
2023-10-27 00:33:17 +02:00
parent 0e4f501f24
commit 4c78d0eac6
4 changed files with 88 additions and 2 deletions

View File

@@ -11,9 +11,10 @@ Program to create a car monitor display using:
#include <UTFT.h>
#include "obd2_display.h"
// #include "obd2_timer.h"
#include "obd2_timer.h"
#define DEBUG 1
// #define RTT_MR 0x400E1A30U
/* pins */
#define PIN_BT_RX 19
@@ -24,6 +25,8 @@ extern uint8_t BigFont[];
extern uint8_t SmallFont[];
char should_clear = 1;
char led_state = LOW;
char flag = 0;
/* Set the pins for the display and dev board */
/* Standard Arduino Mega/Due shield : <display model>,38,39,40,41 */
@@ -46,7 +49,16 @@ void setup() {
Serial.println("Starting");
#endif
TCCR0A=(1<<WGM01); //Set the CTC mode
pinMode(LED_BUILTIN, OUTPUT);
obd2_timer_init();
}
void RTT_Handler()
{
flag = 0;
obd2_timer_reset();
}
void loop() {
@@ -68,4 +80,14 @@ void loop() {
delay(10);
should_clear = 0;
}
if (!flag)
{
led_state = ~led_state;
digitalWrite(LED_BUILTIN, led_state);
flag = 1;
#if (DEBUG == 1)
Serial.println("switching led");
#endif
}
}

15
due_obd2/obd2_display.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef OBD2_DISPLAY_H
#define OBD2_DISPLAY_H
#ifdef __cplusplus
extern "C" {
#endif
/* display size */
#define LCD_W 480
#define LCD_H 320
#ifdef __cplusplus
}
#endif
#endif // !OBD2_DISPLAY_H

32
due_obd2/obd2_timer.c Normal file
View File

@@ -0,0 +1,32 @@
// #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, enable the alarm interrupt, set the prescaler value equal to the processor clock speed*/
RTT->RTT_MR = RTT_MR_RTTRST | RTT_MR_ALMIEN | RTT_MR_RTPRES(32768/2);
/* set the alarm value to 0*/
RTT->RTT_AR = 0;
/* 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 */
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;
}

17
due_obd2/obd2_timer.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef OBD2_TIMER_H
#ifdef __cplusplus
extern "C" {
#endif
#include "Arduino.h"
#define CLOCK_FREQ 32768
void obd2_timer_init();
void obd2_timer_reset();
#ifdef __cplusplus
}
#endif
#endif // !OBD2_TIMER_H