2013-11-09

spi slave selection function refactoring notes


// ***************************************************************************
// 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, uint8_t slaveSelectNumber, \
           SSP_DATA_SETUP_Type xferConfig)
{
setGpioDataPinLow01(PortPinArraySsel00);

if (spiChannelNumber == 0)
SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING);
else // (spiChannelNumber == 1)
SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_POLLING);

setGpioDataPinHigh01(PortPinArraySsel00);
}

// .END




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])
{
shutDownLedDriver060(spiChannelNumber, slaveSelectNumber, ledDriverNumber);

uint8_t columnNumber;
uint8_t digitAddress;

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

startUpLedDriver060(spiChannelNumber, slaveSelectNumber, ledDriverNumber);
}

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

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)
{
shutDownLedDriver060(spiChannelNumber, slaveSelectNumber, ledDriverNumber);
displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_7, digit1);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_6, digit2);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_5, digit3);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_4, digit4);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_3, digit5);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_2, digit6);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_1, digit7);
    displayLedDigit060(spiChannelNumber, slaveSelectNumber, ledDriverNumber, DIGIT_ADDR_0, digit8);
    startUpLedDriver060(spiChannelNumber, slaveSelectNumber, ledDriverNumber);
}

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;
}

    spiTransmit060(spiChannelNumber, slaveSelectNumber, xferConfig);
}

void setupDecodeMode060(uint8_t spiChannelNumber, uint8_t matrixNumber, uint8_t decodeMode)
{
    writeLedCommand02(matrixNumber, spiChannelNumber, DECODE_MODE_ADDR, decodeMode);
}

void setupScanLimit060(uint8_t spiChannelNumber, uint8_t matrixNumber, uint8_t scanLimit)
{
    writeLedCommand02(matrixNumber, spiChannelNumber, SCAN_LIMIT_ADDR, scanLimit);
}

void shutDownLedDriver060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, uint8_t driverNumber)
{
writeLedDriverCommand02(driverNumber, spiChannelNumber, OPERATION_ADDR, SHUT_DOWN);
}

void startUpLedDriver060(uint8_t spiChannelNumber, uint8_t slaveSelectNumber, uint8_t driverNumber)
{
writeLedDriverCommand02(driverNumber, spiChannelNumber, OPERATION_ADDR, NORMAL);
}

.END

No comments:

Post a Comment