2013-10-16

CooCox SPI learning notes

SPI CoX Peripheral Library Serial Peripheral Interface (SPI) Peripheral Driver. 

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

Collaboration diagram for SPI:

Modules

  xSPI
 
CoX SPI Peripheral Interface.


Detailed Description

Serial Peripheral Interface (SPI) Peripheral Driver.

The Serial Peripheral Interface (SPI) is a synchronous serial data communication protocol which operates in full duplex mode. Devices communicate in master/slave mode with 4-wire bi-direction interface.

Configure the SPI controler as master device or a slave , it should generate clock and chip-select signal if it is set as master.

Each SPI controller can generates an individual interrupt when data transfer is finished and the respective interrupt event flag IF will be set. The interrupt event flag will generates an interrupt to CPU if the interrupt enable bit IE is set.

Transfer and receive data which is in your mode

Contents

1. SPI Physical Block

2. SPI Function Description

2.1 SPI device controller configuration

2.2 SPI interrupt control

2.3 SPI Transfer and Receive data

3. SPI Applications

1. SPI Physical Block

2. SPI Function Description


2.1 SPI device controller configuration

Configure the SPI controller as a master or a slave device. It should generate clock and chip-select signals if it is set as a master device.


2.2 SPI interrupt control

Each SPI controller can generate an individual interrupt when data transfer is finished and the respective interrupt event flag IF will be set. The interrupt event flag will generates an interrupt to CPU if the interrupt enable bit IE is set.


2.3 SPI Transfer and Receive data

Transfer and receive data.


3. SPI Applications

will be added in the future



Example for SPI Views (502)  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.

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! 
    //

    //
    // 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();
}



MAX7219 Driver using GPIO interface Committer: CooCox Updated:2013-01-29 10:46:38

http://www.coocox.org/driver_comp/max7219-driver-using-gpio-interface-c637.html?mc=5&sc=26

Description:

MAX7219 Driver using GPIO interface

Depended COX: CoX.GPIO

Supported Device:   MAX7219

Overview

1.MAX7219 Feature

The MAX7219/MAX7221 are compact, serial input/out-put common-cathode display drivers that interface microprocessors (μPs) to 7-segment numeric LED displays of up to 8 digits, bargraph displays, or 64 individual LEDs. 

Included on-chip are a BCD code-B decoder, multiplex scan circuitry, segment and digit drivers, and an 8x8 static RAM that stores each digit. Only one external resistor is required to set the seg-ment current for all LEDs. The MAX7221 is compatible with SPI, QSPI, and Microwire, and has slew-rate-limited segment drivers to reduce EMI. 

A convenient 3-wire serial interface connects to all common μPs. Individual digits may be addressed and updated without rewriting the entire display. The MAX7219/MAX7221 also allow the user to select code-B decoding or no-decode for each digit. The devices include a 150μA low-power shutdown mode, analog and digital brightness control, a scan-limit register that allows the user to display from 1 to 8 digits, and a test mode that forces all LEDs on.

2.MAX7219 API

extern void MAX7219_Init(void); extern void MAX7219_Write(unsigned char ctlByte, unsigned char ucdata);

3.MAX7219 Applications

Here we introduce how to get use MAX7219.

 #include "xhw_types.h"
 #include "xhw_memmap.h"
 #include "xsysctl.h"
 #include "xgpio.h"
 #include "xspi.h"
 #include "MAX7219.h"

 int test(void)
 {
        unsigned short data[] = {0x0108, 0x0207, 0x0306, 0x0405,
                     0x0504, 0x0603, 0x0702, 0x0801};
        int i,j;
        int intensity=0x0f;

     //
     // Set SysClk 36MHz using Extern 12M oscillator
     //

     xSysCtlClockSet(36000000, xSYSCTL_OSC_MAIN | xSYSCTL_XTAL_12MHZ);

        MAX7219_Init();

        for(i=0;i<8;i++)
        {
                MAX7219_Write(data[i]>>8,data[i]);
        }

        while(1)
        {
                if(intensity>0x0f)
                        intensity = 0;
                for(i=0;i<300000;i++);
                MAX7219_Write(MAX7219_INTENSITY_ADDR, intensity);
                intensity++;
        }
 }

.END


SPI SPI CoX SPI Peripheral Interface

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

Collaboration diagram for xSPI:

Modules

  xSPI Interrupts
 
Values that show xSPI interrupts 
.


  xSPI Interrupt Event
 
Values that show xSPI interrupt events. 
.


  xSPI Configure
 
Values that show xSPI Configure 
.


  xSPI DMA
 
Values that show xSPI DMA 
.


  xSPI Slave Select Mode
 
Values show xSPI Slave Select Mode 
.


  xSPI Slave Select
 
Values that show xSPI Slave Select 
.


xSPI API  xSPI API Reference.


Detailed Description

CoX SPI Peripheral Interface.

The xSPI is the SPI Peripheral Interrupt of CoX.It defines some common macros and APIs macro and APIs

Contents

1 How is the SPI API Standard Define?
2 API Groups
2.1 2.1 SPI configuration control APIs
2.2 SPI Interrupt Control APIs
2.3 SPI Transfer&Receive Control
3 Program Examples

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)
    {   
    }
        
 }

.END




MAX7219 7 Segment LED Display Module ¥19.80


http://item.taobao.com/item.htm?spm=a230r.1.14.205.iJYkOi&id=10353123582

.END2

No comments:

Post a Comment