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  * Author: Adam Dunkels <adam@sics.se>
33  *
34  */
35 
36 #include "contiki.h"
37 #include "dev/flash.h"
38 #include "dev/watchdog.h"
39 
40 #define FLASH_TIMEOUT 30
41 #define FLASH_REQ_TIMEOUT 150
42 
43 static uint16_t sfrie;
44 
45 /*---------------------------------------------------------------------------*/
46 void
48 {
49  /* disable all interrupts to protect CPU
50  during programming from system crash */
51  dint();
52 
53  /* Clear interrupt flag1. */
54  SFRIFG1 = 0;
55  /* The IFG1 = 0; statement locks up contikimac - not sure if this
56  statement needs to be here at all. I've removed it for now, since
57  it seems to work, but leave this little note here in case someone
58  stumbles over this code at some point. */
59 
60  /* Stop watchdog. */
61  watchdog_stop();
62 
63  /* disable all NMI-Interrupt sources */
64  sfrie = SFRIE1;
65  SFRIE1 = 0x00;
66 }
67 /*---------------------------------------------------------------------------*/
68 void
70 {
71  /* Enable interrupts. */
72  SFRIE1 = sfrie;
73  eint();
75 }
76 /*---------------------------------------------------------------------------*/
77 void
78 flash_clear(unsigned short *ptr)
79 {
80  uint8_t r;
81  FCTL3 = 0xA500; /* Lock = 0 */
82  while(FCTL3 & 0x0001) r++; /* Wait for BUSY = 0, not needed
83  unless run from RAM */
84  FCTL1 = 0xA502; /* ERASE = 1 */
85  *ptr = 0; /* erase Flash segment */
86  FCTL1 = 0xA500; /* ERASE = 0 automatically done?! */
87  FCTL3 = 0xA510; /* Lock = 1 */
88 }
89 /*---------------------------------------------------------------------------*/
90 void
91 flash_write(unsigned short *ptr, unsigned short word)
92 {
93  uint8_t r;
94  FCTL3 = 0xA500; /* Lock = 0 */
95  while(FCTL3 & 0x0001) r++; /* Wait for BUSY = 0, not needed unless
96  run from RAM */
97  FCTL1 = 0xA540; /* WRT = 1 */
98  *ptr = word; /* program Flash word */
99  FCTL1 = 0xA500; /* WRT = 0 */
100  FCTL3 = 0xA510; /* Lock = 1 */
101 }
102 /*---------------------------------------------------------------------------*/
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