2013-10-17

CooCox CoX SPI/xSPI Program Examples - CooCox

CooCox CoX SPI/xSPI Program Examples

http://www.coocox.org/cox/manual/interface/index.html

http://www.coocox.org/cox/manual/interface/modules.html

http://www.coocox.org/cox/manual/interface/group___s_p_i.html

http://www.coocox.org/cox/manual/interface/group__x_s_p_i.html


1 How is the SPI API Standard Define?

CoX SPI defines the APIs based on the following common functions:

SPI Configure Control

Master or Slave Select

Clock Mode and data width

SPI Interrupt control

Enable Interrupt

ISR and CallBack Function

SPI Transfer and Receive

Configure SPI as master or slave mode

Implement Transfer and Receive

2 API Groups

The CoX SPI APIs can be classified into three groups by functions:

those that deal with SPI mode configuration,

those that deal with SPI interrupt controlling,

those that deal with SPI transfer and receive control.

2.1 2.1 SPI configuration control APIs

xSPIConfigSet()
xSPIEnable()
xSPIDisable()
xSPIDMADisable()
xSPIDMAEnable()

2.2 SPI Interrupt Control APIs

xSPIIntEnable()
xSPIIntDisable()
xSPIIntStatusGet()
xSPIIntPendingClear()

2.3 SPI Transfer & Receive Control

xSPIDataWrite()
xSPIDataRead()
xSPIBitLengthGet()
xSPISingleDataReadWrite()
xSPIIsTxFull()
xSPIIsRxFull()
xSPIIsTxEmpty()
xSPIIsRxEmpty()
xSPIIsBusy()

3 Program Examples

The following example shows how to use the SPI APIs to configue SPI and to implement the function of transferring and receiving.

 #include "xhw_memmap.h"
 #include "Xspi.h"
 #include "Xhw_spi.h"
 #include "Xsysctl.h"
 #include "xhw_types.h"
 #include "Xgpio.h"
 #include "Xhw_gpio.h"
 #include "SPI_loop.h"
 #include "Xhw_sysctl.h"
 #include "test.h"
 #include "testcase.h"

 unsigned char SendData[] = "Wonderful World";
 unsigned char ReceData[64];
 unsigned long i;

 //*****************************************************************************

 //Ininite the SPI case which is needed

 //param None

 //This function ininite the SPI including clock source and enable spi

 //return none

 //*****************************************************************************

 void SpiReceiveTransferInit(void)
 {
    SysCtlKeyAddrUnlock();
    xHWREG(SYSCLK_PWRCON) |= SYSCLK_PWRCON_XTL12M_EN;
    xHWREG(SYSCLK_PLLCON) &= ~SYSCLK_PLLCON_OE;
    xHWREG(SYSCLK_PLLCON) &= ~SYSCLK_PLLCON_PD;
    //
    // Set SysClk 36MHz using Extern 12M oscillator
    //
    xSysCtlClockSet(36000000, xSYSCTL_OSC_MAIN|xSYSCTL_XTAL_12MHZ);
    //
    // Enable Peripheral SPI0,SPI1
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SPI0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SPI1);
    SPIIntEnable(SPI0_BASE, TransferOver);
  }

 //*****************************************************************************

 //Spi Master Slave Mode Configure

 //param None

 //This function configure the SPI as master and slave mode at the same time
 //transfer and receive data

 //return None

 //*****************************************************************************
 void SpiMasterSlaveModeConfig(void)
 {
   
    //
    // Configure Some GPIO pins as SPI Mode
    //
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_2);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_3);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_0);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_1);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_9);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_8);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_10);
    GPIOPinFunctionSet(GPIO_FUNCTION_SPI, GPIO_PORTC_BASE,GPIO_PIN_11);
    //
    // Master Mode  polarity 0,Rx latched at rising edge Tx changed at rising edge
    // 2000000Hz 8Bits Data windth  SPI MSB First
    //
 SPIConfig(SPI0_BASE,2000000,SPI_FORMAT_MODE_5|SPI_DATA_WIDTH8|SPI_MSB_FIRST|SPI_MODE_MASTER);
    //
    // Slave Mode  polarity 0,Rx latched at rising edge Tx changed at rising edge
    // 2000000Hz 8Bits Data windth  SPI MSB First
    //
    xHWREG(SPI1_BASE + SPI_CNTRL) = 0x00000000;
    xHWREG(SPI1_BASE + SPI_CNTRL) |= 0x00040844;
    //
    // Software Control Transfer Select SS0 and SS1
    //
    xSPISSSet(SPI0_BASE,xSPI_SS_SOFTWARE,SPI_SS0);  

    //
    // Level Trig Slave at High Level
    //
    SPISSConfig(SPI0_BASE, SPI_SS_LEVEL_TRIGGER,SPI_SS_ACTIVE_HIGH_RISING);
    SPISSConfig(SPI1_BASE, SPI_SS_LEVEL_TRIGGER,SPI_SS_ACTIVE_HIGH_RISING);


    for(i=0;i<1;i++)
    {
        SPIDataWrite(SPI0_BASE,SendData,15);
        SPIDataRead(SPI1_BASE,ReceData, 16);
        TestIOPut('O');
        TestIOPut('K');
    }
    TestIOPut('\n');
    for(i=0;i<18;i++)
    {
        TestIOPut(ReceData[i]); 
    }
 }
 //******************************************************************************
 //
 // The main function of the SPI transfer 
 //
 //******************************************************************************
 int main(void)
 {
    SpiReceiveTransferInit();
    SpiMasterSlaveModeConfig();
    while(1)
    {  
    }
       
 }

*** Example for SPI - SPI_LoopBack Author: CooCox ***

http://www.coocox.org/show_exam/SPI/349.html

Description:

This example shows how to use SPI. 

Step 1: Set system clock. 

Step 2: Enable GPIO and interrupt. 

Step 3: Set the GPIO pins and trig interrupt. 

This example runs on Nu-LB-Mini_V001 Development Board,and it's suit for Mini51 serial mcu.

Nu-LB-Mini51 User Manual For NuMicro™ Mini51 Series

http://download.nuvoton.com/NuvotonMOSS/DownloadService/Member/DocumentsInfo.aspx?
tp_GUID=UG0220120118161108

2.2 Pin Assignment for Extended Connector 

Nu-LB-Mini51 provides Mini54LAN on board and the extended connector for LQFP-48 pin. Table 2-2 is the 
pin assignment for Mini54LAN.

P0.4, SPISS

P0.5, MOSI

P0.6, MISO


P0.7, SPICLK



MINI54LAN Specifications

http://www.digchip.com/datasheets/parts/datasheet/2103/MINI54LAN.php

Program Memory Size 16KB (16K x 8)

RAM Size 2K x 8

Number of I /O 30

Core Processor ARM Cortex-M0

Connectivity I2C, IrDA, SPI, UART/USART

Example Code

//*****************************************************************************
//
//! Include related header files  
//
//*****************************************************************************
#include "DrvSYS.h"
#include "DrvSPI.h"
#include "DrvGPIO.h"

//*****************************************************************************
//
//! \brief SPI LoopBack Example.
//!
//! \param None  
//!
//! \return None  
//
//*****************************************************************************
void SPILoopBack(void)
{
    uint32_t u32TxData, u32RxData;
     
    //
    // Enable high external clock and use it as system clock (HCLK)
    //
          DrvSYS_Open(XTL_CLK);

    //
    // Waiting for 12M Xtal stable
    //
    while (DrvSYS_GetChipClockSourceStatus(XTL_CLK) != 1);
     
    //
    // Configure SPI0 related multi-function pins
    //
    DrvGPIO_InitFunction(FUNC_SPI);
    
    //
    // Configure SPI0 as a master, 32-bit transaction
    //
    DrvSPI_Open(eDRVSPI_MASTER, eDRVSPI_TYPE7, 32);
    
    //
    // Enable the automatic slave select function of SS0.
    //
    DrvSPI_EnableAutoSS();
    
    //
    // Set the active level of slave select.
    //
    DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_ACTIVE_LOW_FALLING);
    
    //
    // SPI clock rate 2MHz
    //
    DrvSPI_SetClockFreq(200000, 0);

    DrvSPI_SetByteReorder(eDRVSPI_BYTE_REORDER);

    //
    // Please connect P0.5 to P0.6 and test! [Only for Nu-LB-Mini51] 
    //

    //
    // Clear Tx register of SPI1 to avoid send non-zero data to Master. Just for safe.
    //

    u32TxData = 0x55;

    while(DrvSPI_IsBusy() == TRUE);

    DrvSPI_SetTxRegister(&u32TxData, 1);
    
    //
    // set the GO_BUSY bit of SPI0
    //
    DrvSPI_SetGo();
    
    //
    // Wait until transfer is done
    //
    while(DrvSPI_IsBusy() == TRUE);
    DrvSPI_DumpRxRegister(&u32RxData, 1);

    DrvSPI_Close();
}


.END

No comments:

Post a Comment