Contiki 2.5
mfg-token.c
Go to the documentation of this file.
1 /** @file hal/micro/cortexm3/mfg-token.c
2  * @brief Cortex-M3 Manufacturing-Token system
3  *
4  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
5  */
6 #include PLATFORM_HEADER
7 #include "error.h"
9 #include "mfg-token.h"
10 
11 
12 
13 
14 #define DEFINETOKENS
15 #define TOKEN_MFG(name,creator,iscnt,isidx,type,arraysize,...) \
16  const int16u TOKEN_##name = TOKEN_##name##_ADDRESS;
18 #undef TOKEN_DEF
19 #undef TOKEN_MFG
20 #undef DEFINETOKENS
21 
22 
23 
24 
25 
26 
27 
28 
29 static const int8u nullEui[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
30 
31 
32 void halInternalGetMfgTokenData(void *data, int16u ID, int8u index, int8u len)
33 {
34  int8u *ram = (int8u*)data;
35 
36  //0x7F is a non-indexed token. Remap to 0 for the address calculation
37  index = (index==0x7F) ? 0 : index;
38 
39  if(ID == MFG_EUI_64_LOCATION) {
40  //There are two EUI64's stored in the Info Blocks, St and Custom.
41  //0x0A00 is the address used by the generic EUI64 token, and it is
42  //token.c's responbility to pick the returned EUI64 from either St
43  //or Custom. Return the Custom EUI64 if it is not all FF's, otherwise
44  //return the St EUI64.
45  tokTypeMfgEui64 eui64;
46  halCommonGetMfgToken(&eui64, TOKEN_MFG_CUSTOM_EUI_64);
47  if(MEMCOMPARE(eui64,nullEui, 8 /*EUI64_SIZE*/) == 0) {
48  halCommonGetMfgToken(&eui64, TOKEN_MFG_ST_EUI_64);
49  }
50  MEMCOPY(ram, eui64, 8 /*EUI64_SIZE*/);
51  } else {
52  //read from the Information Blocks. The token ID is only the
53  //bottom 16bits of the token's actual address. Since the info blocks
54  //exist in the range DATA_BIG_INFO_BASE-DATA_BIG_INFO_END, we need
55  //to OR the ID with DATA_BIG_INFO_BASE to get the real address.
56  int32u realAddress = (DATA_BIG_INFO_BASE|ID) + (len*index);
57  int8u *flash = (int8u *)realAddress;
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72  MEMCOPY(ram, flash, len);
73  }
74 }
75 
76 
77 void halInternalSetMfgTokenData(int16u token, void *data, int8u len)
78 {
79  StStatus flashStatus;
80  int32u realAddress = (DATA_BIG_INFO_BASE|token);
81  int8u * flash = (int8u *)realAddress;
82  int32u i;
83 
84  //The flash library (and hardware) requires the address and length to both
85  //be multiples of 16bits. Since this API is only valid for writing to
86  //the CIB, verify that the token+len falls within the CIB.
87  assert((token&1) != 1);
88  assert((len&1) != 1);
89  assert((realAddress>=CIB_BOTTOM) && ((realAddress+len-1)<=CIB_TOP));
90 
91  //CIB manufacturing tokens can only be written by on-chip code if the token
92  //is currently unprogrammed. Verify the entire token is unwritten. The
93  //flash library performs a similar check, but verifying here ensures that
94  //the entire token is unprogrammed and will prevent partial writes.
95  for(i=0;i<len;i++) {
96  assert(flash[i] == 0xFF);
97  }
98 
99  //Remember, the flash library operates in 16bit quantities, but the
100  //token system operates in 8bit quantities. Hence the divide by 2.
101  flashStatus = halInternalFlashWrite(realAddress, data, (len/2));
102  assert(flashStatus == ST_SUCCESS);
103 }
104