HC32L17x的LL驱动库之spi

该代码段定义了HC32L1XX微控制器的低功耗SPI(Low-LevelSPI)的库函数接口,包括SPI的启用、禁用、模式设置、时钟相位和极性设置、NSS模式配置等操作。还提供了中断和DMA请求的控制以及数据传输的函数。LL_SPI_InitTypeDef结构体用于初始化SPI接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef HC32L1XX_LL_SPI_H_
#define HC32L1XX_LL_SPI_H_

#ifdef __cplusplus
extern "C" {
#endif 
    
    #include "hc32l1xx.h"
    #include "hc32l1xx_ll_bus.h"
    #include "hc32l1xx_ll_rcc.h"
    #include "hc32l1xx_ll_gpio_ex.h"    
    ///

    #define LL_SPI_PHASE_1EDGE                    SPI_CR_CPHA_1EDGE    
    #define LL_SPI_PHASE_2EDGE                    SPI_CR_CPHA_2EDGE    
    
    #define LL_SPI_POLARITY_LOW                    SPI_CR_CPOL_LOW      
    #define LL_SPI_POLARITY_HIGH                SPI_CR_CPOL_HIGH  

    #define LL_SPI_MODE_MASTER                    SPI_CR_MODE_MASTER   
    #define LL_SPI_MODE_SLAVE                    SPI_CR_MODE_SLAVE 

    #define LL_SPI_BAUDRATEPRESCALER_DIV2        SPI_CR_SCK_PRE_2     
    #define LL_SPI_BAUDRATEPRESCALER_DIV4        SPI_CR_SCK_PRE_4     
    #define LL_SPI_BAUDRATEPRESCALER_DIV8        SPI_CR_SCK_PRE_8     
    #define LL_SPI_BAUDRATEPRESCALER_DIV16        SPI_CR_SCK_PRE_16    
    #define LL_SPI_BAUDRATEPRESCALER_DIV32        SPI_CR_SCK_PRE_32    
    #define LL_SPI_BAUDRATEPRESCALER_DIV64        SPI_CR_SCK_PRE_64    
    #define LL_SPI_BAUDRATEPRESCALER_DIV128        SPI_CR_SCK_PRE_128   
    

    #define LL_SPI_NSS_SOFT                        0ul
    #define LL_SPI_NSS_HARD_INPUT                1ul
    #define LL_SPI_NSS_HARD_OUTPUT                2ul

    #define LL_SPI_NSS_HIGH                        SPI_SSN_NSS
    #define LL_SPI_NSS_LOW                        0ul
    
    #define LL_SPI_NSS_NONE                        0ul
    #define LL_SPI_NSS_PA3                        1ul
    #define LL_SPI_NSS_PA4                        2ul
    #define LL_SPI_NSS_PA6                        3ul
    #define LL_SPI_NSS_PA8                        4ul
    #define LL_SPI_NSS_PA9                        5ul
    #define LL_SPI_NSS_PA12                        6ul
    #define LL_SPI_NSS_PA15                        7ul

    #define LL_SPI_NSS_PB1                        8ul
    #define LL_SPI_NSS_PB2                        9ul
    #define LL_SPI_NSS_PB5                        10ul
    #define LL_SPI_NSS_PB6                        11ul
    #define LL_SPI_NSS_PB9                        12ul
    #define LL_SPI_NSS_PB10                        13ul
    #define LL_SPI_NSS_PB12                        14ul
    #define LL_SPI_NSS_PB14                        15ul

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_Enable(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->CR, SPI_CR_EN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_Disable(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CR, SPI_CR_EN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsEnabled(SPI_TypeDef* SPIx)
    {
        return (uint32_t)(READ_BIT(SPIx->CR, SPI_CR_EN)== SPI_CR_EN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_SetMode(SPI_TypeDef* SPIx, uint32_t Mode)
    {
        MODIFY_REG(SPIx->CR, SPI_CR_MODE, Mode);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_GetMode(SPI_TypeDef* SPIx)
    {
        return (uint32_t)(READ_BIT(SPIx->CR, SPI_CR_MODE));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_SetClockPhase(SPI_TypeDef* SPIx, uint32_t ClockPhase)
    {
        MODIFY_REG(SPIx->CR, SPI_CR_CPHA, ClockPhase);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_GetClockPhase(SPI_TypeDef* SPIx)
    {
        return (uint32_t)(READ_BIT(SPIx->CR, SPI_CR_CPHA));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_SetClockPolarity(SPI_TypeDef* SPIx, uint32_t ClockPolarity)
    {
        MODIFY_REG(SPIx->CR, SPI_CR_CPOL, ClockPolarity);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_GetClockPolarity(SPI_TypeDef* SPIx)
    {
        return (uint32_t)(READ_BIT(SPIx->CR, SPI_CR_CPOL));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_SetBaudRatePrescaler(SPI_TypeDef* SPIx, uint32_t BaudRatePre)
    {
        MODIFY_REG(SPIx->CR, SPI_CR_BAUDRATE_SPR, BaudRatePre);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_GetBaudRatePrescaler(SPI_TypeDef* SPIx)
    {
        return (uint32_t)(READ_BIT(SPIx->CR, SPI_CR_BAUDRATE_SPR));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_SetNSSMode(SPI_TypeDef* SPIx, uint32_t NSS)
    {
        if (SPIx==SPI0)
        {
            MODIFY_REG(GPIO_SUB->CTRL1, GPIO_AF_CTRL1_SPI_NSS,
                (NSS << GPIO_AF_CTRL1_SPI_NSS_POS));
        }
#ifdef SPI1
        else if (SPIx==SPI1)
        {
            MODIFY_REG(GPIO_SUB->CTRL2, GPIO_AF_CTRL2_SPI_NSS,
                (NSS << GPIO_AF_CTRL2_SPI_NSS));
        }
#endif
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_GetNSSMode(SPI_TypeDef* SPIx)
    {
        if (SPIx == SPI0)
        {
            return (READ_BIT(GPIO_SUB->CTRL1, GPIO_AF_CTRL1_SPI_NSS) >> GPIO_AF_CTRL1_SPI_NSS_POS);
        }
#ifdef SPI1
        else if (SPIx == SPI1)
        {
            return (READ_BIT(GPIO_SUB->CTRL2, GPIO_AF_CTRL2_SPI_NSS));
        }
#endif
        return LL_SPI_NSS_NONE;
    }
    
    ///
    //函        数: 
    //功        能: 主机模式下NSS端口输出高电平
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_NSS_High(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->SSN, SPI_SSN_NSS);
    }

    ///
    //函        数: 
    //功        能: 主机模式下NSS端口输出低电平
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_NSS_Low(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->SSN, SPI_SSN_NSS);
    }

    ///
    //函        数: 
    //功        能: 主机模式下NSS端口输出电平
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_NSS(SPI_TypeDef* SPIx,uint32_t level)
    {
        MODIFY_REG(SPIx->SSN, SPI_SSN_NSS,level);
    }

    ///
    //函        数: 
    //功        能: 接收非空标志
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsActiveFlag_RXNE(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->SR, SPI_SR_RXNE) == (SPI_SR_RXNE)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 发送空标志
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsActiveFlag_TXE(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->SR, SPI_SR_TXE) == (SPI_SR_TXE)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 总线忙状态
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsActiveFlag_BSY(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->SR, SPI_SR_BSY) == (SPI_SR_BSY)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 从机NSS错误标志
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsActiveFlag_NSS_ERR(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->SR, SPI_SR_SSERR) == (SPI_SR_SSERR)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 总线冲突标志
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsActiveFlag_MDF(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->SR, SPI_SR_MDF) == (SPI_SR_MDF)) ? 1UL : 0UL);
    }


    ///
    //函        数: 
    //功        能:接收完成标志 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsActiveFlag_SPIF(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->SR, SPI_SR_SPIF) == (SPI_SR_SPIF)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_ClearFlag(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CSR, SPI_CSR_IE);
    }
    
    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_EnableIT(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->CR2, SPI_CR2_IE);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_DisableIT(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CR2, SPI_CR2_IE);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsEnabled_IT(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->CR2, SPI_CR2_IE) == (SPI_CR2_IE)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_EnableIT_RXNE(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->CR2, SPI_CR2_RXNEIE);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_DisableIT_RXNE(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CR2, SPI_CR2_RXNEIE);
    }
    
    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsEnabled_IT_RXNE(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->CR2, SPI_CR2_RXNEIE) == (SPI_CR2_RXNEIE)) ? 1UL : 0UL);
    }
    
    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_EnableIT_TXE(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->CR2, SPI_CR2_TXEIE);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_DisableIT_TXE(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CR2, SPI_CR2_TXEIE);
    }
    
    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsEnabled_IT_TXE(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->CR2, SPI_CR2_TXEIE) == (SPI_CR2_TXEIE)) ? 1UL : 0UL);
    }
    
    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_EnableDMAReq_RX(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->CR2, SPI_CR2_DMARXEN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_DisableDMAReq_RX(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CR2, SPI_CR2_DMARXEN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsEnabled_DMAReq_RX(SPI_TypeDef* SPIx)
    {
        return ((READ_BIT(SPIx->CR2, SPI_CR2_DMARXEN) == (SPI_CR2_DMARXEN)) ? 1UL : 0UL);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_EnableDMAReq_TX(SPI_TypeDef* SPIx)
    {
        SET_BIT(SPIx->CR2, SPI_CR2_DMATXEN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_DisableDMAReq_TX(SPI_TypeDef* SPIx)
    {
        CLEAR_BIT(SPIx->CR2, SPI_CR2_DMATXEN);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_IsEnabled_DMAReq_TX(SPI_TypeDef* SPIx)
    {
        return (uint32_t)(READ_BIT(SPIx->CR2, SPI_CR2_DMATXEN) == (SPI_CR2_DMATXEN));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint32_t LL_SPI_DMAGetRegAddr(SPI_TypeDef* SPIx)
    {
        return (uint32_t) & (SPIx->DR);
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline  uint8_t LL_SPI_ReceiveData8(SPI_TypeDef* SPIx)
    {
        return (*((__IO uint8_t*) & (SPIx->DR)));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline uint16_t LL_SPI_ReceiveData16(SPI_TypeDef* SPIx)
    {
        return (uint16_t)(READ_REG(SPIx->DR));
    }

    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_TransmitData8(SPI_TypeDef* SPIx, uint8_t TxData)
    {
        __IO uint8_t* spidr = ((__IO uint8_t*) & SPIx->DR);
        *spidr = TxData;
    }
    
    ///
    //函        数: 
    //功        能: 
    //输入参    数: 
    //输出参    数: 
    //说        明: 
    //
    static inline void LL_SPI_TransmitData16(SPI_TypeDef* SPIx, uint16_t TxData)
    {
        __IO uint16_t* spidr = ((__IO uint16_t*) & SPIx->DR);
        *spidr = TxData;
    }

    //===结构体定义
    typedef struct
    {
      uint32_t Mode;                          
      uint32_t ClockPolarity;         
      uint32_t ClockPhase;              
      uint32_t NSS;                    
      uint32_t NSSPin;
      uint32_t BaudRate;               
    } LL_SPI_InitTypeDef;

    //===函数定义
    uint8_t LL_SPI_DeInit(SPI_TypeDef* SPIx);
    void LL_SPI_StructInit(LL_SPI_InitTypeDef* SPI_InitStruct);
    uint8_t LL_SPI_Init(SPI_TypeDef* SPIx, LL_SPI_InitTypeDef* SPI_InitStruct);

    ///
#ifdef __cplusplus
}
#endif 

#endif /* HC32L1XX_LL_SPI_H */
#include "hc32l1xx_ll_spi.h"


///
//函        数:
//功        能:
//输入参数:
//输出参数:
//说        明:
//

uint8_t LL_SPI_DeInit(SPI_TypeDef* SPIx)
{
#ifdef SPI0
    if (SPIx == SPI0)
    {
        LL_PER0_GRP1_ForceReset(LL_PER0_GRP1_PERIPH_SPI0);
        LL_PER0_GRP1_ReleaseReset(LL_PER0_GRP1_PERIPH_SPI0);
        return 0;
    }
#endif

#ifdef SPI1
    if (SPIx == SPI1)
    {
        LL_PER0_GRP1_ForceReset(LL_PER0_GRP1_PERIPH_SPI1);
        LL_PER0_GRP1_ReleaseReset(LL_PER0_GRP1_PERIPH_SPI1);
        return 0;
    }
#endif
    return 0;
}

///
//函        数: 
//功        能: 
//输入参    数: 
//输出参    数: 
//说        明: 
//
void LL_SPI_StructInit(LL_SPI_InitTypeDef* SPI_InitStruct)
{
    SPI_InitStruct->Mode = LL_SPI_MODE_MASTER;
    SPI_InitStruct->ClockPolarity = LL_SPI_POLARITY_HIGH;
    SPI_InitStruct->ClockPhase = LL_SPI_PHASE_1EDGE;
    SPI_InitStruct->NSS = LL_SPI_NSS_HIGH;
    SPI_InitStruct->BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV128;
}

///
//函        数:
//功        能:
//输入参数:
//输出参数:
//说        明:
//

uint8_t LL_SPI_Init(SPI_TypeDef* SPIx, LL_SPI_InitTypeDef* SPI_InitStruct)
{
    ---设置模式  CR 
    //LL_SPI_SetMode(SPIx, SPI_InitStruct->Mode);
    ---设置时钟极性  CR 
    //LL_SPI_SetClockPolarity(SPIx, SPI_InitStruct->ClockPolarity);
    ---设置时钟相位  CR 
    //LL_SPI_SetClockPhase(SPIx, SPI_InitStruct->ClockPhase);
    ---设置时钟分频 CR 
    //LL_SPI_SetBaudRatePrescaler(SPIx, SPI_InitStruct->BaudRate);

    WRITE_REG(SPIx->CR, 
        SPI_InitStruct->Mode|
        SPI_InitStruct->ClockPolarity|
        SPI_InitStruct->ClockPhase|
        SPI_InitStruct->BaudRate
    );
    //---设置NSS模式
    if (SPI_InitStruct->Mode==LL_SPI_MODE_MASTER)
    {
        //---主机模式下NSS端口设置输出高电平
        LL_SPI_NSS_High(SPIx);
    }
    //---判断SPI模式
    if (SPI_InitStruct->NSS==LL_SPI_NSS_SOFT)
    {
        SPI_InitStruct->NSSPin = LL_SPI_NSS_NONE;
    }
#ifdef SPI0
    if (SPIx == SPI0)
    {
        LL_GPIO_EX_SetSPI_NSS0(SPI_InitStruct->NSSPin);
        goto GoExit;
    }
#endif
#ifdef SPI1
    if (SPIx == SPI1)
    {
        LL_GPIO_EX_SetSPI_NSS1(SPI_InitStruct->NSSPin);
        goto GoExit;
    }
#endif
#ifdef SPI2
    if (SPIx == SPI2)
    {
        LL_GPIO_EX_SetSPI_NSS2(SPI_InitStruct->NSSPin);
        goto GoExit;
    }
#endif
GoExit:
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值