zhuwenqiang
zhuwenqiang
This guy hasn't written anything yet

注册于 6 years ago

回答
0
文章
0
关注者
0

没有使用明白 ,版主帮我看看。。。。


#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)

这个结构的时间戳

struct stat
{
rt_device_t st_dev;
rt_uint16_t st_mode;
rt_uint32_t st_size;
rt_time_t st_mtime;
rt_uint32_t st_blksize;
};


是创建的时间 还是修改时间

更新下驱动 在底层创建一个线程接收数据,大家给点意见,


#include
#include "nrf24l01.h"

#define NRF_DEBUG

#ifdef NRF_DEBUG
#define NRF_TRACE rt_kprintf
#else
#define NRF_TRACE(...)
#endif /* #ifdef FLASH_DEBUG */

static struct spi_nrf_device nrf24l01_device;

/* write nrf buf */
static rt_uint8_t NRF24L01_Write_Buf(rt_uint8_t reg, const void *buffer, rt_size_t size)
{
return rt_spi_send_then_send(nrf24l01_device.rt_spi_device,&reg,1,buffer,size);
}

/* read nrf buf */
static rt_uint8_t NRF24L01_Read_Buf(rt_uint8_t reg,void *buffer,rt_size_t size)
{
return rt_spi_send_then_recv(nrf24l01_device.rt_spi_device,&reg,1,buffer,size);
}
/* cheeek nef24l01 */
/* 0: ok 1:error*/
rt_uint8_t NRF24L01_Check(void)
{
rt_uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
rt_uint8_t i;

NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);
NRF24L01_Read_Buf(TX_ADDR,buf,5);

for(i=0;i<5;i++)
if(buf
    !=0XA5)break;

    if(i!=5)return 1;
    return 0;
    }

    void NRF24L01_Write_Reg(rt_uint8_t reg,rt_uint8_t date)
    {
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,&reg,1,&date,1);
    }

    rt_uint8_t NRF24L01_Read_Reg(rt_uint8_t reg)
    {
    rt_uint8_t date;
    rt_spi_send_then_recv(nrf24l01_device.rt_spi_device,&reg,1,&date,1);
    return date;
    }

    static rt_err_t spi_nrf_transfer(struct spi_nrf_device *dev)
    {
    rt_uint8_t sta;

    if(nrf24l01_device.tx_packet.data_len>0)
    {
    rt_uint8_t size=nrf24l01_device.tx_packet.data_len;
    NRF_TRACE("send data
    ");
    SetCEN(0);
    NRF24L01_Write_Buf(WR_TX_PLOAD,nrf24l01_device.tx_packet.buffer,size);//????????
    nrf24l01_device.tx_packet.data_len=0;
    SetCEN(1);
    return RT_EOK;
    }
    sta=NRF24L01_Read_Reg(STATUS); //????×????????÷????
    NRF_TRACE("status=%2x
    ",sta);
    NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //????TX_DS?òMAX_RT????±ê??
    if(sta&MAX_TX)//????×??ó??·?????
    {
    NRF_TRACE("send max data
    ");
    NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
    }
    else if(sta&TX_OK)//·????ê??
    {
    NRF_TRACE("send ok
    ");
    nrf24l01_device.rx_packet.data_len=0;
    NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
    }
    else if(sta&RX_OK)//??????????
    {
    NRF_TRACE("recv ok
    ");
    NRF24L01_Read_Buf(RD_RX_PLOAD,nrf24l01_device.rx_packet.buffer,20);//????????
    nrf24l01_device.rx_packet.data_len=20;
    NRF24L01_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷
    }
    return RT_EOK;
    }

    /* RT-Thread device interface */
    static rt_err_t nrf24lxx_init(rt_device_t dev)
    {
    return RT_EOK;
    }

    static rt_err_t nrf24lxx_open(rt_device_t dev, rt_uint16_t oflag)
    {
    return RT_EOK;
    }

    static rt_err_t nrf24lxx_close(rt_device_t dev)
    {
    return RT_EOK;
    }


    static rt_err_t nrf24lxx_control(rt_device_t dev, rt_uint8_t cmd, void *args)
    {
    uint8_t send_buffer[4];

    RT_ASSERT(dev != RT_NULL);
    switch(cmd)
    {
    /* modue tx */
    case SET_MODE_TX:
    NRF_TRACE("set mode tx
    ");
    NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e);
    break;

    /* modue rx */
    case SET_MODE_RX:
    NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0f);
    SetCEN(1);
    break;

    /* EN CH */
    case SET_EN_CH:
    send_buffer[0]=NRF_WRITE_REG+EN_RXADDR;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* rf ch */
    case SET_RF_CH:
    send_buffer[0]=NRF_WRITE_REG+RF_CH;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    case SET_RF_RETR:
    send_buffer[0]=NRF_WRITE_REG+SETUP_RETR;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set tx addr */
    case SET_TX_ADDR:
    send_buffer[0]=NRF_WRITE_REG+TX_ADDR;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
    break;

    /* set tx ack */
    case SET_EN_ACK:
    send_buffer[0]=NRF_WRITE_REG+EN_AA;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set SET_RF_SETUP */
    case SET_RF_SETUP:
    send_buffer[0]=NRF_WRITE_REG+RF_SETUP;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set rx add0 */
    case SET_RX_ADDR0:
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
    break;

    /* set rx add1 */
    case SET_RX_ADDR1:
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P1;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
    break;

    /* set rx add2 */
    case SET_RX_ADDR2:
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P2;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);

    break;

    /* set rx add3 */
    case SET_RX_ADDR3:
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P3;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set rx add4 */
    case SET_RX_ADDR4:
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P4;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set rx add5 */
    case SET_RX_ADDR5:
    send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P5;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set SET_RX_SIZE0 */
    case SET_RX_SIZE0:
    send_buffer[0]=NRF_WRITE_REG+RX_PW_P0;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set SET_RX_SIZE0 */
    case SET_RX_SIZE1:
    send_buffer[0]=NRF_WRITE_REG+RX_PW_P1;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set SET_RX_SIZE0 */
    case SET_RX_SIZE2:
    send_buffer[0]=NRF_WRITE_REG+RX_PW_P2;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;


    /* set SET_RX_SIZE0 */
    case SET_RX_SIZE3:
    send_buffer[0]=NRF_WRITE_REG+RX_PW_P3;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    /* set SET_RX_SIZE0 */
    case SET_RX_SIZE4:
    send_buffer[0]=NRF_WRITE_REG+RX_PW_P4;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;


    /* set SET_RX_SIZE0 */
    case SET_RX_SIZE5:
    send_buffer[0]=NRF_WRITE_REG+RX_PW_P5;
    rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
    break;

    }
    return RT_EOK;
    }

    static rt_size_t nrf24lxx_read(rt_device_t dev,
    rt_off_t pos,
    void* buffer,
    rt_size_t size)
    {
    if (nrf24l01_device.rx_packet.data_len>0)
    {
    size=nrf24l01_device.rx_packet.data_len;
    rt_memcpy(buffer,nrf24l01_device.rx_packet.buffer,size);
    nrf24l01_device.rx_packet.data_len=0;
    return size;
    }
    else
    return 0;
    }

    static rt_size_t nrf24lxx_write(rt_device_t dev,
    rt_off_t pos,
    const void* buffer,
    rt_size_t size)
    {
    NRF_TRACE("write nrf size %d
    ",size);
    if (nrf24l01_device.tx_packet.data_len==0)
    {
    nrf24l01_device.tx_packet.data_len =size;
    rt_memcpy(nrf24l01_device.tx_packet.buffer,buffer,size);
    rt_event_send(&nrf24l01_device.data_event, 1);
    return size;
    }
    else
    return 0;
    }
    /********************************* RT-Thread rt_device interface end **************************************/
    static void nrf_data_thread_entry(void *parameter)
    {
    rt_uint32_t e;
    rt_err_t result;

    NRF_TRACE("spi thread is ok
    ");
    while (1)
    {
    /* receive first event */
    if (rt_event_recv(&nrf24l01_device.data_event,
    1,
    RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
    RT_WAITING_FOREVER,
    &e) != RT_EOK)
    {
    continue;
    }
    spi_nrf_transfer(&nrf24l01_device);
    }
    }

    rt_err_t nrf24l01_init(const char * nrf_device_name, const char * spi_device_name)
    {

    rt_memset(&nrf24l01_device, 0, sizeof(struct spi_nrf_device));
    nrf24l01_device.rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
    if(nrf24l01_device.rt_spi_device == RT_NULL)
    {
    NRF_TRACE("spi device %s not found!
    ", spi_device_name);
    return -RT_ENOSYS;
    }
    /* config spi */
    {
    struct rt_spi_configuration cfg;
    cfg.data_width = 8;
    cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
    cfg.max_hz = 8 * 1000 * 1000; /* 8M */
    rt_spi_configure(nrf24l01_device.rt_spi_device, &cfg);
    }
    rt_event_init(&nrf24l01_device.data_event, "nrf", RT_IPC_FLAG_FIFO);

    /* nrf24lxx_init */
    {
    rt_uint8_t sendbuffer[5];

    if(NRF24L01_Check())
    {
    rt_kprintf("nrf device not found!
    ");
    return -RT_ENOSYS;
    }

    /* set nrf tx addr */
    nrf24lxx_control(&nrf24l01_device.device,SET_RX_ADDR0,"FYYYY");

    sendbuffer[0]=20;
    nrf24lxx_control(&nrf24l01_device.device,SET_RX_SIZE0,sendbuffer);


    /* set nrf en ack */
    sendbuffer[0]=0x00;
    nrf24lxx_control(&nrf24l01_device.device,SET_EN_ACK,sendbuffer);



    /* set nrf en ch */
    sendbuffer[0]=0x01;
    nrf24lxx_control(&nrf24l01_device.device,SET_EN_CH,sendbuffer);

    /* reset send time :500us + 86us; send max count 10 */
    sendbuffer[0]=0x1a;
    nrf24lxx_control(&nrf24l01_device.device,SET_RF_RETR,sendbuffer);

    /* set nrf rf ch */
    sendbuffer[0]=40;
    nrf24lxx_control(&nrf24l01_device.device,SET_RF_CH,sendbuffer);

    sendbuffer[0]=0x0f;
    nrf24lxx_control(&nrf24l01_device.device,SET_RF_SETUP,sendbuffer);

    }
    /* register device */
    nrf24l01_device.device.type = RT_Device_Class_Char;
    nrf24l01_device.device.init = nrf24lxx_init;
    nrf24l01_device.device.open = nrf24lxx_open;
    nrf24l01_device.device.close = nrf24lxx_close;
    nrf24l01_device.device.read = nrf24lxx_read;
    nrf24l01_device.device.write = nrf24lxx_write;
    nrf24l01_device.device.control = nrf24lxx_control;

    /* no private */
    nrf24l01_device.device.user_data = RT_NULL;

    /* nrf thread init */
    {
    rt_thread_t tid;
    tid = rt_thread_create("nrf",
    nrf_data_thread_entry,
    RT_NULL,
    256,
    RT_THREAD_PRIORITY_MAX - 2,
    20);

    if (tid != RT_NULL)
    rt_thread_startup(tid);
    }
    spi_nrf_hw_init();
    rt_device_register(&nrf24l01_device.device, nrf_device_name,
    RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);

    return RT_EOK;
    }
    void spi_nrf_isr(int vector)
    {
    /* enter interrupt */
    rt_interrupt_enter();

    NRF_TRACE("spi_nrf_isr
    ");
    rt_event_send(&nrf24l01_device.data_event, 1);
    /* leave interrupt */
    rt_interrupt_leave();
    }

    #ifdef RT_USING_FINSH
    #include


    void nrf_info(void)
    {
    rt_uint8_t sendbuffer[20];

    NRF24L01_Read_Buf(TX_ADDR,sendbuffer,5);
    rt_kprintf("tx_addr: 0x%2x-0x%2x-0x%2x-0x%2x-0x%2x
    ",sendbuffer[0],
    sendbuffer[1],
    sendbuffer[2],
    sendbuffer[3],
    sendbuffer[4]);


    NRF24L01_Read_Buf(RX_ADDR_P0,sendbuffer,5);
    rt_kprintf("rx_addr0: 0x%2x-0x%2x-0x%2x-0x%2x-0x%2x
    ",sendbuffer[0],
    sendbuffer[1],
    sendbuffer[2],
    sendbuffer[3],
    sendbuffer[4]);
    rt_kprintf("RF_CH = 0x%2d
    ",NRF24L01_Read_Reg(RF_CH));
    rt_kprintf("EN_AA = 0x%2x
    ",NRF24L01_Read_Reg(EN_AA));
    rt_kprintf("EN_RXADDR = 0x%2x
    ",NRF24L01_Read_Reg(EN_RXADDR));
    rt_kprintf("RF_SETUP = 0x%2x
    ",NRF24L01_Read_Reg(RF_SETUP));
    rt_kprintf("SETUP_RETR = 0x%2x
    ",NRF24L01_Read_Reg(SETUP_RETR));
    rt_kprintf("CONFIG = 0x%2x
    ",NRF24L01_Read_Reg(CONFIG));

    }

    FINSH_FUNCTION_EXPORT(nrf_info, printf nrf_info.)
    #endif


struct nrf_data_packet
{
rt_uint8_t data_len;
rt_uint8_t buffer[NRF_SIZE];
};
struct spi_nrf_device
{
struct rt_device device;
struct rt_spi_device * rt_spi_device;
struct rt_event data_event;
struct nrf_data_packet tx_packet;
struct nrf_data_packet rx_packet;
};

还是出现系统崩溃

void list_date(void)
{
time_t now;
time(&now);
rt_kprintf("%s
", ctime(&now));

{
struct tm *gmt;
gmt=gmtime(&now);
rt_kprintf("GMT is: %s", asctime(gmt));
}
}

在底层中创建一个线程 处理中断信息吗

有中断的设备,底层os驱动需要怎样处理,比如有数据接收,发生中断,
怎样去读数据,

读函数可以写成

static rt_size_t nrf24lxx_read(rt_device_t dev,
rt_off_t pos,
void* buffer,
rt_size_t size)
{
rt_uint8_t sta;

sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值
if(sta&RX_OK)//接收到数据
{
NRF_TRACE("recv data
");
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
NRF24L01_Read_Buf(RD_RX_PLOAD,buffer,20);//读取数据
return 20;
}
return 0;
}

请大神帮忙看看有什么地方需要注意,或者怎样修改 ,代码测试能用, 以下NRF_device是结构
struct nrf_date
{
rt_uint8_t buff[30];
rt_uint8_t Wrt;
};

struct spi_nrf_device
{
struct rt_device device;
struct rt_spi_device * rt_spi_device;
struct nrf_date date;
struct rt_mutex lock;
};

struct tm *gmt;
now = time(NULL);
gmt = gmtime(&now);
printf("GMT is: %s", asctime(gmt));


是没有为gmt 分配存储空间造成的吗

OK,搞定 谢谢 !!

	
if (dfs_mount("flash0", "/", "elm", 0, 0) == 0)
{
rt_kprintf("
mount flash ok!
");
}
else
rt_kprintf(" mount flash error!
");

mkdir("/sd",0);

if (dfs_mount("sd0", "/sd", "elm", 0, 0) == 0)
{
rt_kprintf(" mount sd ok!
");
}
else
rt_kprintf(" mount sd error!
");


finsh />
mount flash ok!
mount sd ok!
ls()
Directory /:
1
SD
FLASH
SD0
0, 0x00000000
finsh />cd("sd")
0, 0x00000000
finsh /sd>ls()
Directory /sd:
1
2
3
4
0, 0x00000000
finsh /sd>

使用文件系统 同时挂载SD卡 与FLASH 挂载的路径 有什么不一样的 ,在shell中怎样查看
以下是我的代码

    if (dfs_mount("flash0", "/", "elm", 0, 0) == 0)
{
rt_kprintf("File flash System initialized!
");
}
else
rt_kprintf("File flash System initialzation failed!
");

if (dfs_mount("sd0", "/dev", "elm", 0, 0) == 0)
{
rt_kprintf("File sd System initialized!
");
}
else
rt_kprintf("File sd System initialzation failed!
");

回到
顶部

发布
问题

投诉
建议