- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# spi_drv.h

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00015  *
    00016  * Redistribution and use in source and binary forms, with or without
    00017  * modification, are permitted provided that the following conditions are met:
    00018  *
    00019  * 1. Redistributions of source code must retain the above copyright notice,
    00020  * this list of conditions and the following disclaimer.
    00021  *
    00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00023  * this list of conditions and the following disclaimer in the documentation
    00024  * and/or other materials provided with the distribution.
    00025  *
    00026  * 3. The name of Atmel may not be used to endorse or promote products derived
    00027  * from this software without specific prior written permission.
    00028  *
    00029  * 4. This software may only be redistributed and used in connection with an Atmel
    00030  * AVR product.
    00031  *
    00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00042  */
    00043
    00044 #ifndef _SPI_DRV_H_
    00045 #define _SPI_DRV_H_
    00046
    00047 //_____ I N C L U D E S  ___________________________
    00048
    00049 #include "config.h"
    00050
    00051
    00052 //_____ M A C R O S  ___________________________
    00053
    00054 //----- Modes -----
    00055 #define  MSK_SPI_MODE            ((1<<CPOL)|(1<<CPHA))
    00056 #define  MSK_SPI_CPHA_LEADING    (0<<CPHA)
    00057 #define  MSK_SPI_CPHA_TRAILING   (1<<CPHA)
    00058 #define  MSK_SPI_CPOL_LOW        (0<<CPOL)
    00059 #define  MSK_SPI_CPOL_HIGH       (1<<CPOL)
    00060 #define  SPI_MODE_0              (MSK_SPI_CPOL_LOW|MSK_SPI_CPHA_LEADING)
    00061 #define  SPI_MODE_1              (MSK_SPI_CPOL_LOW|MSK_SPI_CPHA_TRAILING)
    00062 #define  SPI_MODE_2              (MSK_SPI_CPOL_HIGH|MSK_SPI_CPHA_LEADING)
    00063 #define  SPI_MODE_3              (MSK_SPI_CPOL_HIGH|MSK_SPI_CPHA_TRAILING)
    00064
    00065 //----- Bit rates -----
    00066 #define  MSK_SPI_MULT2           0x80
    00067 #define  MSK_SPR                 ((1<<SPR1)|(1<<SPR0))
    00068 #define  MSK_SPI_DIV4            ((0<<SPR1)|(0<<SPR0))
    00069 #define  MSK_SPI_DIV16           ((0<<SPR1)|(1<<SPR0))
    00070 #define  MSK_SPI_DIV64           ((1<<SPR1)|(0<<SPR0))
    00071 #define  MSK_SPI_DIV128          ((1<<SPR1)|(1<<SPR0))
    00072 #define  SPI_RATE_0              (MSK_SPI_MULT2|MSK_SPI_DIV4)     // Fper / 2
    00073 #define  SPI_RATE_1              (MSK_SPI_DIV4)                   // Fper / 4
    00074 #define  SPI_RATE_2              (MSK_SPI_MULT2|MSK_SPI_DIV16)    // Fper / 8
    00075 #define  SPI_RATE_3              (MSK_SPI_DIV16)                  // Fper / 16
    00076 #define  SPI_RATE_4              (MSK_SPI_MULT2|MSK_SPI_DIV64)    // Fper / 32
    00077 #define  SPI_RATE_5              (MSK_SPI_DIV64)                  // Fper / 64
    00078 #define  SPI_RATE_6              (MSK_SPI_DIV128)                 // Fper / 128
    00079
    00080
    00081 //______  D E F I N I T I O N  ___________________________
    00082
    00083 // Global enable
    00084 #define  Spi_enable()            (SPCR |=  (1<<SPE))
    00085 #define  Spi_disable()           (SPCR &= ~(1<<SPE))
    00086
    00087 // Interrupt configuration
    00088 #define  Spi_enable_it()         (SPCR |=  (1<<SPIE))
    00089 #define  Spi_disable_it()        (SPCR &= ~(1<<SPIE))
    00090
    00091 // SPI Configuration : slave/master, modes (Clock Phase, Clock Polarity), Data Order, speed
    00092 #define  Spi_select_slave()      (SPCR &= ~(1<<MSTR))
    00093 #define  Spi_select_master()     (SPCR |=  (1<<MSTR))
    00094 #define  Spi_set_lsbfirst()      (SPCR |=  (1<<DORD))
    00095 #define  Spi_set_msbfirst()      (SPCR &= ~(1<<DORD))
    00096 #define  Spi_set_mode(mode)      {SPCR &= ~MSK_SPI_MODE; SPCR |= mode;}
    00097 #define  Spi_set_rate(rate)      {SPCR &= ~MSK_SPR; SPCR |= rate&MSK_SPR; (rate & MSK_SPI_MULT2)?  Spi_set_doublespeed() : Spi_clear_doublespeed();}
    00098 #define  Spi_set_doublespeed()   (SPSR |=  (1<<SPI2X))
    00099 #define  Spi_clear_doublespeed() (SPSR &= ~(1<<SPI2X))
    00100 #define  Spi_init_bus()          ((DDRB |= (1<<DDB2)|(1<<DDB1)))
    00101 #define  Spi_disable_ss()
    00102 #define  Spi_enble_ss()
    00103
    00104 // SPI Control
    00105 #define  Spi_wait_spif()         while ((SPSR & (1<<SPIF)) == 0)     // for any SPI_RATE_x
    00106 #define  Spi_wait_eor()          while ((SPSR & (1<<SPIF)) == 0)     // wait end of reception
    00107 #define  Spi_wait_eot()          while ((SPSR & (1<<SPIF)) == 0)     // wait end of transmission
    00108 #define  Spi_eor()               ((SPSR & (1<<SPIF)) == (1<<SPIF))   // check end of reception
    00109 #define  Spi_eot()               ((SPSR & (1<<SPIF)) == (1<<SPIF))   // check end of transmission
    00110 #define  Spi_is_colision()       (SPSR&(1<<WCOL))
    00111 #define  Spi_get_byte()          (SPDR)
    00112 #define  Spi_tx_ready()          (SPSR & (1<<SPIF))
    00113 #define  Spi_rx_ready()          Spi_tx_ready()
    00114 #define  Spi_ack_read()          (SPSR)
    00115 #define  Spi_ack_write()         (SPDR)
    00116 #define  Spi_ack_cmd()           (SPSR)
    00117
    00118 // SPI transfer
    00119 #define  Spi_read_data()         (SPDR)
    00120 #define  Spi_write_data(byte)    {(SPDR=byte);Spi_wait_spif();}
    00121
    00122 #endif  // _SPI_DRV_H_
    00123