This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi.h
187 lines (144 loc) · 4.4 KB
/
spi.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright (c)
(c) 2015-2016 Chintalagiri Shashank, Quazar Technologies Pvt. Ltd.
This file is part of
Embedded bootstraps : hal-uC
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 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 Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file hal_uc_spi.h
* @brief HAL for SPI interfaces
*
* This file is the hardware abstraction layer for uC SPI interfaces
*
* @see spi_impl.h
* @see spi_impl.c
*/
#ifndef HAL_UC_SPI_H
#define HAL_UC_SPI_H
#include <platform/types.h>
#include "map.h"
#ifdef uC_INCLUDE_SPI_IFACE
/**
* @name SPI API Setup and Reactor Functions
*/
/**@{*/
/**
* Initialize SPI. Config parameters for the SPI are (currently) defined in UC_MAP.
* @param intfnum Identifier of the SPI interface
*/
void spi_init(void);
void spi_reactor(void);
/**@}*/
/**
* @name SPI Slave Selection API
*/
/**@{*/
typedef enum {
SPI_SELECTOR_FUNC,
SPI_SELECTOR_PIO,
}spi_slave_selector_type;
typedef struct SPI_SSFUNC_t{
void (*const select) (void);
void (*const deselect) (void);
}spi_ssfunc_t;
typedef struct SPI_SSPIO_t{
const PORTSELECTOR_t port;
const PINSELECTOR_t pin;
}spi_sspio_t;
typedef enum SPI_SCLK_CLKPOL_t{
SPI_CLKPOL_AH,
SPI_CLKPOL_AL
}spi_sclk_clkpol_t;
typedef enum SPI_SCLK_CLKPHA_t{
SPI_CLKPHA_CHG_CAP,
SPI_CLKPHA_CAP_CHG
}spi_sclk_clkpha_t;
typedef enum SPI_DATA_ENDIAN_t{
SPI_DATA_MSBFIRST,
SPI_DATA_LSBFIRST
}spi_data_endian_t;
typedef enum SPI_DATA_WIDTH_t{
SPI_DATA_8BIT,
SPI_DATA_7BIT
}spi_data_width_t;
typedef union SPI_SCLK_CONF_t{
struct {
spi_sclk_clkpha_t clkpha : 1;
spi_sclk_clkpol_t clkpol : 1;
spi_data_endian_t endian: 1;
spi_data_width_t width: 1;
uint8_t clkdivider : 4;
};
uint8_t asint;
}spi_sclk_conf;
typedef struct SPI_SLAVE_t{
#if APP_SUPPORT_SPI_CTL
const spi_sclk_conf sclk;
#endif
const spi_slave_selector_type sst;
const union {
const spi_ssfunc_t func;
const spi_sspio_t pio;
} ss;
}spi_slave_t;
void spi_init_slave(uint8_t intfnum, const spi_slave_t * slave);
void spi_select_slave(uint8_t intfnum, const spi_slave_t * slave);
void spi_deselect_slave(uint8_t intfnum, const spi_slave_t * slave);
/**@}*/
/**
* @name SPI Transaction API
*/
/**@{*/
typedef struct SPI_TRANSACTION_t
{
struct SPI_TRANSACTION_t * next;
void (* callback) (struct SPI_TRANSACTION_t *);
volatile uint8_t txlen;
volatile uint8_t rxlen;
volatile uint8_t * txdata;
volatile uint8_t * rxdata;
const spi_slave_t * slave;
}spi_transaction_t;
static inline void spi_enqueue_transaction(uint8_t intfnum, spi_transaction_t * transaction);
static inline void spi_enqueue_pirority_transaction(uint8_t intfnum, spi_transaction_t * transaction);
static inline void spi_cancel_transaction(uint8_t intfnum, spi_transaction_t * transaction);
static inline uint8_t spi_queue_empty(uint8_t intfnum);
/**@}*/
/**
* @name Hardware Debug-Only SPI API Functions
*/
/**@{*/
/**
* @brief Send an recieve a single character over the specified SPI interface
* @param byte The character to be sent
* @param intfnum Identifier of the SPI interface
*
* This function uses the simplest possible method to send a single
* byte of data and returns the character it gets in return at the same time.
* It should not use buffers, interrupts, etc.
*
* @warning In a general application, this function would only be used
* in special circumstances and with extreme care. Using this alongside
* transactions will cause data loss. If you intend to use this interface,
* the peripheral initialization may need to be changed to not enable
* interrupts.
*
* @return Character recieved
*/
static inline uint8_t spi_txrx_bare(uint8_t intfnum, uint8_t byte);
/**@}*/
#endif
// Set up the implentation
#include "uc/spi_impl.h"
#include "uc/spi_handlers.h"
#endif