Subversion Repositories idreammicro-avr

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
67 jlesech 1
/**************************************************************************//**
2
 * \brief SPI library
3
 * \author Copyright (C) 2011  Julien Le Sech - www.idreammicro.com
4
 * \version 1.0
5
 * \date 20110315
6
 *
7
 * This file is part of the iDreamMicro library.
8
 *
9
 * This library is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program. If not, see http://www.gnu.org/licenses/
21
 ******************************************************************************/
22
 
23
/**************************************************************************//**
24
 * \file spi_slave.c
25
 ******************************************************************************/
26
 
27
/******************************************************************************
28
 * Header file inclusions.
29
 ******************************************************************************/
30
 
31
#include <spi/spi.h>
32
 
33
#include <useful/bits.h>
34
 
35
#include <avr/io.h>
36
 
37
#include <stdint.h>
38
 
39
#if defined (__AVR_ATmega128__)
40
    #include "../include/spi_atmega128.h"
41
#elif defined (__AVR_ATmega328P__) || defined (__AVR_ATmega328__)
42
    #include "../include/spi_atmega328.h"
43
#elif defined (__AVR_ATmega2560__)
44
    #include "../include/spi_atmega2560.h"
45
#endif
46
 
47
/******************************************************************************
48
 * Public function definitions.
49
 ******************************************************************************/
50
 
51
/**************************************************************************//**
52
 * \fn void spi_slave__initialize(void)
53
 *
54
 * \brief Initialize SPI in slave mode.
55
 ******************************************************************************/
56
void
57
spi_slave__initialize
58
(
59
    void
60
){
61
    // Configure pins.
62
    DDR_SPI = _BV(DD_MISO);
63
 
64
    // Enable SPI.
65
    SPCR = _BV(SPE);
66
}
67
 
68
/**************************************************************************//**
69
 * \fn uint8_t spi_slave__receive(void)
70
 *
71
 * \brief Receive data.
72
 *
73
 * \return Received data.
74
 ******************************************************************************/
75
uint8_t
76
spi_slave__receive
77
(
78
    void
79
){
80
    // Wait for previous transmission complete.
81
    while (!(SPSR & (1 << SPIF)));
82
 
83
    // Read data from SPI data register.
84
    return SPDR;
85
}