Contiki-NG
flash.c
Go to the documentation of this file.
1 /**
2  * \file
3  * Functions for reading and writing flash ROM.
4  * \author Adam Dunkels <adam@sics.se>
5  */
6 
7 /* Copyright (c) 2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  *
33  * Author: Adam Dunkels <adam@sics.se>
34  *
35  */
36 
37 #include "contiki.h"
38 #include "dev/flash.h"
39 #include "dev/watchdog.h"
40 
41 #define FLASH_TIMEOUT 30
42 #define FLASH_REQ_TIMEOUT 150
43 
44 static unsigned short ie1, ie2;
45 
46 /*---------------------------------------------------------------------------*/
47 void
49 {
50  /* disable all interrupts to protect CPU
51  during programming from system crash */
52  _DINT();
53 
54  /* Clear interrupt flag1. */
55  /* IFG1 = 0; */
56  /* The IFG1 = 0; statement locks up contikimac - not sure if this
57  statement needs to be here at all. I've removed it for now, since
58  it seems to work, but leave this little note here in case someone
59  stumbles over this code at some point. */
60 
61  /* Stop watchdog. */
62  watchdog_stop();
63 
64  /* DCO(SMCLK) is 2,4576MHz, /6 = 409600 Hz
65  select SMCLK for flash timing, divider 5+1 */
66  FCTL2 = 0xA5C5;
67 
68  /* disable all NMI-Interrupt sources */
69  ie1 = IE1;
70  ie2 = IE2;
71  IE1 = 0x00;
72  IE2 = 0x00;
73 }
74 /*---------------------------------------------------------------------------*/
75 void
77 {
78  /* Enable interrupts. */
79  IE1 = ie1;
80  IE2 = ie2;
81  _EINT();
83 }
84 /*---------------------------------------------------------------------------*/
85 void
86 flash_clear(unsigned short *ptr)
87 {
88  FCTL3 = 0xA500; /* Lock = 0 */
89  while(FCTL3 & 0x0001) nop(); /* Wait for BUSY = 0, not needed
90  unless run from RAM */
91  FCTL1 = 0xA502; /* ERASE = 1 */
92  *ptr = 0; /* erase Flash segment */
93  FCTL1 = 0xA500; /* ERASE = 0 automatically done?! */
94  FCTL3 = 0xA510; /* Lock = 1 */
95 }
96 /*---------------------------------------------------------------------------*/
97 void
98 flash_write(unsigned short *ptr, unsigned short word)
99 {
100  FCTL3 = 0xA500; /* Lock = 0 */
101  while(FCTL3 & 0x0001) nop(); /* Wait for BUSY = 0, not needed unless
102  run from RAM */
103  FCTL1 = 0xA540; /* WRT = 1 */
104  *ptr = word; /* program Flash word */
105  FCTL1 = 0xA500; /* WRT = 0 */
106  FCTL3 = 0xA510; /* Lock = 1 */
107 }
108 /*---------------------------------------------------------------------------*/
void flash_done(void)
Function that is to be called after flashing is done.
Definition: flash.c:76
void flash_write(unsigned short *ptr, unsigned short word)
Write a 16-bit word to flash ROM.
Definition: flash.c:98
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:72
void flash_clear(unsigned short *ptr)
Clear a 16-bit word in flash ROM.
Definition: flash.c:86
void flash_setup(void)
Setup function to be called before any of the flash programming functions.
Definition: flash.c:48
void watchdog_stop(void)
Stops the WDT such that it won&#39;t timeout and cause MCU reset.