static void InitAppConfig(void) { AppConfig.Flags.bIsDHCPEnabled = TRUE; AppConfig.Flags.bInConfigMode = TRUE; memcpypgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr)); memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16); FormatNetBIOSName(AppConfig.NetBIOSName); AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2<<8ul | MY_DEFAULT_IP_ADDR_BYTE3<<16ul | MY_DEFAULT_IP_ADDR_BYTE4<<24ul; AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val; AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2<<8ul | MY_DEFAULT_MASK_BYTE3<<16ul | MY_DEFAULT_MASK_BYTE4<<24ul; AppConfig.DefaultMask.Val = AppConfig.MyMask.Val; AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2<<8ul | MY_DEFAULT_GATE_BYTE3<<16ul | MY_DEFAULT_GATE_BYTE4<<24ul; AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2<<8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3<<16ul | MY_DEFAULT_PRIMARY_DNS_BYTE4<<24ul; AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2<<8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3<<16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4<<24ul; #if defined(EEPROM_CS_TRIS) { BYTE c; // When a record is saved, first byte is written as 0x60 to indicate // that a valid record was saved. Note that older stack versions // used 0x57. This change has been made to so old EEPROM contents // will get overwritten. The AppConfig() structure has been changed, // resulting in parameter misalignment if still using old EEPROM // contents. XEEReadArray(0x0000, &c, 1); if(c == 0x42u) { XEEReadArray(0x0001, (BYTE*)&AppConfig, sizeof(AppConfig)); } else SaveAppConfig(); } #elif defined(SPIFLASH_CS_TRIS) { BYTE c; SPIFlashReadArray(0x0000, &c, 1); if(c == 0x42u) { SPIFlashReadArray(0x0001, (BYTE*)&AppConfig, sizeof(AppConfig)); } else SaveAppConfig(); } #endif }
WORD tlread(BYTE *buf, WORD pointer, WORD count, BYTE UNUSED) { WORD byteaddress = sizeof(AppConfig) + SIZEVAR + inspointer + 49000; //This is a cheat, needs to fix memory map properly! XEEReadArray(byteaddress, buf, count); inspointer = inspointer + count; pausewatchdog++; return inspointer; }
/********************************************************************* * Function: uint8_t XEERead(void) * * PreCondition: XEEInit() && XEEBeginRead() are already called. * * Input: None * * Output: uint8_t that was read * * Side Effects: None * * Overview: Reads next byte from EEPROM; internal address * is incremented by one. * * Note: None ********************************************************************/ uint8_t XEERead(void) { // Check if no more bytes are left in our local buffer if (vBytesInBuffer == 0u) { // Get a new set of bytes XEEReadArray(EEPROMAddress, EEPROMBuffer, EEPROM_BUFFER_SIZE); EEPROMAddress += EEPROM_BUFFER_SIZE; vBytesInBuffer = EEPROM_BUFFER_SIZE; } // Return a byte from our local buffer return EEPROMBuffer[EEPROM_BUFFER_SIZE - vBytesInBuffer--]; }
void WX_readPerm_data(void) { unsigned short loc = sizeof (NVM_VALIDATION_STRUCT) + sizeof (APP_CONFIG) ; if (loc + sizeof (WX) >= MPFS_RESERVE_BLOCK) { loc += sizeof (WX); putrsUART((ROM char*) "MPFS_RESERVE_BLOCK too small!"); while (1); } XEEReadArray(loc, (BYTE*) & WX, sizeof (WX)); // if EEPROM is empty if (WX.Calib.Rain_counts == 0xffff) WX_perm_data_init_toDefault(); }
/***************************************************************************** Function: WORD MPFSGetArray(MPFS_HANDLE hMPFS, BYTE* cData, WORD wLen) Description: Reads a series of bytes from a file. Precondition: The file handle referenced by hMPFS is already open. Parameters: hMPFS - the file handle from which to read cData - where to store the bytes that were read wLen - how many bytes to read Returns: The number of bytes successfully read. If this is less than wLen, an EOF occurred while attempting to read. ***************************************************************************/ WORD MPFSGetArray(MPFS_HANDLE hMPFS, BYTE* cData, WORD wLen) { // Make sure we're reading a valid address if(hMPFS > MAX_MPFS_HANDLES) return 0; // Determine how many we can actually read if(wLen > MPFSStubs[hMPFS].bytesRem) wLen = MPFSStubs[hMPFS].bytesRem; // Make sure we're reading a valid address if(MPFSStubs[hMPFS].addr == MPFS_INVALID || wLen == 0u) return 0; if(cData == NULL) { MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; return wLen; } // Read the data #if defined(MPFS_USE_EEPROM) XEEReadArray(MPFSStubs[hMPFS].addr+MPFS_HEAD, cData, wLen); MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; lastRead = MPFS_INVALID; #elif defined(MPFS_USE_SPI_FLASH) #if (GRAPHICS_PICTAIL_VERSION == 3) SST25ReadArray(MPFSStubs[hMPFS].addr+MPFS_HEAD, cData, wLen); #else xSemaphoreTake(QVGASemaphore, portMAX_DELAY); SST39PMPInit(); SST39ReadArray(MPFSStubs[hMPFS].addr+MPFS_HEAD, cData, wLen); LCDPMPInit(); xSemaphoreGive(QVGASemaphore); #endif MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; #else #if defined(__C30__) { DWORD addr; DWORD_VAL read; WORD count; BYTE i; // MPFS Images are addressed by the byte; Program memory by the word. // // Flash program memory is 24 bits wide and only even words are // implemented. The upper byte of the upper word is read as 0x00. // Address in program memory of any given byte is (MPFSAddr * 2) / 3 // // We will read 24 bits at a time, but need to support using only // fractions of the first and last byte. // Find the beginning address in program memory. addr = (MPFSStubs[hMPFS].addr / 3) << 1; // Find where to start in that first 3 bytes read.Val = (addr * 3) >> 1; if(read.Val == MPFSStubs[hMPFS].addr) i = 0; else if(read.Val+1 == MPFSStubs[hMPFS].addr) i = 1; else i = 2; // Add in the MPFS starting address offset addr += MPFS_HEAD; // Update the MPFS Handle MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; // Read the first DWORD read.Val = ReadProgramMemory(addr & 0x00FFFFFF); addr += 2; // Copy values as needed for(count = wLen; count > 0; cData++, count--) { // Copy the next value in *cData = read.v[i++]; // Check if a new DWORD is needed if(i == 3 && count != 1) {// Read in a new DWORD read.Val = ReadProgramMemory(addr & 0x00FFFFFF); addr += 2; i = 0; } } } #else { DWORD dwHITECHWorkaround = MPFS_HEAD; memcpypgm2ram(cData, (ROM void*)(MPFSStubs[hMPFS].addr + dwHITECHWorkaround), wLen); MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; } #endif #endif return wLen; }
/********************************************************************* * Function: MPFS MPFSOpen(BYTE* file) * * PreCondition: None * * Input: file - NULL terminated file name. * * Output: A handle if file is found * MPFS_INVALID if file is not found. * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ MPFS MPFSOpen(BYTE* file) { MPFS_ENTRY entry; MPFS FAT; BYTE fileNameLen; debug_mpfs(debug_mpfs_putc, "\r\nMPFSOpen() '%s' ", file); if( mpfsFlags.bits.bNotAvailable ) { debug_mpfs(debug_mpfs_putc, "NOT AVAILABLE"); return MPFS_NOT_AVAILABLE; } #if defined(MPFS_USE_EEPROM) || defined(MPFS_USE_SPI_FLASH) FAT = MPFS_Start; #else FAT = (MPFS)MPFS_Start; #endif // If string is empty, do not attempt to find it in FAT. if ( *file == '\0' ) return MPFS_INVALID; //debug_mpfs("p1='%s'0x%LX ", file, file); file = (BYTE*)strupr((char*)file); //debug_mpfs("p2='%s'0x%LX ", file, file); debug_mpfs(debug_mpfs_putc, "START=0x%LX ", FAT); for(;;) { // Bring current FAT entry into RAM. #if defined(MPFS_USE_EEPROM) XEEReadArray(FAT, (unsigned char*)&entry, sizeof(entry)); #elif defined(MPFS_USE_SPI_FLASH) SPIFlashReadArray(FAT, (BYTE*)&entry, sizeof(entry)); #else #if defined(__C30__) memcpypgm2ram(&entry, (ROM void*)(WORD)FAT, sizeof(entry)); #else memcpypgm2ram(&entry, (ROM void*)FAT, sizeof(entry)); #endif #endif debug_mpfs(debug_mpfs_putc, "FLAG=0x%X ", entry.Flag); // Make sure that it is a valid entry. if (entry.Flag == MPFS_DATA) { // Does the file name match ? fileNameLen = strlen((char*)file); if ( fileNameLen > MAX_FILE_NAME_LEN ) fileNameLen = MAX_FILE_NAME_LEN; //debug_mpfs("f='%s' (%u) vs '%s' ", entry.Name, fileNameLen, file); if( memcmp((void*)file, (void*)entry.Name, fileNameLen) == 0 ) { #if defined(__PCD__) #warning 4.121 temporary bug fix memcpy(&_currentFile, &entry.Address, 4); #if defined(MPFS_RETURN_OFFSET) _currentFile += MPFS_Start; #endif mpfsOpenCount++; debug_mpfs(debug_mpfs_putc, "found_0x%LX ", _currentFile); return _currentFile; #else #if defined(MPFS_RETURN_OFFSET) entry.Address += MPFS_Start; #endif _currentFile = (MPFS)entry.Address; mpfsOpenCount++; debug_mpfs(debug_mpfs_putc, "found_0x%LX ", _currentFile); return entry.Address; #endif } // File does not match. Try next entry... FAT += sizeof(entry); } else if ( entry.Flag == MPFS_ETX ) { #warning 4.121 temporary bug fix unsigned int32 entry_Address; memcpy(&entry_Address, &entry.Address, 4); if ( entry_Address != (MPFS)MPFS_INVALID ) { FAT = (MPFS)entry_Address; //original, doesn't work 4.121 pcd debug_mpfs(debug_mpfs_putc, "(etx 0x%LX) ", FAT); } else { debug_mpfs(debug_mpfs_putc, "invalid_etx "); break; } } else { debug_mpfs(debug_mpfs_putc, "invalid_flag "); return (MPFS)MPFS_INVALID; } } debug_mpfs(debug_mpfs_putc, "not_found "); return (MPFS)MPFS_INVALID; }
/********************************************************************* * Function: MPFS MPFSOpen(BYTE* file) * * PreCondition: None * * Input: file - NULL terminated file name. * * Output: A handle if file is found * MPFS_INVALID if file is not found. * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ MPFS MPFSOpen(BYTE* file) { MPFS_ENTRY entry; MPFS FAT; BYTE fileNameLen; if( mpfsFlags.bits.bNotAvailable ) return MPFS_NOT_AVAILABLE; #if defined(MPFS_USE_EEPROM) || defined(MPFS_USE_SPI_FLASH) FAT = MPFS_Start; #else FAT = (MPFS)MPFS_Start; #endif // If string is empty, do not attempt to find it in FAT. if ( *file == '\0' ) return MPFS_INVALID; file = (BYTE*)strupr((char*)file); while(1) { // Bring current FAT entry into RAM. #if defined(MPFS_USE_EEPROM) XEEReadArray(FAT, (unsigned char*)&entry, sizeof(entry)); #else #if defined(__C30__) memcpypgm2ram(&entry, (ROM void*)(WORD)FAT, sizeof(entry)); #else memcpypgm2ram(&entry, (ROM void*)FAT, sizeof(entry)); #endif #endif // Make sure that it is a valid entry. if (entry.Flag == MPFS_DATA) { // Does the file name match ? fileNameLen = strlen((char*)file); if ( fileNameLen > MAX_FILE_NAME_LEN ) fileNameLen = MAX_FILE_NAME_LEN; if( memcmp((void*)file, (void*)entry.Name, fileNameLen) == 0 ) { _currentFile = (MPFS)entry.Address; mpfsOpenCount++; return entry.Address; } // File does not match. Try next entry... FAT += sizeof(entry); } else if ( entry.Flag == MPFS_ETX ) { if ( entry.Address != (MPFS)MPFS_INVALID ) FAT = (MPFS)entry.Address; else break; } else return (MPFS)MPFS_INVALID; } return (MPFS)MPFS_INVALID; }
/***************************************************************************** Function: uint16_t MPFSGetArray(MPFS_HANDLE hMPFS, uint8_t* cData, uint16_t wLen) Description: Reads a series of bytes from a file. Precondition: The file handle referenced by hMPFS is already open. Parameters: hMPFS - the file handle from which to read cData - where to store the bytes that were read wLen - how many bytes to read Returns: The number of bytes successfully read. If this is less than wLen, an EOF occurred while attempting to read. ***************************************************************************/ uint16_t MPFSGetArray(MPFS_HANDLE hMPFS, uint8_t* cData, uint16_t wLen) { // Make sure we're reading a valid address if(hMPFS > MAX_MPFS_HANDLES) return 0; // Determine how many we can actually read if(wLen > MPFSStubs[hMPFS].bytesRem) wLen = MPFSStubs[hMPFS].bytesRem; // Make sure we're reading a valid address if(MPFSStubs[hMPFS].addr == MPFS_INVALID || wLen == 0u) return 0; if(cData == NULL) { MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; return wLen; } // Read the data #if defined(MPFS_USE_EEPROM) XEEReadArray(MPFSStubs[hMPFS].addr+MPFS_HEAD, cData, wLen); MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; lastRead = MPFS_INVALID; #elif defined(MPFS_USE_SPI_FLASH) SPIFlashReadArray(MPFSStubs[hMPFS].addr+MPFS_HEAD, cData, wLen); MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; #else #if defined(__C30__) { uint32_t addr; TCPIP_UINT32_VAL read; uint16_t count; uint8_t i; // MPFS Images are addressed by the byte; Program memory by the word. // // Flash program memory is 24 bits wide and only even words are // implemented. The upper byte of the upper word is read as 0x00. // Address in program memory of any given byte is (MPFSAddr * 2) / 3 // // We will read 24 bits at a time, but need to support using only // fractions of the first and last byte. // Find the beginning address in program memory. addr = (MPFSStubs[hMPFS].addr / 3) << 1; // Find where to start in that first 3 bytes read.Val = (addr * 3) >> 1; if(read.Val == MPFSStubs[hMPFS].addr) i = 0; else if(read.Val+1 == MPFSStubs[hMPFS].addr) i = 1; else i = 2; // Add in the MPFS starting address offset addr += MPFS_HEAD; // Update the MPFS Handle MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; // Read the first uint32_t read.Val = ReadProgramMemory(addr & 0x00FFFFFF); addr += 2; // Copy values as needed for(count = wLen; count > 0; cData++, count--) { // Copy the next value in *cData = read.v[i++]; // Check if a new uint32_t is needed if(i == 3 && count != 1) {// Read in a new uint32_t read.Val = ReadProgramMemory(addr & 0x00FFFFFF); addr += 2; i = 0; } } } #else { uint32_t dwHITECHWorkaround = MPFS_HEAD; memcpy(cData, (const void*)(MPFSStubs[hMPFS].addr + dwHITECHWorkaround), wLen); MPFSStubs[hMPFS].addr += wLen; MPFSStubs[hMPFS].bytesRem -= wLen; } #endif #endif return wLen; }
void WiFi_ReadConfigFromMemory(void) { #if defined(MEDIA_STORAGE_EEPROM) XEEReadArray(0,(uint8_t*)p_wifi_ConfigData, sizeof(DRV_WIFI_CONFIG_DATA)); #endif }
static void InitAppConfig(void) { #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS) unsigned char vNeedToSaveDefaults = 0; #endif while(1) { // Start out zeroing all AppConfig bytes to ensure all fields are // deterministic for checksum generation memset((void*)&AppConfig, 0x00, sizeof(AppConfig)); AppConfig.Flags.bIsDHCPEnabled = TRUE; AppConfig.Flags.bInConfigMode = TRUE; memcpypgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr)); // { // _prog_addressT MACAddressAddress; // MACAddressAddress.next = 0x157F8; // _memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAddressAddress, sizeof(AppConfig.MyMACAddr)); // } AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2<<8ul | MY_DEFAULT_IP_ADDR_BYTE3<<16ul | MY_DEFAULT_IP_ADDR_BYTE4<<24ul; AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val; AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2<<8ul | MY_DEFAULT_MASK_BYTE3<<16ul | MY_DEFAULT_MASK_BYTE4<<24ul; AppConfig.DefaultMask.Val = AppConfig.MyMask.Val; AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2<<8ul | MY_DEFAULT_GATE_BYTE3<<16ul | MY_DEFAULT_GATE_BYTE4<<24ul; AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2<<8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3<<16ul | MY_DEFAULT_PRIMARY_DNS_BYTE4<<24ul; AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2<<8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3<<16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4<<24ul; // SNMP Community String configuration #if defined(STACK_USE_SNMP_SERVER) { BYTE i; static ROM char * ROM cReadCommunities[] = SNMP_READ_COMMUNITIES; static ROM char * ROM cWriteCommunities[] = SNMP_WRITE_COMMUNITIES; ROM char * strCommunity; for(i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++) { // Get a pointer to the next community string strCommunity = cReadCommunities[i]; if(i >= sizeof(cReadCommunities)/sizeof(cReadCommunities[0])) strCommunity = ""; // Ensure we don't buffer overflow. If your code gets stuck here, // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h // is either too small or one of your community string lengths // (SNMP_READ_COMMUNITIES) are too large. Fix either. if(strlenpgm(strCommunity) >= sizeof(AppConfig.readCommunity[0])) while(1); // Copy string into AppConfig strcpypgm2ram((char*)AppConfig.readCommunity[i], strCommunity); // Get a pointer to the next community string strCommunity = cWriteCommunities[i]; if(i >= sizeof(cWriteCommunities)/sizeof(cWriteCommunities[0])) strCommunity = ""; // Ensure we don't buffer overflow. If your code gets stuck here, // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h // is either too small or one of your community string lengths // (SNMP_WRITE_COMMUNITIES) are too large. Fix either. if(strlenpgm(strCommunity) >= sizeof(AppConfig.writeCommunity[0])) while(1); // Copy string into AppConfig strcpypgm2ram((char*)AppConfig.writeCommunity[i], strCommunity); } } #endif // Load the default NetBIOS Host Name memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16); FormatNetBIOSName(AppConfig.NetBIOSName); #if defined(WF_CS_TRIS) // Load the default SSID Name WF_ASSERT(sizeof(MY_DEFAULT_SSID_NAME) <= sizeof(AppConfig.MySSID)); memcpypgm2ram(AppConfig.MySSID, (ROM void*)MY_DEFAULT_SSID_NAME, sizeof(MY_DEFAULT_SSID_NAME)); AppConfig.SsidLength = sizeof(MY_DEFAULT_SSID_NAME) - 1; #if defined (EZ_CONFIG_STORE) AppConfig.SecurityMode = MY_DEFAULT_WIFI_SECURITY_MODE; AppConfig.networkType = MY_DEFAULT_NETWORK_TYPE; AppConfig.dataValid = 0; #endif // EZ_CONFIG_STORE #endif // Compute the checksum of the AppConfig defaults as loaded from ROM wOriginalAppConfigChecksum = CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig)); #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS) { NVM_VALIDATION_STRUCT NVMValidationStruct; // Check to see if we have a flag set indicating that we need to // save the ROM default AppConfig values. if(vNeedToSaveDefaults) SaveAppConfig(&AppConfig); // Read the NVMValidation record and AppConfig struct out of EEPROM/Flash #if defined(EEPROM_CS_TRIS) { XEEReadArray(0x0000, (BYTE*)&NVMValidationStruct, sizeof(NVMValidationStruct)); XEEReadArray(sizeof(NVMValidationStruct), (BYTE*)&AppConfig, sizeof(AppConfig)); } #elif defined(SPIFLASH_CS_TRIS) { SPIFlashReadArray(0x0000, (BYTE*)&NVMValidationStruct, sizeof(NVMValidationStruct)); SPIFlashReadArray(sizeof(NVMValidationStruct), (BYTE*)&AppConfig, sizeof(AppConfig)); } #endif // Check EEPROM/Flash validitity. If it isn't valid, set a flag so // that we will save the ROM default values on the next loop // iteration. if((NVMValidationStruct.wConfigurationLength != sizeof(AppConfig)) || (NVMValidationStruct.wOriginalChecksum != wOriginalAppConfigChecksum) || (NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig)))) { // Check to ensure that the vNeedToSaveDefaults flag is zero, // indicating that this is the first iteration through the do // loop. If we have already saved the defaults once and the // EEPROM/Flash still doesn't pass the validity check, then it // means we aren't successfully reading or writing to the // EEPROM/Flash. This means you have a hardware error and/or // SPI configuration error. if(vNeedToSaveDefaults) { while(1); } // Set flag and restart loop to load ROM defaults and save them vNeedToSaveDefaults = 1; continue; } // If we get down here, it means the EEPROM/Flash has valid contents // and either matches the ROM defaults or previously matched and // was run-time reconfigured by the user. In this case, we shall // use the contents loaded from EEPROM/Flash. break; } #endif break; } WiFiInfo.CurrentConfigHasChanged = 1; #if defined (EZ_CONFIG_STORE) // Set configuration for ZG from NVM /* Set security type and key if necessary, convert from app storage to ZG driver */ if (AppConfig.dataValid) CFGCXT.isWifiDoneConfigure = 1; AppConfig.saveSecurityInfo = FALSE; #endif // EZ_CONFIG_STORE }
static void InitAppConfig(void) { #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS) unsigned char vNeedToSaveDefaults = 0; #endif while(1) { // Start out zeroing all AppConfig bytes to ensure all fields are // deterministic for checksum generation memset((void*)&AppConfig, 0x00, sizeof(AppConfig)); AppConfig.Flags.bIsDHCPEnabled = TRUE; AppConfig.Flags.bInConfigMode = TRUE; memcpypgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr)); // { // _prog_addressT MACAddressAddress; // MACAddressAddress.next = 0x157F8; // _memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAddressAddress, sizeof(AppConfig.MyMACAddr)); // } AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2<<8ul | MY_DEFAULT_IP_ADDR_BYTE3<<16ul | MY_DEFAULT_IP_ADDR_BYTE4<<24ul; AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val; AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2<<8ul | MY_DEFAULT_MASK_BYTE3<<16ul | MY_DEFAULT_MASK_BYTE4<<24ul; AppConfig.DefaultMask.Val = AppConfig.MyMask.Val; AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2<<8ul | MY_DEFAULT_GATE_BYTE3<<16ul | MY_DEFAULT_GATE_BYTE4<<24ul; AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2<<8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3<<16ul | MY_DEFAULT_PRIMARY_DNS_BYTE4<<24ul; AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2<<8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3<<16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4<<24ul; // SNMP Community String configuration #if defined(STACK_USE_SNMP_SERVER) { BYTE i; static ROM char * ROM cReadCommunities[] = SNMP_READ_COMMUNITIES; static ROM char * ROM cWriteCommunities[] = SNMP_WRITE_COMMUNITIES; ROM char * strCommunity; for(i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++) { // Get a pointer to the next community string strCommunity = cReadCommunities[i]; if(i >= sizeof(cReadCommunities)/sizeof(cReadCommunities[0])) strCommunity = ""; // Ensure we don't buffer overflow. If your code gets stuck here, // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h // is either too small or one of your community string lengths // (SNMP_READ_COMMUNITIES) are too large. Fix either. if(strlenpgm(strCommunity) >= sizeof(AppConfig.readCommunity[0])) while(1); // Copy string into AppConfig strcpypgm2ram((char*)AppConfig.readCommunity[i], strCommunity); // Get a pointer to the next community string strCommunity = cWriteCommunities[i]; if(i >= sizeof(cWriteCommunities)/sizeof(cWriteCommunities[0])) strCommunity = ""; // Ensure we don't buffer overflow. If your code gets stuck here, // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h // is either too small or one of your community string lengths // (SNMP_WRITE_COMMUNITIES) are too large. Fix either. if(strlenpgm(strCommunity) >= sizeof(AppConfig.writeCommunity[0])) while(1); // Copy string into AppConfig strcpypgm2ram((char*)AppConfig.writeCommunity[i], strCommunity); } } #endif // Vending machine specific defaults strcpypgm2ram((char*)Products[0].name, (ROM char*)"Cola"); strcpypgm2ram((char*)Products[1].name, (ROM char*)"Diet Cola"); strcpypgm2ram((char*)Products[2].name, (ROM char*)"Root Beer"); strcpypgm2ram((char*)Products[3].name, (ROM char*)"Orange"); strcpypgm2ram((char*)Products[4].name, (ROM char*)"Lemonade"); strcpypgm2ram((char*)Products[5].name, (ROM char*)"Iced Tea"); strcpypgm2ram((char*)Products[6].name, (ROM char*)"Water"); Products[0].price = 4; Products[1].price = 4; Products[2].price = 4; Products[3].price = 4; Products[4].price = 5; Products[5].price = 7; Products[6].price = 8; strcpypgm2ram((char*)machineDesc, (ROM char*)"Building C4 - 2nd Floor NW"); machineDesc[32] = '\0'; curItem = 0; curCredit = 0; // Load the default NetBIOS Host Name memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16); FormatNetBIOSName(AppConfig.NetBIOSName); #if defined(WF_CS_TRIS) // Load the default SSID Name WF_ASSERT(sizeof(MY_DEFAULT_SSID_NAME) <= sizeof(AppConfig.MySSID)); memcpypgm2ram(AppConfig.MySSID, (ROM void*)MY_DEFAULT_SSID_NAME, sizeof(MY_DEFAULT_SSID_NAME)); AppConfig.SsidLength = sizeof(MY_DEFAULT_SSID_NAME) - 1; AppConfig.SecurityMode = MY_DEFAULT_WIFI_SECURITY_MODE; AppConfig.WepKeyIndex = MY_DEFAULT_WEP_KEY_INDEX; #if (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_OPEN) memset(AppConfig.SecurityKey, 0x00, sizeof(AppConfig.SecurityKey)); AppConfig.SecurityKeyLength = 0; #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WEP_40 memcpypgm2ram(AppConfig.SecurityKey, (ROM void*)MY_DEFAULT_WEP_KEYS_40, sizeof(MY_DEFAULT_WEP_KEYS_40) - 1); AppConfig.SecurityKeyLength = sizeof(MY_DEFAULT_WEP_KEYS_40) - 1; #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WEP_104 memcpypgm2ram(AppConfig.SecurityKey, (ROM void*)MY_DEFAULT_WEP_KEYS_104, sizeof(MY_DEFAULT_WEP_KEYS_104) - 1); AppConfig.SecurityKeyLength = sizeof(MY_DEFAULT_WEP_KEYS_104) - 1; #elif (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_WITH_KEY) || \ (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA2_WITH_KEY) || \ (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_AUTO_WITH_KEY) memcpypgm2ram(AppConfig.SecurityKey, (ROM void*)MY_DEFAULT_PSK, sizeof(MY_DEFAULT_PSK) - 1); AppConfig.SecurityKeyLength = sizeof(MY_DEFAULT_PSK) - 1; #elif (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_WITH_PASS_PHRASE) || \ (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA2_WITH_PASS_PHRASE) || \ (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE) memcpypgm2ram(AppConfig.SecurityKey, (ROM void*)MY_DEFAULT_PSK_PHRASE, sizeof(MY_DEFAULT_PSK_PHRASE) - 1); AppConfig.SecurityKeyLength = sizeof(MY_DEFAULT_PSK_PHRASE) - 1; #else #error "No security defined" #endif /* MY_DEFAULT_WIFI_SECURITY_MODE */ #endif // Compute the checksum of the AppConfig defaults as loaded from ROM wOriginalAppConfigChecksum = CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig)); #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS) { NVM_VALIDATION_STRUCT NVMValidationStruct; // Check to see if we have a flag set indicating that we need to // save the ROM default AppConfig values. if(vNeedToSaveDefaults) SaveAppConfig(&AppConfig); // Read the NVMValidation record and AppConfig struct out of EEPROM/Flash #if defined(EEPROM_CS_TRIS) { XEEReadArray(0x0000, (BYTE*)&NVMValidationStruct, sizeof(NVMValidationStruct)); XEEReadArray(sizeof(NVMValidationStruct), (BYTE*)&AppConfig, sizeof(AppConfig)); XEEReadArray(sizeof(NVMValidationStruct) + sizeof(AppConfig), (BYTE*)&Products, sizeof(Products)); XEEReadArray(sizeof(NVMValidationStruct) + sizeof(AppConfig) + sizeof(Products), (BYTE*)&machineDesc, sizeof(machineDesc)); } #elif defined(SPIFLASH_CS_TRIS) { SPIFlashReadArray(0x0000, (BYTE*)&NVMValidationStruct, sizeof(NVMValidationStruct)); SPIFlashReadArray(sizeof(NVMValidationStruct), (BYTE*)&AppConfig, sizeof(AppConfig)); SPIFlashReadArray(sizeof(NVMValidationStruct) + sizeof(AppConfig), (BYTE*)&Products, sizeof(Products)); SPIFlashReadArray(sizeof(NVMValidationStruct) + sizeof(AppConfig) + sizeof(Products), (BYTE*)&machineDesc, sizeof(machineDesc)); } #endif // Check EEPROM/Flash validitity. If it isn't valid, set a flag so // that we will save the ROM default values on the next loop // iteration. if((NVMValidationStruct.wConfigurationLength != sizeof(AppConfig)) || (NVMValidationStruct.wOriginalChecksum != wOriginalAppConfigChecksum) || (NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig)))) { // Check to ensure that the vNeedToSaveDefaults flag is zero, // indicating that this is the first iteration through the do // loop. If we have already saved the defaults once and the // EEPROM/Flash still doesn't pass the validity check, then it // means we aren't successfully reading or writing to the // EEPROM/Flash. This means you have a hardware error and/or // SPI configuration error. if(vNeedToSaveDefaults) { while(1); } // Set flag and restart loop to load ROM defaults and save them vNeedToSaveDefaults = 1; continue; } // If we get down here, it means the EEPROM/Flash has valid contents // and either matches the ROM defaults or previously matched and // was run-time reconfigured by the user. In this case, we shall // use the contents loaded from EEPROM/Flash. break; } #endif break; } // Update with default stock values on every reboot Products[0].stock = 15; Products[1].stock = 9; Products[2].stock = 22; Products[3].stock = 18; Products[4].stock = 4; Products[5].stock = 29; Products[6].stock = 14; }
/********************************************************************* * Function: void InitAppConfig(void) * * PreCondition: MPFSInit() is already called. * * Input: None * * Output: Write/Read non-volatile config variables. * * Side Effects: None * * Overview: None * * Note: None ********************************************************************/ static void InitAppConfig(void) { #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS) unsigned char vNeedToSaveDefaults = 0; #endif while(1) { // Start out zeroing all AppConfig bytes to ensure all fields are // deterministic for checksum generation memset((void*)&AppConfig, 0x00, sizeof(AppConfig)); AppConfig.Flags.bIsDHCPEnabled = TRUE; AppConfig.Flags.bInConfigMode = TRUE; memcpypgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr)); // { // _prog_addressT MACAddressAddress; // MACAddressAddress.next = 0x157F8; // _memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAddressAddress, sizeof(AppConfig.MyMACAddr)); // } // SoftAP on certain setups with IP 192.168.1.1 has problem with DHCP client assigning new IP address on redirection. // 192.168.1.1 is a common IP address with most APs. This is still under investigation. // For now, assign this as 192.168.1.3 AppConfig.MyIPAddr.Val = 192ul | 168ul<<8ul | 1ul<<16ul | 3ul<<24ul; AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val; AppConfig.MyMask.Val = 255ul | 255ul<<8ul | 0ul<<16ul | 0ul<<24ul; AppConfig.DefaultMask.Val = AppConfig.MyMask.Val; AppConfig.MyGateway.Val = AppConfig.MyIPAddr.Val; AppConfig.PrimaryDNSServer.Val = AppConfig.MyIPAddr.Val; AppConfig.SecondaryDNSServer.Val = AppConfig.MyIPAddr.Val; // Load the default NetBIOS Host Name memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16); FormatNetBIOSName(AppConfig.NetBIOSName); #if defined(WF_CS_TRIS) // Load the default SSID Name WF_ASSERT(sizeof(MY_DEFAULT_SSID_NAME)-1 <= sizeof(AppConfig.MySSID)); memcpypgm2ram(AppConfig.MySSID, (ROM void*)MY_DEFAULT_SSID_NAME, sizeof(MY_DEFAULT_SSID_NAME)); AppConfig.SsidLength = sizeof(MY_DEFAULT_SSID_NAME) - 1; AppConfig.SecurityMode = MY_DEFAULT_WIFI_SECURITY_MODE; if (AppConfig.SecurityMode == WF_SECURITY_WEP_40) { AppConfig.WepKeyIndex = MY_DEFAULT_WEP_KEY_INDEX; memcpypgm2ram(AppConfig.SecurityKey, (ROM void*)MY_DEFAULT_WEP_KEYS_40, sizeof(MY_DEFAULT_WEP_KEYS_40) - 1); AppConfig.SecurityKeyLength = sizeof(MY_DEFAULT_WEP_KEYS_40) - 1; } else if (AppConfig.SecurityMode == WF_SECURITY_WEP_104) { AppConfig.WepKeyIndex = MY_DEFAULT_WEP_KEY_INDEX; memcpypgm2ram(AppConfig.SecurityKey, (ROM void*)MY_DEFAULT_WEP_KEYS_104, sizeof(MY_DEFAULT_WEP_KEYS_104) - 1); AppConfig.SecurityKeyLength = sizeof(MY_DEFAULT_WEP_KEYS_104) - 1; } AppConfig.networkType = MY_DEFAULT_NETWORK_TYPE; AppConfig.dataValid = 0; #endif // Compute the checksum of the AppConfig defaults as loaded from ROM wOriginalAppConfigChecksum = CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig)); #if defined(EEPROM_CS_TRIS) NVM_VALIDATION_STRUCT NVMValidationStruct; // Check to see if we have a flag set indicating that we need to // save the ROM default AppConfig values. if(vNeedToSaveDefaults) SaveAppConfig(&AppConfig); // Read the NVMValidation record and AppConfig struct out of EEPROM/Flash XEEReadArray(0x0000, (BYTE*)&NVMValidationStruct, sizeof(NVMValidationStruct)); XEEReadArray(sizeof(NVMValidationStruct), (BYTE*)&AppConfig, sizeof(AppConfig)); // Check EEPROM/Flash validitity. If it isn't valid, set a flag so // that we will save the ROM default values on the next loop // iteration. if((NVMValidationStruct.wConfigurationLength != sizeof(AppConfig)) || (NVMValidationStruct.wOriginalChecksum != wOriginalAppConfigChecksum) || (NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig)))) { // Check to ensure that the vNeedToSaveDefaults flag is zero, // indicating that this is the first iteration through the do // loop. If we have already saved the defaults once and the // EEPROM/Flash still doesn't pass the validity check, then it // means we aren't successfully reading or writing to the // EEPROM/Flash. This means you have a hardware error and/or // SPI configuration error. if(vNeedToSaveDefaults) { while(1); } // Set flag and restart loop to load ROM defaults and save them vNeedToSaveDefaults = 1; continue; } // If we get down here, it means the EEPROM/Flash has valid contents // and either matches the ROM defaults or previously matched and // was run-time reconfigured by the user. In this case, we shall // use the contents loaded from EEPROM/Flash. break; #endif break; } #if defined (EZ_CONFIG_STORE) // Set configuration for ZG from NVM /* Set security type and key if necessary, convert from app storage to ZG driver */ if (AppConfig.dataValid) CFGCXT.isWifiDoneConfigure = 1; AppConfig.saveSecurityInfo = FALSE; #endif // EZ_CONFIG_STORE }
static void InitAppConfig(void) { AppConfig.Flags.bIsDHCPEnabled = TRUE; AppConfig.Flags.bInConfigMode = TRUE; memcpypgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr)); // { // _prog_addressT MACAddressAddress; // MACAddressAddress.next = 0x157F8; // _memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAddressAddress, sizeof(AppConfig.MyMACAddr)); // } AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2<<8ul | MY_DEFAULT_IP_ADDR_BYTE3<<16ul | MY_DEFAULT_IP_ADDR_BYTE4<<24ul; AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val; AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2<<8ul | MY_DEFAULT_MASK_BYTE3<<16ul | MY_DEFAULT_MASK_BYTE4<<24ul; AppConfig.DefaultMask.Val = AppConfig.MyMask.Val; AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2<<8ul | MY_DEFAULT_GATE_BYTE3<<16ul | MY_DEFAULT_GATE_BYTE4<<24ul; AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2<<8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3<<16ul | MY_DEFAULT_PRIMARY_DNS_BYTE4<<24ul; AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2<<8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3<<16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4<<24ul; // SNMP Community String configuration #if defined(STACK_USE_SNMP_SERVER) { BYTE i; static ROM char * ROM cReadCommunities[] = SNMP_READ_COMMUNITIES; static ROM char * ROM cWriteCommunities[] = SNMP_WRITE_COMMUNITIES; ROM char * strCommunity; for(i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++) { // Get a pointer to the next community string strCommunity = cReadCommunities[i]; if(i >= sizeof(cReadCommunities)/sizeof(cReadCommunities[0])) strCommunity = ""; // Ensure we don't buffer overflow. If your code gets stuck here, // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h // is either too small or one of your community string lengths // (SNMP_READ_COMMUNITIES) are too large. Fix either. if(strlenpgm(strCommunity) >= sizeof(AppConfig.readCommunity[0])) while(1); // Copy string into AppConfig strcpypgm2ram((char*)AppConfig.readCommunity[i], strCommunity); // Get a pointer to the next community string strCommunity = cWriteCommunities[i]; if(i >= sizeof(cWriteCommunities)/sizeof(cWriteCommunities[0])) strCommunity = ""; // Ensure we don't buffer overflow. If your code gets stuck here, // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h // is either too small or one of your community string lengths // (SNMP_WRITE_COMMUNITIES) are too large. Fix either. if(strlenpgm(strCommunity) >= sizeof(AppConfig.writeCommunity[0])) while(1); // Copy string into AppConfig strcpypgm2ram((char*)AppConfig.writeCommunity[i], strCommunity); } } #endif // Load the default NetBIOS Host Name memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16); FormatNetBIOSName(AppConfig.NetBIOSName); #if defined(ZG_CS_TRIS) // Load the default SSID Name if (sizeof(MY_DEFAULT_SSID_NAME) > sizeof(AppConfig.MySSID)) { ZGErrorHandler((ROM char *)"AppConfig.MySSID[] too small"); } memcpypgm2ram(AppConfig.MySSID, (ROM void*)MY_DEFAULT_SSID_NAME, sizeof(MY_DEFAULT_SSID_NAME)); #endif #if defined(EEPROM_CS_TRIS) { BYTE c; // When a record is saved, first byte is written as 0x60 to indicate // that a valid record was saved. Note that older stack versions // used 0x57. This change has been made to so old EEPROM contents // will get overwritten. The AppConfig() structure has been changed, // resulting in parameter misalignment if still using old EEPROM // contents. // TCPIP configuration settings will be moved to start from 0x0050. // The first 80 bytes will be used for application settings. XEEReadArray(0x0000, &c, 1); if(c == 0x60u) XEEReadArray(0x0001, (BYTE*)&AppConfig, sizeof(AppConfig)); else SaveAppConfig(); } #elif defined(SPIFLASH_CS_TRIS) { BYTE c; SPIFlashReadArray(0x0000, &c, 1); if(c == 0x60u) SPIFlashReadArray(0x0001, (BYTE*)&AppConfig, sizeof(AppConfig)); else SaveAppConfig(); } #endif }