Beispiel #1
0
Datei: main.c Projekt: gstroe/Arm
/**
 * \brief Configures an USART baudrate when USART_MODE=SPI.
 *
 *
 *  \param pUsart  Pointer to the USART peripheral to configure.
 *  \param baudrate  Baudrate at which the USART should operate (in Hz).
 *  \param masterClock  Frequency of the system master clock (in Hz).
 */
static void USART_SPI_SetBaudrate(Usart *pUsart,
								uint32_t baudrate,
								uint32_t masterClock)
{
	unsigned int CD, FP;

	/* Configure baudrate*/
	CD = (masterClock / baudrate);
	FP = ((masterClock / baudrate) - CD);

	pUsart->US_BRGR = (US_BRGR_CD(CD) | US_BRGR_FP(FP));
}
/**
 * \brief Configures an USART peripheral with the specified parameters.
 *
 *
 *  \param usart  Pointer to the USART peripheral to configure.
 *  \param mode  Desired value for the USART mode register (see the datasheet).
 *  \param baudrate  Baudrate at which the USART should operate (in Hz).
 *  \param masterClock  Frequency of the system master clock (in Hz).
 */
void USART_Configure(Usart *pUsart,
                            uint32_t mode,
                            uint32_t baudrate,
                            uint32_t masterClock)
{
  
   unsigned int CD, FP, BaudError, OVER, ActualBaudRate;
  
    /* Reset and disable receiver & transmitter*/
    pUsart->US_CR = US_CR_RSTRX | US_CR_RSTTX
                  | US_CR_RXDIS | US_CR_TXDIS | US_CR_RSTSTA;
    
    pUsart->US_IDR = 0xFFFFFFFF;
    
    /* Configure baudrate*/  
    BaudError = 10;
    OVER = 0;
    
    // Configure baud rate
    while (BaudError > 5)
    {
      
      CD = (masterClock / (baudrate * 8*(2-OVER)));
      FP = ((masterClock / (baudrate * (2-OVER)) ) - CD * 8);     
      ActualBaudRate = (masterClock/(CD*8 + FP))/(2-OVER);
      BaudError = (100-((baudrate*100/ActualBaudRate)));
        
      if (BaudError > 5)
      {
        OVER++;
        if(OVER>=2)
        {
          assert( 0 ) ;
        }
      }
    }
    
    pUsart->US_BRGR = ( US_BRGR_CD(CD) | US_BRGR_FP(FP));
    
    /* Configure mode*/
    pUsart->US_MR = (mode |  (OVER << 19) );

    // Enable receiver and transmitter
    pUsart->US_CR = US_CR_RXEN | US_CR_TXEN;
    

    /* Disable buffering for printf(). */
#if ( defined (__GNUC__) && !defined (__SAMBA__) )
        setvbuf(stdout, (char *)NULL, _IONBF, 0);
#endif

}