Hello - I tried looking for my answer here but didn't really get what I need. I'm also relatively new to all of this.
I purchased the Adafruit's nRF8001 bluetooth module and would like to interface it with my Atmega328p microcontroller. So far what I have done is wired up the module with my uC using the proper SPI pins.
Now I'm trying to communicate with it, and I'm a little stuck. What I want to do is to send the Test System command.
#define BLE_DDR DDRB
#define BLE_PORT PORTB
#define BLE_PIN PINB
#define BLE_SCK PB5
#define BLE_MISO PB4
#define BLE_MOSI PB3
#define BLE_RDY PB2
#define BLE_REQ PB1
#define BLE_RST PB0
//SPI registers
#define SPI_CTRL_REG SPCR
#define SPI_STAT_REG SPSR
#define SPI_DATA_REG SPDR
#define SPI_ENABLE SPE
#define SPI_MASTER MSTR
#define SPI_CLK_DIV_16 SPR0
#define SPI_INTERRUPT SPIF
int main() {
initUSART();
//initialize SPI communication
BLE_DDR |= ((1 << BLE_MOSI) | (1 << BLE_SCK) | (1 << BLE_REQ) | (1 << BLE_RST));
BLE_PORT |= ((1 << BLE_RDY) | (1 << BLE_MISO));
SPI_CTRL_REG |= ((1 << SPI_ENABLE) | (1 << SPI_MASTER) | (1 << SPI_CLK_DIV_16));
BLE_PORT |= (1 << BLE_REQ);
_delay_ms(100);
//communicate to bluetooth via SPI
//turn on test mode on BLE
uint8_t len = 0x02;
// uint8_t op = 0x01;
// uint8_t data = 0x02;
//send it
_delay_ms(5000);
BLE_PORT &= ~(1 << BLE_REQ);
//wait until ble is ready to receive data
loop_until_bit_is_clear(BLE_PIN, BLE_RDY);
printString("sending len\r\n");
SPI_DATA_REG = len;
loop_until_bit_is_set(SPI_STAT_REG, SPI_INTERRUPT);
printString("done\r\n");
return 0;
}
From the datasheet, I need to set the Request line to low in order to begin the communication with the BLE module. However, it appears that I am stuck forever waiting for the RDY line to go low as well, so it will never send the data across the SPI line.
I've read the important bits of the ATMEL and nRF8001 datasheets but came up empty.
Is there something that I have overlooked? If I'm doing this completely wrong, can you point me in the right direction?
Many thanks!