uint8_t TM_USART_Getc(USART_TypeDef* USARTx) {
	uint8_t usart_num = TM_USART_GetUsartNumber(USARTx);
	uint8_t c = 0;
	//Check if we have any data in buffer
	if (tm_usart_buf_num[usart_num] > 0) {
		if (tm_usart_buf_out[usart_num] > (TM_USART_BUFFER_SIZE - 1)) {
			tm_usart_buf_out[usart_num] = 0;
		}
		c = TM_USART_Buffer[usart_num][tm_usart_buf_out[usart_num]];
		TM_USART_Buffer[usart_num][tm_usart_buf_out[usart_num]] = 0;
		tm_usart_buf_out[usart_num]++;
		tm_usart_buf_num[usart_num]--;
	}
	return c;
}
Ejemplo n.º 2
0
uint8_t TM_USART_FindCharacter(USART_TypeDef* USARTx, volatile char c) {
	uint16_t num, out;
	uint8_t usart_num = TM_USART_GetUsartNumber(USARTx);
	
	num = tm_usart_buf_num[usart_num];
	out = tm_usart_buf_out[usart_num];
	
	while (num > 0) {
		if (out > (TM_USART_BUFFER_SIZE - 1)) {
			out = 0;
		}
		if (TM_USART_Buffer[usart_num][out] == c) {
			/* Character founc */
			return 1;
		}
		out++;
		num--;
	}
	
	/* Character is not in buffer */
	return 0;
}
uint8_t TM_USART_BufferEmpty(USART_TypeDef* USARTx) {
	uint8_t usart_num = TM_USART_GetUsartNumber(USARTx);
	return !(tm_usart_buf_num[usart_num] > 0);
}
void TM_USART_Init(USART_TypeDef* USARTx, TM_USART_PinsPack_t pinspack, uint32_t baudrate) {
	/**
	 * Initialization structures declared
	 *
	 * USART: Universal Synchronous/Asynchronous Receiver/Transmitter
	 * NVIC: Nested Vector Interrupt Controller
	 */
	USART_InitTypeDef 	USART_InitStruct;
	NVIC_InitTypeDef	NVIC_InitStruct;

	/**
	 * Default settings:
	 * 
	 * Set Baudrate to value you pass to function
	 * Disable Hardware Flow control
	 * Set Mode To TX and RX, so USART will work in full-duplex mode
	 * Disable parity bit
	 * Set 1 stop bit
	 * Set Data bits to 8
	 */
	USART_InitStruct.USART_BaudRate = baudrate;
	
	/*
	 * Initialize USARTx pins
	 * Set channel for USARTx NVIC
	 */
#ifdef USART1
	if (USARTx == USART1) {
		TM_USART1_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_USART1_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_USART1_MODE;
		USART_InitStruct.USART_Parity = TM_USART1_PARITY;
		USART_InitStruct.USART_StopBits = TM_USART1_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_USART1_WORD_LENGTH;
	}
#endif
#ifdef USART2
	if (USARTx == USART2) {
		TM_USART2_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_USART2_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_USART2_MODE;
		USART_InitStruct.USART_Parity = TM_USART2_PARITY;
		USART_InitStruct.USART_StopBits = TM_USART2_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_USART2_WORD_LENGTH;
	}
#endif
#ifdef USART3
	if (USARTx == USART3) {
		TM_USART3_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_USART3_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_USART3_MODE;
		USART_InitStruct.USART_Parity = TM_USART3_PARITY;
		USART_InitStruct.USART_StopBits = TM_USART3_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_USART3_WORD_LENGTH;
	}
#endif
#ifdef UART4
	if (USARTx == UART4) {
		TM_UART4_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = UART4_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_UART4_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_UART4_MODE;
		USART_InitStruct.USART_Parity = TM_UART4_PARITY;
		USART_InitStruct.USART_StopBits = TM_UART4_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_UART4_WORD_LENGTH;
	}
#endif
#ifdef UART5
	if (USARTx == UART5) {
		TM_UART5_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = UART5_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_UART5_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_UART5_MODE;
		USART_InitStruct.USART_Parity = TM_UART5_PARITY;
		USART_InitStruct.USART_StopBits = TM_UART5_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_UART5_WORD_LENGTH;
	}
#endif
#ifdef USART6
	if (USARTx == USART6) {
		TM_USART6_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = USART6_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_USART6_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_USART6_MODE;
		USART_InitStruct.USART_Parity = TM_USART6_PARITY;
		USART_InitStruct.USART_StopBits = TM_USART6_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_USART6_WORD_LENGTH;
	}
#endif
#ifdef UART7_IRQn
	if (USARTx == UART7) {
		TM_UART7_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = UART7_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_UART7_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_UART7_MODE;
		USART_InitStruct.USART_Parity = TM_UART7_PARITY;
		USART_InitStruct.USART_StopBits = TM_UART7_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_UART7_WORD_LENGTH;
	}
#endif
#ifdef UART8_IRQn
	if (USARTx == UART8) {
		TM_UART8_InitPins(pinspack);
		NVIC_InitStruct.NVIC_IRQChannel = UART8_IRQn;
		
		USART_InitStruct.USART_HardwareFlowControl = TM_UART8_HARDWARE_FLOW_CONTROL;
		USART_InitStruct.USART_Mode = TM_UART8_MODE;
		USART_InitStruct.USART_Parity = TM_UART8_PARITY;
		USART_InitStruct.USART_StopBits = TM_UART8_STOP_BITS;
		USART_InitStruct.USART_WordLength = TM_UART8_WORD_LENGTH;
	}
#endif

	USART_Init(USARTx, &USART_InitStruct);
	USART_Cmd(USARTx, ENABLE);
	
	/**
	 * Enable RX interrupt
	 */
	USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);

	/**
	 * Set Channel Cmd to enable. That will enable USARTx channel in NVIC
	 * Set NVIC_IRQChannelPreemptionPriority. This means high priority for USARTs
	 * USARTx with lower x has high subpriority
	 *
	 * Initialize NVIC
	 */
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = TM_USART_NVIC_PRIORITY;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = TM_USART_GetUsartNumber(USARTx);
	NVIC_Init(&NVIC_InitStruct);
}
Ejemplo n.º 5
0
void TM_USART_ClearBuffer(USART_TypeDef* USARTx) {
	uint8_t usart_num = TM_USART_GetUsartNumber(USARTx);
	tm_usart_buf_num[usart_num] = 0;
	tm_usart_buf_in[usart_num] = 0;
	tm_usart_buf_out[usart_num] = 0;
}