Skip to content

Make NRF52 flash work with SoftDevice #6124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2018
Merged

Make NRF52 flash work with SoftDevice #6124

merged 1 commit into from
Feb 20, 2018

Conversation

marcuschangarm
Copy link
Contributor

@marcuschangarm marcuschangarm commented Feb 16, 2018

The way flash writes and erases are done on the NRF52 changes depends on whether the SoftDevice is enabled or not.

This change does a runtime check before erasing and writing, and then chooses the correct function to perform the action.

How you write and ersae the flash on the NRF52 changes depending on
whether the SoftDevice is enabled or not.

This change does a runtime check before erasing and writing, and
then chooses the correct function to perform the action.
Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was this tested?

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 19, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Feb 19, 2018

Build : SUCCESS

Build number : 1175
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6124/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Feb 19, 2018

@marcuschangarm
Copy link
Contributor Author

Using the mbed OS flash tests, a custom test, and this to enable the SoftDevice:

#include "ble/BLE.h"

void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
}

int main(void)
{ 
    BLE &ble = BLE::Instance();
    ble.init(bleInitComplete);

    FlashIAP flash;

    /** Initialize a flash IAP device
     *
     *  Should be called once per lifetime of the object.
     *  @return 0 on success or a negative error code on failure
     */
    int ret_init = flash.init();
    printf("init: %d\r\n", ret_init);

    /** Get the sector size at the defined address
     *
     *  Sector size might differ at address ranges. 
     *  An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
     *
     *  @param addr Address of or inside the sector to query
     *  @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
     */
    uint32_t sector_size = flash.get_sector_size(0);
    printf("get_sector_size: %u\r\n", sector_size);

    /** Get the flash start address 
     *
     *  @return Flash start address 
     */
    uint32_t flash_start = flash.get_flash_start();
    printf("get_flash_start: %u\r\n", flash_start);

    /** Get the flash size
     *
     *  @return Flash size 
     */
    uint32_t flash_size = flash.get_flash_size();
    printf("get_flash_size: %u\r\n", flash_size);

    /** Get the program page size
     *
     *  The page size defines the writable page size
     *  @return Size of a program page in bytes
     */
    uint32_t page_size = flash.get_page_size();
    printf("get_page_size: %u\r\n", page_size);



    uint8_t buffer[256];
    uint32_t address = flash_start + flash_size - 10 * sector_size;


    /** Read data from a flash device. 
     *
     *  This method invokes memcpy - reads number of bytes from the address
     *
     *  @param buffer Buffer to write to
     *  @param addr   Flash address to begin reading from
     *  @param size   Size to read in bytes
     *  @return       0 on success, negative error code on failure
     */
    int ret_read = flash.read(buffer, address, sizeof(buffer));

    printf("ret_read: %d\r\n", ret_read);
    for (size_t index = 0; index < sizeof(buffer); index++)    
    {
        printf("%02X", buffer[index]);
    }
    printf("\r\n");

    /** Erase sectors
     *
     *  The state of an erased sector is undefined until it has been programmed
     *
     *  @param addr Address of a sector to begin erasing, must be a multiple of the sector size
     *  @param size Size to erase in bytes, must be a multiple of the sector size
     *  @return     0 on success, negative error code on failure
     */
    int ret_erase = flash.erase(address, sector_size);

    printf("ret_erase: %d\r\n", ret_erase);


    ret_read = flash.read(buffer, address, sizeof(buffer));

    printf("ret_read: %d\r\n", ret_read);
    for (size_t index = 0; index < sizeof(buffer); index++)    
    {
        printf("%02X", buffer[index]);
    }
    printf("\r\n");



    /** Program data to pages
     *
     *  The sectors must have been erased prior to being programmed
     *
     *  @param buffer Buffer of data to be written
     *  @param addr   Address of a page to begin writing to, must be a multiple of program and sector sizes
     *  @param size   Size to write in bytes, must be a multiple of program and sector sizes
     *  @return       0 on success, negative error code on failure
     */
    for (size_t index = 0; index < sizeof(buffer); index++) 
    {
        buffer[index] = index;
    }

    int ret_program = flash.program(buffer, address, sizeof(buffer));

    printf("ret_program: %d\r\n", ret_program);



    ret_read = flash.read(buffer, address, sizeof(buffer));

    printf("ret_read: %d\r\n", ret_read);
    for (size_t index = 0; index < sizeof(buffer); index++)    
    {
        printf("%02X", buffer[index]);
    }
    printf("\r\n");


    /** Deinitialize a flash IAP device
     *
     *  @return 0 on success or a negative error code on failure
     */
    int ret_deinit = flash.deinit();

    printf("deinit: %d\r\n", ret_deinit);
 
   for (;;)
    {
        printf("blink!\r\n");

        led1 = !led1;
        wait_ms(1000);
    }
}

@mbed-ci
Copy link

mbed-ci commented Feb 19, 2018

@cmonr cmonr merged commit 8008227 into ARMmbed:master Feb 20, 2018
@marcuschangarm marcuschangarm deleted the nrf52_flash_fix branch February 21, 2018 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants