1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| void UART_init(uart_id_e uart_id, uint32_t baudrate)
{
assert(baudrate > 1000);
assert(uart_id < UART_ID_NB);
buffer_rx_read_index[uart_id] = 0;
buffer_rx_write_index[uart_id] = 0;
buffer_rx_data_ready[uart_id] = FALSE;
/* USARTx configured as follow:
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- OverSampling: enable
*/
UART_HandleStructure[uart_id].Instance = (USART_TypeDef*)instance_array[uart_id];
UART_HandleStructure[uart_id].Init.BaudRate = baudrate;
UART_HandleStructure[uart_id].Init.WordLength = UART_WORDLENGTH_8B;//
UART_HandleStructure[uart_id].Init.StopBits = UART_STOPBITS_1;//
UART_HandleStructure[uart_id].Init.Parity = UART_PARITY_NONE;//
UART_HandleStructure[uart_id].Init.HwFlowCtl = UART_HWCONTROL_NONE;//
UART_HandleStructure[uart_id].Init.Mode = UART_MODE_TX_RX;//
UART_HandleStructure[uart_id].Init.OverSampling = UART_OVERSAMPLING_16;//
/*On applique les parametres d'initialisation ci-dessus */
HAL_UART_Init(&UART_HandleStructure[uart_id]);
/*Activation de l'UART */
__HAL_UART_ENABLE(&UART_HandleStructure[uart_id]);
// On fixe les priorités des interruptions de l'usart PreemptionPriority = 0, SubPriority = 1 et on autorise les interruptions
HAL_NVIC_SetPriority(nvic_irq_array[uart_id] , 1, 1);
HAL_NVIC_EnableIRQ(nvic_irq_array[uart_id]);
HAL_UART_Receive_IT(&UART_HandleStructure[uart_id],&buffer_rx[uart_id][buffer_rx_write_index[uart_id]],1); //Activation de la réception d'un caractère
//Config LibC: no buffering
setvbuf(stdout, NULL, _IONBF, 0 );
setvbuf(stderr, NULL, _IONBF, 0 );
setvbuf(stdin, NULL, _IONBF, 0 );
} |
Partager