rfLPC
A low level library for using NXP's LPC17xx SoC. Config is given for MBED prototyping board
spi.h
Go to the documentation of this file.
1 /* This file is part of rflpc. Copyright 2010-2011 Michael Hauspie
2  *
3  * rflpc is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * rflpc is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with rflpc. If not, see <http://www.gnu.org/licenses/>.
15  */
19 #ifndef __RFLPC_SPI_H__
20 #define __RFLPC_SPI_H__
21 /*
22  Author: Michael Hauspie <michael.hauspie@univ-lille1.fr>
23  Created:
24  Time-stamp: <2014-05-29 15:05:59 (mickey)>
25 */
26 
27 #ifdef RFLPC_CONFIG_ENABLE_SPI
28 
29 #include "../clock.h"
30 #include "../interrupt.h"
31 
38 typedef enum
39 {
42 } rflpc_spi_t;
43 
45 typedef enum
46 {
50 
52 #define RFLPC_SPI_CPOL_RISING_EDGE 0
53 
54 #define RFLPC_SPI_CPOL_FALLING_EDGE 2
55 
57 #define RFLPC_SPI_CPHA_PHASE_PIOR_TO_FIRST_EDGE 0
58 
60 #define RFLPC_SPI_CPHA_PHASE_FIRST_EDGE 1
61 
62 
78 extern void rflpc_spi_init(rflpc_spi_t port, rflpc_spi_mode_t mode, rflpc_clock_divider_t cpu_clock_divider, uint8_t data_size_transfert, uint8_t clock_prescale, uint8_t serial_clock_rate, uint8_t clock_polarity_phase);
79 
86 static inline LPC_SSP_TypeDef *rflpc_spi_get_base_addr(rflpc_spi_t port)
87 {
88  return (port == RFLPC_SPI0) ? LPC_SSP0 : LPC_SSP1;
89 }
90 
97 static inline int rflpc_spi_tx_fifo_empty(rflpc_spi_t port)
98 {
99  LPC_SSP_TypeDef *spi_base = rflpc_spi_get_base_addr(port);
100  return spi_base->SR & 1;
101 }
102 
109 static inline int rflpc_spi_idle(rflpc_spi_t port)
110 {
111  LPC_SSP_TypeDef *spi_base = rflpc_spi_get_base_addr(port);
112  return ((spi_base->SR & 0x10) == 0);
113 }
114 
115 
122 static inline int rflpc_spi_tx_fifo_full(rflpc_spi_t port)
123 {
124  LPC_SSP_TypeDef *spi_base = rflpc_spi_get_base_addr(port);
125  return !(spi_base->SR & (1UL << 1));
126 }
127 
134 static inline int rflpc_spi_rx_fifo_empty(rflpc_spi_t port)
135 {
136  LPC_SSP_TypeDef *spi_base = rflpc_spi_get_base_addr(port);
137  return !(spi_base->SR & (1UL << 2));
138 }
139 
147 static inline void rflpc_spi_write(rflpc_spi_t port, uint16_t data)
148 {
149  LPC_SSP_TypeDef *spi_base = rflpc_spi_get_base_addr(port);
150  while (rflpc_spi_tx_fifo_full(port));
151  spi_base->DR = data;
152 }
153 
159 static inline uint16_t rflpc_spi_read(rflpc_spi_t port)
160 {
161  LPC_SSP_TypeDef *spi_base = rflpc_spi_get_base_addr(port);
162  while (rflpc_spi_rx_fifo_empty(port));
163  return spi_base->DR;
164 }
165 
172 extern void rflpc_spi_set_rx_callback(rflpc_spi_t port, rflpc_irq_handler_t callback);
173 
174 
177 #endif /* ENABLE_SPI */
178 
179 
180 #endif
void rflpc_spi_init(rflpc_spi_t port, rflpc_spi_mode_t mode, rflpc_clock_divider_t cpu_clock_divider, uint8_t data_size_transfert, uint8_t clock_prescale, uint8_t serial_clock_rate, uint8_t clock_polarity_phase)
Inits the SPI interface in master mode.
Use the SPI as slave.
Definition: spi.h:48
Use the SPI port 1.
Definition: spi.h:41
Use the SPI port 0.
Definition: spi.h:40
static int rflpc_spi_tx_fifo_empty(rflpc_spi_t port)
Tests if transmition FIFO is empty.
Definition: spi.h:97
static int rflpc_spi_tx_fifo_full(rflpc_spi_t port)
Tests if the transmition FIFO is full.
Definition: spi.h:122
rflpc_clock_divider_t
This enums defines the different cpu clock dividers for use as peripheral clocks. ...
Definition: clock.h:38
static void rflpc_spi_write(rflpc_spi_t port, uint16_t data)
Sends data through the spi interface.
Definition: spi.h:147
static int rflpc_spi_rx_fifo_empty(rflpc_spi_t port)
Tests if the reception FIFO is empty.
Definition: spi.h:134
rflpc_spi_mode_t
Used to select the SPI operating mode.
Definition: spi.h:45
static LPC_SSP_TypeDef * rflpc_spi_get_base_addr(rflpc_spi_t port)
Returns the base address of the SPI control block depending of the desired port.
Definition: spi.h:86
rflpc_spi_t
Used to select which SPI port to use.
Definition: spi.h:38
Use the SPI as master.
Definition: spi.h:47
static int rflpc_spi_idle(rflpc_spi_t port)
Test if the spi is idle (nor transmiting neither receiving) This is useful when you need to handle CS...
Definition: spi.h:109
void rflpc_spi_set_rx_callback(rflpc_spi_t port, rflpc_irq_handler_t callback)
Sets the interrupt handler for SPI reception.
static uint16_t rflpc_spi_read(rflpc_spi_t port)
Receive data through the spi interface.
Definition: spi.h:159
void(* rflpc_irq_handler_t)(void)
Interrupt handler type.
Definition: interrupt.h:62