Atmega 328 and nRF8001

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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!

This topic has a solution.
Last Edited: Thu. Dec 28, 2017 - 06:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Didn't Adafruit's own software help you?

 

https://learn.adafruit.com/getti...

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is really not a beginner project!

 

Note that the nRF8001 is now very long in the tooth!

 

https://www.nordicsemi.com/eng/Products/Bluetooth-low-energy/nRF8001

 

As Ross says, why not start with the materials that Adafruit provide?

 

Nordic have an Arduino library for nRF8001, called ble-sdk-arduino:

 

https://github.com/NordicSemiconductor/ble-sdk-arduino

 

I would certainly recommend that you start with that - it's probably what the Adafruit examples are based on.

 

Note that Nordic Semiconductor is not an Atmel (or Microchip) company - so this is not the place to discuss the detail of their products.

 

The Nordic forum is here: https://devzone.nordicsemi.com/questions/

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Note that the nRF8001 is now very long in the tooth!

In fact, it was probably (one of) the first chips to implement BLE.

 

Therefore, it has a very early BLE implementation.

 

It is not updateable - so if you're hoping to use any of the latest BLE features, you may be out of luck ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One issue about SPI we see here a lot is failure to setup and control the /SS pin, I did not see any code for this pin, so it defaults as do all pins to be an input.

Here is what the DS says about /SS as an input:

 

If SS is configured as an input, it must be held high to ensure Master SPI operation. If the SS pin is driven low by
peripheral circuitry when the SPI is configured as a Master with the SS pin defined as an input, the SPI system
interprets this as another master selecting the SPI as a slave and starting to send data to it. To avoid bus
contention, the SPI system takes the following actions:
1. The MSTR bit in SPCR is cleared and the SPI system becomes a Slave. As a result of the SPI becoming
a Slave, the MOSI and SCK pins become inputs.

 

If the pin is an input and left floating (no connection) it will float low at some point and will switch to slave mode and stop working!

The cure is to set the port pin as an output in your code.

 

Jim

 

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that the nRF8001 does not use (or even have)  /SS in the conventional SPI sense.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I just realized that I swapped the two pins in my define statements. Oh how stupid...spent hours trying to figure out wtf was going on....

 

Oh and I also needed the reset pin to be on as well.

 

Thanks!

 

edit: for those searching this in the future: the two pins I swapped were the REQ and RDY pins

Last Edited: Thu. Dec 28, 2017 - 06:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oops!

 

Thanks for sharing - now please mark the solution: https://www.avrfreaks.net/comment...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...