forked from gary/BCU
2
0
Fork 0
BCU/library/drv_stm32f4xx/drv_spi.h

89 lines
2.7 KiB
C
Raw Permalink Normal View History

2024-11-26 15:52:49 +08:00
#ifndef _DRV_SPI_H_
#define _DRV_SPI_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "kit_macro.h"
typedef enum
{
kSpiDev_1,
kSpiDev_2,
kSpiDev_3,
kSpiDev_End,
}SpiDev;
//在CLOCK_PCLK2_FREQ 和 CLOCK_PCLK1_FREQ 基础上进行分频
typedef enum
{
kSpiFreq_Div2 = 0,
kSpiFreq_Div4,
kSpiFreq_Div8,
kSpiFreq_Div16,
kSpiFreq_Div32,
kSpiFreq_Div64,
kSpiFreq_Div128,
kSpiFreq_Div256,
kSpiFreq_End,
}SpiFreq;
/********************************************************************
CPOL CPHA
MODE0 0 0
MODE1 0 1
MODE2 1 0
MODE3 1 1
CPOL: SPI空闲时的时钟信号电平(1:, 0:)
CPHA: SPI在时钟第几个边沿采样(1:沿, 0:沿)
**********************************************************************/
typedef enum
{
kSpiMode_C0E0,
kSpiMode_C0E1,
kSpiMode_C1E0,
kSpiMode_C1E1,
kSpiMode_End,
}SpiMode;
typedef enum
{
SpiFrame_MSBFirst,
SpiFrame_LSBFirst,
SpiFrame_End,
}SpiFrame;
#define SPI_CFG_CPHA_0 (0u)
#define SPI_CFG_CPHA_1 KIT_BIT_MASK_32(0)
#define SPI_CFG_CPOL_0 (0u)
#define SPI_CFG_CPOL_1 KIT_BIT_MASK_32(1)
#define SPI_CFG_SLAVER (0u)
#define SPI_CFG_MASTER (0x0104u)
#define SPI_CFG_DISABLE (0u)
#define SPI_CFG_ENABLE KIT_BIT_MASK_32(6)
#define SPI_CFG_MSB (0u)
#define SPI_CFG_LSB KIT_BIT_MASK_32(7)
#define SPI_CFG_CS_HARD (0u) //CS由外设控制
#define SPI_CFG_CS_SOFT KIT_BIT_MASK_32(9) //CS由软件控制
#define SPI_CFG_2L_RX_TX (0u) //双线收发
#define SPI_CFG_2L_RX (0x0400u) //双线只收
#define SPI_CFG_1L (0x8000u) //单线制
#define SPI_CFG_FRAME_TI (0x100000u) //单线制
kit_ret_e drv_spi_init(SpiDev dev, SpiFreq freq, SpiMode mode, SpiFrame frame_order, uint16_t sck_idx, uint16_t miso_idx, uint16_t mosi_idx);
kit_ret_e drv_spi_init1(SpiDev dev, SpiFreq freq, uint32_t spi_config, uint16_t sck_idx, uint16_t miso_idx, uint16_t mosi_idx);
kit_ret_e drv_spi_set_dma(SpiDev dev, bool is_rx_en, bool is_tx_en);
kit_ret_e drv_spi_sync_send_receive(SpiDev dev, uint8_t *data);
uint8_t drv_spi_sync_send_rev(SpiDev dev, uint8_t data);
kit_ret_e drv_spi_series_sync_send_receive(SpiDev dev, uint8_t *tx_buf, uint16_t tx_len, uint8_t *rx_buf, uint16_t rx_len);
kit_ret_e drv_spi_dma_sync_send(SpiDev dev, uint8_t *tx_buf, uint16_t tx_len);
kit_ret_e drv_spi_dma_sync_receive(SpiDev dev, uint8_t *rx_buf, uint16_t rx_len);
#ifdef __cplusplus
}
#endif
#endif