📄 drvspi.c
字号:
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
/* Include related headers */
/*---------------------------------------------------------------------------------------------------------*/
#include "NUC1xx.h"
#include "core_cm0.h"
#include "DrvSPI.h"
#include "DrvSYS.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Global file scope (static) variables */
/*---------------------------------------------------------------------------------------------------------*/
typedef struct
{
uint8_t bInUse;
PFN_DRVSPI_CALLBACK pfnOneTransDoneCallBack; /* Function pointer of the one transaction done interrupt */
uint32_t u32OneTransDoneUserData;
PFN_DRVSPI_CALLBACK pfn3WireStartCallBack; /* Function pointer of the 3-wire SPI start interrupt */
uint32_t u32ThreeWireStartUserData;
} S_DRVSPI_HANDLE;
static S_DRVSPI_HANDLE g_sSpiHandler[4];
static SPI_T * SPI_PORT[4]={SPI0, SPI1, SPI2, SPI3};
/*---------------------------------------------------------------------------------------------------------*/
/* Interrupt Handler */
/*---------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
/* Function: SPI0_IRQHandler */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* SPI0 interrupt handler. Clear the SPI interrupt flag and execute the callback function. */
/*---------------------------------------------------------------------------------------------------------*/
void SPI0_IRQHandler(void)
{
if( SPI0->CNTRL.IF == 1 ) /* One transaction done interrupt */
{
SPI0->CNTRL.IF = 1; /* write '1' to clear SPI0 interrupt flag */
if(g_sSpiHandler[0].pfnOneTransDoneCallBack != NULL)
{
g_sSpiHandler[0].pfnOneTransDoneCallBack(g_sSpiHandler[0].u32OneTransDoneUserData);
}
}
if( SPI0->CNTRL2.SLV_START_INTSTS == 1 ) /* 3-wire SPI start interrupt */
{
SPI0->CNTRL2.SLV_START_INTSTS = 1; /* write '1' to clear SPI0 3-wire start interrupt flag */
if(g_sSpiHandler[0].pfn3WireStartCallBack != NULL)
{
g_sSpiHandler[0].pfn3WireStartCallBack(g_sSpiHandler[0].u32ThreeWireStartUserData);
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: SPI1_IRQHandler */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* SPI1 interrupt handler. Clear the SPI interrupt flag and execute the callback function. */
/*---------------------------------------------------------------------------------------------------------*/
void SPI1_IRQHandler(void)
{
if( SPI1->CNTRL.IF == 1 ) /* One transaction done interrupt */
{
SPI1->CNTRL.IF = 1; /* write '1' to clear SPI1 interrupt flag */
if(g_sSpiHandler[1].pfnOneTransDoneCallBack != NULL)
{
g_sSpiHandler[1].pfnOneTransDoneCallBack(g_sSpiHandler[1].u32OneTransDoneUserData);
}
}
if( SPI1->CNTRL2.SLV_START_INTSTS == 1 ) /* 3-wire SPI start interrupt */
{
SPI1->CNTRL2.SLV_START_INTSTS = 1; /* write '1' to clear SPI1 3-wire start interrupt flag */
if(g_sSpiHandler[1].pfn3WireStartCallBack != NULL)
{
g_sSpiHandler[1].pfn3WireStartCallBack(g_sSpiHandler[1].u32ThreeWireStartUserData);
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: SPI2_IRQHandler */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* SPI2 interrupt handler. Clear the SPI interrupt flag and execute the callback function. */
/*---------------------------------------------------------------------------------------------------------*/
void SPI2_IRQHandler(void)
{
if( SPI2->CNTRL.IF == 1 ) /* One transaction done interrupt */
{
SPI2->CNTRL.IF = 1; /* write '1' to clear SPI2 interrupt flag */
if(g_sSpiHandler[2].pfnOneTransDoneCallBack != NULL)
{
g_sSpiHandler[2].pfnOneTransDoneCallBack(g_sSpiHandler[2].u32OneTransDoneUserData);
}
}
if( SPI2->CNTRL2.SLV_START_INTSTS == 1 ) /* 3-wire SPI start interrupt */
{
SPI2->CNTRL2.SLV_START_INTSTS = 1; /* write '1' to clear SPI2 3-wire start interrupt flag */
if(g_sSpiHandler[2].pfn3WireStartCallBack != NULL)
{
g_sSpiHandler[2].pfn3WireStartCallBack(g_sSpiHandler[2].u32ThreeWireStartUserData);
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: SPI3_IRQHandler */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* SPI3 interrupt handler. Clear the SPI interrupt flag and execute the callback function. */
/*---------------------------------------------------------------------------------------------------------*/
void SPI3_IRQHandler(void)
{
if( SPI3->CNTRL.IF == 1 ) /* One transaction done interrupt */
{
SPI3->CNTRL.IF = 1; /* write '1' to clear SPI3 interrupt flag */
if(g_sSpiHandler[3].pfnOneTransDoneCallBack != NULL)
{
g_sSpiHandler[3].pfnOneTransDoneCallBack(g_sSpiHandler[3].u32OneTransDoneUserData);
}
}
if( SPI3->CNTRL2.SLV_START_INTSTS == 1 ) /* 3-wire SPI start interrupt */
{
SPI3->CNTRL2.SLV_START_INTSTS = 1; /* write '1' to clear SPI3 3-wire start interrupt flag */
if(g_sSpiHandler[3].pfn3WireStartCallBack != NULL)
{
g_sSpiHandler[3].pfn3WireStartCallBack(g_sSpiHandler[3].u32ThreeWireStartUserData);
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_Open */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* eMode [in]: Specify the operation mode (eDRVSPI_MASTER/eDRVSPI_SLAVE) */
/* eType [in]: Specify the transfer type (eDRVSPI_TYPE0 ~ eDRVSPI_TYPE7) */
/* i32BitLength [in]: Specify the bit length in a transaction (1~32) */
/* */
/* Returns: */
/* E_DRVSPI_ERR_INIT: The specified SPI port has been opened before. */
/* E_DRVSPI_ERR_BUSY: The specified SPI port is in busy status. */
/* E_DRVSPI_ERR_BIT_LENGTH: The specified bit length is out of range. */
/* E_SUCCESS: Success. */
/* */
/* Description: */
/* Configure the operation mode, transfer type and bit length of a transaction of the specified SPI */
/* port. */
/* The timing waveform types: */
/*
DRVSPI_TYPE0:
CS --| Active state |---
_ _ _ _ _ _ _ _
CLK ____| |_| |_| |_| |_| |_| |_| |_| |_____
Tx ----| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |---
Rx --| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |-----
DRVSPI_TYPE1:
CS --| Active state |---
_ _ _ _ _ _ _ _
CLK ____| |_| |_| |_| |_| |_| |_| |_| |_____
Tx --| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |-----
Rx --| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |-----
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -