-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
174 lines (141 loc) · 4.74 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
/** @file
*
* @defgroup ble_sdk_app_template_main main.c
* @{
* @ingroup ble_sdk_app_template
* @brief Template project main file.
*
* This file contains a template for creating a new application. It has the code
* necessary to advertise, get a connection restart advertising on disconnect
* and if no new connection created go back to system-off mode.
*
* It can easily be used as a starting point for creating a new application, the
* comments identified with 'YOUR_JOB' indicates where and how you can customize.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "app_error.h"
#include "app_timer.h"
#include "fds.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "app_ble.h"
#include "version.h"
/**@brief Function for the Timer initialization.
*
* @details Initializes the timer module. This creates and starts application timers.
*/
static void timers_init(void) {
// Initialize timer module.
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
// Create timers.
/* YOUR_JOB: Create any timers to be used by the application.
Below is an example of how to create a timer.
For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
one.
ret_code_t err_code;
err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
APP_ERROR_CHECK(err_code); */
}
/**@brief Function for starting timers.
*/
static void application_timers_start(void) {
/* YOUR_JOB: Start your timers. below is an example of how to start a timer.
ret_code_t err_code;
err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
APP_ERROR_CHECK(err_code); */
}
/**@brief Function for putting the chip into sleep mode.
*
* @note This function will not return.
*/
static void sleep_mode_enter(void) {
ret_code_t err_code;
//err_code = bsp_indication_set(BSP_INDICATE_IDLE);
//APP_ERROR_CHECK(err_code);
// Prepare wakeup buttons.
//err_code = bsp_btn_ble_sleep_mode_prepare();
//APP_ERROR_CHECK(err_code);
// Go to system-off mode (this function will not return; wakeup will cause a reset).
err_code = sd_power_system_off();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the nrf log module.
*/
static void log_init(void) {
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_DEBUG("\r\n\r\n##### v%d.%d.%d compiled on %s at %s #####\r\n",
VERSION_FW_MAJOR, VERSION_FW_MINOR, VERSION_FW_BUILD, VERSION_FW_BUILD_DATE, VERSION_FW_BUILD_TIME);
}
/**@brief Function for initializing power management.
*/
static void power_management_init(void) {
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling the idle state (main loop).
*
* @details If there is no pending log operation, then sleep until next the next event occurs.
*/
static void idle_state_handle(void) {
if (NRF_LOG_PROCESS() == false) {
nrf_pwr_mgmt_run();
}
}
/**@brief Function for application main entry.
*/
int main(void) {
bool erase_bonds = false;
// Initialize.
log_init();
timers_init();
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
advertising_init();
services_init();
conn_params_init();
peer_manager_init();
// Start execution.
NRF_LOG_INFO("Template example started.");
NRF_LOG_FLUSH();
application_timers_start();
advertising_start(erase_bonds);
NRF_LOG_DEBUG("Advertising");
NRF_LOG_FLUSH();
// Enter main loop.
for (;;) {
idle_state_handle();
}
}
// If DEBUG is defined then app_error_weak.c module provides good and explanatory error handler.
#ifndef DEBUG
/**@brief Callback function for asserts in the SoftDevice.
*
* @details This function will be called in case of an assert in the SoftDevice.
*
* @warning This handler is an example only and does not fit a final product. You need to analyze
* how your product is supposed to react in case of Assert.
* @warning On assert from the SoftDevice, the system can only recover on reset.
*
* @param[in] line_num Line number of the failing ASSERT call.
* @param[in] file_name File name of the failing ASSERT call.
*/
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) {
/* 0xDEADBEEF is value used as error code on stack dump, can be used to identify stack location on stack unwind. */
app_error_handler(0xDEADBEEF, line_num, p_file_name);
}
#endif
/**
* @}
*/