UART Program modification problem S32k148

Forum related to ERIKA Enterprise and RT-Druid version 3

Moderator: paolo.gai

Post Reply
soumilfm@gmail.com
Newbie
Posts: 9
Joined: Sun Jan 17, 2021 1:05 am

UART Program modification problem S32k148

Post by soumilfm@gmail.com » Mon May 17, 2021 12:56 pm

Hello ,
I implemented the LPUART Echo Example with help of S32k144 example in S32k148.
I want to modify the code such that I dont echo the same buffer back but use it further in tasks . I am trying to copy the buffer in other buffer as soon as I recieve the data parsed by a '&' . I want to erase the buffer as soon as recieve '& ' sign and copy the contents to buffer2 but unfortunately by using memset the buffer is not recieving any further characters once it is erased.
I will be obliged for any help and suggestion. My knowledge of Interrupts in Erika is not good.

Regards Soumil
Attachments
uart_err.PNG
uart_err.PNG (34.1 KiB) Viewed 547 times

soumilfm@gmail.com
Newbie
Posts: 9
Joined: Sun Jan 17, 2021 1:05 am

Re: UART Program modification problem S32k148

Post by soumilfm@gmail.com » Wed May 19, 2021 5:38 pm

Code: Select all

/* ERIKA Enterprise. */
#include "ee.h"

/* HAL */
#include "hal.h"
#include "pre_struct.h"
#include "user_param.h"
#include "user_rules.h"


volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>


// ACC-Mode
#define accoff (1)
#define standby (2)
#define resume (3)
#define cruise  (4)
#define follow (5)
#define stop (6)

static bool strReceived = true;
static uint8_t  i = 0U;
/* Declare a buffer used to store the received data */
static uint8_t  buffer[25] = {0, };
static uint8_t  buffer2[25] = "recieved";
static uint8_t  buffer3[30] = {0, };

static uint8_t  buffer4[5] = "";
static uint8_t  buffer5[20] = "";

static uint8_t i3 = 0U;
static uint8_t speedbuffcount = 0U;
static uint8_t i4 = 0U;
static uint8_t i5 = 0U;
static uint8_t i6 = 0U;

double currentspeed = 0; // cm/s
uint8_t stopdistance = 30; // cm
double frontdistance = 1; // cm
double prevfrontdistance = 0; // cm
uint8_t accmode = accoff;
double accelerationratio = 3;
double initialspeed = 1; // cm/s
double desiredspeed = 15; // cm/s
double deltaX;
double minimumSpeed = 1.22; //cm/s
double timeGap = 0;
double desiredTimeGap = 0.9;
double temp;

// PID Controller A
double epsilon = 0.01;
double integralgain = 0;
double derivativegain;
double deltatime = 0.01;
double maxvalue = 200;
double minvalue = 0;
double error;
double preverror = 0;
double output;
double kp;
double ki;
double kd;


void serial_print(char const * msg) {
  SuspendAllInterrupts();
  DemoHAL_SerialWrite((uint8_t const *)msg, strlen(msg));
  ResumeAllInterrupts();
}

//static uint8_t  i2 = 0U;
TASK(ACC_MODE_AUTOMAT)
{
 //calculate time Gap
  if(currentspeed > 0)
  {
    timeGap = frontdistance / currentspeed;
  }
  else
  {
    timeGap = 0;
  }
  // calculate deltaX
  deltaX = 0.5 + sqrt(timeGap);

  if (deltaX < 0) {
    deltaX = 0;
  } else if (deltaX > 10) {
    deltaX = 10;
  }

  if (accmode == standby) {
    if (currentspeed > minimumSpeed) {
      accmode = resume;
    }
  } else if (accmode == resume) {
    if (timeGap < (deltaX + desiredTimeGap) && timeGap != 0) {
      accmode = follow;
    } else if (currentspeed == desiredspeed && timeGap > desiredTimeGap) {
      accmode = cruise;
    } else if (timeGap == 0) {
      accmode = stop;
    } else if (currentspeed < desiredspeed && timeGap > desiredTimeGap) {
      accmode = resume;
    }
  } else if (accmode == cruise) {
    if (timeGap > (deltaX + desiredTimeGap) && currentspeed == desiredspeed) {
      accmode = cruise;
    } else if (timeGap < (deltaX + desiredTimeGap)) {
      accmode = follow;
    } else if (currentspeed < desiredspeed && timeGap > (deltaX + desiredTimeGap)) {
      accmode = resume;
    }
  } else if (accmode == follow) {
    if (frontdistance > prevfrontdistance) {
      accmode = resume;
    } else if (timeGap > (deltaX + desiredTimeGap) && frontdistance > stopdistance) {
      accmode = resume;
    } else if ((timeGap <= desiredTimeGap || frontdistance < stopdistance)) {
      accmode = stop;
    }
  } else if (accmode == stop) {
    if ((timeGap > desiredTimeGap || frontdistance > stopdistance)) {
      accmode = resume;
    }
  }

  i5 = 4U;
  ActivateTask(SPEED_CHANGE);
  TerminateTask();
}

TASK(SPEED_CHANGE)
{
  if(accmode != follow)
  {
    //accelerate
    if(accmode != cruise)
    {
      currentspeed += 1;
    }
    else
    {
      currentspeed = currentspeed;
    }
    if (currentspeed > desiredspeed)
     { currentspeed = desiredspeed;}
   // acceleration close

    if (accmode == stop)
    {
     currentspeed = 0;
    }

  }

  else if(accmode == follow)
  {
    // decelerate

    uint8_t x= 0U;
    x = currentspeed * currentspeed;
	  x=x+ (2 * fabs(deltaX + desiredTimeGap - timeGap));
	  x= sqrt(x);
    x= x-currentspeed;
    //PID to decelerate
    error = x;
    if(error > epsilon)
    {
      integralgain += error * deltatime;
    }
    derivativegain += (error - preverror) / deltatime;

    output = (kp * error)
      + (ki * integralgain)
      + (kd * derivativegain);

  if (output > maxvalue) {
      output = maxvalue;
      } else if (output < minvalue) {
      output = minvalue;  }

  preverror = error;

  currentspeed -= output;

   if(currentspeed <= 0)
    {
      currentspeed = 0;
    }

  }

  // //send change speed signal

   itoa(currentspeed,buffer5,10);
   strcpy(buffer3,"newspeed:");
   strcat(buffer3,buffer5);
   strcat(buffer3,"&");
  // serial_print("TASK1\r\n");
 // DemoHAL_SerialWrite_Async("Taskspeed",strlen("Taskspeed")) ;
  DemoHAL_SerialWrite_Async(buffer3,strlen(buffer3)) ;
  // memset(&buffer3[0], 0, sizeof(buffer3));
  // memset(&buffer5[0], 0, sizeof(buffer5));
   prevfrontdistance = frontdistance;
  // i6 = 7U;
 // memset(&buffer[0], 0, sizeof(buffer));
  TerminateTask();

}


ISR(LPURAT1_ISR2) {

	DemoHAL_SerialISRHandler();

      /* Check if current byte is new line */
    if (buffer[i] == '&')
    { //DemoHAL_SerialRead(&buffer[i], 1UL);
      i=0;
      ActivateTask(ReceiveTask);

    }else
    { //DemoHAL_SerialRead(&buffer[i], 1UL);
      ++i;
    }
	  // if (strReceived == false) {

	  //   if (buffer[i] == '&') {
	  //     ActivateTask(ReceiveTask);
	  //   }

	  //   ++i;
	  // } else {
	  //   if (DemoHAL_SerialWrite_Status() == STATUS_SUCCESS) {
	  //     /* Reset the buffer length and received complete flag */
	  //     i = 0;
	  //     strReceived = false;
	  //   }
	  // }

}

 TASK(Process_Uart_Bytes)
 {
    if (strstr((char *)buffer2,"speed_value:"))
 	{
 	  i4 = 5U;
 	  i3 = 0U;
 	  speedbuffcount = 0U;
 	  for(i3 = 0U;buffer2[i3]!='&';i3++)
 	    {
 	      if(isdigit(buffer2[i3]))
 	      {
 	        buffer4[speedbuffcount] = buffer2[i3];
 	        speedbuffcount++;
	       }
 	    }
 	  currentspeed = atol(buffer4);
 	 // DemoHAL_SerialWrite(buffer4,strlen(buffer4));
 	  memset(&buffer4[0], 0, sizeof(buffer4));

  	}
    else if(strstr((char *)buffer2,"front_distance:"))
   {

    i4 = 5U;
    i3 = 0U;
    speedbuffcount = 0U;
    for(i3 = 0U;buffer2[i3]!='&';i3++)
      {
        if(isdigit(buffer2[i3]))
        {
          buffer4[speedbuffcount] = buffer2[i3];
          speedbuffcount++;
         }
      }
    frontdistance = atol(buffer4);
   DemoHAL_SerialWrite(buffer4,strlen(buffer4));
    memset(&buffer4[0], 0, sizeof(buffer4));
    }
   else if(strstr((char *)buffer2,"start"))
   {
   accmode = standby;

   // DemoHAL_SerialWrite(buffer2,strlen(buffer2));

    }

//    else
//  {
// 	  //i3 = strlen(buffer2);
// 	  /* Send the received data back */
//      	 DemoHAL_SerialWrite(buffer2, strlen(buffer2));
//      	 i4=0U;
//   }
// 	memset(&buffer2[0], 0, sizeof(buffer2));
// 	strReceived = false;

 // DemoHAL_SerialWrite(buffer3,strlen(buffer3)) ;
  ActivateTask(ACC_MODE_AUTOMAT);

 	TerminateTask();
// //
 }


TASK(ReceiveTask)
 {

//  strReceived = true;
  i6 = 0U;

  strcpy(buffer2,buffer);

  ActivateTask(Process_Uart_Bytes);


  TerminateTask();
}


#define welcomeMsg "This example is a simple echo using LPUART\r\n\
it will send back any character you send to it.\r\n\
The board will greet you if you send 'Hello Board'\r\n\
Now you can begin typing:\r\n"


void idle_hook(void) {
	 DemoHAL_MainFunction();

	  /* Send a welcome message */
	 DemoHAL_SerialWrite("initialspeed:1", strlen("initialspeed:1"));

	  /* Infinite loop:
	   * 	- Receive data from user
	   * 	- Echo the received data back
	   */
	  for (;;) {
	    /* Get the received data */
	 //   while (strReceived == false) {
	      /* Because the terminal appends new line to user data,
	       * receive and store data into a buffer until it is received
	       */

	      DemoHAL_SerialRead(&buffer[i], 1UL);

	      DemoHAL_MainFunction();

	//  }
 }
}


void StartupHook(void) {
  DemoHAL_SerialInit();

}

int main()
{ 
  DemoHAL_Init();
  accmode = standby;
  frontdistance =40;
  currentspeed = 2;

#if (defined(OSEE_API_DYNAMIC))
  InitOS();

  SetIdleHook(idle_hook);
#endif /* OSEE_API_DYNAMIC */

  /* let's start the multiprogramming environment...*/
  StartOS(OSDEFAULTAPPMODE);
  
  return 0;
}



Hello , When I run DemoHAL_SerialWrite_Async(buffer3,strlen(buffer3)) ; in TASK SPEED CHANGE . The read buffer in UART would not be updated at all. The TASK SPEED CHANGE does not return at all to allow further update of buffer from Interrupt .Does anyone have suggestions. I am attaching my OIL file also below.

Code: Select all

CPU mySystem {

  OS myOs {
    EE_OPT = "OS_EE_APPL_BUILD_DEBUG";
    EE_OPT = "OS_EE_BUILD_DEBUG";
//    EE_OPT = "OS_EE_VERBOSE";

    CPU_DATA = CORTEX_M {
      MODEL = M4;
      MULTI_STACK = TRUE;
      IDLEHOOK = TRUE {
        HOOKNAME = "idle_hook";
      };
      EXECUTE_FROM_RAM = FALSE;
      TRACER = OFF;
    };

    MCU_DATA = S32K1XX {
      MODEL = S32K148;
    };

    BOARD_DATA = S32K148EVB_Q144_Q176;

//    USEDYNAMICAPI = TRUE {
//      TASK_ARRAY_SIZE		= 6;
//      SN_ARRAY_SIZE		= 6;
//      STACKS_MEMORY_SIZE	= 2048;
//    };

    LIB = S32_SDK {
      /* Used to select Board: S32K148EVB-Q144-Q176 */
      BOARD = S32K148EVB_Q144_Q176;
      /* Used to select library version. */
      VERSION = "0.8.6 EAR";
      /* Create libs32sdk.a */
      STAND_ALONE = TRUE;
    };

    STATUS = EXTENDED;
    STARTUPHOOK = TRUE;
    ERRORHOOK = FALSE;
    SHUTDOWNHOOK = FALSE;
    PRETASKHOOK = FALSE;
    POSTTASKHOOK = FALSE;
    USEGETSERVICEID = FALSE;
    USEPARAMETERACCESS = FALSE;
    USERESSCHEDULER = FALSE;

    USEORTI = TRUE;

    KERNEL_TYPE = OSEK { CLASS = BCC1; };
  };

  APPDATA myApp {
    APP_SRC  = "code.c";
    APP_SRC  = "hal.c";
    APP_SRC  = "defuzz.c";
    APP_SRC  = "f_operators.c";
    APP_SRC  = "map_function.c";

  };

  TASK ReceiveTask {
    PRIORITY = 1;
  };
  
    TASK Process_Uart_Bytes{
  	PRIORITY = 2;
  };
  
  TASK ACC_MODE_AUTOMAT{
  	PRIORITY = 2;
  };
  TASK SPEED_CHANGE{
  	PRIORITY = 2;
  };
  


  ISR LPURAT1_ISR2 {
    PRIORITY = 1;
    CATEGORY = 2;
    SOURCE   = "LPUART2";
  };
};

Regards
Soumil

Post Reply