I want to make a small wireless network using Atmel µCs and nRF24L01 transceivers. The receiver is based on an ATmega328P and the transmitters are based on ATtiny84s. I connected everything correctly (tried this multiple times) and got erratic behavior, like radio.available() reading true even when the chip was completely disconnected.
I did some research and found the following webpage:
Apparently on the ATtiny using this library the MISO and MOSI pins of the nRF24L01 need to be reversed. Once I did that, the modules worked as I wanted and radio.isChipConnected() also returned positive and they were able to send and receive.
Interestingly, on the ATmega328P the pins are wired non-crossed (MISO to MISO and MOSI to MOSI) and that works a treat, including in-system programming.
I would leave it like this, because it works, but unfortunately now in-system programming fails with an error message to check the connections. Removing the module or switching the wires back solves the problem, but I'd prefer to be able to use in-system programming. Is there a way to modify the library to switch the pins back to normal?
The ATtiny84 doesn’t have native SPI capability as a Master. So the pins labeled MOSI and MISO are for programming, thus with the ATtiny84 being the slave. So, the directions are “correct”. It’s a little unfortunate for your situation for sure, since while running, the DO and DI are the pins being used, and those are opposite directions.
That’s correct—MISO to MISO, MOSI to MOSI, and SCK to SCK. The CS\ lines are independent and are used to select the specific chip. Many devices can be connected this way on the same bus.
I don't think you understand the issue here. There is confusion+ caused by the datasheet of the ATtiny84 labeling a couple pins as MOSI (PA6) and MISO (PA5) in the "Pin Configurations" on page 2. However, this does not make direct sense, since there is no native SPI on the Tiny84. When us beginners go to hook up this wonderful little chip to an NRF24L01 we often get confused and mess it up, hooking things up like you have in your picture. Matching letter? Heck yeah, I can do that!
But that is wrong, going MOSI to MOSI etc. will not work. The Data In (DI) and Data Out (DO) are the ones that are used for SPI traffic. And confusingly (again, for us beginners) the DO pin is the one that also serves as MISO. See how that is reversed? Same for the DI pin.
Again, it's just because the datasheet pin labeling is referenced to programming, not operation. All this is of course very clear in the Serial Programming section, where MOSI is clearly referred to "Input", but for us beginners datasheets are tough to digest sometimes. Same for the DO/DI in the USI section, but reading and understanding that takes even more effort. We just want to get the wireless radios talking, and matching letter is often 'our' first instinct. Then we ask questions and get all confused...
Fortunately, I think most of the AVR chips with dedicated SPI hardware don't lay this "trap" for the unwary.
Datasheets can be real beasts to wade through and a real frightener for beginners as they will detail almost every possible use and configuration, making it hard to sometimes locate the actual nugget of information you need. However, that's the purpose of the datasheet - to provide all the information in one place.
But all is not lost, as sometimes the manufacturer will also have application notes that are specific to the device. If you look on the Microchip website and search for ATTINY84 you should find the ATTINY84 page. You can click on the documentation link and it will take you to the relevant documentation. Put in the search term USI and you should get a few useful documents.
There's application note AVR319: "Using the USI Module for SPI Communication on tinyAVR and megaAVR Devices" which holds the details of the USI in SPI mode. I've attached it below. The first page has this image:
FYI: SPI came from Motorola in the late 1970's. It was designed to expand the I/O and add functionality to there processors. There were 4 pins and they were labeled:
MOSI (Master Out Slave In): This pin carries data sent from the master device to the slave device.
MISO (Master In Slave Out): This pin carries data sent from the slave device to the master.
SCLK (Serial Clock): The clock signal generated by the master to synchronize communication between the master and slave devices. Only the current master generates the clock.
CS (Chip Select) or SS (Slave Select): This pin is used by the master to select which slave device it wants to communicate with. It is an active-low signal, meaning the selected slave is active when the CS pin is low.
These pins enable SPI devices to exchange data efficiently by ensuring a synchronized transfer of bits. The master controls the clock and selects which slave to communicate with, allowing multiple devices to share the same SPI bus with unique CS lines.
Some politically correct advocates got involved and changed the names.
Thanks for the detailed responses, everyone! I gather it's not possible to change this in software and I just need to live with having to unplug and replug the nRF24L01 when I want to program the ATtiny84, right?