Contiki 2.6

flash.c

Go to the documentation of this file.
00001 /**
00002  * \file
00003  * Functions for reading and writing flash ROM.
00004  * \author Adam Dunkels <adam@sics.se>
00005  */
00006 
00007 /* Copyright (c) 2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * Author: Adam Dunkels <adam@sics.se>
00033  *
00034  */
00035 
00036 #include "contiki.h"
00037 #include "dev/flash.h"
00038 #include "dev/watchdog.h"
00039 
00040 #define FLASH_TIMEOUT 30
00041 #define FLASH_REQ_TIMEOUT 150
00042 
00043 static uint16_t sfrie;
00044 
00045 /*---------------------------------------------------------------------------*/
00046 void
00047 flash_setup(void)
00048 {
00049   /* disable all interrupts to protect CPU
00050      during programming from system crash */
00051   dint();
00052 
00053   /* Clear interrupt flag1. */
00054   SFRIFG1 = 0;
00055   /* The IFG1 = 0; statement locks up contikimac - not sure if this
00056      statement needs to be here at all. I've removed it for now, since
00057      it seems to work, but leave this little note here in case someone
00058      stumbles over this code at some point. */
00059 
00060   /* Stop watchdog. */
00061   watchdog_stop();
00062 
00063   /* disable all NMI-Interrupt sources */
00064   sfrie = SFRIE1;
00065   SFRIE1 = 0x00;
00066 }
00067 /*---------------------------------------------------------------------------*/
00068 void
00069 flash_done(void)
00070 {
00071   /* Enable interrupts. */
00072   SFRIE1 = sfrie;
00073   eint();
00074   watchdog_start();
00075 }
00076 /*---------------------------------------------------------------------------*/
00077 void
00078 flash_clear(unsigned short *ptr)
00079 {
00080   uint8_t r;
00081   FCTL3 = 0xA500;               /* Lock = 0 */
00082   while(FCTL3 & 0x0001) r++;  /* Wait for BUSY = 0, not needed
00083                                      unless run from RAM */
00084   FCTL1 = 0xA502;               /* ERASE = 1 */
00085   *ptr  = 0;                    /* erase Flash segment */
00086   FCTL1 = 0xA500;               /* ERASE = 0 automatically done?! */
00087   FCTL3 = 0xA510;               /* Lock = 1 */
00088 }
00089 /*---------------------------------------------------------------------------*/
00090 void
00091 flash_write(unsigned short *ptr, unsigned short word)
00092 {
00093   uint8_t r;
00094   FCTL3 = 0xA500;              /* Lock = 0 */
00095   while(FCTL3 & 0x0001) r++; /* Wait for BUSY = 0, not needed unless
00096                                   run from RAM */
00097   FCTL1 = 0xA540;              /* WRT = 1 */
00098   *ptr  = word;                /* program Flash word */
00099   FCTL1 = 0xA500;              /* WRT = 0 */
00100   FCTL3 = 0xA510;              /* Lock = 1 */
00101 }
00102 /*---------------------------------------------------------------------------*/