九、nrf52832的NVIC(非易失存储器flash)

NVMC
1.写flash之前必须配置写寄存器CONFIG.EEN=1对flash的扇区进行擦除,否则无法进行写操作(全部置1,页操作)
2.配置写寄存器CONFIG.WEN=1 对flash进行写使能。(只能由1写为0)
3.flash写入时都是以一个字(32位)对齐到地址上。
4.nrf52832具有512kbyte的flash,可分为128页,每页4kbyte;每页分为8个数据块,每块512byte;每个块有128个字。
5.写入数据时,cpu会被挂起。
NVMC常用函数
1.flash擦除页函数:nrf_nvmc_page_erase(uint32_t addr)
2.向flash写字函数:nrf_nvmc_write_word(uint32_t addr,uint32_t value)
flash的读写示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
/* 包含log头文件 */
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/* 包含uart头文件 */
#include "app_uart.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"							//带easydma的串口
#endif

#include "nrf_nvmc.h"

/* 定义uart收接发缓冲区 */
#define 	UART_TX_BUF_SIZE		256
#define		UART_RX_BUF_SIZE		256

/**
 * log初始化函数
 */
static void log_init(void)
{
	ret_code_t err = NRF_LOG_INIT(NULL);		//log初始化
	APP_ERROR_CHECK(err);						//检测log配置信息
	NRF_LOG_DEFAULT_BACKENDS_INIT();			//开启log
}

/**
 * uart事件回调函数
 */
void uart_event_handle(app_uart_evt_t* event)
{
	//uart通讯出错事件
	if(event->evt_type == APP_UART_COMMUNICATION_ERROR)
	{
		APP_ERROR_HANDLER(event->data.error_communication);
	}
	//fifo错误事件
	else if(event->evt_type == APP_UART_FIFO_ERROR)
	{
		APP_ERROR_HANDLER(event->data.error_code);
	}
}

/**
 * uart初始化参数配置函数
 */
void uart_config(void)
{
	uint32_t err;
	/* 串口结构体参数 */
	const app_uart_comm_params_t comm_params = {
		RX_PIN_NUMBER,									//接收引脚p0.08
		TX_PIN_NUMBER,									//发送引脚p0.06
		RTS_PIN_NUMBER,									//输出信号引脚p0.05
		CTS_PIN_NUMBER,									//输入信号引脚p0.07
		APP_UART_FLOW_CONTROL_DISABLED,					//使能软件控制流
		false,											//不校验
		NRF_UART_BAUDRATE_115200						//串口波特率115200
	};
	/* 串口初始化 */
	APP_UART_FIFO_INIT(&comm_params,					//串口结构体
						UART_RX_BUF_SIZE,				//串口接收缓冲区大小
						UART_TX_BUF_SIZE,				//串口发送缓冲区大小
						uart_event_handle,				//串口事件回调函数
						APP_IRQ_PRIORITY_LOWEST,		//串口中断优先级最低
						err);							//配置信息
	APP_ERROR_CHECK(err);								//检测配置是否成功
}

/**
 * uart初始化函数
 */

int main(void)
{	
	uint32_t addr;
	uint32_t* pdata;

	log_init();
	uart_config();

	NRF_LOG_INFO("uart example start");
	NRF_LOG_FLUSH();

	addr = 0x0007F000;											//最后一页的首地址,由于程序存储在flash中,防止出错
	nrf_nvmc_page_erase(addr);									//页擦除
	nrf_nvmc_write_word(addr,0x12345678);						//写入一个字数据
	pdata = (uint32_t *)addr;									//指针指向字所在地址

	while(1)
	{
		printf("data is 0x%x\r\n",*pdata);
		nrf_delay_ms(1000);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值