Peripheral in Master Mode (SPI)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Peripheral in Master Mode (SPI)

922 Views
marcohlmann
Contributor I

I have managed to get the freedom_bootloader (from Kinetis Bootloader v2.0)  code sample working on an MK22FN512xxx12 microcontroller on a custom board. I used the provided KinetisFlashTool to upload the led_demo_a000 and successfully booted into it, I have also managed to do the same with my own project code. 

 

For my project, I have an external chip with its own memory space (AT25 Flash). On the AT25 Flash, I will have a flag that notifies the bootloader that an update is ready to be made. The bootloader will then read from a specific address in the AT25 flash which contains the updated firmware that needs to be written to the MK22F512 flash. However, as specified in the documentation, SPI is only supported in slave mode, so I need to modify the bootloader to use SPI in master mode.

 

At the moment I have the SPI disabled according to the bootloader. I have injected some routines of my own inside bootloader_init() that configure the SPI to run in master mode. I am able to communicate with the AT25 and read from it, but I cannot seem to find out how to approach writing to the MK22F512 flash.

 

I don't have a full understanding about how the bootloader works, but I was hoping to find a way to modify the SPI peripheral to initialize as a master. The modified peripheral would then notify the bootloader that the SPI peripheral is active, and start the data transfer + flash program routines. If I cannot do that then I need to just manually write to the flash from bootloader_init() and let the rest of the bootloader run with a dummy peripheral so that it just leads to a timeout. 

 

The problem is I don't understand is how to get the data transfer + flash program routines to start. I also don't know how to manually write to the MK22F512 flash. I tried using the commands provided by fsl_flash, but I keep running into error status codes and hard faults.

 

I will appreciate any insights, alternate approaches, or anything that can help me get this working. I have looked through all the documentation I could find on the bootloader and searched for related questions here, but I am still at a loss.

Labels (1)
1 Reply

672 Views
ramboyang
NXP Employee
NXP Employee

Hello,

Based on above description, it looks there are two flash on your design, the Internal FTFE and the AT25 SPI NOR FLASH.

And my understanding is that you were trying to copy data from the AT25 SPI NOR FLash to internal Flash, right? Maybe you can refer to the bl_reliable_update.c for more details.

static bool get_result_after_copying_application(uint32_t src, uint32_t dst, uint32_t len)
{
    bool updateResult = true;
    status_t status;

    // Erase the destination application region
    status = mem_erase(dst, len);
    if (kStatus_Success != status)
    {
        updateResult = false;
    }
    else
    {
        uint32_t copyBuffer[8]; // Bufer used to hold the data to be written.
        uint32_t writeSize;
        uint32_t bufferSize = sizeof(copyBuffer);
        // Copy the source application to destination application region.
        while (len)
        {
            if (len >= bufferSize)
            {
                writeSize = bufferSize;
            }
            else
            {
                writeSize = len;
            }
            memcpy(copyBuffer, (uint8_t *)src, writeSize);
            status = mem_write(dst, writeSize, (uint8_t *)&copyBuffer[0]);
            if (kStatus_Success != status)
            {
                updateResult = false;
                break;
            }
            else
            {
                src += writeSize;
                dst += writeSize;
                len -= writeSize;
            }
        } // while(len)
    }
    return updateResult;
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

"The problem is I don't understand is how to get the data transfer + flash program routines to start. I also don't know how to manually write to the MK22F512 flash. I tried using the commands provided by fsl_flash, but I keep running into error status codes and hard faults."

If possible, please attach your codes, so that we can check it together to find out the root cause