siliconlabs EFR32如何使用串口调试

看完原理图后,首先解决的是串口打印调试,这是嵌入式编译第一步要做的事情,于是我自己建立了一个demo工程,在demo的基础上进行修改添加,
1.先来看代码的如何实现函数。
siliconlabs EFR32如何使用串口调试_第1张图片
2.进入函数“halnit();”
siliconlabs EFR32如何使用串口调试_第2张图片
3.这个函数主要是做硬件初始化的函数,其他的地方不予要修改,只是做注释就好,我们自己添加一个硬件初始化函数"HardwareAdapterInit();"
siliconlabs EFR32如何使用串口调试_第3张图片
4.进入到此函数中我们可以看到一些IO的初始化定义
siliconlabs EFR32如何使用串口调试_第4张图片
上面可以看到关于UART1的端口定义,这个地方就不细说了。
5.下面的几个函数也是我们自己定义的,主要是硬件的驱动程序“ledinit”“halinternalinitbutton_key”.
6.现在问题来了,我们如何将打印信息从定义的端口输出,这个可以看芯片手册,通过寄存器的配置,让此端口映射到系统的UART1上即可。
这个需要我们做出的改动在"efr32"文件夹下的“uartdrv.c”中的

UARTDRV_InitUart(UARTDRV_Handle_t handle,
                         const UARTDRV_InitUart_t *initData)”函数中,

  Ecode_t retVal;
  USART_InitAsync_TypeDef usartInit = USART_INITASYNC_DEFAULT;

  if (handle == NULL)
  {
    return ECODE_EMDRV_UARTDRV_ILLEGAL_HANDLE;
  }
  if (initData == NULL)
  {
    return ECODE_EMDRV_UARTDRV_PARAM_ERROR;
  }

  memset(handle, 0, sizeof(UARTDRV_HandleData_t));

#if (EMDRV_UARTDRV_HW_FLOW_CONTROL_ENABLE)
  retVal = SetHandleIndex(handle);
  if (retVal != ECODE_EMDRV_UARTDRV_OK)
  {
    return retVal;
  }
  handle->fcType = initData->fcType;
#else
  // Force init data to uartdrvFlowControlNone if flow control is excluded by EMDRV_UARTDRV_HW_FLOW_CONTROL_ENABLE
  handle->fcType = uartdrvFlowControlNone;
#endif

  handle->peripheral.uart = initData->port;
  handle->type = uartdrvUartTypeUart;

  // Set clocks and DMA requests according to available peripherals
  if (false)
  {
#if defined(USART0)
  }
  else if (initData->port == USART0)
  {
    handle->uartClock   = cmuClock_USART0;
    handle->txDmaSignal = dmadrvPeripheralSignal_USART0_TXBL;
    handle->rxDmaSignal = dmadrvPeripheralSignal_USART0_RXDATAV;
#endif
#if defined(USART1)
  }
  else if (initData->port == USART1)
  {
    handle->uartClock   = cmuClock_USART1;
    handle->txDmaSignal = dmadrvPeripheralSignal_USART1_TXBL;
    handle->rxDmaSignal = dmadrvPeripheralSignal_USART1_RXDATAV;
#endif
#if defined(USART2)
  }
  else if (initData->port == USART2)
  {
    handle->uartClock   = cmuClock_USART2;
    handle->txDmaSignal = dmadrvPeripheralSignal_USART2_TXBL;
    handle->rxDmaSignal = dmadrvPeripheralSignal_USART2_RXDATAV;
#endif
#if defined(UART0)
  }
  else if (initData->port == UART0)
  {
    handle->uartClock   = cmuClock_UART0;
    handle->txDmaSignal = dmadrvPeripheralSignal_UART0_TXBL;
    handle->rxDmaSignal = dmadrvPeripheralSignal_UART0_RXDATAV;
#endif
#if defined(UART1)
  }
  else if (initData->port == UART1)
  {
    handle->uartClock   = cmuClock_UART1;
    handle->txDmaSignal = dmadrvPeripheralSignal_UART1_TXBL;
    handle->rxDmaSignal = dmadrvPeripheralSignal_UART1_RXDATAV;
#endif
#if defined(UART2)
  }
  else if (initData->port == UART2)
  {
    handle->uartClock   = cmuClock_UART2;
    handle->txDmaSignal = dmadrvPeripheralSignal_UART2_TXBL;
    handle->rxDmaSignal = dmadrvPeripheralSignal_UART2_RXDATAV;
#endif
  }
  else
  {
    return ECODE_EMDRV_UARTDRV_PARAM_ERROR;
  }

  InitializeQueues(handle, initData->rxQueue, initData->txQueue);

  usartInit.baudrate = initData->baudRate;
  usartInit.stopbits = initData->stopBits;
  usartInit.parity = initData->parity;
  usartInit.oversampling = initData->oversampling;
#if defined(USART_CTRL_MVDIS)
  usartInit.mvdis = initData->mvdis;
#endif

  // UARTDRV is fixed at 8 bit frames.
  usartInit.databits = (USART_Databits_TypeDef)USART_FRAME_DATABITS_EIGHT;

  // Enable clocks
  CMU_ClockEnable(cmuClock_HFPER, true);
  CMU_ClockEnable(cmuClock_GPIO, true);
  CMU_ClockEnable(handle->uartClock, true);

  // Init U(S)ART to default async config.
  // RX/TX enable is done on demand
  usartInit.enable = usartDisable;
  USART_InitAsync(initData->port, &usartInit);

  GPIO_PinModeSet(UART1_TX_GPIOPORT, UART1_TX_GPIOPIN, gpioModePushPull, 1);//161129-guo-change
#if defined(USART_ROUTEPEN_TXPEN)
  initData->port->ROUTEPEN = USART_ROUTEPEN_TXPEN;
//                             | USART_ROUTEPEN_RXPEN;

//  initData->port->ROUTELOC0 = (initData->port->ROUTELOC0
//                               & ~(_USART_ROUTELOC0_TXLOC_MASK
//                                   | _USART_ROUTELOC0_RXLOC_MASK))
//                              | (initData->portLocationTx
//                                 << _USART_ROUTELOC0_TXLOC_SHIFT)
//                              | (initData->portLocationRx
//                                 << _USART_ROUTELOC0_RXLOC_SHIFT);
  initData->port->ROUTELOC0 = (USART_ROUTELOC0_TXLOC << _USART_ROUTELOC0_TXLOC_SHIFT);
//                              | (11<< _USART_ROUTELOC0_RXLOC_SHIFT);

//#if defined(USART_ROUTEPEN_TXPEN)
//  initData->port->ROUTEPEN = USART_ROUTEPEN_TXPEN
//                             | USART_ROUTEPEN_RXPEN;
//  initData->port->ROUTELOC0 = (initData->port->ROUTELOC0
//                               & ~(_USART_ROUTELOC0_TXLOC_MASK
//                                   | _USART_ROUTELOC0_RXLOC_MASK))
//                              | (initData->portLocationTx
//                                 << _USART_ROUTELOC0_TXLOC_SHIFT)
//                              | (initData->portLocationRx
//                                 << _USART_ROUTELOC0_RXLOC_SHIFT);
#else
  initData->port->ROUTE = USART_ROUTE_TXPEN
                          | USART_ROUTE_RXPEN
                          | (initData->portLocation
                             << _USART_ROUTE_LOCATION_SHIFT);
#endif

//  if ((retVal = SetupGpioUart(handle, initData)) != ECODE_EMDRV_UARTDRV_OK)
//  {
//    return retVal;
//  }
//  if ((retVal = ConfigGpio(handle, true)) != ECODE_EMDRV_UARTDRV_OK)
//  {
//    return retVal;
//  }

  INT_Disable();

  // Configure hardware flow control pins and interrupt vectors
#if (EMDRV_UARTDRV_HW_FLOW_CONTROL_ENABLE)
  if (initData->fcType == uartdrvFlowControlHwUart)
  {
#if defined(UART_HW_FLOW_CONTROL_SUPPORT)
//    initData->port->ROUTEPEN |= USART_ROUTEPEN_CTSPEN | USART_ROUTEPEN_RTSPEN;
//    initData->port->CTRLX    |= USART_CTRLX_CTSEN;
//    initData->port->ROUTELOC1 = (initData->portLocationCts << _USART_ROUTELOC1_CTSLOC_SHIFT)
//                                | (initData->portLocationRts << _USART_ROUTELOC1_RTSLOC_SHIFT);
#else
    // Attempting to use USART hardware flow control on a device that does not
    // support it.
    return ECODE_EMDRV_UARTDRV_PARAM_ERROR;
#endif
  }
//  else if (initData->fcType == uartdrvFlowControlHw)
//  {
//    InitializeGpioFlowControl(handle);
//  }
#endif

  // Clear any false IRQ/DMA request
  USART_IntClear(initData->port, ~0x0);

  // Enable TX permanently as the TX circuit consumes very little energy.
  // RX is enabled on demand as the RX circuit consumes some energy due to
  // continuous (over)sampling.
  if (initData->fcType == uartdrvFlowControlHwUart)
  {
    // RX must be enabled permanently when using USART hw flow control
    USART_Enable(initData->port, usartEnable);
  }
  else
  {
    USART_Enable(initData->port, usartEnableTx);
  }

  // Discard false frames and/or IRQs
  initData->port->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX;

  // Initialize DMA.
  DMADRV_Init();

  retVal = InitializeDma(handle);

  INT_Enable();

  if (retVal != ECODE_EMDRV_UARTDRV_OK)
  {
    return retVal;
  }

  return ECODE_EMDRV_UARTDRV_OK;

看了以上代码估计是蒙的,把之前的寄存器配置注释后,只做了一点修改如下图所示
siliconlabs EFR32如何使用串口调试_第5张图片
对寄存器的操作,映射到我需要的端口去。

然后在打印中用此函数“emberAfCorePrintln”即可进行打印。

你可能感兴趣的:(siliconlabs EFR32如何使用串口调试)