WPILibC++ 2024.3.2
SPI.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include "hal/AnalogTrigger.h"
10#include "hal/SPITypes.h"
11#include "hal/Types.h"
12
13/**
14 * @defgroup hal_spi SPI Functions
15 * @ingroup hal_capi
16 * @{
17 */
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23/**
24 * Initializes the SPI port. Opens the port if necessary and saves the handle.
25 *
26 * If opening the MXP port, also sets up the channel functions appropriately.
27 *
28 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS3, 4
29 * for MXP
30 * @param[out] status the error code, or 0 for success
31 */
32void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status);
33
34/**
35 * Performs an SPI send/receive transaction.
36 *
37 * This is a lower-level interface to the spi hardware giving you more control
38 * over each transaction.
39 *
40 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
41 * for MXP
42 * @param dataToSend Buffer of data to send as part of the transaction.
43 * @param dataReceived Buffer to read data into.
44 * @param size Number of bytes to transfer. [0..7]
45 * @return Number of bytes transferred, -1 for error
46 */
47int32_t HAL_TransactionSPI(HAL_SPIPort port, const uint8_t* dataToSend,
48 uint8_t* dataReceived, int32_t size);
49
50/**
51 * Executes a write transaction with the device.
52 *
53 * Writes to a device and wait until the transaction is complete.
54 *
55 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
56 * for MXP
57 * @param dataToSend The data to write to the register on the device.
58 * @param sendSize The number of bytes to be written
59 * @return The number of bytes written. -1 for an error
60 */
61int32_t HAL_WriteSPI(HAL_SPIPort port, const uint8_t* dataToSend,
62 int32_t sendSize);
63
64/**
65 * Executes a read from the device.
66 *
67 * This method does not write any data out to the device.
68 *
69 * Most spi devices will require a register address to be written before they
70 * begin returning data.
71 *
72 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for
73 * MXP
74 * @param buffer A pointer to the array of bytes to store the data read from the
75 * device.
76 * @param count The number of bytes to read in the transaction. [1..7]
77 * @return Number of bytes read. -1 for error.
78 */
79int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count);
80
81/**
82 * Closes the SPI port.
83 *
84 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
85 */
87
88/**
89 * Sets the clock speed for the SPI bus.
90 *
91 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for
92 * MXP
93 * @param speed The speed in Hz (500KHz-10MHz)
94 */
95void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed);
96
97/**
98 * Sets the SPI Mode.
99 *
100 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for
101 * MXP
102 * @param mode The SPI mode to use
103 */
105
106/**
107 * Gets the SPI Mode.
108 *
109 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for
110 * MXP
111 * @return The SPI mode currently set
112 */
114
115/**
116 * Sets the CS Active high for a SPI port.
117 *
118 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for
119 * MXP
120 * @param[out] status the error code, or 0 for success
121 */
122void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status);
123
124/**
125 * Sets the CS Active low for a SPI port.
126 *
127 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
128 * for MXP
129 * @param[out] status the error code, or 0 for success
130 */
131void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status);
132
133/**
134 * Gets the stored handle for a SPI port.
135 *
136 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
137 * @return The stored handle for the SPI port. 0 represents no stored
138 * handle.
139 */
141
142/**
143 * Sets the stored handle for a SPI port.
144 *
145 * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for
146 * MXP.
147 * @param handle The value of the handle for the port.
148 */
149void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle);
150
151/**
152 * Initializes the SPI automatic accumulator.
153 *
154 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2,
155 * 4 for MXP.
156 * @param[in] bufferSize The accumulator buffer size.
157 * @param[out] status the error code, or 0 for success
158 */
159void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status);
160
161/**
162 * Frees an SPI automatic accumulator.
163 *
164 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
165 * for MXP.
166 * @param[out] status the error code, or 0 for success
167 */
168void HAL_FreeSPIAuto(HAL_SPIPort port, int32_t* status);
169
170/**
171 * Sets the period for automatic SPI accumulation.
172 *
173 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
174 * for MXP.
175 * @param[in] period The accumulation period (seconds).
176 * @param[out] status the error code, or 0 for success
177 */
178void HAL_StartSPIAutoRate(HAL_SPIPort port, double period, int32_t* status);
179
180/**
181 * Starts the auto SPI accumulator on a specific trigger.
182 *
183 * Note that triggering on both rising and falling edges is a valid
184 * configuration.
185 *
186 * @param[in] port The number of the port to use. 0-3 for Onboard
187 * CS0-CS2, 4 for MXP.
188 * @param[in] digitalSourceHandle The trigger source to use (Either
189 * HAL_AnalogTriggerHandle or HAL_DigitalHandle).
190 * @param[in] analogTriggerType The analog trigger type, if the source is an
191 * analog trigger.
192 * @param[in] triggerRising Trigger on the rising edge if true.
193 * @param[in] triggerFalling Trigger on the falling edge if true.
194 * @param[out] status the error code, or 0 for success
195 */
196void HAL_StartSPIAutoTrigger(HAL_SPIPort port, HAL_Handle digitalSourceHandle,
197 HAL_AnalogTriggerType analogTriggerType,
198 HAL_Bool triggerRising, HAL_Bool triggerFalling,
199 int32_t* status);
200
201/**
202 * Stops an automatic SPI accumulation.
203 *
204 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
205 * for MXP.
206 * @param[out] status the error code, or 0 for success
207 */
208void HAL_StopSPIAuto(HAL_SPIPort port, int32_t* status);
209
210/**
211 * Sets the data to be transmitted to the device to initiate a read.
212 *
213 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2,
214 * 4 for MXP.
215 * @param[in] dataToSend Pointer to the data to send (Gets copied for continue
216 * use, so no need to keep alive).
217 * @param[in] dataSize The length of the data to send.
218 * @param[in] zeroSize The number of zeros to send after the data.
219 * @param[out] status the error code, or 0 for success
220 */
221void HAL_SetSPIAutoTransmitData(HAL_SPIPort port, const uint8_t* dataToSend,
222 int32_t dataSize, int32_t zeroSize,
223 int32_t* status);
224
225/**
226 * Immediately forces an SPI read to happen.
227 *
228 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
229 * for MXP.
230 * @param[out] status the error code, or 0 for success
231 */
232void HAL_ForceSPIAutoRead(HAL_SPIPort port, int32_t* status);
233
234/**
235 * Reads data received by the SPI accumulator. Each received data sequence
236 * consists of a timestamp followed by the received data bytes, one byte per
237 * word (in the least significant byte). The length of each received data
238 * sequence is the same as the combined dataSize + zeroSize set in
239 * HAL_SetSPIAutoTransmitData.
240 *
241 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2,
242 * 4 for MXP.
243 * @param[out] buffer The buffer to store the data into.
244 * @param[in] numToRead The number of words to read.
245 * @param[in] timeout The read timeout (in seconds).
246 * @param[out] status the error code, or 0 for success
247 * @return The number of words actually read.
248 */
249int32_t HAL_ReadSPIAutoReceivedData(HAL_SPIPort port, uint32_t* buffer,
250 int32_t numToRead, double timeout,
251 int32_t* status);
252
253/**
254 * Gets the count of how many SPI accumulations have been missed.
255 *
256 * @param[in] port The number of the port to use. 0-3 for Onboard CS0-CS2, 4
257 * for MXP.
258 * @param[out] status the error code, or 0 for success
259 * @return The number of missed accumulations.
260 */
261int32_t HAL_GetSPIAutoDroppedCount(HAL_SPIPort port, int32_t* status);
262
263/**
264 * Configure the Auto SPI Stall time between reads.
265 *
266 * @param[in] port The number of the port to use. 0-3 for Onboard
267 * CS0-CS2, 4 for MXP.
268 * @param[in] csToSclkTicks the number of ticks to wait before asserting the
269 * cs pin
270 * @param[in] stallTicks the number of ticks to stall for
271 * @param[in] pow2BytesPerRead the number of bytes to read before stalling
272 * @param[out] status the error code, or 0 for success
273 */
274void HAL_ConfigureSPIAutoStall(HAL_SPIPort port, int32_t csToSclkTicks,
275 int32_t stallTicks, int32_t pow2BytesPerRead,
276 int32_t* status);
277
278#ifdef __cplusplus
279} // extern "C"
280#endif
281/** @} */
HAL_AnalogTriggerType
The type of analog trigger to trigger on.
Definition: AnalogTrigger.h:20
int32_t HAL_TransactionSPI(HAL_SPIPort port, const uint8_t *dataToSend, uint8_t *dataReceived, int32_t size)
Performs an SPI send/receive transaction.
void HAL_StopSPIAuto(HAL_SPIPort port, int32_t *status)
Stops an automatic SPI accumulation.
void HAL_FreeSPIAuto(HAL_SPIPort port, int32_t *status)
Frees an SPI automatic accumulator.
void HAL_SetSPIAutoTransmitData(HAL_SPIPort port, const uint8_t *dataToSend, int32_t dataSize, int32_t zeroSize, int32_t *status)
Sets the data to be transmitted to the device to initiate a read.
int32_t HAL_GetSPIHandle(HAL_SPIPort port)
Gets the stored handle for a SPI port.
void HAL_ConfigureSPIAutoStall(HAL_SPIPort port, int32_t csToSclkTicks, int32_t stallTicks, int32_t pow2BytesPerRead, int32_t *status)
Configure the Auto SPI Stall time between reads.
int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t *buffer, int32_t count)
Executes a read from the device.
void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t *status)
Sets the CS Active low for a SPI port.
int32_t HAL_ReadSPIAutoReceivedData(HAL_SPIPort port, uint32_t *buffer, int32_t numToRead, double timeout, int32_t *status)
Reads data received by the SPI accumulator.
void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t *status)
Initializes the SPI automatic accumulator.
int32_t HAL_WriteSPI(HAL_SPIPort port, const uint8_t *dataToSend, int32_t sendSize)
Executes a write transaction with the device.
void HAL_InitializeSPI(HAL_SPIPort port, int32_t *status)
Initializes the SPI port.
void HAL_CloseSPI(HAL_SPIPort port)
Closes the SPI port.
HAL_SPIMode HAL_GetSPIMode(HAL_SPIPort port)
Gets the SPI Mode.
void HAL_ForceSPIAutoRead(HAL_SPIPort port, int32_t *status)
Immediately forces an SPI read to happen.
void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed)
Sets the clock speed for the SPI bus.
HAL_SPIMode
Definition: SPITypes.h:26
void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle)
Sets the stored handle for a SPI port.
void HAL_SetSPIMode(HAL_SPIPort port, HAL_SPIMode mode)
Sets the SPI Mode.
void HAL_StartSPIAutoTrigger(HAL_SPIPort port, HAL_Handle digitalSourceHandle, HAL_AnalogTriggerType analogTriggerType, HAL_Bool triggerRising, HAL_Bool triggerFalling, int32_t *status)
Starts the auto SPI accumulator on a specific trigger.
int32_t HAL_GetSPIAutoDroppedCount(HAL_SPIPort port, int32_t *status)
Gets the count of how many SPI accumulations have been missed.
void HAL_StartSPIAutoRate(HAL_SPIPort port, double period, int32_t *status)
Sets the period for automatic SPI accumulation.
void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t *status)
Sets the CS Active high for a SPI port.
HAL_SPIPort
Definition: SPITypes.h:17
int32_t HAL_Bool
Definition: Types.h:73
int32_t HAL_Handle
Definition: Types.h:17
constexpr auto count() -> size_t
Definition: core.h:1203