Hi,
I'm trying to get an Arduino Fio running as a synchronous SPI slave of Nordic Semis ANT+ nRF24AP2 transciever chip:
http://www.thisisant.com/images/Resources/PDF/nrf24ap2%20product%20specification.pdf
Although there are a couple of docs around on how to achieve that, I'm lost. My information basis is
-
Application note on Bit sync communication with MSP430
http://www.thisisant.com/images/Resources/PDF/ANT_AN01_Implementing_a_Receiver_for_Tx_only_ANT_devices.pdf -
General ANT Interfacing (has section on Byte Sync via SPI)
http://www.thisisant.com/images/Resources/PDF/Interfacing_with_ANT_General_Purpose_Chipsets_and_Modules.pdf -
General ANT message protocol
http://www.thisisant.com/images/Resources/PDF/1204662412_ant_message_protocol_and_usage.pdf -
AP2 Anormaly: SEN asserted too early:
http://www.thisisant.com/component/option,com_fireboard/Itemid,146/func,view/catid,25/id,1254/
Following these docs and despite having tried hard, I have not yet managed to get Byte Sync SPI running. Wiring is as follows:
AP2 - FIO
SOUT - MOSI
SIN - MISO
SCLK - SCK
SEN - GPIO Interrupt 1 (D3) (to account for AP2 anomaly)
SRDY - GPIO (D7)
SMSGRDY - GPIO (D6)
RESET - GPIO (D4)
Moreover, GPIO D5 is connected to D10 (SS), such that I can account for the SEN bug manually. This is the code I'm running:
#include <SPI.h>
// specifies messages and events
#include "nrf24ap2.h"
void SlaveInit(void) {
// Set MISO output, all others input
pinMode(SCK_PIN, INPUT);
pinMode(MOSI_PIN, INPUT);
pinMode(MISO_PIN, OUTPUT);
pinMode(SS_PIN, INPUT);
pinMode(resetPin, OUTPUT);
// Connect:
/** 1. set up SPI */
// Implements LSBFIRST, SPI_MODE3, SPI_CLOCK_DIV16
SPCR = B11001101;
/** 2. PORTSEL=1 SFLOW=0 RESET=1 SRDY=1 MSGRDY=1 */
// portsel and sflow are hard-wired to correct values
digitalWrite(resetPin, HIGH);
digitalWrite(srdyPin, HIGH);
digitalWrite(smsgrdyPin, HIGH);
/** 3. wait(300μs) */
delay(300);
/** 4. RESET = 0 */
digitalWrite(resetPin, LOW);
/** 5. wait(300μs) */
delay(300);
/** 6. RESET = 1 */
//reset done
digitalWrite(resetPin, HIGH);
/** 7. wait(>2ms) */
delay(20);
/** 8. enable SPI */
SPI.begin(); // hello, SPI bus
Serial.println("Finished Reset.");
// PROGRAM STOPS HERE =( - infinite loop?
byte x = SPI.transfer (0); // get response
if (x == 0xA5) {
// peripheral is alive
Serial.print("Hello ANT! - Message ANT - Host: ");
Serial.println(x);
} else if (x == 0xA4) {
Serial.print("Hello ANT! - Message Host - ANT: ");
Serial.println(x);
} else {
Serial.print("Unknown message: ");
Serial.println(x);
}
}
// infinite loop in while-part =(
char spi_transfer(volatile char data) {
SPDR = data; // Start the transmission
// Wait for the end of the transmission
while (!(SPSR & (1<<SPIF)));
return SPDR; // return the received byte
}
void setup() {
Serial.begin(57600);
// define pin modes
pinMode(smsgrdyPin, OUTPUT);
pinMode(srdyPin, OUTPUT);
// SEN line cannot be used for SPI’s SSEL as it is released at the beginning of the last byte in a transaction rather
// than one clock cycle after the last bit. By the SPI standard, the slave receiving
// this signal should immediately abandon the transaction and tri-state the MISO line.
// In practice this means that SPI hardware will not be able to send or receive the
// ANT protocol checksum byte with this configuration.
// SEN is connected to interrupt 3
pinMode(ssInterruptPin, INPUT);
attachInterrupt(1, slaveSelectInterrupt, FALLING);
// delayed SEN output via dout 5
pinMode(ssOutPin, OUTPUT);
// fix low for now
digitalWrite(ssOutPin, LOW);
SlaveInit();
}
// service routine call has not yet happend =(
void slaveSelectInterrupt() {
Serial.println("Interrupt: Received FALLING SS");
// wait for last byte received and set SS to high
}
void loop() {
// ANT_rxHandler(); // one day =)
}
What am I doing wrong? As I'm pretty stuck at the moment I would appreciate any ideas of you guys.