--- /dev/null
+/* $Id: rfm12.c,v 1.1 2010/07/11 09:09:26 simimeie Exp $
+ * Functions for communicating with the rfm12(b) module
+ */
+
+#include <avr/io.h>
+#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);
+}