void reset_cc1101() { uint8_t val=CCxxx0_SRES; reset_cs(); cs(0); while(miso()); spi_send_rcv(val); while(miso()); cs(1); return; }
uint8_t read_cc1101(uint8_t addr,uint8_t *buf,uint8_t len,uint8_t type) { uint8_t cmd,data=0,i; cs(0); while(miso()); if(type==TYPE_BURST) { cmd = addr | READ_BURST; spi_send_rcv(cmd); for(i=0;i<len;i++) buf[i]=spi_send_rcv(0); } else if(type==TYPE_REG) { cmd = addr | READ_BURST; spi_send_rcv(cmd); return spi_send_rcv(0); } else { cmd = addr | READ_SINGLE; spi_send_rcv(cmd); return spi_send_rcv(0); } cs(1); return 0; }
uint8_t spi_send_rcv(uint8_t data) { uint8_t i,temp; temp = 0; clk(0); for(i=0; i<8; i++) { if(data & 0x80) { mosi(1); } else mosi(0); data <<= 1; clk(1); _nop_(); _nop_(); temp <<= 1; if(miso())temp++; clk(0); _nop_(); _nop_(); } return temp; }
void SPI::begin() { OutputPin sck(SPI_SCK_PORT, SPI_SCK_PIN); OutputPin mosi(SPI_MOSI_PORT, SPI_MOSI_PIN); InputPin miso(SPI_MISO_PORT, SPI_MISO_PIN); OutputPin ss(SPI_SS_PORT, SPI_SS_PIN); sck.begin(); mosi.begin(); miso.begin(); ss.begin(); sck.clear(); mosi.clear(); ss.set(); // Warning: if the SS pin ever becomes a LOW INPUT then SPI // automatically switches to Slave, so the data direction of // the SS pin MUST be kept as OUTPUT. SPCR = (1<<MSTR) | (1<<SPE); }
void write_cc1101(uint8_t addr,uint8_t* buf,uint8_t len,uint8_t type) { uint8_t cmd; int i; cs(0); while(miso()); if(type==TYPE_BURST) {/*write brust */ cmd = addr | WRITE_BURST; spi_send_rcv(cmd); for(i=0;i<len;i++) spi_send_rcv(buf[i]); } else if(type==TYPE_REG) {/*write reg */ spi_send_rcv(addr); spi_send_rcv(buf[0]); } else/* strobe */ spi_send_rcv(addr); cs(1); }