Contiki 2.6
|
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 <stdlib.h> 00040 00041 #include "dev/flash.h" 00042 #include "dev/watchdog.h" 00043 00044 #define FLASH_TIMEOUT 30 00045 #define FLASH_REQ_TIMEOUT 150 00046 00047 #define INFOMEM_LO (unsigned short *)0x1800 00048 #define INFOMEM_HI (unsigned short *)0x1a00 00049 00050 static uint16_t sfrie; 00051 00052 /*---------------------------------------------------------------------------*/ 00053 void 00054 flash_setup(void) 00055 { 00056 /* disable all interrupts to protect CPU 00057 during programming from system crash */ 00058 dint(); 00059 00060 /* Clear interrupt flag1. */ 00061 SFRIFG1 = 0; 00062 /* The IFG1 = 0; statement locks up contikimac - not sure if this 00063 statement needs to be here at all. I've removed it for now, since 00064 it seems to work, but leave this little note here in case someone 00065 stumbles over this code at some point. */ 00066 00067 /* Stop watchdog. */ 00068 watchdog_stop(); 00069 00070 /* disable all NMI-Interrupt sources */ 00071 sfrie = SFRIE1; 00072 SFRIE1 = 0x00; 00073 } 00074 /*---------------------------------------------------------------------------*/ 00075 void 00076 flash_done(void) 00077 { 00078 /* Enable interrupts. */ 00079 SFRIE1 = sfrie; 00080 eint(); 00081 watchdog_start(); 00082 } 00083 /*---------------------------------------------------------------------------*/ 00084 static void 00085 unlock_infomem(void) 00086 { 00087 FCTL4 = 0xa500; 00088 FCTL3 = 0xa540; 00089 } 00090 /*---------------------------------------------------------------------------*/ 00091 static void 00092 lock_infomem(void) 00093 { 00094 FCTL3 = 0xa540; 00095 FCTL4 = 0xa580; 00096 } 00097 /*---------------------------------------------------------------------------*/ 00098 void 00099 flash_clear(unsigned short *ptr) 00100 { 00101 uint8_t r; 00102 00103 /* If ptr is in infomem, we need to unlock it first. */ 00104 if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) { 00105 unlock_infomem(); 00106 } 00107 00108 FCTL3 = 0xa500; /* Lock = 0 */ 00109 while(FCTL3 & 0x0001) { 00110 r++; /* Wait for BUSY = 0, not needed 00111 unless run from RAM */ 00112 } 00113 FCTL1 = 0xa502; /* ERASE = 1 */ 00114 *ptr = 0; /* erase Flash segment */ 00115 FCTL1 = 0xa500; /* ERASE = 0 automatically done?! */ 00116 FCTL3 = 0xa510; /* Lock = 1 */ 00117 00118 if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) { 00119 lock_infomem(); 00120 } 00121 } 00122 /*---------------------------------------------------------------------------*/ 00123 void 00124 flash_write(unsigned short *ptr, unsigned short word) 00125 { 00126 uint8_t r; 00127 00128 /* If ptr is in infomem, we need to unlock it first. */ 00129 if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) { 00130 unlock_infomem(); 00131 } 00132 00133 FCTL3 = 0xa500; /* Lock = 0 */ 00134 while(FCTL3 & 0x0001) { 00135 r++; /* Wait for BUSY = 0, not needed unless 00136 run from RAM */ 00137 } 00138 FCTL1 = 0xa540; /* WRT = 1 */ 00139 *ptr = word; /* program Flash word */ 00140 FCTL1 = 0xa500; /* WRT = 0 */ 00141 FCTL3 = 0xa510; /* Lock = 1 */ 00142 00143 if(ptr >= INFOMEM_LO && ptr <= INFOMEM_HI) { 00144 lock_infomem(); 00145 } 00146 } 00147 /*---------------------------------------------------------------------------*/