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

Dependencies:   mbed

Committer:
seanburford
Date:
Thu Oct 26 21:05:49 2017 +0000
Revision:
1:17593e5a807e
Parent:
0:f443b182a97a
Changes

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 1:17593e5a807e 16 Serial pc(USBTX, USBRX);
seanburford 1:17593e5a807e 17
seanburford 0:f443b182a97a 18 // Local copy of MTF89X register contents.
seanburford 0:f443b182a97a 19 unsigned char regs[32];
seanburford 0:f443b182a97a 20 unsigned char regs2[32];
seanburford 0:f443b182a97a 21
seanburford 0:f443b182a97a 22 int main() {
seanburford 0:f443b182a97a 23 // Deselect chip select.
seanburford 0:f443b182a97a 24 cs_fifo = 1;
seanburford 0:f443b182a97a 25 cs_conf = 1;
seanburford 0:f443b182a97a 26
seanburford 0:f443b182a97a 27 // RGB: Red
seanburford 0:f443b182a97a 28 rled = 0.5;
seanburford 0:f443b182a97a 29 gled = 1;
seanburford 0:f443b182a97a 30 bled = 1;
seanburford 1:17593e5a807e 31
seanburford 0:f443b182a97a 32 // Setup the spi for 8 bit data mode 0.
seanburford 0:f443b182a97a 33 spi.format(8, 0);
seanburford 0:f443b182a97a 34 spi.frequency(1000000);
seanburford 0:f443b182a97a 35
seanburford 0:f443b182a97a 36 // RGB: Orange
seanburford 0:f443b182a97a 37 rled = 0.5;
seanburford 0:f443b182a97a 38 gled = 0.5;
seanburford 0:f443b182a97a 39 bled = 1;
seanburford 0:f443b182a97a 40
seanburford 0:f443b182a97a 41 // Read the MRF registers.
seanburford 0:f443b182a97a 42 const unsigned char start = 0x00; // Bit 0
seanburford 0:f443b182a97a 43 const unsigned char read = 0x40; // Bit 1
seanburford 0:f443b182a97a 44 const unsigned char stop = 0x00; // Bit t
seanburford 0:f443b182a97a 45 cs_conf = 0;
seanburford 0:f443b182a97a 46 for (unsigned char addr = 0; addr < 32; addr++) {
seanburford 0:f443b182a97a 47 spi.write(start | read | (addr << 1) | stop);
seanburford 0:f443b182a97a 48 regs[addr] = spi.write(0);
seanburford 0:f443b182a97a 49 }
seanburford 0:f443b182a97a 50 cs_conf = 1;
seanburford 0:f443b182a97a 51 wait_ms(10);
seanburford 0:f443b182a97a 52 cs_conf = 0;
seanburford 0:f443b182a97a 53 for (unsigned char addr = 0; addr < 32; addr++) {
seanburford 0:f443b182a97a 54 spi.write(start | read | (addr << 1) | stop);
seanburford 0:f443b182a97a 55 regs2[addr] = spi.write(0);
seanburford 0:f443b182a97a 56 }
seanburford 0:f443b182a97a 57 cs_conf = 1;
seanburford 0:f443b182a97a 58
seanburford 1:17593e5a807e 59 for (unsigned char addr = 0; addr < 32; addr++) {
seanburford 1:17593e5a807e 60 pc.printf("%02X A %02X B %02X\r\n", addr, regs[addr], regs2[addr]);
seanburford 1:17593e5a807e 61 }
seanburford 1:17593e5a807e 62
seanburford 0:f443b182a97a 63 // RGB: green.
seanburford 0:f443b182a97a 64 rled = 1;
seanburford 0:f443b182a97a 65 gled = 1;
seanburford 0:f443b182a97a 66 bled = 1;
seanburford 0:f443b182a97a 67 while(1) {
seanburford 0:f443b182a97a 68 gled = 0.5;
seanburford 0:f443b182a97a 69 wait(0.2);
seanburford 0:f443b182a97a 70 gled = 1;
seanburford 0:f443b182a97a 71 wait(0.2);
seanburford 0:f443b182a97a 72 }
seanburford 0:f443b182a97a 73 }