123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #include "hal_uart.h"
- #include "stm32f4xx.h"
- // #include <stdint.h>
- #include <sys/unistd.h>
- static uint8_t usart1_tx_buf[256];
- static uint8_t usart1_rx_buf[256];
- #define HAL_UART1_TX_PORT GPIOA
- #define HAL_UART1_TX_PIN GPIO_Pin_9
- #define HAL_UART1_TX_SOURCE GPIO_PinSource9
- #define HAL_UART1_RX_PORT GPIOA
- #define HAL_UART1_RX_PIN GPIO_Pin_10
- #define HAL_UART1_RX_SOURCE GPIO_PinSource10
- int usart1_snd_tx_cnt = 0;
- // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
- // 确保没有从 C 库链接使用半主机的函数
- #pragma import(__use_no_semihosting)
- #pragma import(__use_no_semihosting_swi)
- // 定义_sys_exit() 以避免使用半主机模式
- void _sys_exit(int x)
- {
- x = x;
- }
- // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
- // 标准库需要的支持函数
- struct __FILE
- {
- int handle;
- };
- /* FILE is typedef ’ d in stdio.h. */
- FILE __stdout;
- FILE __stdin;
- int _ttywrch(int ch)
- {
- ch = ch;
- return ch;
- }
- // && !defined(__clang__)
- #if defined(__GNUC__)
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #define GETCHAR_PROTOTYPE int __io_getchar(void)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
- #endif
- PUTCHAR_PROTOTYPE
- {
- uint32_t tick;
- usart1_snd_tx_cnt++;
- usart_send_it(&usart1_context, &ch, 1);
- tick = get_systick_ms();
- while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE))
- {
- if (get_systick_ms() - tick > 10)
- break;
- }
- return ch;
- }
- GETCHAR_PROTOTYPE
- {
- uint8_t ch;
- if (0 != usart_receive_read(&usart1_context, &ch, 1))
- {
- return ch;
- }
- return -1;
- }
- // &&!defined(__clang__)
- #if defined(__GNUC__)
- __attribute__((weak)) int _write(int file, char *ptr, int len)
- {
- (void)file;
- if ((file != STDOUT_FILENO) && (file != STDERR_FILENO))
- {
- return -1;
- }
- for (int index = 0; index < len; index++)
- __io_putchar(*ptr++);
- return len;
- }
- __attribute__((weak)) int _read(int file, char *ptr, int len)
- {
- (void)file;
- if (file != STDIN_FILENO)
- {
- return -1;
- }
- uint8_t ch;
- for (int index = 0; index < len; index++)
- {
- // *ptr++ = __io_getchar();
- if (0 != usart_receive_read(&usart1_context, &ch, 1))
- {
- return ch;
- }
- return -1;
- }
- return len;
- }
- #endif
- #if 0
- int getchar(void)
- {
- char c = 0;
- // ssize_t
- int bytes_read = read(STDIN_FILENO, &c, sizeof(char));
- if (bytes_read == -1)
- {
- // 读取错误
- return EOF;
- }
- else if (bytes_read == 0)
- {
- // 读取结束
- return EOF;
- }
- else
- {
- // 返回读取的字符
- return (int)c;
- }
- }
- #else
- int getchar(void)
- {
- uint8_t ch;
- if (0 != usart_receive_read(&usart1_context, &ch, 1))
- {
- return ch;
- }
- return -1;
- }
- #endif
- static void usart1_config(void)
- {
- GPIO_InitTypeDef GPIO_StructInit;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
- GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
- GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_StructInit.GPIO_Pin = HAL_UART1_TX_PIN;
- GPIO_PinAFConfig(HAL_UART1_TX_PORT, HAL_UART1_TX_SOURCE, GPIO_AF_USART1);
- GPIO_Init(HAL_UART1_TX_PORT, &GPIO_StructInit);
- GPIO_StructInit.GPIO_Pin = HAL_UART1_RX_PIN;
- GPIO_PinAFConfig(HAL_UART1_RX_PORT, HAL_UART1_RX_SOURCE, GPIO_AF_USART1);
- GPIO_Init(HAL_UART1_RX_PORT, &GPIO_StructInit);
- // Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 串口1中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
- NVIC_Init(&NVIC_InitStructure);
- }
- static void usart1_deconfig(void)
- {
- }
- usart_context_t usart1_context = {
- USART1,
- usart1_tx_buf,
- sizeof(usart1_tx_buf),
- 0,
- 0,
- 0,
- usart1_rx_buf,
- sizeof(usart1_rx_buf),
- 0,
- 0,
- 0,
- usart1_config,
- usart1_deconfig};
- void usart_config_init(usart_context_t *p_usart_content, uint32_t band_rate)
- {
- p_usart_content->config();
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = band_rate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(p_usart_content->usart_periph, &USART_InitStructure); // 初始化串口1
- USART_ITConfig(p_usart_content->usart_periph, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART1, ENABLE); // 使能串口1
- }
- void usart_config_deinit(usart_context_t *p_usart_content)
- {
- USART_DeInit(p_usart_content->usart_periph);
- p_usart_content->deconfig();
- }
- void usart_send(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
- {
- const uint8_t *send_buf = (const uint8_t *)_send_buf;
- uint16_t i;
- for (i = 0; i < send_count; i++)
- {
- while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
- ;
- USART_SendData(p_usart_content->usart_periph, (uint8_t)send_buf[i]);
- }
- while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
- ;
- }
- void usart_send_it(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
- {
- const uint8_t *send_buf = (const uint8_t *)_send_buf;
- uint16_t i;
- uint16_t left;
- // 空间不够直接返回
- if (p_usart_content->tx_wr >= p_usart_content->tx_rd)
- {
- left = p_usart_content->tx_buf_size - (p_usart_content->tx_wr - p_usart_content->tx_rd);
- }
- else
- {
- left = p_usart_content->tx_rd - p_usart_content->tx_wr;
- if (send_count > left)
- {
- p_usart_content->tx_err++;
- return;
- }
- }
- // 内存拷贝
- for (i = 0; i < send_count; i++)
- {
- // send buffer overflow just wait
- p_usart_content->tx_buf[p_usart_content->tx_wr++] = send_buf[i];
- p_usart_content->tx_wr %= p_usart_content->tx_buf_size;
- }
- // 使能发送缓存空中断
- USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, ENABLE);
- }
- void usart_wait_send_finished(usart_context_t *p_usart_content)
- {
- while (p_usart_content->tx_wr != p_usart_content->tx_rd)
- ;
- while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
- ;
- }
- int usart_receive_read(usart_context_t *p_usart_context, void *_receive_buf, const int receive_count)
- {
- uint8_t *receive_buf = (uint8_t *)_receive_buf;
- int i, receive_count_real;
- /* Read data from receive buffer.
- *The buffer have data that received from usart.
- */
- for (i = 0, receive_count_real = 0; i < receive_count; i++)
- {
- if (p_usart_context->rx_rd == p_usart_context->rx_wr)
- {
- return receive_count_real;
- }
- else
- {
- receive_buf[i] = p_usart_context->rx_buf[p_usart_context->rx_rd++];
- p_usart_context->rx_rd %= p_usart_context->rx_buf_size;
- receive_count_real++;
- }
- }
- return receive_count_real;
- }
- static void usart_rxne_it(usart_context_t *p_usart_content)
- {
- p_usart_content->rx_buf[p_usart_content->rx_wr++] = USART_ReceiveData(p_usart_content->usart_periph);
- p_usart_content->rx_wr %= p_usart_content->rx_buf_size;
- // overflow handle
- if (p_usart_content->rx_wr == p_usart_content->rx_rd)
- {
- p_usart_content->rx_rd++;
- p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
- }
- }
- static void usart_txe_it(usart_context_t *p_usart_content)
- {
- if (p_usart_content->tx_rd != p_usart_content->tx_wr)
- {
- USART_SendData(p_usart_content->usart_periph, p_usart_content->tx_buf[p_usart_content->tx_rd++]);
- p_usart_content->tx_rd %= p_usart_content->tx_buf_size;
- }
- else
- {
- USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, DISABLE);
- }
- }
- void usart_it(usart_context_t *p_usart_content)
- {
- // 读取缓沖区非空中断
- if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_RXNE))
- {
- USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_RXNE);
- usart_rxne_it(p_usart_content);
- }
- // 发送缓沖区空中断
- if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
- {
- USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_TXE);
- usart_txe_it(p_usart_content);
- }
- // // 发送完成中断
- // if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
- // {
- // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_TC, DISABLE);
- // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_RXNE, ENABLE);
- // }
- // 发送错误
- if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_ORE))
- USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_ORE);
- }
- void USART1_IRQHandler(void)
- {
- usart_it(&usart1_context);
- }
|