Compare commits

..
22 Commits
Author SHA1 Message Date
jimmy 352b49ef29 Stuff 2021-08-29 21:56:16 +12:00
jimmy 744db56269 Stuff 2021-08-29 21:55:53 +12:00
Karl Palsson 445ae63ed6 Merge pull request #1 from pkarc/master
Nice Work!

New features of this merge:
* Simpler use, more internals hidden from view
* Support for MRF24J40MB modules with power amp
* Optional support for (broken?) devices with extra FCS bytes (MRF module listening to a series 1 xbee for instance)
2011-12-17 14:38:11 -08:00
Pkarc 1a92fdfc8e Added some config/init time options 2011-12-17 14:44:18 -05:00
Pkarc fe59becbba Added some config/init time options 2011-12-17 14:42:19 -05:00
Pkarc d154274100 Added some config/init time options 2011-12-17 14:38:21 -05:00
Pkarc 22973b801b Added some config/init time options. 2011-12-17 14:23:38 -05:00
Pkarc e5957bc117 Added some config/init time options. 2011-12-17 14:23:12 -05:00
Pkarc 693b3bfed3 Implements "much easier to use" as KarlP said in his blog, and some functions to get only the relatively "important data bytes 2011-12-17 01:34:31 -05:00
Pkarc ecf1342ec8 Implements "much easier to use" as KarlP said in his blog, and some functions to get only the relatively "important data bytes 2011-12-17 01:33:54 -05:00
Pkarc b60c20a61d Implements "much easier to use" as KarlP said in his blog, and some functions to get only the relatively "important data bytes 2011-12-17 01:33:24 -05:00
Pkarc 45fe536979 Implements "much easier to use" as KarlP said in his blog, and some functions to get only the relatively "important data bytes 2011-12-17 01:31:52 -05:00
Pkarc fac2f3f530 Implements "much easier to use" as KarlP said in his blog, and some functions to get only the relatively "important data bytes 2011-12-17 01:31:12 -05:00
Karl Palsson effe217111 Add more wrapper functions to hide more of the raw register accesses 2011-03-13 19:49:07 +00:00
Karl Palsson cb92166dcd Add an rx only example 2011-03-13 19:28:44 +00:00
Karl Palsson 5baab2e02c Add example TX only code 2011-03-13 19:24:16 +00:00
Karl Palsson d4ac9cc95a Fix compile error. (insufficient testing!) 2011-03-13 19:23:55 +00:00
Karl Palsson b3e7c52fa1 Put the promiscuous mode setting in setup(), not loop() 2011-03-13 18:37:35 +00:00
Karl Palsson fd58b93767 Now that we're in cpp land, remove the mrf_ prefixes from all methods.
Also, add a proper set_channel method, and a method for promiscuous mode.
2011-03-13 18:35:18 +00:00
Karl Palsson 0a2c207146 Add a simple readme 2011-03-13 17:59:32 +00:00
Karl Palsson 6c1aac9d08 Add an example to the library 2011-03-13 17:54:25 +00:00
Karl Palsson 19edd271b9 Initial basic version of my arduino mrf24j40 library 2011-03-13 17:33:58 +00:00
10 changed files with 114 additions and 98 deletions

No files matched your search

Executable → Regular
+3 -3
View File
@@ -1,3 +1,3 @@
*.hex
*.elf
*.bin
*.hex
*.elf
*.bin
-3
View File
@@ -1,3 +0,0 @@
[submodule "test/usart"]
path = test/usart
url = git@git.chch.tech:avr/usart.git
BIN
View File
Binary file not shown.
Executable → Regular
+28 -5
View File
@@ -1,5 +1,28 @@
all: build run
build:
g++ -Wall -pthread -o main test/${MODE}/main.cpp src/*.cpp -lpigpio -lrt
run:
sudo ./main
MCU?=atmega328p
F_CPU=8000000
PROG?=arduino
PORT?=COM5
CC=avr-g++
OBJCOPY=avr-objcopy
CFLAGS=-Wall -g -mmcu=${MCU} -DF_CPU=${F_CPU} -I.
TARGET=main
SRCS= src/*.cpp $(wildcard lib/*/src/*.cpp)
all: build flash
build:
${CC} ${CFLAGS} -o bin/${MODE}.bin ${SRCS} test/${MODE}/main.cpp -O2
${CC} ${CFLAGS} -o bin/${MODE}.elf ${SRCS} test/${MODE}/main.cpp -O2
${OBJCOPY} -j .text -j .data -O ihex bin/${MODE}.bin bin/${MODE}.hex
flash:
avrdude -p ${MCU} -c ${PROG} -P ${PORT} ${BAUD} -U flash:w:bin/${MODE}.hex
clean:
rm -f bin/*
term:
python3 lib/usart/term.py
avarice:
avarice --program --file bin/main.elf --part atmega32 --dragon :4242
Executable → Regular
+37 -28
View File
@@ -1,49 +1,58 @@
#include "driver.h"
#include <pigpio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
namespace driver
{
namespace {
Mrf24j *mrf;
int spi;
}
void callback(int gpio, int level, uint32_t tick) {
mrf->interrupt_handler();
mrf->check_flags();
}
void init(Mrf24j *mrf) {
gpioInitialise();
driver::mrf = mrf;
spi = spiOpen(0, 1000000, 0);
gpioSetMode(RESET, PI_OUTPUT);
gpioWrite(RESET, 1);
gpioSetMode(SPI_CS, PI_OUTPUT);
cs_high();
gpioSetAlertFunc(INT, callback);
SPI_PORT |= (1<<SPI_CS); //set chip select pin high
SPI_DDR |= (1<<SPI_SCK)|(1<<SPI_MOSI)|(1<<SPI_CS); // spi sck mosi and chip select outputs
RESET_PORT |= (1<<RESET);
RESET_DDR |= (1<<RESET);
SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR0); //enable SPI , Master, fck/16
#ifdef WAKE_PORT
WAKE_PORT &= ~(1<<WAKE_PIN);
WAKE_DDR |= (1<<WAKE_PIN);
#endif
#if defined __AVR_ATmega328P__
EICRA |= (1<<ISC01);
EIMSK |= (1<<INT0);
SPSR |= (1<< SPI2X); // FOSC/2
#elif defined __AVR_ATmega32__
GICR |= (1<<INT2);
#endif
sei();
}
uint8_t transfer(uint8_t data) {
char txbuff[1], rxbuff[1];
txbuff[0] = data;
spiXfer(spi, txbuff, rxbuff, 1);
return rxbuff[0];
SPDR = data;
while(!(SPSR & (1<<SPIF))); //wait for transmition to complete
return SPDR;
}
void reset() {
gpioWrite(RESET , 0);
time_sleep(0.01);
gpioWrite(RESET, 1);
time_sleep(0.02);
RESET_PORT &= ~(1<<RESET);
_delay_ms(10);
RESET_PORT |= (1<<RESET);
_delay_ms(20);
}
void wake() {
#ifdef WAKE_PORT
WAKE_PORT |= (1<<WAKE_PIN);
_delay_ms(1);
WAKE_PORT &= ~(1<<WAKE_PIN);
#endif
}
// INTERRUPT {
// mrf->interrupt_handler(); // mrf24 object interrupt routine
// mrf->check_flags();
// }
INTERRUPT {
mrf->interrupt_handler(); // mrf24 object interrupt routine
mrf->check_flags();
}
}
Executable → Regular
+3 -10
View File
@@ -1,7 +1,7 @@
#pragma once
#include <avr/io.h>
#include "mrf24j.h"
#include <pigpio.h>
#if defined __AVR_ATmega328P__
@@ -27,26 +27,19 @@
#define RESET_DDR DDRB
#define RESET_PORT PORTB
#define RESET PB0
#define WAKE_PORT PORTB
#define WAKE_DDR DDRB
#define WAKE_PIN PB1
#define INTERRUPT ISR(INT2_vect)
#elif defined RASPBERRYPI
#endif
#define RESET 24 //24
#define SPI_CS 8 //8
#define INT 2 //25
namespace driver
{
void init(Mrf24j *mrf);
uint8_t transfer(uint8_t data);
inline void cs_low() {gpioWrite(SPI_CS, 0);}
inline void cs_low() {SPI_PORT &= ~(1<<SPI_CS);}
inline void cs_high() {gpioWrite(SPI_CS, 1);}
inline void cs_high() {SPI_PORT |= (1<<SPI_CS);}
void wake();
void reset();
} // namespace spi
Executable → Regular
+8 -11
View File
@@ -6,8 +6,8 @@
#include "mrf24j.h"
#include "driver.h"
#include <string.h>
#include <pigpio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
// aMaxPHYPacketSize = 127, from the 802.15.4-2006 standard.
static uint8_t rx_buf[127];
@@ -104,17 +104,12 @@ word Mrf24j::address16_read(void) {
return a16h << 8 | read_short(MRF_SADRL);
}
void Mrf24j::send_str(word dest16, char * data) {
byte len = strlen(data); // get the length of the char* array
send(dest16, data, len);
}
/**
* Simple send 16, with acks, not much of anything.. assumes src16 and local pan only.
* @param data
*/
void Mrf24j::send(word dest16, char * data, int len) {
void Mrf24j::send16(word dest16, char * data) {
byte len = strlen(data); // get the length of the char* array
int i = 0;
write_long(i++, bytes_MHR); // header length
// +ignoreBytes is because some module seems to ignore 2 bytes after the header?!.
@@ -189,7 +184,7 @@ void Mrf24j::init(void) {
// Set transmitter power - See “REGISTER 2-62: RF CONTROL 3 REGISTER (ADDRESS: 0x203)”.
write_short(MRF_RFCTL, 0x04); // – Reset RF state machine.
write_short(MRF_RFCTL, 0x00); // part 2
gpioSleep(0, 0, 1000); // delay at least 192usec
_delay_ms(1); // delay at least 192usec
}
/**
@@ -203,6 +198,7 @@ void Mrf24j::interrupt_handler(void) {
if (last_interrupt & MRF_I_RXIF) {
flag_got_rx++;
// read out the packet data...
cli();
rx_disable();
// read start of rxfifo for, has 2 bytes more added by FCS. frame_length = m + n + 2
uint8_t frame_length = read_long(0x300);
@@ -229,6 +225,7 @@ void Mrf24j::interrupt_handler(void) {
rx_info.rssi = read_long(0x301 + frame_length + 1);
rx_enable();
sei();
}
if (last_interrupt & MRF_I_TXNIF) {
flag_got_tx++;
@@ -336,7 +333,7 @@ void Mrf24j::wake() {
driver::wake();
write_short(MRF_RFCTL, 0x04); // RF State Machine reset
write_short(MRF_RFCTL, 0x00);
gpioSleep(0, 0 ,2000); // Delay 2 ms to allow 20 MHz main oscillator time to stabilize before transmitting or receiving.
_delay_ms(2); // Delay 2 ms to allow 20 MHz main oscillator time to stabilize before transmitting or receiving.
}
void Mrf24j::turbo() {
Executable → Regular
+5 -5
View File
@@ -3,10 +3,11 @@
* copyright Karl Palsson, karlp@tweak.net.au, 2011
* modified BSD License / apache license
*/
typedef unsigned char uint8_t;
typedef unsigned char byte;
typedef unsigned int word;
#include <avr/io.h>
typedef uint8_t byte;
typedef uint16_t word;
typedef bool boolean;
#ifndef LIB_MRF24J_H
@@ -226,8 +227,7 @@ class Mrf24j
*/
void set_palna(boolean enabled);
void send(word dest16, char * data, int len);
void send_str(word dest16, char * data);
void send16(word dest16, char * data);
void interrupt_handler(void);
Executable → Regular
+16 -14
View File
@@ -8,8 +8,9 @@
*/
#define F_CPU 8000000UL
#include "../../src/mrf24j.h"
#include "../usart/src/usart.h"
#include <pigpio.h>
#include "../../lib/usart/src/usart.h"
#include <avr/interrupt.h>
#include <util/delay.h>
Mrf24j mrf;
@@ -17,6 +18,8 @@ void handle_rx();
void handle_tx();
int main() {
usart::init(1000000);
mrf.reset();
mrf.init();
@@ -25,35 +28,34 @@ int main() {
// This is _our_ address
mrf.address16_write(0x4202);
mrf.handlers(&handle_rx, &handle_tx);
//mrf.turbo();
mrf.turbo();
printf("Started");
sei();
while(1) {
printf("Hello\n\r");
_delay_ms(2000);
//printf("txxxing...\n\r");
//mrf.send16(0x4201, "abcd");
_delay_ms(5000);
}
}
void handle_rx() {
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): ");
printf("Packet data (PHY Payload):\n\r");
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++) {
putchar(mrf.get_rxinfo()->rx_data[i]);
}
// printf("\r\nASCII data (relevant data):\n\r");
// for (int i = 0; i < mrf.rx_datalength(); i++) {
// usart::put(mrf.get_rxinfo()->rx_data[i]);
// }
printf("LQI/RSSI=");
printf("\r\nLQI/RSSI=");
printf("%i/%i\n\r", mrf.get_rxinfo()->lqi, mrf.get_rxinfo()->rssi);
}
Executable → Regular
+14 -19
View File
@@ -9,45 +9,40 @@
#define F_CPU 8000000UL
#include "../../src/mrf24j.h"
#include "../../lib/usart/src/usart.h"
#include "../../src/driver.h"
#include <stdio.h>
#include <pigpio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
Mrf24j mrf;
int i = 0;
void handle_rx();
void handle_tx();
int main() {
int main() {
usart::init(1000000);
mrf.reset();
mrf.init();
time_sleep(1);
mrf.set_pan(0xcafe);
// This is _our_ address
mrf.address16_write(0x4201);
mrf.handlers(&handle_rx, &handle_tx);
printf("Pan id: %x\n\r", mrf.get_pan());
//mrf.enable_wake();
//mrf.set_bufferPHY(true);
//mrf.turbo();
//mrf.set_power(0b1100000);
mrf.set_bufferPHY(true);
mrf.turbo();
mrf.set_power(0b1100000);
//mrf.sleep();
printf("Started\n\r");
char tmp[20];
sei();
while(1) {
printf("txxxing... %i\n\r", i++);
printf("txxxing...\n\r");
//mrf.wake();
mrf.send16(0x4202, "Tx Hello");
sprintf(tmp,"Received %i\n\r", i);
mrf.send_str(0x4202, tmp);
time_sleep(2);
_delay_ms(2000);
}
}
@@ -74,7 +69,7 @@ void handle_rx() {
void handle_tx() {
if (mrf.get_txinfo()->tx_ok) {
printf("TX went ok, got ack %i\n\r", i);
printf("TX went ok, got ack\n\r");
} else {
printf("TX failed after %i retries\n\n\r", mrf.get_txinfo()->retries);
}