-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample.c
More file actions
203 lines (175 loc) · 5.08 KB
/
sample.c
File metadata and controls
203 lines (175 loc) · 5.08 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "include.h"
#include "common.h"
// Private Variables
static volatile uint32_t Sys_Tick = 0;
// Local Functions Prototypes
static void InitSysTick(void);
void SysTick_Handler(void);
void Delay_SysTick(uint32_t SysTicks);
// Exported variables
volatile state_t state;
// Imported Variables
extern volatile uint8_t joystick_flag;
extern volatile uint8_t btn_flag;
// Imported Functions
extern void ASM_func(uint32_t VETT[], uint32_t n);
// Local Global Variables
uint32_t VETT[N];
uint32_t i;
uint32_t VAR;
int main (void) {
// Imperative Inits
SystemInit();
InitSysTick();
// Initialize Variables
state = STATE_IDLE;
memset(VETT, 0, sizeof(VETT));
i = 0;
VAR = 0;
// Other peripherals Init
LED_init();
joystick_init();
//LCD_Initialization();
//ADC_init();
// BUTTON_init:
// 1: which button
// 2: priority of the associated interrupt
BUTTON_init(BUTTON_0, PRIO_3);
BUTTON_init(BUTTON_1, PRIO_3);
BUTTON_init(BUTTON_2, PRIO_3);
// RIT WORKS WITH CLOCK = 100MHZ
// ONE INTERRUPT EVERY 50ms
init_RIT(RIT_MS_TO_TICKS(RIT_PERIOD_MS)); enable_RIT();
/* TIMER INSTRUCTIONS
// init_timer_simplified:
// 1: which timer
// 2: prescaler
// 3: mr0
// 4: mr1
// 5: configuration of MR0 (interrupt, reset and stop) ( | of TIMER_INTERRUPT_MR, TIMER_RESET_MR, TIMER_STOP_MR)
// 6: configuration of MR1 (interrupt, reset and stop) ( | of TIMER_INTERRUPT_MR, TIMER_RESET_MR, TIMER_STOP_MR)
// TIMER_0
// f_timer_hz = 25000000 Hz = 25MHz
// prescaler =
// MR =
// f_counter_hz = f_timer_hz / (prescaler + 1) =
// T_timer = MR / f_counter_hz =
// MR = T_timer * f_timer_hz / (prescaler + 1) =
// USE TIM_MS_TO_TICKS_SIMPLE for default PR (=0) and f_timer_hz values
// use TIM_MS_TO_TICKS for custom PR and f_timer_hz values
// max ms value is 170000 (2min 50s) => TIM_MS_TO_TICKS_SIMPLE(170000)=2^31-1
*/
// init_timer_simplified(TIMER_0, prescale, mr0, mr1, conf_mr0, conf_mr1); enable_timer(TIMER_0, PRIO_3);
//init_timer_simplified(TIMER_0, 0, TIM_MS_TO_TICKS_SIMPLE(1000), 0, TIMER_INTERRUPT_MR | TIMER_RESET_MR | TIMER_STOP_MR, 0, timer0_callback);
//enable_timer(TIMER_0, PRIO_3);
/* Example: do stuff every 500ms ms for 10000ms */
//init_repetitive_timer_simplified(TIMER_1, 0, TIM_MS_TO_TICKS_SIMPLE(500), 10000, timer1_callback);
//enable_timer(1, PRIO_3);
/* Example: LEDs on for 250ms and off for 750ms */
//init_infinite_timer_pwm(2, 0.25, 1000, timer2s_callback);
//enable_timer(2, PRIO_3);
/* Example: LEDs on for 250ms and off for 750ms for 10000ms */
//init_finite_timer_pwm(3, 0.25, 1000, 10000, timer3_callback);
//enable_timer(3, PRIO_3);
// power control register
LPC_SC->PCON |= 0x1; // PM0=1
LPC_SC->PCON &= 0xFFFFFFFFD; // PM1=0
//execution of wfi or wfe assembly enters Power-Down mode when SLEEPDEEP is on
// call asm function
// ASM_func(VETT, N);
while (1) {
/*Finite State Machine*/
switch(state){
case STATE_IDLE:
break;
case STATE_RESET:
break;
}
__ASM("wfi");
}
}
/*
if(btn_flag & FLAG_BUTTON_0_SHORT) {
btn_flag &= ~FLAG_BUTTON_0_SHORT;
// do stuff
}
if(btn_flag & FLAG_BUTTON_0_LONG) {
btn_flag &= ~FLAG_BUTTON_0_LONG;
// do stuff
}
if(btn_flag & FLAG_BUTTON_1_SHORT) {
btn_flag &= ~FLAG_BUTTON_1_SHORT;
// do stuff
}
if(btn_flag & FLAG_BUTTON_1_LONG) {
btn_flag &= ~FLAG_BUTTON_1_LONG;
// do stuff
}
if(btn_flag & FLAG_BUTTON_2_SHORT) {
btn_flag &= ~FLAG_BUTTON_2_SHORT;
// do stuff
}
if(btn_flag & FLAG_BUTTON_2_LONG) {
btn_flag &= ~FLAG_BUTTON_2_LONG;
// do stuff
}
*/
/*
// Joystick cmd, flags set at first edge
if(joystick_flag & FLAG_JOYSTICK_UP) {
joystick_flag &= ~FLAG_JOYSTICK_UP;
}
*/
/*
if(joystick_flag & FLAG_JOYSTICK_DOWN) {
joystick_flag &= ~FLAG_JOYSTICK_DOWN;
}
*/
/*
if(joystick_flag & FLAG_JOYSTICK_LEFT) {
joystick_flag &= ~FLAG_JOYSTICK_LEFT;
}
*/
/*
if(joystick_flag & FLAG_JOYSTICK_RIGHT) {
joystick_flag &= ~FLAG_JOYSTICK_RIGHT;
}
*/
/*
if(joystick_flag & FLAG_JOYSTICK_SELECT) {
joystick_flag &= ~FLAG_JOYSTICK_SELECT;
}
*/
// Buttons, flags set when released
/*
if(btn_flag & FLAG_BUTTON_0) {
btn_flag &= ~FLAG_BUTTON_0;
}
*/
/*
if(btn_flag & FLAG_BUTTON_1) {
btn_flag &= ~FLAG_BUTTON_1;
}
*/
/*
if(btn_flag & FLAG_BUTTON_2) {
btn_flag &= ~FLAG_BUTTON_2;
}
*/
/* Initialize SysTick using CMSIS Core_CM4 function */
static void InitSysTick(void){
SysTick_Config(SystemFrequency/1000U); /* Configure the SysTick timer */
}
/* SysTick Interrupt Handler */
void SysTick_Handler(void){
Sys_Tick++; /* increment timer */
}
/* Delay Function based on SysTick Counter */
void Delay_SysTick(uint32_t SysTicks){
uint32_t DelayTimer_SysTick = Sys_GetTick() + SysTicks; /* Get End Tick */
while(Sys_GetTick() < DelayTimer_SysTick); /* wait for timer */
}
/*Get Current Elapsed Ticks*/
uint32_t Sys_GetTick(void){
return Sys_Tick;
}