/
main.c
162 lines (131 loc) · 3.69 KB
/
main.c
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
//---------------------------------------------------------------------------
// Copyright (C) 2012 Robin Gilks
//
//
// main.c - This program measures the output from and controls the load on a wind turbine
//
// History: 1.0 - First release.
//
// 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 2 of the License.
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// include files
#include <cfg/debug.h>
#include <cpu/irq.h>
#include <cpu/power.h>
#include <drv/timer.h>
#include <drv/ser.h>
#include <drv/lcd_hd44.h>
#include <drv/term.h>
#include <drv/kbd.h>
#include "features.h"
#include "eeprommap.h"
#include "measure.h"
#include "control.h"
#include "tlog.h"
#include "rpm.h"
#include "rtc.h"
#include "graph.h"
#include "ui.h"
Serial serial;
static void init(void)
{
/* Initialize debugging module (allow kprintf(), etc.) */
// kdbg_init();
/* Initialize system timer */
timer_init();
/*
* XXX: Arduino has a single UART port that was previously
* initialized for debugging purpose.
* In order to activate the serial driver you should disable
* the debugging module.
*/
/* Initialize UART0 */
ser_init(&serial, SER_UART0);
/* Configure UART0 to work at 115.200 bps */
ser_setbaudrate(&serial, 115200);
/* Enable all the interrupts now the basic stuff is initialised */
IRQ_ENABLE;
// set clock up using last values from eeprom
rtc_init();
// read a few more values out of eeprom and init the display etc
load_eeprom_values();
ui_init();
measure_init();
control_init();
rpm_init();
graph_init();
log_init();
}
int main(void)
{
init();
while (1)
{
// keep the clock ticking
run_rtc();
// run volts/amps/temperature reading stuff on the onewire interface
run_measure();
// decide if inverter or shunt load is needed to be turned on
run_control();
// keep the log data in dataflash up to date
run_log();
// calculate turbine RPM from period of raw AC
run_rpm();
// save values for graphic display of power in/out
run_graph();
// display stuff on the LCD & get user input
run_ui();
}
}
#if DEBUG > 0
extern uint8_t _end;
extern uint8_t __stack;
#define STACK_CANARY 0xc5
void StackPaint(void) __attribute__ ((naked)) __attribute__ ((section (".init1")));
uint16_t StackCount(void);
void StackPaint(void)
{
#if 1
uint8_t *p = &_end;
while(p <= &__stack)
{
*p = STACK_CANARY;
p++;
}
#else
__asm volatile (" ldi r30,lo8(_end)\n"
" ldi r31,hi8(_end)\n"
" ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */
" ldi r25,hi8(__stack)\n"
" rjmp .cmp\n"
".loop:\n"
" st Z+,r24\n"
".cmp:\n"
" cpi r30,lo8(__stack)\n"
" cpc r31,r25\n"
" brlo .loop\n"
" breq .loop"::);
#endif
}
uint16_t StackCount(void)
{
const uint8_t *p = &_end;
uint16_t c = 0;
while(*p == STACK_CANARY && p <= &__stack)
{
p++;
c++;
}
return c;
}
#endif