2013-10-25

Olimex SPI1 testing notes

// ***********************************************************************
// spi050.h 2013oct25hk1010
// ***********************************************************************

#include "gpio050.h"
#include "led050.h"
#include "lpc11xx_ssp.h"
#include "semihosting.h"
#include "stdio.h"

// ***********************************************************************
// Max7219 Functions
// ***********************************************************************

void max7219v02Spi1() // 2013oct25hkt1459
{
    // *** Print project title ***********************************************
printf("*** MAX7219 Test (Using SPI1) - 2013oct25hk1459 ***\n\n");

// *** Setup MAX7219 digit control register buffer ***********************
// Digit control register buffer
#define BUFFER_SIZE 2
#define ADDRESS_INDEX 0
#define DATA_INDEX 1

uint8_t digitControlRegisterBuffer[BUFFER_SIZE];
uint8_t dummyReceiveBuffer[BUFFER_SIZE];

// *** Set up SPI ********************************************************
// Setup SPI0 SCLK and SSEL
// setupSpi050(SCK0_PIO0_6);
// setupSpiSsel();

setupSpi1050();

// Setup SPI0 xferConfig
    SSP_DATA_SETUP_Type xferConfig;
    SSP_DATA_SETUP_Type *xferConfigPointer;
    xferConfigPointer = &xferConfig;

    setupXferConfig(xferConfigPointer, digitControlRegisterBuffer, dummyReceiveBuffer, BUFFER_SIZE);


// *** Setup Max7219 digit and control ***********************************
    // Register addresses
#define NO_OP_ADDR 0x00
#define DIGIT_0_ADDR 0x01
    #define DIGIT_1_ADDR 0x02
#define DIGIT_2_ADDR 0x03
#define DIGIT_3_ADDR 0x04
#define DIGIT_4_ADDR 0x05
#define DIGIT_5_ADDR 0x06
#define DIGIT_6_ADDR 0x07
#define DIGIT_7_ADDR 0x08
#define DECODE_MODE_ADDR 0x09
#define INTENSITY       0x0a
#define SCAN_LIMIT_ADDR 0x0b
#define SHUTDOWN_ADDR   0x0c
#define DISPLAY_TEST     0x0f

// Control byte
#define SHUTDOWN_MODE          0x00
#define NORMAL_OPERATION_MODE  0x01
    #define CODE_B_DECODE_ALL_BITS 0xff
    #define DISPLAY_8_DIGITS       0x07

#define DIGIT_0_DATA 0
#define DIGIT_1_DATA 1
    #define DIGIT_2_DATA 2
    #define DIGIT_3_DATA 3
    #define DIGIT_4_DATA 4
#define DIGIT_5_DATA 5
#define DIGIT_6_DATA 6
#define DIGIT_7_DATA 7
#define DIGIT_8_DATA 8
#define DIGIT_9_DATA 9

    while (1)
        {
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR, SHUTDOWN_MODE);
        writeBufferToMax7219Spi1(xferConfigPointer);
        delayTime(ONE_SECOND);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DECODE_MODE_ADDR,
                                       CODE_B_DECODE_ALL_BITS);
        writeBufferToMax7219Spi1(xferConfigPointer);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
        writeBufferToMax7219Spi1(xferConfigPointer);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_0_ADDR, DIGIT_1_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_1_ADDR, DIGIT_1_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_2_ADDR, DIGIT_2_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_3_ADDR, DIGIT_3_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_4_ADDR, DIGIT_4_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_5_ADDR, DIGIT_5_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_6_ADDR, DIGIT_6_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_7_ADDR, DIGIT_7_DATA);
        writeBufferToMax7219Spi1(xferConfigPointer);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR,
                                       NORMAL_OPERATION_MODE);
        writeBufferToMax7219Spi1(xferConfigPointer);
        delayTime(ONE_SECOND);
        }
}

void max7219v02() // 2013oct25hkt0934
{
    // *** Print project title ***********************************************
printf("*** MAX7219 Test - 2013oct25hk1010 ***\n\n");


// *** Setup MAX7219 digit control register buffer ***********************
// Digit control register buffer
#define BUFFER_SIZE 2
#define ADDRESS_INDEX 0
#define DATA_INDEX 1

uint8_t digitControlRegisterBuffer[BUFFER_SIZE];
uint8_t dummyReceiveBuffer[BUFFER_SIZE];


// *** Set up SPI ********************************************************
// Setup SPI0 SCLK and SSEL
setupSpi050(SCK0_PIO0_6);
setupSpiSsel();

// Setup SPI0 xferConfig
    SSP_DATA_SETUP_Type xferConfig;
    SSP_DATA_SETUP_Type *xferConfigPointer;
    xferConfigPointer = &xferConfig;

    setupXferConfig(xferConfigPointer, digitControlRegisterBuffer, dummyReceiveBuffer, BUFFER_SIZE);


// *** Setup Max7219 digit and control ***********************************
    // Register addresses
#define NO_OP_ADDR 0x00
#define DIGIT_0_ADDR 0x01
    #define DIGIT_1_ADDR 0x02
#define DIGIT_2_ADDR 0x03
#define DIGIT_3_ADDR 0x04
#define DIGIT_4_ADDR 0x05
#define DIGIT_5_ADDR 0x06
#define DIGIT_6_ADDR 0x07
#define DIGIT_7_ADDR 0x08
#define DECODE_MODE_ADDR 0x09
#define INTENSITY       0x0a
#define SCAN_LIMIT_ADDR 0x0b
#define SHUTDOWN_ADDR   0x0c
#define DISPLAY_TEST     0x0f

// Control byte
#define SHUTDOWN_MODE          0x00
#define NORMAL_OPERATION_MODE  0x01
    #define CODE_B_DECODE_ALL_BITS 0xff
    #define DISPLAY_8_DIGITS       0x07

#define DIGIT_0_DATA 0
#define DIGIT_1_DATA 1
    #define DIGIT_2_DATA 2
    #define DIGIT_3_DATA 3
    #define DIGIT_4_DATA 4
#define DIGIT_5_DATA 5
#define DIGIT_6_DATA 6
#define DIGIT_7_DATA 7
#define DIGIT_8_DATA 8
#define DIGIT_9_DATA 9

    while (1)
        {
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR, SHUTDOWN_MODE);
        writeBufferToMax7219(xferConfigPointer);
        delayTime(ONE_SECOND);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DECODE_MODE_ADDR,
                                       CODE_B_DECODE_ALL_BITS);
        writeBufferToMax7219(xferConfigPointer);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
        writeBufferToMax7219(xferConfigPointer);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_0_ADDR, DIGIT_1_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_1_ADDR, DIGIT_1_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_2_ADDR, DIGIT_2_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_3_ADDR, DIGIT_3_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_4_ADDR, DIGIT_4_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_5_ADDR, DIGIT_5_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_6_ADDR, DIGIT_6_DATA);
        writeBufferToMax7219(xferConfigPointer);
        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, DIGIT_7_ADDR, DIGIT_7_DATA);
        writeBufferToMax7219(xferConfigPointer);

        writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR,
                                       NORMAL_OPERATION_MODE);
        writeBufferToMax7219(xferConfigPointer);
        delayTime(ONE_SECOND);
        }
}


// *** Max7219 Functions ***

void writeSegmentLedCommandToBuffer(uint8_t digitControlRegisterBuffer[BUFFER_SIZE], \
                    uint8_t commandRegisterAddress, uint8_t command)
{
digitControlRegisterBuffer[ADDRESS_INDEX] = commandRegisterAddress;
digitControlRegisterBuffer[DATA_INDEX] = command;
}

void writeBufferToMax7219(SSP_DATA_SETUP_Type *xferConfigPointer)
{
setSsel1Low();
SSP_ReadWrite(LPC_SSP0, xferConfigPointer, SSP_TRANSFER_POLLING);
setSsel1High();
}

void writeBufferToMax7219Spi1(SSP_DATA_SETUP_Type *xferConfigPointer)
{
setSsel1Low();
SSP_ReadWrite(LPC_SSP1, xferConfigPointer, SSP_TRANSFER_POLLING);
setSsel1High();
}

// *** SPI Functions ***

void setupSpi050(SCK0_Position_Typedef sck0)
{
// Enable SSP0 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP0, ENABLE);

// Reset SSP0 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP0, DISABLE);
SYSCON_SetSPI0ClockDiv(10);

// Assign GPIO pins for SPI
// SSP_SSP0PinsInit(sck0, ENABLE);
SSP_SSP0PinsInit(sck0, DISABLE); // Disable SSEL

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP0, &SSP_ConfigStruct);

// Enable SSP peripheral
SSP_Cmd(LPC_SSP0, ENABLE);
}

void setupSpi1050(SCK0_Position_Typedef sck1)
{
// Enable SSP1 block clock
SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_SSP1, ENABLE);

// Reset SSP1 and clock divider
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, ENABLE);
SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, DISABLE);
SYSCON_SetSPI0ClockDiv(10);

// Assign GPIO pins for SPI1
// SSP_SSP0PinsInit(sck1, ENABLE);
SSP_SSP0PinsInit(sck1, DISABLE); // Disable SSEL1

// Initialize SSP with default configuration (Master mode, 8 bit data)
SSP_CFG_Type SSP_ConfigStruct;
SSP_ConfigStructInit(&SSP_ConfigStruct);
SSP_Init(LPC_SSP1, &SSP_ConfigStruct);

// Enable SSP peripheral
SSP_Cmd(LPC_SSP1, ENABLE);
}

void setupXferConfig(SSP_DATA_SETUP_Type *xferConfigPointer, uint8_t digitControlRegisterBuffer[], \
            uint8_t dummyReceiveBuffer[], uint8_t bufferSize)
{
    xferConfigPointer->tx_data = digitControlRegisterBuffer;
    xferConfigPointer->rx_data = dummyReceiveBuffer;
    xferConfigPointer->length = bufferSize;
 }

void setupSpiSsel()
{
setupGpioPinOutputLow050(PortPinArraySsel); // setup GPIO pin as output
}

void setSselLow()
{
setGpioDataPinLow01(PortPinArraySsel); // SSEL low
}

void setSselHigh()
{
setGpioDataPinHigh01(PortPinArraySsel); // SSEL High
}

void setupSpiSsel1()
{
setupGpioPinOutputLow050(PortPinArraySsel1); // setup GPIO pin as output
}

void setSsel1Low()
{
setGpioDataPinLow01(PortPinArraySsel1); // SSEL1 low
}

void setSsel1High()
{
setGpioDataPinHigh01(PortPinArraySsel1); // SSEL1 High
}



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

void max7219v01() // 2013oct24hkt2229
{
    // *** Print project title ***********************************************
printf("*** MAX7219 Test - 2013oct24hk0937 ***\n\n");


// *** Setup MAX7219 digit control register buffer ***********************
// Digit control register buffer
#define BUFFER_SIZE 2
#define ADDRESS_INDEX 0
#define DATA_INDEX 1

uint8_t digitControlRegisterBuffer[BUFFER_SIZE];
uint8_t dummyReceiveBuffer[BUFFER_SIZE];


// *** Set up SPI ********************************************************
// Setup SCLK and SSEL
setUpSpi050(SCK0_PIO0_6);
setupSpiSsel();

// Setup xferConfig
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = (void*)digitControlRegisterBuffer;
    xferConfig.rx_data = (void*)dummyReceiveBuffer;
    xferConfig.length = BUFFER_SIZE;


// *** Setup Max7219 digit and control ***********************************
    // Register addresses
#define NO_OP_ADDR 0x00
#define DIGIT_0_ADDR 0x01
    #define DIGIT_1_ADDR 0x02
#define DIGIT_2_ADDR 0x03
#define DIGIT_3_ADDR 0x04
#define DIGIT_4_ADDR 0x05
#define DIGIT_5_ADDR 0x06
#define DIGIT_6_ADDR 0x07
#define DIGIT_7_ADDR 0x08
#define DECODE_MODE_ADDR 0x09
#define INTENSITY       0x0a
#define SCAN_LIMIT_ADDR 0x0b
#define SHUTDOWN_ADDR   0x0c
#define DISPLAY_TEST     0x0f

// Control byte
#define SHUTDOWN_MODE 0x00
#define NORMAL_OPERATION_MODE 0x01

    // *** Test Max7219 ******************************************************

    // *** Normal operation 1 second, shut down 1 second ***
    while (1)
    {
    writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR, NORMAL_OPERATION_MODE);
setSselLow();
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setSselHigh();

delayTime(ONE_SECOND);

    writeSegmentLedCommandToBuffer(digitControlRegisterBuffer, SHUTDOWN_ADDR, SHUTDOWN_MODE);
setSselLow();
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setSselHigh();

delayTime(ONE_SECOND);
    }
}

// ***********************************************************************
// End of max7219
// ***********************************************************************

No comments:

Post a Comment