MQX _timer_delay(x) issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MQX _timer_delay(x) issue

910 Views
sudhanshumehta
Contributor IV

Hi Team,

following is my startup code for K21 tower board. .

I am using codewarrior 10.6 and MQX4.2

I configured "ttya" serial port for RS232.

I have a problem. In "void button_task(uint32_t)" when I use _time_delay(any number  e:g 100, 1000 ms) then i am not able to send data on "ttya" serial port.

If i remove _time_delay(x) function from that task, then i am able to send data on "ttya" serial port.

Please note that in void led_task(uint32_t) task, _time_delay(x) is making correct delay.

Am I making any mistake in using _time_delay(x) function or configuring serial port?

/*HEADER**********************************************************************

*

* Copyright 2008 Freescale Semiconductor, Inc.

*

* This software is owned or controlled by Freescale Semiconductor.

* Use of this software is governed by the Freescale MQX RTOS License

* distributed with this Material.

* See the MQX_RTOS_LICENSE file distributed for more details.

*

* Brief License Summary:

* This software is provided in source form for you to use free of charge,

* but it is not open source software. You are allowed to use this software

* but you cannot redistribute it or derivative works of it in source form.

* The software may be used only in connection with a product containing

* a Freescale microprocessor, microcontroller, or digital signal processor.

* See license agreement file for full license terms including other

* restrictions.

*****************************************************************************

*

* Comments:

*

*   This example demonstrates how to use Processor Expert to configure MQX BSP.

*   The application demo

*   1. The PWM signal is generated using FlexTimer FTM0 Channel 0

*      It can be observed by scope on PWM0 - A40 pin

*      on TWR-ELEV FUNCTIONAL or TWR-PROTO board.

*   2. It also toggles by LED1 on board using GPIO driver

*      to signalize that application is running.

*

*

*END************************************************************************/

#include <mqx.h>

#include <bsp.h>

#include <fio.h>

LWGPIO_STRUCT SW2;

#if ! BSPCFG_ENABLE_IO_SUBSYSTEM

#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h.

/* Please recompile BSP with this option. */

#endif

#ifndef PE_LDD_VERSION

#error Demo requires Processor Expert BSP code to be generated.

#error See "<MQX_INSTALL_DIR>\doc\tools\cw\MQX_CW10_Getting_Started.pdf" chapter "CW10.x, MQX and Processor Expert" for details

#else

#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED

#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL.

/*

* Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h

* and recompile BSP with this option.

*/

#endif

/* Task enumerations and prototypes */

enum {

    PWM_TASK = 1,

    LED_TASK,

    BUTTON_TASK

} etask_type;

void pwm_task(uint32_t);

void led_task(uint32_t);

void button_task(uint32_t);

/* Task template list */

const TASK_TEMPLATE_STRUCT  MQX_template_list[] =

{

   /* Task Index,      Function,       Stack,  Priority,    Name,          Attributes,          Param,   Time Slice */

    { PWM_TASK,        pwm_task,       800,       8,       "PWM Task",     MQX_AUTO_START_TASK,   0,         0 },

    { LED_TASK,        led_task,       800,       9,       "LED Task",     MQX_AUTO_START_TASK,   0,         0 },

    { BUTTON_TASK,     button_task,    2000,      9,      "BUTTON Task",  MQX_AUTO_START_TASK,   0,         0 },

    { 0 }

};

#define TERMINAL_CURSOR_POSITION_MAX    (80)

/*TASK*-------------------------------------------------------------------------

*

* Task Name    : pwm_task

* Comments     :

*

*END*-------------------------------------------------------------------------*/

static int                  pwm_task_count;

static LDD_TDeviceData     *PWM_DeviceData;

static LDD_TError           PWM_Error;

volatile PWM_TValueType     PWM_Value;

volatile PWM_TValueType     PWM_MaxValue;

volatile PWM_TValueType     PWM_Step;

volatile int                PWM_Freguency;

void pwm_task

   (

      uint32_t initial_data

   )

{

    static int terminal_cursor_position = 1;

    _task_block(); //this task is blocked

    /* Print example functional description on terminal */

    puts("\n");

    puts("\n********************************************************************************");

    puts("\n*");

    puts("\n*   This example demonstrates how to use Processor Expert to configure MQX BSP.");

    puts("\n*   The application demo :");

    puts("\n*   1. The PWM signal is generated using FlexTimer FTM0 Channel 0");

    puts("\n*      It can be observed by scope on PWM0 - A40 pin");

    puts("\n*      on TWR-ELEV FUNCTIONAL or TWR-PROTO board.");

    puts("\n*   2. It also toggles LED1 on board using GPIO driver");

    puts("\n*      to signalize that application is running.");

    puts("\n*");

    puts("\n********************************************************************************");

    puts("\n");

    /* Initialize PWM device on FTM0 device */

     puts("\nInitializing PWM device.....");

    PWM_DeviceData = PWM_Init(NULL);

    if (PWM_DeviceData == NULL)  {

    puts("failed");

        _task_block();

    }

    else  {

        puts("done");

    }

    PWM_Value       = 0;

    PWM_Step        = PWM_PERIOD_TICKS / 32;

    PWM_MaxValue    = PWM_PERIOD_TICKS;

    PWM_Freguency   = (PWM_CNT_INP_FREQ_U_0/PWM_PERIOD_TICKS);

  printf("\n - PWM frequency              = %d Hz", PWM_Freguency);

    puts("\nThe PWM signal is generated on FTM0 Channel 0");

    puts("\n");

    /* Enable PWM device */

    PWM_Error = PWM_Enable(PWM_DeviceData);

    while(1)

    {

        pwm_task_count++;

        /* Suspend task for 250ms */

        _time_delay(250);

        /* Print dot on console to see that application is running */

        if (terminal_cursor_position++ > TERMINAL_CURSOR_POSITION_MAX) {

            terminal_cursor_position = 1;

            puts("\n");

        }

        else {

            puts(".");

        }

    }

}

/*

** ===================================================================

**     Event       :  PWM_OnCounterRestart (overloads function in module Events)

**

**     Component   :  PWM [TimerUnit_LDD]

**     Description :

**         Called if counter overflow/underflow or counter is

**         reinitialized by modulo or compare register matching.

**         OnCounterRestart event and Timer unit must be enabled. See

**         <SetEventMask> and <GetEventMask> methods. This event is

**         available only if a <Interrupt> is enabled.

**     Parameters  :

**         NAME            - DESCRIPTION

**       * UserDataPtr     - Pointer to the user or

**                           RTOS specific data. The pointer passed as

**                           the parameter of Init method.

**     Returns     : Nothing

** ===================================================================

*/

void PWM_OnCounterRestart(LDD_TUserData *UserDataPtr)

{

    /* Increment PWM duty-cycle from 0-100% */

    PWM_Value += PWM_Step;

    if (PWM_Value > PWM_MaxValue) PWM_Value = 0;

    /* Set new PWM channel value */

    PWM_Error = PWM_SetOffsetTicks(PWM_DeviceData, 0, PWM_Value);

}

/*TASK*-------------------------------------------------------------------------

*

* Task Name    : led_task

* Comments     :

*

*END*-------------------------------------------------------------------------*/

/* LED */

LDD_TDeviceData     *LED_DeviceData;

LDD_TError           LED_Error;

static int           led_count;

void led_task

   (

      uint32_t initial_data

   )

{

    //_task_block();

    /* Initialize GPIO  */

    puts("\nInitializing LED device.....");

    LED_DeviceData = GPIO1_Init(NULL);

    if (LED_DeviceData == NULL)    {

        puts("failed");

        _task_block();

    }

    else {

        puts("done");

    }

    puts("\n");

    while(1)

    {

        led_count++;

        _time_delay(500);

        GPIO1_ToggleFieldBits(LED_DeviceData, LED1, 1);

    }

}

#define customerPortName "ttya:"

FILE_PTR customerPort_FD;

MQX_FILE_PTR customerPort_FD1;

uint32_t  customerPortBaudrate  = 115200;

void sendByte(unsigned char value)

{unsigned char ret = 0;

     ret= _io_write(customerPort_FD, (void *)&value, sizeof(value));

    

}

void button_task

   (

      uint32_t initial_data

   )

{

   

    volatile int i=0, j=0, k=0;

    unsigned char customerPortByte = 's';

    unsigned char customerPortByte1 = 't';

    unsigned char ret = 0;

    customerPort_FD = fopen(customerPortName, 0x00);

   

    printf("value init task %d.\n", customerPort_FD);

    ret =   ioctl(customerPort_FD,IO_IOCTL_SERIAL_SET_BAUD,&customerPortBaudrate);

    printf("init value %x \n", ret);

   

   

    /* Initialize GPIO  */

    if (!lwgpio_init(&SW2, LWGPIO_PORT_C | LWGPIO_PIN7, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))

    {

       printf("Initialising switch 2 failed.\n");

       _task_block();

    }

    else

    {

        printf("SW2 initiated.\n");

    }

    lwgpio_set_functionality(&SW2, LWGPIO_MUX_C7_GPIO);

       

    printf("\n - port used            = %s Hz", BSP_DEFAULT_IO_CHANNEL);

    while(1)

    {

        if (lwgpio_get_value(&SW2))

         {

           printf("value 1  \n");

                 

           sendByte('A');

           sendByte('B');

           sendByte('C');

         }

         else

         {

           printf("value 2  \n");

           ret= _io_write(customerPort_FD, (void *)&customerPortByte1, 1);

           sendByte('D');

           sendByte('E');

           sendByte('F');

         }                

    }

}

#endif /* PE_LDD_VERSION */

/* EOF */

0 Kudos
4 Replies

483 Views
sudhanshumehta
Contributor IV
0 Kudos

483 Views
sudhanshumehta
Contributor IV

I have slightly modified button_task. now if i give delay of 15 sec it is sending good characters but if i give delay of 105 ms, it is not giving me good characters.

void button_task

   (

      uint32_t initial_data

   )

{

   

    volatile int i=0, j=0, k=0;

    unsigned char customerPortByte = 's';

    unsigned char customerPortByte1 = 't';

    unsigned char ret = 0;

    customerPort_FD = fopen(customerPortName, 0x00);

   

    printf("value init task %d.\n", customerPort_FD);

    ret =   ioctl(customerPort_FD,IO_IOCTL_SERIAL_SET_BAUD,&customerPortBaudrate);

    printf("init value %x \n", ret);

   

   

    /* Initialize GPIO  */

    if (!lwgpio_init(&SW2, LWGPIO_PORT_C | LWGPIO_PIN7, LWGPIO_DIR_INPUT, LWGPIO_VALUE_NOCHANGE))

    {

       printf("Initialising switch 2 failed.\n");

       _task_block();

    }

    else

    {

        printf("SW2 initiated.\n");

    }

    lwgpio_set_functionality(&SW2, LWGPIO_MUX_C7_GPIO);

       

    printf("\n - port used            = %s Hz", BSP_DEFAULT_IO_CHANNEL);

    while(1)

    {

        if (lwgpio_get_value(&SW2))

         {

           printf("value 1  \n");

           _time_delay(15);

           sendByte('A');

           sendByte('B');

           sendByte('C');

         }

         else

         {

           printf("value 2  \n");

           _time_delay(105);

           sendByte('D');

           sendByte('E');

           sendByte('F');

         }

       

    }

}

0 Kudos

483 Views
DavidS
NXP Employee
NXP Employee

Hi Sudhanshu,

I think the issue was mainly the stack size of PWM task.

Summary of my Debug Process:

I played around with different configurations initially and added _time_delay() to button_task and was getting unhandled exception in the pwm_task when a _time_delay() was used.  If I didn't use _time_delay() in button_task then I did not have unhandled exception.

I increased all tasks stacks *2 and code started to work.  I used the MQX TAD to see the stack size.  Then I reduced the tasks stack to around 80% and all seems stable.

I've attached my main.c for the pe_demo using the C:\Freescale\Freescale_MQX_4_2\demo\pe_demo\build\cw10gcc\pe_demo_twrk21f120m project.

Hardware Setup:

TWR-K21F120M+TWR-SER

I removed J13 jumper from the default pins 1-2 of TWR-K21F120M card.

I removed J19 jumper from default pins 1-2 of the TWR-SER card.

I jumpered the UART0_TX/PTA14 from J13 pin 1 of TWR-K21F120M card to the TWR-SER card J19 pin 1 to allow me to output to UART0/ttya.

Regards,

David

0 Kudos

483 Views
sudhanshumehta
Contributor IV

HI David,

I thank you for replying. this has resolved half of problem.

in the button task, inside while(1), inside if condition, if you comment everything printing on uart0(only in if condition part not in else condition part), and then execute the program, then when you press "SW2", data in else case will not go correctly to uart0..

Thanks

Sudhanshu

pastedImage_0.png

0 Kudos