-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrf_dfu_flash_buttonless.c
170 lines (135 loc) · 4.51 KB
/
nrf_dfu_flash_buttonless.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
/* Copyright (c) 2016 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "nrf_dfu_flash.h"
#include "nrf_dfu_types.h"
#include "softdevice_handler.h"
#include "nrf_log.h"
#ifdef SOFTDEVICE_PRESENT
// Only include fstorage if SD interaction is required
#include "fstorage.h"
#endif
#define FLASH_FLAG_NONE (0)
#define FLASH_FLAG_OPER (1<<0)
#define FLASH_FLAG_FAILURE_SINCE_LAST (1<<1)
#define FLASH_FLAG_SD_ENABLED (1<<2)
static uint32_t m_flags;
#ifdef BLE_STACK_SUPPORT_REQD
// Function prototypes
static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result);
FS_REGISTER_CFG(fs_config_t fs_dfu_config) =
{
.callback = fs_evt_handler, // Function for event callbacks.
.p_start_addr = (uint32_t*)MBR_SIZE,
.p_end_addr = (uint32_t*)BOOTLOADER_SETTINGS_ADDRESS + CODE_PAGE_SIZE
};
static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
{
// Clear the operation flag
m_flags &= ~FLASH_FLAG_OPER;
if (result == FS_SUCCESS)
{
// Clear flag for ongoing operation and failure since last
m_flags &= ~FLASH_FLAG_FAILURE_SINCE_LAST;
}
else
{
NRF_LOG_INFO("Generating failure\r\n");
m_flags |= FLASH_FLAG_FAILURE_SINCE_LAST;
}
if (evt->p_context)
{
//lint -e611
((dfu_flash_callback_t)evt->p_context)(evt, result);
}
}
#endif
uint32_t nrf_dfu_flash_init(bool sd_enabled)
{
m_flags = FLASH_FLAG_SD_ENABLED;
return NRF_SUCCESS;
}
fs_ret_t nrf_dfu_flash_store(uint32_t const * p_dest, uint32_t const * const p_src, uint32_t len_words, dfu_flash_callback_t callback)
{
fs_ret_t ret_val = FS_SUCCESS;
if ((m_flags & FLASH_FLAG_SD_ENABLED) != 0)
{
// Check if there is a pending error
if ((m_flags & FLASH_FLAG_FAILURE_SINCE_LAST) != 0)
{
NRF_LOG_INFO("Flash: Failure since last\r\n");
return FS_ERR_FAILURE_SINCE_LAST;
}
// Set the flag to indicate ongoing operation
m_flags |= FLASH_FLAG_OPER;
//lint -e611
ret_val = fs_store(&fs_dfu_config, p_dest, p_src, len_words, (void*)callback);
if (ret_val != FS_SUCCESS)
{
NRF_LOG_INFO("Flash: failed %d\r\n", ret_val);
return ret_val;
}
// Set the flag to indicate ongoing operation
m_flags |= FLASH_FLAG_OPER;
}
return ret_val;
}
/** @brief Internal function to initialize DFU BLE transport
*/
fs_ret_t nrf_dfu_flash_erase(uint32_t const * p_dest, uint32_t num_pages, dfu_flash_callback_t callback)
{
fs_ret_t ret_val = FS_SUCCESS;
NRF_LOG_INFO("Erasing: 0x%08x, num: %d\r\n", (uint32_t)p_dest, num_pages);
if ((m_flags & FLASH_FLAG_SD_ENABLED) != 0)
{
// Check if there is a pending error
if ((m_flags & FLASH_FLAG_FAILURE_SINCE_LAST) != 0)
{
NRF_LOG_INFO("Erase: Failure since last\r\n");
return FS_ERR_FAILURE_SINCE_LAST;
}
m_flags |= FLASH_FLAG_OPER;
ret_val = fs_erase(&fs_dfu_config, p_dest, num_pages, (void*)callback);
if (ret_val != FS_SUCCESS)
{
NRF_LOG_INFO("Erase failed: %d\r\n", ret_val);
m_flags &= ~FLASH_FLAG_OPER;
return ret_val;
}
// Set the flag to indicate ongoing operation
m_flags |= FLASH_FLAG_OPER;
}
return ret_val;
}
void nrf_dfu_flash_error_clear(void)
{
m_flags &= ~FLASH_FLAG_FAILURE_SINCE_LAST;
}
fs_ret_t nrf_dfu_flash_wait(void)
{
NRF_LOG_INFO("Waiting for finished...\r\n");
#ifdef BLE_STACK_SUPPORT_REQD
if ((m_flags & FLASH_FLAG_SD_ENABLED) != 0)
{
while ((m_flags & FLASH_FLAG_OPER) != 0)
{
(void)sd_app_evt_wait();
}
if ((m_flags & FLASH_FLAG_FAILURE_SINCE_LAST) != 0)
{
NRF_LOG_INFO("Failure since last\r\n");
return FS_ERR_FAILURE_SINCE_LAST;
}
}
#endif
NRF_LOG_INFO("After wait!\r\n");
return FS_SUCCESS;
}