targets/TARGET_STM/TARGET_STM32G4/flash_api.c
/** Program one page starting at defined address
 *
 * The page should be at page boundary, should not cross multiple sectors.
 * This function does not do any check for address alignments or if size
 * is aligned to a page size.
 * @param obj The flash object
 * @param address The sector starting address
 * @param data The data buffer to be programmed
 * @param size The number of bytes to program
 * @return 0 for success, -1 for error
 */
Program one page starting at defined address
The page should be at page boundary, should not cross multiple sectors.
This function does not do any check for address alignments or if size
is aligned to a page size.
Program one page starting at defined address The page should be at page boundary should not cross multiple sectors . This function does not do any check for address alignments or if size is aligned to a page size .
int32_t flash_program_page(flash_t *obj, uint32_t address,
                           const uint8_t *data, uint32_t size)
{
    uint32_t StartAddress = 0;
    int32_t status = 0;
    if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
        return -1;
    }
    if ((size % 8) != 0) {
        return -1;
    }
    if (flash_unlock() != HAL_OK) {
        return -1;
    }
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
    StartAddress = address;
    if ((uint32_t) data % 8 != 0) {
        volatile uint64_t data64;
        while ((address < (StartAddress + size)) && (status == 0)) {
            for (uint8_t i = 0; i < 8; i++) {
                *(((uint8_t *) &data64) + i) = *(data + i);
            }
            if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data64)
                    == HAL_OK) {
                address = address + 8;
                data = data + 8;
            } else {
                status = -1;
            }
        }
    } else { 
        while ((address < (StartAddress + size)) && (status == 0)) {
            if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address,
                                  *((uint64_t *) data))
                    == HAL_OK) {
                address = address + 8;
                data = data + 8;
            } else {
                status = -1;
            }
        }
    }
    flash_lock();
    return status;
}
int32_t flash_program_page ( flash_t * obj , uint32_t address , const uint8_t * data , uint32_t size ) { uint32_t StartAddress = 0 ; int32_t status = 0 ; if ( ( address >= ( FLASH_BASE + FLASH_SIZE ) ) || ( address < FLASH_BASE ) ) { return -1 ; } if ( ( size % 8 ) != 0 ) { return -1 ; } if ( flash_unlock ( ) != HAL_OK ) { return -1 ; } __HAL_FLASH_CLEAR_FLAG ( FLASH_FLAG_ALL_ERRORS ) ; StartAddress = address ; if ( ( uint32_t ) data % 8 != 0 ) { volatile uint64_t data64 ; while ( ( address < ( StartAddress + size ) ) && ( status == 0 ) ) { for ( uint8_t i = 0 ; i < 8 ; i ++ ) { * ( ( ( uint8_t * ) & data64 ) + i ) = * ( data + i ) ; } if ( HAL_FLASH_Program ( FLASH_TYPEPROGRAM_DOUBLEWORD , address , data64 ) == HAL_OK ) { address = address + 8 ; data = data + 8 ; } else { status = -1 ; } } } else { while ( ( address < ( StartAddress + size ) ) && ( status == 0 ) ) { if ( HAL_FLASH_Program ( FLASH_TYPEPROGRAM_DOUBLEWORD , address , * ( ( uint64_t * ) data ) ) == HAL_OK ) { address = address + 8 ; data = data + 8 ; } else { status = -1 ; } } } flash_lock ( ) ; return status ; }
Program one page starting at defined address
 The page should be at page boundary, should not cross multiple sectors.
Program one page starting at defined address The page should be at page boundary should not cross multiple sectors .
/* L4 flash devices can only be programmed 64bits/8 bytes at a time */ /* Clear error programming flags */ /* Program the user Flash area word by word */ /*  HW needs an aligned address to program flash, which data
     *  parameters doesn't ensure  */ /*  case where data is aligned, so let's avoid any copy */