Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
ralphd
PostPosted: Feb 13, 2014 - 01:52 AM
Hangaround


Joined: Feb 11, 2014
Posts: 421
Location: GWN

Nrf24l01+ modules are a cheap and low-power option for MCU wireless communication. Libraries are available for Arduino, and for <a href="https://github.com/stanleyseow/arduino-nrf24l01">arduino compatible MCUs like the ATTiny85</a>. Controlling the nrf modules usually requires power plus 5 pins - CE, CSN, SCK, MOSI, & MISO. With pin-limited MCUs like the ATtiny85, 5 pins is a lot to tie up. On something like the Digispark, with PB3 and PB4 hard-wired to USB+ and USB-, using the nrf24l01+ modules might seem impossible. Another issue is that although the nrf inputs are 5v tolerant, Vcc must be between 1.9 and 3.6V. I've designed a simple solution to provide 3V power as well as control the modules with just 3 of the pins on the ATtiny85.

For powering the nrf24l01+ module, a 3.3v regulator could be used, but a cheaper and simpler way is to drop a 5V supply to 3V using a 20mA-rated red led. Most red LEDs have a forward voltage drop between 1.8 and 2.2V, leaving 3.2-2.8V for the nrf, which is well within the 1.9-3.6V requirement.

Controlling CE without using a pin on the AVR is also easy: just tie it high. In my setup function, I set Mirf.cePin = 7 as a dummy since the tiny85 only has pins 0-6. I later commented out the digitalWrite calls in Nrf24l::ceHi() and ceLow(), and removed the Mirf.cePin line from setup() which cut down on the size of my compiled sketch.

I initially thought the CSN line could be tied low, but when I tried it my test sketch was not getting a valid status back from the nrf module. I also found section 8.3.1 of the datasheet: "Every new command must be started by a high to low transition on CSN." So in order to control the nrf with just 3 pins, CSN needs to be multiplexed with one (or more) of SCK, MOSI, or MISO. After a few different ideas, I came up with the attached circuit.

When SCK on the ATtiny85 goes high for several microseconds, C1 will charge through R1 and bring CSN high. If SCK is brought low for several microseconds before being used to clock the SPI data, C1 will discharge through D1 and bring CSN low. High pulses on SCK of less than a few microseconds during communication with the nrf won't last long enough to charge C1, so CSN will stay low.

To support the multiplexed SCK/CSN, I modified Mirf.cpp as follows:
Code:
void Nrf24l::csnHi(){
 PORTB |= (1<<PINB2);  // SCK->CSN HIGH
 delayMicroseconds(64);  // allow csn to settle
}

void Nrf24l::csnLow(){
 PORTB &= ~(1<<PINB2);  // SCK->CSN LOW
 delayMicroseconds(8);  // allow csn to settle
}

The circuit still worked with a 32us delay in csnHi and 8us in csnLow, but I doubled those values to have a good safety margin. The delays could be reduced with a smaller capacitor for C1. Going lower than .01uF could risk CSN going high during the high clock pulses of SCK.

When connecting the nrf module to a tiny85, connect MISO(pin7) on the module to MOSI/DI(PB0), and not MISO/DI(PB1). Here's the connections required:
nrf module ATtiny85 pin
SCK(5) PB2 (physical pin 7)
MOSI(6) PB1 (physical pin 6)
MISO(7) PB0 (physical pin 5)

I also changed TinyDebugSerial.h to define TINY_DEBUG_SERIAL_BIT 5, and connected pb5 to the Rx line of my ttl serial module.

Finally, here's my test sketch. When it runs, it reports a status of 'E', which is the reset value of the status register according to the datasheet. If you connect things wrong it will usually report 0 or FF.
Code:
#include <SPI85.h>
#include <Mirf.h>
#include <MirfHardwareSpi85Driver.h>

void setup()
{
  Serial.begin(115200);

  Mirf.spi = &MirfHardwareSpi85;
  Mirf.init();
}

void loop()
{
  uint8_t nrfStatus;
  delay(3000);
  Serial.print("\nMirf status: ");
  nrfStatus = Mirf.getStatus();
  // do back-to-back getStatus to test if CSN goes high
  nrfStatus = Mirf.getStatus();
  Serial.print(nrfStatus, HEX);
}


Last edited by ralphd on Feb 19, 2014 - 04:47 PM; edited 1 time in total
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Feb 13, 2014 - 09:49 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71075
Location: (using avr-gcc in) Finchingfield, Essex, England

Ralph,

We generally ask that for tutorials here you either put the text of the article in to the first post or at least attach a PDF file with the text. Too often links have been posted and after a year or two the URL goes dead. I have just used Print-to-PDF to create a shapshot of that page and attached it to your post above. But it's not very good, perhaps you could produce a better version? You can use the [Edit] button above to make changes.

Cliff, moderator.

PS I'll delete this "noise" after I think you've had a chance to read it.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Torby
PostPosted: Feb 15, 2014 - 02:55 PM
Raving lunatic


Joined: Nov 11, 2003
Posts: 6084
Location: Tulsa OK USA

Interesting.

You don't know how to include a payload in the ack packet, do you?

_________________
Torby

Cartesian coordinates are SO 17th century.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Torby
PostPosted: Feb 17, 2014 - 01:45 PM
Raving lunatic


Joined: Nov 11, 2003
Posts: 6084
Location: Tulsa OK USA

Saw you on Hack-A-Day. Congratulations.

_________________
Torby

Cartesian coordinates are SO 17th century.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
ralphd
PostPosted: Feb 19, 2014 - 04:50 PM
Hangaround


Joined: Feb 11, 2014
Posts: 421
Location: GWN

Torby wrote:
Interesting.

You don't know how to include a payload in the ack packet, do you?

Sorry, no I don't, but this page might help:
http://dmitry.gr/index.php?r=05.Project ... E%20fakery

_________________
Reason over passion - Pierre E Trudeau
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
ralphd
PostPosted: Feb 19, 2014 - 04:52 PM
Hangaround


Joined: Feb 11, 2014
Posts: 421
Location: GWN

Torby wrote:
Saw you on Hack-A-Day. Congratulations.

Thanks. I guess it's like TMZ for geeks/nerds. Smile

_________________
Reason over passion - Pierre E Trudeau
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits