infomem.c

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, Freie Universitaet Berlin. All rights reserved.
00003 
00004 These sources were developed at the Freie Universität Berlin, Computer
00005 Systems and Telematics group.
00006 
00007 Redistribution and use in source and binary forms, with or without
00008 modification, are permitted provided that the following conditions are
00009 met:
00010 
00011 - Redistributions of source code must retain the above copyright
00012 notice, this list of conditions and the following disclaimer.
00013 
00014 - Redistributions in binary form must reproduce the above copyright
00015 notice, this list of conditions and the following disclaimer in the
00016 documentation and/or other materials provided with the distribution.
00017  
00018 - Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
00019 contributors may be used to endorse or promote products derived from
00020 this software without specific prior written permission.
00021 
00022 This software is provided by FUB and the contributors on an "as is"
00023 basis, without any representations or warranties of any kind, express
00024 or implied including, but not limited to, representations or
00025 warranties of non-infringement, merchantability or fitness for a
00026 particular purpose. In no event shall FUB or contributors be liable
00027 for any direct, indirect, incidental, special, exemplary, or
00028 consequential damages (including, but not limited to, procurement of
00029 substitute goods or services; loss of use, data, or profits; or
00030 business interruption) however caused and on any theory of liability,
00031 whether in contract, strict liability, or tort (including negligence
00032 or otherwise) arising in any way out of the use of this software, even
00033 if advised of the possibility of such damage.
00034 
00035 This implementation was developed by the CST group at the FUB.
00036 
00037 For documentation and questions please use the web site
00038 http://scatterweb.mi.fu-berlin.de and the mailinglist
00039 scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
00040 Berlin, 2007
00041 */
00042 
00043 /**
00044  * @file                infomem.c
00045  * @addtogroup  storage
00046  * @brief               MSP430 Infomemory Storage
00047  * @author              Michael Baar    <baar@inf.fu-berlin.de>
00048  *
00049  * Functions to store and read data from the two infomemories (2 x 128 Bytes).
00050  * Offset addresses start at zero, size has a maximum of 128, write operations
00051  * across both blocks are not allowed.
00052  */
00053 #include <string.h>
00054 #include <signal.h>
00055 #include <stdarg.h>
00056 #include "contiki-conf.h"
00057 #include <msp430/flash.h>
00058 #include "infomem.h"
00059 
00060 void
00061 infomem_read(void *buffer, unsigned int offset, unsigned char size)
00062 {
00063   uint8_t *address = (uint8_t *)INFOMEM_START + offset;
00064   memcpy(buffer, address, size);
00065 }
00066 
00067 bool
00068 infomem_write(unsigned int offset, unsigned char count, ...)
00069 {
00070   char backup[INFOMEM_BLOCK_SIZE];      
00071   uint8_t *buffer;
00072   uint16_t i;
00073   uint8_t *flash;
00074   va_list argp;
00075   uint16_t size;
00076   uint8_t *data;
00077   int s;
00078 
00079   if(offset > (2 * INFOMEM_BLOCK_SIZE)) {
00080     return FALSE;
00081   }
00082 
00083   flash = (uint8_t *)INFOMEM_START;
00084 
00085   s = splhigh();
00086 
00087   /* backup into RAM */
00088   memcpy(backup, flash, INFOMEM_BLOCK_SIZE);
00089 
00090   /* merge backup with new data */
00091   va_start(argp, count);
00092 
00093   buffer = (uint8_t *)backup + offset;
00094   for(i = 0; i < count; i++) {
00095     data = va_arg(argp, uint8_t *);
00096     size = va_arg(argp, uint16_t);
00097     memcpy(buffer, data, size);
00098     buffer += size;
00099   }
00100 
00101   va_end(argp);
00102 
00103   /* init flash access */
00104   FCTL2 = FWKEY + FSSEL1 + FN2;
00105   FCTL3 = FWKEY;
00106 
00107   /* erase flash */
00108   FCTL1 = FWKEY + ERASE;
00109   *flash = 0;
00110 
00111   /* write flash */
00112   FCTL1 = FWKEY + WRT;
00113   buffer = (uint8_t *)backup;
00114   for(i = 0; i < INFOMEM_BLOCK_SIZE; i++) {
00115     *flash++ = *buffer++;
00116   }
00117 
00118   FCTL1 = FWKEY;
00119   FCTL3 = FWKEY + LOCK;
00120 
00121   splx(s);
00122 
00123   return TRUE;
00124 }

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