WXK
2024-12-16 78e84fcf264afd731cd66c807d9fcb690fe12126
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**************************************************************************//**
* @file     pan_hal_spi.h
* @version  V0.0.0
* $Revision: 1 $
* $Date:    23/09/10 $
* @brief    Panchip series SPI HAL (Hardware Abstraction Layer) header file.
*
* @note
* Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
*****************************************************************************/
 
#ifndef _PAN_HAL_SPI_H_
#define _PAN_HAL_SPI_H_
 
#include "pan_hal_def.h"
 
/**
 * @brief SPI HAL Interface
 * @defgroup spi_hal_interface Spi HAL Interface
 * @{
 */
 
// Config SPI software buffer size
#define SPI_RX_BUF_SIZE         256
#define SPI_TX_BUF_SIZE         256
 
/**
 * @brief  Structure to configure SPI interrupt mode.
 */
typedef struct
{
    SPI_IrqDef Mode; /*!< SPI interrupt mode. */
} SPI_InterruptOptDef;
 
/**
 * @brief  Enumeration defining SPI baud rates.
 */
typedef enum {
    BAUDRATE_100K   = 100000,   /*!< Baud rate: 100,000 bps. */
    BAUDRATE_500K   = 500000,   /*!< Baud rate: 500,000 bps. */
    BAUDRATE_1M     = 1000000,  /*!< Baud rate: 1,000,000 bps. */
    BAUDRATE_2M     = 2000000,  /*!< Baud rate: 2,000,000 bps. */
    BAUDRATE_5M     = 5000000,  /*!< Baud rate: 5,000,000 bps. */
    BAUDRATE_10M    = 10000000  /*!< Baud rate: 10,000,000 bps. */
} SPI_BaudRate;
 
/**
 * @brief  SPI Init Structure definition
 */
typedef struct
{
  SPI_RoleDef role;              /*!< Specifies the SPI operating mode */
  SPI_DataFrameSizeDef dataSize; /*!< Specifies the SPI data size */
  SPI_ClockPolDef clkPolarity;   /*!< Specifies the serial clock steady state */
  SPI_ClockPhaseDef clkPhase;    /*!< Specifies the clock active edge for the bit capture */
  // SPI_BaudRate Baudrate;      /*!< Specifies the Baud Rate Prescaler used to configure the transmit and receive SCK clock. */
  SPI_BaudRateDivDef baudrateDiv; /*!< Specifies the Baud Rate Prescaler used to configure the transmit and receive SCK clock. */
  SPI_FrameFormatDef format;      /*!< Specifies the SPI format */
} SPI_InitOptDef;
 
 
typedef enum{
    SPI_STAT_NULL,
    SPI_STAT_RX= 0x10,
    SPI_STAT_TX = 0x01,
    SPI_STAT_RXTX = 0x11,
}SPI_XferStatOpt;
 
/**
 * @brief  SPI handle Structure definition
 */
typedef struct
{
  SPI_T *pSpix; /*!< SPI registers base address        */
 
  SPI_InitOptDef initObj;           /*!< SPI communication parameters      */
  SPI_XferStatOpt xferStat;    /*!< SPI transfer status               */
  SPI_InterruptOptDef interruptObj; /*!< SPI interrupt options           */
 
  uint16_t *pTxBuffPtr; /*!< Pointer to SPI Tx transfer buffer */
  uint16_t txXferSize; /*!< SPI Tx transfer size              */
  __IO uint16_t txXferCount; /*!< SPI Tx transfer counter           */
 
  uint16_t *pRxBuffPtr; /*!< Pointer to SPI Rx transfer buffer */
  uint16_t rxXferSize; /*!< SPI Rx transfer size              */
  __IO uint16_t rxXferCount; /*!< SPI Rx transfer counter           */
 
  IRQn_Type IRQn; /*!< SPI IRQ number                    */
  SPI_CallbackFunc rxIntCallback; /*!< Callback function for Rx interrupt */
  SPI_CallbackFunc txIntCallback; /*!< Callback function for Tx interrupt */
 
  uint32_t dmaSrc;         /*!< DMA source address for SPI data transfer */
  uint32_t dmaDst;         /*!< DMA destination address for SPI data transfer */
 
  __IO uint32_t errorCode; /*!< SPI Error code                    */
 
} SPI_HandleTypeDef;
 
/**
 * @brief Array of SPI handle structures for managing multiple SPI interfaces.
 *
 * You can access specific SPI handles using SPI0_OBJ and SPI1_OBJ aliases.
 */
extern SPI_HandleTypeDef spiHandleArray[2];
 
/**
 * @brief SPI handle for the first SPI interface (SPI0).
 *
 * You can use this handle to configure and manage SPI0.
 */
#define SPI0_OBJ spiHandleArray[0]
 
/**
 * @brief SPI handle for the second SPI interface (SPI1).
 *
 * You can use this handle to configure and manage SPI1.
 */
#define SPI1_OBJ spiHandleArray[1]
 
/**
 * @brief Initialize the SPI module.
 * @param spi Pointer to the SPI handle structure.
 */
void HAL_SPI_Init(SPI_HandleTypeDef *pSpi);
 
/**
 * @brief DeInitialize the SPI module.
 * @param spi Pointer to the SPI handle structure.
 */
void HAL_SPI_DeInit(SPI_HandleTypeDef *pSpi);
 
/**
 * @brief Initialize SPI interrupts.
 * @param spi Pointer to the SPI handle structure.
 */
void HAL_SPI_Init_INT(SPI_HandleTypeDef *pSpi);
 
/**
 * @brief Initialize SPI interrupts.
 * @param spi Pointer to the SPI handle structure.
 */
void HAL_SPI_DeInit_INT(SPI_HandleTypeDef *pSpi);
 
/**
 * @brief Send data over SPI.
 * @param spi Pointer to the SPI handle structure.
 * @param Buf Pointer to the data buffer to send.
 * @param Size Size of the data to send.
 * @param Timeout Timeout for the send operation.
 */
void HAL_SPI_SendData(SPI_HandleTypeDef *pSpi, uint16_t *pBuf, size_t size, uint32_t timeout); 
 
/**
 * @brief Receive data from SPI.
 * @param spi Pointer to the SPI handle structure.
 * @param Buf Pointer to the buffer to store received data.
 * @param Size Size of the data to receive.
 * @param Timeout Timeout for the receive operation.
 */
void HAL_SPI_ReceiveData(SPI_HandleTypeDef *pSpi, uint16_t *pBuf, size_t size, uint32_t timeout); 
 
/**
 * @brief Send and receive data over SPI.
 * @param spi Pointer to the SPI handle structure.
 * @param SendBuf Pointer to the data buffer to send.
 * @param SendBufSize Size of the data to send.
 * @param RecvBuf Pointer to the buffer to store received data.
 * @param RecvBufSize Size of the data to receive.
 */
void HAL_SPI_SendReceiveData (SPI_HandleTypeDef *pSpi, uint16_t* pSendBuf, size_t sendBufSize, uint16_t* pRecvBuf, size_t recvBufSize);
/**
 * @brief Send data over SPI with interrupt support.
 * @param spi Pointer to the SPI handle structure.
 * @param pBuf Pointer to the data buffer to send.
 * @param size Size of the data to send.
 * @param callback Callback function to handle the transmit interrupt.
 */
void HAL_SPI_SendData_INT(SPI_HandleTypeDef *pSpi, uint16_t *pBuf, size_t size, SPI_CallbackFunc callback);
/**
 * @brief Receive data over SPI with interrupt support.
 * @param spi Pointer to the SPI handle structure.
 * @param pBuf Pointer to the data buffer to store received data.
 * @param size Size of the data to receive.
 * @param callback Callback function to handle the receive interrupt.
 */
void HAL_SPI_ReceiveData_INT(SPI_HandleTypeDef *pSpi, uint16_t* pBuf, size_t size,SPI_CallbackFunc callback);
 
/**
 * @brief Send and receive data over SPI with interrupt support.
 * @param spi Pointer to the SPI handle structure.
 * @param SendBuf Pointer to the data buffer to send.
 * @param SendBufSize Size of the data to send.
 * @param SendCallback Callback function to handle the transmit interrupt.
 * @param RecvBuf Pointer to the buffer to store received data.
 * @param RecvBufSize Size of the data to receive.
 * @param RecvCallback Callback function to handle the receive interrupt.
 */
void HAL_SPI_SendReceiveData_INT(SPI_HandleTypeDef *pSpi, uint16_t* pSendBuf, size_t sendBufSize,SPI_CallbackFunc sendCallback, uint16_t* pRecvBuf, size_t recvBufSize,SPI_CallbackFunc recvCallback);
 
/**
 * @brief Send data over SPI using DMA for improved performance.
 * @param spi Pointer to the SPI handle structure.
 * @param Buf Pointer to the data buffer to send.
 * @param Size Size of the data to send.
 * @param Callback Callback function to handle the DMA transfer completion.
 * @return DMA channel number used for the transfer.
 */
uint32_t HAL_SPI_SendData_DMA(SPI_HandleTypeDef *pSpi, uint16_t *pBuf, size_t size,SPI_CallbackFunc callback);
 
/**
 * @brief Receive data over SPI using DMA for improved performance.
 * @param spi Pointer to the SPI handle structure.
 * @param Buf Pointer to the data buffer to send.
 * @param Size Size of the data to send.
 * @param Callback Callback function to handle the DMA transfer completion.
 * @return DMA channel number used for the transfer.
 */
uint32_t HAL_SPI_ReceiveData_DMA(SPI_HandleTypeDef *pSpi, uint16_t *pBuf, size_t size, SPI_CallbackFunc callback);
/**
 * @brief Send and receive data over SPI using DMA for improved performance.
 * @param spi Pointer to the SPI handle structure.
 * @param SendBuf Pointer to the data buffer to send.
 * @param SendBufSize Size of the data to send.
 * @param SendCallback Callback function to handle the DMA transmit completion.
 * @param RecvBuf Pointer to the buffer to store received data.
 * @param RecvBufSize Size of the data to receive.
 * @param RecvCallback Callback function to handle the DMA receive completion.
 */
void HAL_SPI_SendReceiveData_DMA(SPI_HandleTypeDef *pSpi, uint16_t* pSendBuf, size_t sendBufSize,SPI_CallbackFunc sendCallback, uint16_t* pRecvBuf, size_t recvBufSize,SPI_CallbackFunc recvCallback);
/** @} */ // end of group
#endif