Blame |
Last modification |
View Log
| Download
| RSS feed
/**************************************************************************//**
* \brief SPI library
* \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com
* \version 1.0
* \date 20110315
*
* This file is part of the iDreamMicro library.
*
* This library is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
******************************************************************************/
/**************************************************************************//**
* \file spi.c
******************************************************************************/
/******************************************************************************
* Header file inclusions.
******************************************************************************/
#include <spi/spi.h>
#include <useful/bits.h>
#include <avr/io.h>
#include <stdint.h>
#if defined (__AVR_ATmega128__)
#include "../include/spi_atmega128.h"
#elif defined (__AVR_ATmega328P__) || defined (__AVR_ATmega328__)
#include "../include/spi_atmega328.h"
#elif defined (__AVR_ATmega2560__)
#include "../include/spi_atmega2560.h"
#endif
/******************************************************************************
* Public function definitions.
******************************************************************************/
/**************************************************************************//**
* \fn void spi__set_clock_rate(spi__clock_rate_t clock_rate)
*
* \brief Set clock rate.
*
* \param[in] clock_rate Clock rate to set.
******************************************************************************/
void
spi__set_clock_rate
(
spi__clock_rate_t clock_rate
){
switch (clock_rate)
{
case SPI__CLOCK_RATE__FOSC_OVER_2:
{
BIT__SET(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__RST(SPCR, SPR0);
}
break;
case SPI__CLOCK_RATE__FOSC_OVER_4:
{
BIT__RST(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__RST(SPCR, SPR0);
}
break;
case SPI__CLOCK_RATE__FOSC_OVER_8:
{
BIT__SET(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
case SPI__CLOCK_RATE__FOSC_OVER_16:
{
BIT__RST(SPCR, SPI2X);
BIT__RST(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
case SPI__CLOCK_RATE__FOSC_OVER_32:
{
BIT__SET(SPCR, SPI2X);
BIT__SET(SPCR, SPR1);
BIT__RST(SPCR, SPR0);
}
break;
case SPI__CLOCK_RATE__FOSC_OVER_64:
{
BIT__SET(SPCR, SPI2X);
BIT__SET(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
case SPI__CLOCK_RATE__FOSC_OVER_128:
{
BIT__RST(SPCR, SPI2X);
BIT__SET(SPCR, SPR1);
BIT__SET(SPCR, SPR0);
}
break;
default:
break;
}
}
/**************************************************************************//**
* \fn void spi__set_data_mode(spi__data_mode_t mode)
*
* \brief Set SPI data mode.
*
* \param[in] mode Data mode to set.
******************************************************************************/
void
spi__set_data_mode
(
spi__data_mode_t mode
){
switch (mode)
{
case SPI__DATA_MODE_0:
{
BIT__RST(SPCR, CPOL);
BIT__RST(SPCR, CPHA);
}
break;
case SPI__DATA_MODE_1:
{
BIT__RST(SPCR, CPOL);
BIT__SET(SPCR, CPHA);
}
break;
case SPI__DATA_MODE_2:
{
BIT__SET(SPCR, CPOL);
BIT__RST(SPCR, CPHA);
}
break;
case SPI__DATA_MODE_3:
{
BIT__SET(SPCR, CPOL);
BIT__SET(SPCR, CPHA);
}
break;
default:
break;
}
}
/**************************************************************************//**
* \fn void spi__set_data_order(spi__data_order_t data_order)
*
* \brief Set SPI data order.
*
* \param[in] data_order Data order to set.
******************************************************************************/
void
spi__set_data_order
(
spi__data_order_t data_order
){
if (SPI__DATA_ORDER__LSB_FIRST == data_order)
{
BIT__SET(SPCR, DORD);
}
else
{
BIT__RST(SPCR, DORD);
}
}