-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
218 lines (163 loc) · 6.03 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <time.h>
#include <stm32f10x.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_exti.h>
#include <stm32f10x_iwdg.h>
#include <stm32f10x_pwr.h>
#include <stm32f10x_it.h>
#include "misc_functions.h"
#include "common_lib/utils.h"
#include "common_lib/usart.h"
#include "common_lib/i2c_dma.h"
#include "device_lib/at24c64.h"
#include "device_lib/esp8266.h"
#include "device_lib/rda5807.h"
#include "device_lib/hd44780-i2c.h"
#include "device_lib/ir.h"
time_t _current_raw_time = 0;
struct tm* _current_time = 0;
bool _time_set = false;
// Callback function called after receiving packet from WiFi module (+IPD)
void incoming_packet_handler(char* string, uint8_t size) {
static time_t remote_time;
if (_time_set)
return;
remote_time = atoi(string) + 1;
_time_set = true;
LED_toggle(2);
// add one second, to compensate missing "tick"
RTC_SetCounter(remote_time);
RTC_WaitForLastTask();
force_update_time();
}
void NVIC_Configuration(void)
{
/* 1 bit for pre-emption priority, 3 bits for subpriority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_SetPriority(I2C1_EV_IRQn, 0x00);
NVIC_EnableIRQ(I2C1_EV_IRQn);
NVIC_SetPriority(I2C1_ER_IRQn, 0x01);
NVIC_EnableIRQ(I2C1_ER_IRQn);
NVIC_SetPriority(I2C2_EV_IRQn, 0x00);
NVIC_EnableIRQ(I2C2_EV_IRQn);
NVIC_SetPriority(I2C2_ER_IRQn, 0x01);
NVIC_EnableIRQ(I2C2_ER_IRQn);
}
void iwdg_setup() {
// Enable write access to IWDG_PR and IWDG_RLR registers
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
// IWDG counter clock: LSI / 256 -> max timeout = 26 s
IWDG_SetPrescaler(IWDG_Prescaler_128);
// Set counter reload value to obtain 3000 ms IWDG timeout
// 40000 -> 40kHz LSI oscillator, but it might varies between 30 and 60 kHz
IWDG_SetReload(40000 / 85);
// Reload IWDG counter
IWDG_ReloadCounter();
// Enable IWDG
IWDG_Enable();
}
void IR_Init() {
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Use PB6 as input from IR receiver
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Enable clock and its interrupts
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line10;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// interrupt for decoding command
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Init() {
// Enable clock and its interrupts
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitTypeDef TIM_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_Prescaler = SystemCoreClock / 1000000 - 1;
TIM_InitStructure.TIM_Period = 10000 - 1; // Update event every 10000 us / 10 ms
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_InitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
int main(void)
{
// Start Independent Watchdog
iwdg_setup();
// Configure NVIC
NVIC_Configuration();
// Setup LED on board
LED_Init2();
IWDG_ReloadCounter();
// Setup RTC
rtc_setup();
IWDG_ReloadCounter();
// Setup UART
USART1_Init(115200);
// Setup I2C
I2C_LowLevel_Init(I2C1);
// Setup Timers and IR
TIM_Init();
IR_Init();
setup_delay_timer(TIM4);
ir_nec_init(GPIO_Pin_10, GPIOB);
// Setup FM radio module;
rda5807_init(TIM4);
IWDG_ReloadCounter();
// Setup HD44780 I2C display
hd44780_init(TIM4);
hd44780_print("Radio");
add_custom_characters();
// Systicks every 1 second
if (SysTick_Config(SysTick_LOAD_RELOAD_Msk)) { while (1); }
// Add predefined radio stations
populate_stations();
// Load settings from EEPROM
load_settings();
// Setup Wifi module (used for clock sync)
IWDG_ReloadCounter();
esp8266_init(TIM4);
// Open local UDP port (5505) for time sync
esp8266_close_connection();
esp8266_establish_two_way_connection(
ESP8266_PROTOCOL_UDP, "0.0.0.0", 5555, 5505, 0,
&incoming_packet_handler);
while (1) {
// Trigger watchdog and enter deep sleep mode to save power
IWDG_ReloadCounter();
__WFI();
}
}