MQX

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

MQX

605 Views
sudhanshumehta
Contributor IV

Hi I am using MQX 4.2 and K21 tower module.

I am facing problem with UART 0

I am using it in polling mode at baud rate 115200

Main.c file is attached. In serial_task()  if I uncomment

//sendByte('W');

line then MQX is sending data as expected. where as if i commment this line, It is creating problem and sending wrong data.

I feel problem is with Uart0 driver or some timer problem.

/*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>

#include <lwmsgq.h>

//LWGPIO_STRUCT ;

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,

    SERIAL_TASK

} etask_type;

void pwm_task(uint32_t);

void led_task(uint32_t);

void button_task(uint32_t);

void serial_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,    3000,      9,      "BUTTON Task",   MQX_AUTO_START_TASK,   0,         0 },

    { SERIAL_TASK,     serial_task,    3000,      10,      "SERIAL 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:"

MQX_FILE_PTR customerPort_FD;

uint32_t  customerPortBaudrate  = 115200;

void sendByte(unsigned char value)

{

    unsigned char ret = 0;

    unsigned char local_value[1] = {value};

   

     fwrite(local_value , 1 , sizeof(local_value) , customerPort_FD );

}

//extern uint32_t server_queue[];

uint32_t server_queue[100];

void serial_task

   (

      uint32_t initial_data

   )

{

    uint32_t msz[1] = {0};

    unsigned char customerPortByte = 's';

    unsigned char customerPortByte1 = 't';

    unsigned char ret = 0;

    customerPort_FD = fopen(customerPortName, (char *)(IO_SERIAL_RAW_IO|IO_SERIAL_NON_BLOCKING) );

     int num=0;

     _time_delay(100);

    while(1)

    {

     _time_delay(5);

    

     //sendByte('W');

     if (!LWMSGQ_IS_EMPTY(server_queue))

     {

       _lwmsgq_receive((void *)server_queue, msz, LWMSGQ_RECEIVE_BLOCK_ON_EMPTY, 0, 0);

       printf("message received ==  %c \n", msz[0]);

      

       //sendByte( 'A');

       for (num =0; num<6; num++)

       {

            sendByte( 'A' );

           

       }

      }

    }

}

void button_task

   (

      uint32_t initial_data

   )

{   

    bool pressed = 0; 

    unsigned char ret = 0;

    uint32_t msg[1] = {'A'};

    _mqx_uint          result;

    

    result = _lwmsgq_init((void *)server_queue, 1, 1);

      if (result != MQX_OK) {

         // lwmsgq_init failed

      } /* Endif */

   

   

    /* 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);

       

    while(1)

    {

        if (lwgpio_get_value(&SW2))

         {

           //printf("value 1  \n");

           pressed = 0;

           _time_delay(15);

         }

         else

         {

             if(pressed == 0)

             {

               _lwmsgq_send((void *)server_queue, msg, LWMSGQ_SEND_BLOCK_ON_FULL);

               printf("button pressed  \n");

               pressed = 1;

               msg[0]++;

          

             }

             _time_delay(10);

         }

    }

}

#endif /* PE_LDD_VERSION */

/* EOF */

0 Kudos
Reply
1 Reply

400 Views
soledad
NXP Employee
NXP Employee

Hi,

Could you please try to follow the David's advice in the thread: MQX _timer_delay(x) issue

Regards

Sol

0 Kudos
Reply