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.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__set_clock_rate(spi__clock_rate_t clock_rate) |
||
| 53 | * |
||
| 54 | * \brief Set clock rate. |
||
| 55 | * |
||
| 56 | * \param[in] clock_rate Clock rate to set. |
||
| 57 | ******************************************************************************/ |
||
| 58 | void |
||
| 59 | spi__set_clock_rate |
||
| 60 | ( |
||
| 61 | spi__clock_rate_t clock_rate |
||
| 62 | ){ |
||
| 63 | switch (clock_rate) |
||
| 64 | { |
||
| 65 | case SPI__CLOCK_RATE__FOSC_OVER_2: |
||
| 66 | { |
||
| 67 | BIT__SET(SPCR, SPI2X); |
||
| 68 | BIT__RST(SPCR, SPR1); |
||
| 69 | BIT__RST(SPCR, SPR0); |
||
| 70 | } |
||
| 71 | break; |
||
| 72 | |||
| 73 | case SPI__CLOCK_RATE__FOSC_OVER_4: |
||
| 74 | { |
||
| 75 | BIT__RST(SPCR, SPI2X); |
||
| 76 | BIT__RST(SPCR, SPR1); |
||
| 77 | BIT__RST(SPCR, SPR0); |
||
| 78 | } |
||
| 79 | break; |
||
| 80 | |||
| 81 | case SPI__CLOCK_RATE__FOSC_OVER_8: |
||
| 82 | { |
||
| 83 | BIT__SET(SPCR, SPI2X); |
||
| 84 | BIT__RST(SPCR, SPR1); |
||
| 85 | BIT__SET(SPCR, SPR0); |
||
| 86 | } |
||
| 87 | break; |
||
| 88 | |||
| 89 | case SPI__CLOCK_RATE__FOSC_OVER_16: |
||
| 90 | { |
||
| 91 | BIT__RST(SPCR, SPI2X); |
||
| 92 | BIT__RST(SPCR, SPR1); |
||
| 93 | BIT__SET(SPCR, SPR0); |
||
| 94 | } |
||
| 95 | break; |
||
| 96 | |||
| 97 | case SPI__CLOCK_RATE__FOSC_OVER_32: |
||
| 98 | { |
||
| 99 | BIT__SET(SPCR, SPI2X); |
||
| 100 | BIT__SET(SPCR, SPR1); |
||
| 101 | BIT__RST(SPCR, SPR0); |
||
| 102 | } |
||
| 103 | break; |
||
| 104 | |||
| 105 | case SPI__CLOCK_RATE__FOSC_OVER_64: |
||
| 106 | { |
||
| 107 | BIT__SET(SPCR, SPI2X); |
||
| 108 | BIT__SET(SPCR, SPR1); |
||
| 109 | BIT__SET(SPCR, SPR0); |
||
| 110 | } |
||
| 111 | break; |
||
| 112 | |||
| 113 | case SPI__CLOCK_RATE__FOSC_OVER_128: |
||
| 114 | { |
||
| 115 | BIT__RST(SPCR, SPI2X); |
||
| 116 | BIT__SET(SPCR, SPR1); |
||
| 117 | BIT__SET(SPCR, SPR0); |
||
| 118 | } |
||
| 119 | break; |
||
| 120 | |||
| 121 | default: |
||
| 122 | break; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /**************************************************************************//** |
||
| 127 | * \fn void spi__set_data_mode(spi__data_mode_t mode) |
||
| 128 | * |
||
| 129 | * \brief Set SPI data mode. |
||
| 130 | * |
||
| 131 | * \param[in] mode Data mode to set. |
||
| 132 | ******************************************************************************/ |
||
| 133 | void |
||
| 134 | spi__set_data_mode |
||
| 135 | ( |
||
| 136 | spi__data_mode_t mode |
||
| 137 | ){ |
||
| 138 | switch (mode) |
||
| 139 | { |
||
| 140 | case SPI__DATA_MODE_0: |
||
| 141 | { |
||
| 142 | BIT__RST(SPCR, CPOL); |
||
| 143 | BIT__RST(SPCR, CPHA); |
||
| 144 | } |
||
| 145 | break; |
||
| 146 | |||
| 147 | case SPI__DATA_MODE_1: |
||
| 148 | { |
||
| 149 | BIT__RST(SPCR, CPOL); |
||
| 150 | BIT__SET(SPCR, CPHA); |
||
| 151 | } |
||
| 152 | break; |
||
| 153 | |||
| 154 | case SPI__DATA_MODE_2: |
||
| 155 | { |
||
| 156 | BIT__SET(SPCR, CPOL); |
||
| 157 | BIT__RST(SPCR, CPHA); |
||
| 158 | } |
||
| 159 | break; |
||
| 160 | |||
| 161 | case SPI__DATA_MODE_3: |
||
| 162 | { |
||
| 163 | BIT__SET(SPCR, CPOL); |
||
| 164 | BIT__SET(SPCR, CPHA); |
||
| 165 | } |
||
| 166 | break; |
||
| 167 | |||
| 168 | default: |
||
| 169 | break; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /**************************************************************************//** |
||
| 174 | * \fn void spi__set_data_order(spi__data_order_t data_order) |
||
| 175 | * |
||
| 176 | * \brief Set SPI data order. |
||
| 177 | * |
||
| 178 | * \param[in] data_order Data order to set. |
||
| 179 | ******************************************************************************/ |
||
| 180 | void |
||
| 181 | spi__set_data_order |
||
| 182 | ( |
||
| 183 | spi__data_order_t data_order |
||
| 184 | ){ |
||
| 185 | if (SPI__DATA_ORDER__LSB_FIRST == data_order) |
||
| 186 | { |
||
| 187 | BIT__SET(SPCR, DORD); |
||
| 188 | } |
||
| 189 | else |
||
| 190 | { |
||
| 191 | BIT__RST(SPCR, DORD); |
||
| 192 | } |
||
| 193 | } |