-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
276 lines (231 loc) · 9.3 KB
/
app.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
/***************************************************************************//**
* @file
* @brief Core application logic.
*******************************************************************************
* # License
* <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* Date: 02-25-2022
* Author: Dave Sluiter
* Description: This code was created by the Silicon Labs application wizard
* and started as "Bluetooth - SoC Empty".
* It is to be used only for ECEN 5823 "IoT Embedded Firmware".
* The MSLA referenced above is in effect.
*
*
*
*
* @student Raghu Sai Phani Sriraj Vemparala, raghu.vemparala@colorado.edu
*
*
*
******************************************************************************/
#include "em_common.h"
#include "app_assert.h"
#include "sl_bluetooth.h"
#include "gatt_db.h"
#include "app.h"
#include "src/i2c.h"
#include "src/ble.h"
// *************************************************
// Students: It is OK to modify this file.
// Make edits appropriate for each
// assignment.
// *************************************************
#include "sl_status.h" // for sl_status_print()
#include "src/ble_device_type.h"
#include "src/gpio.h"
#include "src/lcd.h"
#include "src/oscillators.h"
#include "src/timer.h"
#include "src/scheduler.h"
// Students: Here is an example of how to correctly include logging functions in
// each .c file.
// Apply this technique to your other .c files.
// Do not #include "src/log.h" in any .h file! This logging scheme is
// designed to be included at the top of each .c file that you want
// to call one of the LOG_***() functions from.
// Include logging specifically for this .c file
#define INCLUDE_LOG_DEBUG 0
#include "src/log.h"
// *************************************************
// Power Manager
// *************************************************
// See: https://docs.silabs.com/gecko-platform/latest/service/power_manager/overview
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
// -----------------------------------------------------------------------------
// defines for power manager callbacks
// -----------------------------------------------------------------------------
// Return values for app_is_ok_to_sleep():
// Return false to keep sl_power_manager_sleep() from sleeping the MCU.
// Return true to allow system to sleep when you expect/want an IRQ to wake
// up the MCU from the call to sl_power_manager_sleep() in the main while (1)
// loop.
//
// Students: We'll need to modify this for A2 onward so that compile time we
// control what the lowest EM (energy mode) the MCU sleeps to. So
// think "#if (expression)".
#if LOWEST_ENERGY_MODE == 0
#define APP_IS_OK_TO_SLEEP (false)
#elif ((LOWEST_ENERGY_MODE == 1) || (LOWEST_ENERGY_MODE == 2) || (LOWEST_ENERGY_MODE == 3))
#define APP_IS_OK_TO_SLEEP (true)
#endif
// Return values for app_sleep_on_isr_exit():
// SL_POWER_MANAGER_IGNORE; // The module did not trigger an ISR and it doesn't want to contribute to the decision
// SL_POWER_MANAGER_SLEEP; // The module was the one that caused the system wakeup and the system SHOULD go back to sleep
// SL_POWER_MANAGER_WAKEUP; // The module was the one that caused the system wakeup and the system MUST NOT go back to sleep
//
// Notes:
// SL_POWER_MANAGER_IGNORE, we see calls to app_process_action() on each IRQ. This is the
// expected "normal" behavior.
//
// SL_POWER_MANAGER_SLEEP, the function app_process_action()
// in the main while(1) loop will not be called! It would seem that sl_power_manager_sleep()
// does not return in this case.
//
// SL_POWER_MANAGER_WAKEUP, doesn't seem to allow ISRs to run. Main while loop is
// running continuously, flooding the VCOM port with printf text with LETIMER0 IRQs
// disabled somehow, LED0 is not flashing.
#define APP_SLEEP_ON_ISR_EXIT (SL_POWER_MANAGER_IGNORE)
//#define APP_SLEEP_ON_ISR_EXIT (SL_POWER_MANAGER_SLEEP)
//#define APP_SLEEP_ON_ISR_EXIT (SL_POWER_MANAGER_WAKEUP)
#endif // defined(SL_CATALOG_POWER_MANAGER_PRESENT)
// *************************************************
// Power Manager Callbacks
// The values returned by these 2 functions AND
// adding and removing power manage requirements is
// how we control when EM mode the MCU goes to when
// sl_power_manager_sleep() is called in the main
// while (1) loop.
// *************************************************
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
bool app_is_ok_to_sleep(void)
{
return APP_IS_OK_TO_SLEEP;
} // app_is_ok_to_sleep()
sl_power_manager_on_isr_exit_t app_sleep_on_isr_exit(void)
{
return APP_SLEEP_ON_ISR_EXIT;
} // app_sleep_on_isr_exit()
#endif // defined(SL_CATALOG_POWER_MANAGER_PRESENT)
/**************************************************************************//**
* @Function SL_WEAK void app_init()
* @Description: Application initialization
* @Param NULL
* @Return NULL
*****************************************************************************/
SL_WEAK void app_init(void)
{
// Put your application 1-time initialization code here.
// This is called once during start-up.
// Don't call any Bluetooth API functions until after the boot event.
sl_power_manager_add_em_requirement(LOWEST_ENERGY_MODE);
gpioInit();
cmu_init();
init_LETIMER0();
NVIC_ClearPendingIRQ(LETIMER0_IRQn);
NVIC_EnableIRQ(LETIMER0_IRQn);
NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn);
NVIC_EnableIRQ(GPIO_EVEN_IRQn);
NVIC_ClearPendingIRQ(GPIO_ODD_IRQn);
NVIC_EnableIRQ(GPIO_ODD_IRQn);
} // app_init()
///*****************************************************************************
// * delayApprox(), private to this file.
// * A value of 3500000 is ~ 1 second. After assignment 1 you can delete or
// * comment out this function. Wait loops are a bad idea in general.
// * We'll discuss how to do this a better way in the next assignment.
// *****************************************************************************/
//static void delayApprox(int delay)
//{
// volatile int i;
//
// for (i = 0; i < delay; ) {
// i=i+1;
// }
//
//} // delayApprox()
/**************************************************************************
* @Function SL_WEAK void app_process_action(void)
* @Description LED toggling
* @Param NULL
* @Return NULL
*****************************************************************************/
SL_WEAK void app_process_action(void)
{
// Put your application code here for A1 to A4.
// This is called repeatedly from the main while(1) loop
// Notice: This function is not passed or has access to Bluetooth stack events.
// We will create/use a scheme that is far more energy efficient in
// later assignments.
// uint32_t evt = 0;
// LOG_INFO("Entering Inappprocessaction\n\r");
// evt = getNextEvent();
// ambient_light_state_machine(evt);
// switch (evt) {
// case evtLETimer_UF:
// read_temp_from_si7021();
// break;
// }
// if(uf_int)
// {
// /*Turn ON LED's 0*/
// gpioLed0SetOn();
// }
// else
// {
// /*Turn OFF LED's 1*/
// gpioLed0SetOff();
//
// }
}
/**************************************************************************//**
* Bluetooth stack event handler.
* This overrides the dummy weak implementation.
*
* @param[in] evt Event coming from the Bluetooth stack.
*
* The code here will process events from the Bluetooth stack. This is the only
* opportunity we will get to act on an event.
*
*****************************************************************************/
void sl_bt_on_event(sl_bt_msg_t *evt)
{
// Just a trick to hide a compiler warning about unused input parameter evt.
(void) evt;
// For A5 onward:
// Some events require responses from our application code,
// and don’t necessarily advance our state machines.
// For A5 uncomment the next 2 function calls
//LOG_INFO("Event =%d\n\r",evt->data.evt_system_external_signal.extsignals);
handle_ble_event(evt); // put this code in ble.c/.h
//GLIB_drawStringOnLine();
// sequence through states driven by events
// state_machine(evt); // put this code in scheduler.c/.h
#if DEVICE_IS_BLE_SERVER
ambient_light_state_machine(evt);
#else
discovery_state_machine(evt);
#endif
} // sl_bt_on_event()