SPI communication bare metal source code

SPI Master Mode Source File

/****************************************************************************
SPI mode communication
SPI master mode:
SPI master mode byte transfer begins when register UxBUF is written with a byte. USART uses baud rate generator to generate
into the SCK serial clock, and transfer the byte provided by the transmit register to the output pin MOSI. At the same time, receiving
The register gets the received byte from input pin MISO.

SPI slave mode: (rising or falling edge trigger programmable control)
On the falling edge of SSN, the SPI slave mode is active, receiving data on the MOSI input and outputting data on the MOSI output.
On the rising edge of SSN, the SPI slave mode is inactive and does not receive data.
****************************************************************************/
#include <ioCC2540.h>
#include "hal_cc8051.h"

#define LED1          P1_0

unsigned char temp = 0; // data send and receive buffer

void SPI_Master_Init()
{
    CLKCONCMD = 0x80; while (CLKCONSTA != 0x80); // system clock configured as 32MHz
    
    // SPI master mode configuration
    PERCFG |= 0x02; // Alternate location 2 for I/O using USART1
                                  // P1_4: SSN, P1_5: SCK, P1_6: MOSI, P1_7: MISO
    P1SEL |= 0xE0; // Configure P1_5, P1_6, P1_7 as peripheral functions   
    P1SEL &= ~0x10; // Configure P1_4 as general purpose I/O port (SSN)
    P1DIR |= 0x10; // Configure SSN pin as output pin
    U1BAUD = 0x00; U1GCR |= 0x11; // configure baud rate 4MHz
    U1CSR &= ~0xA0; // Configure as SPI mode and as SPI master
    U1GCR &= ~0xC0; //SCLK is at low level when idle, data is accepted on the rising edge, and data is sent on the falling edge
    U1GCR |= 0x20; // MSB (high byte) is transmitted first
}

void SPI_Master_Receive()
{
    P1_4 = 0; // SSN falling edge, SPI slave is active and starts to send and receive data
    U1DBUF = 0x33; // Send data to the data buffer register
    while(!(U1CSR&0x02)); // Wait for the data transfer to complete, that is, the send completion flag is set to 1
    U1CSR &= 0xFD; // Clear the send completion flag bit, that is, the send completion flag bit is 0
    temp = U1DBUF; // Receive data from data buffer register
    P1_4 = 1; // SSN rising edge, the SPI slave is not active and does not receive data
}


void P1_Init()
{
    P1DIR |= 0x01;
    LED1 = 0;
}

void main()
{
    SPI_Master_Init();
    P1_Init();
    
    for(;;)
    {
      SPI_Master_Receive();
      
      if( temp == 0x11 )
      {
        LED1 = 1;
      }
      else
        LED1 = 0;
      
      halMcuWaitMs(300);
    }
    
}

source file from schema

#include <ioCC2530.h>

#define LED2          P1_1

unsigned char temp=0; // data receive buffer

void SPI_Slave_Init()
{
    CLKCONCMD = 0x80; while(CLKCONSTA != 0x80); // system clock is configured as 32MHz     
    
    // SPI slave mode configuration
    PERCFG |= 0x02; // Alternate location 2 for I/O using USART1
                                   // P1_4: SSN 、 P1_5: SCK 、 P1_6: MOSI 、 P1_7: MISO
    P1SEL |= 0xF0; // Configure P1_4, P1_5, P1_6, P1_7 as peripheral functions
    U1BAUD = 0x00; U1GCR |= 0x11; // configure baud rate 4MHz
    U1CSR &= ~0x80; U1CSR |= 0x20; // Configure as SPI mode and SPI slave
    U1GCR &= ~0xC0; //SCLK is at low level when idle, data is accepted on the rising edge, and data is sent on the falling edge
    U1GCR |= 0x20; // MSB (high byte) is transmitted first
}

void SPI_Slave_Receive()
{
    while (!(U1CSR&0x04)); //Wait for the completion of data acceptance, that is, the acceptance completion flag is set to 1
    U1CSR &= 0xFB; // Clear the acceptance completion flag, that is, the acceptance completion flag is set to 0
    temp = U1DBUF; // read data from data buffer register
}

void P1_Init()
{
    P1DIR |= 0x02;
    LED2 = 0;
}

void main()
{
    P1_Init();
    SPI_Slave_Init();
    
    for(;;)
    {
      U1DBUF = 0x11;
      SPI_Slave_Receive();
      if( temp == 0x33 )
      {
        LED2 = 1;
      }
      else
        LED2 = 0;
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385331&siteId=291194637