You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
1.5 KiB

#include "driver.h"
#include <avr/interrupt.h>
#include <util/delay.h>
namespace driver
{
namespace {
Mrf24j *mrf;
}
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();
}
uint8_t transfer(uint8_t data) {
SPDR = data;
while(!(SPSR & (1<<SPIF))); //wait for transmition to complete
return SPDR;
}
void reset() {
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();
}
}