#include "cc110l.h" #define CONFIG0 0x00 #define SRX 0x34 #define STX 0x35 #define SNOP 0x3D #define FIFO 0x3F #define STATE_MASK 0x70 #define STATE_IDLE (0x00 << 4) #define STATE_RX (0x01 << 4) #define STATE_TX (0x02 << 4) #define WRITE 0x00 #define BURST 0x40 #define READ 0x80 uint8_t debug[16] = {0}; uint8_t spiret; uint8_t spitran; int8_t ind = -1; static uint8_t config_regs[] = { 0x29, 0x2E, 0x06, 0x07, (RADIO_SYNC >> 8) & 0xFF, RADIO_SYNC & 0xFF, 0xFF, 0x04, 0x05, RADIO_ADDR, RADIO_CHANNEL, 0x06, 0x00, 0x22, 0xB6, 0x27, 0x2A, 0x83, 0x03, 0x21, 0xEE, 0x65, 0x07, 0x30, 0x18, 0x16, 0x6C, 0x43, 0x40, 0x91, 0x87, 0x6B, 0xF8, 0x56, 0x10, 0xE9, 0x2A, 0x00, 0x1F, 0x41, 0x00, 0x59, 0x7F, 0x3F, 0x81, 0x35, 0x09 }; static void spi_begin() { RADIO_CLK_PORT &= ~RADIO_CLK_BIT; RADIO_CS_PORT &= ~RADIO_CS_BIT; __nop(); while (RADIO_MISO_PORT & RADIO_MISO_BIT) __nop(); } static inline void spi_end() { __nop(); RADIO_CS_PORT |= RADIO_CS_BIT; } static uint8_t spi_comm(uint8_t byte) { uint8_t bit; for (bit = 8; bit; bit--) { if (byte & 0x80) RADIO_MOSI_PORT |= RADIO_MOSI_BIT; else RADIO_MOSI_PORT &= ~RADIO_MOSI_BIT; RADIO_CLK_PORT |= RADIO_CLK_BIT; __nop(); byte <<= 1; if (RADIO_MISO_PORT & RADIO_MISO_BIT) byte |= 0x01; RADIO_CLK_PORT &= ~RADIO_CLK_BIT; } spiret = byte; spitran++; __nop(); return byte; } static void spi_state_wait(uint8_t status) { //while ((spi_comm(SNOP) & STATE_MASK) != status) // __nop(); while ((spi_comm(SNOP) & STATE_MASK) != STATE_IDLE) __nop(); } void radio_init() { uint8_t n; RADIO_CS_PORT |= RADIO_CS_BIT; __nop(); spi_begin(); spi_comm(CONFIG0 | BURST | WRITE); for (n = 0; n < sizeof(config_regs); n++) spi_comm(config_regs[n]); spi_end(); } void radio_send(uint8_t *buffer, uint8_t length) { uint8_t i; spi_begin(); spi_comm(FIFO | BURST | WRITE); spi_comm(length + 1); spi_comm(RADIO_ADDR); for (i = 0; i < length; i++) spi_comm(buffer[i]); spi_end(); spi_begin(); spi_comm(STX); spi_state_wait(STATE_TX); spi_end(); } uint8_t radio_recv(uint8_t *buffer) { uint8_t i, length; spi_begin(); spi_comm(SRX); spi_state_wait(STATE_RX); spi_comm(FIFO | BURST | READ); length = spi_comm(0x00); spi_comm(0x00); // address for (i = 0; i < length; i++) buffer[i] = spi_comm(0x00); spi_end(); return length; } uint8_t radio_test(uint8_t reg) { uint8_t r = 0; RADIO_CS_PORT |= RADIO_CS_BIT; RADIO_CLK_PORT &= ~RADIO_CLK_BIT; __delay_cycles(10000); spi_begin(); spi_comm(reg | READ); r = spi_comm(0x00); spi_end(); return r; } void burst_test() { uint8_t n; __delay_cycles(10000); ind = -2; spi_begin(); ind = 0; debug[0] = spi_comm(CONFIG0 | BURST | READ); for (n = 1; n < 16; n++) { ind = n; debug[n] = spi_comm(0); __nop(); } spi_end(); }