//----------------------------------------------------------------------------- static void uart_init(uint32_t baud) { HAL_GPIO_UART_TX_abcd(3); HAL_GPIO_UART_RX_abcd(0); PMC->PMC_PCER0 = PMC_PCER0_PID14; USART1->US_CR = US_CR_RXEN | US_CR_TXEN; USART1->US_MR = US_MR_USART_MODE_NORMAL | US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_1_BIT; USART1->US_BRGR = US_BRGR_CD(F_CPU / 16 / baud); }
/** * \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 }
/** * @brief UART initialization. * @details This function must be invoked with interrupts disabled. * * @param[in] sdp pointer to a @p SerialDriver object * @param[in] config the architecture-dependent serial driver configuration */ static void uart_init(SerialDriver *sdp, const SerialConfig *config) { #if SAMA_SERIAL_USE_UART && SAMA_SERIAL_USE_FLEXCOM if (sdp->uart != NULL) #endif /* SAMA_SERIAL_USE_UART && SAMA_SERIAL_USE_FLEXCOM */ #if SAMA_SERIAL_USE_UART { Uart *u = sdp->uart; /* Disabling write protection */ sdDisableWP(u); /* Baud rate setting.*/ u->UART_BRGR = UART_BRGR_CD(sdp->clock / (16 * config->speed)); u->UART_CR = config->cr; u->UART_MR = config->mr; u->UART_IER = UART_IER_RXRDY; /* Clearing error status bit */ u->UART_CR |= UART_CR_RSTSTA; /* Enabling Tx and Rx */ u->UART_CR |= UART_CR_RXEN | UART_CR_TXEN; /* Enabling write protection */ sdEnableWP(u); } #endif /* SAMA_SERIAL_USE_UART */ #if SAMA_SERIAL_USE_UART && SAMA_SERIAL_USE_FLEXCOM else if (sdp->usart != NULL) #endif /* SAMA_SERIAL_USE_UART && SAMA_SERIAL_USE_FLEXCOM */ #if SAMA_SERIAL_USE_FLEXCOM { Flexcom *fl = sdp->flexcom; Usart *us = sdp->usart; /* Disabling write protection */ sdFlexDisableWP(us) /* Enabling USART on FLEXCOM */ fl->FLEX_MR = FLEX_MR_OPMODE_USART; /* Baud rate setting (OVER = 0 and SYNC = 0)*/ us->US_BRGR = US_BRGR_CD(sdp->clock / (16 * config->speed)); us->US_CR = config->cr; us->US_MR = config->mr; us->US_IER = US_IER_RXRDY; /* Clearing status bit */ us->US_CR |= US_CR_RSTSTA; /* Enabling Tx and Rx */ us->US_CR |= US_CR_RXEN | US_CR_TXEN; /* Enabling write protection */ sdFlexEnableWP(us) }
static int baudrate_set(Usart *const usart, u32_t baudrate, u32_t mck_freq_hz) { u32_t divisor; __ASSERT(baudrate, "baud rate has to be bigger than 0"); __ASSERT(mck_freq_hz/16 >= baudrate, "MCK frequency is too small to set required baud rate"); divisor = mck_freq_hz / 16 / baudrate; if (divisor > 0xFFFF) { return -EINVAL; }; usart->US_BRGR = US_BRGR_CD(divisor); return 0; }