void printWelcomeMessage(void) { char *strings[] = {"\033[0;0H", "\033[2J", WELCOME_MSG, MAIN_MENU, PROMPT}; for (uint8_t i = 0; i < 5; i++) { UART_Transmit(&huart2, (uint8_t*)strings[i], strlen(strings[i])); while (HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX || HAL_UART_GetState(&huart2) == HAL_UART_STATE_BUSY_TX_RX); } }
/** Attempts to determine if the serial peripheral is already in use for RX * * @param obj The serial object * @return Non-zero if the RX transaction is ongoing, 0 otherwise */ uint8_t serial_rx_active(serial_t *obj) { MBED_ASSERT(obj); UartHandle.Instance = (USART_TypeDef *)(SERIAL_OBJ(uart)); return ((HAL_UART_GetState(&UartHandle) & UART_STATE_RX_ACTIVE) ? 1 : 0); }
/** Attempts to determine if the serial peripheral is already in use for RX * * @param obj The serial object * @return Non-zero if the RX transaction is ongoing, 0 otherwise */ uint8_t serial_rx_active(serial_t *obj) { MBED_ASSERT(obj); UART_HandleTypeDef *handle = &UartHandle[SERIAL_OBJ(index)]; return ((HAL_UART_GetState(handle) & UART_STATE_RX_ACTIVE) ? 1 : 0); }
int uart_dma_write(char *ptr, int len) { HAL_UART_StateTypeDef ustate; if (len > TX_BUFFER_LEN - tx_idx_write) len = TX_BUFFER_LEN - tx_idx_write; if (len <= 0) /* full */ return 0; memcpy(tx_buffer + tx_idx_write, ptr, len); tx_idx_write += len; ustate = HAL_UART_GetState(&huart1); if (ustate == HAL_UART_STATE_READY || ustate == HAL_UART_STATE_BUSY_RX) { int l = tx_idx_write - tx_idx_dma; uint8_t * p = (uint8_t *)(tx_buffer + tx_idx_dma); tx_idx_dma = tx_idx_write; HAL_UART_Transmit_DMA(&huart1, p, l); } return len; }
void Trace_Raw( const char * format, ... ) { int iMaxSize = TRACE_OUTPUT_BUFFER_SIZE - iIndex - 2; int iLastIndex=iIndex,iTickCount=0; va_list args; if( (TRACE_OUTPUT_BUFFER_SIZE-iIndex)>1 ) { va_start (args, format); iIndex += vsnprintf (&pcOutputBuffer[iIndex],iMaxSize,format, args); if( iIndex >= 511 ) { iIndex = iLastIndex; while( HAL_UART_GetState( &huart6 ) != HAL_UART_STATE_READY ) { HAL_Delay(1); iTickCount++; } Trace_Task(); iIndex += vsnprintf (&pcOutputBuffer[iIndex],TRACE_OUTPUT_BUFFER_SIZE-1,format, args); iIndex += snprintf(&pcOutputBuffer[iIndex],TRACE_OUTPUT_BUFFER_SIZE-iIndex-1,"Trace wait:%dms\r\n",iTickCount); } va_end (args); } }
void bluetoothWaitReady(void) { #if !defined DISABLE_BLUETOOTH // Wait until UART becomes ready while (HAL_UART_GetState(&huart3) != HAL_UART_STATE_READY); #endif }
/** * Attempts to determine if the serial peripheral is already in use for RX * * @param obj The serial object * @return Non-zero if the RX transaction is ongoing, 0 otherwise */ uint8_t serial_rx_active(serial_t *obj) { MBED_ASSERT(obj); struct serial_s *obj_s = SERIAL_S(obj); UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; return ((HAL_UART_GetState(huart) == HAL_UART_STATE_BUSY_RX) ? 1 : 0); }
uint8_t serial_rx_active(serial_t *obj) { UART_HandleTypeDef *handle = &UartHandle[obj->serial.module]; HAL_UART_StateTypeDef state = HAL_UART_GetState(handle); switch(state) { case HAL_UART_STATE_RESET: case HAL_UART_STATE_READY: case HAL_UART_STATE_ERROR: case HAL_UART_STATE_TIMEOUT: case HAL_UART_STATE_BUSY_TX: return 0; default: return 1; } }
/** * @brief Receives an amount of data in non blocking mode. * @note This function differs from HAL's function as it does not enable HalfTranferComplete * @param huart: pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @param pData: Pointer to data buffer * @param Size: Amount of data to be received * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit. * @retval HAL status */ static HAL_StatusTypeDef MBED_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) { uint32_t *tmp; uint32_t tmp1 = 0; tmp1 = HAL_UART_GetState(huart); if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX)) { if((pData == NULL ) || (Size == 0)) { return HAL_ERROR; } /* Process Locked */ __HAL_LOCK(huart); huart->pRxBuffPtr = pData; huart->RxXferSize = Size; huart->ErrorCode = HAL_UART_ERROR_NONE; /* Check if a transmit process is ongoing or not */ huart->RxState = HAL_UART_STATE_BUSY_RX; /* Set the UART DMA transfer complete callback */ huart->hdmarx->XferCpltCallback = h_UART_DMAReceiveCplt; /* Set the UART DMA Half transfer complete callback */ huart->hdmarx->XferHalfCpltCallback = h_UART_DMARxHalfCplt; /* Set the DMA error callback */ huart->hdmarx->XferErrorCallback = h_UART_DMAError; /* Enable the DMA Stream */ tmp = (uint32_t*)&pData; MBED_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size); /* Enable the DMA transfer for the receiver request by setting the DMAR bit in the UART CR3 register */ huart->Instance->CR3 |= USART_CR3_DMAR; /* Process Unlocked */ __HAL_UNLOCK(huart); return HAL_OK; } else { return HAL_BUSY; } }
/** * @brief Uart Initialization Function. * @param numUart: numero della periferica UART da inizializzare. * numUart può assumere uno dei seguenti valori: * @arg HELPER_UART1: periferica UART 1; * @arg HELPER_UART2: periferica UART 2. * @arg HELPER_UART3: periferica UART 3; * @arg HELPER_UART4: periferica UART 4. * @arg HELPER_UART5: periferica UART 5; * @arg HELPER_UART6: periferica UART 6. * * @retval Stato, che può assumere uno dei seguenti valori: * @arg HELPER_UART_OK: inizializzazione avvenuta con successo; * @arg HELPER_UART_ERROR: Errore qualsiasi avvenuto durante l'Inizializzazione. */ HELPER_UART_Status_TypeDef HELPER_UART_Init(HELPER_UART_TypeDef numUart){ //A seconda della periferica mi riferisco ad un Handle Specifico UART_HandleTypeDef *UartHandle_x=HELPER_UART_GetHandle(numUart); if(HAL_UART_GetState(UartHandle_x) == HAL_UART_STATE_RESET) { /* UART configuration -----------------------------------------------------*/ UartHandle_x->Instance = HELPER_UART(numUart); UartHandle_x->Init.Mode = HELPER_UART_MODE(numUart); UartHandle_x->Init.WordLength = HELPER_UART_LENGHT(numUart); UartHandle_x->Init.StopBits = HELPER_UART_STOP_B(numUart); UartHandle_x->Init.Parity = HELPER_UART_PARITY(numUart); UartHandle_x->Init.BaudRate = HELPER_UART_BAUDRATE(numUart); UartHandle_x->Init.HwFlowCtl = HELPER_UART_HWCONTROL(numUart); UartHandle_x->Init.OverSampling= HELPER_UART_OVERSAMPLING(numUart); //La HAL_UART_MspInit(UartHandle_x) sarà chiamata internamente dalla HAL_XYZ_Init(..) HAL_StatusTypeDef status = 0; switch(HELPER_UART_FUNCTIONAL_MODE(numUart)) { case HELPER_UART_HALF_DUPLEX_MODE: status = HAL_HalfDuplex_Init(UartHandle_x);break; case HELPER_UART_LIN_MODE: status = HAL_LIN_Init(UartHandle_x,UART_LINBREAKDETECTLENGTH_11B);break; case HELPER_UART_MULTIPROCESSOR_MODE: status = HAL_MultiProcessor_Init(UartHandle_x, HELPER_UART_MULTIPROCESSOR_ADDRESS(numUart), HELPER_UART_MULTIPROCESSOR_WAKEUP_METHOD(numUart) ); break; default: status = HAL_UART_Init(UartHandle_x); } if(status!=HAL_OK) return HELPER_UART_ERROR; else return HELPER_UART_OK; } else return HELPER_UART_ERROR; }
HAL_StatusTypeDef WifiCom_Transmit_Reglage ( void ) { uint32_t etat_uart = HAL_UART_GetState ( &Wifi_Com_Uart ) ; if ( ( etat_uart != HAL_UART_STATE_BUSY_TX ) && ( etat_uart != HAL_UART_STATE_BUSY_TX_RX ) ) { /* Charge le buffer émission */ Wifi_Buf_Emi.Transport.Header1 = HEADER1 ; Wifi_Buf_Emi.Transport.Header2 = HEADER2 ; Wifi_Buf_Emi.Transport.Type = TYPE_MSG_REGLAGE ; Wifi_Buf_Emi.Transport.Total_Octet = 0 ; Wifi_Buf_Emi.Transport.Position_Premier_Octet = 0 ; Wifi_Buf_Emi.Transport.Nombre_Doctets = TAILLE_MESSAGE_REGLAGE ; Wifi_Buf_Emi.Reglage.Etat_Moteur_Droit = Etat_Moteur[MOTEUR_DROIT] ; Wifi_Buf_Emi.Reglage.Etat_Moteur_Gauche = Etat_Moteur[MOTEUR_GAUCHE] ; Wifi_Buf_Emi.Reglage.ConsigneManuelle.Vitesse = ConsigneManuelle.Vitesse ; Wifi_Buf_Emi.Reglage.ConsigneManuelle.ConsigneAngulaire = ConsigneManuelle.ConsigneAngulaire ; Wifi_Buf_Emi.Reglage.VitesseMesure[MOTEUR_DROIT] = VitesseMesure[MOTEUR_DROIT] ; Wifi_Buf_Emi.Reglage.VitesseMesure[MOTEUR_GAUCHE] = VitesseMesure[MOTEUR_GAUCHE] ; Wifi_Buf_Emi.Reglage.Phare_Luminosite = Phare_Luminosite ; Wifi_Buf_Emi.Reglage.Temperature_Exterieure = Etat_Automate_Suivi_Mur ;//= Temperature_Exterieure ; Wifi_Buf_Emi.Reglage.V_Batterie = V_Batterie ; Wifi_Buf_Emi.Reglage.Etat_Sauvegarde = Etat_Sauvegarde; Wifi_Buf_Emi.Reglage.Mode_Commande = Mode_Commande ; Wifi_Buf_Emi.Reglage.ConsigneBoucle_Ouverte_Moteur_Droit = ConsigneBoucle_Ouverte[MOTEUR_DROIT]; Wifi_Buf_Emi.Reglage.ConsigneBoucle_Ouverte_Moteur_Gauche = ConsigneBoucle_Ouverte[MOTEUR_GAUCHE] ; Wifi_Buf_Emi.Reglage.Status_Lidar = Status_Rplidar ; Wifi_Buf_Emi.Reglage.Status_Moteur_Lidar = Status_Moteur_Lidar; Wifi_Buf_Emi.Reglage.Erreur_Code_Lidar = ErrorCode_Rplidar; Wifi_Buf_Emi.Reglage.Offset_Orientation_Lidar = Offset_Orientation_Lidar ; Wifi_Buf_Emi.Reglage.NotUsed1 = Offset_Orientation_Lidar ; // dispo apour autre Wifi_Buf_Emi.Reglage.Compas = Compas ; Wifi_Buf_Emi.Reglage.ConsigneAngulaireNulle = ConsigneAngulaireNulle ; Wifi_Buf_Emi.Reglage.Compensation_Ligne_Droite = Compensation_Ligne_Droite; Wifi_Buf_Emi.Reglage.Distance_Obstacle_Av = Distance_Obstacle_Av ; Wifi_Buf_Emi.Reglage.Distance_Obstacle_Ar = Distance_Obstacle_Ar ; Wifi_Buf_Emi.Reglage.Suivi_De_Mur = Suivi_De_Mur; Wifi_Buf_Emi.Reglage.Crc = Calcule_Checksum ( Wifi_Buf_Emi.Word, TAILLE_MESSAGE_REGLAGE /4); Max_Repetition = MAX_REPETITION ; return ( HAL_UART_Transmit_DMA(&Wifi_Com_Uart, Wifi_Buf_Emi.Octet, TAILLE_MESSAGE) ); } else return ( HAL_BUSY ); }
void Trace_Task( void ) { if( iIndex ) { if( HAL_UART_GetState( &huart6 ) == HAL_UART_STATE_READY ) { pcOutputBuffer[TRACE_OUTPUT_BUFFER_SIZE-1]=0; HAL_UART_Transmit_DMA( &huart6 ,(uint8_t *) pcOutputBuffer, iIndex ); if( pcOutputBuffer == tcOutputBufferPing ) { pcOutputBuffer = tcOutputBufferPong; } else { pcOutputBuffer = tcOutputBufferPing; } iIndex = 0; } } }
void uartStartTxDMA(uartPort_t *s) { uint16_t size = 0; uint32_t fromwhere=0; HAL_UART_StateTypeDef state = HAL_UART_GetState(&s->Handle); if ((state & HAL_UART_STATE_BUSY_TX) == HAL_UART_STATE_BUSY_TX) return; if (s->port.txBufferHead > s->port.txBufferTail) { size = s->port.txBufferHead - s->port.txBufferTail; fromwhere = s->port.txBufferTail; s->port.txBufferTail = s->port.txBufferHead; } else { size = s->port.txBufferSize - s->port.txBufferTail; fromwhere = s->port.txBufferTail; s->port.txBufferTail = 0; } s->txDMAEmpty = false; //HAL_CLEANCACHE((uint8_t *)&s->port.txBuffer[fromwhere],size); HAL_UART_Transmit_DMA(&s->Handle, (uint8_t *)&s->port.txBuffer[fromwhere], size); }
HAL_StatusTypeDef WifiCom_Transmit_Service ( T_Requete Requete, T_Ack_Nack Ack_Nack, T_Presence_Host Etat_Host ) { uint32_t etat_uart = HAL_UART_GetState ( &Wifi_Com_Uart ) ; if ( ( etat_uart != HAL_UART_STATE_BUSY_TX ) && ( etat_uart != HAL_UART_STATE_BUSY_TX_RX ) ) { /* Charge le buffer émission */ Wifi_Buf_Emi.Transport.Header1 = HEADER1 ; Wifi_Buf_Emi.Transport.Header2 = HEADER2 ; Wifi_Buf_Emi.Transport.Type = TYPE_MSG_DE_SERVICE ; Wifi_Buf_Emi.Transport.Total_Octet = 0 ; Wifi_Buf_Emi.Transport.Position_Premier_Octet = 0 ; Wifi_Buf_Emi.Transport.Nombre_Doctets = TAILLE_MESSAGE_DE_SERVICE ; Wifi_Buf_Emi.De_Service.Type.Service.Requete = Requete ; Wifi_Buf_Emi.De_Service.Type.Service.Ack_Nack = Ack_Nack ; Wifi_Buf_Emi.De_Service.Type.Service.Presence_Host = Etat_Host ; Wifi_Buf_Emi.De_Service.Crc = Calcule_Checksum ( Wifi_Buf_Emi.Word, TAILLE_MESSAGE_DE_SERVICE /4); Max_Repetition = 0 ; return ( HAL_UART_Transmit_DMA(&Wifi_Com_Uart, Wifi_Buf_Emi.Octet, TAILLE_MESSAGE) ); } else return ( HAL_BUSY ); }
int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint) #endif { // DMA usage is currently ignored (void) hint; // Check buffer is ok MBED_ASSERT(tx != (void*)0); MBED_ASSERT(tx_width == 8); // support only 8b width if (tx_length == 0) return 0; // Set up buffer h_serial_tx_buffer_set(obj, (void *)tx, tx_length, tx_width); // Set up events h_serial_tx_enable_event(obj, SERIAL_EVENT_TX_ALL, 0); // Clear all events h_serial_tx_enable_event(obj, event, 1); // Set only the wanted events UART_HandleTypeDef *handle = &UartHandle[SERIAL_OBJ(index)]; // Enable interrupt IRQn_Type irqn = h_serial_get_irq_index(obj); NVIC_ClearPendingIRQ(irqn); NVIC_DisableIRQ(irqn); NVIC_SetPriority(irqn, 1); NVIC_SetVector(irqn, (uint32_t)handler); NVIC_EnableIRQ(irqn); #if DEVICE_SERIAL_ASYNCH_DMA // Enable DMA interrupt irqn = h_serial_tx_get_irqdma_index(obj); NVIC_ClearPendingIRQ(irqn); NVIC_DisableIRQ(irqn); NVIC_SetPriority(irqn, 1); NVIC_SetVector(irqn, (uint32_t)handler); NVIC_EnableIRQ(irqn); // the following function will enable program and enable the DMA transfer if (HAL_UART_Transmit_DMA(handle, (uint8_t*)tx, tx_length) != HAL_OK) { /* Transfer error in transmission process */ return 0; } #else // the following function will enable UART_IT_TXE and error interrupts if (HAL_UART_Transmit_IT(handle, (uint8_t*)tx, tx_length) != HAL_OK) { /* Transfer error in transmission process */ return 0; } #endif DEBUG_PRINTF("UART%u: Tx: 0=(%u, %u) %x\n", obj->serial.module+1, tx_length, tx_width, HAL_UART_GetState(handle)); return tx_length; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization: global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure leds */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* UART configured as follows: - Word Length = 8 Bits (7 data bit + 1 parity bit) : BE CAREFUL : Program 7 data bits + 1 parity bit in PC HyperTerminal - Stop Bit = One Stop bit - Parity = ODD parity - BaudRate = 9600 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 9600; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_ODD; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; if(HAL_UART_Init(&UartHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Start the transmission process #####################################*/ /* While the UART in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxStartMessage, TXSTARTMESSAGESIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /*##-3- Put UART peripheral in reception process ###########################*/ /* Any data received will be stored "aRxBuffer" buffer : the number max of data received is 10 */ if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-4- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { if (tx_complete == 1) { BSP_LED_Toggle(LED1); HAL_Delay(250); } } if (rx_complete == 1) { BSP_LED_On(LED1); } /*##-5- Send the received Buffer ###########################################*/ if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /*##-6- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /*##-7- Send the End Message ###############################################*/ if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxEndMessage, TXENDMESSAGESIZE)!= HAL_OK) { /* Turn LED3 on: Transfer error in transmission process */ BSP_LED_On(LED3); while(1) { } } /*##-8- Wait for the end of the transfer ###################################*/ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED1, LED2 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* UART1 configured as follow: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = ODD parity - BaudRate = 9600 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 9600; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_ODD; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; if(HAL_UART_Init(&UartHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Start the transmission process #####################################*/ /* User start transmission data through "TxBuffer" buffer */ if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxStartMessage, TXSTARTMESSAGESIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /*##-3- Put UART peripheral in reception process ###########################*/ /* Any data received will be stored in "RxBuffer" buffer : the number max of data received is 10 */ if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-4- Wait for the end of the transfer ###################################*/ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /*##-5- Send the received Buffer ###########################################*/ if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /*##-6- Wait for the end of the transfer ###################################*/ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /*##-7- Send the End Message ###############################################*/ if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxEndMessage, TXENDMESSAGESIZE)!= HAL_OK) { /* Turn LED3 on: Transfer error in transmission process */ BSP_LED_On(LED3); while(1) { } } /*##-8- Wait for the end of the transfer ###################################*/ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /* Infinite loop */ while (1) { } }
HAL_UART_StateTypeDef Uart::GetState() { return HAL_UART_GetState(&m_huart); }
/** * @brief Main program * @param None * @retval None */ void UART_Hyperterminal_IT_example(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 180 Mhz */ SystemClock_Config(); /* Configure LED1, LED2 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* UART1 configured as follow: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = ODD parity - BaudRate = 9600 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 9600; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_ODD; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; if(HAL_UART_Init(&UartHandle) != HAL_OK) { /* Turn LED3 on: in case of Initiliazation Error */ BSP_LED_On(LED3); while(1) { } } /*##-2- Start the transmission process #####################################*/ /* While the UART in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Turn LED3 on: Transfer error in transmission process */ BSP_LED_On(LED3); while(1) { } } /*##-3- Put UART peripheral in reception process ###########################*/ /* Any data received will be stored "aRxBuffer" buffer : the number max of data received is 10 */ if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Turn LED3 on: Transfer error in reception process */ BSP_LED_On(LED3); while(1) { } } /*##-4- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /*##-5- Send the received Buffer ###########################################*/ if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Turn LED3 on: Transfer error in transmission process */ BSP_LED_On(LED3); while(1) { } } /*##-6- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /* Infinite loop */ while (1) { } }
/** Begin asynchronous RX transfer (enable interrupt for data collecting) * The used buffer is specified in the serial object - rx_buff * * @param obj The serial object * @param rx The buffer for sending * @param rx_length The number of words to transmit * @param rx_width The bit width of buffer word * @param handler The serial handler * @param event The logical OR of events to be registered * @param handler The serial handler * @param char_match A character in range 0-254 to be matched * @param hint A suggestion for how to use DMA with this transfer */ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint) { // DMA usage is currently ignored (void) hint; /* Sanity check arguments */ MBED_ASSERT(obj); MBED_ASSERT(rx != (void*)0); MBED_ASSERT(rx_width == 8); // support only 8b width h_serial_rx_enable_event(obj, SERIAL_EVENT_RX_ALL, 0); h_serial_rx_enable_event(obj, event, 1); // set CharMatch if (char_match != SERIAL_RESERVED_CHAR_MATCH) { obj->char_match = char_match; } h_serial_rx_buffer_set(obj, rx, rx_length, rx_width); IRQn_Type irqn = h_serial_get_irq_index(obj); NVIC_ClearPendingIRQ(irqn); NVIC_DisableIRQ(irqn); NVIC_SetPriority(irqn, 0); NVIC_SetVector(irqn, (uint32_t)handler); NVIC_EnableIRQ(irqn); UART_HandleTypeDef *handle = &UartHandle[SERIAL_OBJ(index)]; // flush current data + error flags __HAL_UART_CLEAR_PEFLAG(handle); #if DEVICE_SERIAL_ASYNCH_DMA // Enable DMA interrupt irqn = h_serial_rx_get_irqdma_index(obj); NVIC_ClearPendingIRQ(irqn); NVIC_DisableIRQ(irqn); NVIC_SetPriority(irqn, 1); NVIC_SetVector(irqn, (uint32_t)handler); NVIC_EnableIRQ(irqn); // following HAL function will program and enable the DMA transfer MBED_UART_Receive_DMA(handle, (uint8_t*)rx, rx_length); #else // following HAL function will enable the RXNE interrupt + error interrupts HAL_UART_Receive_IT(handle, (uint8_t*)rx, rx_length); #endif /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ __HAL_UART_ENABLE_IT(handle, UART_IT_ERR); DEBUG_PRINTF("UART%u: Rx: 0=(%u, %u, %u) %x\n", obj->serial.module+1, rx_length, rx_width, char_match, HAL_UART_GetState(handle)); return; }
void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint) { // TODO: DMA usage is currently ignored (void) hint; bool use_rx = (rx != NULL && rx_length > 0); IRQn_Type irq_n = UartIRQs[obj->serial.module]; if (!use_rx || !irq_n) return; obj->rx_buff.buffer = rx; obj->rx_buff.length = rx_length; obj->rx_buff.pos = 0; obj->rx_buff.width = rx_width; obj->serial.event = (obj->serial.event & ~SERIAL_EVENT_RX_MASK) | (event & SERIAL_EVENT_RX_MASK); obj->serial.char_match = char_match; UART_HandleTypeDef *handle = &UartHandle[obj->serial.module]; // register the thunking handler vIRQ_SetVector(irq_n, handler); vIRQ_EnableIRQ(irq_n); // HAL_StatusTypeDef rc = HAL_UART_Receive_IT(handle, rx, rx_length); handle->pRxBuffPtr = rx; handle->RxXferSize = rx_length; handle->RxXferCount = rx_length; if(handle->State == HAL_UART_STATE_BUSY_TX) { handle->State = HAL_UART_STATE_BUSY_TX_RX; } else { handle->State = HAL_UART_STATE_BUSY_RX; } __HAL_UART_CLEAR_PEFLAG(handle); handle->Instance->CR1 |= USART_CR1_RXNEIE | USART_CR1_PEIE; handle->Instance->CR3 |= USART_CR3_EIE; DEBUG_PRINTF("UART%u: Rx: 0=(%u, %u, %u) %x\n", obj->serial.module+1, rx_length, rx_width, char_match, HAL_UART_GetState(handle)); }
/** @brief: check UartDMA * @return: true-> uartDMA s ready to send ****************************************************************/ int32_t IsUartDMAReady(UART_HandleTypeDef * huart) { int32_t state = HAL_UART_GetState(huart); return (state == HAL_UART_STATE_READY || state == HAL_UART_STATE_BUSY_RX); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F0xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Low Level Initialization */ HAL_Init(); /* Configure LED1, LED2 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /* Configure the system clock to 48 MHz */ SystemClock_Config(); /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* UART configured as follows: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = ODD parity - BaudRate = 9600 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 9600; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_ODD; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; if (HAL_UART_Init(&UartHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Start the transmission process #####################################*/ /* User start transmission data through "TxBuffer" buffer */ if (HAL_UART_Transmit_DMA(&UartHandle, (uint8_t *)aTxBuffer, TXBUFFERSIZE) != HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /*##-3- Put UART peripheral in reception process ###########################*/ /* Any data received will be stored in "RxBuffer" buffer : the number max of data received is 10 */ if (HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-4- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /*##-5- Send the received Buffer ###########################################*/ if (HAL_UART_Transmit_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /*##-6- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY) { } /* Infinite loop */ while (1) { } }
int serial_tx_asynch(serial_t *obj, void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint) { // TODO: DMA usage is currently ignored (void) hint; bool use_tx = (tx != NULL && tx_length > 0); IRQn_Type irq_n = UartIRQs[obj->serial.module]; if (!use_tx || !irq_n) return 0; obj->tx_buff.buffer = tx; obj->tx_buff.length = tx_length; obj->tx_buff.pos = 0; obj->tx_buff.width = tx_width; obj->serial.event = (obj->serial.event & ~SERIAL_EVENT_TX_MASK) | (event & SERIAL_EVENT_TX_MASK); // register the thunking handler vIRQ_SetVector(irq_n, handler); vIRQ_EnableIRQ(irq_n); UART_HandleTypeDef *handle = &UartHandle[obj->serial.module]; // HAL_StatusTypeDef rc = HAL_UART_Transmit_IT(handle, tx, tx_length); // manually implemented HAL_UART_Transmit_IT for tighter control of what it does handle->pTxBuffPtr = tx; handle->TxXferSize = tx_length; handle->TxXferCount = tx_length; if(handle->State == HAL_UART_STATE_BUSY_RX) { handle->State = HAL_UART_STATE_BUSY_TX_RX; } else { handle->State = HAL_UART_STATE_BUSY_TX; } // if the TX register is empty, directly input the first transmit byte if (__HAL_UART_GET_FLAG(handle, UART_FLAG_TXE)) { handle->Instance->DR = *handle->pTxBuffPtr++; handle->TxXferCount--; } // chose either the tx reg empty or if last byte wait directly for tx complete if (handle->TxXferCount != 0) { handle->Instance->CR1 |= USART_CR1_TXEIE; } else { handle->Instance->CR1 |= USART_CR1_TCIE; } DEBUG_PRINTF("UART%u: Tx: 0=(%u, %u) %x\n", obj->serial.module+1, tx_length, tx_width, HAL_UART_GetState(handle)); return tx_length; }