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_master.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_master__initialize(void)
53
 *
54
 * \brief Initialize SPI in master mode.
55
 ******************************************************************************/
56
void
57
spi_master__initialize
58
(
59
    void
60
){
61
    // Configure pins.
62
    DDR_SPI |= _BV(DD_MOSI) | _BV(DD_SCK);
63
 
64
    // Enable SPI and set master mode.
65
    SPCR = _BV(SPE) | _BV(MSTR);
66
}
67
 
68
/**************************************************************************//**
69
 * \fn void spi_master__transmit(uint8_t data)
70
 *
71
 * \brief Transmit data.
72
 *
73
 * \param   data    Data to transmit.
74
 ******************************************************************************/
75
void
76
spi_master__transmit
77
(
78
    uint8_t data
79
){
80
    // Write data to SPI data register.
81
    SPDR = data;
82
 
83
    // Wait for transmission complete.
84
    while (!(SPSR & (1 << SPIF)));
85
}