Using SPI to talk to the MRF89X radio module (on an MRF89XAM9A board).

Dependencies:   mbed

Committer:
seanburford
Date:
Tue Sep 19 06:03:20 2017 +0000
Revision:
0:f443b182a97a
Child:
1:17593e5a807e
Initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seanburford 0:f443b182a97a 1 #include "mbed.h"
seanburford 0:f443b182a97a 2
seanburford 0:f443b182a97a 3 // SPI to the MRF89X.
seanburford 0:f443b182a97a 4 SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk
seanburford 0:f443b182a97a 5 DigitalOut cs_fifo(PTC12);
seanburford 0:f443b182a97a 6 DigitalOut cs_conf(PTC17);
seanburford 0:f443b182a97a 7 DigitalIn mrf_reset(PTA16); // Hi-Z
seanburford 0:f443b182a97a 8 DigitalIn mrf_irq0(PTC16);
seanburford 0:f443b182a97a 9 DigitalIn mrf_irq1(PTC13);
seanburford 0:f443b182a97a 10
seanburford 0:f443b182a97a 11 // RGB LED.
seanburford 0:f443b182a97a 12 PwmOut rled(LED_RED);
seanburford 0:f443b182a97a 13 PwmOut gled(LED_GREEN);
seanburford 0:f443b182a97a 14 PwmOut bled(LED_BLUE);
seanburford 0:f443b182a97a 15
seanburford 0:f443b182a97a 16 // Local copy of MTF89X register contents.
seanburford 0:f443b182a97a 17 unsigned char regs[32];
seanburford 0:f443b182a97a 18 unsigned char regs2[32];
seanburford 0:f443b182a97a 19
seanburford 0:f443b182a97a 20 int main() {
seanburford 0:f443b182a97a 21 // Deselect chip select.
seanburford 0:f443b182a97a 22 cs_fifo = 1;
seanburford 0:f443b182a97a 23 cs_conf = 1;
seanburford 0:f443b182a97a 24
seanburford 0:f443b182a97a 25 // RGB: Red
seanburford 0:f443b182a97a 26 rled = 0.5;
seanburford 0:f443b182a97a 27 gled = 1;
seanburford 0:f443b182a97a 28 bled = 1;
seanburford 0:f443b182a97a 29
seanburford 0:f443b182a97a 30 // Setup the spi for 8 bit data mode 0.
seanburford 0:f443b182a97a 31 spi.format(8, 0);
seanburford 0:f443b182a97a 32 spi.frequency(1000000);
seanburford 0:f443b182a97a 33
seanburford 0:f443b182a97a 34 // RGB: Orange
seanburford 0:f443b182a97a 35 rled = 0.5;
seanburford 0:f443b182a97a 36 gled = 0.5;
seanburford 0:f443b182a97a 37 bled = 1;
seanburford 0:f443b182a97a 38
seanburford 0:f443b182a97a 39 // Read the MRF registers.
seanburford 0:f443b182a97a 40 const unsigned char start = 0x00; // Bit 0
seanburford 0:f443b182a97a 41 const unsigned char read = 0x40; // Bit 1
seanburford 0:f443b182a97a 42 const unsigned char stop = 0x00; // Bit t
seanburford 0:f443b182a97a 43 cs_conf = 0;
seanburford 0:f443b182a97a 44 for (unsigned char addr = 0; addr < 32; addr++) {
seanburford 0:f443b182a97a 45 spi.write(start | read | (addr << 1) | stop);
seanburford 0:f443b182a97a 46 regs[addr] = spi.write(0);
seanburford 0:f443b182a97a 47 }
seanburford 0:f443b182a97a 48 cs_conf = 1;
seanburford 0:f443b182a97a 49 wait_ms(10);
seanburford 0:f443b182a97a 50 cs_conf = 0;
seanburford 0:f443b182a97a 51 for (unsigned char addr = 0; addr < 32; addr++) {
seanburford 0:f443b182a97a 52 spi.write(start | read | (addr << 1) | stop);
seanburford 0:f443b182a97a 53 regs2[addr] = spi.write(0);
seanburford 0:f443b182a97a 54 }
seanburford 0:f443b182a97a 55 cs_conf = 1;
seanburford 0:f443b182a97a 56
seanburford 0:f443b182a97a 57 // RGB: green.
seanburford 0:f443b182a97a 58 rled = 1;
seanburford 0:f443b182a97a 59 gled = 1;
seanburford 0:f443b182a97a 60 bled = 1;
seanburford 0:f443b182a97a 61 while(1) {
seanburford 0:f443b182a97a 62 gled = 0.5;
seanburford 0:f443b182a97a 63 wait(0.2);
seanburford 0:f443b182a97a 64 gled = 1;
seanburford 0:f443b182a97a 65 wait(0.2);
seanburford 0:f443b182a97a 66 }
seanburford 0:f443b182a97a 67 }