Contiki 2.6

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 <stdarg.h>
00055 #include "contiki-conf.h"
00056 #include "infomem.h"
00057 
00058 void
00059 infomem_read(void *buffer, unsigned int offset, unsigned char size)
00060 {
00061   uint8_t *address = (uint8_t *)INFOMEM_START + offset;
00062   memcpy(buffer, address, size);
00063 }
00064 
00065 bool
00066 infomem_write(unsigned int offset, unsigned char count, ...)
00067 {
00068   char backup[INFOMEM_BLOCK_SIZE];      
00069   uint8_t *buffer;
00070   uint16_t i;
00071   uint8_t *flash;
00072   va_list argp;
00073   uint16_t size;
00074   uint8_t *data;
00075   int s;
00076 
00077   if(offset > (2 * INFOMEM_BLOCK_SIZE)) {
00078     return FALSE;
00079   }
00080 
00081   flash = (uint8_t *)INFOMEM_START;
00082 
00083   s = splhigh();
00084 
00085   /* backup into RAM */
00086   memcpy(backup, flash, INFOMEM_BLOCK_SIZE);
00087 
00088   /* merge backup with new data */
00089   va_start(argp, count);
00090 
00091   buffer = (uint8_t *)backup + offset;
00092   for(i = 0; i < count; i++) {
00093     data = va_arg(argp, uint8_t *);
00094     size = va_arg(argp, uint16_t);
00095     memcpy(buffer, data, size);
00096     buffer += size;
00097   }
00098 
00099   va_end(argp);
00100 
00101   /* init flash access */
00102   FCTL2 = FWKEY + FSSEL1 + FN2;
00103   FCTL3 = FWKEY;
00104 
00105   /* erase flash */
00106   FCTL1 = FWKEY + ERASE;
00107   *flash = 0;
00108 
00109   /* write flash */
00110   FCTL1 = FWKEY + WRT;
00111   buffer = (uint8_t *)backup;
00112   for(i = 0; i < INFOMEM_BLOCK_SIZE; i++) {
00113     *flash++ = *buffer++;
00114   }
00115 
00116   FCTL1 = FWKEY;
00117   FCTL3 = FWKEY + LOCK;
00118 
00119   splx(s);
00120 
00121   return TRUE;
00122 }