Contiki 2.5
infomem.c
Go to the documentation of this file.
1 /*
2 Copyright 2007, Freie Universitaet Berlin. All rights reserved.
3 
4 These sources were developed at the Freie Universität Berlin, Computer
5 Systems and Telematics group.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10 
11 - Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 
18 - Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
21 
22 This software is provided by FUB and the contributors on an "as is"
23 basis, without any representations or warranties of any kind, express
24 or implied including, but not limited to, representations or
25 warranties of non-infringement, merchantability or fitness for a
26 particular purpose. In no event shall FUB or contributors be liable
27 for any direct, indirect, incidental, special, exemplary, or
28 consequential damages (including, but not limited to, procurement of
29 substitute goods or services; loss of use, data, or profits; or
30 business interruption) however caused and on any theory of liability,
31 whether in contract, strict liability, or tort (including negligence
32 or otherwise) arising in any way out of the use of this software, even
33 if advised of the possibility of such damage.
34 
35 This implementation was developed by the CST group at the FUB.
36 
37 For documentation and questions please use the web site
38 http://scatterweb.mi.fu-berlin.de and the mailinglist
39 scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
40 Berlin, 2007
41 */
42 
43 /**
44  * @file infomem.c
45  * @addtogroup storage
46  * @brief MSP430 Infomemory Storage
47  * @author Michael Baar <baar@inf.fu-berlin.de>
48  *
49  * Functions to store and read data from the two infomemories (2 x 128 Bytes).
50  * Offset addresses start at zero, size has a maximum of 128, write operations
51  * across both blocks are not allowed.
52  */
53 #include <string.h>
54 #include <signal.h>
55 #include <stdarg.h>
56 #include "contiki-conf.h"
57 #include <msp430/flash.h>
58 #include "infomem.h"
59 
60 void
61 infomem_read(void *buffer, unsigned int offset, unsigned char size)
62 {
63  uint8_t *address = (uint8_t *)INFOMEM_START + offset;
64  memcpy(buffer, address, size);
65 }
66 
67 bool
68 infomem_write(unsigned int offset, unsigned char count, ...)
69 {
70  char backup[INFOMEM_BLOCK_SIZE];
71  uint8_t *buffer;
72  uint16_t i;
73  uint8_t *flash;
74  va_list argp;
75  uint16_t size;
76  uint8_t *data;
77  int s;
78 
79  if(offset > (2 * INFOMEM_BLOCK_SIZE)) {
80  return FALSE;
81  }
82 
83  flash = (uint8_t *)INFOMEM_START;
84 
85  s = splhigh();
86 
87  /* backup into RAM */
88  memcpy(backup, flash, INFOMEM_BLOCK_SIZE);
89 
90  /* merge backup with new data */
91  va_start(argp, count);
92 
93  buffer = (uint8_t *)backup + offset;
94  for(i = 0; i < count; i++) {
95  data = va_arg(argp, uint8_t *);
96  size = va_arg(argp, uint16_t);
97  memcpy(buffer, data, size);
98  buffer += size;
99  }
100 
101  va_end(argp);
102 
103  /* init flash access */
104  FCTL2 = FWKEY + FSSEL1 + FN2;
105  FCTL3 = FWKEY;
106 
107  /* erase flash */
108  FCTL1 = FWKEY + ERASE;
109  *flash = 0;
110 
111  /* write flash */
112  FCTL1 = FWKEY + WRT;
113  buffer = (uint8_t *)backup;
114  for(i = 0; i < INFOMEM_BLOCK_SIZE; i++) {
115  *flash++ = *buffer++;
116  }
117 
118  FCTL1 = FWKEY;
119  FCTL3 = FWKEY + LOCK;
120 
121  splx(s);
122 
123  return TRUE;
124 }