MPC5748G UART uart_pal

文章目录

    • 前言
    • UART简介
    • 新建工程
    • PE配置
    • 补全代码
    • 微信公众号

前言

MPC5748G开发环境搭建
MPC5748G S32_SDK_PA_RTM_3.0.0 GPIO 调试仿真设置
MPC5748G SDK 定时器 timing_pal

前三篇讲了开发环境的搭建, GPIO的读写操作, 调试仿真的设置, 定时器的使用等, 本篇介绍下串口.

UART简介

MPC5748G并没有专门的叫UART的东西, 使用LINFlexD这个组件兼任的, 多达18个, 很是生猛:
MPC5748G UART uart_pal_第1张图片
DEVKIT-MPC5748G为例, 板子上的PC8, PC9(LINFlexD)连接到OpenSDA的虚拟串口, 正好用OpenSDA的USB口就可以和这个串口通信:
MPC5748G UART uart_pal_第2张图片

新建工程

步骤如下:

  • 打开S32, File -> New -> S32DS Application Project
  • 选择MCU: MPC5748G, 填入工程名
  • 默认3个核都勾上了, 这里去掉Z4_1和Z2_2的勾选, 只保留Z4_0, 选择SDK: MPC5748G_SDK_Z4_0 3.0.0, 点击Finish结束.

PE配置

LINFlexD设置:
MPC5748G UART uart_pal_第3张图片
Components Library中双击添加uart_pal:
MPC5748G UART uart_pal_第4张图片
对uart_pal1进行配置, 选择LINFlexD_2, 波特率随手写个1.5Mbps, 接收的函数名字随便写:

MPC5748G UART uart_pal_第5张图片
点击生成代码:
MPC5748G UART uart_pal_第6张图片

补全代码

直接把官方uart_pal的例程的一部分搞过来看:

/* Including necessary module. Cpu.h contains other modules needed for compiling.*/
#include "Cpu.h"

  volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
#include 

#define TIMEOUT    100UL
#define welcomeMsg "Hello MPC5748G UART PAL!\r\n"
#define BUFFER_SIZE     256UL

/* Buffer used to receive data from the console */
uint8_t buffer[BUFFER_SIZE];
uint8_t bufferIdx;

/* UART rx callback for continuous reception, byte by byte
 * Receive and store data byte by byte until new line character is received,
 * or the buffer becomes full (256 characters received)
 */
void uartRxCallback(void *driverState, uart_event_t event, void *userData) {
    /* Unused parameters */
    (void)driverState;
    (void)userData;

    /* Check the event type */
    if (event == UART_EVENT_RX_FULL)
    {
        /* The reception stops when newline is received or the buffer is full */
        if ((buffer[bufferIdx] != '\n') && (bufferIdx != (BUFFER_SIZE - 2U)))
        {
            /* Update the buffer index and the rx buffer */
            bufferIdx++;
            UART_SetRxBuffer(&uart_pal1_instance, &buffer[bufferIdx], 1U);
        }
    }
}

/*! 
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
int main(void)
{
  /* Write your local variable definition here */
	status_t status;
	uint32_t bytesRemaining = 0;

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  #ifdef PEX_RTOS_INIT
    PEX_RTOS_INIT();                   /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */

    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                   g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE);

    /* Initialize pins */
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

    /* Initialize UART PAL over LINFlexD */
    UART_Init(&uart_pal1_instance, &uart_pal1_Config0);

    /* Send a welcome message */
    UART_SendDataBlocking(&uart_pal1_instance, (uint8_t *)welcomeMsg, strlen(welcomeMsg), TIMEOUT);


    while(1) {

        /* Receive and store data byte by byte until new line character is received,
           * or the buffer becomes full (256 characters received)
           */
          UART_ReceiveData(&uart_pal1_instance, buffer, 1U);
          /* Wait for transfer to be completed */
          while(UART_GetReceiveStatus(&uart_pal1_instance, &bytesRemaining) == STATUS_BUSY);
          /* Check the status */
          status = UART_GetReceiveStatus(&uart_pal1_instance, &bytesRemaining);
          if (status != STATUS_SUCCESS)
          {
              /* If an error occurred, send the error message and exit the loop */
              //UART_SendDataBlocking(&uart_pal1_instance, (uint8_t *)errorMsg, strlen(errorMsg), TIMEOUT);
              break;
          }
          /* Process end of line in Doc/Window(CRLF) or Unix(LF) */
          if (buffer[bufferIdx - 1] == '\r')
          {
              bufferIdx = bufferIdx - 1;
              /* Replace end of line character with null */
              buffer[bufferIdx] = 0U;
          }
          else
          {
              /* Replace end of line character with null */
              buffer[bufferIdx] = 0U;
          }
          /* If the received string is "Hello!", send back "Hello World!" */
          if(strcmp((char *)buffer, "Hello!") == 0)
          {
              strcpy((char *)buffer, "Hello World!\n");
          }
          else
          {
              buffer[bufferIdx] = '\n';
              bufferIdx++;
              /* Append string terminator to the received data */
              buffer[bufferIdx] = 0U;
          }
          /* Send the received data back */
          UART_SendDataBlocking(&uart_pal1_instance, buffer, strlen((char *) buffer), TIMEOUT);
          /* Reset the buffer index to start a new reception */
          bufferIdx = 0U;

    }

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;) {
    if(exit_code != 0) {
      break;
    }
  }
  return exit_code;
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */

调试运行, 打开sscoom:
MPC5748G UART uart_pal_第7张图片
符合预期, 这个丢字节真的是…

printf的用法参考: 浅谈嵌入式软件开发之重定向标准输入输出设备使用printf()函数格式化输出调试信息(基于S32DS IDE和MPC5744P), 没试过.

微信公众号

欢迎扫描关注我的微信公众号, 及时获取最新文章:
在这里插入图片描述

你可能感兴趣的:(MPC5748G)