177 lines
4.4 KiB
C
Raw Normal View History

2022-03-26 20:51:40 +08:00
/********************************** (C) COPYRIGHT *******************************
* File Name : CH58x_SPI1.c
* Author : WCH
* Version : V1.0
* Date : 2018/12/15
* Description
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH58x_common.h"
/*********************************************************************
* @fn SPI1_MasterDefInit
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽĬ<EFBFBD>ϳ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ0+3<EFBFBD><EFBFBD>ȫ˫<EFBFBD><EFBFBD>+8MHz
*
* @param none
*
* @return none
*/
void SPI1_MasterDefInit(void)
{
R8_SPI1_CLOCK_DIV = 4; // <20><>Ƶʱ<C6B5><CAB1>4<EFBFBD><34>Ƶ
R8_SPI1_CTRL_MOD = RB_SPI_ALL_CLEAR;
R8_SPI1_CTRL_MOD = RB_SPI1_SDO_OE | RB_SPI_SCK_OE;
R8_SPI1_CTRL_CFG |= RB_SPI_AUTO_IF; // <20><><EFBFBD><EFBFBD>BUFFER/FIFO<46>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>IF_BYTE_END<4E><44>־
}
/*********************************************************************
* @fn SPI1_CLKCfg
*
* @brief SPI1 <EFBFBD><EFBFBD>׼ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>= d*Tsys
*
* @param c - ʱ<EFBFBD>ӷ<EFBFBD>Ƶϵ<EFBFBD><EFBFBD>
*
* @return none
*/
void SPI1_CLKCfg(uint8_t c)
{
if(c == 2)
{
R8_SPI1_CTRL_CFG |= RB_SPI_MST_DLY_EN;
}
else
{
R8_SPI1_CTRL_CFG &= ~RB_SPI_MST_DLY_EN;
}
R8_SPI1_CLOCK_DIV = c;
}
/*********************************************************************
* @fn SPI1_DataMode
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
*
* @param m - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ refer to ModeBitOrderTypeDef
*
* @return none
*/
void SPI1_DataMode(ModeBitOrderTypeDef m)
{
switch(m)
{
case Mode0_LowBitINFront:
R8_SPI1_CTRL_MOD &= ~RB_SPI_MST_SCK_MOD;
R8_SPI1_CTRL_CFG |= RB_SPI_BIT_ORDER;
break;
case Mode0_HighBitINFront:
R8_SPI1_CTRL_MOD &= ~RB_SPI_MST_SCK_MOD;
R8_SPI1_CTRL_CFG &= ~RB_SPI_BIT_ORDER;
break;
case Mode3_LowBitINFront:
R8_SPI1_CTRL_MOD |= RB_SPI_MST_SCK_MOD;
R8_SPI1_CTRL_CFG |= RB_SPI_BIT_ORDER;
break;
case Mode3_HighBitINFront:
R8_SPI1_CTRL_MOD |= RB_SPI_MST_SCK_MOD;
R8_SPI1_CTRL_CFG &= ~RB_SPI_BIT_ORDER;
break;
default:
break;
}
}
/*********************************************************************
* @fn SPI1_MasterSendByte
*
* @brief <EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ֽ<EFBFBD> (buffer)
*
* @param d - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
*
* @return none
*/
void SPI1_MasterSendByte(uint8_t d)
{
R8_SPI1_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
R8_SPI1_BUFFER = d;
while(!(R8_SPI1_INT_FLAG & RB_SPI_FREE));
}
/*********************************************************************
* @fn SPI1_MasterRecvByte
*
* @brief <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD>ֽ<EFBFBD> (buffer)
*
* @param none
*
* @return <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
*/
uint8_t SPI1_MasterRecvByte(void)
{
R8_SPI1_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
R8_SPI1_BUFFER = 0xFF; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(!(R8_SPI1_INT_FLAG & RB_SPI_FREE));
return (R8_SPI1_BUFFER);
}
/*********************************************************************
* @fn SPI1_MasterTrans
*
* @brief ʹ<EFBFBD><EFBFBD>FIFO<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD>ֽ<EFBFBD>
*
* @param pbuf - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
* @param len - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4095
*
* @return none
*/
void SPI1_MasterTrans(uint8_t *pbuf, uint16_t len)
{
uint16_t sendlen;
sendlen = len;
R8_SPI1_CTRL_MOD &= ~RB_SPI_FIFO_DIR; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
R16_SPI1_TOTAL_CNT = sendlen; // <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
R8_SPI1_INT_FLAG = RB_SPI_IF_CNT_END;
while(sendlen)
{
if(R8_SPI1_FIFO_COUNT < SPI_FIFO_SIZE)
{
R8_SPI1_FIFO = *pbuf;
pbuf++;
sendlen--;
}
}
while(R8_SPI1_FIFO_COUNT != 0); // <20>ȴ<EFBFBD>FIFO<46>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/*********************************************************************
* @fn SPI1_MasterRecv
*
* @brief ʹ<EFBFBD><EFBFBD>FIFO<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD><EFBFBD>ֽ<EFBFBD>
*
* @param pbuf - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
* @param len - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4095
*
* @return none
*/
void SPI1_MasterRecv(uint8_t *pbuf, uint16_t len)
{
uint16_t readlen;
readlen = len;
R8_SPI1_CTRL_MOD |= RB_SPI_FIFO_DIR; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
R16_SPI1_TOTAL_CNT = len; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ȣ<EFBFBD>FIFO<46><4F><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>Ȳ<EFBFBD>Ϊ0<CEAA><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
R8_SPI1_INT_FLAG = RB_SPI_IF_CNT_END;
while(readlen)
{
if(R8_SPI1_FIFO_COUNT)
{
*pbuf = R8_SPI1_FIFO;
pbuf++;
readlen--;
}
}
}