From: simimeie Date: Sun, 11 Jul 2010 09:09:26 +0000 (+0000) Subject: first preparations for talking to the rfm12 module (note that nothing of X-Git-Url: http://git.rrze.uni-erlangen.de/gitweb/?a=commitdiff_plain;h=c95cbd3ffc28a8ef677f963396632ec713ac476b;p=moodlight.git first preparations for talking to the rfm12 module (note that nothing of use is implemented yet). --- diff --git a/rfm12.c b/rfm12.c new file mode 100644 index 0000000..01297e4 --- /dev/null +++ b/rfm12.c @@ -0,0 +1,43 @@ +/* $Id: rfm12.c,v 1.1 2010/07/11 09:09:26 simimeie Exp $ + * Functions for communicating with the rfm12(b) module + */ + +#include +#include "rfm12.h" + +/* Unfortunately, the manufacturer documentation for chip and module is + * almost unreadable crap. + * This one is better: http://www.mikrocontroller.net/articles/RFM12 + */ +/* Note: the signal is inverted, thus pulling this high DEselects the chip */ +#define spi_ss_hi() { PORTC |= _BV(0); } +/* Consequently, this SELECTS the chip */ +#define spi_ss_lo() { PORTC &= (uint8_t)~_BV(0); } + +static void spi_sendbyte(uint8_t v) { + /* Start transmission */ + SPDR = v; + /* Wait for transmission complete */ + while ((SPSR & _BV(SPIF)) == 0) { + } +} + +static uint8_t spi_readbyte() { + SPDR = 0x00; /* Dummy write */ + while ((SPSR & _BV(SPIF)) == 0) { + } + /* Return Data Register */ + return SPDR; +} + +void rfm12_init(void) { + /* Set MOSI, SCK and SS output, MISO input */ + DDRB |= _BV(3) | _BV(5); + DDRC |= _BV(0); + DDRB &= (uint8_t)~_BV(4); + spi_ss_hi(); + /* Enable SPI, Master, set clock rate fck/4. + * This works as long as our own clockrate is below 10 MHz - because + * 2.5 MHz is the maximum the rfm12b can do. */ + SPCR |= _BV(SPE) | _BV(MSTR); +} diff --git a/rfm12.h b/rfm12.h new file mode 100644 index 0000000..df96924 --- /dev/null +++ b/rfm12.h @@ -0,0 +1,10 @@ +/* $Id: rfm12.h,v 1.1 2010/07/11 09:09:26 simimeie Exp $ + * Functions for communicating with the rfm12(b) module + */ + +#ifndef __RFM12B_H__ +#define __RFM12B_H__ + +void rfm12_init(void); + +#endif /* __RFM12B_H__ */