一 简介:
nrf52840 是个ble soc,支持多种协议ble、zigbee、thread、mesh,外设接口也很丰富,主要uartx2,spix1,i2cx2,usb等。以下是基于i2c编写eeprom和rtc的驱动,如下:
后续更新相关协议dfu\ota方法:
- ble dfu\ota的三种方式 (usb、uart 、ble)
- zigbe dfu\ota 的三种方式( usb、uart、ble、zigbee)
- mesh dfu\ota的两种方式 (ble、mesh)
- …
二 代码 :
1. eeprom、rtc驱动源码src
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf_drv_twi.h"
#include "ft24c64_am1805.h"
#include "process.h"
#include "param.h"
#define TWI_INSTANCE_ID 1
static volatile bool m_xfer_done = false;
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
/*************************************************************************
* 参 数 : WriteAddr[in]:地址
* : dat[in]:写入的数据
* 返回值 : NRF_SUCCESS:写数据成功
*************************************************************************/
ret_code_t FT24C64A_write_byte(uint16_t WriteAddr,uint8_t dat)
{
ret_code_t err_code;
uint8_t tx_buf[FT24C64A_ADDRESS_LEN+1];
if (WriteAddr > FT24C64A_ENDADDR)
return NRF_ERROR_INVALID_ADDR;
tx_buf[0] = (WriteAddr & 0xFF00)>>8;//WriteAddr;
tx_buf[1] = WriteAddr &0xFF;//dat;
tx_buf[2] = dat;
m_xfer_done = false;
err_code = nrf_drv_twi_tx(&m_twi, FT24C64A_ADDRESS, tx_buf, FT24C64A_ADDRESS_LEN+1, false);
if(err_code)
iic_error_flg = 1;
while (m_xfer_done == false);
FT24C64A_DELAY;
return err_code;
}
/*************************************************************************
* 参 数 : page[in]:页面,1~256
* : pdata[in]:指向待写入的数据缓存
* : size[in]:写入的数据长度,不能超过1个页面的大小32个字节
* 返回值 : NRF_SUCCESS:写页面成功
*************************************************************************/
ret_code_t FT24C64A_write_page(uint16_t page,uint8_t const * pdata,uint8_t size)
{
ret_code_t err_code;
uint8_t tx_buf[FT24C64A_ADDRESS_LEN+FT24C64A_PAGESIZE];
if (size > FT24C64A_PAGESIZE)
return NRF_ERROR_INVALID_LENGTH;
if ((page == 0)||(page > FT24C64A_PAGENUM))
return NRF_ERROR_INVALID_ADDR;;
tx_buf[0] = ((page-1)*FT24C64A_PAGESIZE & 0xff00)>>8;
tx_buf[1] = ((page-1)*FT24C64A_PAGESIZE )& 0xff;
memcpy(tx_buf+FT24C64A_ADDRESS_LEN, pdata, size);
m_xfer_done = false;
err_code = nrf_drv_twi_tx(&m_twi, FT24C64A_ADDRESS, tx_buf, size+FT24C64A_ADDRESS_LEN, false);
if(err_code)
iic_error_flg =