/******************************************************************************* * Function Name: BLA_SetFlashByte ******************************************************************************** * Summary: * Sets byte at specified address in Flash. * * Parameters: * None * * Returns: * None * *******************************************************************************/ void BLA_SetFlashByte(uint32 address, uint8 runType) { uint32 flsAddr = address - CYDEV_FLASH_BASE; uint8 rowData[CYDEV_FLS_ROW_SIZE]; #if !(CY_PSOC4) uint8 arrayId = (flsAddr / CYDEV_FLS_SECTOR_SIZE); #endif /* !(CY_PSOC4) */ uint16 rowNum = ((flsAddr % CYDEV_FLS_SECTOR_SIZE) / CYDEV_FLS_ROW_SIZE); uint32 baseAddr = address - (address % CYDEV_FLS_ROW_SIZE); uint16 idx; for (idx = 0; idx < CYDEV_FLS_ROW_SIZE; idx++) { rowData[idx] = BLA_GET_CODE_DATA(baseAddr + idx); } rowData[address % CYDEV_FLS_ROW_SIZE] = runType; #if(CY_PSOC4) CySysFlashWriteRow(rowNum, rowData); #else CyWriteRowData(arrayId, rowNum, rowData); #endif /* (CY_PSOC4) */ }
/******************************************************************************* * The following code is OBSOLETE and must not be used. *******************************************************************************/ void Bootloadable_1_SetFlashByte(uint32 address, uint8 runType) { uint32 flsAddr = address - CYDEV_FLASH_BASE; uint8 rowData[CYDEV_FLS_ROW_SIZE]; #if !(CY_PSOC4) uint8 arrayId = ( uint8 )(flsAddr / CYDEV_FLS_SECTOR_SIZE); #endif /* !(CY_PSOC4) */ #if (CY_PSOC4) uint16 rowNum = ( uint16 )(flsAddr / CYDEV_FLS_ROW_SIZE); #else uint16 rowNum = ( uint16 )((flsAddr % CYDEV_FLS_SECTOR_SIZE) / CYDEV_FLS_ROW_SIZE); #endif /* (CY_PSOC4) */ uint32 baseAddr = address - (address % CYDEV_FLS_ROW_SIZE); uint16 idx; for (idx = 0u; idx < CYDEV_FLS_ROW_SIZE; idx++) { rowData[idx] = Bootloadable_1_GET_CODE_DATA(baseAddr + idx); } rowData[address % CYDEV_FLS_ROW_SIZE] = runType; #if(CY_PSOC4) (void) CySysFlashWriteRow((uint32) rowNum, rowData); #else (void) CyWriteRowData(arrayId, rowNum, rowData); #endif /* (CY_PSOC4) */ #if(CY_PSOC5) /*************************************************************************** * When writing to flash, data in the instruction cache can become obsolete. * Therefore, the cache data does not correlate to the data just written to * flash. A call to CyFlushCache() is required to invalidate the data in the * cache and force fresh information to be loaded from flash. ***************************************************************************/ CyFlushCache(); #endif /* (CY_PSOC5) */ }
/******************************************************************************* * Function Name: CyBLE_Nvram_Write ******************************************************************************** * * Summary: * This API writes the data to the NVRAM store. It will check the appropriate * alignment of a start address and also perform an address range check based * on the length before performing the write operation. * This function performs memory compare and writes only row where there are new * data to write. * * Parameters: * const uint8 *buffer: Pointer to the buffer containing the data to be stored. * const uint8 *varFlash: Pointer to the array or variable in the flash. * uint16 length: the length of the data in bytes. * * Return: * CYRET_SUCCESS a successful write * CYRET_BAD_PARAM A request to write outside the flash boundary. * CYRET_UNKNOWN Other errors in writing the flash * * Side Effects: * For BLE devices with 128K of Flash memory this API will automatically * modify the clock settings for the device. * Writing to flash requires changes to be done to the IMO (set to 48 MHz) * and HFCLK (source set to IMO) settings. The configuration is restored before * returning. This will impact the operation of most of the hardware in the * device. * *******************************************************************************/ cystatus CyBLE_Nvram_Write (const uint8 buffer[], const uint8 varFlash[], uint16 length) { uint8 writeBuffer[CY_FLASH_SIZEOF_ROW]; uint32 rowId; uint32 dstIndex; uint32 srcIndex = 0u; cystatus rc = CYRET_SUCCESS; uint32 eeOffset; uint32 byteOffset; uint32 rowsNotEqual; eeOffset = (uint32)varFlash; /* Make sure, that varFlash[] points to Flash */ if ((eeOffset + length) < CYBLE_HAL_FLASH_END_ADDR) { rowId = eeOffset / CY_FLASH_SIZEOF_ROW; byteOffset = CY_FLASH_SIZEOF_ROW * rowId; while ((srcIndex < length) && (CYRET_SUCCESS == rc)) { rowsNotEqual = 0u; /* Copy data to the write buffer either from the source buffer or from the flash */ for (dstIndex = 0u; dstIndex < CY_FLASH_SIZEOF_ROW; dstIndex++) { if ((byteOffset >= eeOffset) && (srcIndex < length)) { /* Detect that row programming is required */ if(writeBuffer[dstIndex] != buffer[srcIndex]) { writeBuffer[dstIndex] = buffer[srcIndex]; rowsNotEqual = 1u; } srcIndex++; } else { writeBuffer[dstIndex] = CY_GET_XTND_REG8(CYDEV_FLASH_BASE + byteOffset); } byteOffset++; } if(rowsNotEqual != 0u) { /* Write flash row */ rc = CySysFlashWriteRow(rowId, writeBuffer); } /* Go to the next row */ rowId++; } } else { rc = CYRET_BAD_PARAM; } /* Mask return codes from flash, if they are not supported */ if ((CYRET_SUCCESS != rc) && (CYRET_BAD_PARAM != rc)) { rc = CYRET_UNKNOWN; } return (rc); }
cystatus Em_EEPROM_Write(const uint8 srcBuf[], const uint8 eepromPtr[], uint32 byteCount) #endif /* (!CY_PSOC3) */ { uint8 writeBuffer[CY_FLASH_SIZEOF_ROW]; uint32 arrayId; uint32 rowId; uint32 dstIndex; uint32 srcIndex; cystatus rc; uint32 eeOffset; uint32 byteOffset; #if (!CY_PSOC4) #if (CYDEV_ECC_ENABLE == 0) uint8 rowBuffer[CY_FLASH_SIZEOF_ROW + CY_FLASH_SIZEOF_ECC_ROW]; #endif /* CYDEV_ECC_ENABLE == 0) */ #endif /* (!CY_PSOC4) */ eeOffset = (uint32)eepromPtr; #if (CY_PSOC3) eeOffset &= Em_EEPROM_CODE_ADDR_MASK; #endif /* (CY_PSOC3) */ /* Make sure, that eepromPtr[] points to ROM */ #if (CY_PSOC3) if (((uint32)eepromPtr >= Em_EEPROM_CODE_ADDR_OFFSET) && (((uint32)eepromPtr + byteCount) <= Em_EEPROM_CODE_ADDR_END)) #else if (((uint32)eepromPtr + byteCount) < Em_EEPROM_FLASH_END_ADDR) #endif /* (CY_PSOC3) */ { #if (!CY_PSOC4) (void)CySetTemp(); #if(CYDEV_ECC_ENABLE == 0) (void)CySetFlashEEBuffer(rowBuffer); #endif #endif /* (!CY_PSOC4) */ arrayId = eeOffset / CY_FLASH_SIZEOF_ARRAY; rowId = (eeOffset / CY_FLASH_SIZEOF_ROW) % Em_EEPROM_ROWS_IN_ARRAY; byteOffset = (CY_FLASH_SIZEOF_ARRAY * arrayId) + (CY_FLASH_SIZEOF_ROW * rowId); srcIndex = 0u; rc = CYRET_SUCCESS; while ((srcIndex < byteCount) && (CYRET_SUCCESS == rc)) { /* Copy data to the write buffer either from the source buffer or from the flash */ for (dstIndex = 0u; dstIndex < CY_FLASH_SIZEOF_ROW; dstIndex++) { if ((byteOffset >= eeOffset) && (srcIndex < byteCount)) { writeBuffer[dstIndex] = srcBuf[srcIndex]; srcIndex++; } else { writeBuffer[dstIndex] = CY_GET_XTND_REG8(CYDEV_FLASH_BASE + byteOffset); } byteOffset++; } /* Write flash row */ #if (CY_PSOC4) rc = CySysFlashWriteRow(rowId, writeBuffer); #else rc = CyWriteRowData((uint8)arrayId, (uint16)rowId, writeBuffer); #endif /* (CY_PSOC4) */ /* Go to the next row */ rowId++; #if (CY_FLASH_NUMBER_ARRAYS > 1) if (rowId >= Em_EEPROM_ROWS_IN_ARRAY) { arrayId++; rowId = 0u; } #endif /* (CY_FLASH_NUMBER_ARRAYS > 1) */ } /* Flush both Cache and PHUB prefetch buffer */ #if (CY_PSOC5) CyFlushCache(); #elif (CY_PSOC3) CY_FLASH_CONTROL_REG |= 6u; #endif /* (CY_PSOC5) */ } else { rc = CYRET_BAD_PARAM; } /* Mask return codes from flash, if they are not supported */ if ((CYRET_SUCCESS != rc) && (CYRET_BAD_PARAM != rc)) { rc = CYRET_UNKNOWN; } return (rc); }