Landtiger LPC1768 + freeRTOS + PWM problem

hrzrfn wrote on Thursday, July 04, 2013:

Greetings

I’m a beginner in LPC1768 & freertos development, so I hope I could get some help for my project

I’m using freeRTOS LPC1768 rowley crossworks demo as base config for my project.  I’ve modified the code by removing all demo tasks except for the flash.c and uIP task, and added my own PWM code to the project

the issue was that both of the flash.c and uIP task work perfectly, however my PWM are not working. I’ve configured for the PWM to be channeled to on-board LED. From debugging, it seems that the PWM is initialized, but never run. Below is the PWM code:

LPC_SC->PCONP |= 1 << 6; // enable power for PWM1 
  LPC_SC->PCLKSEL0 |= 1 << 12; //PCLK = CCLK = 64MHz
  LPC_PWM1->PR = 64 - 1; //prescale to obtain 1MHz
  
  LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); //output to pin 2.0 (LED 11). From this, the LED is turned off,showing initilization are taking place
  LPC_PINCON->PINMODE4 = (LPC_PINCON->PINMODE4 & ~0x3) | 0x2; //no pull-up/down
  
  LPC_PWM1->MR0 = 20000;  /*set clock cycles for PWM. PWM freq = PCLK/MR0 (?)*/
  LPC_PWM1->MR1 = 1500; // pulse width 1.5ms
  LPC_PWM1->LER = (1 << 0) | (1 << 1); //Apply changes to MR
 
  LPC_PWM1->MCR |= 1 << 1; // interrupt on match0.Reset timer on Match0;
  LPC_PWM1->PCR = (1<<9); //set output to PWM1
  LPC_PWM1->TCR = (1<<0)|(1<<3); //reset & enable counter
}

when I’m not using freeRTOS, the PWM would already run after the LPC_PWM1->TCR = (1<<0)|(1<<3); line…
however, with freeRTOS, the PWM doesnt run after LPC_PWM1->TCR = (1<<0)|(1<<3); line…

any idea on whats happening?
really appreciate any help.thank you in advance

davedoors wrote on Friday, July 05, 2013:

We can’t normally comment on chip specific peripheral configurations, there are too many chips with too many peripherals and it is outside the scope of FreeRTOS support, but if you say the PWM works without FreeRTOS then I think it is ok to assume the peripheral configuration is ok.

I notice in your code that you are using an interrupt. What frequency are interrupts generated? What is the interrupt handler doing? What is the priority of the interrupt handler? If the handler has a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY then the kernel should not effect its timing, but that can only be done if the handler is not calling FreeRTOS API functions.

hrzrfn wrote on Sunday, July 07, 2013:

Hye thank you for your reply

From your questions, I’ve realize I had to include NVIC interrupt handler inside my program whilist I didn’t have to when trying my program without freeRTOS

thus, modifying my program to be:

void vPWMTaskFunction( void *pvParameters );
void PWM1_IRQHandler(void);
void PWM1_IRQHandler (void) 
{
	if((PWM1->IR & 0x01) == 0x01) // Check whether it is an interrupt from match compare 0
	{
		PWM1->IR |= 0x01; // Clear the interrupt flag
	}
	
	return;
}
void vPWMTaskFunction( void *pvParameters )
{
  int numA = 1000;
  ( void ) pvParameters;
 SC->PCONP |= 1 << 6; // enable power for PWM1 
 SC->PCLKSEL0 |= 1 << 12; //PCLK = CCLK = 64MHz
 PWM1->PR = 64 - 1;
 PINCON->PINSEL4 = (PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); //output to pin 2.0
 PINCON->PINMODE4 = (PINCON->PINMODE4 & ~0x3) | 0x2;
 PWM1->MR0 = 20000;  /*set clock cycles for PWM. PWM freq = PCLK/MR0 (?)*/
 PWM1->MR1 = 1500; // pulse width 1.5ms
 PWM1->LER = (1 << 0) | (1 << 1); //Apply changes
 PWM1->MCR |= 1 << 1; // interrupt on match0.Reset timer on Match0;
 NVIC_SetPriority( PWM1_IRQn, configPWM_INTERRUPT_PRIORITY );
 NVIC_EnableIRQ( PWM1_IRQn );
  PWM1->PCR = (1<<9); //--- PWM1 output
  PWM1->TCR = (1<<0)|(1<<3);
    for( ;; )
    {
                PWM1->MR1 = numA; // pulse width 1.5ms
                PWM1->LER |=  (1 << 1); //Apply changes
                numA++; 
                if(numA >= 2000){
                numA = 1000;
                }
    }
}

-I’m trying to produce PWM with 50Hz frequency.
-The interrupt handler I’ve included is to check & clear the interrupt flag
-I set 6 for the pwm interrupt handler priority, (configPWM_INTERRUPT_PRIORITY) in FreeRTOSconfig.h

still the code doesnt work. Any clue for me? Sorry, I’m really a beginner in freertos & still struggling after reading online manuals, really appreciate some help here

thx in advance

hrzrfn wrote on Sunday, July 07, 2013:

pardon me but ignore some of the comments i’ve made on the coding, i’ve forgot to cross them out after making some modifications !

PWM1->MR1 = numA; // pulse width 1.5ms
                PWM1->LER |=  (1 << 1); //Apply changes

part

hrzrfn wrote on Tuesday, July 30, 2013:

Hi.

I have made some modification to my code. This time i’m using freertos template ported to KEIL http://mazcanproject.sourceforge.net/blog/?p=69

void PWM1_IRQHandler (void) 
{
  uint32_t regVal;
  regVal = LPC_PWM1->IR;
  if ( regVal & (1 << 0) )
  {
	match_counter1++;	
  }
  LPC_PWM1->IR |= regVal;		/* clear interrupt flag on match 0 */
  return;
}
void vPWMTask ( void *pvParameters )
{
  ( void ) pvParameters;
  match_counter1 = 0;
  LPC_SC->PCONP |= 1 << 6; // enable power for PWM1 
  LPC_SC->PCLKSEL0 |= 1 << 12; //PCLK = CCLK = 64MHz
  LPC_PWM1->PR = 64 - 1;
  
  LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); //output to pin 2.0
  LPC_PINCON->PINMODE4 = (LPC_PINCON->PINMODE4 & ~0x3) | 0x2;
  LPC_PWM1->TCR = 0x00000002;
  LPC_PWM1->MCR = 1 << 0;
  
  LPC_PWM1->MR0 = 256;  /*set clock cycles for PWM. PWM freq = PCLK/MR0 (?)*/
  LPC_PWM1->MR1 = 10; // pulse width 1.5ms
  LPC_PWM1->LER = (1 << 0) | (1 << 1); //Apply changes
  
  NVIC_EnableIRQ(PWM1_IRQn);
  LPC_PWM1->PCR = (1<<9); //--- PWM1 output
  LPC_PWM1->TCR = (1<<0)|(1<<3);
  for( ;; )
	{
           if (match_counter1 >= 0x1000) {
			match_counter1 = 0;
			long_duty++;
			if (long_duty >= 256) {
				long_duty = 0;
			}
           }
	LPC_PWM1->MR1 = long_duty; // pulse width 1.5ms
        LPC_PWM1->LER |=  (1 << 1); //Apply changes
  }
}
/**
 * @brief Main program body
 */
int c_entry(void)
{
//	xTaskCreate( vPWMTask, ( signed char * ) "PWM", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
	xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
	/* Start the scheduler. */
	vTaskStartScheduler();
	while(1);
	return 1;
}
/* Support required entry point for other toolchain */
int main (void)
{
	return c_entry();
}

and my program worked in this freertos template using KEIL.

But when I tried my PWM code to examples from freertos (LPC1768 for crossworks & IAR), the PWM does not work anymore

this starts to confuse me. I tried debug my code. The pwm task is running, the interrupt are called, but the LED (as pwm output) does not light up…it seems that the LED stayed at the initialization state only…

really appreciate some help here. thanks!

davedoors wrote on Tuesday, July 30, 2013:

If I understand you correctly you have the pwm and led working in a keil project, but when you move the exact same setup and interrupt code to the Rowley (or IAR?) project the pwm works but the led does not?

There must be a difference in the hardware setup up.  Could it be that

the Keil project is doing some extra configuration that you have not seen because it does it before main() is called?

or

that the pwm and interrupt code is the same in both, but only the keil project is configuring the pin used by the led to be driven by the pwm?

or

the library code used to setup the pin used by the led has a bug in the Rowley (or IAR) version that is not in the keil version.

The io port will have some registers that set up things like if it is input or output, if the port is powered up at all, which of the multiplexed functions the pin is using (some pins can be used in different modes and as io for different peripherals. Can you view these registers in the debugger in both the Keil and Rowley (or IAR) projects to ensure they match?

hrzrfn wrote on Tuesday, July 30, 2013:

Hi dave thank you for your reply

in KEIL, at the PINCON->PINSEL4 = (PINCON->PINSEL4 & ~(0x3 << 0)) | (0x1 << 0); part, the debugger shows

0x000005EE 484C      LDR      r0,[pc,#304]  ; @0x00000720

in IAR

0xe80: 0x482e         LDR.N     R0, ??DataTable3_8      ; PINSEL4

does this mean anything? I’m hope i’m getting to your question

rtel wrote on Wednesday, July 31, 2013:

Unfortunately those lines don’t show anything other than one compiler is loading a constant using program counter relative addressing and the other isn’t.  I think the intention is to see what values end up in the registers after all the initialisation is complete, rather than look at how the values are written to the registers.

Regards.

hrzrfn wrote on Monday, August 05, 2013:

Hi all

finally I’ve got the PWM function working
not only that, the code now also works with the web demo

I’ve merged the LPC17xx.H header file from freertos demo with the original LPC17xx.H header from NXP LPC17xx CMSIS file

hope this would help others who’s having hard times with this

cheers