-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
199 lines (154 loc) · 4.59 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
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
/**
* \file
* <!--
* This file is part of Robin's Tunnel house window opener
*
* Bertos 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, or
* (at your option) any later version.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2015 Robin Gilks (www.gilks.org)
*
* -->
*
* \author Robin Gilks ([email protected])
*
* \brief Window opener with nrf24l01 RF remote linking
*
* This project uses an i2c connected LCD display, SPI connected RF module,
* 1-wire connected sensors, digital i/o to relays or H-bridges, analog to
* track battery voltage and a serial interface for debugging.
*/
#include <cfg/debug.h>
#include <cpu/irq.h>
#include <drv/timer.h>
#include <drv/ser.h>
#include "measure.h"
#include "rtc.h"
#include "eeprommap.h"
#include "window.h"
#include "nrf.h"
#include "ui.h"
Serial serial;
#define DEBUG 0
/* I/O pins used by the tunnel house window controller
GND 4
PD2 D2 5 FET driver } LO_UP motor driver
PD3 D3 6 FET driver } HI_UP motor driver
PD4 D4 7 1-wire } LO
PD5 D5 8 1-wire } HI temperature sensors
PD6 D6 9 1-wire } EX
PD7 D7 10 FET driver } LO_DN motor driver
PB0 D8 11 FET driver } HI_DN motor driver
PB1 D9 12 CE (3) } NRF24L01
PB2 D10 13 SS(CSN)(4) }
PB3 D11 14 MOSI (6) } interface
PB4 D12 15 MISO (7) }
PB5 D13 16 SCK (5) }
PC0 A0 19 up }
PC1 A1 20 centre } buttons
PC2 A2 21 down }
PC4 A4 23 SDA } LCD
PC5 A5 24 SCL }
PC3 A3 26 Current Sense } motor LO
PC7 A7 22 Current sense } motor HI
PC6 A6 25 Battery } analog
5V 27
GND 29
VIN 30
*/
static void
init (void)
{
/* Initialize system timer */
timer_init ();
/* Initialize UART0 */
ser_init (&serial, SER_UART0);
/* Configure UART0 to work at 115.200 bps */
ser_setbaudrate (&serial, 115200);
// get the config stuff & last time setting
load_eeprom_values ();
// real time clock
rtc_init ();
// temperature sensors
measure_init ();
// open/closing of windows
window_init ();
/* Enable all the interrupts */
IRQ_ENABLE;
// display and button handling
ui_init ();
// initialise RF link to remote
nrf_init();
// if bad limit value assume eeprom is not initialised
if (gLimits[SENSOR_LOW][LIMIT_UP] == -1)
ui_load_defaults();
}
int
main (void)
{
uint8_t key = 0;
init ();
while (1)
{
// keep real time clock stuff up to date
run_rtc ();
// run temperature reading stuff on the 1-wire interface
run_measure ();
// run state machine for window opening motors
run_windows ();
// send data back to base
key = run_nrf ();
// display stuff on the LCD & get user input
run_ui (key);
}
}
#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)
{
uint8_t *p = &_end;
while (p <= &__stack)
{
*p = STACK_CANARY;
p++;
}
}
uint16_t
StackCount (void)
{
const uint8_t *p = &_end;
uint16_t c = 0;
while (*p == STACK_CANARY && p <= &__stack)
{
p++;
c++;
}
return c;
}
#endif