flashpage.c
1.3 KB
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
/*
* Copyright (C) 2014 INRIA
* 2017 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_msp430fxyz
* @{
*
* @file
* @brief Implementation of the peripheral flashpage interface
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include "cpu.h"
#include "irq.h"
#include "periph/flashpage.h"
void flashpage_write(int page, void *data)
{
assert(page < FLASHPAGE_NUMOF);
uint8_t *src = (uint8_t *)data;
uint8_t *dst = (uint8_t *)flashpage_addr(page);
unsigned istate;
/* disable interrupts and unlock flash */
istate = irq_disable();
FCTL3 = FWKEY;
while (FCTL3 & BUSY) {}
/* erase page */
FCTL1 = (FWKEY | ERASE);
*dst = 0; /* erases the page */
while (FCTL3 & BUSY) {}
if (data) {
FCTL1 = (FWKEY | WRT);
for (unsigned i = 0; i < FLASHPAGE_SIZE; i++) {
*(dst++) = *(src++);
while (!(FCTL3 & WAIT)) {}
}
}
/* lock flash and re-enable interrupts */
FCTL1 = (FWKEY);
FCTL3 = (FWKEY | LOCK);
irq_restore(istate);
}