-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
151 lines (119 loc) · 3.54 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
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <config.h>
#include <turning_light.h>
#include "usart0.h"
#include "timer0.h"
#include "timer1.h"
#include "log.h"
#include <events.h>
#include "globals.h"
#include "tools.h"
#include "adc.h"
#include "pulse_det.h"
#include <nvm.h>
#include "temperature.h"
#include "handler_menu.h"
#include "handler_app.h"
#include "handler_startup.h"
#define DEBUG_EVENTS 0
/**
* Get original reset reason from Optiboot.
* Optiboot bootloader should save original MCUSR into R2
*/
uint8_t mcusr __attribute__ ((section(".noinit")));
void vGetOptibootMCUSR(void) __attribute__((naked)) __attribute((section(".init()")));
void vGetOptibootMCUSR(void)
{
__asm__ __volatile__ ( "mov %0, r2 \n" : "=r" (mcusr) : );
}
void main(void) __attribute__ ((noreturn));
void main(void)
{
// SimulationLoop();
WdtEnable();
MCUSR |= _BV(PUD); // disable all pull-ups
// after power up, all ports are tri-state: DDR=0 PORT=0
// to prevent from floating, better is to set all ports to LOW
//
// to put pin into HI state, first change DDR=1 then PORT=1
DDRB = 0xFF; PORTB=0;
DDRC = 0xFF; PORTC=0;
DDRD = 0xFF; PORTD=0;
DIDR0 = 0xFF; // digital input buffer disable on ADC
DIDR1 = 0xFF; // digital input buffer disable on analog comparator
// RX and TX into Tri state mode PD0 PD1
DDRD &= ~( _BV(DDD0) | _BV(DDD1) );
VOLTAGE_ADC_PIN_SETUP;
VOLTAGE_DIVIDER_PIN_SETUP;
VOLTAGE_DIVIDER_ENABLE;
WEBASTO_PIN_SETUP;
HEATER_OFF;
ARDUINO_LED_SETUP;
ARDUINO_LED_ON;
WEBASTO_STATE_PIN_SETUP;
// PB0 into Input Tri-state (pin is externally pulled down by voltage divider)
DDRB &= ~ _BV(PB0); // DDR=0 = input port
PORTB &= ~ _BV(PB0); // PORT=0 = disable pull-up
USART0_vInit();
LOG_P(PSTR("Build " __TIME__ " " __DATE__ "\n"));
LOG_P(PSTR("Reset reason: 0x%02X "), mcusr);
if (mcusr & _BV(PORF) ) LOG_P (PSTR("PWR "));
if (mcusr & _BV(EXTRF)) LOG_P (PSTR("RST "));
if (mcusr & _BV(BORF) ) LOG_P (PSTR("BRW "));
if (mcusr & _BV(WDRF) ) LOG_P (PSTR("WDT "));
LOG_NL;
NVM_vLoadSettings();
EventInit();
TIMER0_vInit();
EventPost(EV_CLOCK_1S); // first generic event can be used as startup notification
sei(); // start interrupts (especially timer)
ARDUINO_LED_OFF;
TEMP_vCalculateCalibration();
eState = ST_CHECK_FOR_MENU_ENTER;
eHandler = HND_STARTUP;
uiHeaterSwitchOffAfterS = 0;
for (;;)
{
set_sleep_mode (SLEEP_MODE_IDLE); // wait for event (INT from 1ms timer)
sleep_mode();
if (TRUE == bIsEventWaiting())
{
EVENT_DEF eEvent = EventGet();
#if (DEBUG_EVENTS)
DEBUG_P(PSTR("\n----------------------------\n"));
DEBUG_T_P(PSTR("Event 0x%02X\n\n"), eEvent);
#endif
switch (eHandler)
{
case HND_STARTUP:
STARTUP_vHandleEvent(eEvent);
break;
case HND_APP:
APP_vHandleEvent(eEvent);
break;
case HND_MENU:
MENU_vHandleEvent(eEvent);
break;
}
} // if (TRUE == bIsEventWaiting())
if (uiHeaterSwitchOffAfterS>0)
{
HEATER_ON
}
else
{
HEATER_OFF
}
// printf("\n");
//_delay_ms(50);
//USART0_vSendByte (USART0_ucGetByte());
//LOG_P(PSTR(" Woked up!\n"));
//_delay_ms(1000);
}
}