2013-11-06

MAX7219 function refactoring notes



// ***********************************************************************
// Program  - Fong EKG v5.0
// Function - Olimex EKG Board Evaluation
// Author   - TL Fong
// Build    - 2013.11.06.01
// Date     - 2013nov06hkt1130
// Hardware - Olimex/CooCox/MagicBlue/WHUT/Somy LPC1114/C14/301/FN28
//            Olimex EKG board (SHIELD-EKG/EMG Rev B, Bulgaria 2011)
// Software - GCC ARM 4.7, CoIDE 1.7.5,  CoLinkEx 1.1, Flash Magic v7.66
// ***********************************************************************


testMax7219SegmentLed01(SPI_CHANNEL_0);



// ***************************************************************************
// Function - Test MAX7219 7-segment and 8x8 matrix LED
// Date     - 2013nov06
// ***************************************************************************

void testMax7219SegmentLed01(uint8_t spiChannelNumber)
{
setupSpi051(spiChannelNumber);
max7219SegmentLed01(spiChannelNumber);
}




// ***********************************************************************
// mled050.h 2013nov06hkt1128
// 4-digit 7-segment and 8x8 matrix LED controlled by MAX7219
// ***********************************************************************

#include "spi050.h"

// *** MAX7219 Register Address Map ***
#define NO_OP_ADDR 0x00
#define DIGIT_ADDR_0 0x01
#define DIGIT_ADDR_1 0x02
#define DIGIT_ADDR_2 0x03
#define DIGIT_ADDR_3 0x04
#define DIGIT_ADDR_4 0x05
#define DIGIT_ADDR_5 0x06
#define DIGIT_ADDR_6 0x07
#define DIGIT_ADDR_7 0x08
#define DECODE_MODE_ADDR 0x09
#define INTENSITY_ADDR      0x0a
#define SCAN_LIMIT_ADDR  0x0b
#define OPERATION_ADDR    0x0c // Shutdown address actually
#define DISPLAY_TEST_ADDR   0x0f

// *** MAX7219 Command Format ***
#define SHUTDOWN            0x00
#define NORMAL              0x01
#define CODE_B_ALL_DIGITS   0xff
#define DISPLAY_8_DIGITS    0x07

// *** Data Constants ***
#define DIGIT_0 0
#define DIGIT_1 1
#define DIGIT_2 2
#define DIGIT_3 3
#define DIGIT_4 4
#define DIGIT_5 5
#define DIGIT_6 6
#define DIGIT_7 7
#define DIGIT_8 8
#define DIGIT_9 9

// ***************************************************************************
// Function - Display 4 digits of 7-segment LED
// Date     - 2013nov06
// ***************************************************************************
void max7219SegmentLed01(uint8_t spiChannelNumber)
{
    // *** Print project title ***********************************************
printf("\n\n*** Matrix LED 01 - 2013nov06hkt1128 ***\n\n");

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

uint8_t txBuffer[BUFFER_SIZE];
uint8_t rxBuffer[BUFFER_SIZE];

    // *** Setup SPI xferConfig ***
    SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = txBuffer;
    xferConfig.rx_data = rxBuffer;
    xferConfig.length = BUFFER_SIZE;

    // *** Setup MAX7219 for digit decode and scan limit ***
    setUpMax7219(txBuffer, xferConfig, spiChannelNumber);

    // *** Display 8 digits ***
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_7, DIGIT_1);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_6, DIGIT_1);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_5, DIGIT_0);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_4, DIGIT_6);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_3, DIGIT_1);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_2, DIGIT_1);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_1, DIGIT_2);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, DIGIT_ADDR_0, DIGIT_8);
    displayOneDigit(txBuffer, xferConfig, spiChannelNumber, OPERATION_ADDR, NORMAL);
}

// ***************************************************************************
// Function - Write MAX7219 command
// Date     - 2013nov06
// ***************************************************************************
void writeLedCommand(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
             uint8_t spiChannelNumber, \
             uint8_t commandRegisterAddress, uint8_t command)
{
txBuffer[ADDRESS_INDEX] = commandRegisterAddress;
txBuffer[DATA_INDEX] = command;
    SpiWriteRead050(xferConfig, spiChannelNumber);
}

// ***************************************************************************
// Function - Display 1 digit
// Date     - 2013nov06
// ***************************************************************************
void displayOneDigit(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
             uint8_t spiChannelNumber, \
             uint8_t digitAddress, uint8_t digitData)
{
writeLedCommand(txBuffer, xferConfig, spiChannelNumber, digitAddress, digitData);
}

// ***************************************************************************
// Function - Set up MAX7219 for Code B of all 8 digits, scan 8 digits
// Date     - 2013nov06
// ***************************************************************************
void setUpMax7219(uint8_t txBuffer[BUFFER_SIZE], SSP_DATA_SETUP_Type xferConfig, \
                  uint8_t spiChannelNumber)
{
writeLedCommand(txBuffer, xferConfig, spiChannelNumber, SHUTDOWN_ADDR, SHUTDOWN_MODE);
    delayTime(ONE_FIFTH_SECOND);
    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, \
                DECODE_MODE_ADDR, CODE_B_ALL_DIGITS);
    writeLedCommand(txBuffer, xferConfig, spiChannelNumber, \
                SCAN_LIMIT_ADDR, DISPLAY_8_DIGITS);
}

// ***********************************************************************
// End
// ***********************************************************************





// ***********************************************************************
// Program  - Fong EKG v5.0
// Function - Olimex EKG Board Evaluation
// Author   - TL Fong
// Build    - 2013.11.06.01
// Date     - 2013nov06hkt1130
// Hardware - Olimex/CooCox/MagicBlue/WHUT/Somy LPC1114/C14/301/FN28
//            Olimex EKG board (SHIELD-EKG/EMG Rev B, Bulgaria 2011)
// Software - GCC ARM 4.7, CoIDE 1.7.5,  CoLinkEx 1.1, Flash Magic v7.66
// ***********************************************************************

#include "test050.h"

// ***********************************************************************
// Main Function
// ***********************************************************************

int main()
{
// *** Old tests ***
// testOlimexLedKey01();
// testWhutLedKey01();
// testOlimex7segmentLed01();
// testSpi03();
// testWhut7segmentLed01(); // OK 2013oct23
// testWhutLedP108(); // OK 2013oct23
// testOlimex7segmentLed0101();
// testOlimex7segmentLed0201();
// testBlinkP03()
// testMcp320801();

    // *** Olimex tests - 2013oct31hkt122 ***
// testBlinkOlimexLed1();
// testBlinkLpc1114LedP03();
// testOlimex7segmentLed0101();
// testMax7219v04(SPI_CHANNEL_0);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_0);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_1);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_2);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_3);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_4);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_5);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_6);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_7);

    // *** MagicBlue tests - 2013nov03hkt1054 ***
// testBlinkLpc1114LedP09(); // MOSI now for SPI0

testBlinkLpc1114LedP03();

/*
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_0);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_1);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_2);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_3);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_4);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_5);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_6);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_7);
testMcp3208(SPI_CHANNEL_0, ADC_CHANNEL_0);
*/

/*
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_0);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_1);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_2);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_3);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_4);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_5);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_6);
testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_7);
*/

// testMax7219v04(SPI_CHANNEL_1);
// testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_0);
// testMcp25Lc256(SPI_CHANNEL_1);

// testMax7219v04(SPI_CHANNEL_1);
// testMax7219v04(SPI_CHANNEL_0);

testMax7219SegmentLed01(SPI_CHANNEL_0);

    return 0;
}

// ***********************************************************************
// End
// ***********************************************************************



// ***********************************************************************
// test050.h 2013oct24hkt1022
// ***********************************************************************

#include "led050.h"
#include "key050.h"
#include "spi050.h"
#include "adc050.h"
#include "sled050.h"
#include "eeprom050.h"
#include "mled050.h"

// ***************************************************************************
// Function - Test MAX7219 7-segment and 8x8 matrix LED
// Date     - 2013nov06
// ***************************************************************************
void testMax7219SegmentLed01(uint8_t spiChannelNumber)
{
setupSpi051(spiChannelNumber);
max7219SegmentLed01(spiChannelNumber);
}


// ***************************************************************************
// Function - Test 8-channel 12-bit ADC MCP3208
// Call     - testMcp3208(SPI_CHANNEL_1, ADC_CHANNEL_2);
// Date     - 2013oct27
// ***************************************************************************
void testMcp3208(uint8_t spiChannelNumber, uint8_t adcChannelNumber)
{
mcp3208v03(spiChannelNumber, adcChannelNumber); // SPI0
}

// ***************************************************************************
// Function - Test Max7219
// Call     - testMax7219v04(uint8_t spiChannelNumber)
// Date     - 2013oct30
// ***************************************************************************
void testMax7219v04(uint8_t spiChannelNumber)
{
max7219v04(spiChannelNumber);
}

// ***************************************************************************
// Function - Blink Olimex Led01 (P30)
// Call     - testBlinkOlimexLed1()
// Date     - 2013oct27
// ***************************************************************************
void testBlinkOlimexLed1()
{
setupGpioPinOutputLow050(OlimexBoardLedPortPinArray1); // Setup as output P30
blinkLed0501(OlimexBoardLedPortPinArray1); // Blink
}

// ***************************************************************************
// Function - Blink P03 LED
// Call     - testBlinkLpc1114LedP03()
// Date     - 2013oct27
// ***************************************************************************
void testBlinkLpc1114LedP03()
{
    setupGpioPinOutputLow050(PortPinArrayP03); // Setup as output P03
blinkLed0501(PortPinArrayP03); // Blink
}

// ***************************************************************************
// Function - Blink P09 LED
// Call     - testBlinkLpc1114LedP03()
// Date     - 2013oct27
// ***************************************************************************
void testBlinkLpc1114LedP09()
{
    setupGpioPinOutputLow050(PortPinArrayP09); // Setup as output
blinkLed0501(PortPinArrayP09); // Blink
}

// ***************************************************************************
// Function - Test HC595 based 7-segment LED
// Call     - testOlimex7segmentLed0101()
// Date     - 2013oct27
// ***************************************************************************
void testOlimex7segmentLed0101()
{
displayAllDigits050(OlimexBoardSevenSegmentLedPortPinArrayPointerArray1, MAX_OLIMEX_7_SEG_LED_PIN_NUMBER);
}

void testOlimex7segmentLed0102()
{
displayFourDigits050(OlimexBoardSevenSegmentLedPortPinArrayPointerArray1, MAX_OLIMEX_7_SEG_LED_PIN_NUMBER, 2, 0, 1, 3);
}

// ***************************************************************************
// End of tests - 2013oct27
// ***************************************************************************


void testMax7219Spi1()
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// max7219v01();
max7219v02Spi1();
}

/*
void testMax7219()
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// max7219v01();
max7219v02();
}
*/

void testMcp3208Spi1() // 2013oct25
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// *** Test MCP3028 ***
mcp3208v02Spi1();
}

void testSspLoopBack04()
{
sspLoopback04();
}

void testBlinkP03()
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led
}

void testMcp320801() // 2013oct20
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// *** Test MCP3028 ***
mcp320801();
}

void testSpi03() // 2013oct19
{
// *** Blink LED at P03 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// *** Test SPI loopback ***
// loopBackSpi01();

loopBackSpi03();
}

void testOlimexLedKey01() // tested OK 2013oct13
{
// *** For all mcu boards - setup GPIO pin as out and toggle ***
setupGpioPinOutputLow050(PortPinArrayP09); // setup GPIO pin as ooutput
blinkLed0501(PortPinArrayP09); // blink led

// *** Olimex test only - setup I/O pins for testing ***
setupOlimexGpio050(); // setup Olimex GPIO pins as output and input

// *** Olimex test only - blink single LED and run all LEDs***
blinkOlimexLed(LED_7, ON_ONE_FIFTH_SECOND, OFF_ONE_FIFTH_SECOND, REPEAT_6_TIMES); // blink one Led
runOlimexLeds();  // sequentially blink all 8 Leds

// *** Olimex test only - echo key by led ***
echoOlimexKeyByLed(KEY_1, LED_1); // echo Olimex key by led
echoOlimexKeyByLed(KEY_2, LED_2); // echo Olimex key by led
echoOlimexKeyByLed(KEY_1, LED_3); // echo Olimex key by led

// *** For all mcu boards, given mcu board struct, echo key by led ***
echoKeyByLed0504(OlimexLpc1114DevBoardStruct, KEY_1, LED_5); // given mcuboard struct, echo key by led
}

void testWhutLedKey01() // tested OK 2013oct14
{
// *** For all mcu boards - setup GPIO pin as out and toggle ***
setupGpioPinOutputLow050(PortPinArrayP108); // setup LED1 pin
blinkLed0501(PortPinArrayP108); // blink LED1

// *** Whut only - setup I/O pins for testing ***
setupWhutGpio050(); // setup Olimex GPIO pins as output and input

// *** Whut only - blink single LED and run all LEDs***
blinkWhutLed(LED_1, ON_ONE_FIFTH_SECOND, OFF_ONE_FIFTH_SECOND, REPEAT_6_TIMES); // blink one Led
runWhutLeds();  // sequentially blink all 8 Leds

// *** Whut only - echo key by led ***
echoWhutKeyByLed(KEY_1, LED_1); // echo key by led
echoWhutKeyByLed(KEY_2, LED_2); // echo key by led
echoWhutKeyByLed(KEY_1, LED_3); // echo key by led

// *** For all mcu boards, given mcu board struct, echo key by led ***
echoKeyByLed0505(WhutLpc11c14DevBoardStruct, KEY_1, LED_2); // given mcuboard struct, echo key by led
}

void testWhutLedP108()
{
    // *** system self diagnostic test - blink LED P108 ***
setupGpioPinOutputLow050(PortPinArrayP108); // setup LED1 pin
blinkLed0501(PortPinArrayP108); // blink LED1
}

void testWhut7segmentLed01()
{
    // *** system self diagnostic test - blink LED P108 ***
setupGpioPinOutputLow050(PortPinArrayP108); // setup LED1 pin
blinkLed0501(PortPinArrayP108); // blink LED1

// *** Testing 7 segment LED ***
displayAllDigits050(WhutBoardSevenSegmentLedPortPinArrayPointerArray, MAX_WHUT_7_SEG_LED_PIN_NUMBER);
}



void testOlimex7segmentLed0201()
{
    // *** system self diagnostic test - blink LED P09 ***
setupGpioPinOutputLow050(PortPinArrayP03); // setup GPIO pin as output
blinkLed0501(PortPinArrayP03); // blink led

// *** Testing 7 segment LED ***
displayAllDigits050(OlimexBoardSevenSegmentLedPortPinArrayPointerArray2, MAX_OLIMEX_7_SEG_LED_PIN_NUMBER);
}

// ***********************************************************************
// End
// ***********************************************************************


// ***********************************************************************
// spi050.h 2013oct26hk1611
// ***********************************************************************

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

#ifndef SPI_HEADER_SEEN
#define SPI_HEADER_SEEN

// *** SPI Functions ***

#define SPI_CHANNEL_0 0
#define SPI_CHANNEL_1 1

void setupSpi051(int spiChannelNumber)
{
if (spiChannelNumber == 0)
{
// 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_PIO0_6, DISABLE); // Select P06, disable

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

// Setup Ssel0
setupGpioPinOutputLow050(PortPinArraySsel0);
}
else // (if spiChannelNumber == 1)
{
// 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_SetSPI1ClockDiv(10);

// No need to assign GPIO pins for SPI1, just disable
SSP_SSP1PinsInit(DISABLE); // disable SSEL

// IOCON_SetPinFunc(IOCON_PIO2_2, PIO2_2_FUN_MISO1);
// IOCON_SetPinFunc(IOCON_PIO2_3, PIO2_3_FUN_PIO_MOSI1);
// IOCON_SetPinFunc(IOCON_PIO2_1, PIO2_1_FUN_SCK1);
// if(useSSEL == ENABLE) {
        //    IOCON_SetPinFunc(IOCON_PIO2_0, PIO2_0_FUN_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 SSP1 peripheral
SSP_Cmd(LPC_SSP1, ENABLE);

// Setup Ssel1
setupGpioPinOutputLow050(PortPinArraySsel1);
}
}

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 SpiWriteRead050(SSP_DATA_SETUP_Type xferConfig, int spiChannelNumber)
{
if (spiChannelNumber == 0)
{
setGpioDataPinLow01(PortPinArraySsel0);
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(PortPinArraySsel0);
}
else // (spiChannelNumber == 1)
{
setGpioDataPinLow01(PortPinArraySsel1);
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(PortPinArraySsel1);
}
}

#endif /* SPI_HEADER_SEEN */

// ***********************************************************************
// End
// ***********************************************************************





No comments:

Post a Comment