查看: 11359|回复: 2

[S32] S32K144_LPUART1中断接收的回调函数

[复制链接]

该用户从未签到

3

主题

15

帖子

1

注册会员

Rank: 2

积分
117
最后登录
2022-6-26
发表于 2018-5-22 22:52:33 | 显示全部楼层 |阅读模式
对于S32K144,我们新建一个空白工程,选择ARM Bare-Metal 32-bit Target Binary Toolchain,默认是FSL Make Builder,很容易通过拖放组件自动生成代码,我们只拖一个lpuart1组件,拖放如下代码:

  CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
  CLOCK_SYS_UpdateConfiguration(0, CLOCK_MANAGER_POLICY_FORCIBLE);
  PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
  LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);

除了CLOCK_SYS_UpdateConfiguration函数需要手动填写两个参数,其余参数都是自动填写的,CLOCK_SYS_Init函数参数填写的有错误,不过很容易改正。

要注意的是LPUART_DRV_InstallRxCallback 函数的使用,我们想架空FSL的串口接收数据处理部分,又不想重写中断,那就
安装一个回调函数,由于这个函数的存在,S32K144的串口通信就要比STM32的HAL/LL/StdPeriph库灵活的多,回调函数内容
简单设置如下:

void LPUART1_Rx_Callback_IRQHandler(void *driverState, uart_event_t event, void *userData)
{
  uint8_t ch_temp;
  ch_temp = LPUART1->DATA;
  receive_buf[receive_bytes] = ch_temp;
  if(++receive_bytes >= 64)receive_bytes = 0;

  if (lpuart1_State.bitCountPerChar == LPUART_8_BITS_PER_CHAR)  {
    ++lpuart1_State.rxBuff;
    --lpuart1_State.rxSize;
  }
  lpuart1_State.isRxBusy = false;
  lpuart1_State.receiveStatus = STATUS_SUCCESS;

  LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)&receive_buf[receive_bytes], 1);

  LPUART_DRV_SendDataPolling(INST_LPUART1, &ch_temp, 1);

}

当然初始化要加上这两行代码:
  LPUART_DRV_InstallRxCallback(INST_LPUART1, LPUART1_Rx_Callback_IRQHandler, 0);
  /* Start receive for one char */
  LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)receive_buf, 1);

由于重写了回调函数,串口不会只接收一个字符就关闭,是永远开着的。为了验证接收是否正确,收一个就发一个。

以上代码测试正常!


我知道答案 目前已有2人回答
回复

使用道具 举报

该用户从未签到

3

主题

15

帖子

1

注册会员

Rank: 2

积分
117
最后登录
2022-6-26
 楼主| 发表于 2018-5-23 20:08:29 | 显示全部楼层
进一步的修改和调试如下

void LPUART1_Rx_Callback_IRQHandler(void *driverState, uart_event_t event, void *userData)
{
  uint8_t ch_temp;
  ch_temp = LPUART1->DATA;
  receive_buf[receive_bytes] = ch_temp;
  if(++receive_bytes >= 64)receive_bytes = 0;

#if 0
  if (lpuart1_State.bitCountPerChar == LPUART_8_BITS_PER_CHAR)  {
    ++lpuart1_State.rxBuff;
    --lpuart1_State.rxSize;
  }
  lpuart1_State.isRxBusy = false;
  lpuart1_State.receiveStatus = STATUS_SUCCESS;

  LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)&receive_buf[receive_bytes], 1);

#endif

#if 1
  // 以下代码仅作测试,实际驱动不会包含
  LPUART_DRV_SendDataPolling(INST_LPUART1, &ch_temp, 1);
#endif
}
回复 支持 反对

使用道具 举报

该用户从未签到

3

主题

15

帖子

1

注册会员

Rank: 2

积分
117
最后登录
2022-6-26
 楼主| 发表于 2018-5-24 15:01:36 | 显示全部楼层
/* ###################################################################
**     Filename    : main.c
**     Processor   : S32K14x
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.00
** @brief
**         Main module.
**         This module contains user's application code.
*/
/*!
**  @addtogroup main_module main module documentation
**  @{
*/
/* MODULE main */


/* 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 <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "lpuart_driver.h"



#define welcomeMsg0 "This example is an simple echo using LPUART0\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\
\nNow you can begin typing:\r\n"

#define welcomeMsg1 "This example is an simple echo using LPUART1\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\
\nNow you can begin typing:\r\n"

/* Declare a buffer used to store the received data */
uint32_t bytesRemaining;

#define lpuart0_receive_size 100
#define lpuart1_receive_size 100

uint8_t         lpuart0_send_buf[100]         =        {0, };
uint8_t         lpuart1_send_buf[100]         =        {0, };

uint8_t         lpuart0_receive_buf[lpuart0_receive_size]         =        {0, };
uint8_t         lpuart1_receive_buf[lpuart1_receive_size]         =        {0, };

uint32_t lpuart0_receive_bytes = 0;
uint32_t lpuart1_receive_bytes = 0;

/* @brief Callback for all peripherals which support UART features
* Implements : uart_callback_t_Class
* typedef void (*uart_callback_t)(void *driverState, uart_event_t event, void *userData);
*/

void LPUART0_Rx_Callback_IRQHandler(void *driverState, uart_event_t event, void *userData)
{
  uint8_t ch_temp;
  ch_temp = LPUART0->DATA;
  lpuart0_receive_buf[lpuart0_receive_bytes] = ch_temp;
  if(++lpuart0_receive_bytes >= lpuart0_receive_size)lpuart0_receive_bytes = 0;

#if 0
  if (lpuart0_State.bitCountPerChar == LPUART_8_BITS_PER_CHAR)  {
    ++lpuart0_State.rxBuff;
    --lpuart0_State.rxSize;
  }
  lpuart0_State.isRxBusy = false;
  lpuart0_State.receiveStatus = STATUS_SUCCESS;

  LPUART_DRV_ReceiveData(INST_LPUART0, (uint8_t *)&lpuart0_receive_buf[lpuart0_receive_bytes], 1);
#endif

#if 1
  LPUART_DRV_SendDataPolling(INST_LPUART0, &ch_temp, 1);
#endif
}
void LPUART1_Rx_Callback_IRQHandler(void *driverState, uart_event_t event, void *userData)
{
  uint8_t ch_temp;
  ch_temp = LPUART1->DATA;
  lpuart1_receive_buf[lpuart1_receive_bytes] = ch_temp;
  if(++lpuart1_receive_bytes >= lpuart1_receive_size)lpuart1_receive_bytes = 0;

#if 0
  if (lpuart1_State.bitCountPerChar == LPUART_8_BITS_PER_CHAR)  {
    ++lpuart1_State.rxBuff;
    --lpuart1_State.rxSize;
  }
  lpuart1_State.isRxBusy = false;
  lpuart1_State.receiveStatus = STATUS_SUCCESS;

  LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)&lpuart1_receive_buf[lpuart1_receive_bytes], 1);
#endif

#if 1
  LPUART_DRV_SendDataPolling(INST_LPUART1, &ch_temp, 1);
#endif
}


/*!
  \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 */
  CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,        g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
  CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE);

  PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

  LPUART_DRV_Init(INST_LPUART0, &lpuart0_State, &lpuart0_InitConfig0);
  LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);

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

  LPUART_DRV_InstallRxCallback(INST_LPUART0, LPUART0_Rx_Callback_IRQHandler, (isr_t*)0);
  LPUART_DRV_InstallRxCallback(INST_LPUART1, LPUART1_Rx_Callback_IRQHandler, (isr_t*)0);

  /* lpuart0 lpuart1 Start receive for one char or more */
  LPUART_DRV_ReceiveData(INST_LPUART0, (uint8_t *)lpuart0_receive_buf, 1);
  LPUART_DRV_ReceiveData(INST_LPUART1, (uint8_t *)lpuart1_receive_buf, 1);

#if 1
  /* Send a welcome message */
  LPUART_DRV_SendData(INST_LPUART0, (uint8_t *)welcomeMsg0, strlen(welcomeMsg0));
  /* Wait for transmission to be complete */
  while(LPUART_DRV_GetTransmitStatus(INST_LPUART0, &bytesRemaining) != STATUS_SUCCESS);

  /* Send a welcome message */
  LPUART_DRV_SendData(INST_LPUART1, (uint8_t *)welcomeMsg1, strlen(welcomeMsg1));
  /* Wait for transmission to be complete */
  while(LPUART_DRV_GetTransmitStatus(INST_LPUART1, &bytesRemaining) != STATUS_SUCCESS);
#endif
       
  while(1){

  }

  /*** 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 */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.1 [05.21]
**     for the Freescale S32K series of microcontrollers.
**
** ###################################################################
*/
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2025-9-11 12:41 , Processed in 0.091400 second(s), 23 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表