Ejemplo n.º 1
0
/******************************************************************************
 * @brief  Main function
 *
 *****************************************************************************/
int main(void)
{
  /* Initialize chip - handle erratas */
  CHIP_Init( );

  /* Initialize clocks and oscillators */
  cmuSetup( );

  /* Initialize UART peripheral */
  uartSetup( );

  /* Initialize Development Kit in EBI mode */
  BSP_Init(BSP_INIT_DEFAULT);

  /* Enable RS-232 transceiver on Development Kit */
  BSP_PeripheralAccess(BSP_RS232_UART, true);

  /* When DVK is configured, and no more DVK access is needed, the interface can safely be disabled to save current */
  BSP_Disable();


  /* Write welcome message to UART */
  uartPutData((uint8_t*) welcomeString, welLen);

  /*  Eternal while loop
   *  CPU will sleep during Rx and Tx. When a byte is transmitted, an interrupt
   *  wakes the CPU which copies the next byte in the txBuf queue to the
   *  UART TXDATA register.
   *
   *  When the predefined termiation character is received, the all pending
   *  data in rxBuf is copied to txBuf and echoed back on the UART */
  while (1)
  {
    /* Wait in EM1 while UART transmits */
    EMU_EnterEM1();

    /* Check if RX buffer has overflowed */
    if (rxBuf.overflow)
    {
      rxBuf.overflow = false;
      uartPutData((uint8_t*) overflowString, ofsLen);
    }

    /* Check if termination character is received */
    if (rxBuf.data[(rxBuf.wrI - 1) % BUFFERSIZE] == TERMINATION_CHAR)
    {
      /* Copy received data to UART transmit queue */
      uint8_t tmpBuf[BUFFERSIZE];
      int     len = uartGetData(tmpBuf, 0);
      uartPutData(tmpBuf, len);
    }
  }
}
Ejemplo n.º 2
0
/******************************************************************************
* @brief  usartSetup function
*
******************************************************************************/
void usartSetup(void)
{
  cmuSetup();
  
  /* Configure GPIO pin as open drain */
  GPIO_PinModeSet(SC_GPIO_DATA_PORT, SC_GPIO_DATA_PIN, gpioModeWiredAndPullUp, 1);

  /* Prepare struct for initializing USART in asynchronous mode*/
  usartInit.enable       = usartDisable;   /* Don't enable USART upon intialization */
  usartInit.refFreq      = 0;              /* Provide information on reference frequency. When set to 0, the reference frequency is */
  usartInit.baudrate     = SC_BAUD_RATE;   /* Baud rate */
  usartInit.oversampling = usartOVS16;     /* Oversampling. Range is 4x, 6x, 8x or 16x */
  usartInit.databits     = usartDatabits8; /* Number of data bits. Range is 4 to 10 */
  usartInit.parity       = usartEvenParity;  /* Parity mode */
  usartInit.stopbits     = usartStopbits1p5; /* Number of stop bits. Range is 0 to 2, 1.5 for smartcard. */
  usartInit.mvdis        = false;          /* Disable majority voting */
  usartInit.prsRxEnable  = false;          /* Enable USART Rx via Peripheral Reflex System */
  usartInit.prsRxCh      = usartPrsRxCh0;  /* Select PRS channel if enabled */

  /* Initialize USART with usartInit struct */
  USART_InitAsync(usart, &usartInit);
  
  /* Smart card specific settings for T0 mode. */
  usart->CTRL |= USART_CTRL_SCMODE | USART_CTRL_AUTOTRI | USART_CTRL_LOOPBK;

  /* Prepare USART Rx interrupt */
  USART_IntClear(usart, _USART_IF_MASK);
  USART_IntEnable(usart, USART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(USART1_RX_IRQn);
  NVIC_EnableIRQ(USART1_RX_IRQn);
  
  /* Enable I/O pins at USART1 location #2 */
  usart->ROUTE = USART_ROUTE_TXPEN | SC_USART_LOCATION;

  /* Disable reception before enabling uart to discard erroneus stuff while card is unpowered. */
  usartFlushBuffer();
  usartAcceptRX(false);
  
  /* Enable USART */
  USART_Enable(usart, usartEnable);
}