rom.c

00001 /*
00002  * Copyright (c) 2006, Swedish Institute of Computer Science
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * @(#)$Id: rom.c,v 1.2 2006/12/01 15:06:42 bg- Exp $
00030  */
00031 
00032 #include <io.h>
00033 
00034 #include "contiki.h"
00035 
00036 #include "dev/rom.h"
00037 
00038 struct ictx {
00039   int s;
00040   unsigned short ie1, ie2, wdtctl;
00041 };
00042 
00043 static void
00044 mask_intr(struct ictx *c)
00045 {
00046   /* Disable all interrupts. */
00047   c->s = splhigh();
00048 
00049 #define WDTRPW               0x6900  /* Watchdog key returned by read */
00050 
00051   /* Disable all NMI-Interrupt sources */
00052   c->wdtctl = WDTCTL;
00053   c->wdtctl ^= (WDTRPW ^ WDTPW);
00054   WDTCTL = WDTPW | WDTHOLD;    
00055   c->ie1 = IE1;
00056   IE1 = 0x00;                  
00057   c->ie2 = IE2;
00058   IE2 = 0x00;
00059 
00060   /* MSP430F1611 257 < f < 476 kHz, 2.4576MHz/(5+1) = 409.6 kHz. */
00061   FCTL2 = FWKEY | FSSEL_SMCLK | (FN2 | FN1);
00062   FCTL3 = FWKEY;                /* Unlock flash. */
00063 }
00064 
00065 static void
00066 rest_intr(struct ictx *c)
00067 {
00068   FCTL1 = FWKEY;                /* Disable erase or write. */
00069   FCTL3 = FWKEY | LOCK;         /* Lock flash. */
00070   /* Restore interrupts. */
00071   IE2 = c->ie2;
00072   IE1 = c->ie1;
00073   WDTCTL = c->wdtctl;
00074   splx(c->s);
00075 }
00076 
00077 /*
00078  * This helper routine must reside in RAM!
00079  */
00080 static void
00081 blkwrt(void *to, const void *from, const void *from_end)
00082      // __attribute__ ((section(".data")))
00083      ;
00084 
00085 int
00086 rom_erase(long nbytes, off_t offset)
00087 {
00088   int nb = nbytes;
00089   char *to = (char *)(uintptr_t)offset;
00090   struct ictx c;
00091   
00092   if(nbytes % ROM_ERASE_UNIT_SIZE != 0) {
00093     return -1;
00094   }
00095 
00096   if(offset % ROM_ERASE_UNIT_SIZE != 0) {
00097     return -1;
00098   }
00099 
00100   while (nbytes > 0) {
00101     mask_intr(&c);
00102 
00103     FCTL1 = FWKEY | ERASE;      /* Segment erase. */
00104     *to  = 0;                   /* Erase segment containing to. */
00105     nbytes -= ROM_ERASE_UNIT_SIZE;
00106     to += ROM_ERASE_UNIT_SIZE;
00107 
00108     rest_intr(&c);
00109   }
00110 
00111   return nb;
00112 }
00113 
00114 int
00115 rom_pwrite(const void *buf, int nbytes, off_t offset)
00116 {
00117   const char *from = buf;
00118   int nb = nbytes;
00119   char *to = (char *)(uintptr_t)offset;
00120   struct ictx c;
00121   
00122   mask_intr(&c);
00123 
00124   while(nbytes > 0) {
00125     int n = (nbytes > 64) ? 64 : nbytes;
00126     FCTL1 = FWKEY | BLKWRT | WRT;       /* Enable block write. */
00127     blkwrt(to, from, from + n);
00128     while(FCTL3 & BUSY);
00129     to += 64;
00130     from += 64;
00131     nbytes -= n;
00132   }
00133 
00134   rest_intr(&c);
00135 
00136   return nb;
00137 }
00138 
00139 /*
00140  * This helper routine must reside in RAM!
00141  */
00142 asm(".data");
00143 asm(".p2align 1,0");
00144 asm(".type   blkwrt,@function");
00145 asm(".section        .data");
00146 
00147 static void
00148 blkwrt(void *_to, const void *_from, const void *_from_end)
00149 {
00150   unsigned short *to = _to;
00151   const unsigned short *from = _from;
00152   const unsigned short *from_end = _from_end;
00153   do {
00154     *to++ = *from++;
00155     while(!(FCTL3 & WAIT));
00156   } while(from < from_end);
00157   FCTL1 = FWKEY;                /* Disable block write. */
00158   /* Now ROM is available again! */
00159 }

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