Contiki 2.5
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  * $Id: flash.c,v 1.3 2010/11/15 21:52:54 adamdunkels Exp $
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 
38 #include "contiki.h"
39 #include "dev/flash.h"
40 #include "dev/watchdog.h"
41 
42 #define FLASH_TIMEOUT 30
43 #define FLASH_REQ_TIMEOUT 150
44 
45 static unsigned short ie1, ie2;
46 
47 /*---------------------------------------------------------------------------*/
48 void
50 {
51  /* disable all interrupts to protect CPU
52  during programming from system crash */
53  _DINT();
54 
55  /* Clear interrupt flag1. */
56  /* IFG1 = 0; */
57  /* The IFG1 = 0; statement locks up contikimac - not sure if this
58  statement needs to be here at all. I've removed it for now, since
59  it seems to work, but leave this little note here in case someone
60  stumbles over this code at some point. */
61 
62  /* Stop watchdog. */
63  watchdog_stop();
64 
65  /* DCO(SMCLK) is 2,4576MHz, /6 = 409600 Hz
66  select SMCLK for flash timing, divider 5+1 */
67  FCTL2 = 0xA5C5;
68 
69  /* disable all NMI-Interrupt sources */
70  ie1 = IE1;
71  ie2 = IE2;
72  IE1 = 0x00;
73  IE2 = 0x00;
74 }
75 /*---------------------------------------------------------------------------*/
76 void
78 {
79  /* Enable interrupts. */
80  IE1 = ie1;
81  IE2 = ie2;
82  _EINT();
83  watchdog_start();
84 }
85 /*---------------------------------------------------------------------------*/
86 void
87 flash_clear(unsigned short *ptr)
88 {
89  FCTL3 = 0xA500; /* Lock = 0 */
90  while(FCTL3 & 0x0001) nop(); /* Wait for BUSY = 0, not needed
91  unless run from RAM */
92  FCTL1 = 0xA502; /* ERASE = 1 */
93  *ptr = 0; /* erase Flash segment */
94  FCTL1 = 0xA500; /* ERASE = 0 automatically done?! */
95  FCTL3 = 0xA510; /* Lock = 1 */
96 }
97 /*---------------------------------------------------------------------------*/
98 void
99 flash_write(unsigned short *ptr, unsigned short word)
100 {
101  FCTL3 = 0xA500; /* Lock = 0 */
102  while(FCTL3 & 0x0001) nop(); /* Wait for BUSY = 0, not needed unless
103  run from RAM */
104  FCTL1 = 0xA540; /* WRT = 1 */
105  *ptr = word; /* program Flash word */
106  FCTL1 = 0xA500; /* WRT = 0 */
107  FCTL3 = 0xA510; /* Lock = 1 */
108 }
109 /*---------------------------------------------------------------------------*/