Cortex-M单片机下载数据到外部flash
来源:互联网 发布:淘宝怎样付款 编辑:程序博客网 时间:2024/06/06 05:40
主机环境:Windows 10 64bit
开发环境:MDK 5.23
目标板:LPC54114
之前在NXP社区得到一块LPC54114的板卡,是由万利制作的,LPC54114是一款双核MCU,包含了一颗M4主核和一颗M0+从核,利用该板卡学习了一下双核MCU的开发流程,板卡中提供了一颗W25P80FLASH,之前拥有的板卡都没有挂外部FLASH,刚好之前利用LPC54608学习TouchGFX时可以把图片资源下载到外部FLASH中,因此借此机会来学习一下如何把数据下载到外部FLASH。
平时烧录代码我们都是烧录到MCU内部的FLASH中,如下图:
可以看到下载的时片上256KB的FLASH,该算法文件是由DFP包提供的,具体可以在keil5的安装目录下找到,如下:
加载到内部FLASH的算法有了,但加载到外部FLASH的算法还没得。keil5中提供了LPC54608板卡的外部FLASH算法,LPC54114的外部FLASH算法就需要我们自己实现了,而且我们的板卡不是NXP官方的板卡,更不可能提供外部FLASH的加载算法了。。。
进入到keil的FLASH目录,如下:
拷贝LPC5460x_MT25QL128文件夹到LPC54114_W25P80文件夹,并把其中有关信息全部替换成LPC54114相关信息。keil中提供了FlashOS.h头文件,定义了烧录到外部FLASH的相关接口,很简单,如下:
这就是本次我们需要实现的相关接口,修改的关键文件只有两个:FlashDev.c以及FlashPrg.c,首先看FlashDev.c文件,该文件定义了外部Flash的相关信息,经过简单修改就可以使用了,如下:
#ifdef W25P80struct FlashDevice const FlashDevice = { FLASH_DRV_VERS, // Driver Version, do not modify! "LPC54114 W25P80 SPI", // Device Name EXTSPI, // Device Type 0x10000000, // Device Start Address 0x00100000, // Device Size (1MB) 256, // Programming Page Size 0, // Reserved, must be 0 0xFF, // Initial Content of Erased Memory 300, // Program Page Timeout 300 mSec 3000, // Erase Sector Timeout 3000 mSec// Specify Size and Address of Sectors 0x010000, 0x000000, // Sector Size 64kB (16 Sectors) SECTOR_END};#endif我们只需要修改设备名字、设备类型、起始地址、大小、页大小、扇区大小等等,这些信息都可以在W25P80的手册中找到。其中设备的起始地址可以随便定义,但不要和内部FLASH地址冲突即可。这里定义的是0x10000000.FlashPrg.c文件是接口的实现文件,这里我把LPC54114SDK中的polling_flash下的驱动文件拿来直接使用了,即mx25r_flash.c/h文件,只需要在FlashPrg.c中简单调用即可,如下:
/* ----------------------------------------------------------------------------- * Copyright (c) 2016 ARM Ltd. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from * the use of this software. Permission is granted to anyone to use this * software for any purpose, including commercial applications, and to alter * it and redistribute it freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software in * a product, an acknowledgment in the product documentation would be * appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * * 3. This notice may not be removed or altered from any source distribution. * * * $Date: 29. August 2017 * $Revision: V1.0.0 * * Project: Flash Device Algorithm for * NXP LPC54114 W25P80 SPI Flash * --------------------------------------------------------------------------- */#include "..\FlashOS.H" // FlashOS Structures#include "fsl_iocon.h"#include "fsl_spi.h"#include "mx25r_flash.h"#define SECTOR_ADDR 0#define SECTOR_SIZE 65536#define FLASH_SPI_SSEL 3#define EXAMPLE_SPI_MASTER SPI2#define EXAMPLE_SPI_MASTER_CLK_SRC kCLOCK_Flexcomm2#define EXAMPLE_SPI_MASTER_CLK_FREQ 12000000unsigned long base_adr;unsigned char chk_buf[256];struct mx25r_instance mx25r;void SPI_InitPins(void) { CLOCK_EnableClock(kCLOCK_Iocon); /* enable clock for IOCON */ /* SPI pins */ IOCON_PinMuxSet(IOCON, 0, 8, (IOCON_FUNC1 | IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)); /* SPI_MOSI */ IOCON_PinMuxSet(IOCON, 0, 9, (IOCON_FUNC1 | IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)); /* SPI_MISO */ IOCON_PinMuxSet(IOCON, 0, 10, (IOCON_FUNC1 | IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)); /* SPI_SCK */ IOCON_PinMuxSet(IOCON, 0, 2, (IOCON_FUNC2 | IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)); /* SPI_CSN */}int flash_transfer_cb(void *transfer_prv, uint8_t *tx_data, uint8_t *rx_data, size_t dataSize, bool eof){ spi_transfer_t xfer = {0}; xfer.txData = tx_data; xfer.rxData = rx_data; xfer.dataSize = dataSize; /* terminate frame */ if (eof) { xfer.configFlags |= kSPI_FrameAssert; } SPI_MasterTransferBlocking((SPI_Type *)transfer_prv, &xfer); return 0;}int flash_init(void){ spi_master_config_t masterConfig = {0}; SPI_MasterGetDefaultConfig(&masterConfig); masterConfig.direction = kSPI_MsbFirst; masterConfig.polarity = kSPI_ClockPolarityActiveHigh; masterConfig.phase = kSPI_ClockPhaseFirstEdge; masterConfig.baudRate_Bps = 1000000; masterConfig.sselNum = (spi_ssel_t)FLASH_SPI_SSEL; SPI_MasterInit(EXAMPLE_SPI_MASTER, &masterConfig, EXAMPLE_SPI_MASTER_CLK_FREQ); mx25r_init(&mx25r, flash_transfer_cb, EXAMPLE_SPI_MASTER); return mx25r_err_ok;}/* * Initialize Flash Programming Functions * Parameter: adr: Device Base Address * clk: Clock Frequency (Hz) * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { SYSCON->FXCOMCLKSEL[2] = 0; /* reset FLEXCOMM for SPI */ SYSCON->PRESETCTRLSET[1] = (uint32_t)(1<<13); SYSCON->PRESETCTRLCLR[1] = (uint32_t)(1<<13); SPI_InitPins(); flash_init(); base_adr = adr; return (0);}/* * De-Initialize Flash Programming Functions * Parameter: fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify) * Return Value: 0 - OK, 1 - Failed */int UnInit (unsigned long fnc) { return (0);}/* * Erase complete Flash Memory * Return Value: 0 - OK, 1 - Failed */int EraseChip (void) { mx25r_cmd_chip_erase(&mx25r); return (0); /* Finished without Errors */}/* * Erase Sector in Flash Memory * Parameter: adr: Sector Address * Return Value: 0 - OK, 1 - Failed */int EraseSector (unsigned long adr) { mx25r_cmd_sector_erase(&mx25r,adr-base_adr); return (0); /* Finished without Errors */}/* * Blank Check Checks if Memory is Blank * Parameter: adr: Block Start Address * sz: Block Size (in bytes) * pat: Block Pattern * Return Value: 0 - OK, 1 - Failed */int BlankCheck (unsigned long adr, unsigned long sz, unsigned char pat) { return (1); /* Always Force Erase */}/* * Program Page in Flash Memory * Parameter: adr: Page Start Address * sz: Page Size * buf: Page Data * Return Value: 0 - OK, 1 - Failed */int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) { mx25r_cmd_write(&mx25r,adr-base_adr,buf,sz); return (0); /* Finished without Errors */}/* * Verify Flash Contents * Parameter: adr: Start Address * sz: Size (in bytes) * buf: Data * Return Value: (adr+sz) - OK, Failed Address */unsigned long Verify (unsigned long adr, unsigned long sz, unsigned char *buf){ int i = 0; mx25r_cmd_read(&mx25r,adr-base_adr,chk_buf,sz); for(i = 0; i < sz; i++) { if(buf[i] != chk_buf[i]) { return (adr+i);} } return (adr+sz);}编译该工程,即可产生我们所需要的加载FLASH的算法文件,如下:
下面就来测试下该算法文件是否起作用,在LPC54114的SDK中的polling_flash工程中加入一段测试数据,如下:
const uint8_t extFlash[] __attribute__ ((at(0x10000000))) = {'N','X','P','I','C'};把这几个字符存放到外部FLASH的0地址上,这里0x10000000是我们刚才定义的外部FLASH的起始地址。此外还需要添加一下该算法文件,如下:
下载该代码,本以为就此成功了,结果弹出一个框框,说无法加载FLASH编程算法,
MDK中的输出信息如下:Insufficient RAM for Flash Algorithms !,百度搜索了一下说是为该算法分配的RAM空间小了,默认的是0x1000,4KB大小,这里加大了已被改成了8KB,如下:
再次下载,很顺畅地下载成功了,赶紧打开串口助手查看测试结果:
成功地从外部FLASH读取到了我们定义的‘NXPIC’字符,测试成功!大家一起来测试一下吧!
工程文件:http://download.csdn.net/download/key123zhangxing/9956278
- Cortex-M单片机下载数据到外部flash
- 大端小端以及51单片机,CORTEX M 32位单片机中的归属
- ST下载提示No Cortex-M SW Device Found
- 下载出错Error:Flash Download Failed-"Cortex-M3"
- ARM® Cortex®-M内核单片机STM32家族介绍,覆盖STM32F、STM32H、STM32L全系列
- 单片机外部RAM的数据传送
- Cortex-M3 单片机内核
- Cortex-M 系列
- Cortex-M入门
- Cortex-M处理器
- 下载Microblaze程序到Flash
- Flash读取外部数据之TXT文本
- 2.7 将程序下载到单片机
- STM32用j-link下载程序出现NO Cortex-M Device found in JTAG chain问题
- 更换STM32芯片类型引起keil下载Error Flash download failed-Cortex-M3问题的解决
- Outlook下载邮件到外部文件
- ARM Cortex-M(14)
- ARM Cortex-M Series Processors
- opencv中的标准霍夫线变换HoughLines()的-学习笔记
- Python多进程和多线程
- 每次新版本build时,让软件的版本号自增
- 返回n个随机生成的数组
- C++STL一般总结
- Cortex-M单片机下载数据到外部flash
- element UI table表格组件使用[超级完整功能]
- 20170829WindowsPrj02_02_GDI
- 第五章:数组
- 目标检测的图像特征提取之(一)HOG特征
- Python的多继承
- 1071: 骑马修栅栏
- Java字符串学习总结
- poj 1734(最小环+路径)