示例#1
0
/**
  * @brief  Configure USART1.
  * @param  None
  * @retval None
  */
void USART1_Configuration(void)
{
    GPIO_InitPara GPIO_InitStructure;
    USART_InitPara USART_InitStructure;

    /* USART1 GPIO Configuration */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure); 


    /* Configure the USART1*/ 
    USART_InitStructure.USART_BRR = 115200;
    USART_InitStructure.USART_WL = USART_WL_8B;
    USART_InitStructure.USART_STBits = USART_STBITS_1;
    USART_InitStructure.USART_Parity = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;

    USART_Init(USART1, &USART_InitStructure);
    USART_Enable(USART1, ENABLE);
}
示例#2
0
/***************************************************************************//**
 * @brief
 *   Init USART for I2S mode.
 *
 * @details
 *   This function will configure basic settings in order to operate in I2S
 *   mode.
 *
 *   Special control setup not covered by this function must be done after
 *   using this function by direct modification of the CTRL and I2SCTRL
 *   registers.
 *
 *   Notice that pins used by the USART module must be properly configured
 *   by the user explicitly, in order for the USART to work as intended.
 *   (When configuring pins, one should remember to consider the sequence of
 *   configuration, in order to avoid unintended pulses/glitches on output
 *   pins.)
 *
 * @param[in] usart
 *   Pointer to USART peripheral register block. (UART does not support this
 *   mode.)
 *
 * @param[in] init
 *   Pointer to initialization structure used to configure basic I2S setup.
 *
 * @note
 *   This function does not apply to all USART's. Refer to chip manuals.
 *
 ******************************************************************************/
void USART_InitI2s(USART_TypeDef *usart, USART_InitI2s_TypeDef *init)
{
  USART_Enable_TypeDef enable;

  /* Make sure the module exists on the selected chip */
  EFM_ASSERT(USART_I2S_VALID(usart));

  /* Override the enable setting. */
  enable            = init->sync.enable;
  init->sync.enable = usartDisable;

  /* Init USART as a sync device. */
  USART_InitSync(usart, &init->sync);

  /* Configure and enable I2CCTRL register acording to selected mode. */
  usart->I2SCTRL = ((uint32_t) init->format) |
                   ((uint32_t) init->justify) |
                   (init->delay    ? USART_I2SCTRL_DELAY    : 0) |
                   (init->dmaSplit ? USART_I2SCTRL_DMASPLIT : 0) |
                   (init->mono     ? USART_I2SCTRL_MONO     : 0) |
                   (USART_I2SCTRL_EN);

  if (enable != usartDisable)
  {
    USART_Enable(usart, enable);
  }
}
示例#3
0
/**
 * Initialize the UART.
 *
 */
void
usart2_init(unsigned long baudrate)
{
  USART_InitAsync_TypeDef init =   USART_INITASYNC_DEFAULT;
  /* Configure controller */

  // Enable clocks
  CMU_ClockEnable(cmuClock_HFPER, true);
  CMU_ClockEnable(cmuClock_USART2, true);

  init.enable = usartDisable;
  init.baudrate = baudrate;
  USART_InitAsync(USART2, &init);

  /* Enable pins at USART2 location */
  USART2->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN |
                  (USART2_LOCATION << _USART_ROUTE_LOCATION_SHIFT);

  /* Clear previous RX interrupts */
  USART_IntClear(USART2, USART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(USART2_RX_IRQn);

  /* Enable RX interrupts */
  USART_IntEnable(USART2, USART_IF_RXDATAV);
  NVIC_EnableIRQ(USART2_RX_IRQn);

  /* Finally enable it */
  USART_Enable(USART2, usartEnable);
}
示例#4
0
/***************************************************************************//**
 * @brief
 *   Initialize USART device
 *
 * @details
 *
 * @note
 *
 * @param[in] dev
 *   Pointer to device descriptor
 *
 * @return
 *   Error code
 ******************************************************************************/
static rt_err_t rt_usart_init (rt_device_t dev)
{
    struct efm32_usart_device_t *usart;

    usart = (struct efm32_usart_device_t *)(dev->user_data);

    if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
    {
        if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
        {
            struct efm32_usart_dma_mode_t *dma_tx;

            dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);

            usart->state |= USART_STATE_RX_BUSY;
        }

        if (dev->flag & RT_DEVICE_FLAG_INT_RX)
        {
            struct efm32_usart_int_mode_t *int_rx;

            int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);

            int_rx->data_ptr = RT_NULL;
        }

        /* Enable USART */
        USART_Enable(usart->usart_device, usartEnable);

        dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
    }

    return RT_EOK;
}
示例#5
0
文件: main.c 项目: eeinz/trochili
/**
  * @brief  Start Bit Method to Wake Up USART from DeepSleep mode Test.
  * @param  None
  * @retval None
  */
static void WakeUp_StartBitMethod(void)
{ 
    /* Configure the wake up Method = Start bit */
    USART_DEEPSLEEPModeWakeUpSourceConfig(USART1, USART_WAKEUPSOURCE_STARTBIT);
    
    /* Enable USART1 */ 
    USART_Enable(USART1, ENABLE);
    
    /* Before entering the USART in STOP mode the REACK flag must be checked to ensure the USART RX is ready */
    while(USART_GetBitState(USART1, USART_FLAG_REA) == RESET)
    {}
    
    /* Enable USART STOP mode by setting the UESM bit in the CTLR1 register.*/
    USART_DEEPSLEEPModeEnable(USART1, ENABLE);
    
    /* Enable the wake up from stop Interrupt */ 
    USART_INT_Set(USART1, USART_INT_WU, ENABLE);   
    
    /* Enable PWR APB clock */
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_PWR, ENABLE);
    
    /*Enter Deep-sleep mode*/
    PWR_DEEPSLEEPMode_Entry(PWR_LDO_LOWPOWER, PWR_DEEPSLEEPENTRY_WFI);
    
    /* Waiting Wake Up interrupt */
    while(InterruptCounter == 0x00)
    {}
    
    /* Disable USART peripheral in DEEPSLEEP mode */ 
    USART_DEEPSLEEPModeEnable(USART1, DISABLE);
    
    while(USART_GetBitState(USART1, USART_FLAG_RBNE) == RESET)
    {}
    DataReceived = USART_DataReceive(USART1);
    
    /* Clear the TEN bit (if a transmission is on going or a data is in the TDR, it will be sent before
    efectivelly disabling the transmission) */
    USART_TransferDirection_Enable(USART1, USART_RXORTX_TX, DISABLE);
    
    /* Check the Transfer Complete Flag */
    while (USART_GetBitState(USART1, USART_FLAG_TC) == RESET)
    {}
    
    /* USART Disable */
    USART_Enable(USART1, DISABLE);
}
示例#6
0
/**************************************************************************//**
 * @brief   Shutdown the PAL SPI interface
 *
 * @detail  This function releases/stops all resources used by the
 *          PAL SPI interface functions.
 *
 * @return  EMSTATUS code of the operation.
 *****************************************************************************/
EMSTATUS PAL_SpiShutdown (void)
{
  EMSTATUS status = PAL_EMSTATUS_OK;

  /* Disable the USART device used for SPI. */
  USART_Enable( PAL_SPI_USART_UNIT, usartDisable);

  /* Disable the USART clock. */
  CMU_ClockEnable( PAL_SPI_USART_CLOCK, false );

  return status;
}
/*
 *  Hardware initialization for the radio connection
 */
void CC1101_Radio::initializeInterface()
{
	CMU_ClockEnable( cmuClock_USART0, true );

	USART_InitSync( cc1101USART, &interfaceInit );
	USART_Enable( cc1101USART, usartEnable );

	cc1101USART->ROUTE = USART_ROUTE_RXPEN | // Enable the MISO-Pin
						 USART_ROUTE_TXPEN |   // Enable the MOSI-Pin
						 USART_ROUTE_CLKPEN |  // Enable the SPI-Clock Pin
						 cc1101Location;

	GPIO_PinModeSet( _CC1101_SPI_MOSI_PIN, gpioModePushPull, 1 );
	GPIO_PinModeSet( _CC1101_SPI_MISO_PIN, gpioModeInputPull, 1 );
	GPIO_PinModeSet( _CC1101_SPI_CLK_PIN, gpioModePushPull, 1 );
	GPIO_PinModeSet( _CC1101_SPI_CS_PIN, gpioModePushPull, 1 );
}
示例#8
0
/**************************************************************************//**
 * @brief Intializes UART/LEUART
 *****************************************************************************/
void UART1_SerialInit(void)
{
  /* Configure GPIO pins */
  CMU_ClockEnable(cmuClock_GPIO, true);
  /* To avoid false start, configure output as high */
  GPIO_PinModeSet(gpioPortB, 9, gpioModePushPull, 1);
  GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0);

  USART_TypeDef           *usart = UART1;
  USART_InitAsync_TypeDef init   = USART_INITASYNC_DEFAULT;

  /* Enable EFM32GG_DK3750 RS232/UART switch */
  BSP_PeripheralAccess(BSP_RS232_UART, true);

  /* Enable peripheral clocks */
  CMU_ClockEnable(cmuClock_HFPER, true);
  CMU_ClockEnable(cmuClock_UART1, true);

  /* Configure USART for basic async operation */
  init.enable = usartDisable;
  USART_InitAsync(usart, &init);

  /* Enable pins at UART1 location #2 */
  usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | USART_ROUTE_LOCATION_LOC2;

  /* Clear previous RX interrupts */
  USART_IntClear(UART1, USART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(UART1_RX_IRQn);

  /* Enable RX interrupts */
  USART_IntEnable(UART1, USART_IF_RXDATAV);
  NVIC_EnableIRQ(UART1_RX_IRQn);

  /* Finally enable it */
  USART_Enable(usart, usartEnable);

#if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
  setvbuf(stdout, NULL, _IONBF, 0);   /*Set unbuffered mode for stdout (newlib)*/
#endif

  initialized = true;
}
示例#9
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);
}
示例#10
0
void serial_init(void)
{

		
    //RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA , ENABLE );   

    RCC_APB1PeriphClock_Enable( RCC_APB1PERIPH_USART2 , ENABLE );
	
		GPIO_InitPara GPIO_InitStructure;

		GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE2, GPIO_AF_1 );    
		GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE3, GPIO_AF_1 ); 

		GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_2 | GPIO_PIN_3;
		GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_AF;
		GPIO_InitStructure.GPIO_Speed   = GPIO_SPEED_50MHZ;
		GPIO_InitStructure.GPIO_OType   = GPIO_OTYPE_PP;
		GPIO_InitStructure.GPIO_PuPd    = GPIO_PUPD_NOPULL;
		GPIO_Init( GPIOA , &GPIO_InitStructure); 



		USART_InitPara USART_InitStructure;
		
		USART_InitStructure.USART_BRR           = 57600;
		USART_InitStructure.USART_WL            = USART_WL_8B;
		USART_InitStructure.USART_STBits        = USART_STBITS_1;
		USART_InitStructure.USART_Parity        = USART_PARITY_RESET;
		USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
		USART_InitStructure.USART_RxorTx        = USART_RXORTX_TX;
		USART_Init(USART2, &USART_InitStructure);

    USART_Enable(USART2, ENABLE);
		
	  USART_INT_Set(USART2, USART_INT_TC , ENABLE); // USART_INT_TBE
	


}
示例#11
0
void EvbUart2Config(void)
{
    /* Configure the GPIO ports */
    GPIO_InitPara  GPIO_InitStructure;
    USART_InitPara  USART_InitStructure;

    /* Enable GPIO clock */
    RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOA, ENABLE);

    /* Enable USART APB clock */
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_USART2, ENABLE);

    /* Connect PXx to USARTx_Tx */
    GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE2, GPIO_AF_1);

    /* Connect PXx to USARTx_Rx */
    GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE3, GPIO_AF_1);

    /* Configure USART Rx/Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_2 | GPIO_PIN_3;
    GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_AF;
    GPIO_InitStructure.GPIO_Speed   = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_OType   = GPIO_OTYPE_PP;
    GPIO_InitStructure.GPIO_PuPd    = GPIO_PUPD_NOPULL;
    GPIO_Init(GPIOA , &GPIO_InitStructure);

    USART_DeInit( USART2 );

    USART_InitStructure.USART_BRR                 = 115200;
    USART_InitStructure.USART_WL                  = USART_WL_8B;
    USART_InitStructure.USART_STBits              = USART_STBITS_1;
    USART_InitStructure.USART_Parity              = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx              = USART_RXORTX_RX | USART_RXORTX_TX;
    USART_Init(USART2, &USART_InitStructure);

    /* USART enable */
    USART_Enable(USART2, ENABLE);
}
示例#12
0
文件: main.c 项目: eeinz/trochili
/**
  * @brief Configure the USART Device
  * @param  None
  * @retval None
  */
static void USART_Configuration(void)
{ 
    USART_InitPara USART_InitStructure;

    /* Configure the HSI as USART clock */
    RCC_USARTCLKConfig(RCC_USART1CLK_HSI);
    
    /* USARTx configured as follow:
    - BaudRate = 115200 baud  
    - Word Length = 8 Bits
    - Stop Bit = 1 Stop Bit
    - Parity = No Parity
    - Hardware flow control disabled (RTS and CTS signals)
    - Receive and transmit enabled
    */
    USART_DeInit(USART1);
    USART_InitStructure.USART_BRR = 115200;
    USART_InitStructure.USART_WL = USART_WL_8B;
    USART_InitStructure.USART_STBits = USART_STBITS_1;
    USART_InitStructure.USART_Parity = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;

    GD_EVAL_COMInit(&USART_InitStructure);
    /* USART Disable */
    USART_Enable(USART1, DISABLE);
    /* USART1 IRQ Channel configuration */
    {
        NVIC_InitPara NVIC_InitStructure;
        
        /* Enable the USARTx Interrupt */
        NVIC_InitStructure.NVIC_IRQ                = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
        NVIC_InitStructure.NVIC_IRQSubPriority     = 0;
        NVIC_InitStructure.NVIC_IRQEnable          = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
    }
}
示例#13
0
/******************************************************************************
* @brief  uartSetup function
*
******************************************************************************/
void uartSetup(void)
{
  /* Enable clock for GPIO module (required for pin configuration) */
  CMU_ClockEnable(cmuClock_GPIO, true);
  /* Configure GPIO pins */
  GPIO_PinModeSet(gpioPortB, 9, gpioModePushPull, 1);
  GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0);


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

  /* Initialize USART with uartInit struct */
  USART_InitAsync(uart, &uartInit);

  /* Prepare UART Rx and Tx interrupts */
  USART_IntClear(uart, _UART_IF_MASK);
  USART_IntEnable(uart, UART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(UART1_RX_IRQn);
  NVIC_ClearPendingIRQ(UART1_TX_IRQn);
  NVIC_EnableIRQ(UART1_RX_IRQn);
  NVIC_EnableIRQ(UART1_TX_IRQn);

  /* Enable I/O pins at UART1 location #2 */
  uart->ROUTE = UART_ROUTE_RXPEN | UART_ROUTE_TXPEN | UART_ROUTE_LOCATION_LOC2;

  /* Enable UART */
  USART_Enable(uart, usartEnable);
}
/**************************************************************************//**
 * @brief  Setup SPI as Master
 *****************************************************************************/
void setupSpi(void)
{
  USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT;  
  
  /* Initialize SPI */
  usartInit.databits = usartDatabits8;
  usartInit.baudrate = 1000000;
  USART_InitSync(USART1, &usartInit);
  
  /* Turn on automatic Chip Select control */
  USART1->CTRL |= USART_CTRL_AUTOCS;
  
  /* Enable SPI transmit and receive */
  USART_Enable(USART1, usartEnable);
  
  /* Configure GPIO pins for SPI */
  GPIO_PinModeSet(gpioPortD, 0, gpioModePushPull, 0); /* MOSI */
  GPIO_PinModeSet(gpioPortD, 1, gpioModeInput,    0); /* MISO */
  GPIO_PinModeSet(gpioPortD, 2, gpioModePushPull, 0); /* CLK */	
  GPIO_PinModeSet(gpioPortD, 3, gpioModePushPull, 1); /* CS */	
 
  /* Enable routing for SPI pins from USART to location 1 */
  USART1->ROUTE = USART_ROUTE_TXPEN | 
                  USART_ROUTE_RXPEN | 
                  USART_ROUTE_CSPEN |
                  USART_ROUTE_CLKPEN | 
                  USART_ROUTE_LOCATION_LOC1;
  
  /* Configure interrupt for TX/RX, but do not enable them */
  /* Interrupts will be enabled only for reading last 3 bytes
   * when using AUTOTX */
  NVIC_ClearPendingIRQ(USART1_RX_IRQn);
  NVIC_EnableIRQ(USART1_RX_IRQn);
  NVIC_ClearPendingIRQ(USART1_TX_IRQn);
  NVIC_EnableIRQ(USART1_TX_IRQn);
}
/**************************************************************************//**
 * @brief  Setup SPI as Master
 *****************************************************************************/
void setupSpi(void)
{
    USART_InitSync_TypeDef usartInit = USART_INITSYNC_DEFAULT;  

    /* Initialize SPI */
    usartInit.databits  = usartDatabits8;   /* 8 bits of data */
    usartInit.baudrate  = SPI_BAUDRATE;     /* Clock frequency */
    usartInit.master    = true;             /* Master mode */
    usartInit.msbf      = true;             /* Most Significant Bit first */
    usartInit.clockMode = usartClockMode0;  /* Clock idle low, sample on rising edge */

    USART_InitSync(SPI_CHANNEL, &usartInit);

    /* Enable SPI transmit and receive */
    USART_Enable(SPI_CHANNEL, usartEnable);

    /* Configure GPIO pins for SPI */
    //GPIO_PinModeSet(SPI_PORT_MOSI,  SPI_PIN_MOSI,   gpioModePushPull,   0); /* MOSI */
    //GPIO_PinModeSet(SPI_PORT_MISO,  SPI_PIN_MISO,   gpioModeInput,      0); /* MISO */
    //GPIO_PinModeSet(SPI_PORT_CLK,   SPI_PIN_CLK,    gpioModePushPull,   0); /* CLK */
    //GPIO_PinModeSet(SPI_PORT_CS,    SPI_PIN_CS,     gpioModePushPull,   1); /* CS */
    //edit: do this via the hw_gpio_configure_pin interface to signal that the pins are in use
    //note: normally this should be done in the platform-specific initialisation code BUT, since this is a driver for a device (uart) that is
    //an integral part of the MCU we are certain this code will NOT be used in combination with a different MCU so we can do this here
    error_t err;
    err = hw_gpio_configure_pin(SPI_PIN_MOSI, false,   gpioModePushPull,   0); assert(err == SUCCESS); /* MOSI */
    err = hw_gpio_configure_pin(SPI_PIN_MISO, false,   gpioModeInput,      0); assert(err == SUCCESS); /* MISO */
    err = hw_gpio_configure_pin(SPI_PIN_CLK, false,    gpioModePushPull,   0); assert(err == SUCCESS); /* CLK */
    err = hw_gpio_configure_pin(SPI_PIN_CS, false,     gpioModePushPull,   1); assert(err == SUCCESS); /* CS */

    /* Enable routing for SPI pins from USART to location 1 */
    SPI_CHANNEL->ROUTE =  USART_ROUTE_TXPEN |
                          USART_ROUTE_RXPEN |
                          USART_ROUTE_CLKPEN |
                          SPI_ROUTE_LOCATION;
}
示例#16
0
文件: main.c 项目: LonelyWolf/stm32
int main(void) {
	// Initialize the MCU clock system
	SetSysClock();
	SystemCoreClockUpdate();


	// Prefetch is useful if at least one wait state is needed to access the Flash memory
	if (RCC_GetLatency() != FLASH_ACR_LATENCY_0WS) {
		// Enable prefetch buffer (a.k.a ART)
		RCC_PrefetchEnable();
	}


	// Initialize debug output port (USART2)
	//   clock source: SYSCLK
	//   mode: transmit only
	USART2_HandleInit();
	RCC_SetClockUSART(RCC_USART2_CLK_SRC, RCC_PERIPH_CLK_SYSCLK);
	USART_Init(&hUSART2, USART_MODE_TX);

	// Configure USART:
	//   oversampling by 16
	//   115200, 8-N-1
	//   no hardware flow control
	USART_SetOversampling(&hUSART2, USART_OVERS16);
	USART_SetBaudRate(&hUSART2, 115200);
	USART_SetDataMode(&hUSART2, USART_DATAWIDTH_8B, USART_PARITY_NONE, USART_STOPBITS_1);
	USART_SetHWFlow(&hUSART2, USART_HWCTL_NONE);
	USART_Enable(&hUSART2);
	USART_CheckIdleState(&hUSART2, 0xC5C10); // Timeout of about 100ms at 80MHz CPU


	// Say "hello world" into the USART port
	RCC_ClocksTypeDef Clocks;
	RCC_GetClocksFreq(&Clocks);
	printf("---STM32L476RG---\r\n");
	printf("STM32L476 Template (%s @ %s)\r\n", __DATE__, __TIME__);
	printf("CPU: %.3uMHz\r\n", SystemCoreClock / 1000);
	printf("SYSCLK: %.3uMHz, HCLK: %.3uMHz\r\n", Clocks.SYSCLK_Frequency / 1000, Clocks.HCLK_Frequency / 1000);
	printf("APB1: %.3uMHz, APB2: %.3uMHz\r\n", Clocks.PCLK1_Frequency / 1000, Clocks.PCLK2_Frequency / 1000);
	printf("System clock: %s\r\n", _sysclk_src_str[RCC_GetSysClockSource()]);
#if 0
	// DEV: 0x461 = STM32L496xx/4A6xx
	//      0x415 = STM32L475xx/476xx/486xx devices
	// REV: 0x1000: Rev 1 for STM32L475xx/476xx/486xx devices
	//              Rev A for STM32L496xx/4A6xx devices
	//      0x1001: Rev 2 for STM32L475xx/476xx/486xx devices
	//              Rev B for STM32L496xx/4A6xx devices
	//      0x1003: Rev 3 for STM32L475xx/476xx/486xx devices
	//      0x1007: Rev 4 for STM32L475xx/476xx/486xx devices
	printf("MCU: DEV=%03X REV=%04X FLASH=%uKB ID=%08X%08X%08X\r\n",
			DBGMCU->IDCODE & 0xFFFU, // DEV_ID
			DBGMCU->IDCODE >> 16, // REV_ID
			(uint16_t)(*(volatile uint32_t*)(FLASHSIZE_BASE)), // Flash size in kilobytes
			*(volatile uint32_t*)(UID_BASE), // Unique ID 96 bits
			*(volatile uint32_t*)(UID_BASE + 4U),
			*(volatile uint32_t*)(UID_BASE + 8U)
		);
#endif // MCU model


	// Initialize delay functions
	Delay_Init();


	// Initialize the PA5 pin (LED on the Nucleo board)
	// Enable the GPIOA peripheral
	RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
	// Configure PA5 as push-pull output without pull-up, at lowest speed
	GPIO_set_mode(GPIOA, GPIO_Mode_OUT, GPIO_PUPD_NONE, GPIO_PIN_5);
	GPIO_out_cfg(GPIOA, GPIO_OT_PP, GPIO_SPD_LOW, GPIO_PIN_5);


	// The main loop
	while (1) {
		// Invert the PA5 pin state every half of second
		Delay_ms(500);
		GPIO_PIN_INVERT(GPIOA, GPIO_PIN_5);
	}
}
示例#17
0
/***************************************************************************//**
* @brief
*   Configure USART device
*
* @details
*
* @note
*
* @param[in] dev
*   Pointer to device descriptor
*
* @param[in] cmd
*   IIC control command
*
* @param[in] args
*   Arguments
*
* @return
*   Error code
******************************************************************************/
static rt_err_t rt_usart_control (
    rt_device_t     dev,
    rt_uint8_t      cmd,
    void            *args)
{
    RT_ASSERT(dev != RT_NULL);

    rt_err_t    err_code;
    struct efm32_usart_device_t *usart;

    usart = (struct efm32_usart_device_t *)(dev->user_data);

    /* Lock device */
    if (rt_hw_interrupt_check())
    {
        err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
    }
    else
    {
        err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
    }
    if (err_code != RT_EOK)
    {
        return err_code;
    }

    switch (cmd)
    {
    case RT_DEVICE_CTRL_SUSPEND:
        /* Suspend device */
        dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
        USART_Enable(usart->usart_device, usartDisable);
        break;

    case RT_DEVICE_CTRL_RESUME:
        /* Resume device */
        dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
        USART_Enable(usart->usart_device, usartEnable);
        break;

    case RT_DEVICE_CTRL_USART_RBUFFER:
        /* Set RX buffer */
    {
        struct efm32_usart_int_mode_t *int_rx;
        rt_uint8_t size;

        int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
        size = (rt_uint8_t)((rt_uint32_t)args & 0xFFUL);

        /* Free previous RX buffer */
        if (int_rx->data_ptr != RT_NULL)
        {
            if (size == 0)
            {   /* Free RX buffer */
                rt_free(int_rx->data_ptr);
                int_rx->data_ptr = RT_NULL;
            }
            else if (size != int_rx->data_size)
            {
                /* Re-allocate RX buffer */
                if ((int_rx->data_ptr = rt_realloc(int_rx->data_ptr, size)) \
                        == RT_NULL)
                {
                    usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
                    err_code = -RT_ENOMEM;
                    break;
                }
                // TODO: Is the following line necessary?
                //rt_memset(int_rx->data_ptr, 0, size);
            }
        }
        else
        {
            /* Allocate new RX buffer */
            if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
            {
                usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
                err_code = -RT_ENOMEM;
                break;
            }
        }
        int_rx->data_size = size;
        int_rx->read_index = 0;
        int_rx->save_index = 0;
    }
    break;

    }

    /* Unlock device */
    rt_sem_release(usart->lock);

    return err_code;
}
示例#18
0
void spi_enable(spi_t *obj, uint8_t enable)
{
    USART_Enable(obj->spi.spi, (enable ? usartEnable : usartDisable));
}
示例#19
0
/**************************************************************************//**
 * @brief UART/LEUART IRQ Handler
 *****************************************************************************/
void RETARGET_IRQ_NAME(void)
{
#if defined(RETARGET_USART)
  if (RETARGET_UART->STATUS & USART_STATUS_RXDATAV)
  {
#else
  if (RETARGET_UART->IF & LEUART_IF_RXDATAV)
  {
#endif

    /* Store Data */
    rxBuffer[rxWriteIndex] = RETARGET_RX(RETARGET_UART);
    rxWriteIndex++;
    rxCount++;
    if (rxWriteIndex == RXBUFSIZE)
    {
      rxWriteIndex = 0;
    }
    /* Check for overflow - flush buffer */
    if (rxCount > RXBUFSIZE)
    {
      rxWriteIndex = 0;
      rxCount      = 0;
      rxReadIndex  = 0;
    }
  }
}

/** @} (end group RetargetIo) */

/**************************************************************************//**
 * @brief UART/LEUART toggle LF to CRLF conversion
 * @param on If non-zero, automatic LF to CRLF conversion will be enabled
 *****************************************************************************/
void RETARGET_SerialCrLf(int on)
{
  if (on)
    LFtoCRLF = 1;
  else
    LFtoCRLF = 0;
}


/**************************************************************************//**
 * @brief Intializes UART/LEUART
 *****************************************************************************/
void RETARGET_SerialInit(void)
{
  /* Enable peripheral clocks */
  CMU_ClockEnable(cmuClock_HFPER, true);
  /* Configure GPIO pins */
  CMU_ClockEnable(cmuClock_GPIO, true);
  /* To avoid false start, configure output as high */
  GPIO_PinModeSet(RETARGET_TXPORT, RETARGET_TXPIN, gpioModePushPull, 1);
  GPIO_PinModeSet(RETARGET_RXPORT, RETARGET_RXPIN, gpioModeInput, 0);

#if defined(RETARGET_USART)
  USART_TypeDef           *usart = RETARGET_UART;
  USART_InitAsync_TypeDef init   = USART_INITASYNC_DEFAULT;

  /* Enable DK RS232/UART switch */
  RETARGET_PERIPHERAL_ENABLE();

  CMU_ClockEnable(RETARGET_CLK, true);

  /* Configure USART for basic async operation */
  init.enable = usartDisable;
  USART_InitAsync(usart, &init);

  /* Enable pins at correct UART/USART location. */
  #if defined( USART_ROUTEPEN_RXPEN )
  usart->ROUTEPEN = USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN;
  usart->ROUTELOC0 = ( usart->ROUTELOC0 &
                       ~( _USART_ROUTELOC0_TXLOC_MASK
                          | _USART_ROUTELOC0_RXLOC_MASK ) )
                     | ( RETARGET_TX_LOCATION << _USART_ROUTELOC0_TXLOC_SHIFT )
                     | ( RETARGET_RX_LOCATION << _USART_ROUTELOC0_RXLOC_SHIFT );
  #else
  usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | RETARGET_LOCATION;
  #endif

  /* Clear previous RX interrupts */
  USART_IntClear(RETARGET_UART, USART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(RETARGET_IRQn);

  /* Enable RX interrupts */
  USART_IntEnable(RETARGET_UART, USART_IF_RXDATAV);
  NVIC_EnableIRQ(RETARGET_IRQn);

  /* Finally enable it */
  USART_Enable(usart, usartEnable);

#else
  LEUART_TypeDef      *leuart = RETARGET_UART;
  LEUART_Init_TypeDef init    = LEUART_INIT_DEFAULT;

  /* Enable DK LEUART/RS232 switch */
  RETARGET_PERIPHERAL_ENABLE();

  /* Enable CORE LE clock in order to access LE modules */
  CMU_ClockEnable(cmuClock_CORELE, true);

#if defined(RETARGET_VCOM)
  /* Select HFXO/2 for LEUARTs (and wait for it to stabilize) */
  CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_CORELEDIV2);
#else
  /* Select LFXO for LEUARTs (and wait for it to stabilize) */
  CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
#endif

  CMU_ClockEnable(RETARGET_CLK, true);

  /* Do not prescale clock */
  CMU_ClockDivSet(RETARGET_CLK, cmuClkDiv_1);

  /* Configure LEUART */
  init.enable = leuartDisable;
#if defined(RETARGET_VCOM)
  init.baudrate = 115200;
#endif
  LEUART_Init(leuart, &init);
  /* Enable pins at default location */
  leuart->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | RETARGET_LOCATION;

  /* Clear previous RX interrupts */
  LEUART_IntClear(RETARGET_UART, LEUART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(RETARGET_IRQn);

  /* Enable RX interrupts */
  LEUART_IntEnable(RETARGET_UART, LEUART_IF_RXDATAV);
  NVIC_EnableIRQ(RETARGET_IRQn);

  /* Finally enable it */
  LEUART_Enable(leuart, leuartEnable);
#endif

#if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
  setvbuf(stdout, NULL, _IONBF, 0);   /*Set unbuffered mode for stdout (newlib)*/
#endif

  initialized = true;
}
示例#20
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{   
    /* System Clocks Configuration */
    RCC_Configuration();

    /* Configure the GPIO ports */
    GPIO_Configuration();

    /* USART1 and USART2 configuration */

    USART_InitStructure.USART_BRR = 230400;
    USART_InitStructure.USART_WL = USART_WL_8B;
    USART_InitStructure.USART_STBits = USART_STBITS_1;
    USART_InitStructure.USART_Parity = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
    
    /* Configure USART1 */
    USART_Init(USART1, &USART_InitStructure);

    /* Configure USART2 */
    USART_Init(USART2, &USART_InitStructure);
    
    /* Enable USART1 */
    USART_Enable(USART1, ENABLE);

    /* Enable USART2 */
    USART_Enable(USART2, ENABLE);

    /* Enable USART1 Half Duplex Mode*/
    USART_HalfDuplex_Enable(USART1, ENABLE);
    /* Enable USART2 Half Duplex Mode*/
    USART_HalfDuplex_Enable(USART2, ENABLE);
    
    while(NbrOfDataToRead2--)
    {
        /* Wait until end of transmit */
        while(USART_GetBitState(USART1, USART_FLAG_TBE) == RESET)
        {
        }
        /* Write one byte in the USARTy Transmit Data Register */
        USART_DataSend(USART1, TxBuffer1[TxCounter1++]);

        /* Wait the byte is entirely received by USARTz */  
        while(USART_GetBitState(USART2, USART_FLAG_RBNE) == RESET)
        {
        }
        /* Store the received byte in the RxBuffer2 */
        RxBuffer2[RxCounter2++] = USART_DataReceive(USART2);
    }

    /* Clear the USARTy Data Register */
    USART_DataReceive(USART1);

    while(NbrOfDataToRead1--)
    { 
        /* Wait until end of transmit */
        while(USART_GetBitState(USART2, USART_FLAG_TBE)== RESET)
        {
        }
        /* Write one byte in the USARTz Transmit Data Register */
        USART_DataSend(USART2, TxBuffer2[TxCounter2++]);

        /* Wait the byte is entirely received by USARTy */
        while(USART_GetBitState(USART1,USART_FLAG_RBNE) == RESET)
        {
        }
        /* Store the received byte in the RxBuffer1 */
        RxBuffer1[RxCounter1++] = USART_DataReceive(USART1);
    }
  
    /* Check the received data with the send ones */
    TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
    /* TransferStatus1 = PASSED, if the data transmitted from USART2 and    
       received by USART1 are the same */
    /* TransferStatus1 = FAILED, if the data transmitted from USART2 and 
       received by USART1 are different */
    TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
    /* TransferStatus2 = PASSED, if the data transmitted from USART1 and    
       received by USART2 are the same */
    /* TransferStatus2 = FAILED, if the data transmitted from USART1 and 
       received by USART2 are different */

    while (1)
    {
    }
}
示例#21
0
文件: uart.c 项目: MatKub/RIOT
int uart_init_blocking(uart_t _uart, uint32_t baudrate)
{
#if UART_0_EN || UART_1_EN
    USART_TypeDef *uart;
    USART_InitAsync_TypeDef uartInit = USART_INITASYNC_DEFAULT;
#endif

#if UART_2_EN
    LEUART_Init_TypeDef leuartInit;
#endif

    switch(_uart) {
#if UART_0_EN
    case UART_0:
    uart = UART_0_DEV;
    /* Enabling clock to USART0 */
    CMU_ClockEnable(cmuClock_USART0, true);
    /* Output PIN */
    gpio_init(GPIO_T(UART_0_PORT, UART_0_TX_PIN), GPIO_DIR_PUSH_PULL,
            GPIO_PULLUP);
    gpio_init(GPIO_T(UART_0_PORT, UART_0_RX_PIN), GPIO_DIR_INPUT,
            GPIO_PULLDOWN);
    /* Prepare struct for initializing UART in asynchronous mode*/
    uartInit.enable = usartDisable; /* Don't enable UART upon intialization */
    uartInit.refFreq = 0; /* Provide information on reference frequency.
     When set to 0, the reference frequency is */
    uartInit.baudrate = baudrate; /* Baud rate */
    uartInit.oversampling = usartOVS16; /* Oversampling. Range is 4x, 6x, 8x or 16x */
    uartInit.databits = usartDatabits8; /* Number of data bits. Range is 4 to 10 */
    uartInit.parity = usartNoParity; /* Parity mode */
    uartInit.stopbits = usartStopbits1; /* Number of stop bits. Range is 0 to 2 */
    uartInit.mvdis = false; /* Disable majority voting */
    uartInit.prsRxEnable = false; /* Enable USART Rx via Peripheral Reflex System */
    uartInit.prsRxCh = usartPrsRxCh0; /* Select PRS channel if enabled */
    /* Initialize USART with uartInit struct */
    USART_InitAsync(uart, &uartInit);
    uart->ROUTE = UART_ROUTE_RXPEN | UART_ROUTE_TXPEN | UART_0_LOC;
    USART_Enable(UART_0_DEV, usartEnable);
    break;

#endif
#if UART_1_EN
    case UART_1:
    uart = UART_1_DEV;
    /* Enabling clock to USART0 */
    uart = UART_1_DEV;
    CMU_ClockEnable(cmuClock_USART1, true);
    /* Output PIN */
    gpio_init(GPIO_T(UART_1_PORT, UART_1_TX_PIN), GPIO_DIR_PUSH_PULL,
            GPIO_PULLUP);
    gpio_init(GPIO_T(UART_1_PORT, UART_1_RX_PIN), GPIO_DIR_INPUT,
            GPIO_PULLDOWN);
    /* Prepare struct for initializing UART in asynchronous mode*/
    uartInit.enable = usartDisable; /* Don't enable UART upon intialization */
    uartInit.refFreq = 0; /* Provide information on reference frequency.
     When set to 0, the reference frequency is */
    uartInit.baudrate = baudrate; /* Baud rate */
    uartInit.oversampling = usartOVS16; /* Oversampling. Range is 4x, 6x, 8x or 16x */
    uartInit.databits = usartDatabits8; /* Number of data bits. Range is 4 to 10 */
    uartInit.parity = usartNoParity; /* Parity mode */
    uartInit.stopbits = usartStopbits1; /* Number of stop bits. Range is 0 to 2 */
    uartInit.mvdis = false; /* Disable majority voting */
    uartInit.prsRxEnable = false; /* Enable USART Rx via Peripheral Reflex System */
    uartInit.prsRxCh = usartPrsRxCh0; /* Select PRS channel if enabled */
    /* Initialize USART with uartInit struct */
    USART_InitAsync(uart, &uartInit);
    uart->ROUTE = UART_ROUTE_RXPEN | UART_ROUTE_TXPEN | UART_1_LOC;
    USART_Enable(UART_1_DEV, usartEnable);
    break;
#endif
#if UART_2_EN
    case UART_2:
        CMU_ClockEnable(cmuClock_LEUART0, true);
        gpio_init(GPIO_T(UART_2_PORT, UART_2_TX_PIN), GPIO_DIR_PUSH_PULL,
                GPIO_PULLUP);
        gpio_init(GPIO_T(UART_2_PORT, UART_2_RX_PIN), GPIO_DIR_INPUT,
                GPIO_PULLDOWN);
        LEUART_Reset(UART_2_DEV);
        leuartInit.enable = leuartEnable;
        leuartInit.baudrate = baudrate;
        leuartInit.databits = leuartDatabits8;
        leuartInit.parity = leuartNoParity;
        leuartInit.stopbits = leuartStopbits1;
        leuartInit.refFreq = 0;
        LEUART_Init(UART_2_DEV, &leuartInit);
        UART_2_DEV->ROUTE =
        LEUART_ROUTE_TXPEN | LEUART_ROUTE_RXPEN | UART_2_LOC;
        LEUART_Enable(UART_2_DEV, usartEnable);
        break;
#endif
    default:
        return -1;
    }
    return 0;
}
示例#22
0
文件: main.c 项目: LonelyWolf/stm32
int main(void) {
	// Initialize the MCU clock system
	SystemInit();
	SetSysClock();
	SystemCoreClockUpdate();


	// Initialize debug output port (USART2)
	//   clock source: SYSCLK
	//   mode: transmit only
	USART2_HandleInit();
	RCC_SetClockUSART(RCC_USART2_CLK_SRC,RCC_PERIPH_CLK_SYSCLK);
	USART_Init(&hUSART2,USART_MODE_TX);

	// Configure USART:
	//   oversampling by 16
	//   115200, 8-N-1
	//   no hardware flow control
	USART_SetOversampling(&hUSART2,USART_OVERS16);
	USART_SetBaudRate(&hUSART2,115200);
	USART_SetDataMode(&hUSART2,USART_DATAWIDTH_8B,USART_PARITY_NONE,USART_STOPBITS_1);
	USART_SetHWFlow(&hUSART2,USART_HWCTL_NONE);
	USART_Enable(&hUSART2);
	USART_CheckIdleState(&hUSART2,0xC5C10); // Timeout of about 100ms at 80MHz CPU


	// Say "hello world"
	RCC_ClocksTypeDef Clocks;
	RCC_GetClocksFreq(&Clocks);
	printf("\r\n---STM32L476RG---\r\n");
	printf("CRC peripheral (%s @ %s)\r\n",__DATE__,__TIME__);
	printf("CPU: %.3uMHz\r\n",SystemCoreClock / 1000);
	printf("SYSCLK=%.3uMHz, HCLK=%.3uMHz\r\n",Clocks.SYSCLK_Frequency / 1000,Clocks.HCLK_Frequency / 1000);
	printf("APB1=%.3uMHz, APB2=%.3uMHz\r\n",Clocks.PCLK1_Frequency / 1000,Clocks.PCLK2_Frequency / 1000);
	printf("System clock: %s\r\n",_sysclk_src_str[RCC_GetSysClockSource()]);


	// Enable the CRC peripheral
	CRC_Enable();

	// Disable reverse for input and output data
	CRC_SetInRevMode(CRC_IN_NORMAL);
	CRC_SetOutRevMode(CRC_OUT_NORMAL);


	// First column in output is the calculated CRC, second - reference value

	// 8-bit CRC tests
	CRC_SetPolynomialSize(CRC_PSIZE_8B);
	printf("CRC-8 tests:\r\n");

	// CCITT CRC-8 (Poly=0x07, Init=0x00, RefIn=false, RefOut=false)
	CRC_SetPolynomial(0x07);
	CRC_SetInitValue(0x00);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("CCITT    : 0x%02X . 0x%02X\r\n", CRC_GetData8(), 0x34);

	// CDMA2000 CRC-8 (Poly=0x9B, Init=0xFF, RefIn=false, RefOut=false)
	CRC_SetPolynomial(0x9B);
	CRC_SetInitValue(0xFF);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("CDMA2000 : 0x%02X . 0x%02X\r\n", CRC_GetData8(), 0xCE);

	// WCDMA CRC-8 (Poly=0x9B, Init=0x00, RefIn=true, RefOut=true)
	CRC_SetInitValue(0x00);
	CRC_SetInRevMode(CRC_IN_REV_BYTE);
	CRC_SetOutRevMode(CRC_OUT_REVERSED);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("WCDMA    : 0x%02X . 0x%02X\r\n", CRC_GetData8(), 0xEA);

	// I-CODE CRC-8 (Poly=0x1D, Init=0xFD, RefIn=false, RefOut=false)
	CRC_SetPolynomial(0x1D);
	CRC_SetInitValue(0xFD);
	CRC_SetInRevMode(CRC_IN_NORMAL);
	CRC_SetOutRevMode(CRC_OUT_NORMAL);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("I-CODE   : 0x%02X . 0x%02X\r\n", CRC_GetData8(), 0x87);

	// ROHC CRC-8 (Poly=0x07, Init=0xFF, RefIn=true, RefOut=true)
	CRC_SetPolynomial(0x07);
	CRC_SetInitValue(0xFF);
	CRC_SetInRevMode(CRC_IN_REV_BYTE);
	CRC_SetOutRevMode(CRC_OUT_REVERSED);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("ROHC     : 0x%02X . 0x%02X\r\n", CRC_GetData8(), 0x1A);


	// 16-bit CRC tests
	CRC_SetPolynomialSize(CRC_PSIZE_16B);
	printf("CRC-16 tests:\r\n");

	// MODBUS (Poly=0x8005, Init=0xFFFF, RefIn=true, RefOut=true)
	CRC_SetPolynomial(0x8005);
	CRC_SetInitValue(0xFFFF);
	CRC_SetInRevMode(CRC_IN_REV_BYTE);
	CRC_SetOutRevMode(CRC_OUT_REVERSED);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("MODBUS   : 0x%04X . 0x%04X\r\n", CRC_GetData16(), 0x3BD1);

	// CDMA2000 (Poly=0xC867, Init=0xFFFF, RefIn=false, RefOut=false)
	CRC_SetPolynomial(0xC867);
	CRC_SetInRevMode(CRC_IN_NORMAL);
	CRC_SetOutRevMode(CRC_OUT_NORMAL);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("CDMA2000 : 0x%04X . 0x%04X\r\n", CRC_GetData16(), 0x52F7);

	// XMODEM (Poly=0x1021, Init=0x0000, RefIn=false, RefOut=false)
	CRC_SetPolynomial(0x1021);
	CRC_SetInitValue(0x0000);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("XMODEM   : 0x%04X . 0x%04X\r\n", CRC_GetData16(), 0x047B);

	// USB (Poly=0x8005, Init=0xFFFF, RefIn=true, RefOut=true, XorOut=0xFFFF)
	CRC_SetPolynomial(0x8005);
	CRC_SetInitValue(0xFFFF);
	CRC_SetInRevMode(CRC_IN_REV_BYTE);
	CRC_SetOutRevMode(CRC_OUT_REVERSED);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("USB      : 0x%04X . 0x%04X\r\n", CRC_GetData16() ^ 0xFFFF, 0xC42E);


	// 32-bit CRC tests
	CRC_SetPolynomialSize(CRC_PSIZE_32B);
	printf("CRC-32 tests:\r\n");

	// MPEG-2 (Poly=0x04C11DB7, Init=0xFFFFFFFF, RefIn=true, RefOut=true)
	CRC_SetPolynomial(0x04C11DB7);
	CRC_SetInitValue(0xFFFFFFFF);
	CRC_SetInRevMode(CRC_IN_NORMAL);
	CRC_SetOutRevMode(CRC_OUT_NORMAL);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("MPEG-2   : 0x%08X . 0x%08X\r\n", CRC_GetData32(), 0xF5C62CB4);

	// POSIX (Poly=0x04C11DB7, Init=0x00000000, RefIn=false, RefOut=false, XorOut=0xFFFFFFFF)
	CRC_SetPolynomial(0x04C11DB7);
	CRC_SetInitValue(0x00000000);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("POSIX    : 0x%08X . 0x%08X\r\n", CRC_GetData32() ^ 0xFFFFFFFF, 0xD338A790);

	// CRC-32D (Poly=0xA833982B, Init=0xFFFFFFFF, RefIn=true, RefOut=true, XorOut=0xFFFFFFFF)
	CRC_SetPolynomial(0xA833982B);
	CRC_SetInitValue(0xFFFFFFFF);
	CRC_SetInRevMode(CRC_IN_REV_BYTE);
	CRC_SetOutRevMode(CRC_OUT_REVERSED);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("CRC-32D  : 0x%08X . 0x%08X\r\n", CRC_GetData32() ^ 0xFFFFFFFF, 0xD2178F40);

	// XFER (Poly=0x000000AF, Init=0x00000000, RefIn=false, RefOut=false)
	CRC_SetPolynomial(0x000000AF);
	CRC_SetInitValue(0x00000000);
	CRC_SetInRevMode(CRC_IN_NORMAL);
	CRC_SetOutRevMode(CRC_OUT_NORMAL);
	CRC_Reset();
	CRC_CalcBuffer((uint32_t *)data_buf, buf_size);
	printf("XFER     : 0x%08X . 0x%08X\r\n", CRC_GetData32(), 0x5D4143CF);


	// The main loop
	while (1) {
	}
}