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

Generated on Mon Apr 11 14:23:36 2011 for Contiki 2.5 by  doxygen 1.6.1