rt-thread/bsp/synwit/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_spi.c

263 lines
13 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SWM320_spi.c
* : SWM320单片机的SPI功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
2021-05-06 10:10:29 +08:00
* :
2021-09-13 21:04:42 +08:00
* : V1.1.0 20171025
* :
2021-02-18 13:29:12 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
2021-09-13 21:04:42 +08:00
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
2021-02-18 13:29:12 +08:00
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_spi.h"
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_Init()
* : SPI同步串行接口初始化FIFO触发设定
* : SPI_TypeDef * SPIx SPISPI0SPI1
* SPI_InitStructure * initStruct SPI相关设定值的结构体
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void SPI_Init(SPI_TypeDef * SPIx, SPI_InitStructure * initStruct)
2021-05-06 10:42:41 +08:00
{
switch((uint32_t)SPIx)
2021-05-06 10:42:41 +08:00
{
case ((uint32_t)SPI0):
SYS->CLKEN |= (0x01 << SYS_CLKEN_SPI0_Pos);
break;
case ((uint32_t)SPI1):
SYS->CLKEN |= (0x01 << SYS_CLKEN_SPI0_Pos); //与SPI0使用同一位时钟使能
2021-05-06 10:42:41 +08:00
break;
}
SPI_Close(SPIx); //一些关键寄存器只能在SPI关闭时设置
2021-05-06 10:42:41 +08:00
SPIx->CTRL &= ~(SPI_CTRL_FFS_Msk | SPI_CTRL_CPHA_Msk | SPI_CTRL_CPOL_Msk |
SPI_CTRL_SIZE_Msk | SPI_CTRL_MSTR_Msk | SPI_CTRL_CLKDIV_Msk | SPI_CTRL_SSN_H_Msk);
SPIx->CTRL |= (initStruct->FrameFormat << SPI_CTRL_FFS_Pos) |
(initStruct->SampleEdge << SPI_CTRL_CPHA_Pos) |
(initStruct->IdleLevel << SPI_CTRL_CPOL_Pos) |
((initStruct->WordSize-1) << SPI_CTRL_SIZE_Pos) |
(initStruct->Master << SPI_CTRL_MSTR_Pos) |
(initStruct->clkDiv << SPI_CTRL_CLKDIV_Pos) |
(0 << SPI_CTRL_SSN_H_Pos);
SPIx->IF = (0x01 << SPI_IF_RFOVF_Pos); //清除中断标志
2021-05-06 10:42:41 +08:00
SPIx->IE &= ~(SPI_IE_RFHF_Msk | SPI_IE_TFHF_Msk | SPI_IE_FTC_Msk);
SPIx->IE |= (initStruct->RXHFullIEn << SPI_IE_RFHF_Pos) |
2023-12-12 18:23:12 +08:00
(initStruct->TXEmptyIEn << SPI_IE_TFE_Pos) |
2021-05-06 10:42:41 +08:00
(initStruct->TXCompleteIEn << SPI_IE_FTC_Pos);
switch((uint32_t)SPIx)
2021-05-06 10:42:41 +08:00
{
case ((uint32_t)SPI0):
if(initStruct->RXHFullIEn | initStruct->TXEmptyIEn | initStruct->TXCompleteIEn)
2021-05-06 10:42:41 +08:00
{
NVIC_EnableIRQ(SPI0_IRQn);
}
else
{
NVIC_DisableIRQ(SPI0_IRQn);
}
break;
case ((uint32_t)SPI1):
if(initStruct->RXHFullIEn | initStruct->TXEmptyIEn | initStruct->TXCompleteIEn)
2021-05-06 10:42:41 +08:00
{
NVIC_EnableIRQ(SPI1_IRQn);
}
else
{
NVIC_DisableIRQ(SPI1_IRQn);
}
break;
}
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_Open()
* : SPI打开
* : SPI_TypeDef * SPIx SPISPI0SPI1
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void SPI_Open(SPI_TypeDef * SPIx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
SPIx->CTRL |= (0x01 << SPI_CTRL_EN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_Close()
* : SPI关闭
* : SPI_TypeDef * SPIx SPISPI0SPI1
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void SPI_Close(SPI_TypeDef * SPIx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
SPIx->CTRL &= ~SPI_CTRL_EN_Msk;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_Read()
* :
* : SPI_TypeDef * SPIx SPISPI0SPI1
* : uint32_t
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t SPI_Read(SPI_TypeDef * SPIx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return SPIx->DATA;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_Write()
* :
* : SPI_TypeDef * SPIx SPISPI0SPI1
* uint32_t
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void SPI_Write(SPI_TypeDef * SPIx, uint32_t data)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
SPIx->DATA = data;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_WriteWithWait()
* :
* : SPI_TypeDef * SPIx SPISPI0SPI1SPI1
* uint32_t
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void SPI_WriteWithWait(SPI_TypeDef * SPIx, uint32_t data)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
SPIx->STAT |= (1 << SPI_STAT_WTC_Pos);
SPIx->DATA = data;
while((SPIx->STAT & SPI_STAT_WTC_Msk) == 0);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:10:29 +08:00
* : SPI_ReadWrite()
* :
2021-09-13 21:04:42 +08:00
* : SPI_TypeDef * SPIx SPISPI0SPI1
* uint32_t data
* : uint32_t
2021-05-06 10:10:29 +08:00
* : SPI模块SPI_Write()SPI_Write()SPI_STAT_RFNE状态
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t SPI_ReadWrite(SPI_TypeDef * SPIx, uint32_t data)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
SPIx->DATA = data;
while(!(SPIx->STAT & SPI_STAT_RFNE_Msk));
2021-05-06 10:42:41 +08:00
return SPIx->DATA;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_IsRXEmpty()
* : FIFO是否空SPI_Read()
* : SPI_TypeDef * SPIx SPISPI0SPI1
* : uint32_t 1 FIFO空 0 FIFO非空
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t SPI_IsRXEmpty(SPI_TypeDef * SPIx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (SPIx->STAT & SPI_STAT_RFNE_Msk) ? 0 : 1;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_IsTXFull()
* : FIFO是否满SPI_Write()
* : SPI_TypeDef * SPIx SPISPI0SPI1
* : uint32_t 1 FIFO满 0 FIFO不满
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t SPI_IsTXFull(SPI_TypeDef * SPIx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (SPIx->STAT & SPI_STAT_TFNF_Msk) ? 0 : 1;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SPI_IsTXEmpty()
* : FIFO是否空
* : SPI_TypeDef * SPIx SPISPI0SPI1
* : uint32_t 1 FIFO空 0 FIFO非空
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t SPI_IsTXEmpty(SPI_TypeDef * SPIx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (SPIx->STAT & SPI_STAT_TFE_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2023-12-12 18:23:12 +08:00
* : SPI_INTEn()
* : 使
2021-09-13 21:04:42 +08:00
* : SPI_TypeDef * SPIx SPISPI0SPI1
2023-12-12 18:23:12 +08:00
* uint32_t it interrupt typeSPI_IT_RX_OVFSPI_IT_RX_FULLSPI_IT_RX_HFULLSPI_IT_TX_EMPTYSPI_IT_TX_HFULL
* SPI_IT_TX_DONE
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2023-12-12 18:23:12 +08:00
void SPI_INTEn(SPI_TypeDef * SPIx, uint32_t it)
2021-02-18 13:29:12 +08:00
{
2023-12-12 18:23:12 +08:00
SPIx->IE |= it;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2023-12-12 18:23:12 +08:00
* : SPI_INTDis()
* :
2021-09-13 21:04:42 +08:00
* : SPI_TypeDef * SPIx SPISPI0SPI1
2023-12-12 18:23:12 +08:00
* uint32_t it interrupt typeSPI_IT_RX_OVFSPI_IT_RX_FULLSPI_IT_RX_HFULLSPI_IT_TX_EMPTYSPI_IT_TX_HFULL
* SPI_IT_TX_DONE
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2023-12-12 18:23:12 +08:00
void SPI_INTDis(SPI_TypeDef * SPIx, uint32_t it)
2021-02-18 13:29:12 +08:00
{
2023-12-12 18:23:12 +08:00
SPIx->IE &= ~it;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2023-12-12 18:23:12 +08:00
* : SPI_INTClr()
* :
2021-09-13 21:04:42 +08:00
* : SPI_TypeDef * SPIx SPISPI0SPI1
2023-12-12 18:23:12 +08:00
* uint32_t it interrupt typeSPI_IT_RX_OVFSPI_IT_RX_FULLSPI_IT_RX_HFULLSPI_IT_TX_EMPTYSPI_IT_TX_HFULL
* SPI_IT_TX_DONE
2021-05-06 10:10:29 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2023-12-12 18:23:12 +08:00
void SPI_INTClr(SPI_TypeDef * SPIx, uint32_t it)
2021-02-18 13:29:12 +08:00
{
2023-12-12 18:23:12 +08:00
SPIx->IF = it;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2023-12-12 18:23:12 +08:00
* : SPI_INTStat()
* :
2021-09-13 21:04:42 +08:00
* : SPI_TypeDef * SPIx SPISPI0SPI1
2023-12-12 18:23:12 +08:00
* uint32_t it interrupt typeSPI_IT_RX_OVFSPI_IT_RX_FULLSPI_IT_RX_HFULLSPI_IT_TX_EMPTYSPI_IT_TX_HFULL
* SPI_IT_TX_DONE
* : uint32_t 1 0
2021-05-06 10:10:29 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2023-12-12 18:23:12 +08:00
uint32_t SPI_INTStat(SPI_TypeDef * SPIx, uint32_t it)
2021-02-18 13:29:12 +08:00
{
2023-12-12 18:23:12 +08:00
return (SPIx->IF & it) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}