2013-11-09

SPI slave select function development notes


// ***********************************************************************
// Program  - Fong EKG v5.01
// Function - Olimex EKG Board Evaluation
// Author   - TL Fong
// Build    - 2013.11.09.01
// Date     - 2013nov09hkt1420
// 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()
{
testBlinkLpc1114LedP03(); // testing system LED

// testAdc04(SPI_CHANNEL_1); // testing ADC
// testMax7219MatrixLed05(SPI_CHANNEL_0); // testing 7-segment/8x8 matrix LED

testLedArray060(SPI_CHANNEL_0, SLAVE_SELECT_0); // testing 7-segment/8x8 matrix LED
}

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



// ***********************************************************************
// test050.h 2013nov08hkt1038
// ***********************************************************************

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

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

void testLedArray060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
spiSetup060(spiChannelNumber, slaveSelectNumber);
testDisplayLedArray060(spiChannelNumber, slaveSelectNumber);
}

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



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

#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 SHUT_DOWN              0x00
#define NORMAL                 0x01
#define CODE_B_ALL_DIGITS      0xff
#define NO_DECODE_ALL_DIGITS   0x00
#define DECODE_ALL_8_DIGITS    0xff
#define NO_DECODE_ALL_8_DIGITS 0x00
#define NO_DECODE_ALL_8_ROWS   0x00
#define DISPLAY_8_DIGITS       0x07
#define SCAN_ALL_8_DIGITS      0x07
#define SCAN_ALL_8_ROWS        0x07

// *** Data Constants ***
#define DEVICE_1 1
#define DEVICE_2 2
#define DEVICE_3 3
#define DEVICE_4 4

#define LED_DRIVER_1 1
#define LED_DRIVER_2 2
#define LED_DRIVER_3 3
#define LED_DRIVER_4 4

#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
#define CHAR_HYPHEN 10
#define CHAR_E 11
#define CHAR_H 12
#define CHAR_L 13
#define CHAR_P 14
#define CHAR_BLANK 15

#define PATTERN_ALL_ON 0xff
#define PATTERN_ALL_OFF 0x00

#define ADDRESS_INDEX 0
#define DATA_INDEX 1

#define MAX_DEVICE_NUMBER 16
#define MAX_BUFFER_SIZE 32
#define MAX_MATRIX_NUMBER 4

uint8_t char_A[5] = {0x7E, 0x11, 0x11, 0x11, 0x7E};
uint8_t char_K[5] = {0x41, 0x22, 0x14, 0x08, 0x7f};
uint8_t char_O[5] = {0x3E, 0x41, 0x41, 0x41, 0x3E};

uint8_t CharBigO[8] = {0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00, 0x00, 0x00};
uint8_t CharBigK[8] = {0x41, 0x22, 0x14, 0x08, 0x7F, 0x00, 0x00, 0x00};

uint8_t TriGram0[8] = {0x54, 0x54, 0x54, 0x00, 0x54, 0x54, 0x54, 0x00};


// *** Version 6 2013nov09 ***

void testDisplayLedArray060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
printf("\n\n*** Display LED Array 060 - 2013nov09hkt1646 ***\n\n");

setupLedDriver060(spiChannelNumber, slaveSelectNumber); // drivers 1, 2 for matrix, 3, 4 for 7-segment

    displayLedArray060(spiChannelNumber, slaveSelectNumber, LED_DRIVER_1, CharBigK);
    displayLedArray060(spiChannelNumber, slaveSelectNumber, LED_DRIVER_2, CharBigO);

    display8LedDigit060(spiChannelNumber, slaveSelectNumber, LED_DRIVER_3, 1, 2, 3, 4, 5, 6, 7, 8);
    display8LedDigit060(spiChannelNumber, slaveSelectNumber, LED_DRIVER_4, 8, 7, 6, 5, 4, 3, 2, 1);

    delayTenthSecond(10);
    // display8LedDigit060(spiChannelNumber, slaveSelectNumber, LED_DRIVER_3, 0, 0, 0, 0, 2, 2, 2, 2);
    display8LedDigit060(spiChannelNumber, slaveSelectNumber, LED_DRIVER_3, 0, 1, 0, 1, 0, 2, 0, 2);
}

void setupLedDriver060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
   uint8_t i;

   for (i = 1; i < 3; i++)
   {
  writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, i, DECODE_MODE_ADDR, NO_DECODE_ALL_8_ROWS);
  writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, i, SCAN_LIMIT_ADDR, SCAN_ALL_8_ROWS);
       writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, i, OPERATION_ADDR, SHUT_DOWN);
   }

   for (i = 3; i < 5; i++)
   {
  writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, i, DECODE_MODE_ADDR, DECODE_ALL_8_DIGITS);
  writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, i, SCAN_LIMIT_ADDR, SCAN_ALL_8_DIGITS);
       writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, i, OPERATION_ADDR, SHUT_DOWN);
   }
}

void displayLedArray060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, \
               uint8_t ledDriverNumber, uint8_t matrixPattern[8])
{
shutDownLedDriver(spiChannelNumber, ledDriverNumber);

uint8_t columnNumber;
uint8_t digitAddress;

for (columnNumber = 0; columnNumber < 8; columnNumber++)
{
digitAddress = columnNumber + 1;
        displayOneDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, \
                  digitAddress,  matrixPattern[7 - columnNumber]);
}

startUpLedDriver(spiChannelNumber, ledDriverNumber);
}

void displayOneDigit060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, \
               uint8_t deviceNumber, uint8_t digitAddress, uint8_t digitData)
{
writeLedDriverCommand060(spiChannelNumber, slaveSelectNumber, deviceNumber, digitAddress, digitData);
}

void writeLedDriverCommand060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, \
                     uint8_t deviceNumber, uint8_t commandRegisterAddress, uint8_t command)
{
uint8_t txBuffer[MAX_BUFFER_SIZE];
uint8_t rxBuffer[MAX_BUFFER_SIZE];

SSP_DATA_SETUP_Type xferConfig;
    xferConfig.tx_data = txBuffer;
    xferConfig.rx_data = rxBuffer;
    xferConfig.length =  deviceNumber * 2;

    txBuffer[ADDRESS_INDEX] = commandRegisterAddress;
txBuffer[DATA_INDEX] = command;

    int i;
    for (i = 1; i < deviceNumber; i++)
    {
        txBuffer[(i * 2) + ADDRESS_INDEX] = NO_OP_ADDR;
}

    SpiWriteRead050(xferConfig, spiChannelNumber);
}

void display8LedDigit060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, uint8_t ledDriverNumber, \
           uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4, \
           uint8_t digit5, uint8_t digit6, uint8_t digit7, uint8_t digit8)
{
shutDownLedDriver(spiChannelNumber, ledDriverNumber);
displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_7, digit1);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_6, digit2);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_5, digit3);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_4, digit4);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_3, digit5);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_2, digit6);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_1, digit7);
    displayOneDigit04(spiChannelNumber, ledDriverNumber, DIGIT_ADDR_0, digit8);
    startUpLedDriver(spiChannelNumber, ledDriverNumber);
}




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

#define SLAVE_SELECT_0 0
#define SLAVE_SELECT_1 1

// *** version 060 ***

// ***************************************************************************
// Function - Setup SPI channel and slave select GPIO port
// Date     - 2013nov09
// Notes    - 1. Port P06 also selected as default, BUT DISABLED!!!
//            2. There is a bug in lpc11xx_iocon.c v1.0
//            3. SlaveSelect GPIO port is initialized to output Low
//            4. portPinArray PortPinArraySsel0 = {PORT0, PIN2}; // SSEL0
//               portPinArray PortPinArraySsel1 = {PORT2, PIN0}; // SSEL1
// ***************************************************************************
void spiSetup060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber)
{
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, BUT 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);
}
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

// Note: A bug in Coocox lpc11xx_iocon.c v1.0 26Jan2010
// 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 Ssel0

// setupGpioPinOutputLow050(PortPinArraySsel0);

switch (spiChannelNumber)
{
case 0: // SPI channel 0
switch (slaveSelectNumber)
{
case 0: // slave select 0
setupGpioPinOutputLow050(PortPinArraySsel00);
case 1: // slave select 1
setupGpioPinOutputLow050(PortPinArraySsel01);
}

case 1: // SPI channel 1
switch (slaveSelectNumber)
{
case 0: // slave select 0
setupGpioPinOutputLow050(PortPinArraySsel10);
case 1: // slave select 1
setupGpioPinOutputLow050(PortPinArraySsel11);
}
}
}

// ***************************************************************************
// Function - SPI transmit
// Date     - 2013nov09
// Notes    - 1. SlaveSlave Port P06 selected as default, BUT DISABLED!!!
//            2. SSEL0 default is P02, SSEL1 default is P20
// ***************************************************************************
void spiTransmit060(uint8_t spiChannelNumber, portPinArray slaveSelectPortPinArray, \
           SSP_DATA_SETUP_Type xferConfig)
{
setGpioDataPinLow01(slaveSelectPortPinArray);
if (spiChannelNumber == 0)
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
else // (spiChannelNumber == 1)
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);
setGpioDataPinHigh01(slaveSelectPortPinArray);
}


.END

No comments:

Post a Comment