Content originally posted in LPCWare by george.thaliath on Wed Nov 20 16:06:43 MST 2013
/*********************************************************************************/
Main
/*********************************************************************************/
start loop
Declare all the pins
check vor valid data on the UART
update PWM
Repeat update PWM if or not valid data (refresh rate is around 200Hz)
end loop
/*********************************************************************************/
config(delcaration of port pins and init)
/*********************************************************************************/
void TickHandler(void)
{
uint32_t i;
uint32_t millisec_increment = 1000 / SYS_TICK_RATE_HZ;
sys_millisec_cnt += millisec_increment;
if (sys_millisec_cnt >= 1000)
{
sys_millisec_cnt -= 1000;
sys_seconds_cnt++;
led_state ^= 1;
#ifndef PROTOTYPE_BOARD
if (led_state) {
LPC_GPIO0->DATA |= (1 << 2); /* Led on P0.2 off */
} else {
LPC_GPIO0->DATA &= ~(1 << 2); /* Led on P0.2 on */
}
#else
if (led_state) {
LPC_GPIO0->DATA |= (1 << 7); /* Led on P0.7 */
} else {
LPC_GPIO0->DATA &= ~(1 << 7); /* Led on P0.7 */
}
#endif
}
for (i=0; i<NR_OF_MSEC_COUNTERS; i++)
{
app_millisec_cnt += millisec_increment;
}
}
void bsp_init(void)
{
uint32_t i;
/* Init global variables */
led_state = 0;
sys_seconds_cnt = 0;
sys_millisec_cnt = 0;
for (i=0; i<NR_OF_MSEC_COUNTERS; i++)
{
app_millisec_cnt_avail = true;
}
/* Setup the system tick timer */
NVIC_SetPriority(SysTick_IRQn, SYSTICK_IRQ_PRIORITY);
SysTick_Config(LPC_CORE_CLOCKSPEED_HZ / SYS_TICK_RATE_HZ);
#ifdef SUPPORT_I2C_BUS
LPC_SYSCON->PRESETCTRL |= (1 << 1);
#endif
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 6) | /* Enable AHB clock to the GPIO domain */
#ifdef SUPPORT_I2C_BUS
(1 << 5) | /* Enable AHB clock to the I2C */
#endif
(1 << 7) | /* Enable AHB clock to the 16-bit counter/timer 0 */
(1 << 8) | /* Enable AHB clock to the 16-bit counter/timer 1 */
(1 << 9) | /* Enable AHB clock to the 32-bit counter/timer 0 */
(1 << 10)| /* Enable AHB clock to the 32-bit counter/timer 1 */
(1 << 12) | /* Enable AHB clock to the UART */
(1 << 15); /* Enable AHB clock to the WDT */
/* Configuring LED pin P0.3 (traffic LED) */
LPC_IOCON->PIO0_3 = 0xC0; // select PIO mode, no pull up/down, disable hysteresis
LPC_GPIO0->DIR |= (1 << 3); // set PIO pin P0.3 as output
#ifndef PROTOTYPE_BOARD
/* Configuring LED pin P0.2 (heartbeat LED) */
LPC_IOCON->PIO0_2 = 0xC0; // select PIO mode, no pull up/down, disable hysteresis
LPC_GPIO0->DIR |= (1 << 2); // set PIO pin P0.2 as output
/* Configure pin P0.7 for DMX bus data direction control */
LPC_IOCON->PIO0_7 = 0xC0; // select PIO mode, no pull up/down, disable hysteresis
LPC_GPIO0->DIR |= (1 << 7); // set PIO pin P0.7 as output
#else
/* Configuring LED pin P0.7 */
LPC_IOCON->PIO0_7 = 0xC0; // select PIO mode, no pull up/down, disable hysteresis
LPC_GPIO0->DIR |= (1 << 7); // set PIO pin P0.7 as output
/* Configure pin P2.6 for DMX bus data direction control */
// LPC_IOCON->PIO2_6 = 0xC0; // select PIO mode, no pull up/down, disable hysteresis
// LPC_GPIO2->DIR |= (1 << 6); // set PIO pin P2.6 as output
#endif
// bsp_recv_from_dmx_bus();
/* Configuring the DIP switches P2.1, P2.4, P3.5 and P0.6 */
LPC_IOCON->PIO2_1 = 0xC8; // select PIO mode, pull up, disable hysteresis DV
LPC_GPIO2->DIR &= ~(1 << 1); // set PIO pin P2.1 as input Bit-0
LPC_IOCON->PIO2_4 = 0xC8; // select PIO mode, pull up, disable hysteresis DV
LPC_GPIO2->DIR &= ~(1 << 4); // set PIO pin P2.4 as input Bit-1
LPC_IOCON->PIO3_5 = 0xC8; // select PIO mode, pull up, disable hysteresis DV
LPC_GPIO3->DIR &= ~(1 << 5); // set PIO pin P3.5 as input Bit-2
LPC_IOCON->PIO0_6 = 0xC8; // select PIO mode, pull up, disable hysteresis DV
LPC_GPIO0->DIR &= ~(1 << 6); // set PIO pin P0.6 as input Bit-3
// dmx_set_to_dmx_bus();
LPC_IOCON->PIO2_11 = 0xC0; // select PIO mode, pull up, disable hysteresis
LPC_GPIO2->DIR |= (1 << 11); // set PIO pin P2.11 as output X1
LPC_IOCON->PIO2_2 = 0xC0; // select PIO mode,pull up, disable hysteresis
LPC_GPIO2->DIR |= (1 << 2); // set PIO pin P2.2 as output X10
LPC_IOCON->PIO2_10 = 0xC0; // select PIO mode,pull up, disable hysteresis
LPC_GPIO2->DIR |= (1 << 10); // set PIO pin P2.10 as output X100
LPC_IOCON->PIO2_9 = 0xC0; // select PIO mode,pull up, disable hysteresis
LPC_GPIO2->DIR |= (1 << 9); // set PIO pin P2.9 as output Mode
// Analog_Channel_Set(1);
LPC_IOCON->PIO2_7 = 0xD0; // select PIO mode, pull up, disable hysteresis
LPC_GPIO2->DIR |= (1 << 7); // set PIO pin P2.7 as output S1
LPC_IOCON->PIO2_8 = 0xD0; // select PIO mode, pull up, disable hysteresis
LPC_GPIO2->DIR |= (1 << 8); // set PIO pin P2.8 as output S2
LPC_IOCON->PIO3_2 = 0xD0; // select PIO mode, pull up, disable hysteresis
LPC_GPIO3->DIR |= (1 << 2); // set PIO pin P3.2 as output S0
LPC_GPIO2->DATA |= (1 << 7);
LPC_GPIO2->DATA |= (1 << 8);
LPC_GPIO3->DATA |= (1 << 2);
//start of wL control pin and Wlink control
LPC_IOCON->PIO0_8 = 0xC0; // select PIO mode, pull up, disable hysteresis DV
LPC_GPIO0->DIR &= ~(1 << 8); // set PIO pin P2.1 as input Bit-0
LPC_GPIO0->DATA |= (1 << 8);
//end of WL control pin
//to be removed, this is been set for demo
LPC_IOCON->PIO1_11 = 0xC0;
LPC_GPIO1->DIR |= (1 << 11);
LPC_IOCON->PIO1_4 = 0xC0;
LPC_GPIO1->DIR |= (1 << 4);
/* Configure CT32B0 and CT32B1 match pins 0 and 1 */
LPC_IOCON-> PIO2_5 &= ~0x07;
LPC_IOCON-> PIO2_5 |= 0x01; // select CT32B0_MAT0
LPC_IOCON-> PIO2_6 &= ~0x07;
LPC_IOCON-> PIO2_6 |= 0x01;// select CT32B0_MAT1
LPC_IOCON->PIO0_1 = 0xE2; //CA; // select CT32B0_MAT2
Timer_PWM_Init(CT32B0);
LPC_IOCON->JTAG_TDO_PIO1_1 &= ~0x07;
LPC_IOCON->JTAG_TDO_PIO1_1 |= 0x03;/* Timer1_32 MAT0 */
LPC_IOCON->JTAG_nTRST_PIO1_2 &= ~0x07;
LPC_IOCON->JTAG_nTRST_PIO1_2 |= 0x03;/* Timer1_32 MAT1 */
LPC_IOCON->ARM_SWDIO_PIO1_3 &= ~0x07;
LPC_IOCON->ARM_SWDIO_PIO1_3 |= 0x03;/* Timer1_32 MAT2 */
Timer_PWM_Init(CT32B1);
dim_leds(0,0,0,0,0,0,0);
/* Configuring UART RXD pin */
LPC_IOCON->PIO1_6 = 0xD1; // select RXD mode, pull up, disable hysteresis
/* Configuring UART TXD pin */
LPC_IOCON->PIO1_7 = 0xD1; // select TXD mode, pull up, disable hysteresis
Uart_Init();
#ifdef SUPPORT_I2C_BUS
/* Configuring I2C SCL pin */
LPC_IOCON->PIO0_4 = 0x0001; // select SCL mode, standard mode
/* Configuring I2C SDA pin */
LPC_IOCON->PIO0_5 = 0x0001; // select SDA mode, standard mode
I2C_Init(I2CMASTER,0,0,0);
lcd_init();
#endif
#ifdef SUPPORT_MANUAL_CONTROL
/* Configuring joystick pins P3.0, P3.1, P2.3, P3.3 */
LPC_IOCON->PIO3_0 = 0xC8;// select PIO mode, disable hysteresis
LPC_GPIO3->DIR &= ~(1 << 0);
LPC_IOCON->PIO3_1 = 0xC8; // select PIO mode, disable hysteresis
LPC_GPIO3->DIR &= ~(1 << 1);
LPC_IOCON->PIO2_3 = 0xC8; // select PIO mode, disable hysteresis Deepak Varma
LPC_GPIO2->DIR &= ~(1 << 3);
LPC_IOCON->PIO3_3 = 0xC8; // select PIO mode, disable hysteresis
LPC_GPIO3->DIR &= ~(1 << 3); // set PIO pin P2.3 as input
#endif
//setting up the emergency pins on the micro controller
//pin numbers 33 is Em on
//pin number 45 is EM off
//pin number 45
LPC_IOCON->PIO1_5 = 0xD0;// select PIO mode, disable hysteresis
LPC_GPIO1->DIR &= ~(1 << 5);// set as input
//setting pin number 33
LPC_IOCON->JTAG_TMS_PIO1_0 = 0xD0;// select PIO mode, disable hysteresis
LPC_GPIO1->DIR &= ~(1 << 0);// set as input
WDT_Init(10); // 10 seconds timeout
}
/*********************************************************************************/
init of the timer
/*********************************************************************************/
void Timer_PWM_Init(uint8_t ctId)
{
uint32_t pclk_freq = SystemAHBFrequency;/* get the clock of the timer unit */
uint32_t pwmfrq = pclk_freq / PWM_FREQ;
switch (ctId)
{
case CT32B0:
LPC_TMR32B0->TCR = 0x02; /* disable and reset this CounterTimer */
LPC_TMR32B0->PR = 0x00; /* set prescaler to zero */
LPC_TMR32B0->MR3 = pwmfrq; /* set the default frequency in MR3 */
Timer_PWM_Set_Duty_Cycle(ctId, CTMAT0, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY,2,4);
Timer_PWM_Set_Duty_Cycle(ctId, CTMAT1, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY,2,4);
Timer_PWM_Set_Duty_Cycle(ctId, CTMAT2, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY,2,4);
LPC_TMR32B0->MCR = 0x400; /* reset timer on match of MR3, no interrupts */
LPC_TMR32B0->PWMC = 0x07; /* select PWM mode for CT32B0_MAT0 ,CT32B0_MAT1 and CT32B0_MAT2 */
LPC_TMR32B0->IR = 0x1F; /* reset all interrupt flags */
LPC_TMR32B0->CTCR = 0x00; /* use the CounterTimer in timer mode */
LPC_TMR32B0->TCR = 0x01; /* CounterTimer enable */
break;
case CT32B1:
LPC_TMR32B1->TCR = 0x02; /* disable and reset this CounterTimer */
LPC_TMR32B1->PR = 0x00; /* set prescaler to zero */
LPC_TMR32B1->MR3 = pwmfrq; /* set the default frequency in MR3 */
Timer_PWM_Set_Duty_Cycle(ctId, CTMAT0, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY,2,4);
Timer_PWM_Set_Duty_Cycle(ctId, CTMAT1, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY,2,4);
Timer_PWM_Set_Duty_Cycle(ctId, CTMAT2, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY, PWM_DUTY,2,4);
LPC_TMR32B1->MCR = 0x400; /* reset timer on match of MR3, no interrupts */
LPC_TMR32B1->PWMC = 0x07; /* select PWM mode for CT32B1_MAT0 , CT32B1_MAT1 and CT32B1_MAT2 */
LPC_TMR32B1->IR = 0x1F; /* reset all interrupt flags */
LPC_TMR32B1->CTCR = 0x00; /* use the CounterTimer in timer mode */
LPC_TMR32B1->TCR = 0x01; /* CounterTimer enable */
break;
default:
break;
}
}