#include "SPI.h"
#define DEBUGMSG printf
STATIC UINT32 g_unSpi0Rx0Data = 0, g_unSpi1Rx0Data = 0;
/****************************************
*Function name: Spi0MasterInit
*Input: None
*Output: None
*Function: Initialize SPI0 to master mode
**********************************************/
VOID Spi0MasterInit(VOID)
{
P1_MFP &= ~(P14_AIN4_SPI0SS | P15_AIN5_SPI0MOSI | P16_AIN6_SPI0MISO | P17_AIN7_SPI0CLK);
P1_MFP |= (SPI0SS | SPI0MOSI | SPI0MISO | SPI0CLK); //Enable SPI0 related pins
ENABLE_SPI0_CLK; //SPI0 clock enable
SPI0_SSR &= ~LVL_H; //The slave select signal is activated by a low level
SPI0_CNTRL &= ~LSB_FIRST; //The most significant bit is sent/received first
SPI0_CNTRL &= ~CLKP_IDLE_H; //SCLK is low when idle
SPI0_CNTRL |= TX_NEG_F; //SDO signal changes on the falling edge of SPICLK
SPI0_CNTRL &= ~RX_NEG_F; //SDI signal is latched on the rising edge of SPICLK
CLKDIV &= 0xFFFFFFF0; //HCLK_N = 0, Pclk = SYSclk/(HCLK_N+1)
SPI0_DIVIDER &= 0xFFFF0000; //SPIclk = Pclk/((HCLK_N+1)*2)
SPI0_DIVIDER |= 0x00000002;
SET_SPI0_MASTER_MODE; //SPI0 works in master mode
ENABLE_SPI0_AUTO_SLAVE_SLECT; //Enable automatic slave selection
SPI0_SSR |= SSR_ACT;
}
/****************************************
*Function name: Spi0Send1W
*Input: ulData sent data
ucLength data length
*Output: None
*Function: SPI0 sends data
**********************************************/
VOID Spi0Send1W(UINT32 ulData, UINT8 ucLength)
{
SPI0_CNTRL &= TX_NUM_ONE;
Spi0Length(ucLength);
SPI0_TX0 = ulData;
SPI0_CNTRL |= GO_BUSY;
}
/****************************************
*Function name: GetSlaveID
*Input: None
*Output: None
*Function: Get slave ID
******************************************/
VOID GetSlaveID(VOID)
{
Spi0Send1W(0x00000055, 0x08);
while((SPI0_CNTRL & GO_BUSY)!=0);
Spi0Send1W(0x00000000, 0x08);
while((SPI0_CNTRL & GO_BUSY)!=0);
g_unSpi0Rx0Data = SPI0_RX0;
}
/****************************************
*Function name: Spi0Length
*Input: ucLength data length
*Output: None
*Function: Set SPI0 data length
******************************************/
VOID Spi0Length(UINT8 ucLength)
{
if(ucLength <= 0x20)
{
if((ucLength & 0x01) == 0)
SPI0_CNTRL &= ~(1<<3);
else
SPI0_CNTRL |= (1<<3);
if((ucLength & 0x02) == 0)
SPI0_CNTRL &= ~(1<<4);
else
SPI0_CNTRL |= (1<<4);
if((ucLength & 0x04) == 0)
SPI0_CNTRL &= ~(1<<5);
else
SPI0_CNTRL |= (1<<5);
if((ucLength & 0x08) == 0)
SPI0_CNTRL &= ~(1<<6);
else
SPI0_CNTRL |= (1<<6);
if((ucLength & 0x10) == 0)
SPI0_CNTRL &= ~(1<<7);
else
SPI0_CNTRL |= (1<<7);
}
}
/****************************************
*Function name: Spi1SlaveInit
*Input: None
*Output: None
*Function: Initialize SPI1 as slave
**********************************************/
VOID Spi1SlaveInit(VOID)
{
P0_MFP &= ~(P04_AD4_SPI1SS | P05_AD5_SPI1MOSI | P06_AD6_SPI1MISO | P07_AD7_SPI1CLK);
P0_MFP |= (SPI1SS | SPI1MOSI | SPI1MISO | SPI1CLK); //Enable SPI1 related pins
ENABLE_SPI1_CLK;
SPI1_SSR &= LTRIG_EDG; //Slave input edge trigger
SPI1_SSR &= ~LVL_H; //Slave select signal is activated by low level
SPI1_CNTRL &= ~LSB_FIRST; //First send/receive the most significant bit
SPI1_CNTRL &= ~CLKP_IDLE_H; //SCLK is low when idle
SPI1_CNTRL |= TX_NEG_F; //SDO signal changes on the falling edge of SPICLK
SPI1_CNTRL &= ~RX_NEG_F; //SDI signal is latched on the rising edge of SPICLK
CLKDIV &= 0xFFFFFFF0; //HCLK_N = 0, Pclk = SYSclk/(HCLK_N+1)
SPI1_DIVIDER &= 0xFFFF0000; //SPIclk = Pclk/((HCLK_N+1)*2)
SPI1_DIVIDER |= 0x00000002;
SET_SPI1_SLAVE_MODE; //SPI1 works in slave mode
ENABLE_SPI1_INTERRUPT; //Enable SPI1 interrupt
NVIC_ISER |= SPI1_INT;
Spi1Length(8); //Set SPI1 data length
SPI1_GO_BUSY; //Wait for SPI1 RX0 to receive signal
}
/********************************************
*Function name: Spi1Length
*Input: ucLength data length
*Output: None
*Function: Set SPI1 data length
********************************************/
VOID Spi1Length(UINT8 ucLength)
{
if(ucLength <= 0x20)
{
if((ucLength & 0x01) == 0)
SPI1_CNTRL &= ~(1<<3);
else
SPI1_CNTRL |= (1<<3);
if((ucLength & 0x02) == 0)
SPI1_CNTRL &= ~(1<<4);
else
SPI 1_CNTRL |= (1<<4);
if((ucLength & 0x04) == 0)
SPI1_CNTRL &= ~(1<<5);
else
SPI1_CNTRL |= (1<<5);
if((ucLength & 0x08) == 0)
SPI1_CNTRL &= ~(1<<6);
else
SPI1_CNTRL |= (1<<6);
if((ucLength & 0x10) == 0)
SPI1_CNTRL &= ~(1<<7);
else
SPI1_CNTRL |= (1<<7);
}
}
/****************************************
*Function name: PrintGetData
*Input: None
*Output: None
*Function: Print the data received by SPI0/1
******************************************/
VOID PrintGetData(VOID)
{
DEBUGMSG("Slave Get Command = %X ", g_unSpi1Rx0Data);
DEBUGMSG("Master Get Feed Back Data = %X ", g_unSpi0Rx0Data);
}
/*********************************************
*Function name: SPI1_IRQHandler
*Input: None
*Output: None
*Function: SPI1 interrupt service function
************************************************/
VOID SPI1_IRQHandler(VOID)
{
SPI1_CNTRL |= SPI_IF; //Clear interrupt flag
if(SPI1_RX0 == 0x55)
{
g_unSpi1Rx0Data = SPI1_RX0;
SPI1_TX0 = 0x0000000DA;
}
else
SPI1_TX0 = 0x00000000;
SPI1_GO_BUSY; //Wait for SPI1 RX0 to receive signal
}
/****************************************
*Function name: main
*Input: None
*Output: None
*Function: Function body
******************************************/
INT32 main(VOID)
{
PROTECT_REG //Protect FLASH memory during ISP download
(
PWRCON |= XTL12M_EN; //Default clock source is external crystal
while((CLKSTATUS & XTL12M_STB) == 0); //Wait for 12MHz clock to stabilizeCLKSEL0
= (CLKSEL0 & (~HCLK)) | HCLK_12M; //Set external crystal as system clock
)
UartInit(12000000,9600); //Set baud rate to 9600bps
Spi0MasterInit(); //SPI0 initialized to master modeSpi1SlaveInit
(); //SPI1 initialized to slave modewhile
(1)
{
DEBUGMSG("Master will send 0x55 data to slave and receive 0xDA data ");
DEBUGMSG("Put AnyKey to Start Test ");
GetSlaveID();
PrintGetData();
printf(" ");
Delayms(1000);
}
}
Previous article:Nuvoton M051 ADC analog-to-digital conversion program
Next article:Nuvoton M051 WDT watchdog program
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Playing with Zynq Serial 46——[ex65] Image Laplace Edge Extraction of MT9V034 Camera
- Reading and writing 8-bit data in C2000 series
- [ESP32-Audio-Kit Audio Development Board Review] First Look at the Anxinke Audio Development Board
- How to test the third generation semiconductors? Advanced skills are ready! Collect energy to win prizes! The event has begun~
- Programming example of MSP430 controlling digital tube
- Learn about C2000 32-bit microcontrollers
- Migrate ssh service to EK200-zlib-openssl-openssh
- Application Note Download | Keysight Technologies "Quickly Find and Identify Hidden Signal Errors"
- Ask a Question
- Cost less than 5 yuan, reliable single-fire power supply solution (using NP101A chip, including PDF...