这几天一直在捣鼓板子,查资料现学现卖,觉都睡不好,一遍熟悉知识点消化,一边总结。做梦都在搞板子!由于第一次使用Hal库,第一次使用新的编译器,又在LL库的边缘徘徊,期间又切换到RTTstudio。 最初几天串口没有调试通,很郁闷,看到别的小伙伴一篇篇的优质测评帖,我心里急啊。今天,我的第三篇测评,他终于来了!
1. 首先,来个驱动讲解吧,这里要注意的是一个回调函数,我们基本不用管就行,但是和初始化函数长得很像,初学者容易搞糊涂。
那么我们在使用的时候,main函数中调用A函数初始化即可。
C函数为回调函数,其他外设初始化的时候也会自动产生,C函数已经被B函数调用了!B函数在HAL库中。
这就是三个长得像的家伙的关系。
CubeMX有个特点,函数前都不会有啥注释的。哈哈。
下面就是初始化代码了,这个是CubeMx自动产生的:
- #include "usart.h"
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- UART_HandleTypeDef hlpuart1;
- UART_HandleTypeDef huart1;
- /* LPUART1 init function */
- void MX_LPUART1_UART_Init(void)
- {
- /* USER CODE BEGIN LPUART1_Init 0 */
- /* USER CODE END LPUART1_Init 0 */
- /* USER CODE BEGIN LPUART1_Init 1 */
- /* USER CODE END LPUART1_Init 1 */
- hlpuart1.Instance = LPUART1;
- hlpuart1.Init.BaudRate = 115200;
- hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
- hlpuart1.Init.StopBits = UART_STOPBITS_1;
- hlpuart1.Init.Parity = UART_PARITY_NONE;
- hlpuart1.Init.Mode = UART_MODE_TX_RX;
- hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
- hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&hlpuart1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN LPUART1_Init 2 */
- /* USER CODE END LPUART1_Init 2 */
- }
- /* USART1 init function */
- void MX_USART1_UART_Init(void)
- {
- /* USER CODE BEGIN USART1_Init 0 */
- /* USER CODE END USART1_Init 0 */
- /* USER CODE BEGIN USART1_Init 1 */
- /* USER CODE END USART1_Init 1 */
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_9B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
- huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART1_Init 2 */
- /* USER CODE END USART1_Init 2 */
- }
- void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(uartHandle->Instance==LPUART1)
- {
- /* USER CODE BEGIN LPUART1_MspInit 0 */
- /* USER CODE END LPUART1_MspInit 0 */
- /* LPUART1 clock enable */
- __HAL_RCC_LPUART1_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**LPUART1 GPIO Configuration
- PA2 ------> LPUART1_TX
- PA3 ------> LPUART1_RX
- */
- GPIO_InitStruct.Pin = LPUART1_TX_Pin|LPUART1_RX_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.Alternate = GPIO_AF12_LPUART1;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* USER CODE BEGIN LPUART1_MspInit 1 */
- /* USER CODE END LPUART1_MspInit 1 */
- }
- else if(uartHandle->Instance==USART1)
- {
- /* USER CODE BEGIN USART1_MspInit 0 */
- /* USER CODE END USART1_MspInit 0 */
- /* USART1 clock enable */
- __HAL_RCC_USART1_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- /**USART1 GPIO Configuration
- PC4 ------> USART1_TX
- PC5 ------> USART1_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /* USER CODE BEGIN USART1_MspInit 1 */
- /* USER CODE END USART1_MspInit 1 */
- }
- }
- void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
- {
- if(uartHandle->Instance==LPUART1)
- {
- /* USER CODE BEGIN LPUART1_MspDeInit 0 */
- /* USER CODE END LPUART1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_LPUART1_CLK_DISABLE();
- /**LPUART1 GPIO Configuration
- PA2 ------> LPUART1_TX
- PA3 ------> LPUART1_RX
- */
- HAL_GPIO_DeInit(GPIOA, LPUART1_TX_Pin|LPUART1_RX_Pin);
- /* USER CODE BEGIN LPUART1_MspDeInit 1 */
- /* USER CODE END LPUART1_MspDeInit 1 */
- }
- else if(uartHandle->Instance==USART1)
- {
- /* USER CODE BEGIN USART1_MspDeInit 0 */
- /* USER CODE END USART1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_USART1_CLK_DISABLE();
- /**USART1 GPIO Configuration
- PC4 ------> USART1_TX
- PC5 ------> USART1_RX
- */
- HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4|GPIO_PIN_5);
- /* USER CODE BEGIN USART1_MspDeInit 1 */
- /* USER CODE END USART1_MspDeInit 1 */
- }
- }
复制代码
然后:重点来了:这个部分还是要自己添加才行,CubeMX还没有达到100%给你写代码的水平哦:
- /**
- * @brief Retargets the C library printf function to the USART.
- * @param None
- * @retval None
- */
- #ifdef __GNUC__
- /* With GCC, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- PUTCHAR_PROTOTYPE
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the USART1 and Loop until the end of transmission */
- HAL_UART_Transmit(&hlpuart1, (uint8_t *)&ch, 1, 0xFFFF);
- return ch;
- }
复制代码
整个工程编译下来占用空间大小:
==============================================================================
Code (inc. data) RO Data RW Data ZI Data Debug
8204 434 568 20 1316 691029 Grand Totals
8204 434 568 20 1316 691029 ELF Image Totals
8204 434 568 20 0 0 ROM Totals
==============================================================================
Total RO Size (Code + RO Data) 8772 ( 8.57kB)
Total RW Size (RW Data + ZI Data) 1336 ( 1.30kB)
Total ROM Size (Code + RO Data + RW Data) 8792 ( 8.59kB)
==============================================================================
最后,看看效果如何:
非常完美!
整个工程代码见附件:
|