Compare commits

...

No commits in common. "master" and "pi" have entirely different histories.
master ... pi

11 changed files with 65 additions and 115 deletions

6
.gitignore vendored Normal file → Executable file
View File

@ -1,3 +1,3 @@
*.hex
*.elf
*.bin
*.hex
*.elf
*.bin

6
.gitmodules vendored Normal file → Executable file
View File

@ -1,3 +1,3 @@
[submodule "test/usart"]
path = test/usart
url = git@git.chch.tech:avr/usart.git
[submodule "test/usart"]
path = test/usart
url = git@git.chch.tech:avr/usart.git

BIN
main Executable file

Binary file not shown.

40
makefile Normal file → Executable file
View File

@ -1,35 +1,5 @@
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= $(wildcard src/*.cpp) test/usart/src/usart.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
fuse:
avrdude -p ${MCU} -c ${PROG} -P ${PORT} -U lfuse:w:0xe4:m -U hfuse:w:0xd2:m
lock:
avrdude -p ${MCU} -c ${PROG} -P ${PORT} -U lock:w:0xC0:m
all: build run
build:
g++ -Wall -pthread -o main test/${MODE}/main.cpp src/*.cpp -lpigpio -lrt
run:
sudo ./main

67
src/driver.cpp Normal file → Executable file
View File

@ -1,58 +1,49 @@
#include "driver.h"
#include <avr/interrupt.h>
#include <util/delay.h>
#include <pigpio.h>
namespace driver
{
namespace {
Mrf24j *mrf;
int spi;
}
void init(Mrf24j *mrf) {
driver::mrf = mrf;
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();
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);
}
uint8_t transfer(uint8_t data) {
SPDR = data;
while(!(SPSR & (1<<SPIF))); //wait for transmition to complete
return SPDR;
char txbuff[1], rxbuff[1];
txbuff[0] = data;
spiXfer(spi, txbuff, rxbuff, 1);
return rxbuff[0];
}
void reset() {
RESET_PORT &= ~(1<<RESET);
_delay_ms(10);
RESET_PORT |= (1<<RESET);
_delay_ms(20);
gpioWrite(RESET , 0);
time_sleep(0.01);
gpioWrite(RESET, 1);
time_sleep(0.02);
}
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();
// }
}

10
src/driver.h Normal file → Executable file
View File

@ -1,7 +1,7 @@
#pragma once
#include <avr/io.h>
#include "mrf24j.h"
#include <pigpio.h>
#if defined __AVR_ATmega328P__
@ -35,14 +35,18 @@
#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() {SPI_PORT &= ~(1<<SPI_CS);}
inline void cs_low() {gpioWrite(SPI_CS, 0);}
inline void cs_high() {SPI_PORT |= (1<<SPI_CS);}
inline void cs_high() {gpioWrite(SPI_CS, 1);}
void wake();
void reset();
} // namespace spi

10
src/mrf24j.cpp Normal file → Executable file
View File

@ -6,8 +6,8 @@
#include "mrf24j.h"
#include "driver.h"
#include <string.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <pigpio.h>
// aMaxPHYPacketSize = 127, from the 802.15.4-2006 standard.
static uint8_t rx_buf[127];
@ -189,7 +189,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
_delay_ms(1); // delay at least 192usec
gpioSleep(0, 0, 1000); // delay at least 192usec
}
/**
@ -203,7 +203,6 @@ 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);
@ -230,7 +229,6 @@ 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++;
@ -338,7 +336,7 @@ void Mrf24j::wake() {
driver::wake();
write_short(MRF_RFCTL, 0x04); // RF State Machine reset
write_short(MRF_RFCTL, 0x00);
_delay_ms(2); // Delay 2 ms to allow 20 MHz main oscillator time to stabilize before transmitting or receiving.
gpioSleep(0, 0 ,2000); // Delay 2 ms to allow 20 MHz main oscillator time to stabilize before transmitting or receiving.
}
void Mrf24j::turbo() {

7
src/mrf24j.h Normal file → Executable file
View File

@ -3,11 +3,10 @@
* copyright Karl Palsson, karlp@tweak.net.au, 2011
* modified BSD License / apache license
*/
typedef unsigned char uint8_t;
#include <avr/io.h>
typedef uint8_t byte;
typedef uint16_t word;
typedef unsigned char byte;
typedef unsigned int word;
typedef bool boolean;
#ifndef LIB_MRF24J_H

19
test/rx/main.cpp Normal file → Executable file
View File

@ -9,8 +9,7 @@
#define F_CPU 8000000UL
#include "../../src/mrf24j.h"
#include "../usart/src/usart.h"
#include <avr/interrupt.h>
#include <util/delay.h>
#include <pigpio.h>
Mrf24j mrf;
@ -18,8 +17,6 @@ void handle_rx();
void handle_tx();
int main() {
usart::init(1000000);
mrf.reset();
mrf.init();
@ -30,22 +27,17 @@ int main() {
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);
printf("Hello\n\r");
_delay_ms(2000);
}
}
void handle_rx() {
PORTD |= (1<<PD4);
printf("------------------------------------------------------\n\r");
printf("received a packet %i bytes long\n\r", mrf.get_rxinfo()->frame_length);
@ -58,12 +50,11 @@ void handle_rx() {
printf("\r\nASCII data (relevant data): ");
for (int i = 0; i < mrf.rx_datalength(); i++) {
usart::put(mrf.get_rxinfo()->rx_data[i]);
putchar(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() {

14
test/tx/main.cpp Normal file → Executable file
View File

@ -9,11 +9,9 @@
#define F_CPU 8000000UL
#include "../../src/mrf24j.h"
#include "../usart/src/usart.h"
#include "../../src/driver.h"
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <pigpio.h>
Mrf24j mrf;
@ -22,23 +20,23 @@ int i = 0;
void handle_rx();
void handle_tx();
int main() {
usart::init(1000000);
int main() {
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.sleep();
sei();
printf("Started\n\r");
char tmp[20];
@ -49,7 +47,7 @@ int main() {
sprintf(tmp,"Received %i\n\r", i);
mrf.send_str(0x4202, tmp);
_delay_ms(2000);
time_sleep(2);
}
}

@ -1 +0,0 @@
Subproject commit 05034f925671f06abe15097326b05eceeef0bc1e