130 lines
4.6 KiB
C
130 lines
4.6 KiB
C
#ifndef SPI_MASTER_H
|
|
#define SPI_MASTER_H
|
|
/*================================================================================================
|
|
|
|
spi.h SiWiCom copyright 2006 v1.0 (2006-05-11)
|
|
|
|
Name: Joakim Nilsson E-mail: mail@jopin.se
|
|
Name: Christoffer Martinsson E-mail: cm@cmtec.se
|
|
|
|
Description: Routines for SPI interface. Basic design by CM. Modifyed and extended by JN
|
|
for project SiWiCom 2006
|
|
|
|
SPI configuration is done by hardware.h witch must contain definitions for:
|
|
(X=letter i.e PORTX=PORTB, n=number i.e PXn=PB4)
|
|
|
|
SPI_PORT PORTX (Example: #define SPI_PORT PORTB) // PORTB as SPI-port
|
|
SPI_DDR DDRX (Example: #define SPI_DDR DDRB) // DDRB as direction for SPI pindirection
|
|
|
|
MOSI PXn (Example: #define MOSI PB3) // Master Out - Slave In
|
|
MISO PXn (Example: #define MISO PB4) // Master In - Slave Out
|
|
SCK PXn (Example: #define SCK PB5 // Serial Clock
|
|
|
|
Processor as SPI-master or SPI-slave, example:
|
|
#define _SPI_MASTER_
|
|
//#define _SPI_SLAVE_
|
|
|
|
If processor is defined as _SPI_MASTER_, Chip select port(s) (at least one, max 8) must be asigned as:
|
|
#define SPI_CS0_PORT PORTX
|
|
.
|
|
.
|
|
.
|
|
#define SPI_CS7_PORT PORTX
|
|
|
|
|
|
and SPI direction for the above asigned port(s) as:
|
|
#define SPI_CS0_DDR DDRX
|
|
.
|
|
.
|
|
.
|
|
#define SPI_CS7_DDR DDRX
|
|
|
|
and SPI pin(s) used for chip select as:
|
|
|
|
#define SPI_CS0 PXn
|
|
.
|
|
.
|
|
.
|
|
#define SPI_CS7 PXn
|
|
|
|
================================================================================================*/
|
|
#define SPI_CS0_NR 0
|
|
#define SPI_CS1_NR 1
|
|
#define SPI_CS2_NR 2
|
|
#define SPI_CS3_NR 3
|
|
#define SPI_CS4_NR 4
|
|
#define SPI_CS5_NR 5
|
|
#define SPI_CS6_NR 6
|
|
#define SPI_CS7_NR 7
|
|
/*================================================================================================
|
|
Functions
|
|
================================================================================================*/
|
|
/*================================================================================================
|
|
spiWrite
|
|
|
|
Description: Write data to SPI.
|
|
|
|
Input: data to send
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void spiWrite(uint8_t data);
|
|
/*================================================================================================
|
|
spiRead
|
|
|
|
Description: Read data from SPI buffer.
|
|
|
|
Input: -
|
|
Return: SPI-value
|
|
------------------------------------------------------------------------------------------------*/
|
|
uint8_t spiRead(void);
|
|
/*================================================================================================
|
|
spiSetHighSpeed
|
|
|
|
Description: Set datatrasfer to fc/4.
|
|
|
|
Input: -
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void spiSetHighSpeed(void);
|
|
/*================================================================================================
|
|
spiSetLowSpeed
|
|
|
|
Description: Set datatrasfer to fc/64.
|
|
|
|
Input: -
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void spiSetLowSpeed(void);
|
|
/*================================================================================================
|
|
spiInit
|
|
|
|
Description: Init SPI interface as master or slave depending on the "hardware.h" configuration.
|
|
|
|
Input: -
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void spiInit(void);
|
|
/*================================================================================================
|
|
spiChipSelect
|
|
|
|
Description: Select device for communication via SPI. Protects chipselect on two devices at the
|
|
same time and secure that chosen chipselect is an initialised legal channel.
|
|
|
|
Input: uint8_t (The number of the desired chipselect. (Between 0 and 7) )
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void spiChipSelect(uint8_t chipselect);
|
|
/*================================================================================================
|
|
spiChipDeSelect
|
|
|
|
Description: Deselect all SPI channels
|
|
Input: -
|
|
Return: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void spiChipDeSelect(void);
|
|
/*================================================================================================
|
|
End
|
|
================================================================================================*/
|
|
#endif // SPI_MASTER_H
|
|
|