I2C

RTOS IIC总线使用

发布于 2016-01-09 15:17:35
版主,给我发个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接口函数

查看更多

关注者
0
被浏览
3.5k
4 个回答
zhuwenqiang
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 chan=(freq-fbtm)/spc; //????CHAN????????????
chan&=0X3FF; //????10??
temp|=chan<<6;
temp|=1<<4; //TONE ENABLE
RDA5820_WR_Reg(0X03,temp) ; //?è??????
rt_thread_delay(2); //????20ms
while((RDA5820_RD_Reg(0X0B)&(1<<7))==0);//????FM_READY

}
//?????±?°????
//·?????:??????(????10Khz)
rt_uint16_t RDA5820_Freq_Get(void)
{
rt_uint16_t temp;
rt_uint8_t spc=0,band=0;
rt_uint16_t fbtm,chan;
temp=RDA5820_RD_Reg(0X03); //????0X03??????
chan=temp>>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)
aozima
aozima 2016-01-09
拒绝白嫖,拒绝键盘侠!
版主帮我看看

是否封装成一个device取决于需求。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友