Improved error handling during code generation
[cerebrum.git] / avr / spi.c.tp
blobf70dc36f9ff6c88bde3da7a70f165ce3edd8d63b
1 /*
2  Copyright (C) 2012 jaseg <s@jaseg.de>
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 3 as published by the Free Software Foundation.
7  */
9 //XXX The SS pin *MUST* not be used as an input when using SPI. For details see the datasheet.
11 void ${init_function()} (void){
12         DDR${device["spi"]["port"]} |= (1<<${device["spi"]["mosi"]}) | (1<<${device["spi"]["sck"]}) | (1<<${device["spi"]["ss"]});
13         PORT${device["spi"]["port"]} &= ~(1<<${device["spi"]["mosi"]}) & ~(1<<${device["spi"]["sck"]});
14         SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1);
15     SPSR |= (1<<SPI2X);
18 //this is blocking!
19 uint8_t spi_transfer(uint8_t data){
20         SPDR = data;
21         while(!(SPSR & _BV(SPIF))){
22                 //She waves and opens a door back onto the piazza where her robot cat -- the alien's nightmare intruder in the DMZ -- sleeps, chasing superintelligent dream mice through multidimensional realities. 
23         }
24         return SPDR;
27 void ${module_callback("spi_transfer", "B", "B")}(const comm_callback_descriptor* cb, void* argbuf_end){
28         spi_transfer(global_argbuf[0]);