ch55x/src/include/spi.c
2020-11-20 01:14:53 +03:00

132 lines
5.8 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************** (C) COPYRIGHT *******************************
* File Name : SPI.C
* Author : WCH
* Version : V1.0
* Date : 2017/07/05
* Description : CH554 SPIÖ÷¡¢´Óģʽ½Ó¿Úº¯Êý
×¢£ºÆ¬Ñ¡ÓÐЧʱ£¬´Ó»ú»á×Ô¶¯¼ÓÔØSPI0_S_PREµÄÔ¤ÖÃÖµµ½·¢ËÍÒÆÎ»»º³åÇø£¬ËùÒÔ×îºÃ¿ÉÒÔÔÚÆ¬Ñ¡
ÓÐЧǰÏòSPI0_S_PRE¼Ä´æÆ÷дÈëÔ¤·¢Öµ£¬»òÕßÔÚÖ÷»ú¶Ë¶ªÆúÊ׸ö½ÓÊÕ×Ö½Ú£¬·¢ËÍʱעÒâÖ÷»ú»áÏÈ
È¡×ßSPI0_S_PREÀïÃæµÄÖµ²úÉúÒ»¸öS0_IF_BYTEÖжϡ£
Èç¹ûƬѡ´ÓÎÞЧµ½ÓÐЧ£¬´Ó»úÊ×ÏȽøÐз¢Ë͵ϰ£¬×îºÃ°ÑÊä³öµÄÊ××ֽڷŵ½SPI0_S_PRE¼Ä´æÆ÷ÖУ»
Èç¹ûÒѾ­´¦ÓÚÆ¬Ñ¡ÓÐЧµÄ»°£¬Êý¾ÝÊý¾ÝʹÓÃSPI0_DATA¾Í¿ÉÒÔ
*******************************************************************************/
#include <ch554.h>
#include "spi.h"
/*******************************************************************************
* Function Name : SPIMasterModeSet( uint8_t mode )
* Description : SPIÖ÷»úģʽ³õʼ»¯
* Input : uint8_t mode
* Output : None
* Return : None
*******************************************************************************/
void SPIMasterModeSet(uint8_t mode)
{
SPI0_SETUP = 0; //Masterģʽ,¸ßλÔÚǰ
if(mode == 0){
SPI0_CTRL = 0x60; //ģʽ0
}
else if(mode == 3){
SPI0_CTRL = 0x68; //ģʽ3
}
P1_MOD_OC &= 0x0F;
P1_DIR_PU |= 0xB0; //SCS,MOSI,SCKÉèÍÆÍìÊä³ö
P1_DIR_PU &= 0xBF; //MISOÉ踡¿ÕÊäÈë
// Set clock speed
SPI0_CK_SE = 0x02;
}
/*******************************************************************************
* Function Name : CH554SPIInterruptInit()
* Description : CH554SPIÖжϳõʼ»¯
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CH554SPIInterruptInit()
{
//IP_EX |= bIP_SPI0; //SPI0ÖжÏÓÅÏȼ¶ÉèÖÃ
SPI0_SETUP |= bS0_IE_FIFO_OV | bS0_IE_BYTE; //ʹÄܽÓÊÕ1×Ö½ÚÖжϣ¬Ê¹ÄÜFIFOÒç³öÖжÏ
SPI0_CTRL |= bS0_AUTO_IF; //×Ô¶¯ÇåS0_IF_BYTEÖжϱêÖ¾
SPI0_STAT |= 0xff; //Çå¿ÕSPI0ÖжϱêÖ¾
#ifdef SPI_Interrupt
IE_SPI0 = 1; //ʹÄÜSPI0ÖжÏ
#endif
}
/*******************************************************************************
* Function Name : CH554SPIMasterWrite(uint8_t dat)
* Description : CH554Ó²¼þSPIдÊý¾Ý,Ö÷»úģʽ
* Input : uint8_t dat Êý¾Ý
* Output : None
* Return : None
*******************************************************************************/
void CH554SPIMasterWrite(uint8_t dat)
{
SPI0_DATA = dat;
while(S0_FREE == 0); //µÈ´ý´«ÊäÍê³É
//Èç¹ûbS0_DATA_DIRΪ1£¬´Ë´¦¿ÉÒÔÖ±½Ó¶Áȡһ¸ö×Ö½ÚµÄÊý¾ÝÓÃÓÚ¿ìËÙ¶Áд
}
/*******************************************************************************
* Function Name : CH554SPIMasterRead( )
* Description : CH554Ó²¼þSPI0¶ÁÊý¾Ý£¬Ö÷»úģʽ
* Input : None
* Output : None
* Return : uint8_t ret
*******************************************************************************/
uint8_t CH554SPIMasterRead()
{
SPI0_DATA = 0xff;
while(S0_FREE == 0);
return SPI0_DATA;
}
/*******************************************************************************
* Function Name : SPISlvModeSet( )
* Description : SPI´Ó»úģʽ³õʼ»¯
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPISlvModeSet( )
{
SPI0_SETUP = 0x80; //Slvģʽ,¸ßλÔÚǰ
SPI0_CTRL = 0x81; //¶ÁдFIFO,×Ô¶¯ÇåS0_IF_BYTE±êÖ¾
P1_MOD_OC &= 0x0F;
P1_DIR_PU &= 0x0F; //SCS,MOSI,SCK,MISOÈ«ÉèÖø¡¿ÕÊäÈë
}
/*******************************************************************************
* Function Name : CH554SPISlvWrite(uint8_t dat)
* Description : CH554Ó²¼þSPIдÊý¾Ý£¬´Ó»úģʽ
* Input : uint8_t dat Êý¾Ý
* Output : None
* Return : None
*******************************************************************************/
void CH554SPISlvWrite(uint8_t dat)
{
SPI0_DATA = dat;
while(S0_IF_BYTE==0);
S0_IF_BYTE = 0;
}
/*******************************************************************************
* Function Name : CH554SPISlvRead( )
* Description : CH554Ó²¼þSPI0¶ÁÊý¾Ý£¬´Ó»úģʽ
* Input : None
* Output : None
* Return : uint8_t ret
*******************************************************************************/
uint8_t CH554SPISlvRead()
{
while(S0_IF_BYTE==0);
S0_IF_BYTE = 0;
return SPI0_DATA;
}