source: asp3_tinet_ecnl_arm/trunk/asp3_dcre/mbed/hal/spi_api.h@ 364

Last change on this file since 364 was 364, checked in by coas-nagasima, 5 years ago

TINETとSocket APIなどを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 7.5 KB
Line 
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 2006-2013 ARM Limited
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19#ifndef MBED_SPI_API_H
20#define MBED_SPI_API_H
21
22#include "device.h"
23#include "hal/dma_api.h"
24#include "hal/buffer.h"
25
26#if DEVICE_SPI
27
28#define SPI_EVENT_ERROR (1 << 1)
29#define SPI_EVENT_COMPLETE (1 << 2)
30#define SPI_EVENT_RX_OVERFLOW (1 << 3)
31#define SPI_EVENT_ALL (SPI_EVENT_ERROR | SPI_EVENT_COMPLETE | SPI_EVENT_RX_OVERFLOW)
32
33#define SPI_EVENT_INTERNAL_TRANSFER_COMPLETE (1 << 30) // Internal flag to report that an event occurred
34
35#define SPI_FILL_WORD (0xFFFF)
36
37#if DEVICE_SPI_ASYNCH
38/** Asynch SPI HAL structure
39 */
40typedef struct {
41 struct spi_s spi; /**< Target specific SPI structure */
42 struct buffer_s tx_buff; /**< Tx buffer */
43 struct buffer_s rx_buff; /**< Rx buffer */
44} spi_t;
45
46#else
47/** Non-asynch SPI HAL structure
48 */
49typedef struct spi_s spi_t;
50
51#endif
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/**
58 * \defgroup hal_GeneralSPI SPI Configuration Functions
59 * @{
60 */
61
62/** Initialize the SPI peripheral
63 *
64 * Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
65 * @param[out] obj The SPI object to initialize
66 * @param[in] mosi The pin to use for MOSI
67 * @param[in] miso The pin to use for MISO
68 * @param[in] sclk The pin to use for SCLK
69 * @param[in] ssel The pin to use for SSEL
70 */
71void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel);
72
73/** Release a SPI object
74 *
75 * TODO: spi_free is currently unimplemented
76 * This will require reference counting at the C++ level to be safe
77 *
78 * Return the pins owned by the SPI object to their reset state
79 * Disable the SPI peripheral
80 * Disable the SPI clock
81 * @param[in] obj The SPI object to deinitialize
82 */
83void spi_free(spi_t *obj);
84
85/** Configure the SPI format
86 *
87 * Set the number of bits per frame, configure clock polarity and phase, shift order and master/slave mode.
88 * The default bit order is MSB.
89 * @param[in,out] obj The SPI object to configure
90 * @param[in] bits The number of bits per frame
91 * @param[in] mode The SPI mode (clock polarity, phase, and shift direction)
92 * @param[in] slave Zero for master mode or non-zero for slave mode
93 */
94void spi_format(spi_t *obj, int bits, int mode, int slave);
95
96/** Set the SPI baud rate
97 *
98 * Actual frequency may differ from the desired frequency due to available dividers and bus clock
99 * Configures the SPI peripheral's baud rate
100 * @param[in,out] obj The SPI object to configure
101 * @param[in] hz The baud rate in Hz
102 */
103void spi_frequency(spi_t *obj, int hz);
104
105/**@}*/
106/**
107 * \defgroup SynchSPI Synchronous SPI Hardware Abstraction Layer
108 * @{
109 */
110
111/** Write a byte out in master mode and receive a value
112 *
113 * @param[in] obj The SPI peripheral to use for sending
114 * @param[in] value The value to send
115 * @return Returns the value received during send
116 */
117int spi_master_write(spi_t *obj, int value);
118
119/** Write a block out in master mode and receive a value
120 *
121 * The total number of bytes sent and received will be the maximum of
122 * tx_length and rx_length. The bytes written will be padded with the
123 * value 0xff.
124 *
125 * @param[in] obj The SPI peripheral to use for sending
126 * @param[in] tx_buffer Pointer to the byte-array of data to write to the device
127 * @param[in] tx_length Number of bytes to write, may be zero
128 * @param[in] rx_buffer Pointer to the byte-array of data to read from the device
129 * @param[in] rx_length Number of bytes to read, may be zero
130 * @param[in] write_fill Default data transmitted while performing a read
131 * @returns
132 * The number of bytes written and read from the device. This is
133 * maximum of tx_length and rx_length.
134 */
135int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill);
136
137/** Check if a value is available to read
138 *
139 * @param[in] obj The SPI peripheral to check
140 * @return non-zero if a value is available
141 */
142int spi_slave_receive(spi_t *obj);
143
144/** Get a received value out of the SPI receive buffer in slave mode
145 *
146 * Blocks until a value is available
147 * @param[in] obj The SPI peripheral to read
148 * @return The value received
149 */
150int spi_slave_read(spi_t *obj);
151
152/** Write a value to the SPI peripheral in slave mode
153 *
154 * Blocks until the SPI peripheral can be written to
155 * @param[in] obj The SPI peripheral to write
156 * @param[in] value The value to write
157 */
158void spi_slave_write(spi_t *obj, int value);
159
160/** Checks if the specified SPI peripheral is in use
161 *
162 * @param[in] obj The SPI peripheral to check
163 * @return non-zero if the peripheral is currently transmitting
164 */
165int spi_busy(spi_t *obj);
166
167/** Get the module number
168 *
169 * @param[in] obj The SPI peripheral to check
170 * @return The module number
171 */
172uint8_t spi_get_module(spi_t *obj);
173
174/**@}*/
175
176#if DEVICE_SPI_ASYNCH
177/**
178 * \defgroup AsynchSPI Asynchronous SPI Hardware Abstraction Layer
179 * @{
180 */
181
182/** Begin the SPI transfer. Buffer pointers and lengths are specified in tx_buff and rx_buff
183 *
184 * @param[in] obj The SPI object that holds the transfer information
185 * @param[in] tx The transmit buffer
186 * @param[in] tx_length The number of bytes to transmit
187 * @param[in] rx The receive buffer
188 * @param[in] rx_length The number of bytes to receive
189 * @param[in] bit_width The bit width of buffer words
190 * @param[in] event The logical OR of events to be registered
191 * @param[in] handler SPI interrupt handler
192 * @param[in] hint A suggestion for how to use DMA with this transfer
193 */
194void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint8_t bit_width, uint32_t handler, uint32_t event, DMAUsage hint);
195
196/** The asynchronous IRQ handler
197 *
198 * Reads the received values out of the RX FIFO, writes values into the TX FIFO and checks for transfer termination
199 * conditions, such as buffer overflows or transfer complete.
200 * @param[in] obj The SPI object that holds the transfer information
201 * @return Event flags if a transfer termination condition was met; otherwise 0.
202 */
203uint32_t spi_irq_handler_asynch(spi_t *obj);
204
205/** Attempts to determine if the SPI peripheral is already in use
206 *
207 * If a temporary DMA channel has been allocated, peripheral is in use.
208 * If a permanent DMA channel has been allocated, check if the DMA channel is in use. If not, proceed as though no DMA
209 * channel were allocated.
210 * If no DMA channel is allocated, check whether tx and rx buffers have been assigned. For each assigned buffer, check
211 * if the corresponding buffer position is less than the buffer length. If buffers do not indicate activity, check if
212 * there are any bytes in the FIFOs.
213 * @param[in] obj The SPI object to check for activity
214 * @return Non-zero if the SPI port is active or zero if it is not.
215 */
216uint8_t spi_active(spi_t *obj);
217
218/** Abort an SPI transfer
219 *
220 * @param obj The SPI peripheral to stop
221 */
222void spi_abort_asynch(spi_t *obj);
223
224
225#endif
226
227/**@}*/
228
229#ifdef __cplusplus
230}
231#endif // __cplusplus
232
233#endif // SPI_DEVICE
234
235#endif // MBED_SPI_API_H
236
237/** @}*/
Note: See TracBrowser for help on using the repository browser.