2010-06-23 10:15:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Basic SPI macros
|
|
|
|
* \author
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
2013-11-24 15:57:08 +00:00
|
|
|
#ifndef SPI_H_
|
|
|
|
#define SPI_H_
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2009-09-07 11:31:26 +00:00
|
|
|
/* Define macros to use for checking SPI transmission status depending
|
|
|
|
on if it is possible to wait for TX buffer ready. This is possible
|
|
|
|
on for example MSP430 but not on AVR. */
|
|
|
|
#ifdef SPI_WAITFORTxREADY
|
|
|
|
#define SPI_WAITFORTx_BEFORE() SPI_WAITFORTxREADY()
|
|
|
|
#define SPI_WAITFORTx_AFTER()
|
|
|
|
#define SPI_WAITFORTx_ENDED() SPI_WAITFOREOTx()
|
|
|
|
#else /* SPI_WAITFORTxREADY */
|
|
|
|
#define SPI_WAITFORTx_BEFORE()
|
|
|
|
#define SPI_WAITFORTx_AFTER() SPI_WAITFOREOTx()
|
|
|
|
#define SPI_WAITFORTx_ENDED()
|
|
|
|
#endif /* SPI_WAITFORTxREADY */
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
extern unsigned char spi_busy;
|
|
|
|
|
|
|
|
void spi_init(void);
|
|
|
|
|
2010-06-23 10:15:28 +00:00
|
|
|
/* Write one character to SPI */
|
2015-03-18 18:43:02 +00:00
|
|
|
#define SPI_WRITE(data) \
|
|
|
|
do { \
|
|
|
|
SPI_WAITFORTx_BEFORE(); \
|
|
|
|
SPI_TXBUF = data; \
|
|
|
|
SPI_WAITFOREOTx(); \
|
2010-06-23 10:15:28 +00:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Write one character to SPI - will not wait for end
|
|
|
|
useful for multiple writes with wait after final */
|
2015-03-18 18:43:02 +00:00
|
|
|
#define SPI_WRITE_FAST(data) \
|
|
|
|
do { \
|
|
|
|
SPI_WAITFORTx_BEFORE(); \
|
|
|
|
SPI_TXBUF = data; \
|
|
|
|
SPI_WAITFORTx_AFTER(); \
|
2010-06-23 10:15:28 +00:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Read one character from SPI */
|
2015-03-18 18:43:02 +00:00
|
|
|
#define SPI_READ(data) \
|
|
|
|
do { \
|
|
|
|
SPI_TXBUF = 0; \
|
|
|
|
SPI_WAITFOREORx(); \
|
|
|
|
data = SPI_RXBUF; \
|
2010-06-23 10:15:28 +00:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* Flush the SPI read register */
|
2014-03-18 14:10:13 +00:00
|
|
|
#ifndef SPI_FLUSH
|
2010-06-23 10:15:28 +00:00
|
|
|
#define SPI_FLUSH() \
|
2015-03-18 18:43:02 +00:00
|
|
|
do { \
|
|
|
|
SPI_RXBUF; \
|
|
|
|
} while(0)
|
2014-03-18 14:10:13 +00:00
|
|
|
#endif
|
2010-06-23 10:15:28 +00:00
|
|
|
|
2013-11-24 15:57:08 +00:00
|
|
|
#endif /* SPI_H_ */
|