-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
288 lines (236 loc) · 8.08 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*=============================================================*
* _ _ ___ _ _ *
* | \| | _ _ | _ ) _ _ (_) __ | |__ *
* | .` | | +| | | _ \ | '_| | | / _| | / / *
* |_|\_| \_,_| |___/ _|_|_ _|_|_ \__|_ |_\_\ *
*_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| *
*"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-' *
* *
* NuBrick IoT applications firmware *
* *
* Written by MB40 for Nuvoton Technology. *
* tlshen@nuvoton.com/tzulan611126@gmail.com *
* *
*=============================================================*
*/
#include <stdio.h>
#include <stdlib.h>
#include "M451Series.h"
#include "Def.h"
#include "devCheck.h"
#include "nvti2c.h"
#include "timerctrl.h"
#include "i2cdev.h"
#include "flash.h"
#include "scheduler.h"
#include "battery.h"
#include "buzzer.h"
#include "led.h"
#include "Gas.h"
#include "ir.h"
#include "lr.h"
#include "retarget.h"
#include "AHRSLib.h"
#include "Sensors.h"
#include "Report.h"
#include "Calibrate.h"
#include "ahrs.h"
#include "sonar.h"
#include "key.h"
#include "timer1IRQ.h"
#include "i2c_ms.h"
#include "tiddev.h"
#include "tidmst.h"
#include "temp_HTU21D.h"
#define MAX_TID_DEV (15)
#define EMBEDDED_TID_DEV (9)
/***********************************************
Customized session to add another sensor boards
***********************************************/
/*
To include your board header file which declares PTID_Device_T and control functions.
for example:
#include "lightsensor.h"
*/
//#include "lightsensor.h"
PTID_Device_T pTidList[MAX_TID_DEV] =
{
/* for embedded device boards */
&BatDev,
&BuzDev,
&LedDev,
&AHRSDev,
&SonDev,
&TempDev,
&GasDev,
&IRDev,
&KeyDev,
/* for custom device boards
fill your PTID_Device_T structure here to specified ID. */
&ResDev9, // ID 9
&ResDev10, // ID 10
&ResDev11, // ID 11
&ResDev12, // ID 12
&ResDev13, // ID 13
&ResDev14 // ID 14
};
APFN_FUNC_T pfnDevFunc[MAX_TID_DEV] =
{
/*
Functions for each device:
{Initial, Period, Pulling, Report}
Initial : Init and configure device
Period : Process device sensor data or set sensor once per 0.1 seconds.
Pulling : Process device sensor data or set sensor frequently.
Report : Report process data to master
*/
/* for embedded device boards */
{Battery_Init, MasterControl, NULL, report_battery},
{Buzzer_Init, NULL, Buzzer_Control, report_buzzer},
{Led_Init, NULL, Led_Control, report_led},
{AHRS_Init, AHRS_Control, NULL, report_ahrs},
{SonarInit, SonarDetect, SonarTimeOutCheck, report_sonar},
{HTU21D_Init, WaitHTU21D, GetHTU21DTemp, report_temp},
{Gas_Init, GetGas, NULL, report_gas},
{IR_Init, IR_Control, IR_Check, report_ir},
{key_init, NULL, NULL, report_key},
/* for custom device boards
fill your four board functions here to specified ID. */
{Lr_Init, GetLr, NULL, report_resDev9}, // For device ID 9
{NULL, NULL, NULL, report_resDev10}, // For device ID 10
{NULL, NULL, NULL, report_resDev11}, // For device ID 11
{NULL, NULL, NULL, report_resDev12}, // For device ID 12
{NULL, NULL, NULL, report_resDev13}, // For device ID 13
{NULL, NULL, NULL, report_resDev14} // For device ID 14
};
/***********************************************
End of customized session
***********************************************/
volatile uint32_t RecentTimeCounter;
volatile uint32_t MainTimeMSCounter;
#define SYSTEM_CLOCK 72000000
void setupSystemClock()
{
SYS_UnlockReg();
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Waiting for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Switch HCLK clock source to HIRC */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable external XTAL 12MHz clock */
//CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Waiting for external XTAL clock ready */
//CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
CLK_SetCoreClock(SYSTEM_CLOCK);
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
/* Waiting for PLL clock ready */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
SYS_LockReg();
}
void setupUART()
{
/* Enable peripheral clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Peripheral clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));
/* Set PD multi-function pins for UART0 RXD, TXD */
SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
printf("Version:%d\n", VERSION_CODE);
}
void setup()
{
int i;
setupSystemClock();
setupUART();
setup_system_tick(SYSTEM_TICK_FREQ);
ID_Init(); // MO: Is it also system init?
GetID();
/* Init AHRS I2C */
/* ----Initialize I2C slave mode--- */
if(devNum == 0)
I2C_MS_Master_Init();
else
I2C_MS_Slave_Init();
//I2CWakeUpInit();
TIDMstFirstInitFIN = 0;
TIDMstInitFIN = 0;
TIDMstStage = 0;
TIDMstInitDevState = 1;
FlashInit();
// Setup function pointers for default sensor boards.
for(i=0; i<MAX_TID_DEV; i++)
{
if (pTidList[i])
SetDeviceFunction(pTidList[i], &(pfnDevFunc[i]));
}
// Call board init function
if (pTidList[devNum]->func.pfnSetup)
pTidList[devNum]->func.pfnSetup();
// Load TID feature from flash
GetFlashTID(&(pTidList[devNum]->Feature), devNum);
// Set Timer1 to (1000/value) in (Hz)
Timer1Init(1000 / (pTidList[devNum]->Feature.data1.value));
/* TID initialize */
SlvDataInit();
MstDataInit();
}
// Main Control loop
void loop()
{
CommandProcess();
TaskScheduler();
PowerControl();
// ******************************************************
// Get data for once erery 0.1s
// ******************************************************
if(TMR1INTCount != RecentTimeCounter)
{
MainTimeMSCounter = getTickCount();
if (pTidList[devNum]->func.pfnPeriod)
pTidList[devNum]->func.pfnPeriod();
RecentTimeCounter = TMR1INTCount;
}
// =============TID Master handle received data===============
// -----------TID Master handle received data------------
if(devNum == 0)
{
if(TIDMstInitFIN==0)
{
TIDMst_GetDev();
}
else if(TIDMstInitFIN==1)
{
TIDMstDevTRx();
}
}
// ==================== SLAVE PART ======================
// -----------TID slave handle received data--------------
else
{
if(I2CMS_SlvRxFin==1)
TID_SlvRxUpdate();
}
// -----------Slave part excute subfunction--------------
if (pTidList[devNum]->func.pfnPulling)
pTidList[devNum]->func.pfnPulling();
// --------------TID Slave Store data-----------------
if(devNum != 0)
SlvDataStore();
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
setup();
while(true) loop();
}