/** * Erase a range of flash */ void eraseRange(FlashData_t *flashData) { uint32_t address = fixAddress(flashData->address); uint32_t endAddress = address + flashData->dataSize; uint32_t pageMask = flashData->sectorSize-1U; if ((flashData->flags&DO_ERASE_RANGE) == 0) { return; } // Check for empty range before block rounding if (flashData->dataSize == 0) { return; } // Round start address to start of block (inclusive) address &= ~pageMask; // Round end address to end of block (inclusive) endAddress |= pageMask; // Erase each sector while (address < endAddress) { flashData->controller->fccob0_3 = (F_ERSSCR << 24) | address; executeCommand(flashData->controller); // Advance to start of next sector address += flashData->sectorSize; } flashData->flags &= ~DO_ERASE_RANGE; }
/** * Program a range of flash from buffer * * Returns an error if the security location is to be programmed * to permanently lock the device */ void programRange(FlashData_t *flashData) { uint32_t address = fixAddress(flashData->address); uint32_t endAddress = flashData->address+flashData->dataSize; const uint32_t *data = flashData->dataAddress; if ((flashData->flags&DO_PROGRAM_RANGE) == 0) { return; } if ((address & 0x07) != 0) { setErrorCode(FLASH_ERR_ILLEGAL_PARAMS); } // Program words while (address < endAddress) { if (address == (NV_FSEC_ADDRESS&~3)) { // Check for permanent secure value if ((*data & (FTFL_FSEC_MEEN_MASK)) == (FTFL_FSEC_MEEN_DISABLE)) { setErrorCode(FLASH_ERR_ILLEGAL_SECURITY); } } flashData->controller->fccob0_3 = (F_PGM8 << 24) | address; flashData->controller->fccob4_7 = *data++; flashData->controller->fccob8_B = *data++; executeCommand(flashData->controller); address += 8; } flashData->flags &= ~DO_PROGRAM_RANGE; }
/** * Erase entire flash block */ void eraseFlashBlock(FlashData_t *flashData) { uint32_t address = fixAddress(flashData->address); if ((flashData->flags&DO_ERASE_BLOCK) == 0) { return; } flashData->controller->fccob0_3 = (F_ERSBLK << 24) | address; executeCommand(flashData->controller); flashData->flags &= ~DO_ERASE_BLOCK; }
/** * Write multiple bytes (registers) in the accelerometer * *@param reg register value of the accelerometer from where the writing will get started *@param count number of the bytes to be written *@param buffer storing the bytes to be written * *@return the status 0= Failure Others= Success */ uint8_t st_LIS3LwriteMultipleRegisters(uint8_t reg,uint8_t count, uint8_t* buffer ) { uint8_t modified_reg; init_i2c(); modified_reg= fixAddress(count,reg); if (msp430_i2c_write(I2C_START,LIS3L_I2C_ADDRESS, 1,&modified_reg)!=0) return msp430_i2c_write(I2C_STOP,LIS3L_I2C_ADDRESS, count, buffer); else return 0; //Failure }
/* * Verify a range of flash against buffer */ void verifyRange(FlashData_t *flashData) { uint32_t address = fixAddress(flashData->address); uint32_t endAddress = flashData->address+flashData->dataSize; const uint32_t *data = flashData->dataAddress; if ((flashData->flags&DO_VERIFY_RANGE) == 0) { return; } // Verify words while (address < endAddress) { flashData->controller->fccob0_3 = (F_PGMCHK << 24) | address; flashData->controller->fccob4_7 = (F_USER_MARGIN<<24) | 0; flashData->controller->fccob8_B = *data; executeCommand(flashData->controller); address += 4; data++; } flashData->flags &= ~DO_VERIFY_RANGE; }