Friday 16 March 2012

Now with added wireless

Unlike the jeenodes, arduinos don't usually come with an RFM12b wireless transmitter. But jeelabs sell a handy module.

The USB host port on the Mega ADK communicates using the SPI bus. So does the wireless module, but fortunately the bus can be shared. The three main bus signals (MOSI, MISO, SCK) can be connected to different clients, only the Slave Select (SS) needs to be separate. On top of that, the wireless module requires an interrupt line.

On the Mega board, I used pins 52, 51, 50  for SCK, SDI (MOSI) and SDO (MISO); pin 48 for SS (just because it was next to 50 and 52), and 19 for IRQ - plus of course 5V and GND.

I had to modify the jeelabs RF12 library a bit. The library expects to use either the default interrupt 0 (on pin 2), or a pin-change based interrupt on any other pin. On the Mega 2560 and ADK, four additional interrupt lines can be used. I didn't want to use IRQ 0 in case it is required for the USB host, so I used one of the additional interrupts - IRQ 4 on pin 19. To do that, I had to change the call to attach() and detach() to take an IRQ number other than 0. Pin 48 is on Port L.

Throughout RF12.cpp, I've renamed RFM_IRQ into RFM_IRQ_PIN, and then

#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)

#define RFM_IRQ 4
#define RFM_IRQ_PIN     19 // was 2
#define SS_DDR      DDRL
#define SS_PORT     PORTL
#define SS_BIT      1

#define SPI_SS      53    // PB0, pin 19
#define SPI_MOSI    51    // PB2, pin 21
#define SPI_MISO    50    // PB3, pin 22
#define SPI_SCK     52    // PB1, pin 20

 ...

#else
    if ((nodeid & NODE_ID) != 0)
        attachInterrupt(RFM_IRQ, rf12_interrupt, LOW);
    else
        detachInterrupt(RFM_IRQ);
#endif


This is successfully running the RF12demo and the pingPong demo. I still have to test how this works when both USB and wireless are active - once I can send commands from the tablet to a node connected via wireless, I've got the backbone working.



1 comment:

  1. Hi Thorsten,

    SPI is a real pain and I can't get all my devices work together (Wifly + SD + RFM12B) on a mega2560 board.

    I've tried your solution (with success if all others devices are not plugged).

    Do you have an idea ?

    Cheers.
    Vince

    ReplyDelete