-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.c
151 lines (121 loc) · 3.58 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
#include "nrf.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "uartreader.h"
#include "hexparser.h"
#define APPLICATION_BASE_ADDRESS 0x14000
volatile bool is_bootloader_running = true;
typedef void (*application_main_t)(void);
void init(void)
{
if (NRF_UICR->CLENR0 == 0xFFFFFFFF)
{
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
NRF_UICR->CLENR0 = APPLICATION_BASE_ADDRESS;
while (!NRF_NVMC->READY);
NRF_NVMC->CONFIG = 0;
NVIC_SystemReset();
}
}
void erase_app(void)
{
uint32_t last_page_address = NRF_FICR->CODEPAGESIZE * NRF_FICR->CODESIZE;
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos;
for (uint32_t address = APPLICATION_BASE_ADDRESS; address < last_page_address; address += NRF_FICR->CODEPAGESIZE)
{
NRF_NVMC->ERASEPAGE = address;
while (!NRF_NVMC->READY);
}
NRF_NVMC->CONFIG = 0x0;
}
void write_record(uint16_t base_address, hexparser_record * record)
{
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
for (uint32_t i = 0; i < record->byte_count / 4; i++)
{
*(uint32_t *) (base_address << 4 | (record->address + (4*i))) = record->data.words[i];
while (NRF_NVMC->READY & (NVMC_READY_READY_Busy << NVMC_READY_READY_Pos))
{
}
}
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
}
uartreader_response_t write_line(uartreader_evt_t * evt)
{
nrf_gpio_pin_toggle(LED0);
static uint16_t base_address = 0;
hexparser_record record;
hexparser_parse_string((char * ) evt->data, evt->len, &record);
if (!hexparser_is_record_valid(&record))
{
return FAILURE;
}
switch (record.type)
{
case EXTENDED_LINEAR_ADDRESS_RECORD:
case EXTENDED_SEGMENT_ADDRESS_RECORD:
nrf_gpio_pin_toggle(LED1);
base_address = record.data.words[0];
break;
case DATA_RECORD:
nrf_gpio_pin_toggle(LED0);
write_record(base_address, &record);
break;
default:
nrf_gpio_pin_toggle(LED0);
nrf_gpio_pin_toggle(LED1);
break;
}
return SUCCESS;
}
void uart_handler(uartreader_evt_t * evt)
{
uartreader_response_t result;
switch (evt->cmd)
{
case ERASE_APP:
erase_app();
uartreader_send_response(SUCCESS);
break;
case WRITE_LINE:
result = write_line(evt);
uartreader_send_response(result);
break;
case RESET_AND_RUN:
uartreader_send_response(SUCCESS);
NVIC_SystemReset();
break;
case NOP:
uartreader_send_response(SUCCESS);
break;
}
}
int main(void)
{
nrf_gpio_cfg_output(LED0);
nrf_gpio_cfg_output(LED1);
nrf_gpio_cfg_input(BUTTON0, NRF_GPIO_PIN_PULLUP);
init();
application_main_t application_main = *(application_main_t *)(APPLICATION_BASE_ADDRESS+4);
if ((application_main != (application_main_t) 0xFFFFFFFF) &&
(nrf_gpio_pin_read(BUTTON0) != 0))
{
is_bootloader_running = false;
application_main();
}
is_bootloader_running = true;
nrf_gpio_pin_set(LED0);
uartreader_init_t init;
init.baudrate = UART_BAUDRATE_BAUDRATE_Baud38400;
init.txd_pin_no = TX_PIN_NUMBER;
init.rxd_pin_no = RX_PIN_NUMBER;
init.cts_pin_no = CTS_PIN_NUMBER;
init.rts_pin_no = RTS_PIN_NUMBER;
init.hwfc = HWFC;
init.evt_handler = uart_handler;
uartreader_init(&init);
while (1)
{
__WFI();
}
}