/* Calculate a clock divider(CD) and a fractional part (FP) for the USART asynchronous modes to generate a baudrate as close as possible to the baudrate set point.  */
uint32_t usart_set_async_baudrate(Usart *p_usart, uint32_t baudrate, uint32_t ul_mck)
/* Calculate a clock divider(CD) and a fractional part (FP) for the USART asynchronous modes to generate a baudrate as close as possible to the baudrate set point.  */
uint32_t usart_set_async_baudrate(Usart *p_usart, uint32_t baudrate, uint32_t ul_mck)
{
	uint32_t over;
	uint32_t cd_fp;
	uint32_t cd;
	uint32_t fp;
	if (ul_mck >= HIGH_FRQ_SAMPLE_DIV * baudrate) {
		over = HIGH_FRQ_SAMPLE_DIV;
	} else {
		over = LOW_FRQ_SAMPLE_DIV;
	}
	cd_fp = (8 * ul_mck + (over * baudrate) / 2) / (over * baudrate);
	cd = cd_fp >> 3;
	fp = cd_fp & 0x07;
	if (cd < MIN_CD_VALUE || cd > MAX_CD_VALUE) {
		return 1;
	}
	if (over == 8) {
		p_usart->US_MR |= US_MR_OVER;
	}
	p_usart->US_BRGR = (cd << US_BRGR_CD_Pos) | (fp << US_BRGR_FP_Pos);
	return 0;
}