LT_SPI

Committer:
roger5641
Date:
Sun Nov 12 01:21:36 2017 +0000
Revision:
0:516e11acba42
ver1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roger5641 0:516e11acba42 1 //! @todo Review this document.
roger5641 0:516e11acba42 2 /*!
roger5641 0:516e11acba42 3 LT_SPI: Routines to communicate with ATmega328P's hardware SPI port.
roger5641 0:516e11acba42 4
roger5641 0:516e11acba42 5 REVISION HISTORY
roger5641 0:516e11acba42 6 $Revision: 3659 $
roger5641 0:516e11acba42 7 $Date: 2015-07-01 10:19:20 -0700 (Wed, 01 Jul 2015) $
roger5641 0:516e11acba42 8
roger5641 0:516e11acba42 9 Copyright (c) 2013, Linear Technology Corp.(LTC)
roger5641 0:516e11acba42 10 All rights reserved.
roger5641 0:516e11acba42 11
roger5641 0:516e11acba42 12 Redistribution and use in source and binary forms, with or without
roger5641 0:516e11acba42 13 modification, are permitted provided that the following conditions are met:
roger5641 0:516e11acba42 14
roger5641 0:516e11acba42 15 1. Redistributions of source code must retain the above copyright notice, this
roger5641 0:516e11acba42 16 list of conditions and the following disclaimer.
roger5641 0:516e11acba42 17 2. Redistributions in binary form must reproduce the above copyright notice,
roger5641 0:516e11acba42 18 this list of conditions and the following disclaimer in the documentation
roger5641 0:516e11acba42 19 and/or other materials provided with the distribution.
roger5641 0:516e11acba42 20
roger5641 0:516e11acba42 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
roger5641 0:516e11acba42 22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
roger5641 0:516e11acba42 23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
roger5641 0:516e11acba42 24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
roger5641 0:516e11acba42 25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
roger5641 0:516e11acba42 26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
roger5641 0:516e11acba42 27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
roger5641 0:516e11acba42 28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
roger5641 0:516e11acba42 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
roger5641 0:516e11acba42 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
roger5641 0:516e11acba42 31
roger5641 0:516e11acba42 32 The views and conclusions contained in the software and documentation are those
roger5641 0:516e11acba42 33 of the authors and should not be interpreted as representing official policies,
roger5641 0:516e11acba42 34 either expressed or implied, of Linear Technology Corp.
roger5641 0:516e11acba42 35
roger5641 0:516e11acba42 36 The Linear Technology Linduino is not affiliated with the official Arduino team.
roger5641 0:516e11acba42 37 However, the Linduino is only possible because of the Arduino team's commitment
roger5641 0:516e11acba42 38 to the open-source community. Please, visit http://www.arduino.cc and
roger5641 0:516e11acba42 39 http://store.arduino.cc , and consider a purchase that will help fund their
roger5641 0:516e11acba42 40 ongoing work.
roger5641 0:516e11acba42 41 */
roger5641 0:516e11acba42 42
roger5641 0:516e11acba42 43 /*! @file
roger5641 0:516e11acba42 44 @ingroup LT_SPI
roger5641 0:516e11acba42 45 Library Header File for LT_SPI: Routines to communicate with ATmega328P's hardware SPI port.
roger5641 0:516e11acba42 46 */
roger5641 0:516e11acba42 47
roger5641 0:516e11acba42 48 #ifndef LT_SPI_H
roger5641 0:516e11acba42 49 #define LT_SPI_H
roger5641 0:516e11acba42 50
roger5641 0:516e11acba42 51 #include "mbed.h"
roger5641 0:516e11acba42 52
roger5641 0:516e11acba42 53 SPI spi(A6,A5,A4,A3);
roger5641 0:516e11acba42 54 // Uncomment the following to use functions that implement LTC SPI routines
roger5641 0:516e11acba42 55
roger5641 0:516e11acba42 56 // //! @name SPI CLOCK DIVIDER CONSTANTS
roger5641 0:516e11acba42 57 // //! @{
roger5641 0:516e11acba42 58 // #define SPI_CLOCK_DIV4 0x00 // 4 Mhz
roger5641 0:516e11acba42 59 // #define SPI_CLOCK_DIV16 0x01 // 1 Mhz
roger5641 0:516e11acba42 60 // #define SPI_CLOCK_DIV64 0x02 // 250 khz
roger5641 0:516e11acba42 61 // #define SPI_CLOCK_DIV128 0x03 // 125 khz
roger5641 0:516e11acba42 62 // #define SPI_CLOCK_DIV2 0x04 // 8 Mhz
roger5641 0:516e11acba42 63 // #define SPI_CLOCK_DIV8 0x05 // 2 Mhz
roger5641 0:516e11acba42 64 // #define SPI_CLOCK_DIV32 0x06 // 500 khz
roger5641 0:516e11acba42 65 // //! @}
roger5641 0:516e11acba42 66 //
roger5641 0:516e11acba42 67 // //! @name SPI HARDWARE MODE CONSTANTS
roger5641 0:516e11acba42 68 // //! @{
roger5641 0:516e11acba42 69 // #define SPI_MODE0 0x00
roger5641 0:516e11acba42 70 // #define SPI_MODE1 0x04
roger5641 0:516e11acba42 71 // // #define SPI_MODE2 0x08
roger5641 0:516e11acba42 72 // #define SPI_MODE3 0x0C
roger5641 0:516e11acba42 73 // //! @}
roger5641 0:516e11acba42 74 //
roger5641 0:516e11acba42 75 // //! @name SPI SET MASKS
roger5641 0:516e11acba42 76 //! @{
roger5641 0:516e11acba42 77 // #define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
roger5641 0:516e11acba42 78 // #define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
roger5641 0:516e11acba42 79 // #define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
roger5641 0:516e11acba42 80 // //! @}
roger5641 0:516e11acba42 81
roger5641 0:516e11acba42 82 //! Reads and sends a byte
roger5641 0:516e11acba42 83 //! @return void
roger5641 0:516e11acba42 84 void spi_transfer_byte(uint8_t cs_pin, //!< Chip select pin
roger5641 0:516e11acba42 85 uint8_t tx, //!< Byte to be transmitted
roger5641 0:516e11acba42 86 uint8_t *rx //!< Byte to be received
roger5641 0:516e11acba42 87 );
roger5641 0:516e11acba42 88
roger5641 0:516e11acba42 89 //! Reads and sends a word
roger5641 0:516e11acba42 90 //! @return void
roger5641 0:516e11acba42 91 void spi_transfer_word(uint8_t cs_pin, //!< Chip select pin
roger5641 0:516e11acba42 92 uint16_t tx, //!< Byte to be transmitted
roger5641 0:516e11acba42 93 uint16_t *rx //!< Byte to be received
roger5641 0:516e11acba42 94 );
roger5641 0:516e11acba42 95
roger5641 0:516e11acba42 96 //! Reads and sends a byte array
roger5641 0:516e11acba42 97 //! @return void
roger5641 0:516e11acba42 98 void spi_transfer_block(uint8_t cs_pin, //!< Chip select pin
roger5641 0:516e11acba42 99 uint8_t *tx, //!< Byte array to be transmitted
roger5641 0:516e11acba42 100 uint8_t *rx, //!< Byte array to be received
roger5641 0:516e11acba42 101 uint8_t length //!< Length of array
roger5641 0:516e11acba42 102 );
roger5641 0:516e11acba42 103
roger5641 0:516e11acba42 104 //! Connect SPI pins to QuikEval connector through the Linduino MUX. This will disconnect I2C.
roger5641 0:516e11acba42 105 //void quikeval_SPI_connect();
roger5641 0:516e11acba42 106
roger5641 0:516e11acba42 107 //! Configure the SPI port for 4Mhz SCK.
roger5641 0:516e11acba42 108 //! This function or spi_enable() must be called
roger5641 0:516e11acba42 109 //! before using the other SPI routines.
roger5641 0:516e11acba42 110 //void quikeval_SPI_init();
roger5641 0:516e11acba42 111
roger5641 0:516e11acba42 112 //! Setup the processor for hardware SPI communication.
roger5641 0:516e11acba42 113 //! Must be called before using the other SPI routines.
roger5641 0:516e11acba42 114 //! Alternatively, call quikeval_SPI_connect(), which automatically
roger5641 0:516e11acba42 115 //! calls this function.
roger5641 0:516e11acba42 116 void spi_enable(void); //!< Configures SCK frequency. Use constant defined in header file.
roger5641 0:516e11acba42 117
roger5641 0:516e11acba42 118
roger5641 0:516e11acba42 119 //! Disable the SPI hardware port
roger5641 0:516e11acba42 120 //void spi_disable();
roger5641 0:516e11acba42 121
roger5641 0:516e11acba42 122 //! Write a data byte using the SPI hardware
roger5641 0:516e11acba42 123 //void spi_write(int8_t data //!< Byte to be written to SPI port
roger5641 0:516e11acba42 124 // );
roger5641 0:516e11acba42 125
roger5641 0:516e11acba42 126 //! Read and write a data byte using the SPI hardware
roger5641 0:516e11acba42 127 //! @return the data byte read
roger5641 0:516e11acba42 128 //int8_t spi_read(int8_t data //!< The data byte to be written
roger5641 0:516e11acba42 129 // );
roger5641 0:516e11acba42 130
roger5641 0:516e11acba42 131 #endif // LT_SPI_H