-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi_helper.h
57 lines (48 loc) · 2.25 KB
/
spi_helper.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
#ifndef SPI_HELPER_H
#define SPI_HELPER_H
#include "fsl_lpspi.h"
#include "fsl_lpspi_freertos.h"
/// @define SPI instance
#define LPSPI_MASTER_BASEADDR (LPSPI0)
/// @define SPI clock name
#define LPSPI_MASTER_CLOCK_NAME (kCLOCK_Lpspi0)
/// @define SPI clock source
#define LPSPI_MASTER_CLOCK_SOURCE (kCLOCK_IpSrcFircAsync)
/// @define SPI clock frequency
#define LPSPI_MASTER_CLOCK_FREQ (CLOCK_GetIpFreq(LPSPI_MASTER_CLOCK_NAME))
/// @define SPI chip select pin for initialization
#define LPSPI_MASTER_PCS_FOR_INIT (kLPSPI_Pcs2)
/// @define SPI chip select for data transfer
#define LPSPI_MASTER_PCS_FOR_TRANSFER (kLPSPI_MasterPcs2)
/// @define SPI transfer baud rate (bps)
#define TRANSFER_BAUDRATE (50000U)
/// @define Number of bits per each SPI data frame
#define BITS_PER_FRAME (8);
/// @define Nanosecond divisor
#define NANOSECOND_DIVISOR (1000000000)
/// @brief Master SPI RTOS handler
lpspi_rtos_handle_t master_rtos_handle;
/// @brief Master SPI configuration
lpspi_master_config_t master_config;
/// @brief Initialize the SPI
/// @returns '0' if the initialization was successful; otherwise '-1'
int Initialize_SPI(void);
/// @brief Transfer data (read and write) via SPI
/// @param[out] rx_buffer Receive buffer
/// @param[in] tx_buffer Transmission buffer
/// @param buffer_size Minimum transmission buffer and receive buffer size
/// @returns '0' if the transfer was successful; otherwise '-1'
/// @note For read-only or write-only transmission, 'tx_buffer' or 'rx_buffer' can be 'NULL' respectively.
int Transfer_SPI(uint8_t* rx_buffer, uint8_t* tx_buffer, size_t buffer_size);
/// @brief Transfer data (read and write) via SPI using a polling mechanism
/// @param[out] rx_buffer Receive buffer
/// @param[in] tx_buffer Transmission buffer
/// @param buffer_size Minimum transmission buffer and receive buffer size
/// @param timeout Polling timeout in milliseconds
/// @returns '0' if the transfer was successful within the given timeout; otherwise a negative number
/// @remarks The function meant to be used within a loop.
/// @note For read-only or write-only transmission, 'tx_buffer' or 'rx_buffer' can be 'NULL' respectively.
int Poll_SPI(uint8_t* rx_buffer, uint8_t* tx_buffer, size_t buffer_size, uint32_t timeout);
/// @brief Dispose the SPI
void Dispose_SPI(void);
#endif