first preparations for talking to the rfm12 module (note that nothing of
[moodlight.git] / rfm12.c
1 /* $Id: rfm12.c,v 1.1 2010/07/11 09:09:26 simimeie Exp $
2  * Functions for communicating with the rfm12(b) module
3  */
4
5 #include <avr/io.h>
6 #include "rfm12.h"
7
8 /* Unfortunately, the manufacturer documentation for chip and module is
9  * almost unreadable crap.
10  * This one is better: http://www.mikrocontroller.net/articles/RFM12
11  */
12 /* Note: the signal is inverted, thus pulling this high DEselects the chip */
13 #define spi_ss_hi() { PORTC |= _BV(0); }
14 /* Consequently, this SELECTS the chip */
15 #define spi_ss_lo() { PORTC &= (uint8_t)~_BV(0); }
16
17 static void spi_sendbyte(uint8_t v) {
18   /* Start transmission */
19   SPDR = v;
20   /* Wait for transmission complete */
21   while ((SPSR & _BV(SPIF)) == 0) {
22   }
23 }
24
25 static uint8_t spi_readbyte() {
26   SPDR = 0x00; /* Dummy write */
27   while ((SPSR & _BV(SPIF)) == 0) {
28   }
29   /* Return Data Register */
30   return SPDR;
31 }
32
33 void rfm12_init(void) {
34   /* Set MOSI, SCK and SS output, MISO input */
35   DDRB |= _BV(3) | _BV(5);
36   DDRC |= _BV(0);
37   DDRB &= (uint8_t)~_BV(4);
38   spi_ss_hi();
39   /* Enable SPI, Master, set clock rate fck/4.
40    * This works as long as our own clockrate is below 10 MHz - because
41    * 2.5 MHz is the maximum the rfm12b can do. */
42   SPCR |= _BV(SPE) | _BV(MSTR);
43 }
This page took 0.055422 seconds and 3 git commands to generate.