summaryrefslogtreecommitdiff
path: root/src/arch/rm46l8lp/arch.cc
blob: e07e50741248270ac2acbd38a962791ea30c8020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright 2020 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#include "arch.h"
#include "gio.h"
#include "rti.h"

#ifdef __acweaving
#define __delay_cycles(x)
#endif

void Arch::setup(void)
{
	gioInit();

	// disable counter blocks
	rtiREG1->GCTRL = 0;

	// internal clock, RTIUC0 -> RTIFRC0
	rtiREG1->TBCTRL = 0;

	// no capture sources
	rtiREG1->CAPCTRL = 0;

	// input source: counter 0 -> compare 0/1, counter 1 -> compare 2/3
	rtiREG1->COMPCTRL = 0x00001100U;

	// reset up / free running counter 0
	rtiREG1->CNT[0].UCx = 0;
	rtiREG1->CNT[0].FRCx = 0;

	// reset up / free running counter 1
	rtiREG1->CNT[1].UCx = 0;
	rtiREG1->CNT[1].FRCx = 0;

	// free running counter 0 freq = RTICLK/2^32
	rtiREG1->CNT[0].CPUCx = 0xffffffffU;

	// free running counter 1 freq = RTICLK/2 ( == VCLK/4, see halcogen/system.c)
	rtiREG1->CNT[1].CPUCx = 1;

	// one interrupt per second
	rtiREG1->CMP[2].COMPx = F_CPU/4;
	rtiREG1->CMP[2].UDCPx = F_CPU/4;

	// clear all pending interrupts
	rtiREG1->INTFLAG = 0x0007000FU;

	// disable all interrupts
	rtiREG1->CLEARINTENA = 0x00070F0FU;

#if defined(CONFIG_loop) || defined(TIMER_S)
	rtiEnableNotification(rtiNOTIFICATION_COMPARE2);
#endif
	_enable_IRQ();
#if defined(CONFIG_loop) || defined(TIMER_S)
	rtiStartCounter(rtiCOUNTER_BLOCK1);
#endif
}

#ifdef CONFIG_wakeup
extern void wakeup();
#endif

#if defined(CONFIG_loop)
extern void loop();
volatile bool run_loop = 0;
#endif

void Arch::delay_us(unsigned int const us)
{
	if (us < 10) {
		for (unsigned int i = 0; i < us; i++) {
			__delay_cycles(F_CPU / 4000000UL);
		}
	} else {
		for (unsigned int i = 0; i < us/10; i++) {
			__delay_cycles(F_CPU / 400000UL);
		}
	}
}
void Arch::delay_ms(unsigned int const ms)
{
	for (unsigned int i = 0; i < ms; i++) {
		__delay_cycles(F_CPU / 4000UL);
	}
}


void Arch::idle_loop(void)
{
	while (1) {
#if defined(CONFIG_loop)
		if (run_loop) {
			loop();
			run_loop = 0;
		}
#endif
	}
}

void Arch::idle(void)
{
#ifdef CONFIG_wakeup
	wakeup();
#endif
}

#if defined(CONFIG_loop) || defined(TIMER_S)
#include "driver/uptime.h"
void rtiNotification(uint32 notification)
{
	if (notification == rtiNOTIFICATION_COMPARE2) {
#ifdef CONFIG_loop
		run_loop = 1;
#endif
#ifdef TIMER_S
		uptime.tick_s();
#endif
	}
}
#endif

Arch arch;