TM4C123G开发板学习记录(八)存储和安全管理(中)

3 实验及代码分析

3.1 读写Flash-写数据到安全的Flash区域,然后用Debugger观察写入情况

3.1.1导入项目

TM4C123G开发板学习记录(八)存储和安全管理(中)

3.1.2 编辑源文件

打开main.c,编写代码如下

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/flash.h"               // 1) Flash操作函数声明
#include "driverlib/eeprom.h"

//MEMORY CONFIGURATION
//
//         name            origin    length      used     unused   attr    fill
//----------------------  --------  ---------  --------  --------  ----  --------
//  FLASH      00000000   00040000  000017e6  0003e81a  R  X

// 2)选择大于上面0x17e6,并且在1k的整数倍的地址
#define SAFE_ERASE_FLASH_ADDR 0x10000

// 3) 测试数据
uint32_t ui32Data[2] = {0x12345678, 0x56789abc};
uint32_t ui32Read[2];

int main(void)
{
    // 40MHz system clock
    SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

    // Enable Port F LED indicator
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0x00);
    SysCtlDelay(20000000);

    // 4) 擦除该起始地址1KFlash,观察是否0xFF
    assert(0 == FlashErase(SAFE_ERASE_FLASH_ADDR));
   // 5) 写数据,观察是否0x12345678 0x56789abc。注意字节顺序
    assert(0 == FlashProgram(ui32Data, SAFE_ERASE_FLASH_ADDR, sizeof(ui32Data)));

    // Turn on red LED to indicate Flash op finish
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0x02);
    SysCtlDelay(20000000);

    // Op on EEPROM
    SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0);
    // Performs any necessary recovery in case of power failures during write.
    EEPROMInit();
    // Erases the EEPROM and returns it to the factory default condition.
    EEPROMMassErase();
    // Read/Write data
    EEPROMRead(ui32Read, 0x0, sizeof(ui32Read));
    EEPROMProgram(ui32Data, 0x0, sizeof(ui32Data));
    EEPROMRead(ui32Read, 0x0, sizeof(ui32Read));

    // Turn on blue LED to indicate EEPROM op finish
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_2);

    while (1)
    {
    }
}

3.1.3 编译,下载,启动调试

TM4C123G开发板学习记录(八)存储和安全管理(中)

在上面注释4处设置断点,然后单步执行,并打开Memory观察窗口,输入上面SAFE_ERASE_FLASH_ADDR值0x10000

TM4C123G开发板学习记录(八)存储和安全管理(中)

执行36行代码Erase后

TM4C123G开发板学习记录(八)存储和安全管理(中)

                                                     图3.1

执行38行代码Program后

TM4C123G开发板学习记录(八)存储和安全管理(中)

                                                    图3.2

终止调试程序。

发现问题

重新启动调试,停在36行断点处,观察memory窗口0x10000,

预期图3.2内容,因为Flash是非易失内存(non-volatile)

实际图3.1内容

解决问题

首先重新调试源代码,配合手册,查看寄存器变化,都没有问题。怀疑是CCS调试窗口更新。

独立测试

使用LMFlashProgramBuild1613版本,使用Flash Utilities/Upload Flash Content功能,设置起止地址,上传实际Flash内容

TM4C123G开发板学习记录(八)存储和安全管理(中)

打开content.bin文件,内容和预期的是一致的。由于本人机器是Little E难点,所以,下图是78 56 34 12。

TM4C123G开发板学习记录(八)存储和安全管理(中)

3.2 BitBand例子分析

例子在TivaWare安装目录下,examples\boards\ek-tm4c123gxl\bitband,支持多种开发环境,CCS,Keil uVision,GCC等。

写操作示例

TM4C123G开发板学习记录(八)存储和安全管理(中)

读操作示例

TM4C123G开发板学习记录(八)存储和安全管理(中)

HWREGBITW展开

TM4C123G开发板学习记录(八)存储和安全管理(中)

HWREGBITW实现bitband到alias转换

TM4C123G开发板学习记录(八)存储和安全管理(中)

bit-band alias = bit-band base + (byte offset * 0x20) + (bit number * 4)

BitBand
开始 结束 描述
0x2000.0000 0x2000.7FFF SRAM bit-band region
0x2200.0000 0x220F.FFFF SRAM bit-band alias
0x4000.0000 0x400F.FFFF Peripheral bit-band region
0x4200.0000 0x43FF.FFFF Peripheral bit-band alia