Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
I2C_IIC
RTOS IIC总线使用
发布于 2016-01-09 15:17:35 浏览:4731
订阅该版
版主,给我发个RT_THREAD IIC总线使用历程, 在写底层蒙了... 每个IIC器件都需要写以下接口函数吗 ``` static rt_size_t sd2405al_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { struct rt_i2c_msg msgs[2]; rt_uint8_t buff[5]; buff[0]= (rt_uint8_t)pos; rt_memcpy(&buff[1],buffer,size); msgs[0].addr = sd2405al_device.i2c_addr; msgs[0].flags = RT_I2C_WR; msgs[0].buf = (rt_uint8_t *)buff; msgs[0].len = 1+size; if (rt_i2c_transfer(sd2405al_device.i2c_bus, msgs, 1) == 1) return size; SD2404AL_TRACE("rt_i2c_transfer write error "); return RT_ERROR; } ``` 还是有统一的IIC接口函数
查看更多
4
个回答
默认排序
按发布时间排序
zhuwenqiang
2016-01-09
这家伙很懒,什么也没写!
没有使用明白 ,版主帮我看看。。。。 ``` #include "rda_5820.h" #include
#include
#define FM_DEBUG #ifdef FM_DEBUG #define FM_TRACE rt_kprintf #else #define FM_TRACE(...) #endif /* #ifdef FLASH_DEBUG */ struct i2c_rda_device rad_5820_device; /* RT-Thread device interface */ static rt_err_t rad_5820_init(rt_device_t dev) { return RT_EOK; } static rt_err_t rad_5820_open(rt_device_t dev, rt_uint16_t oflag) { return RT_EOK; } static rt_err_t rad_5820_close(rt_device_t dev) { return RT_EOK; } void RDA5820_Freq_Set(rt_uint16_t freq); void RDA5820_Vol_Set(rt_uint8_t vol); static rt_err_t rad_5820_control(rt_device_t dev, rt_uint8_t cmd, void *args) { switch (cmd) { case FM_FREQ: /* ?è?????? */ RDA5820_Freq_Set(*(rt_uint16_t*)args); break; case FM_VOLUME: /* ?è?????? */ RDA5820_Vol_Set(*(rt_uint8_t*)args); break; default: break; } return RT_EOK; } static rt_size_t rad_5820_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { struct rt_i2c_msg msgs[2]; msgs[0].addr = rad_5820_device.i2c_addr; msgs[0].flags = RT_I2C_WR; msgs[0].buf = (rt_uint8_t *)&pos; msgs[0].len = 1; msgs[1].addr = rad_5820_device.i2c_addr; msgs[1].flags = RT_I2C_RD; /* Read from slave */ msgs[1].buf = (rt_uint8_t *)buffer; msgs[1].len = size; if (rt_i2c_transfer(rad_5820_device.i2c_bus, msgs, 2) == 2) return size; FM_TRACE("rt_i2c_transfer read error "); return RT_ERROR; } static rt_size_t rad_5820_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { struct rt_i2c_msg msgs[2]; rt_uint8_t buff[5]; buff[0]= (rt_uint8_t)pos; rt_memcpy(&buff[1],buffer,size); msgs[0].addr = rad_5820_device.i2c_addr; msgs[0].flags = RT_I2C_WR; msgs[0].buf = (rt_uint8_t *)buff; msgs[0].len = 1+size; if (rt_i2c_transfer(rad_5820_device.i2c_bus, msgs, 1) == 1) return size; FM_TRACE("rt_i2c_transfer write error "); return RT_ERROR; } /********************************* RT-Thread rt_device interface end **************************************/ rt_uint16_t RDA5820_RD_Reg(rt_uint8_t addr) { rt_uint8_t buff[5]; rt_uint16_t val=0; rad_5820_read(0,addr,buff,2); val|=buff[0]; val<<=8; val|=buff[1]; return val; } void RDA5820_WR_Reg(rt_uint8_t addr,rt_uint16_t val) { rt_uint8_t buff[5]; buff[0]=val>>8; buff[1]=val&0XFF; rad_5820_write(0,addr,buff,2); } //?è??RDA5820??RX???? void RDA5820_RX_Mode(void) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X40); //????0X40?????? temp&=0xfff0; //RX ???? RDA5820_WR_Reg(0X40,temp) ; //FM RX???? } //?è??RDA5820??TX???? void RDA5820_TX_Mode(void) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X40); //????0X40?????? temp&=0xfff0; temp|=0x0001; //TX ???? RDA5820_WR_Reg(0X40,temp) ; //FM TM ???? } //???????????? //·?????·??§:0~127 rt_uint8_t RDA5820_Rssi_Get(void) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X0B); //????0X0B?????? return temp>>9; //·??????????? } //?è??????ok //vol:0~15; void RDA5820_Vol_Set(rt_uint8_t vol) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X05); //????0X05?????? temp&=0XFFF0; temp|=vol&0X0F; RDA5820_WR_Reg(0X05,temp) ; //?è?????? } //?????è?? //mute:0,??????;1,???? void RDA5820_Mute_Set(rt_uint8_t mute) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X02); //????0X02?????? if(mute)temp|=1<<14; else temp&=~(1<<14); RDA5820_WR_Reg(0X02,temp) ; //?è??MUTE } //?è???é???? //rssi:0~127; void RDA5820_Rssi_Set(rt_uint8_t rssi) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X05); //????0X05?????? temp&=0X80FF; temp|=(rt_uint16_t)rssi<<8; RDA5820_WR_Reg(0X05,temp) ; //?è??RSSI } //?è??TX·??????? //gain:0~63 void RDA5820_TxPAG_Set(rt_uint8_t gain) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X42); //????0X42?????? temp&=0XFFC0; temp|=gain; //GAIN RDA5820_WR_Reg(0X42,temp) ; //?è??PA?????? RDA5820_WR_Reg(0X42,temp) ; //?è??PA?????? } //?è??TX ???????????? //gain:0~7 void RDA5820_TxPGA_Set(rt_uint8_t gain) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X42); //????0X42?????? temp&=0XF8FF; temp|=gain<<8; //GAIN RDA5820_WR_Reg(0X42,temp) ; //?è??PGA RDA5820_WR_Reg(0X42,temp) ; //?è??PA?????? } //?è??RDA5820???¤×÷???? //band:0,87~108Mhz;1,76~91Mhz;2,76~108Mhz;3,???§×??¨??(53H~54H) void RDA5820_Band_Set(rt_uint8_t band) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X03); //????0X03?????? temp&=0XFFF3; temp|=band<<2; RDA5820_WR_Reg(0X03,temp) ; //?è??BAND } //?è??RDA5820?????????? //band:0,100Khz;1,200Khz;3,50Khz;3,±??? void RDA5820_Space_Set(rt_uint8_t spc) { rt_uint16_t temp; temp=RDA5820_RD_Reg(0X03); //????0X03?????? temp&=0XFFFC; temp|=spc; RDA5820_WR_Reg(0X03,temp) ; //?è??BAND } //?è??RDA5820?????? //freq:??????(??????10Khz),±???10805,±í??108.05Mhz void RDA5820_Freq_Set(rt_uint16_t freq) { rt_uint16_t temp; rt_uint8_t spc=0,band=0; rt_uint16_t fbtm,chan; temp=RDA5820_RD_Reg(0X03); //????0X03?????? temp&=0X001F; band=(temp>>2)&0x03; //???????? spc=temp&0x03; //????·?±??? if(spc==0)spc=10; else if(spc==1)spc=20; else spc=5; if(band==0)fbtm=8700; else if(band==1||band==2)fbtm=7600; else { fbtm=RDA5820_RD_Reg(0X53);//????bottom???? fbtm*=10; } if(freq
>6; band=(temp>>2)&0x03; //???????? spc=temp&0x03; //????·?±??? if(spc==0)spc=10; else if(spc==1)spc=20; else spc=5; if(band==0)fbtm=8700; else if(band==1||band==2)fbtm=7600; else { fbtm=RDA5820_RD_Reg(0X53);//????bottom???? fbtm*=10; } temp=fbtm+chan*spc; return temp;//·????????? } rt_err_t rt_hw_rad_5820_init(const char * i2c_device_name) { rt_uint16_t ID; rt_memset(&rad_5820_device, 0, sizeof(struct i2c_rda_device)); rad_5820_device.i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(i2c_device_name); if (rad_5820_device.i2c_bus == NULL) { FM_TRACE("I2C device %s not found! ", i2c_device_name); return -RT_ENOSYS; } rad_5820_device.i2c_addr = 0X11; //×??áI2C?÷?????· ID=RDA5820_RD_Reg(RDA5820_R00); if(ID!=0x5805) { FM_TRACE("rad_5820_device ID=%04x ",ID); return -RT_ENOSYS; } FM_TRACE("I2C FM device ID=%04x ", ID); { RDA5820_WR_Reg(RDA5820_R02,0x0002); //?í???? rt_thread_delay(50); RDA5820_WR_Reg(RDA5820_R02,0xC001); //?????ù,???? rt_thread_delay(60); RDA5820_WR_Reg(RDA5820_R05,0X884F); //???÷????8,LNAN,1.8mA,VOL×??ó RDA5820_WR_Reg(0X07,0X7800); // RDA5820_WR_Reg(0X13,0X0008); // RDA5820_WR_Reg(0X15,0x1420); //VCO?è?? 0x17A0/0x1420 RDA5820_WR_Reg(0X16,0XC000); // RDA5820_WR_Reg(0X1C,0X3126); // RDA5820_WR_Reg(0X22,0X9C24); //fm_true RDA5820_WR_Reg(0X47,0XF660) ; //tx rds } RDA5820_Band_Set(0); //?è????????87~108Mhz RDA5820_Space_Set(0); //?è????????100Khz RDA5820_TxPGA_Set(3); //?????????è????3 RDA5820_TxPAG_Set(63);//·?????????×??ó. RDA5820_RX_Mode(); //?è???????????? RDA5820_Freq_Set(9360); RDA5820_Vol_Set(2); { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE); //????PB?????±?? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//PB.7 ???ì???? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOD, &GPIO_InitStructure); //PD.7 ???ì???? GPIO_SetBits(GPIOD,GPIO_Pin_7); //PB.5 ?????? GPIO_ResetBits(GPIOB,GPIO_Pin_7); //PB.5 ?????? } /* register device */ rad_5820_device.device.type = RT_Device_Class_Char; rad_5820_device.device.init = rad_5820_init; rad_5820_device.device.open = rad_5820_open; rad_5820_device.device.close = rad_5820_close; rad_5820_device.device.read = rad_5820_read; rad_5820_device.device.write = rad_5820_write; rad_5820_device.device.control = rad_5820_control; rad_5820_device.device.user_data = RT_NULL; rt_device_register(&rad_5820_device.device, "FM", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return RT_EOK; } #include
FINSH_FUNCTION_EXPORT(RDA5820_Vol_Set, vol=0-15) FINSH_FUNCTION_EXPORT(RDA5820_Freq_Set, Freq=108.05MHZ=10805) ```
zhuwenqiang
2016-01-09
这家伙很懒,什么也没写!
版主帮我看看
aozima
2016-01-09
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
>版主帮我看看 --- 是否封装成一个device取决于需求。
撰写答案
登录
注册新账号
关注者
0
被浏览
4.7k
关于作者
zhuwenqiang
这家伙很懒,什么也没写!
提问
9
回答
14
被采纳
0
关注TA
发私信
相关问题
1
NXP的I2C应该比ST的好用吧
2
Use of I2C device driver
3
关于I2C 驱动问题请教
4
我如何知道这个iic的io配置和我电路设计的是一致的?
5
I2C模拟读操作失败,不知道问什么进不去读函数
6
RTT的I2C有官方文档资料没有
7
求 STM32F103 IIC 自定义IO初始化 代码
8
报一个LPC4008代码中I2C的bug
9
关于在RTT中使用STM32 I2C的疑问
10
裸机跑调外设ic程序已经调好,rt-thread系统 在stm32f407如何实现i2c外设
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
2
RT-Thread 发布 EtherKit开源以太网硬件!
3
rt-thread使用cherryusb实现虚拟串口
4
《C++20 图形界面程序:速度与渲染效率的双重优化秘籍》
5
《原子操作:程序世界里的“最小魔法单位”解析》
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
7
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
3
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部