MAX78002 Peripheral Driver API
Peripheral Driver API for the MAX78002
spi.h
1
6/******************************************************************************
7 *
8 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9 * Analog Devices, Inc.),
10 * Copyright (C) 2023-2024 Analog Devices, Inc.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 ******************************************************************************/
25
26#ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_SPI_H_
27#define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_SPI_H_
28
29/***** includes *******/
30#include <stdbool.h>
31#include "mxc_assert.h"
32#include "mxc_device.h"
33#include "mxc_lock.h"
34#include "mxc_pins.h"
35#include "mxc_sys.h"
36#include "gpio.h"
37#include "spi_regs.h"
38#include "dma_regs.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
50/***** Definitions *****/
51
52// clang-format off
53
57typedef enum {
58 MXC_SPI_TYPE_SLAVE = 0,
59 MXC_SPI_TYPE_TARGET = 0,
60 MXC_SPI_TYPE_MASTER = 1,
61 MXC_SPI_TYPE_CONTROLLER = 1
63
68typedef enum {
69 MXC_SPI_TSCONTROL_HW_AUTO = 0, // Automatically by hardware
70 MXC_SPI_TSCONTROL_SW_APP = 1 // Through software in the application
72
76typedef enum {
77 MXC_SPI_STATE_READY = 0, // Ready for transaction
78 MXC_SPI_STATE_BUSY = 1 // Busy transferring
80
84typedef enum {
85 MXC_SPI_INTERFACE_STANDARD = 0,
86 MXC_SPI_INTERFACE_4WIRE = 0,
87 MXC_SPI_INTERFACE_QUAD = 1,
88 MXC_SPI_INTERFACE_3WIRE = 2,
89 MXC_SPI_INTERFACE_DUAL = 3
91
102typedef enum {
103 MXC_SPI_CLKMODE_0 = 0, // CPOL: 0 CPHA: 0
104 MXC_SPI_CLKMODE_1 = 1, // CPOL: 0 CPHA: 1
105 MXC_SPI_CLKMODE_2 = 2, // CPOL: 1 CPHA: 0
106 MXC_SPI_CLKMODE_3 = 3 // CPOL: 1 CPHA: 1
108
110
115typedef enum {
121
133typedef enum {
140
141typedef struct _mxc_spi_req_t mxc_spi_req_t;
142
149typedef void (*mxc_spi_callback_t)(void *, int result);
150typedef mxc_spi_callback_t spi_complete_cb_t; // Support SPI v1 name.
151
152typedef struct _mxc_spi_pins_t mxc_spi_pins_t;
153struct _mxc_spi_pins_t {
154 bool ss0;
155 bool ss1;
156 bool ss2;
157
158 bool vddioh;
159
160 bool clock;
161 bool miso;
162 bool mosi;
163 bool sdio2;
164 bool sdio3;
165
166 mxc_gpio_drvstr_t drvstr;
167};
168
169typedef struct {
170 // SPI Settings.
171 mxc_spi_regs_t *spi; // Selected SPI Instance
172 mxc_spi_clkmode_t clk_mode; // Clock modes
173 uint8_t frame_size; // Number of bits per character sent
174
175 // DMA Settings.
176 bool use_dma_tx; // Enable DMA TX.
177 bool use_dma_rx; // Enable DMA RX. (use_dma_tx must be true to use DMA RX).
178 mxc_dma_regs_t *dma; // Select DMA instance for SPI DMA.
179} mxc_spi_cfg_t;
180
181// Suppport names for backwards compatibility.
182struct _mxc_spi_req_t {
183 mxc_spi_regs_t *spi; // Pointer to SPI registers
184 int ssIdx;
185 int ssDeassert;
186 uint8_t *txData;
187 uint8_t *rxData;
188 uint32_t txLen; // Number of frames to be stored in txData
189 uint32_t rxLen; // Number of frames to be stored in rxData
190 uint32_t txCnt; // Number of bytes transmitted from txData (Unused for SPI v2)
191 uint32_t rxCnt; // Number of bytes stored in rxData (Unused for SPI v2)
192 mxc_spi_callback_t completeCB; // completeCB
193 uint16_t txDummyValue; // Value of dummy bytes to be sent
194};
195// clang-format on
196
197/* ************************************************************************* */
198/* Control/Configuration functions */
199/* ************************************************************************* */
200
240 int numTargets, uint8_t ts_active_pol_mask, uint32_t freq, mxc_spi_pins_t pins);
241
255int MXC_SPI_Config(mxc_spi_cfg_t *cfg);
256
278int MXC_SPI_ConfigStruct(mxc_spi_cfg_t *cfg, bool use_dma_tx, bool use_dma_rx);
279
288
300
310
320void MXC_SPI_EnableInt(mxc_spi_regs_t *spi, unsigned int intEn);
321
331void MXC_SPI_DisableInt(mxc_spi_regs_t *spi, unsigned int intDis);
332
341
351
362int MXC_SPI_SetFrequency(mxc_spi_regs_t *spi, unsigned int hz);
363
375
384int MXC_SPI_SetFrameSize(mxc_spi_regs_t *spi, int frame_size);
385
395
408
419
429
439
450
461
470
484
495
506
515
524
531
538
556int MXC_SPI_SetTXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes);
557
575int MXC_SPI_SetRXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes);
576
585
594
596
604int MXC_SPI_SetDataSize(mxc_spi_regs_t *spi, int dataSize);
605
614
624
633
644int MXC_SPI_SetSlave(mxc_spi_regs_t *spi, int ssIdx);
645
656
666
675
685unsigned int MXC_SPI_WriteTXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len);
686
696unsigned int MXC_SPI_ReadRXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len);
697
709int MXC_SPI_SetDefaultTXData(mxc_spi_regs_t *spi, unsigned int defaultTXData);
710
723
724/* ** DMA Functions ** */
725
739int MXC_SPI_DMA_Init(mxc_spi_regs_t *spi, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx);
740
750
760
770
780int MXC_SPI_DMA_SetRequestSelect(mxc_spi_regs_t *spi, bool use_dma_tx, bool use_dma_rx);
781
782/* ** Transaction Functions ** */
783
807int MXC_SPI_MasterTransaction(mxc_spi_req_t *req);
808
819int MXC_SPI_MasterTransactionAsync(mxc_spi_req_t *req);
820
836int MXC_SPI_MasterTransactionDMA(mxc_spi_req_t *req);
837
845int MXC_SPI_ControllerTransaction(mxc_spi_req_t *req);
846
858
870int MXC_SPI_ControllerTransactionDMA(mxc_spi_req_t *req);
871
879int MXC_SPI_ControllerTransactionDMAB(mxc_spi_req_t *req);
880
888int MXC_SPI_SlaveTransaction(mxc_spi_req_t *req);
889
897int MXC_SPI_SlaveTransactionAsync(mxc_spi_req_t *req);
898
914int MXC_SPI_SlaveTransactionDMA(mxc_spi_req_t *req);
915
923int MXC_SPI_TargetTransaction(mxc_spi_req_t *req);
924
932int MXC_SPI_TargetTransactionAsync(mxc_spi_req_t *req);
933
945int MXC_SPI_TargetTransactionDMA(mxc_spi_req_t *req);
946
947/* ** Handler Functions ** */
948
959
970
979
988
991#ifdef __cplusplus
992}
993#endif
994
995#endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_SPI_H_
Registers, Bit Masks and Bit Positions for the DMA Peripheral Module.
mxc_gpio_drvstr_t
Enumeration type for drive strength on a given pin. This represents what the two GPIO_DS[2] (Drive St...
Definition: gpio.h:144
Definition: spi_regs.h:76
int MXC_SPI_ControllerTransaction(mxc_spi_req_t *req)
Set up a blocking, non-interrupt-driven SPI controller transaction.
void MXC_SPI_DMA_TX_Handler(mxc_spi_regs_t *spi)
The processing function for DMA TX transactions.
int MXC_SPI_SetFrequency(mxc_spi_regs_t *spi, unsigned int hz)
Set the frequency of the SPI interface.
int MXC_SPI_Init(mxc_spi_regs_t *spi, mxc_spi_type_t controller_target, mxc_spi_interface_t if_mode, int numTargets, uint8_t ts_active_pol_mask, uint32_t freq, mxc_spi_pins_t pins)
Initialize and enable SPI peripheral.
unsigned int MXC_SPI_GetFlags(mxc_spi_regs_t *spi)
Gets the interrupt flags that are currently set.
int MXC_SPI_GetSlave(mxc_spi_regs_t *spi)
Gets the slave select (SS) line used for transmissions.
int MXC_SPI_DMA_SetRequestSelect(mxc_spi_regs_t *spi, bool use_dma_tx, bool use_dma_rx)
Sets the SPI instance's DMA TX/RX request select.
bool MXC_SPI_DMA_GetInitialized(mxc_spi_regs_t *spi)
Helper function that checks whether the MXC_SPI_Init function cfgalized DMA for SPI DMA transactons.
unsigned int MXC_SPI_GetTXFIFOAvailable(mxc_spi_regs_t *spi)
Get the amount of free space available in the transmit FIFO.
void MXC_SPI_EnableInt(mxc_spi_regs_t *spi, unsigned int intEn)
Enables specific interrupts.
void MXC_SPI_ClearFlags(mxc_spi_regs_t *spi)
Clears the interrupt flags that are currently set.
mxc_spi_interface_t
The list of supported SPI Interface Modes.
Definition: spi.h:84
unsigned int MXC_SPI_ReadRXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len)
Unloads bytes from the receive FIFO.
int MXC_SPI_SetRXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes)
Set the receive threshold level.
int MXC_SPI_SetTSControl(mxc_spi_regs_t *spi, mxc_spi_tscontrol_t ts_control)
Configures the Pre-defined SPI Target Select pins for a specific instance.
mxc_spi_interface_t MXC_SPI_GetInterface(mxc_spi_regs_t *spi)
Gets the SPI interface mode used for transmissions.
int MXC_SPI_SetWidth(mxc_spi_regs_t *spi, mxc_spi_width_t spiWidth)
Sets the SPI width used for transmissions.
int MXC_SPI_SetSlave(mxc_spi_regs_t *spi, int ssIdx)
Sets the slave select (SS) line used for transmissions.
int MXC_SPI_GetDataSize(mxc_spi_regs_t *spi)
Gets the number of bits per character.
unsigned int MXC_SPI_GetTXThreshold(mxc_spi_regs_t *spi)
Get the current transmit threshold level.
int MXC_SPI_GetPeripheralClock(mxc_spi_regs_t *spi)
Returns the frequency of the clock used as the bit rate generator for a given SPI instance.
int MXC_SPI_SetDataSize(mxc_spi_regs_t *spi, int dataSize)
Sets the number of bits per character.
mxc_spi_tscontrol_t
The list of Target Select Control Scheme Options for target assertion/deassertion.
Definition: spi.h:68
int MXC_SPI_ReadyForSleep(mxc_spi_regs_t *spi)
Checks whether the SPI instance is ready for sleep.
int MXC_SPI_SetInterface(mxc_spi_regs_t *spi, mxc_spi_interface_t if_mode)
Sets the SPI interface mode used for transmissions.
mxc_spi_type_t
The list of types for the SPI peripheral.
Definition: spi.h:57
mxc_spi_mode_t MXC_SPI_GetMode(mxc_spi_regs_t *spi)
Gets the spi mode.
unsigned int MXC_SPI_GetRXFIFOAvailable(mxc_spi_regs_t *spi)
Get the number of bytes currently available in the receive FIFO.
int MXC_SPI_ControllerTransactionAsync(mxc_spi_req_t *req)
Set up a non-blocking, interrupt-driven SPI controller transaction.
int MXC_SPI_GetFrameSize(mxc_spi_regs_t *spi)
Gets the number of bits per frame.
int MXC_SPI_DMA_Init(mxc_spi_regs_t *spi, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx)
This function initializes the DMA for SPI DMA transactions.
mxc_spi_mode_t
The list of SPI modes.
Definition: spi.h:133
int MXC_SPI_SlaveTransactionAsync(mxc_spi_req_t *req)
Setup an interrupt-driven SPI transaction.
unsigned int MXC_SPI_GetFrequency(mxc_spi_regs_t *spi)
Get the frequency of the SPI interface.
void MXC_SPI_ClearTXFIFO(mxc_spi_regs_t *spi)
Removes and discards all bytes currently in the transmit FIFO.
void MXC_SPI_Handler(mxc_spi_regs_t *spi)
The processing function for asynchronous transactions.
int MXC_SPI_MasterTransactionAsync(mxc_spi_req_t *req)
Setup an interrupt-driven SPI transaction.
int MXC_SPI_Config(mxc_spi_cfg_t *cfg)
Configure the SPI peripheral.
int MXC_SPI_ControllerTransactionDMA(mxc_spi_req_t *req)
Set up a non-blocking, DMA-driven SPI controller transaction.
mxc_spi_width_t
Definition: spi.h:115
int MXC_SPI_SetTXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes)
Set the transmit threshold level.
int MXC_SPI_SetFrameSize(mxc_spi_regs_t *spi, int frame_size)
Sets the number of bits per frame.
int MXC_SPI_Shutdown(mxc_spi_regs_t *spi)
Disable and shutdown the SPI instance.
int MXC_SPI_SlaveTransactionDMA(mxc_spi_req_t *req)
Setup a DMA driven SPI transaction.
mxc_spi_width_t MXC_SPI_GetWidth(mxc_spi_regs_t *spi)
Gets the SPI width used for transmissions.
int MXC_SPI_SlaveTransaction(mxc_spi_req_t *req)
Performs a blocking SPI transaction.
int MXC_SPI_SetCallback(mxc_spi_regs_t *spi, mxc_spi_callback_t callback, void *data)
Sets the SPI instance's callback function.
void MXC_SPI_DMA_RX_Handler(mxc_spi_regs_t *spi)
The processing function for DMA RX transactions.
int MXC_SPI_SetMode(mxc_spi_regs_t *spi, mxc_spi_mode_t spiMode)
Sets the spi mode using clock polarity and clock phase.
void MXC_SPI_AsyncHandler(mxc_spi_regs_t *spi)
The processing function for asynchronous transactions.
int MXC_SPI_GetActive(mxc_spi_regs_t *spi)
Checks the SPI instance for an ongoing transmission.
void MXC_SPI_ClearRXFIFO(mxc_spi_regs_t *spi)
Removes and discards all bytes currently in the receive FIFO.
int MXC_SPI_AbortTransmission(mxc_spi_regs_t *spi)
Aborts an ongoing SPI Transmission.
int MXC_SPI_DMA_GetRXChannel(mxc_spi_regs_t *spi)
Retreive the DMA RX Channel associated with SPI instance.
void MXC_SPI_DisableInt(mxc_spi_regs_t *spi, unsigned int intDis)
Disables specific interrupts.
int MXC_SPI_TargetTransactionDMA(mxc_spi_req_t *req)
Setup a DMA-driven SPI Target transaction.
int MXC_SPI_ControllerTransactionDMAB(mxc_spi_req_t *req)
Set up a blocking, DMA-driven SPI controller transaction.
void(* mxc_spi_callback_t)(void *, int result)
The callback routine used to indicate the transaction has terminated.
Definition: spi.h:149
unsigned int MXC_SPI_WriteTXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len)
Loads bytes into the transmit FIFO.
int MXC_SPI_SetDefaultTXData(mxc_spi_regs_t *spi, unsigned int defaultTXData)
Sets the TX data to transmit as a 'dummy' byte.
void MXC_SPI_AbortAsync(mxc_spi_regs_t *spi)
Abort any asynchronous requests in progress.
int MXC_SPI_DMA_GetTXChannel(mxc_spi_regs_t *spi)
Retreive the DMA TX Channel associated with SPI instance.
int MXC_SPI_TargetTransaction(mxc_spi_req_t *req)
Setup a blocking SPI Target transaction.
int MXC_SPI_SetClkMode(mxc_spi_regs_t *spi, mxc_spi_clkmode_t clk_mode)
Sets the SPI clock mode (clock polarity and clock phase).
int MXC_SPI_TargetTransactionAsync(mxc_spi_req_t *req)
Setup an interrupt-driven, non-blocking SPI Target transaction.
mxc_spi_clkmode_t
The list of SPI clock modes.
Definition: spi.h:102
unsigned int MXC_SPI_GetRXThreshold(mxc_spi_regs_t *spi)
Get the current receive threshold level.
mxc_spi_state_t
The list of possible states for an SPI instance.
Definition: spi.h:76
int MXC_SPI_StartTransmission(mxc_spi_regs_t *spi)
Starts a SPI Transmission.
int MXC_SPI_MasterTransactionDMA(mxc_spi_req_t *req)
Setup a DMA driven SPI transaction.
int MXC_SPI_MasterTransaction(mxc_spi_req_t *req)
Performs a blocking SPI transaction.
mxc_spi_clkmode_t MXC_SPI_GetClkMode(mxc_spi_regs_t *spi)
Gets the SPI clock mode (clock polarity and clock phase).
void MXC_SPI_HWSSControl(mxc_spi_regs_t *spi, int state)
Enable/Disable HW CS control feature.
int MXC_SPI_ConfigStruct(mxc_spi_cfg_t *cfg, bool use_dma_tx, bool use_dma_rx)
Overwrite the cfg struct with default values.
@ SPI_MODE_0
clock phase = 0, clock polarity = 0
Definition: spi.h:134
@ SPI_MODE_2
clock phase = 1, clock polarity = 0
Definition: spi.h:136
@ SPI_MODE_1
clock phase = 0, clock polarity = 1
Definition: spi.h:135
@ SPI_MODE_3
clock phase = 1, clock polarity = 1
Definition: spi.h:137
@ SPI_WIDTH_QUAD
4 Data lines, half duplex
Definition: spi.h:119
@ SPI_WIDTH_STANDARD
MISO/MOSI, full duplex.
Definition: spi.h:117
@ SPI_WIDTH_3WIRE
1 Data line, half duplex
Definition: spi.h:116
@ SPI_WIDTH_DUAL
2 Data lines, half duplex
Definition: spi.h:118
System level header file.
Registers, Bit Masks and Bit Positions for the SPI Peripheral Module.