OM6621E UART0应用例程

pengam · 372次点击 · 2023-05-05
//该例程为UART0,该串口与UART1不同,不支持标准波特率,不支持流控
//配置方法也与UART1不一样

//将源文件 SDK\hardware\peripheral\uart_ex.c 加入到项目工程

//添加头文件
#include "uart_ex.h"

//配置TX RX,不支持流控
#define PIN_UART0_TX                    7
#define PIN_UART0_RX                    9

//波特率仅支持以下值
typedef enum
{
  UART0_BAUDRATE_1K   = 1000,
  UART0_BAUDRATE_2K   = 2000,
  UART0_BAUDRATE_4K   = 4000,
  UART0_BAUDRATE_5K   = 5000,
  UART0_BAUDRATE_10K  = 10000,
  UART0_BAUDRATE_20K  = 20000,
  UART0_BAUDRATE_25K  = 25000,
  UART0_BAUDRATE_50K  = 50000,
  UART0_BAUDRATE_100K = 100000,
  UART0_BAUDRATE_500K = 500000,
  UART0_BAUDRATE_1M   = 1000000,
}uart0_baudrate_cfg;

//默认波特率
#define UART0_BAUD_RATE_DEFAULT  UART0_BAUDRATE_1K

//全局静态配置变量,在芯片唤醒处POWER_SLEEP_LEAVE_TOP_HALF和上电初始化都需要使用
static uart_ex_cfg_t uart_ex_config;

//=====================================================================
//与其它任何高速时钟的外设一样,都需要在POWER_SLEEP_LEAVE_TOP_HALF进行restore设置一次
//如果不配置,则芯片休眠唤醒后无UART0功能
static void peripheral_restore(void)
{    
    uart_ex_open(HS_UART0, &uart_ex_config);
}
//=====================================================================
//接收回调
static void uart0_rx_data_handler(uint8_t data)
{
    //接收测试,收到立刻发送
    uart_ex_send_block(HS_UART0, &data, 1);
}

//=====================================================================
//上电初始化内容
//配置UART TX脚位
static void uart0_config(void)
{
   //配置UART0 TX脚位
    pinmux_config(PIN_UART0_TX,  PINMUX_UART0_SDA_O_CFG);
    pmu_pin_mode_set(BIT_MASK(PIN_UART0_TX), PMU_PIN_MODE_PP);
  //配置UART0 RX脚位
  #if defined(PIN_UART0_RX)
    pinmux_config(PIN_UART0_RX,  PINMUX_UART0_SDA_I_CFG);
    pmu_pin_mode_set(BIT_MASK(PIN_UART0_RX), PMU_PIN_MODE_PU);
  #endif
  /*
  baud_rate[]={1000,2000,4000,5000,10000,20000,25000,50000,100000,500000,1000000}
    mode         smod_val       baud_rate
  UART_EX_MD_2     SMOD_1        1000000
  UART_EX_MD_2     SMOD_0        500000
  UART_EX_MD_1      x            1000~500000
  UART_EX_MD_3      x            1000~500000
  */
    memset(&uart_ex_config, 0, sizeof(uart_ex_cfg_t));
    uart_ex_config.baud_rate=UART0_BAUD_RATE_DEFAULT;
    uart_ex_config.tb80_val=1;
    uart_ex_config.rb80_val=1;    
    if(uart_ex_config.baud_rate<UART0_BAUDRATE_500K)
    {
      uart_ex_config.mode=UART_EX_MD_1;
      uart_ex_config.smod_val=SMOD_1;
    }
    else if(uart_ex_config.baud_rate==UART0_BAUDRATE_500K)
    {
      uart_ex_config.mode=UART_EX_MD_2;
      uart_ex_config.smod_val=SMOD_0;
    }
    else if(uart_ex_config.baud_rate==UART0_BAUDRATE_1M)
    {
      uart_ex_config.mode=UART_EX_MD_2;
      uart_ex_config.smod_val=SMOD_1;
    }
  #if defined(PIN_UART0_RX)  
    uart_ex_config.rx_cb=uart0_rx_data_handler;
  #else
    uart_ex_config.rx_cb=NULL;
  #endif  
    //也可以配置发送回调,此处未定义
    uart_ex_config.tx_cb=NULL;    
    //打开串口外设
    uart_ex_open(HS_UART0, &uart_ex_config);
    //发送测试, 可删除
    uint32_t tx_idx;
    const char tx_string[]="UART0 Enable\n";
    for(tx_idx=0; tx_idx<strlen(tx_string); tx_idx++)
    {
      uart_ex_send_block(HS_UART0, (uint8_t *)&tx_string[tx_idx], 1);
    }
    //如果开启了接收,则必须关闭芯片休眠功能
    #if defined(PIN_UART0_RX)  
      co_power_sleep_enable(false);
    #else
      co_power_sleep_enable(true);
    #endif      
}


OM6621E_UART0_ example202305058161.c
被收藏 1  ∙  1 赞  
加入收藏
点赞
3 回复  
善言善语 (您需要 登录 后才能回复 没有账号 ?)

请先登录网站