This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.c
166 lines (123 loc) · 3.66 KB
/
flash.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
#include "flash.h"
#include "dev.h"
#include <signal.h>
/* Values of the registers */
#define FCTL1_VAL 0
/* BLKWRT = 0: Byte/word write mode (not block) */
/* WRT = 0: Not in write mode at the moment */
/* EEIEX = 0: No interrupts during write */
/* EEI = 0: No interrupts during erase */
/* MERAS:ERASE = 0: No erase. */;
#define FCTL2_VAL (FSSEL_SMCLK | 43)
/* Flash controller clocked by SMCLK */
/* Divide the 16 MHz DCO to 363 KHz: */
#define FCTL3_VAL 0
/* Lock the flash against writes and erasure */
#define flash_lock() do { FCTL3 = FCTL3_VAL | FWKEY | LOCK; } while (0)
#define flash_unlock() do { FCTL3 = FCTL3_VAL | FWKEY; } while (0)
#define flash_busy() ( FCTL3 & BUSY )
#define flash_write_mode() do { FCTL1 = FCTL1_VAL | FWKEY | WRT; } while (0)
#define flash_write_mode_off() do { FCTL1 = FCTL1_VAL | FWKEY; } while (0)
/* Get a pointer to the segment that x is in */
#define mem_segment(x) ( (uint16_t*)(((uint16_t)x) & 0xfe00 ) )
/* The address of the next chunk */
uint16_t *next_chunk;
/* Pointer to the last area to be erased */
static uint16_t *last_erased;
/* Buffer for the interrupt vector table */
static uint8_t ivt_buf[64];
bool firmware_received;
/* For grabbing 16-bit words from 8-bit buffers */
#define fw_word(buf,n) ( ((uint16_t)buf[n*2]) | (((uint16_t)buf[n*2+1])<<8) )
void flash_write_chunk( const uint8_t *source, uint16_t *dest )
{
uint8_t i;
/* Save WDT state and then disable it. Also clear it just to be safe */
uint8_t wdt_state = 0xff & WDTCTL;
WDTCTL = WDTPW | WDTHOLD | WDTCNTCL;
/* Check that the section has been erased */
if( last_erased < mem_segment( dest ) )
flash_erase_segment( dest );
flash_unlock();
flash_write_mode();
/* Now write it */
for( i=0; i<(CHUNK_SIZE/2); i++ )
*(dest + i) = fw_word(source,i);
flash_write_mode_off();
flash_lock();
/* Restore WDT state */
WDTCTL = WDTPW | wdt_state;
}
void flash_init( void )
{
/* Pointer into the other area */
uint16_t *other_area;
FCTL1 = FCTL1_VAL | FWKEY;
FCTL2 = FCTL2_VAL | FWKEY;
flash_lock();
/* Calculate which area we need to write to */
if( (uint16_t)mem_segment( flash_init ) >= FLASH_AREA_1 )
other_area = (uint16_t*)FLASH_AREA_0;
else
other_area = (uint16_t*)FLASH_AREA_1;
/* Nothing's been written yet */
next_chunk = other_area;
firmware_received = false;
last_erased = mem_segment(other_area - 1);
}
void flash_erase_segment( uint16_t *addr )
{
flash_unlock();
/* Segment erase -> MERAS = 0, ERASE = 1 */
FCTL1 = (FCTL1 & (~FRKEY)) | FWKEY | ERASE;
/* Initiate the erase with a dummy write */
*addr = 0xffff;
flash_lock();
last_erased = mem_segment(addr);
}
void flash_rx_chunk( uint16_t c_addr, const uint8_t *fw )
{
uint8_t i;
/* Pointer to the chunk */
uint16_t *c = (uint16_t*)c_addr;
/* Only accept 16-byte aligned chunks */
if( c_addr % 16 != 0 )
return;
if( c >= IVT )
{
/* It's an interrupt vector table entry */
uint8_t cpos = (c - IVT)*2;
for( i=0; i<CHUNK_SIZE; i++ )
ivt_buf[i + cpos] = fw[i];
/* Last entry? */
if( c == IVT + 24 )
{
/* Got the IVT */
firmware_received = true;
next_chunk = 0;
}
else
next_chunk = c + CHUNK_SIZE/2;
}
else if( c == next_chunk )
{
flash_write_chunk( fw, c );
next_chunk += CHUNK_SIZE/2;
}
}
void flash_switchover( void )
{
uint8_t i;
if( !firmware_received )
return;
flash_write_mode_off();
/* Disable interrupts */
dint();
for( i=0; i<32; i += (CHUNK_SIZE/2) )
flash_write_chunk( ivt_buf + (i*2), IVT + i );
/* Tell our future self that we've just upgraded */
firmware_rebooted = FIRMWARE_REBOOT_MAGIC;
/* Finished loading new firmware! */
/* Jump to the reset vector! */
(*(void (*)()) (fw_word(ivt_buf,31))) ();
}