当前位置: 首页 > news >正文

【NXP-MCXA153】i2c驱动移植

介绍

‌I2C总线由飞利浦公司开发,是一种串行单工通信总线,它主要用于连接微控制器和其他外围设备并在总线上的器件之间传送信息(需要指定设备地址);常见的i2c设备有EEPROM、触摸屏、各种IoT传感器、时钟模块等,通常由两根线组成:SCL和SDA,使用时需要接上拉电阻,常见的通信速率为100k和400k

移植流程

以i2c0为例

① 在board里边添加相应的外设:配置i2c0外设为复位状态、设置GPIO引脚功能

② 添加相应的Kconfig开关,用以指示相应的外设开启与关闭(本质是通过宏定义或者条件编译的方式)

③ 根据SDK_2_14_2_FRDM-MCXA153提供的i2c示例工程编写i2c总线驱动,需要实现2个关键的函数

  • lpc_i2c_xfer
  • rt_hw_i2c_init

④ 添加相应的库文件依赖:fsl_lpi2c.c、fsl_lpi2c_edma.c

开发板引脚对应关系

序号GPIOfunction
1P3_27SCL
2P3_28SDA

驱动文件

pin_mux.c

BOARD_InitPins函数里加入以下代码

#ifdef BSP_USING_I2C0													const port_pin_config_t port3_27_pin34_config = {/* Internal pull-up resistor is enabled */kPORT_PullUp,/* Low internal pull resistor value is selected. */kPORT_LowPullResistor,/* Fast slew rate is configured */kPORT_FastSlewRate,/* Passive input filter is disabled */kPORT_PassiveFilterDisable,/* Open drain output is enabled */kPORT_OpenDrainEnable,/* Low drive strength is configured */kPORT_LowDriveStrength,/* Normal drive strength is configured */kPORT_NormalDriveStrength,/* Pin is configured as LPI2C0_SCL */kPORT_MuxAlt2,/* Digital input enabled */kPORT_InputBufferEnable,/* Digital input is not inverted */kPORT_InputNormal,/* Pin Control Register fields [15:0] are not locked */kPORT_UnlockRegister};/* PORT3_27 (pin 34) is configured as LPI2C0_SCL */PORT_SetPinConfig(PORT3, 27U, &port3_27_pin34_config);const port_pin_config_t port3_28_pin33_config = {/* Internal pull-up resistor is enabled */kPORT_PullUp,/* Low internal pull resistor value is selected. */kPORT_LowPullResistor,/* Fast slew rate is configured */kPORT_FastSlewRate,/* Passive input filter is disabled */kPORT_PassiveFilterDisable,/* Open drain output is enabled */kPORT_OpenDrainEnable,/* Low drive strength is configured */kPORT_LowDriveStrength,/* Normal drive strength is configured */kPORT_NormalDriveStrength,/* Pin is configured as LPI2C0_SDA */kPORT_MuxAlt2,/* Digital input enabled */kPORT_InputBufferEnable,/* Digital input is not inverted */kPORT_InputNormal,/* Pin Control Register fields [15:0] are not locked */kPORT_UnlockRegister};/* PORT3_28 (pin 33) is configured as LPI2C0_SDA */PORT_SetPinConfig(PORT3, 28U, &port3_28_pin33_config);
#endif

board/Kconfig

加入i2c0相关配置

menuconfig BSP_USING_I2Cconfig BSP_USING_I2Cbool "Enable I2C"select RT_USING_I2Cdefault yif BSP_USING_I2Cconfig BSP_USING_I2C0bool "Enable I2C0"default yendif

drv_i2c.c

i2c驱动层修改如下

/** Copyright (c) 2006-2024, RT-Thread Development Team** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2023-08-17     hywing       The first version*/#include <rtdevice.h>
#include "fsl_lpi2c.h"
#include "fsl_lpi2c_edma.h"
#include "fsl_edma.h"#ifdef RT_USING_I2Cenum
{
#ifdef BSP_USING_I2C0I2C0_INDEX,
#endif
};#define i2c_dbg                 rt_kprintfstruct lpc_i2c_bus
{struct rt_i2c_bus_device    parent;LPI2C_Type                  *I2C;clock_attach_id_t           clock_attach_id;clock_div_name_t            clock_div_name;clock_name_t                clock_src;uint32_t                    baud;char                        *name;
};struct lpc_i2c_bus lpc_obj[] =
{
#ifdef BSP_USING_I2C0{.I2C = LPI2C0,.baud = 100000U,.clock_attach_id = kFRO12M_to_LPI2C0,.clock_div_name = kCLOCK_DivLPI2C0,.clock_src = kCLOCK_Fro12M,.name = "i2c0",},
#endif
#ifdef BSP_USING_I2C1{.I2C = LPI2C1,.baud = 100000U,.clock_attach_id = kFRO12M_to_FLEXCOMM1,.clock_div_name = kCLOCK_DivFlexcom1Clk,.clock_src = kCLOCK_Fro12M,.name = "i2c1",},
#endif
#ifdef BSP_USING_I2C2{.I2C = LPI2C2,.baud = 100000U,.clock_attach_id = kFRO12M_to_FLEXCOMM2,.clock_div_name = kCLOCK_DivFlexcom2Clk,.clock_src = kCLOCK_Fro12M,.name = "i2c2",},
#endif
};static rt_ssize_t lpc_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
{struct rt_i2c_msg *msg;lpi2c_master_transfer_t xfer = {0};rt_uint32_t i;rt_ssize_t ret = 0;struct lpc_i2c_bus *lpc_i2c = (struct lpc_i2c_bus *)bus;for (i = 0; i < num; i++){msg = &msgs[i];if (msg->flags & RT_I2C_RD){xfer.slaveAddress = msg->addr;xfer.direction = kLPI2C_Read;xfer.subaddress = 0;xfer.subaddressSize = 0;xfer.data = msg->buf;xfer.dataSize = msg->len;if(i != 0)xfer.flags = kLPI2C_TransferRepeatedStartFlag;elsexfer.flags = kLPI2C_TransferDefaultFlag;if (LPI2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success){i2c_dbg("i2c bus read failed!\n");return i;}}else{xfer.slaveAddress = msg->addr;xfer.direction = kLPI2C_Write;xfer.subaddress = 0;xfer.subaddressSize = 0;xfer.data = msg->buf;xfer.dataSize = msg->len;if(i == 0)xfer.flags = kLPI2C_TransferNoStopFlag;elsexfer.flags = kLPI2C_TransferDefaultFlag;if (LPI2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success){i2c_dbg("i2c bus write failed!\n");return i;}}}ret = i;return ret;
}static const struct rt_i2c_bus_device_ops i2c_ops =
{lpc_i2c_xfer,RT_NULL,RT_NULL
};int rt_hw_i2c_init(void)
{int i;lpi2c_master_config_t masterConfig;for(i=0; i<ARRAY_SIZE(lpc_obj); i++){CLOCK_SetClockDiv(lpc_obj[i].clock_div_name, 1u);CLOCK_AttachClk(lpc_obj[i].clock_attach_id);LPI2C_MasterGetDefaultConfig(&masterConfig);masterConfig.baudRate_Hz = lpc_obj[i].baud;LPI2C_MasterInit(lpc_obj[i].I2C, &masterConfig, /*CLOCK_GetFreq(lpc_obj[i].clock_src)*/CLOCK_GetLpi2cClkFreq());lpc_obj[i].parent.ops = &i2c_ops;rt_i2c_bus_device_register(&lpc_obj[i].parent, lpc_obj[i].name);}return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);#endif /* RT_USING_I2C */

SConscript

Libraries/MCXA153/SConscript文件里边加上以下代码

if GetDepend('BSP_USING_I2C'):src += ['MCXA153/drivers/fsl_lpi2c.c']src += ['MCXA153/drivers/fsl_lpi2c_edma.c']

测试用例

打开使能i2c0驱动

在这里插入图片描述

使能ssd1306模块并连接SCL(P3_27)、SDA(P3_28)引脚

在这里插入图片描述
拉取更新软件包,并导出MDK5工程

pkgs --update
scons --target=mdk5

在msh终端输入ssd1306_TestAll,屏幕输出效果如下,说明i2c BSP驱动已经正确移植

在这里插入图片描述

http://www.lryc.cn/news/426459.html

相关文章:

  • C++(11)类语法分析(2)
  • 数字验证每日十问--(3)
  • 22.给定 n 对括号,实现一个算法生成所有可能的正确匹配的括号组合
  • 检测到目标URL存在http host头攻击漏洞
  • C++奇迹之旅:手写vector模拟实现与你探索vector 容器的核心机制与使用技巧
  • 018、钩子函数 mounted和beforeDestroy、父组件向子组件传递参数 props 的使用
  • xlnt在Windows中的dll,lib生成
  • 【网络】私有IP和公网IP的转换——NAT技术
  • java 面试 PDF 资料整理
  • 初步认识Linux系统
  • JavaScript AI 编程助手
  • 达梦数据库的系统视图v$datafile
  • Triton/window安装: triton-2.0.0-cp310-cp310-win_amd64.whl文件
  • 应急响应-DDOS-典型案例
  • JAVA学习之知识补充(下)
  • qt生成一幅纯马赛克图像
  • python循环——九九乘法表(更加轻松的理解循环结构)
  • UDS诊断系列之十八故障码的状态掩码
  • 【jvm】直接引用
  • PythonStudio 控件使用常用方式(二十七)TActionList
  • PDF 转Word 开源库
  • Docker - 深入理解Dockerfile中的 RUN, CMD 和 ENTRYPOINT
  • Python 函数式编程 内置高阶函数及周边【进阶篇 3】推荐
  • 【Rust光年纪】探秘Rust GUI库:从安装配置到API概览
  • Element plus部分组件样式覆盖记录
  • 重塑业务生态,Vatee万腾平台:引领行业变革的新引擎
  • 标准术语和定义中的【架构】应该如何描述
  • 华为鸿蒙Core Vision Kit 骨骼检测技术
  • Table API SQL系统(内置)函数System (Built-in) Function详解
  • 一键运行RocketMQ5.3和Dashboard