中级会员
 
- 积分
- 263
- 金钱
- 263
- 注册时间
- 2023-10-17
- 在线时间
- 49 小时
|

楼主 |
发表于 2023-10-19 16:09:55
|
显示全部楼层
- #include "./SYSTEM/sys/sys.h"
- #include "./SYSTEM/usart/usart.h"
- /* 如果使用os,则包括下面的头文件即可 */
- #if SYS_SUPPORT_OS
- #include "os.h" /* os 使用 */
- #endif
- /******************************************************************************************/
- /* 加入以下代码, 支持printf函数, 而不需要选择use MicroLIB */
- #if 1
- #if (__ARMCC_VERSION >= 6010050) /* 使用AC6编译器时 */
- __asm(".global __use_no_semihosting\n\t"); /* 声明不使用半主机模式 */
- __asm(".global __ARM_use_no_argv \n\t"); /* AC6下需要声明main函数为无参数格式,否则部分例程可能出现半主机模式 */
- #else
- /* 使用AC5编译器时, 要在这里定义__FILE 和 不使用半主机模式 */
- #pragma import(__use_no_semihosting)
- struct __FILE
- {
- int handle;
- /* Whatever you require here. If the only file you are using is */
- /* standard output using printf() for debugging, no file handling */
- /* is required. */
- };
- #endif
- /* 不使用半主机模式,至少需要重定义_ttywrch\_sys_exit\_sys_command_string函数,以同时兼容AC6和AC5模式 */
- int _ttywrch(int ch)
- {
- ch = ch;
- return ch;
- }
- /* 定义_sys_exit()以避免使用半主机模式 */
- void _sys_exit(int x)
- {
- x = x;
- }
- char *_sys_command_string(char *cmd, int len)
- {
- return NULL;
- }
- /* FILE 在 stdio.h里面定义. */
- FILE __stdout;
- /* 重定义fputc函数, printf函数最终会通过调用fputc输出字符串到串口 */
- int fputc(int ch, FILE *f)
- {
- while ((USART1->SR & 0X40) == 0); /* 等待上一个字符发送完成 */
- USART1->DR = (uint8_t)ch; /* 将要发送的字符 ch 写入到DR寄存器 */
- return ch;
- }
- #endif
- /***********************************************END*******************************************/
-
- #if USART_EN_RX /* 如果使能了接收 */
- /* 接收缓冲, 最大USART_REC_LEN个字节. */
- uint8_t g_usart_rx_buf[USART_REC_LEN];
- /* 接收状态
- * bit15, 接收完成标志
- * bit14, 接收到0x0d
- * bit13~0, 接收到的有效字节数目
- */
- uint16_t g_usart_rx_sta = 0;
- uint8_t g_Rx_Finish_Flag;
- uint8_t g_rx_buffer[RXBUFFERSIZE]; /* HAL库使用的串口接收缓冲 */
- UART_HandleTypeDef g_uart1_handle; /* UART句柄 */
- /**
- * @brief 串口X初始化函数
- * [url=home.php?mod=space&uid=271674]@param[/url] baudrate: 波特率, 根据自己需要设置波特率值
- * [url=home.php?mod=space&uid=60778]@note[/url] 注意: 必须设置正确的时钟源, 否则串口波特率就会设置异常.
- * 这里的USART的时钟源在sys_stm32_clock_init()函数中已经设置过了.
- * @retval 无
- */
- void Usart_Init(uint32_t baudrate)
- {
- g_uart1_handle.Instance = USART1;
- g_uart1_handle.Init.BaudRate = baudrate;
- g_uart1_handle.Init.WordLength = UART_WORDLENGTH_8B;
- g_uart1_handle.Init.StopBits = UART_STOPBITS_1;
- g_uart1_handle.Init.Parity = UART_PARITY_NONE;
- g_uart1_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- g_uart1_handle.Init.Mode = UART_MODE_TX_RX;
- HAL_UART_Init(&g_uart1_handle);
-
- HAL_UART_Receive_IT(&g_uart1_handle, (uint8_t*)g_rx_buffer, RXBUFFERSIZE);
- HAL_UART_Transmit_IT(&g_uart1_handle, (uint8_t*)g_usart_rx_buf, 100);
- }
- /**
- * @brief UART底层初始化函数
- * @param huart: UART句柄类型指针
- * @note 此函数会被HAL_UART_Init()调用
- * 完成时钟使能,引脚配置,中断配置
- * @retval 无
- */
- void HAL_UART_MspInit(UART_HandleTypeDef *huart)
- {
- GPIO_InitTypeDef gpio_init_struct;
- if(huart->Instance == USART1) /* 如果是串口1,进行串口1 MSP初始化 */
- {
- __HAL_RCC_USART1_CLK_ENABLE(); /* USART1 时钟使能 */
- __HAL_RCC_GPIOA_CLK_ENABLE(); /* 发送接收引脚时钟使能 */
- gpio_init_struct.Pin = GPIO_PIN_9; /* TX引脚 */
- gpio_init_struct.Mode = GPIO_MODE_AF_PP; /* 复用推挽输出 */
- gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
- gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
- gpio_init_struct.Alternate = GPIO_AF7_USART1; /* 复用为USART1 */
- HAL_GPIO_Init(GPIOA, &gpio_init_struct); /* 初始化发送引脚 */
- gpio_init_struct.Pin = GPIO_PIN_10; /* RX引脚 */
- gpio_init_struct.Alternate = GPIO_AF7_USART1; /* 复用为USART1 */
- HAL_GPIO_Init(USART_RX_GPIO_PORT, &gpio_init_struct); /* 初始化接收引脚 */
- #if USART_EN_RX
- HAL_NVIC_EnableIRQ(USART_UX_IRQn); /* 使能USART1中断通道 */
- HAL_NVIC_SetPriority(USART_UX_IRQn, 3, 3); /* 抢占优先级3,子优先级3 */
- #endif
- }
- }
- /**
- * @brief Rx传输回调函数
- * @param huart: UART句柄类型指针
- * @retval 无
- */
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- if(huart->Instance == USART_UX) /* 如果是串口1 */
- {
- g_usart_rx_buf[g_usart_rx_sta] = g_rx_buffer[0] ;
-
- if(g_usart_rx_buf[g_usart_rx_sta] == 0x0a)
- {
- g_Rx_Finish_Flag = 1;
- }
- else
- {
- g_usart_rx_sta++;
- }
- HAL_UART_Receive_IT(&g_uart1_handle, (uint8_t *)g_rx_buffer, RXBUFFERSIZE);
- }
- }
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
- {
- if(huart->Instance == USART_UX) /* 如果是串口1 */
- {
- printf("发送已完成\r\n");
- HAL_UART_Transmit_IT(&g_uart1_handle, (uint8_t *)g_rx_buffer, RXBUFFERSIZE);
- }
- }
- /**
- * @brief 串口1中断服务函数
- * @param 无
- * @retval 无
- */
- void USART1_IRQHandler(void)
- {
- #if SYS_SUPPORT_OS /* 使用OS */
- OSIntEnter();
- #endif
- HAL_UART_IRQHandler(&g_uart1_handle); /* 调用HAL库中断处理公用函数 */
- #if SYS_SUPPORT_OS /* 使用OS */
- OSIntExit();
- #endif
- }
- #endif
-
-
复制代码 |
|