83 lines
1.9 KiB
C++
83 lines
1.9 KiB
C++
/**
|
|
* Example code for using a microchip mrf24j40 module to send and receive
|
|
* packets using plain 802.15.4
|
|
* Requirements: 3 pins for spi, 3 pins for reset, chip select and interrupt
|
|
* notifications
|
|
* This example file is considered to be in the public domain
|
|
* Originally written by Karl Palsson, karlp@tweak.net.au, March 2011
|
|
*/
|
|
|
|
|
|
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include "../../lib/mrf24j.h"
|
|
#include "../../lib/usart.h"
|
|
#include "../../lib/spi.h"
|
|
|
|
const int pin_reset = 5;
|
|
const int pin_cs = 10; // default CS pin on ATmega8/168/328
|
|
const int pin_interrupt = 2; // default interrupt pin on ATmega8/168/328
|
|
|
|
Mrf24j mrf;
|
|
|
|
void handle_rx();
|
|
void handle_tx();
|
|
|
|
|
|
int main() {
|
|
usart::init(50000);
|
|
spi::init();
|
|
//s.init();
|
|
mrf.reset();
|
|
mrf.init();
|
|
|
|
mrf.set_pan(0xcafe);
|
|
// This is _our_ address
|
|
mrf.address16_write(0x4202);
|
|
|
|
// uncomment if you want to receive any packet on this channel
|
|
//mrf.set_promiscuous(true);
|
|
|
|
// uncomment if you want to enable PA/LNA external control
|
|
//mrf.set_palna(true);
|
|
|
|
// uncomment if you want to buffer all PHY Payload
|
|
//mrf.set_bufferPHY(true);
|
|
|
|
MCUCSR |= (1<<ISC00); // INT2 falling edge
|
|
GICR |= (1<<INT0);
|
|
|
|
sei();
|
|
DDRA |= (1<<PA0);
|
|
|
|
while(1) {
|
|
mrf.check_flags(&handle_rx, &handle_tx);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
ISR(INT0_vect) {
|
|
mrf.interrupt_handler(); // mrf24 object interrupt routine
|
|
}
|
|
|
|
|
|
void handle_rx() {
|
|
PORTA |= (1<<PA0);
|
|
printf("Received a packet %i bytes long\n\n", mrf.get_rxinfo()->frame_length);
|
|
|
|
printf("\r\nASCII data (relevant data):\n");
|
|
for (int i = 0; i < mrf.rx_datalength(); i++) {
|
|
printf("%c", mrf.get_rxinfo()->rx_data[i]);
|
|
}
|
|
printf("\n\n");
|
|
// Serial.print("\r\nLQI/RSSI=");
|
|
// Serial.print(mrf.get_rxinfo()->lqi, DEC);
|
|
// Serial.print("/");
|
|
// Serial.println(mrf.get_rxinfo()->rssi, DEC);
|
|
PORTA &= ~(1<<PA0);
|
|
}
|
|
|
|
void handle_tx() {
|
|
|
|
} |