单片机GD32F303RCT6 (Macos环境)开发 (十五)—— i2c1采用DMA方式的读写函数

news/2024/12/2 18:40:04/

i2c1采用DMA方式的读写函数

1、关于i2c1的DMA的映射如图
在这里插入图片描述
2、关于代码的宏定义配置
Application目录的Makefile中 ENABLE_I2C_TEST = yes才会编译I2C1的相关代码。
同时修改i2c.h文件,定义I2C1_MODE为I2C1_MODE_DMA,这样i2c1的配置为dma模式。

#define I2C1_MODE                   I2C1_MODE_DMA

3、I2C相关配置,外部调用函数,供main.c初始化i2c1时使用。
代码如下:

void i2c1_dma_init(void)
{/* enable GPIOB clock */rcu_periph_clock_enable(RCU_GPIOB);rcu_periph_clock_enable(RCU_DMA0);/* enable I2C1 clock */rcu_periph_clock_enable(RCU_I2C1);/* connect PB10 to I2C1_SCL *//* connect PB11 to I2C2_SDA */gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11);//配置PB10,PB11为复用功能/* configure I2C clock */i2c_clock_config(I2C1, I2C1_SPEED, I2C_DTCY_2);/* configure I2C address */i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0xff);/* enable I2C1 */i2c_enable(I2C1);/* enable acknowledge */i2c_ack_config(I2C1, I2C_ACK_ENABLE);
}

4、DMA发送函数,可发送多字节寄存器+数据格式。

static void i2c1_dma_tx_config(uint8_t *p_data,uint8_t len)
{dma_parameter_struct dma_init_struct;/* initialize DMA channel3 */dma_deinit(DMA0, DMA_CH3);dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;dma_init_struct.memory_addr = (uint32_t)p_data;dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;dma_init_struct.number = len;dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C1);dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;dma_init(DMA0, DMA_CH3, &dma_init_struct);
}static void i2c1_dma_tx_data(uint8_t slave_addr,uint8_t *p_data,uint8_t len)
{/* wait until I2C bus is idle */while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));/* send a start condition to I2C bus */i2c_start_on_bus(I2C1);/* wait until SBSEND bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));/* send slave address to I2C bus */i2c_master_addressing(I2C1, slave_addr, I2C_TRANSMITTER);/* wait until ADDSEND bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));/* clear the ADDSEND bit */i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);/* wait until the transmit data buffer is empty */while( SET != i2c_flag_get(I2C1, I2C_FLAG_TBE));i2c1_dma_tx_config(p_data,len);/* enable I2C1 DMA */i2c_dma_config(I2C1, I2C_DMA_ON);/* enable DMA0 channel3 */dma_channel_enable(DMA0, DMA_CH3);/* DMA0 channel3 full transfer finish flag */while(!dma_flag_get(DMA0, DMA_CH3, DMA_FLAG_FTF));/* send a stop condition to I2C bus */i2c_stop_on_bus(I2C1);/* wait until the stop condition is finished */while(I2C_CTL0(I2C1)&0x0200);  
}
/*** 封装一个函数按寄存器写的函数,寄存器地址可以有多位。* 数据可以有多个* dma方式
*/
void i2c1_dma_send_data(uint8_t slave_addr,uint8_t *reg_addr,uint16_t addr_len,uint8_t *p_data,uint8_t data_len)
{uint8_t data[I2C_TX_RX_DATA_LEN_MAX] = {0};uint8_t *p;if ((addr_len + data_len) > I2C_TX_RX_DATA_LEN_MAX){perror("data to long and return\r\n");return ;}p = data;memcpy(p,reg_addr,addr_len);p += addr_len;memcpy(p,p_data,data_len);i2c1_dma_tx_data(slave_addr,data,(addr_len+data_len));}	

5、DMA接收函数封装,注意读取一个自己数据的时候,不能采用dma的方式,在函数中做了判断,一个字节的读取继续用poll方式。

uint8_t i2c1_buff_rx[128];static void i2c1_dma_rx_config(uint8_t *p_data,uint8_t len)
{dma_parameter_struct dma_init_struct;/* initialize DMA channel4 */dma_deinit(DMA0, DMA_CH4);dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;dma_init_struct.memory_addr = (uint32_t)p_data;dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;dma_init_struct.number = len;dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2C1);dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;dma_init(DMA0, DMA_CH4, &dma_init_struct);
}void i2c1_dma_receive_data(uint8_t slave_addr, uint8_t *reg_addr,uint16_t addr_len,  uint8_t* p_buffer, uint16_t number_of_byte)
{  /* wait until I2C bus is idle */while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));/* send a start condition to I2C bus */i2c_start_on_bus(I2C1);/* wait until SBSEND bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));/* send slave address to I2C bus */i2c_master_addressing(I2C1, slave_addr, I2C_TRANSMITTER);/* wait until ADDSEND bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));/* clear the ADDSEND bit */i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);/* wait until the transmit data buffer is empty */while(SET != i2c_flag_get( I2C1 , I2C_FLAG_TBE));/* enable I2C1*/i2c_enable(I2C1);/* send the EEPROM's internal address to write to */while (addr_len){i2c_data_transmit(I2C1, *reg_addr++); addr_len--; /* wait until BTC bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));}/* send a start condition to I2C bus */i2c_start_on_bus(I2C1);/* wait until SBSEND bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));/* send slave address to I2C bus */i2c_master_addressing(I2C1, slave_addr, I2C_RECEIVER);/* wait until ADDSEND bit is set */while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));/* clear the ADDSEND bit */i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);if(number_of_byte < 2) {/* disable acknowledge */i2c_ack_config(I2C1, I2C_ACK_DISABLE);/* clear ADDSEND register by reading I2C_STAT0 then I2C_STAT1 register (I2C_STAT0 has already been read) */i2c_flag_get(I2C1, I2C_FLAG_ADDSEND);/* send a stop condition to I2C bus*/i2c_stop_on_bus(I2C1);/* wait for the byte to be received */while(!i2c_flag_get(I2C1, I2C_FLAG_RBNE));/* read the byte received from the EEPROM */*p_buffer = i2c_data_receive(I2C1);/* decrement the read bytes counter */number_of_byte--;     } else{i2c1_dma_rx_config(p_buffer,number_of_byte);i2c_dma_last_transfer_config(I2C1, I2C_DMALST_ON);/* enable I2C1 DMA */i2c_dma_config(I2C1, I2C_DMA_ON);/* enable DMA0 channel5 */dma_channel_enable(DMA0, DMA_CH4);/* wait until BTC bit is set */while(!dma_flag_get(DMA0, DMA_CH4, DMA_FLAG_FTF));}/* wait until the stop condition is finished */while(I2C_CTL0(I2C1)&0x0200);i2c_stop_on_bus(I2C1);/* enable acknowledge */i2c_ack_config(I2C1,I2C_ACK_ENABLE);i2c_ackpos_config(I2C1,I2C_ACKPOS_CURRENT);}

5、main.c中调用,往eeprom中写入{0x02,0x07,0x05,0x45,0x56,0x89,0xF3,然后读出来,看看是否一致。

uint8_t addr = 0x00;
unsigned char data[7] = {0x02,0x07,0x05,0x45,0x56,0x89,0xF3};
i2c1_dma_init();
i2c1_dma_send_data(0xA0,&addr,1,data,7);delay_1ms(200);
unsigned char r_data[7];
i2c1_dma_receive_data(0xA0,&addr,1,r_data,7);
print_register_value(r_data,7);

在这里插入图片描述
读出来是一样的
6、代码路径:https://gitee.com/xiaoguo-tec_0/gd32-iap-code.git


http://www.ppmy.cn/news/64227.html

相关文章

如何提取文件名称到excel

如何提取文件名称到excel&#xff1f;在市场中很多小伙伴&#xff08;例如公司行政和文员&#xff09;就会碰到这个问题&#xff0c;将一些文件的名全部提取出来然后保存到excel表格中。当你在工作中如果遇到这个问题时&#xff0c;你却还在使用最传统的方法一个一个复制粘贴进…

企业对网络安全的重视度开始降低

近日&#xff0c;英国科学技术部发布了《2023年企业网络安全合规调查报告》&#xff08; Cyber Security Breaches Survey &#xff09;&#xff0c;对英国所有企业和社会性组织目前的网络威胁态势和合规建设进行研究&#xff0c;同时也就如何提升新一代网络应用的合规性给出专…

08 FPGA—计数器与分频器的应用

1. 理论 时序逻辑电路中最基本的单元—寄存器&#xff0c;我们可以使用寄存器来做计数器。基本上关于时间的设计都离不开计数器。 计数器在数字系统中主要是对脉冲的个数进行计数&#xff0c;以实现测量、计数和控制的功能&#xff0c;同时兼有分频功能。计数器一般都是从 0 开…

互联网摸鱼日报(2023-05-10)

互联网摸鱼日报&#xff08;2023-05-10&#xff09; InfoQ 热门话题 华为云联创营 MVP 公布 | 10 位专家入选&#xff0c;用技术创新推进产业数字化 极狐(GitLab)重磅发布新产品「极狐星」&#xff0c;让研发效能看得清&#xff0c;算得准&#xff0c;成就企业精英效能管理 …

使用阿里云服务器三分钟搭建网站教程(详细图文详解)

使用阿里云服务器快速搭建网站教程&#xff0c;先为云服务器安装宝塔面板&#xff0c;然后在宝塔面板上新建站点&#xff0c;阿里云服务器网以搭建WordPress网站博客为例&#xff0c;来详细说下从阿里云服务器CPU内存配置选择、Web环境、域名解析到网站上线全流程&#xff1a; …

测试遵循的原则

测试遵循的原则 软件测试是在规定的条件下对程序进行操作&#xff0c;以发现程序错误&#xff0c;衡量软件质量&#xff0c;并对其是否能满足设计要求进行评估的过程。软件测试的基本原则有助于测试人员进行高质量的测试&#xff0c;尽早尽可能多的发现缺陷&#xff0c;并负责跟…

spring-transaction源码分析(3)Transactional事务失效原因

问题概述 在Transactional方法中使用this方式调用另一个Transactional方法时&#xff0c;拦截器无法拦截到被调用方法&#xff0c;严重时会使事务失效。 类似以下代码&#xff1a; Transactional public void insertBlogList(List<Blog> blogList) {for (Blog blog : …

MapReduce调优

MapReduce调优 MapReduce应用场景优点缺点擅长应用场景不擅长应用MapReduce优化需求与方向 文件存储格式行式存储、列式存储Sequence File优缺点Sequence File格式Sequence File 未压缩格式Sequence File 基于record压缩格式Sequence File基于block压缩格式生成Sequence File文…