summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/spi/spi.h
blob: 834e9ea53829c63124484a93d2e9a0ce5291267c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef spi_h
#define spi_h
/* spi.h */
/* avr.spi - SPI AVR module. {{{
 *
 * Copyright (C) 2008 Nélio Laranjeiro
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "io.h"

/* Configuration constants are used for all drivers, even if values come from
 * hardware driver. */

/** Operate as a slave. */
#define SPI_SLAVE 0
/** Operate as master. */
#define SPI_MASTER _BV (MSTR)

/** Output MSB first. */
#define SPI_MSB_FIRST 0
/** Output LSB first. */
#define SPI_LSB_FIRST _BV (DORD)

/** Clock polarity: rising edge is the leading one, SCK is low when idle. */
#define SPI_CPOL_RISING 0
/** Clock polarity: falling edge is the leading one, SCK is high when idle. */
#define SPI_CPOL_FALLING _BV (CPOL)
/** Clock phase: sample on leading edge. */
#define SPI_CPHA_SAMPLE 0
/** Clock phase: setup on leading edge. */
#define SPI_CPHA_SETUP _BV (CPHA)

/** SPI mode 0, sample on rising edge, setup on falling edge. */
#define SPI_MODE_0 (SPI_CPOL_RISING | SPI_CPHA_SAMPLE)
/** SPI mode 1, setup on rising edge, sample on falling edge. */
#define SPI_MODE_1 (SPI_CPOL_RISING | SPI_CPHA_SETUP)
/** SPI mode 2, sample on falling edge, setup on rising edge. */
#define SPI_MODE_2 (SPI_CPOL_FALLING | SPI_CPHA_SAMPLE)
/** SPI mode 3, setup on falling edge, sample on rising edge. */
#define SPI_MODE_3 (SPI_CPOL_FALLING | SPI_CPHA_SETUP)
/** SPI mode mask. */
#define SPI_MODE_MASK SPI_MODE_3

/* Define selected drivers. */
#define SPI_DRIVER_NONE '0'
#define SPI_DRIVER_HARD 'h'
#define SPI_DRIVER_SOFT 's'
#define SPI_DRIVER__(drv) SPI_DRIVER_ ## drv
#define SPI_DRIVER_(drv) SPI_DRIVER__ (drv)
#define SPI0_DRIVER SPI_DRIVER_ (AC_SPI0_DRIVER)
#define SPI1_DRIVER SPI_DRIVER_ (AC_SPI1_DRIVER)

/* Include drivers header. */
#if SPI0_DRIVER == SPI_DRIVER_HARD || SPI1_DRIVER == SPI_DRIVER_HARD
# include "spi_hard.h"
#endif
#if SPI0_DRIVER == SPI_DRIVER_SOFT || SPI1_DRIVER == SPI_DRIVER_SOFT
# include "spi_soft.h"
#endif

/* Map names to drivers. */
#if SPI0_DRIVER == SPI_DRIVER_HARD
# define spi_init spi_hard_init
# define spi_send spi_hard_send
# define spi_recv spi_hard_recv
# define spi_send_and_recv spi_hard_send_and_recv
#elif SPI0_DRIVER == SPI_DRIVER_SOFT
# define spi_init spi_soft_init
# define spi_send spi_soft_send
# define spi_recv spi_soft_recv
# define spi_send_and_recv spi_soft_send_and_recv
#endif
#if SPI1_DRIVER == SPI_DRIVER_HARD
# define spi1_init spi_hard_init
# define spi1_send spi_hard_send
# define spi1_recv spi_hard_recv
# define spi1_send_and_recv spi_hard_send_and_recv
#elif SPI1_DRIVER == SPI_DRIVER_SOFT
# define spi1_init spi_soft_init
# define spi1_send spi_soft_send
# define spi1_recv spi_soft_recv
# define spi1_send_and_recv spi_soft_send_and_recv
#endif

#endif /* spi_h */