aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/flash_ram/FlashProgram.c
blob: c2b228ffb4a610185a4d35a694575f25fd3ca2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * FlashProgram.c
 *
 *  Created on: Jan 22, 2019
 *      Author: shlomo
 */

#include "driverlib/rom.h"
#include "include.h"
#include <DataDef.h>
#include "FlashProgram.h"


//*****************************************************************************
//
// Read the application image from the file system and program it into flash.
//
// This function will attempt to open and read the firmware image file from
// the mass storage device.  If the file is found it will be programmed into
// flash.  The name of the file to be read is configured by the macro
// \b USB_UPDATE_FILENAME.  It will be programmed into flash starting at the
// address specified by ui32FlashStart.
//
// \return Zero if successful or non-zero if the file cannot be read or
// programmed.
//
//*****************************************************************************
uint32_t ReadAppAndProgram(uint32_t ui32FlashStart,uint32_t ui32FileSize,void* buffer)
{
    //uint32_t ui32FileSize;
    uint32_t ui32DataSize;
    uint32_t ui32Remaining;
    uint32_t ui32ProgAddr;
    uint32_t ui32BufferAddr;
    volatile uint32_t ui32Idx;
    uint32_t ui32FlashEnd;


    //
    // Get the size of flash.  This is the ending address of the flash.
    // If reserved space is configured, then the ending address is reduced
    // by the amount of the reserved block.
    //
    ui32FlashEnd = ui32FlashStart + ui32FileSize;
#warning check all addresses

    //
    // Check to make sure the file size is not too large to fit in the flash.
    // If it is too large, then return an error.
    //
    /*if((ui32FileSize + ui32FlashStart) > ui32FlashEnd)
    {
        return(1);
    }*/

    //
    // Enter a loop to erase all the requested flash pages beginning at the
    // application start address (above the USB stick updater).
    //
    for(ui32Idx = ui32FlashStart; ui32Idx < ui32FlashEnd; ui32Idx += 1024)
    {
        ROM_FlashErase(ui32Idx);
    }

    //
    // Enter a loop to read sectors from the application image file and
    // program into flash.  Start at the user app start address (above the USB
    // stick updater).
    //
    ui32ProgAddr = ui32FlashStart;
    ui32BufferAddr = buffer;
    ui32Remaining = ui32FileSize;
    while(ui32Remaining)
    {
        //
        // Compute how much data was read from this sector and adjust the
        // remaining amount.
        //

        ui32DataSize = ui32Remaining >= 512 ? 512 : ui32Remaining;
        ui32Remaining -= ui32DataSize;

        //
        // Call the function to program a block of flash.  The length of the
        // block passed to the flash function must be divisible by 4.
        //
        ROM_FlashProgram((uint32_t *)ui32BufferAddr, ui32ProgAddr,
                         (ui32DataSize + 3) & ~3);

        //
        // If there is more image to program, then update the programming
        // address.  Progress will continue to the next iteration of
        // the while loop.
        //
        if(ui32Remaining)
        {
            ui32ProgAddr += ui32DataSize;
            ui32BufferAddr += ui32DataSize;
        }

    }

    //
    // If we make it here, that means that an attempt to read a sector of
    // data from the device was not successful.  That means that the complete
    // user app has not been programmed into flash, so just return an error
    // indication.
    //
    return(1);
}