Contiki 3.x
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 <stdlib.h>
39 
40 #include "dev/flash.h"
41 #include "dev/watchdog.h"
42 
43 #define FLASH_TIMEOUT 30
44 #define FLASH_REQ_TIMEOUT 150
45 
46 #define INFOMEM_LO (unsigned short *)0x1800
47 #define INFOMEM_HI (unsigned short *)0x1a00
48 
49 static uint16_t sfrie;
50 
51 /*---------------------------------------------------------------------------*/
52 void
54 {
55  /* disable all interrupts to protect CPU
56  during programming from system crash */
57  dint();
58 
59  /* Clear interrupt flag1. */
60  SFRIFG1 = 0;
61  /* The IFG1 = 0; statement locks up contikimac - not sure if this
62  statement needs to be here at all. I've removed it for now, since
63  it seems to work, but leave this little note here in case someone
64  stumbles over this code at some point. */
65 
66  /* Stop watchdog. */
67  watchdog_stop();
68 
69  /* disable all NMI-Interrupt sources */
70  sfrie = SFRIE1;
71  SFRIE1 = 0x00;
72 }
73 /*---------------------------------------------------------------------------*/
74 void
76 {
77  /* Enable interrupts. */
78  SFRIE1 = sfrie;
79  eint();
81 }
82 /*---------------------------------------------------------------------------*/
83 static void
84 unlock_infomem(void)
85 {
86  FCTL4 = 0xa500;
87  FCTL3 = 0xa540;
88 }
89 /*---------------------------------------------------------------------------*/
90 static void
91 lock_infomem(void)
92 {
93  FCTL3 = 0xa540;
94  FCTL4 = 0xa580;
95 }
96 /*---------------------------------------------------------------------------*/
97 void
98 flash_clear(unsigned short *ptr)
99 {
100  uint8_t r;
101 
102  /* If ptr is in infomem, we need to unlock it first. */
103  if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) {
104  unlock_infomem();
105  }
106 
107  FCTL3 = 0xa500; /* Lock = 0 */
108  while(FCTL3 & 0x0001) {
109  r++; /* Wait for BUSY = 0, not needed
110  unless run from RAM */
111  }
112  FCTL1 = 0xa502; /* ERASE = 1 */
113  *ptr = 0; /* erase Flash segment */
114  FCTL1 = 0xa500; /* ERASE = 0 automatically done?! */
115  FCTL3 = 0xa510; /* Lock = 1 */
116 
117  if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) {
118  lock_infomem();
119  }
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 flash_write(unsigned short *ptr, unsigned short word)
124 {
125  uint8_t r;
126 
127  /* If ptr is in infomem, we need to unlock it first. */
128  if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) {
129  unlock_infomem();
130  }
131 
132  FCTL3 = 0xa500; /* Lock = 0 */
133  while(FCTL3 & 0x0001) {
134  r++; /* Wait for BUSY = 0, not needed unless
135  run from RAM */
136  }
137  FCTL1 = 0xa540; /* WRT = 1 */
138  *ptr = word; /* program Flash word */
139  FCTL1 = 0xa500; /* WRT = 0 */
140  FCTL3 = 0xa510; /* Lock = 1 */
141 
142  if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) {
143  lock_infomem();
144  }
145 }
146 /*---------------------------------------------------------------------------*/
void flash_clear(unsigned short *ptr)
Clear a 16-bit word in flash ROM.
Definition: flash.c:86
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
void watchdog_stop(void)
In watchdog mode, the WDT can not be stopped.
Definition: watchdog.c:58
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 flash_setup(void)
Setup function to be called before any of the flash programming functions.
Definition: flash.c:48