Fong Blinky 2013aug0304 debugging notes

The second statement works for Led1, but not Led2.   Need to debug.

initializeFongBoard01();
testFongBoardBlinky01(Led1, FourTimes);


// ***********************************************************************
// Program - FongBlinky
// Description - Testing LPC1114FN28/102
// Author - TL Fong <fongmcu.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.08.03.04
// Date - 2013aug03hkt2151
// Hardware - NXP ARM Cortex M0 LPC1114FN28/102, LPC1114FBD48/302,   
// Software - MDK-Lite/uVision 4.71, Flash Magic v7.51, CIMSIS 1.1
// ***********************************************************************

#include <stdio.h>
#include "lpc11xx.h"

// ***********************************************************************
// GPIO port/pin number definition and declaration
// ***********************************************************************

// *** Port and pin numbers ***

typedef const int portNumber;

portNumber Port0 = 0;
portNumber Port1 = 1;
portNumber Port2 = 2;
portNumber Port3 = 3;
portNumber Port4 = 4;

typedef const int pinNumber;

pinNumber Pin0 = 0;
pinNumber Pin1 = 1;
pinNumber Pin2 = 2;
pinNumber Pin3 = 3;
pinNumber Pin4 = 4;
pinNumber Pin5 = 5;
pinNumber Pin6 = 6;
pinNumber Pin7 = 7;
pinNumber Pin8 = 8;
pinNumber Pin9 = 9;
pinNumber Pin10 = 10;
pinNumber Pin11 = 11;

typedef const int ledNumber;

ledNumber Led1 = 1;
ledNumber Led2 = 2;
ledNumber Led3 = 3;
ledNumber Led4 = 4;

typedef const int keyNumber;

typedef int keyPortArrayArrayIndex;

keyNumber Key1 = 1;
keyNumber Key2 = 2;
keyNumber Key3 = 3;
keyNumber Key4 = 4;
keyNumber Key5 = 5;

// ***********************************************************************
// Fong Board and Xia Board configuration - last update 2013aug03
// ***********************************************************************

// *** Led and key port pin array array ***

typedef int ledPortPinArrayArray[4][2]; // maximum 4 LEDs, each with unique port/pin
typedef int keyPortPinArrayArray[5][2]; // maximum 5 keys

typedef int portPinArrayArrayIndex;

portPinArrayArrayIndex PortIndex = 0; // index to access port and pin numbers
portPinArrayArrayIndex PinIndex  = 1;

// * Fong Board has 2 LEDs and 2 keys *

int MaxFongLedNumber = 2;
int MaxFongKeyNumber = 2;

ledPortPinArrayArray FongLedPortPinArrayArray = {{Port1, Pin8}, {Port0, Pin2}};
keyPortPinArrayArray FongKeyPortPinArrayArray = {{Port1, Pin9}, {Port0, Pin3}};

// * Xia Board has 4 LEDs and 5 keys *

ledPortPinArrayArray XiaLedPortPinArrayArray = {{Port1, Pin8}, {Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}};
int MaxXiaLedNumber = 4;

keyPortPinArrayArray XiaKeyPortPinArrayArray = {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}};
int MaxXiaKeyNumber = 5;

// *** Mpu Board Struct ***

typedef struct
{
ledPortPinArrayArray ledPortPinArrayArray[4][2];
keyPortPinArrayArray keyPortPinArrayArray[5][2];
} mcuBoardStruct;

// * Fong Board Struct *

mcuBoardStruct FongBoardStruct = {{{Port1, Pin8},{Port0, Pin2}, {Port1, Pin8}, {Port0, Pin2}},
                            {{Port1, Pin9},{Port0, Pin3}, {Port1, Pin9}, {Port1, Pin9}, {Port1, Pin9}}};

mcuBoardStruct *FongBoard = &FongBoardStruct;


// * Xia Board Struct *

mcuBoardStruct XiaBoardStruct = {{{Port1, Pin8},{Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}},
                           {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}}};

mcuBoardStruct *XiaBoard = &XiaBoardStruct;
 

// ***********************************************************************
// GPIO port/pin set input/output and read/write data functions 
// ***********************************************************************

// * typedefs and #defines *

typedef int directionValue;

directionValue InputDirection = 0;
directionValue OutputDirection = 1;

typedef int logicLevelValue;

logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;

#define LOW 0
#define HIGH 1

// * GPIO port/pin control/data structure pointers array *

// # LPC11xx has 4 control/data structures to handle 4 GPIO ports.
// # The pointers of the 4 structures are stored in a 4 element array.

typedef LPC_GPIO_TypeDef *gpioStructPointerArray[4];
gpioStructPointerArray GpioStructPointerArray = {LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3};

// ***********************************************************************
// Name   - getGpioStructPointer
// Input  - port number
// Output - GPIO control/data structure pointer
// ***********************************************************************

LPC_GPIO_TypeDef *getGpioStructPointer(int portNumber)
{
  LPC_GPIO_TypeDef *gpioStructPointer;
gpioStructPointer =  GpioStructPointerArray[portNumber]; 
return gpioStructPointer;
}

// ***********************************************************************
// Name   - setGpioDirectionRegisterBitInput
// Input  - port control structure, pin number
// Output - set pin as input
// ***********************************************************************

void setGpioDirectionRegisterBitInput(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR  &= ~(1 << bitNumber);
}

// ***********************************************************************
// Name   - setGpioDirectionRegisterBitOutput
// Input  - port control structure, pin number
// Output - set pin as output
// ***********************************************************************

void setGpioDirectionRegisterBitOutput(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR |= (1 << bitNumber);
}




void setGpioDataRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA &= ~(1 << bitNumber);
}

void setGpioDataRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA |= (1 << bitNumber);
}



void setPortPinDirection(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, directionValue directionValue)
  {
  if (directionValue == InputDirection)
 setGpioDirectionRegisterBitInput(gpio_struct_ptr, pinNumber);
else
setGpioDirectionRegisterBitOutput(gpio_struct_ptr, pinNumber);
  }

void setPortPinData(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, logicLevelValue logicLevelValue)
{
  if (logicLevelValue == LogicLevelLow)
setGpioDataRegisterBitLow(gpio_struct_ptr, pinNumber);        
else 
setGpioDataRegisterBitHigh(gpio_struct_ptr, pinNumber);  
}

logicLevelValue getGpioDataRegisterBitValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitPosition)
{
logicLevelValue logicLevelValue;

const int GpioDataRegisterMask = (1 << bitPosition);

if ((gpio_struct_ptr->DATA  &= GpioDataRegisterMask) != 0) 
logicLevelValue = LogicLevelHigh;
else
logicLevelValue = LogicLevelLow;

return logicLevelValue;
}




// ***********************************************************************
// LED GPIO functions 
// ***********************************************************************

portNumber getLedPortNumber01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int portNumber;
  portNumber = ledPortPinArrayArray [ledNumber - 1][PortIndex];
  return portNumber;
}

pinNumber getLedPinNumber01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int ledPinNumber;
  ledPinNumber = ledPortPinArrayArray [ledNumber - 1][PinIndex];
  return ledPinNumber;
}

portNumber getLedPortNumber02(ledNumber ledNumber, mcuBoardStruct *mcuBoardPtr)
{
  int portNumber;
  portNumber = (int)(mcuBoardPtr->ledPortPinArrayArray [ledNumber - 1][PortIndex]);
  return portNumber;
}

pinNumber getLedPinNumber02(ledNumber ledNumber, mcuBoardStruct *mcuBoardPtr)
{
  int ledPinNumber;
  ledPinNumber = (int)(mcuBoardPtr->ledPortPinArrayArray [ledNumber - 1][PinIndex]);
  return ledPinNumber;
}


// ***********************************************************************
// Key GPIO functions 
// ***********************************************************************

portNumber getKeyPortNumber01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int portNumber;
  portNumber = keyPortPinArrayArray [keyNumber - 1][PortIndex];
  return portNumber;
}

pinNumber getKeyPinNumber01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int keyPinNumber;
  keyPinNumber = keyPortPinArrayArray [keyNumber - 1][PinIndex];
  return keyPinNumber;
}

typedef logicLevelValue keyStatus;
keyStatus KeyPressed = LOW;
keyStatus KeyReleased = HIGH;

keyStatus getKeyStatus(keyPortPinArrayArray keyPortPinArrayArray, keyNumber keyNumber)
{
keyStatus keyStatus;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber01(keyNumber, keyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber01(keyNumber, keyPortPinArrayArray);
 
keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);
  return keyStatus;
}

// ***********************************************************************
// GPIO, Timer, and IOCON control struct pointer arrays
// ***********************************************************************

typedef LPC_TMR_TypeDef *timerStructPointerArray[4];
timerStructPointerArray TimerStructPointerArray = {LPC_TMR16B0, LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1};

typedef LPC_IOCON_TypeDef *lpcIoCon;
lpcIoCon LpcIoCon = LPC_IOCON;

// *** Timer config and enable functions ***

void setPinFunction()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}

// *** Timer enable bit array ***

int TimerEnableBitArray[4] = {7, 8, 9, 10}; // SYSAHBCLKCTRL timer enable bits


// ***********************************************************************
// Timer type definition and declaration
// ***********************************************************************

typedef const int timerNumber;
timerNumber Timer16Bit0 = 0;
timerNumber Timer16Bit1 = 1;
timerNumber Timer32Bit0 = 2;
timerNumber Timer32Bit1 = 3;

timerNumber Timer1 = Timer16Bit0;
timerNumber Timer2 = Timer16Bit1;
timerNumber Timer3 = Timer32Bit0;
timerNumber Timer4 = Timer32Bit1;

int const TimerNumberMax = 4;

// ***********************************************************************
// Timer functions
// ***********************************************************************

LPC_TMR_TypeDef *getTimerStructPointer(int timerNumber)
{
  LPC_TMR_TypeDef *timerStructPointer;
timerStructPointer =  TimerStructPointerArray[timerNumber]; 
return timerStructPointer;
}

int getTimerEnableBit(int timerNumber)
{
int timerEnableBit;

timerEnableBit = TimerEnableBitArray[timerNumber];
return timerEnableBit;
}

void enableTimer(int timerNumber)
{
int timerEnableBit;
timerEnableBit = getTimerEnableBit(timerNumber);
LPC_SYSCON ->SYSAHBCLKCTRL |= (1 <<timerEnableBit);
}

// ***********************************************************************
// Delay Functions
// ***********************************************************************

// *** delay using looping ***

void delayTenthSecond(int count)
  {
int i, j, k;
for (i = 0; i < count; i++)
    {
for (j = 0; j < 0x04; j++)
 {
        for (k = 0; k < 0x000BB00; k++)
          {
       }
}
     }
}

// *** delay using Systick ***

static volatile uint32_t TimeTick = 0;

void SysTick_Handler(void)
{
       TimeTick++;
}

void delayMilliSecond(uint32_t mS)
{
  SysTick->LOAD = (((24000)*mS)-1);
  SysTick->VAL = 0;
  SysTick->CTRL |= ((1<<1)|(1<<0));
       
  while(!TimeTick)
  {
  };
  TimeTick = 0;
  SysTick->CTRL =0;
}


// *** Timer constants ***

typedef int milliSecond;
milliSecond OnQuarterSecond  =  250;
milliSecond OnHalfSecond     =  500;
milliSecond OffQuarterSecond =  250;
milliSecond OffHalfSecond    =  500;
milliSecond OffOneSecond     = 1000;

typedef int tenthSecond;
/*
tenthSecond OnHalfSecond = 5;
tenthSecond OnOneSecond = 10;

tenthSecond OffHalfSecond = 5;
tenthSecond OffOneSecond = 10;

tenthSecond FourSeconds = 40;
*/

// *** Loop constants ***

typedef int repeatCount;
repeatCount TwoTimes   =   2;
repeatCount FourTimes   =  4;
repeatCount FiveTimes   =  5;
repeatCount SixTimes    =  6;
repeatCount EightTimes  =  8;
repeatCount TenTimes    = 10;
repeatCount TwentyTimes = 20;

// ***********************************************************************
// PWM functions
// ***********************************************************************

void setupPwmPin()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}


// ***********************************************************************
// Initialization functions
// ***********************************************************************

// * Led *

void initializeLedPortPin01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber01(ledNumber, ledPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber01(ledNumber, ledPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllLeds01(ledPortPinArrayArray ledPortPinArrayArray, int maxLedNumber)
{
  int ledNumber;

  for (ledNumber = 0; ledNumber < maxLedNumber; ledNumber++)
  {
 initializeLedPortPin01(ledNumber, ledPortPinArrayArray);
  }
}

// * Key *

void initializeKeyPortPin01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber01(keyNumber, keyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getKeyPinNumber01(keyNumber, keyPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, InputDirection);
}

void initializeAllKeys01(keyPortPinArrayArray keyPortPinArrayArray, int maxKeyNumber)
{
  int keyNumber;

  for (keyNumber = 0; keyNumber < maxKeyNumber; keyNumber++)
  {
 initializeLedPortPin01(keyNumber, keyPortPinArrayArray);
  }
}

// * Timer *

void initializeAllTimers01()
{
enableTimer(Timer1);
enableTimer(Timer2);
enableTimer(Timer3);
enableTimer(Timer4);
}

// * All *

void initializeFongBoard01()
{
initializeAllLeds01(FongLedPortPinArrayArray, MaxFongLedNumber);
initializeAllKeys01(FongKeyPortPinArrayArray, MaxFongKeyNumber);
initializeAllTimers01();
}

void initializeXiaBoard01()
{
initializeAllLeds01(XiaLedPortPinArrayArray, MaxFongLedNumber);
initializeAllKeys01(XiaKeyPortPinArrayArray, MaxFongKeyNumber);
initializeAllTimers01();
}


// ***********************************************************************
// LED test functions
// ***********************************************************************

void blinky01(ledPortPinArrayArray ledPortPinArrayArray, ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber01(ledNumber, ledPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber01(ledNumber, ledPortPinArrayArray);

  delayMilliSecond(500);

for (count = 0; count < blinkCount; count++)
{
  setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayMilliSecond(onTime);    
setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayMilliSecond(offTime);
  }
}

void blinky02(mcuBoardStruct *mcuBoardPtr, ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber02(ledNumber, mcuBoardPtr);

// portNumber = 1;

gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber02(ledNumber, mcuBoardPtr);

// pinNumber = 8;
 
for (count = 0; count < blinkCount; count++)
{
  setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayMilliSecond(onTime);    
setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayMilliSecond(offTime);
  }
}

// ***********************************************************************
// Key test functions 
// ***********************************************************************

void echoKey(keyPortPinArrayArray keyPortPinArrayArray, keyNumber keyNumber, ledPortPinArrayArray ledPortPinArrayArray, ledNumber ledNumber, int echoCount)
{
  int count;
keyStatus keyStatus;

for (count = 0; count < echoCount; count++)
{
 keyStatus = ~KeyPressed;
while (keyStatus == ~KeyPressed)
{
  keyStatus = getKeyStatus(keyPortPinArrayArray, keyNumber);

       if (keyStatus == KeyPressed)
  {
  blinky01(ledPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, FourTimes);
delayMilliSecond(1000); 
break;
  }
else
delayMilliSecond(100);   
    }
  }
}

// ***********************************************************************
// Sample test Functions
// ***********************************************************************

void testFongBoardBlinky01(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky01(FongLedPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testFongBoardBlinky02(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky02(FongBoard, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testFongBoardEchoKey01(keyNumber keyNumber, ledNumber ledNumber, repeatCount repeatCount) 
{
echoKey(FongKeyPortPinArrayArray, keyNumber, FongLedPortPinArrayArray, ledNumber, repeatCount);
}


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

int main()
{  
initializeFongBoard01();
testFongBoardBlinky01(Led1, FourTimes);
}

// ***********************************************************************
// End of Program
// ***********************************************************************

// .END 

USB to 3V3 TTL adapter testing notes
































I have tested my new RMB10 toys OK.  So they replace the bulky 12V RS232 cables and USB > 12V > 3V3 and COM 1 12V to 3V3 adapters which are requires external 3V3 power supply.

The set up is now neat and tidy, besides safe, because there are no more +/=12V signals to accidentally short circuit and burn things out.

.END








USB to TTL serial adapter testing notes






















Now I am testing yet another USB to TTL serial adapter.

.END

LPCXpresso5 playing notes












Now I have started playing with the free lpcxpresso5 IDE with 128k debug limit.

.END

Fong Blinky 2013aug0301 testing notes

// ***********************************************************************
// Program - FongBlinky
// Description - Testing LPC1114FN28/102
// Author - TL Fong <fongmcu.blogspot.hk> <tlfong01hongkong@google.com>
// Build - 2013.08.03.02
// Date - 2013aug03hkt1133
// Hardware - NXP ARM Cortex M0 LPC1114FN28/102, LPC1114FBD48/302,   
// Software - MDK-Lite/uVision 4.71, Flash Magic v7.51, CIMSIS 1.1
// ***********************************************************************

#include <stdio.h>
#include "lpc11xx.h"


// ***********************************************************************
// GPIO port/pin number definition and declaration
// ***********************************************************************

// *** Port and pin numbers ***

typedef const int portNumber;

portNumber Port0 = 0;
portNumber Port1 = 1;
portNumber Port2 = 2;
portNumber Port3 = 3;
portNumber Port4 = 4;

typedef const int pinNumber;

pinNumber Pin0 = 0;
pinNumber Pin1 = 1;
pinNumber Pin2 = 2;
pinNumber Pin3 = 3;
pinNumber Pin4 = 4;
pinNumber Pin5 = 5;
pinNumber Pin6 = 6;
pinNumber Pin7 = 7;
pinNumber Pin8 = 8;
pinNumber Pin9 = 9;
pinNumber Pin10 = 10;
pinNumber Pin11 = 11;

typedef const int ledNumber;

ledNumber Led1 = 1;
ledNumber Led2 = 2;
ledNumber Led3 = 3;
ledNumber Led4 = 4;

typedef const int keyNumber;

typedef int keyPortArrayArrayIndex;

keyNumber Key1 = 1;
keyNumber Key2 = 2;
keyNumber Key3 = 3;
keyNumber Key4 = 4;
keyNumber Key5 = 5;

// ***********************************************************************
// Fong Board and Xia Board configuration - last update 2013aug03
// ***********************************************************************

// *** Led and key port pin array array ***

typedef int ledPortPinArrayArray[4][2]; // maximum 4 LEDs, each with unique port/pin
typedef int keyPortPinArrayArray[5][2]; // maximum 5 keys

typedef int portPinArrayArrayIndex;

portPinArrayArrayIndex PortIndex = 0; // index to access port and pin numbers
portPinArrayArrayIndex PinIndex  = 1;

// * Fong Board has 2 LEDs and 2 keys *

int MaxFongLedNumber = 2;
int MaxFongKeyNumber = 2;

ledPortPinArrayArray FongLedPortPinArrayArray = {{Port1, Pin8}, {Port0, Pin2}};
keyPortPinArrayArray FongKeyPortPinArrayArray = {{Port1, Pin9}, {Port0, Pin3}};

// * Xia Board has 4 LEDs and 5 keys *

ledPortPinArrayArray XiaLedPortPinArrayArray = {{Port1, Pin8}, {Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}};
int MaxXiaLedNumber = 4;

keyPortPinArrayArray XiaKeyPortPinArrayArray = {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}};
int MaxXiaKeyNumber = 5;

// *** Mpu Board Struct ***

typedef struct
{
ledPortPinArrayArray ledPortPinArrayArray[4][2];
keyPortPinArrayArray keyPortPinArrayArray[5][2];
} mcuBoardStruct;

// * Fong Board Struct *

mcuBoardStruct FongBoardStruct = {{{Port1, Pin8},{Port0, Pin2}, {Port1, Pin8}, {Port0, Pin2}},
                            {{Port1, Pin9},{Port0, Pin3}, {Port1, Pin9}, {Port1, Pin9}, {Port1, Pin9}}};

mcuBoardStruct *FongBoard = &FongBoardStruct;


// * Xia Board Struct *

mcuBoardStruct XiaBoardStruct = {{{Port1, Pin8},{Port2, Pin7}, {Port2, Pin8}, {Port2, Pin5}},
                           {{Port0, Pin2},{Port0, Pin3}, {Port3, Pin4}, {Port1, Pin9}, {Port1, Pin4}}};

mcuBoardStruct *XiaBoard = &XiaBoardStruct;
 

// ***********************************************************************
// GPIO port/pin set input/output and read/write data functions 
// ***********************************************************************

// * typedefs and #defines *

typedef int directionValue;

directionValue InputDirection = 0;
directionValue OutputDirection = 1;

typedef int logicLevelValue;

logicLevelValue LogicLevelLow = 0;
logicLevelValue LogicLevelHigh = 1;

#define LOW 0
#define HIGH 1

// * GPIO port/pin control/data structure pointers array *

// # LPC11xx has 4 control/data structures to handle 4 GPIO ports.
// # The pointers of the 4 structures are stored in a 4 element array.

typedef LPC_GPIO_TypeDef *gpioStructPointerArray[4];
gpioStructPointerArray GpioStructPointerArray = {LPC_GPIO0, LPC_GPIO1, LPC_GPIO2, LPC_GPIO3};

// ***********************************************************************
// Name   - getGpioStructPointer
// Input  - port number
// Output - GPIO control/data structure pointer
// ***********************************************************************

LPC_GPIO_TypeDef *getGpioStructPointer(int portNumber)
{
  LPC_GPIO_TypeDef *gpioStructPointer;
gpioStructPointer =  GpioStructPointerArray[portNumber]; 
return gpioStructPointer;
}

// ***********************************************************************
// Name   - setGpioDirectionRegisterBitInput
// Input  - port control structure, pin number
// Output - set pin as input
// ***********************************************************************

void setGpioDirectionRegisterBitInput(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR  &= ~(1 << bitNumber);
}

// ***********************************************************************
// Name   - setGpioDirectionRegisterBitOutput
// Input  - port control structure, pin number
// Output - set pin as output
// ***********************************************************************

void setGpioDirectionRegisterBitOutput(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DIR |= (1 << bitNumber);
}




void setGpioDataRegisterBitLow(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA &= ~(1 << bitNumber);
}

void setGpioDataRegisterBitHigh(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitNumber)
{
gpio_struct_ptr->DATA |= (1 << bitNumber);
}



void setPortPinDirection(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, directionValue directionValue)
  {
  if (directionValue == InputDirection)
 setGpioDirectionRegisterBitInput(gpio_struct_ptr, pinNumber);
else
setGpioDirectionRegisterBitOutput(gpio_struct_ptr, pinNumber);
  }

void setPortPinData(LPC_GPIO_TypeDef *gpio_struct_ptr, int pinNumber, logicLevelValue logicLevelValue)
{
  if (logicLevelValue == LogicLevelLow)
setGpioDataRegisterBitLow(gpio_struct_ptr, pinNumber);        
else 
setGpioDataRegisterBitHigh(gpio_struct_ptr, pinNumber);  
}

logicLevelValue getGpioDataRegisterBitValue(LPC_GPIO_TypeDef *gpio_struct_ptr, int bitPosition)
{
logicLevelValue logicLevelValue;

const int GpioDataRegisterMask = (1 << bitPosition);

if ((gpio_struct_ptr->DATA  &= GpioDataRegisterMask) != 0) 
logicLevelValue = LogicLevelHigh;
else
logicLevelValue = LogicLevelLow;

return logicLevelValue;
}




// ***********************************************************************
// LED GPIO functions 
// ***********************************************************************

portNumber getLedPortNumber01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int portNumber;
  portNumber = ledPortPinArrayArray [ledNumber - 1][PortIndex];
  return portNumber;
}

pinNumber getLedPinNumber01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
  int ledPinNumber;
  ledPinNumber = ledPortPinArrayArray [ledNumber - 1][PinIndex];
  return ledPinNumber;
}

portNumber getLedPortNumber02(ledNumber ledNumber, mcuBoardStruct *mcuBoardPtr)
{
  int portNumber;
  portNumber = (int)(mcuBoardPtr->ledPortPinArrayArray [ledNumber - 1][PortIndex]);
  return portNumber;
}

pinNumber getLedPinNumber02(ledNumber ledNumber, mcuBoardStruct *mcuBoardPtr)
{
  int ledPinNumber;
  ledPinNumber = (int)(mcuBoardPtr->ledPortPinArrayArray [ledNumber - 1][PinIndex]);
  return ledPinNumber;
}


// ***********************************************************************
// Key GPIO functions 
// ***********************************************************************

portNumber getKeyPortNumber01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int portNumber;
  portNumber = keyPortPinArrayArray [keyNumber - 1][PortIndex];
  return portNumber;
}

pinNumber getKeyPinNumber01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
  int keyPinNumber;
  keyPinNumber = keyPortPinArrayArray [keyNumber - 1][PinIndex];
  return keyPinNumber;
}

typedef logicLevelValue keyStatus;
keyStatus KeyPressed = LOW;
keyStatus KeyReleased = HIGH;

keyStatus getKeyStatus(keyPortPinArrayArray keyPortPinArrayArray, keyNumber keyNumber)
{
keyStatus keyStatus;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber01(keyNumber, keyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);
pinNumber = getKeyPinNumber01(keyNumber, keyPortPinArrayArray);
 
keyStatus = getGpioDataRegisterBitValue(gpio_struct_ptr, pinNumber);
  return keyStatus;
}

// ***********************************************************************
// GPIO, Timer, and IOCON control struct pointer arrays
// ***********************************************************************

typedef LPC_TMR_TypeDef *timerStructPointerArray[4];
timerStructPointerArray TimerStructPointerArray = {LPC_TMR16B0, LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1};

typedef LPC_IOCON_TypeDef *lpcIoCon;
lpcIoCon LpcIoCon = LPC_IOCON;

// *** Timer config and enable functions ***

void setPinFunction()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}

// *** Timer enable bit array ***

int TimerEnableBitArray[4] = {7, 8, 9, 10}; // SYSAHBCLKCTRL timer enable bits


// ***********************************************************************
// Timer type definition and declaration
// ***********************************************************************

typedef const int timerNumber;
timerNumber Timer16Bit0 = 0;
timerNumber Timer16Bit1 = 1;
timerNumber Timer32Bit0 = 2;
timerNumber Timer32Bit1 = 3;

timerNumber Timer1 = Timer16Bit0;
timerNumber Timer2 = Timer16Bit1;
timerNumber Timer3 = Timer32Bit0;
timerNumber Timer4 = Timer32Bit1;

int const TimerNumberMax = 4;

// ***********************************************************************
// Timer functions
// ***********************************************************************

LPC_TMR_TypeDef *getTimerStructPointer(int timerNumber)
{
  LPC_TMR_TypeDef *timerStructPointer;
timerStructPointer =  TimerStructPointerArray[timerNumber]; 
return timerStructPointer;
}

int getTimerEnableBit(int timerNumber)
{
int timerEnableBit;

timerEnableBit = TimerEnableBitArray[timerNumber];
return timerEnableBit;
}

void enableTimer(int timerNumber)
{
int timerEnableBit;
timerEnableBit = getTimerEnableBit(timerNumber);
LPC_SYSCON ->SYSAHBCLKCTRL |= (1 <<timerEnableBit);
}

// ***********************************************************************
// Delay Functions
// ***********************************************************************

// *** delay using looping ***

void delayTenthSecond(int count)
  {
int i, j, k;
for (i = 0; i < count; i++)
    {
for (j = 0; j < 0x04; j++)
 {
        for (k = 0; k < 0x000BB00; k++)
          {
       }
}
     }
}

// *** delay using Systick ***

static volatile uint32_t TimeTick = 0;

void SysTick_Handler(void)
{
       TimeTick++;
}

void delayMilliSecond(uint32_t mS)
{
  SysTick->LOAD = (((24000)*mS)-1);
  SysTick->VAL = 0;
  SysTick->CTRL |= ((1<<1)|(1<<0));
       
  while(!TimeTick)
  {
  };
  TimeTick = 0;
  SysTick->CTRL =0;
}


// *** Timer constants ***

typedef int milliSecond;
milliSecond OnQuarterSecond  =  250;
milliSecond OnHalfSecond     =  500;
milliSecond OffQuarterSecond =  250;
milliSecond OffHalfSecond    =  500;
milliSecond OffOneSecond     = 1000;

typedef int tenthSecond;
/*
tenthSecond OnHalfSecond = 5;
tenthSecond OnOneSecond = 10;

tenthSecond OffHalfSecond = 5;
tenthSecond OffOneSecond = 10;

tenthSecond FourSeconds = 40;
*/

// *** Loop constants ***

typedef int repeatCount;
repeatCount TwoTimes   =   2;
repeatCount FourTimes   =  4;
repeatCount FiveTimes   =  5;
repeatCount SixTimes    =  6;
repeatCount EightTimes  =  8;
repeatCount TenTimes    = 10;
repeatCount TwentyTimes = 20;

// ***********************************************************************
// PWM functions
// ***********************************************************************

void setupPwmPin()
{
LPC_IOCON ->PIO1_9 |= (1 << 0); // PIN1_9 = CT16B1_MAT0
}


// ***********************************************************************
// Initialization functions
// ***********************************************************************

// * Led *

void initializeLedPortPin01(ledNumber ledNumber, ledPortPinArrayArray ledPortPinArrayArray)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber01(ledNumber, ledPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber01(ledNumber, ledPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, OutputDirection);
}

void initializeAllLeds01(ledPortPinArrayArray ledPortPinArrayArray, int maxLedNumber)
{
  int ledNumber;

  for (ledNumber = 0; ledNumber < maxLedNumber; ledNumber++)
  {
 initializeLedPortPin01(ledNumber, ledPortPinArrayArray);
  }
}

// * Key *

void initializeKeyPortPin01(keyNumber keyNumber, keyPortPinArrayArray keyPortPinArrayArray)
{
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getKeyPortNumber01(keyNumber, keyPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getKeyPinNumber01(keyNumber, keyPortPinArrayArray);

setPortPinDirection(gpio_struct_ptr, pinNumber, InputDirection);
}

void initializeAllKeys01(keyPortPinArrayArray keyPortPinArrayArray, int maxKeyNumber)
{
  int keyNumber;

  for (keyNumber = 0; keyNumber < maxKeyNumber; keyNumber++)
  {
 initializeLedPortPin01(keyNumber, keyPortPinArrayArray);
  }
}

// * Timer *

void initializeAllTimers01()
{
enableTimer(Timer1);
enableTimer(Timer2);
enableTimer(Timer3);
enableTimer(Timer4);
}

// * All *

void initializeFongBoard01()
{
initializeAllLeds01(FongLedPortPinArrayArray, MaxFongLedNumber);
initializeAllKeys01(FongKeyPortPinArrayArray, MaxFongKeyNumber);
initializeAllTimers01();
}

void initializeXiaBoard01()
{
initializeAllLeds01(XiaLedPortPinArrayArray, MaxFongLedNumber);
initializeAllKeys01(XiaKeyPortPinArrayArray, MaxFongKeyNumber);
initializeAllTimers01();
}


// ***********************************************************************
// LED test functions
// ***********************************************************************

void blinky01(ledPortPinArrayArray ledPortPinArrayArray, ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber01(ledNumber, ledPortPinArrayArray);
gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber01(ledNumber, ledPortPinArrayArray);

  delayMilliSecond(500);

for (count = 0; count < blinkCount; count++)
{
  setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayMilliSecond(onTime);    
setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayMilliSecond(offTime);
  }
}

void blinky02(mcuBoardStruct *mcuBoardPtr, ledNumber ledNumber, int onTime, int offTime, int blinkCount)
{
  int count;
int portNumber;
int pinNumber;
LPC_GPIO_TypeDef *gpio_struct_ptr;

portNumber = getLedPortNumber02(ledNumber, mcuBoardPtr);

// portNumber = 1;

gpio_struct_ptr = getGpioStructPointer(portNumber);

pinNumber = getLedPinNumber02(ledNumber, mcuBoardPtr);

// pinNumber = 8;
 
for (count = 0; count < blinkCount; count++)
{
  setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelLow);
     delayMilliSecond(onTime);    
setPortPinData(gpio_struct_ptr, pinNumber, LogicLevelHigh);
     delayMilliSecond(offTime);
  }
}

// ***********************************************************************
// Key test functions 
// ***********************************************************************

void echoKey(keyPortPinArrayArray keyPortPinArrayArray, keyNumber keyNumber, ledPortPinArrayArray ledPortPinArrayArray, ledNumber ledNumber, int echoCount)
{
  int count;
keyStatus keyStatus;

for (count = 0; count < echoCount; count++)
{
 keyStatus = ~KeyPressed;
while (keyStatus == ~KeyPressed)
{
  keyStatus = getKeyStatus(keyPortPinArrayArray, keyNumber);

       if (keyStatus == KeyPressed)
  {
  blinky01(ledPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, FourTimes);
delayMilliSecond(1000); 
break;
  }
else
delayMilliSecond(100);   
    }
  }
}

// ***********************************************************************
// Sample test Functions
// ***********************************************************************

void testFongBoardBlinky01(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky01(FongLedPortPinArrayArray, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testFongBoardBlinky02(ledNumber ledNumber, repeatCount repeatCount) 
{
   blinky02(FongBoard, ledNumber, OnHalfSecond, OffOneSecond, repeatCount);
}

void testFongBoardEchoKey01(keyNumber keyNumber, ledNumber ledNumber, repeatCount repeatCount) 
{
echoKey(FongKeyPortPinArrayArray, keyNumber, FongLedPortPinArrayArray, ledNumber, repeatCount);
}


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

int main()
{  
initializeFongBoard01();

// testFongBoardBlinky01(Led1, SixTimes);

testFongBoardBlinky01(Led1, EightTimes);

  // testFongBoardBlinky01(Led2, SixTimes);

}

// ***********************************************************************
// End of Program
// ***********************************************************************

// .END