forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrf52.c
130 lines (114 loc) · 3.04 KB
/
nrf52.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
/* nrf52.c
*
* Copyright (C) 2021 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef TARGET_nrf52
#include <stdint.h>
#include "image.h"
#include "nrf52.h"
#ifdef DEBUG_UART
void uart_init(void)
{
UART0_BAUDRATE = BAUD_115200;
UART0_ENABLE = 1;
}
static void uart_write_char(char c)
{
UART0_EVENT_ENDTX = 0;
UART0_TXD_PTR = (uint32_t)(&c);
UART0_TXD_MAXCOUNT = 1;
UART0_TASK_STARTTX = 1;
while(UART0_EVENT_ENDTX == 0)
;
}
void uart_write(const char* buf, unsigned int sz)
{
uint32_t pos = 0;
while (sz-- > 0) {
char c = buf[pos++];
if (c == '\n') { /* handle CRLF */
uart_write_char('\r');
}
uart_write_char(c);
}
}
#endif /* DEBUG_UART */
static void RAMFUNCTION flash_wait_complete(void)
{
while (NVMC_READY == 0)
;
}
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
int i = 0;
uint32_t *src, *dst;
while (i < len) {
if ((len - i > 3) && ((((address + i) & 0x03) == 0) && ((((uint32_t)data) + i) & 0x03) == 0)) {
src = (uint32_t *)data;
dst = (uint32_t *)address;
NVMC_CONFIG = NVMC_CONFIG_WEN;
flash_wait_complete();
dst[i >> 2] = src[i >> 2];
flash_wait_complete();
i+=4;
} else {
uint32_t val;
uint8_t *vbytes = (uint8_t *)(&val);
int off = (address + i) - (((address + i) >> 2) << 2);
dst = (uint32_t *)(address - off);
val = dst[i >> 2];
vbytes[off] = data[i];
NVMC_CONFIG = NVMC_CONFIG_WEN;
flash_wait_complete();
dst[i >> 2] = val;
flash_wait_complete();
i++;
}
}
return 0;
}
void RAMFUNCTION hal_flash_unlock(void)
{
}
void RAMFUNCTION hal_flash_lock(void)
{
}
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
{
uint32_t end = address + len - 1;
uint32_t p;
for (p = address; p <= end; p += FLASH_PAGE_SIZE) {
NVMC_CONFIG = NVMC_CONFIG_EEN;
flash_wait_complete();
NVMC_ERASEPAGE = p;
flash_wait_complete();
}
return 0;
}
void hal_init(void)
{
TASKS_HFCLKSTART = 1;
while(TASKS_HFCLKSTARTED == 0)
;
}
void hal_prepare_boot(void)
{
TASKS_HFCLKSTOP = 1;
}
#endif /* TARGET_nrf52 */