-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
201 lines (164 loc) · 7.14 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
/***************************************************************************//**
* @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: 08-07-2021
* 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.
*
******************************************************************************/
// *************************************************
// Students: It is OK to modify this file.
// Make edits appropriate for each
// assignment.
// *************************************************
#include "app.h"
// Include logging for this file
//#define INCLUDE_LOG_DEBUG 1
#include "src/log.h"
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_adc.h"
#include "src/scheduler.h"
#include "src/ble.h"
// 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.
//#define APP_IS_OK_TO_SLEEP (false)
//#define APP_IS_OK_TO_SLEEP (true)
#if LOWEST_ENERGY_MODE == EM0
#define APP_IS_OK_TO_SLEEP (false)
#else
#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)
/*****************************************************************************
* Application Power Manager callbacks
*****************************************************************************/
#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)
//#include "native_gecko.h"
/**************************************************************************//**
* Application Init.
*****************************************************************************/
SL_WEAK void app_init(void)
{
// Put your application 1-time init code here
// This is called once during start-up.
// Don't call any Bluetooth API functions until after the boot event.
// Student Edit: Add a call to gpioInit() here
gpioInit();
Oscillator_Init(); // Oscillator initialization
initLETIMER0(); // LETIMER initialization
/*Setting LEMODE EM1 and EM2 for Power Manager*/
if (LOWEST_ENERGY_MODE){
sl_power_manager_add_em_requirement(SL_POWER_MANAGER_EM1);
}
else if(LOWEST_ENERGY_MODE == EM2){
sl_power_manager_add_em_requirement(SL_POWER_MANAGER_EM2);
}
else{
//No Action
}
}
/**************************************************************************//**
* Application Process Action.
*****************************************************************************/
SL_WEAK void app_process_action(void)
{
// Put your application code here.
// 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.
/* No Action*/
} // app_process_action()
/**************************************************************************//**
* 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)
{
// Some events require responses from our application code,
// and don’t necessarily advance our state machines.
// For assignment 5 uncomment the next 2 function calls
handle_ble_event(evt); // put this code in ble.c/.h
// sequence through states driven by events
#if DEVICE_IS_BLE_SERVER
// SERVER
// sequence through states driven by events
state_machine(evt); // put this code in scheduler.c/.h
#else
//CLIENT
// sequence through service and characteristic discovery
discovery_state_machine(evt); // put this code in src/scheduler.c/.h
#endif
} // sl_bt_on_event()