76 lines
1.9 KiB
C++
76 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
|
|
*/
|
|
#define F_CPU 8000000UL
|
|
#include "../../src/mrf24j.h"
|
|
#include "../usart/src/usart.h"
|
|
#include <avr/interrupt.h>
|
|
#include <util/delay.h>
|
|
|
|
Mrf24j mrf;
|
|
|
|
void handle_rx();
|
|
void handle_tx();
|
|
|
|
int main() {
|
|
usart::init(1000000);
|
|
|
|
mrf.reset();
|
|
mrf.init();
|
|
|
|
mrf.set_pan(0xcafe);
|
|
mrf.set_bufferPHY(true);
|
|
// This is _our_ address
|
|
mrf.address16_write(0x4202);
|
|
mrf.handlers(&handle_rx, &handle_tx);
|
|
//mrf.turbo();
|
|
|
|
sei();
|
|
DDRD |= (1<<PD4) | (1<<PD5);
|
|
printf("Started");
|
|
while(1) {
|
|
//printf("txxxing...\n\r");
|
|
//mrf.send16(0x4201, "abcd");
|
|
//_delay_ms(5000);
|
|
PORTD ^= (1<<PD5);
|
|
_delay_ms(500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void handle_rx() {
|
|
PORTD |= (1<<PD4);
|
|
printf("------------------------------------------------------\n\r");
|
|
printf("received a packet %i bytes long\n\r", mrf.get_rxinfo()->frame_length);
|
|
|
|
if(mrf.get_bufferPHY()){
|
|
printf("Packet data (PHY Payload): ");
|
|
for (int i = 0; i < mrf.get_rxinfo()->frame_length; i++) {
|
|
printf("%x", mrf.get_rxbuf()[i]);
|
|
}
|
|
}
|
|
|
|
printf("\r\nASCII data (relevant data): ");
|
|
for (int i = 0; i < mrf.rx_datalength(); i++) {
|
|
usart::put(mrf.get_rxinfo()->rx_data[i]);
|
|
}
|
|
|
|
printf("LQI/RSSI=");
|
|
printf("%i/%i\n\r", mrf.get_rxinfo()->lqi, mrf.get_rxinfo()->rssi);
|
|
PORTD &= ~(1<<PD4);
|
|
}
|
|
|
|
void handle_tx() {
|
|
if (mrf.get_txinfo()->tx_ok) {
|
|
printf("TX went ok, got ack\n\r");
|
|
} else {
|
|
printf("TX failed after %i retries\n\n\r", mrf.get_txinfo()->retries);
|
|
}
|
|
}
|