osd-contiki/cpu/stm32w108/hal/micro/cortexm3/mfg-token.c

105 lines
3 KiB
C
Raw Permalink Normal View History

2010-10-25 11:03:38 +02:00
/** @file hal/micro/cortexm3/mfg-token.c
* @brief Cortex-M3 Manufacturing-Token system
*
* <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
*/
#include PLATFORM_HEADER
#include "error.h"
#include "hal/micro/cortexm3/flash.h"
#include "mfg-token.h"
#define DEFINETOKENS
#define TOKEN_MFG(name,creator,iscnt,isidx,type,arraysize,...) \
const uint16_t TOKEN_##name = TOKEN_##name##_ADDRESS;
2010-10-25 11:03:38 +02:00
#include "hal/micro/cortexm3/token-manufacturing.h"
#undef TOKEN_DEF
#undef TOKEN_MFG
#undef DEFINETOKENS
static const uint8_t nullEui[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
2010-10-25 11:03:38 +02:00
void halInternalGetMfgTokenData(void *data, uint16_t ID, uint8_t index, uint8_t len)
2010-10-25 11:03:38 +02:00
{
uint8_t *ram = (uint8_t*)data;
2010-10-25 11:03:38 +02:00
//0x7F is a non-indexed token. Remap to 0 for the address calculation
index = (index==0x7F) ? 0 : index;
if(ID == MFG_EUI_64_LOCATION) {
//There are two EUI64's stored in the Info Blocks, St and Custom.
//0x0A00 is the address used by the generic EUI64 token, and it is
//token.c's responbility to pick the returned EUI64 from either St
//or Custom. Return the Custom EUI64 if it is not all FF's, otherwise
//return the St EUI64.
tokTypeMfgEui64 eui64;
halCommonGetMfgToken(&eui64, TOKEN_MFG_CUSTOM_EUI_64);
if(MEMCOMPARE(eui64,nullEui, 8 /*EUI64_SIZE*/) == 0) {
halCommonGetMfgToken(&eui64, TOKEN_MFG_ST_EUI_64);
}
MEMCOPY(ram, eui64, 8 /*EUI64_SIZE*/);
} else {
//read from the Information Blocks. The token ID is only the
//DATA_BIG_INFO_BASE-relative 16-bit offset of the token. Since the
//info blocks exist in the range DATA_BIG_INFO_BASE-DATA_BIG_INFO_END,
//we need to add the ID to DATA_BIG_INFO_BASE to get the real address.
uint32_t realAddress = (DATA_BIG_INFO_BASE+ID) + (len*index);
uint8_t *flash = (uint8_t *)realAddress;
2010-10-25 11:03:38 +02:00
MEMCOPY(ram, flash, len);
}
}
void halInternalSetMfgTokenData(uint16_t token, void *data, uint8_t len)
2010-10-25 11:03:38 +02:00
{
StStatus flashStatus;
uint32_t realAddress = (DATA_BIG_INFO_BASE+token);
uint8_t * flash = (uint8_t *)realAddress;
uint32_t i;
2010-10-25 11:03:38 +02:00
//The flash library (and hardware) requires the address and length to both
//be multiples of 16bits. Since this API is only valid for writing to
//the CIB, verify that the token+len falls within the CIB.
assert((token&1) != 1);
assert((len&1) != 1);
assert((realAddress>=CIB_BOTTOM) && ((realAddress+len-1)<=CIB_TOP));
//CIB manufacturing tokens can only be written by on-chip code if the token
//is currently unprogrammed. Verify the entire token is unwritten. The
//flash library performs a similar check, but verifying here ensures that
//the entire token is unprogrammed and will prevent partial writes.
for(i=0;i<len;i++) {
assert(flash[i] == 0xFF);
}
//Remember, the flash library operates in 16bit quantities, but the
//token system operates in 8bit quantities. Hence the divide by 2.
flashStatus = halInternalFlashWrite(realAddress, data, (len/2));
assert(flashStatus == ST_SUCCESS);
}