ECU/GSM/dist/src/USART_program.c
Error_Status USART_enumInit(u8 Channel) {
	Error_Status returnVal = OK;

	if (Channel >= CHANNEL_MIN && Channel <= CHANNEL_MAX) {
//		RCC_enumEnableClock( USART1_CLOCK );

		volatile UART_REG * selectedChannel = USART1;

		switch(Channel) {
			case 1:
				CallbackFunction1 = &USART_Stub;

				#ifdef USART1_WORD_LENGTH
					USART1->CR1 |= USART1_WORD_LENGTH << 12;
				#endif

				#ifdef USART1_STOP_BITS
					USART1->CR2 |= USART1_STOP_BITS << 12;
				#endif

				#if USART1_ENABLE_TRANSMITTER == ENABLE
					SET_BIT(USART1->CR1, 3);
				#endif

				#if USART1_ENABLE_RECEIVER == ENABLE
					SET_BIT(USART1->CR1, 2);
				#endif

				#if	USART1_RECEIVE_CALLBACK == ENABLE
					SET_BIT(USART1->CR1, 5);					// RXNE interrupt enable
				#endif

				#if USART1_ENABLE_DMA_RECEIVER == ENABLE
					SET_BIT(USART1->CR3, 6);					// DMA ENABLE RECEIVER
				#endif

				#if USART1_ENABLE_DMA_TRANSMITTER == ENABLE
					SET_BIT(USART1->CR3, 7);					// DMA ENABLE TRANSMITTER
				#endif

				break;
			case 2:
				selectedChannel = USART2;
				CallbackFunction2 = &USART_Stub;

				#ifdef USART2_WORD_LENGTH
					USART2->CR1 |= USART2_WORD_LENGTH << 12;
				#endif

				#ifdef USART2_STOP_BITS
					USART2->CR2 |= USART2_STOP_BITS << 12;
				#endif

				#if USART2_ENABLE_TRANSMITTER == ENABLE
					SET_BIT(USART2->CR1, 3);
				#endif

				#if USART2_ENABLE_RECEIVER == ENABLE
					SET_BIT(USART2->CR1, 2);
				#endif

				#if	USART2_RECEIVE_CALLBACK == ENABLE
					SET_BIT(USART2->CR1, 5);									// RXNE interrupt enable
				#endif

				#if USART2_ENABLE_DMA_RECEIVER == ENABLE
					SET_BIT(USART2->CR3, 6);					// DMA ENABLE RECEIVER
				#endif

				#if USART2_ENABLE_DMA_TRANSMITTER == ENABLE
					SET_BIT(USART2->CR3, 7);					// DMA ENABLE TRANSMITTER
				#endif

				break;
			case 3:
				selectedChannel = USART3;

				#ifdef USART3_WORD_LENGTH
					USART3->CR1 |= USART3_WORD_LENGTH << 12;
				#endif

				#ifdef USART3_STOP_BITS
					USART3->CR2 |= USART3_STOP_BITS << 12;
				#endif

				#if USART3_ENABLE_TRANSMITTER == ENABLE
					SET_BIT(USART3->CR1, 3);
				#endif

				#if USART3_ENABLE_RECEIVER == ENABLE
					SET_BIT(USART3->CR1, 2);
				#endif

				#if	USART3_RECEIVE_CALLBACK == ENABLE
					SET_BIT(USART3->CR1, 5);									// RXNE interrupt enable
				#endif

				#if USART3_ENABLE_DMA_RECEIVER == ENABLE
					SET_BIT(USART3->CR3, 6);					// DMA ENABLE RECEIVER
				#endif

				#if USART3_ENABLE_DMA_TRANSMITTER == ENABLE
					SET_BIT(USART3->CR3, 7);					// DMA ENABLE TRANSMITTER
				#endif

				CallbackFunction3 = &USART_Stub;
				break;
			default:
				returnVal = NOK;			// Shouldn't reach this part
		}

		u32 freq = 72000000;												// APB2 Bus Fck

		if (Channel > 1) {
			freq = 36000000;												// APB1 bus Fck
		}

		u16 Mantissa = freq / (16 * BAUDRATE);								// Mantissa (whole part)
		u8 Fraction = freq / (16.0 * BAUDRATE) * 100.0 - Mantissa * 100;	// Fractional part
		selectedChannel->BRR  = (Mantissa << 4) + Fraction;					// Set BAUDRATE

		SET_BIT(selectedChannel->CR1, 13);									// USART enable
	} else {
		returnVal = numberOutOfRange;
	}

	return returnVal;
}
/**
  * @brief  Sets the BAUDRATE and enables USART transmitter and/or receiver
  * @param  Channel:	Number of the UART Channel to use.
  * 					Can be USART_CHANNEL_x where x is from 1 to 5
  * @retval Error_Status
  */
@brief  Sets the BAUDRATE and enables USART transmitter and/or receiver
@param  Channel:    Number of the UART Channel to use.
Can be USART_CHANNEL_x where x is from 1 to 5
@retval Error_Status
@brief Sets the BAUDRATE and enables USART transmitter and / or receiver @param Channel : Number of the UART Channel to use . Can be USART_CHANNEL_x where x is from 1 to 5 @retval Error_Status
Error_Status USART_enumInit(u8 Channel) {
	Error_Status returnVal = OK;
	if (Channel >= CHANNEL_MIN && Channel <= CHANNEL_MAX) {
		volatile UART_REG * selectedChannel = USART1;
		switch(Channel) {
			case 1:
				CallbackFunction1 = &USART_Stub;
				#ifdef USART1_WORD_LENGTH
					USART1->CR1 |= USART1_WORD_LENGTH << 12;
				#endif
				#ifdef USART1_STOP_BITS
					USART1->CR2 |= USART1_STOP_BITS << 12;
				#endif
				#if USART1_ENABLE_TRANSMITTER == ENABLE
					SET_BIT(USART1->CR1, 3);
				#endif
				#if USART1_ENABLE_RECEIVER == ENABLE
					SET_BIT(USART1->CR1, 2);
				#endif
				#if	USART1_RECEIVE_CALLBACK == ENABLE
					SET_BIT(USART1->CR1, 5);					
				#endif
				#if USART1_ENABLE_DMA_RECEIVER == ENABLE
					SET_BIT(USART1->CR3, 6);					
				#endif
				#if USART1_ENABLE_DMA_TRANSMITTER == ENABLE
					SET_BIT(USART1->CR3, 7);					
				#endif
				break;
			case 2:
				selectedChannel = USART2;
				CallbackFunction2 = &USART_Stub;
				#ifdef USART2_WORD_LENGTH
					USART2->CR1 |= USART2_WORD_LENGTH << 12;
				#endif
				#ifdef USART2_STOP_BITS
					USART2->CR2 |= USART2_STOP_BITS << 12;
				#endif
				#if USART2_ENABLE_TRANSMITTER == ENABLE
					SET_BIT(USART2->CR1, 3);
				#endif
				#if USART2_ENABLE_RECEIVER == ENABLE
					SET_BIT(USART2->CR1, 2);
				#endif
				#if	USART2_RECEIVE_CALLBACK == ENABLE
					SET_BIT(USART2->CR1, 5);									
				#endif
				#if USART2_ENABLE_DMA_RECEIVER == ENABLE
					SET_BIT(USART2->CR3, 6);					
				#endif
				#if USART2_ENABLE_DMA_TRANSMITTER == ENABLE
					SET_BIT(USART2->CR3, 7);					
				#endif
				break;
			case 3:
				selectedChannel = USART3;
				#ifdef USART3_WORD_LENGTH
					USART3->CR1 |= USART3_WORD_LENGTH << 12;
				#endif
				#ifdef USART3_STOP_BITS
					USART3->CR2 |= USART3_STOP_BITS << 12;
				#endif
				#if USART3_ENABLE_TRANSMITTER == ENABLE
					SET_BIT(USART3->CR1, 3);
				#endif
				#if USART3_ENABLE_RECEIVER == ENABLE
					SET_BIT(USART3->CR1, 2);
				#endif
				#if	USART3_RECEIVE_CALLBACK == ENABLE
					SET_BIT(USART3->CR1, 5);									
				#endif
				#if USART3_ENABLE_DMA_RECEIVER == ENABLE
					SET_BIT(USART3->CR3, 6);					
				#endif
				#if USART3_ENABLE_DMA_TRANSMITTER == ENABLE
					SET_BIT(USART3->CR3, 7);					
				#endif
				CallbackFunction3 = &USART_Stub;
				break;
			default:
				returnVal = NOK;			
		}
		u32 freq = 72000000;												
		if (Channel > 1) {
			freq = 36000000;												
		}
		u16 Mantissa = freq / (16 * BAUDRATE);								
		u8 Fraction = freq / (16.0 * BAUDRATE) * 100.0 - Mantissa * 100;	
		selectedChannel->BRR  = (Mantissa << 4) + Fraction;					
		SET_BIT(selectedChannel->CR1, 13);									
	} else {
		returnVal = numberOutOfRange;
	}
	return returnVal;
}
Error_Status USART_enumInit ( u8 Channel ) { Error_Status returnVal = OK ; if ( Channel >= CHANNEL_MIN && Channel <= CHANNEL_MAX ) { volatile UART_REG * selectedChannel = USART1 ; switch ( Channel ) { case 1 : CallbackFunction1 = & USART_Stub ; #ifdef USART1_WORD_LENGTH USART1 -> CR1 |= USART1_WORD_LENGTH << 12 ; #endif #ifdef USART1_STOP_BITS USART1 -> CR2 |= USART1_STOP_BITS << 12 ; #endif #if USART1_ENABLE_TRANSMITTER == ENABLE SET_BIT ( USART1 -> CR1 , 3 ) ; #endif #if USART1_ENABLE_RECEIVER == ENABLE SET_BIT ( USART1 -> CR1 , 2 ) ; #endif #if USART1_RECEIVE_CALLBACK == ENABLE SET_BIT ( USART1 -> CR1 , 5 ) ; #endif #if USART1_ENABLE_DMA_RECEIVER == ENABLE SET_BIT ( USART1 -> CR3 , 6 ) ; #endif #if USART1_ENABLE_DMA_TRANSMITTER == ENABLE SET_BIT ( USART1 -> CR3 , 7 ) ; #endif break ; case 2 : selectedChannel = USART2 ; CallbackFunction2 = & USART_Stub ; #ifdef USART2_WORD_LENGTH USART2 -> CR1 |= USART2_WORD_LENGTH << 12 ; #endif #ifdef USART2_STOP_BITS USART2 -> CR2 |= USART2_STOP_BITS << 12 ; #endif #if USART2_ENABLE_TRANSMITTER == ENABLE SET_BIT ( USART2 -> CR1 , 3 ) ; #endif #if USART2_ENABLE_RECEIVER == ENABLE SET_BIT ( USART2 -> CR1 , 2 ) ; #endif #if USART2_RECEIVE_CALLBACK == ENABLE SET_BIT ( USART2 -> CR1 , 5 ) ; #endif #if USART2_ENABLE_DMA_RECEIVER == ENABLE SET_BIT ( USART2 -> CR3 , 6 ) ; #endif #if USART2_ENABLE_DMA_TRANSMITTER == ENABLE SET_BIT ( USART2 -> CR3 , 7 ) ; #endif break ; case 3 : selectedChannel = USART3 ; #ifdef USART3_WORD_LENGTH USART3 -> CR1 |= USART3_WORD_LENGTH << 12 ; #endif #ifdef USART3_STOP_BITS USART3 -> CR2 |= USART3_STOP_BITS << 12 ; #endif #if USART3_ENABLE_TRANSMITTER == ENABLE SET_BIT ( USART3 -> CR1 , 3 ) ; #endif #if USART3_ENABLE_RECEIVER == ENABLE SET_BIT ( USART3 -> CR1 , 2 ) ; #endif #if USART3_RECEIVE_CALLBACK == ENABLE SET_BIT ( USART3 -> CR1 , 5 ) ; #endif #if USART3_ENABLE_DMA_RECEIVER == ENABLE SET_BIT ( USART3 -> CR3 , 6 ) ; #endif #if USART3_ENABLE_DMA_TRANSMITTER == ENABLE SET_BIT ( USART3 -> CR3 , 7 ) ; #endif CallbackFunction3 = & USART_Stub ; break ; default : returnVal = NOK ; } u32 freq = 72000000 ; if ( Channel > 1 ) { freq = 36000000 ; } u16 Mantissa = freq / ( 16 * BAUDRATE ) ; u8 Fraction = freq / ( 16.0 * BAUDRATE ) * 100.0 - Mantissa * 100 ; selectedChannel -> BRR = ( Mantissa << 4 ) + Fraction ; SET_BIT ( selectedChannel -> CR1 , 13 ) ; } else { returnVal = numberOutOfRange ; } return returnVal ; }
@brief  Sets the BAUDRATE and enables USART transmitter and/or receiver
 @param  Channel:    Number of the UART Channel to use.
@brief Sets the BAUDRATE and enables USART transmitter and / or receiver @param Channel : Number of the UART Channel to use .
//		RCC_enumEnableClock( USART1_CLOCK ); // RXNE interrupt enable // DMA ENABLE RECEIVER // DMA ENABLE TRANSMITTER // RXNE interrupt enable // DMA ENABLE RECEIVER // DMA ENABLE TRANSMITTER // RXNE interrupt enable // DMA ENABLE RECEIVER // DMA ENABLE TRANSMITTER // Shouldn't reach this part // APB2 Bus Fck // APB1 bus Fck // Mantissa (whole part) // Fractional part // Set BAUDRATE // USART enable