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  * $Id: flash.c,v 1.3 2010/11/15 21:52:54 adamdunkels Exp $
00033  *
00034  * Author: Adam Dunkels <adam@sics.se>
00035  *
00036  */
00037 
00038 #include "contiki.h"
00039 #include "dev/flash.h"
00040 #include "dev/watchdog.h"
00041 
00042 #define FLASH_TIMEOUT 30
00043 #define FLASH_REQ_TIMEOUT 150
00044 
00045 static unsigned short ie1, ie2;
00046 
00047 /*---------------------------------------------------------------------------*/
00048 void
00049 flash_setup(void)
00050 {
00051   /* disable all interrupts to protect CPU
00052      during programming from system crash */
00053   _DINT();
00054 
00055   /* Clear interrupt flag1. */
00056   /*  IFG1 = 0; */
00057   /* The IFG1 = 0; statement locks up contikimac - not sure if this
00058      statement needs to be here at all. I've removed it for now, since
00059      it seems to work, but leave this little note here in case someone
00060      stumbles over this code at some point. */
00061 
00062   /* Stop watchdog. */
00063   watchdog_stop();
00064   
00065   /* DCO(SMCLK) is 2,4576MHz, /6 = 409600 Hz
00066      select SMCLK for flash timing, divider 5+1 */
00067   FCTL2 = 0xA5C5;
00068 
00069   /* disable all NMI-Interrupt sources */
00070   ie1 = IE1;
00071   ie2 = IE2;
00072   IE1 = 0x00;
00073   IE2 = 0x00;
00074 }
00075 /*---------------------------------------------------------------------------*/
00076 void
00077 flash_done(void)
00078 {
00079   /* Enable interrupts. */
00080   IE1 = ie1;
00081   IE2 = ie2;
00082   _EINT();
00083   watchdog_start();
00084 }
00085 /*---------------------------------------------------------------------------*/
00086 void
00087 flash_clear(unsigned short *ptr)
00088 {
00089   FCTL3 = 0xA500;               /* Lock = 0 */
00090   while(FCTL3 & 0x0001) nop();  /* Wait for BUSY = 0, not needed
00091                                    unless run from RAM */
00092   FCTL1 = 0xA502;               /* ERASE = 1 */
00093   *ptr  = 0;                    /* erase Flash segment */
00094   FCTL1 = 0xA500;               /* ERASE = 0 automatically done?! */
00095   FCTL3 = 0xA510;               /* Lock = 1 */
00096 }
00097 /*---------------------------------------------------------------------------*/
00098 void
00099 flash_write(unsigned short *ptr, unsigned short word)
00100 {
00101   FCTL3 = 0xA500;              /* Lock = 0 */
00102   while(FCTL3 & 0x0001) nop(); /* Wait for BUSY = 0, not needed unless
00103                                   run from RAM */
00104   FCTL1 = 0xA540;              /* WRT = 1 */
00105   *ptr  = word;                /* program Flash word */
00106   FCTL1 = 0xA500;              /* WRT = 0 */
00107   FCTL3 = 0xA510;              /* Lock = 1 */
00108 }
00109 /*---------------------------------------------------------------------------*/