Fix code, now that we have sspSendReceive

This commit is contained in:
Stefan `Sec` Zehl 2011-07-08 00:06:53 +02:00
parent 1c2846734c
commit fc7b630902
3 changed files with 41 additions and 52 deletions

View File

@ -32,5 +32,5 @@ $(LIBFILE): $(OBJS)
clean:
rm -f $(OBJS) $(LIBFILE)
nrf24l01p.o: nrf24l01p.h
nrf24l01p.o: nrf24l01p.c nrf24l01p.h

View File

@ -7,69 +7,58 @@
#define MAC_BEACON "BEACO"
/*-----------------------------------------------------------------------*/
/* Transmit a byte via SPI (Platform dependent) */
/* Transmit a byte via SPI */
/*-----------------------------------------------------------------------*/
void xmit_spi(uint8_t dat) {
inline void xmit_spi(uint8_t dat) {
sspSend(0, (uint8_t*) &dat, 1);
}
/*-----------------------------------------------------------------------*/
/* Receive a byte from MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
uint8_t rcvr_spi (void) {
uint8_t data = 0;
sspReceive(0, &data, 1);
return data;
}
#define rcvr_spi_m(dst) \
do { \
sspReceive(0, (uint8_t*)(dst), 1); \
} while(0)
#define CS_LOW() gpioSetValue(RB_SPI_NRF_CS, 0)
#define CS_HIGH() gpioSetValue(RB_SPI_NRF_CS, 1)
#define CE_LOW() gpioSetValue(RB_NRF_CE, 0)
#define CE_HIGH() gpioSetValue(RB_NRF_CE, 1)
void nrf_cmd(uint8_t cmd){
CS_LOW();
xmit_spi(cmd);
CS_HIGH();
};
uint8_t nrf_cmd_status(uint8_t cmd){
CS_LOW();
sspSendReceive(0, &cmd, 1);
return cmd;
CS_HIGH();
};
void nrf_cmd_rw_long(uint8_t* data, int len){
CS_LOW();
sspSendReceive(0,data,len);
CS_HIGH();
};
void nrf_write_reg(const uint8_t reg, const uint8_t val){
CS_LOW();
xmit_spi(C_W_REGISTER | reg);
xmit_spi(val);
CS_HIGH();
};
/*
uint8_t nrf_read_reg(const uint8_t reg, uint8_t val){
xmit_spi(C_R_REGISTER | reg);
// do i need to read the status byte here?
xmit_spi(val);
return rcvr_spi();
};
*/
void nrf_write_reg_long(const uint8_t reg, int len, char* data){
xmit_spi(C_W_REGISTER | reg);
for(int i=0;i<len;i++){
xmit_spi(data[i]);
};
};
//XXX: Why is len a pointer?
void nrf_cmd_read_long(const uint8_t cmd, int *len, char* data){
data[0] = 0xFF;
for(int i=1;i<*len;i++)
void nrf_read_long(const uint8_t reg, int len, uint8_t* data){
CS_LOW();
xmit_spi(reg);
for(int i=0;i<len;i++)
data[i] = 0x00;
sspSendReceive(0,data,*len);
sspSendReceive(0,data,len);
CS_HIGH();
};
void nrf_write_reg_long(const uint8_t reg, int len, uint8_t* data){
CS_LOW();
xmit_spi(C_W_REGISTER | reg);
sspSend(0,data,len);
CS_HIGH();
};
void nrf_init() {
@ -101,26 +90,24 @@ void nrf_init() {
);
nrf_write_reg(R_RX_PW_P0,16);
nrf_write_reg_long(R_RX_ADDR_P0,5,"\x1\x2\x3\x2\1");
nrf_write_reg_long(R_RX_ADDR_P0,5,(uint8_t*)"\x1\x2\x3\x2\1");
// nrf_write_reg(R_RX_PW_P1,16);
// nrf_write_reg_long(R_RX_ADDR_P1,5,"R0KET");
// OpenBeacon transmit address
nrf_write_reg_long(R_TX_ADDR,5,MAC_BEACON);
nrf_write_reg_long(R_TX_ADDR,5,(uint8_t*)MAC_BEACON);
// Set speed / strength
nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3);
// XXX: or write R_CONFIG last?
CS_HIGH();
};
int nrf_rcv_pkt_time(int maxtime, int maxsize, char * pkt){
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
char buf;
int len;
CS_LOW();
nrf_write_reg(R_CONFIG,
R_CONFIG_PRIM_RX| // Receive mode
R_CONFIG_PWR_UP| // Power on
@ -130,7 +117,7 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, char * pkt){
delayms(maxtime); // XXX: check interrupt?
CE_LOW();
len=1;
nrf_cmd_read_long(C_R_RX_PL_WID,&len,&buf);
nrf_read_long(C_R_RX_PL_WID,len,&buf);
len=buf;
if(len>32 || len==0){
return 0; // no packet
@ -138,7 +125,7 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, char * pkt){
if(len>maxsize){
return -1; // packet too large
};
nrf_cmd_read_long(C_R_RX_PAYLOAD,&len,pkt);
nrf_read_long(C_R_RX_PAYLOAD,len,pkt);
CS_HIGH();
return len;
};

View File

@ -93,13 +93,15 @@
#define R_RF_SETUP_DR_250K 0x20
/* exported functions */
int nrf_rcv_pkt_time(int maxtime, int maxsize, char * pkt);
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt);
void nrf_init() ;
void nrf_cmd_read_long(const uint8_t cmd, int *len, char* data);
void nrf_write_reg_long(const uint8_t reg, int len, char* data);
void nrf_write_reg(const uint8_t reg, const uint8_t val);
uint8_t nrf_cmd_status(uint8_t cmd);
void nrf_cmd(uint8_t cmd);
uint8_t nrf_cmd_status(uint8_t cmd);
void nrf_cmd_rw_long(uint8_t* data, int len);
void nrf_read_long(const uint8_t reg, int len, uint8_t* data);
void nrf_write_reg(const uint8_t reg, const uint8_t val);
void nrf_write_reg_long(const uint8_t reg, int len, uint8_t* data);
/* END */