aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/flash_ram/FlashProgram.c
blob: c757c3a3b9b3c9c2d6b23ba037c1bb1b4368a146 (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
51pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; backg
/*
 * FlashProgram.c
 *
 *  Created on: Jan 22, 2019
 *      Author: shlomo
 */

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

//#define FLASH_DEBUG
#ifdef FLASH_DEBUG
#define  MAX_CONT_LOG 100

#define READ 1
#define DELETE 2
uint32_t Address[MAX_CONT_LOG+1] = {0};
uint32_t Size[MAX_CONT_LOG+1] = {0};
uint32_t EraseWrite[MAX_CONT_LOG+1] = {0};
byte RxIndex = 0;
#endif
//*****************************************************************************
//
// 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 status = OK;
    //uint32_t ui32FlashEnd;

#ifdef FLASH_DEBUG
    Address[RxIndex] = ui32FlashStart;
    Size[RxIndex] = ui32FileSize;
    EraseWrite[RxIndex] = READ;
    if (RxIndex++>=MAX_CONT_LOG)
        RxIndex = 0;
#endif
    //
    // 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;


    //
    // 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 = (uint32_t)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_IntMasterDisable();
        status = ROM_FlashProgram((uint32_t *)ui32BufferAddr, ui32ProgAddr,(ui32DataSize + 3) & ~3);
        ROM_IntMasterEnable();
        if (status)
            Report("FlashProgram Error", __FILE__,status,ui32ProgAddr , RpMessage, (ui32DataSize + 3) & ~3, 0);

        //
        // 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);
}
int base_flashErased = false;
uint32_t EraseFlashSection(void)
{
    volatile uint32_t ui32Idx = FLASH_RAM_BASE;
    uint32_t status;

    if (base_flashErased == true)
        return WARNING;
    else
        base_flashErased = true; //once in a run

#ifdef FLASH_DEBUG
    Address[RxIndex] = ui32FlashStart;
    Size[RxIndex] = ui32FileSize;
    EraseWrite[RxIndex] = DELETE;
    if (RxIndex++>=MAX_CONT_LOG)
        RxIndex = 0;
#endif
    status = FlashErase(ui32Idx);
    //FlashErase returns 0 or -1
    return status;

}
/*void FlashTest(void)
{
    int i;
    uint32_t stack[0x200];
    uint32_t offset = 0;

    for (i = 0; i < 0x200; i++)
        stack[i] = i;

    while (offset < FLASH_SIZE)
    {
        ReadAppAndProgram(FLASH_RAM_BASE+offset,0x200,stack);
        offset+=0x200;
    }
    EraseFlashSection();

}*/
//Each word may not be subject to more than a specific number of programming cycles before an
//erase cycle is required. In other words, for any given word, FlashProgram can only be called
//twice before FlashErase is called.
void FlashInit(void)
{
    volatile uint32_t ui32Idx;
    //FlashUsecSet(120);
#ifdef FLASH_DEBUG
    Address[RxIndex] = FLASH_RAM_BASE;
    Size[RxIndex] = FLASH_SIZE;
    EraseWrite[RxIndex] = DELETE;
    if (RxIndex++>=MAX_CONT_LOG)
        RxIndex = 0;
#endif
    ROM_IntMasterDisable();
    for(ui32Idx = FLASH_RAM_BASE; ui32Idx < FLASH_RAM_BASE+FLASH_SIZE; ui32Idx += 1024)
    {
        ROM_FlashErase(ui32Idx);
    }
    ROM_IntMasterEnable();
}