fdlapp_control.c 12.7 KB
Newer Older
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
/*********************************************************************************************************************
 * File Name     : $Source: fdlapp_control.c $
 * Mod. Revision : $Revision: 1.8 $
 * Mod. Date     : $Date: 2014/07/30 13:14:47MESZ $
 * Device(s)     : RV40 Flash based RH850 microcontroller
 * Description   : Application sample program control module
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * DISCLAIMER 
 * This software is supplied by Renesas Electronics Corporation and is only  intended for use with Renesas products. 
 * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all 
 * applicable laws, including copyright laws. 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED 
 * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. 
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS 
 * AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY 
 * REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of 
 * this software. By using this software, you agree to the additional terms and conditions found by accessing the
 * following link: 
 * http://www.renesas.com/disclaimer 
 * 
 * Copyright (C) 2014 Renesas Electronics Corporation. All rights reserved.     
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * header files include
 *********************************************************************************************************************/
#include "r_typedefs.h"
#include "r_fdl.h"
#include "fdl_descriptor.h"
#include "target.h"
#include "Watchdog.h"
#include "Emulated_EEPROM_Access.h"
#include "fdl_user.h"
#include <stdint.h>

/*********************************************************************************************************************
 * Module internal function prototypes
 *********************************************************************************************************************/

/*********************************************************************************************************************
   Function name:  SampleApp_FDL_Control
 *********************************************************************************************************************/
/**
    EEL sample function to handle FDL functionality. Executing a set of dummy Flash operations

    @param          ---
    @return         ---
 */
/*********************************************************************************************************************/
#define OFFSET_ADDR 0xFF200000UL

void SampleApp_FDL_Control(void)
{

    r_fdl_status_t fdlRet;
    r_fdl_request_t req;
    uint8_t wBuf_au08[64] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
                             0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
                             0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
                             0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
                             0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
    uint32_t rBuf_au32[64]; /* Read buffer must be 32bit aligned as the read operation accesses it 32bit aligned */
    volatile uint16_t data;

    /* 1st initialize the FDL */
    fdlRet = R_FDL_Init(&sampleApp_fdlConfig_enu);
    if (R_FDL_OK != fdlRet)
    {
        /* Error handler */
        while (1)
            ;
    }

#ifndef R_FDL_LIB_V1_COMPATIBILITY
    /* Prepare the environment */
    req.idx_u32 = 0;
    req.command_enu = R_FDL_CMD_PREPARE_ENV;
    req.cnt_u16 = 0;
    req.accessType_enu = R_FDL_ACCESS_NONE;
    R_FDL_Execute(&req);

    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
    }
    if (R_FDL_OK != req.status_enu)
    {
        /* Error handler */
        while (1)
            ;
    }
#endif

    /* -----------------------------------------------------------------------
       Example...
       Read a Word --> address 0x0004 of the Data Flash
       1) Do a blank check to distinguish if the read value is based on erased
          data or written data. We can only trust written data
       2) read the data 
       ----------------------------------------------------------------------- */
    req.command_enu = R_FDL_CMD_BLANKCHECK;
    req.idx_u32 = 0; //地址
    req.cnt_u16 = 1; //
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);

    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
    }

    if (R_FDL_OK == req.status_enu)
    {
        /* The half word is blank... we may not read */
    }
    else if (R_FDL_ERR_BLANKCHECK == req.status_enu)
    {
        /* Read the data */
        data = (*(uint16_t *)0xff200004);
    }
    else
    {
        /* Error handler.. on an internal error */
        while (1)
            ;
    }

    /* -----------------------------------------------------------------------
       Example...
       Erase Flash block 0
       ----------------------------------------------------------------------- */
    req.command_enu = R_FDL_CMD_ERASE;
    req.idx_u32 = 0; //起始块///32K
    req.cnt_u16 = 1; //块计数
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);

    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
    }
    if (R_FDL_OK != req.status_enu)
    {
        /* Error handler */
        while (1)
            ;
    }
    /* wBuf_au08[0] = 0xaa;
    wBuf_au08[1] = 0x55;
    wBuf_au08[2] = 0xCC;
    wBuf_au08[3] = 0xFF;

    wBuf_au08[20] = 0xaa;
    wBuf_au08[21] = 0x55;
    wBuf_au08[22] = 0xCC;
    wBuf_au08[23] = 0xFF;*/
    /* -----------------------------------------------------------------------
       Write data:
       addresses 0x00000000 - 0x0000001F
       ----------------------------------------------------------------------- */
    req.command_enu = R_FDL_CMD_WRITE;
    req.idx_u32 = 0x0000; //地址
    req.cnt_u16 = 0x8;    //长度  单位u32
    req.bufAddr_u32 = (uint32_t)(&wBuf_au08[0]);
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);
    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
    }
    if (R_FDL_OK != req.status_enu)
    {
        /* Error handler */
        while (1)
            ;
    }

    /* -----------------------------------------------------------------------
       Read data:
       addresses 0x00000000 - 0x0000001F
       ----------------------------------------------------------------------- */
    req.command_enu = R_FDL_CMD_READ;
    req.idx_u32 = 0x0000; //地址
    req.cnt_u16 = 0x8;    //长度  单位u32
    req.bufAddr_u32 = (uint32_t)(&rBuf_au32[0]);
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);
    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
    }
    if (R_FDL_OK != req.status_enu)
    {
        /* Error handler */
        while (1)
            ;
    }

    /* -----------------------------------------------------------------------
       Read data:
       addresses 0x00000020 - 0x00000023 
       ... Most probably resulting in ECC error
       ----------------------------------------------------------------------- */
    req.command_enu = R_FDL_CMD_READ;
    req.idx_u32 = 0x0020;
    req.cnt_u16 = 0x1;
    req.bufAddr_u32 = (uint32_t)(&rBuf_au32[0]);
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);
    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
    }

    if ((R_FDL_ERR_ECC_SED != req.status_enu) &&
        (R_FDL_ERR_ECC_DED != req.status_enu))
    {
        /* Error handler */
        while (1)
            ;
    }

    return;
}
/*********************************************************************************/
/*********************************************************************************/
/*********************************************************************************/
r_fdl_status_t TYW_FDL_Init(void)
{
    r_fdl_status_t fdlRet;
    r_fdl_request_t req;

    FDL_Open();

    /* 1st initialize the FDL */
    fdlRet = R_FDL_Init(&sampleApp_fdlConfig_enu);
    if (R_FDL_OK != fdlRet)
    {
        /* Error handler */
        return fdlRet;
    }

#ifndef R_FDL_LIB_V1_COMPATIBILITY
    /* Prepare the environment */
    req.command_enu = R_FDL_CMD_PREPARE_ENV;
    req.idx_u32 = 0;
    req.cnt_u16 = 0;
    req.accessType_enu = R_FDL_ACCESS_NONE;
    R_FDL_Execute(&req);

    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
        WDT_Clear();
    }
    return req.status_enu;
#endif
}

r_fdl_status_t TYW_FDL_Erase(uint32_t u32StartAddr, uint32_t u32EndAddr)
{

    r_fdl_request_t req;
    uint32_t StartBlockIndex = 0U;
    uint32_t EndBlockIndex = 0U;

    StartBlockIndex = ((u32StartAddr - OFFSET_ADDR) / 64U);
    EndBlockIndex = ((u32EndAddr - OFFSET_ADDR) / 64U);

    req.command_enu = R_FDL_CMD_ERASE;
    req.idx_u32 = StartBlockIndex;                       //起始块///32K
    req.cnt_u16 = (EndBlockIndex + 1 - StartBlockIndex); //1; //块计数
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);

    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
        WDT_Clear();
    }
    if (R_FDL_OK == req.status_enu)
    {
        EEPROM_Mem_Access_Complete_Callback();
    }

    return req.status_enu;
}
r_fdl_status_t TYW_FDL_Write(uint32_t u32Addr, uint32_t u32Data[], uint32_t u32Len)
{
    r_fdl_request_t req;

    req.command_enu = R_FDL_CMD_WRITE;
    req.idx_u32 = u32Addr - OFFSET_ADDR; //地址
    req.cnt_u16 = u32Len;                //长度  单位u32
    req.bufAddr_u32 = (uint32_t)(&u32Data[0]);
    req.accessType_enu = R_FDL_ACCESS_USER;
    R_FDL_Execute(&req);
    while (R_FDL_BUSY == req.status_enu)
    {
        R_FDL_Handler();
        WDT_Clear();
    }
    if (R_FDL_OK == req.status_enu)
    {
        EEPROM_Mem_Access_Complete_Callback();
    }

    return req.status_enu;
}
/*
u32StartAddr:需是4的倍数
u32EndAddr:加1之后需是4的倍数
*/
uint32_t TYW_FDL_Blank_Check(uint32_t u32StartAddr, uint32_t u32EndAddr)
{
    r_fdl_request_t req;
    uint32_t u32Addr = 0xFFFFFFFFU;
    uint32_t i;
    uint32_t Result;
    uint32_t Count = (u32EndAddr - u32StartAddr + 1) / 4;
    uint32_t CalBuf = 0U;

    CalBuf = u32EndAddr - (u32EndAddr % 4);

    for (i = 0; i < Count; i++)
    {
        req.command_enu = R_FDL_CMD_BLANKCHECK;
        req.idx_u32 = CalBuf - OFFSET_ADDR; //
        req.cnt_u16 = 1;                    //
        req.accessType_enu = R_FDL_ACCESS_USER;
        R_FDL_Execute(&req);

        while (R_FDL_BUSY == req.status_enu)
        {
            R_FDL_Handler();
            WDT_Clear();
        }

        if (R_FDL_OK == req.status_enu)
        {
            u32Addr = CalBuf - u32StartAddr;
            CalBuf -= 4;
        }
        else
        {
            break;
        }
    }
    return (u32Addr);
}

r_fdl_status_t TYW_FDL_Flash_Read(uint32_t u32Addr, uint32_t u32Data[], uint32_t u32Len)
{
    r_fdl_request_t req;
    uint32_t u32BlankCeck = 0UL;

    u32BlankCeck = TYW_FDL_Blank_Check(u32Addr, (u32Addr + u32Len * 4UL-1UL));
    if (u32BlankCeck == 0xFFFFFFFFU)
    {
        req.command_enu = R_FDL_CMD_READ;
        req.idx_u32 = u32Addr - OFFSET_ADDR; //地址
        req.cnt_u16 = u32Len;                //长度  单位u32
        req.bufAddr_u32 = (uint32_t)(&u32Data[0]);
        req.accessType_enu = R_FDL_ACCESS_USER;
        R_FDL_Execute(&req);
        while (R_FDL_BUSY == req.status_enu)
        {
            R_FDL_Handler();
            WDT_Clear();
        }
        return req.status_enu;
    }
    return R_FDL_ERR_ECC_DED;
}