|
@@ -1,6 +1,7 @@
|
|
|
#include "hal_uart.h"
|
|
|
#include "stm32f4xx.h"
|
|
|
// #include <stdint.h>
|
|
|
+#include <stdint.h>
|
|
|
#include <sys/unistd.h>
|
|
|
|
|
|
static uint8_t usart1_tx_buf[256];
|
|
@@ -9,6 +10,9 @@ static uint8_t usart1_rx_buf[256];
|
|
|
static uint8_t usart2_tx_buf[256];
|
|
|
static uint8_t usart2_rx_buf[256];
|
|
|
|
|
|
+static uint8_t usart3_tx_buf[256];
|
|
|
+static uint8_t usart3_rx_buf[256];
|
|
|
+
|
|
|
#define HAL_UART1_TX_PORT GPIOA
|
|
|
#define HAL_UART1_TX_PIN GPIO_Pin_9
|
|
|
#define HAL_UART1_TX_SOURCE GPIO_PinSource9
|
|
@@ -23,6 +27,13 @@ static uint8_t usart2_rx_buf[256];
|
|
|
#define HAL_UART2_RX_PIN GPIO_Pin_3
|
|
|
#define HAL_UART2_RX_SOURCE GPIO_PinSource3
|
|
|
|
|
|
+#define HAL_UART3_TX_PORT GPIOB
|
|
|
+#define HAL_UART3_TX_PIN GPIO_Pin_10
|
|
|
+#define HAL_UART3_TX_SOURCE GPIO_PinSource2
|
|
|
+#define HAL_UART3_RX_PORT GPIOB
|
|
|
+#define HAL_UART3_RX_PIN GPIO_Pin_11
|
|
|
+#define HAL_UART3_RX_SOURCE GPIO_PinSource3
|
|
|
+
|
|
|
int usart1_snd_tx_cnt = 0;
|
|
|
|
|
|
// 加入以下代码,支持printf函数,而不需要选择use MicroLIB
|
|
@@ -221,6 +232,37 @@ static void usart2_deconfig(void)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+static void usart3_config(void)
|
|
|
+{
|
|
|
+ GPIO_InitTypeDef GPIO_StructInit;
|
|
|
+ NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
|
|
+ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, 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_UART3_TX_PIN;
|
|
|
+ GPIO_PinAFConfig(HAL_UART3_TX_PORT, HAL_UART3_TX_SOURCE, GPIO_AF_USART3);
|
|
|
+ GPIO_Init(HAL_UART3_TX_PORT, &GPIO_StructInit);
|
|
|
+
|
|
|
+ GPIO_StructInit.GPIO_Pin = HAL_UART3_RX_PIN;
|
|
|
+ GPIO_PinAFConfig(HAL_UART3_RX_PORT, HAL_UART3_RX_SOURCE, GPIO_AF_USART3);
|
|
|
+ GPIO_Init(HAL_UART3_RX_PORT, &GPIO_StructInit);
|
|
|
+
|
|
|
+ // Usart1 NVIC 配置
|
|
|
+ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // 串口1中断通道
|
|
|
+ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
|
|
|
+ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
|
|
|
+ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
|
|
|
+ NVIC_Init(&NVIC_InitStructure);
|
|
|
+}
|
|
|
+
|
|
|
+static void usart3_deconfig(void)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
usart_context_t usart1_context = {
|
|
|
USART1,
|
|
|
usart1_tx_buf,
|
|
@@ -251,6 +293,21 @@ usart_context_t usart2_context = {
|
|
|
usart2_config,
|
|
|
usart2_deconfig};
|
|
|
|
|
|
+usart_context_t usart3_context = {
|
|
|
+ USART3,
|
|
|
+ usart3_tx_buf,
|
|
|
+ sizeof(usart3_tx_buf),
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ usart3_rx_buf,
|
|
|
+ sizeof(usart3_rx_buf),
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ usart3_config,
|
|
|
+ usart3_deconfig};
|
|
|
+
|
|
|
void usart_config_init(usart_context_t *p_usart_content, uint32_t band_rate)
|
|
|
{
|
|
|
p_usart_content->config();
|
|
@@ -290,6 +347,115 @@ void usart_send(usart_context_t *p_usart_content, const void *_send_buf, const u
|
|
|
;
|
|
|
}
|
|
|
|
|
|
+int32_t usart_read_until_char(usart_context_t *p_usart_content, char c)
|
|
|
+{
|
|
|
+ uint32_t i;
|
|
|
+
|
|
|
+ for (i = p_usart_content->rx_rd; i < p_usart_content->rx_wr; i++)
|
|
|
+ {
|
|
|
+ if (p_usart_content->rx_buf[i % p_usart_content->rx_buf_size] == c)
|
|
|
+ {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+// read until memory
|
|
|
+// return:第一次出现mem位置, -1 没找到
|
|
|
+int32_t usart_read_until_mem(usart_context_t *p_usart_content, char *mem, int len)
|
|
|
+{
|
|
|
+ int32_t i, j, pos;
|
|
|
+
|
|
|
+ for (i = p_usart_content->rx_rd; i < p_usart_content->rx_wr; i++)
|
|
|
+ {
|
|
|
+ // overflow
|
|
|
+ for (j = 0; j < len; j++)
|
|
|
+ {
|
|
|
+ pos = i + j;
|
|
|
+ if (p_usart_content->rx_buf[pos % p_usart_content->rx_buf_size] != mem[j])
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 找到内存快
|
|
|
+ if (j == len)
|
|
|
+ {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+// read ble format 04 FC code len data
|
|
|
+// return 满足format条件结束位置, -1 没找到
|
|
|
+int32_t usart_read_ble_format(usart_context_t *p_usart_content)
|
|
|
+{
|
|
|
+ uint16_t rx_wr = p_usart_content->rx_wr;
|
|
|
+ uint16_t rx_rd = p_usart_content->rx_rd;
|
|
|
+
|
|
|
+ if (rx_wr < rx_rd)
|
|
|
+ {
|
|
|
+ rx_wr += p_usart_content->rx_buf_size;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rx_wr - rx_rd < 4)
|
|
|
+ {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第一次开机跳过特殊桢处理 00 04,跳过00
|
|
|
+ if (p_usart_content->rx_buf[rx_rd % p_usart_content->rx_buf_size] == 0x00 || p_usart_content->rx_buf[(rx_rd + 1) % p_usart_content->rx_buf_size] == 0x04)
|
|
|
+ {
|
|
|
+ p_usart_content->rx_rd++;
|
|
|
+ p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
|
|
|
+ return -4;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (p_usart_content->rx_buf[rx_rd % p_usart_content->rx_buf_size] != 0x04 || p_usart_content->rx_buf[(rx_rd + 1) % p_usart_content->rx_buf_size] != 0xFC)
|
|
|
+ {
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 蓝牙长度不够
|
|
|
+ if (rx_wr - rx_rd < p_usart_content->rx_buf[(rx_rd + 3) % p_usart_content->rx_buf_size] + 4)
|
|
|
+ {
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ return p_usart_content->rx_buf[(rx_rd + 3) % p_usart_content->rx_buf_size] + 4;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t usart_copy_data(usart_context_t *p_usart_content, int rlen, char *data)
|
|
|
+{
|
|
|
+ int32_t i, p;
|
|
|
+
|
|
|
+ // 拷贝rlen + 1个数据
|
|
|
+ for (i = 0; i < rlen; i++)
|
|
|
+ {
|
|
|
+ p = (p_usart_content->rx_rd + i) % p_usart_content->rx_buf_size;
|
|
|
+ data[i] = p_usart_content->rx_buf[p];
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t usart_read_update(usart_context_t *p_usart_content, int rlen, char *data)
|
|
|
+{
|
|
|
+ int32_t i, p;
|
|
|
+
|
|
|
+ // 拷贝rlen + 1个数据
|
|
|
+ for (i = 0; i < rlen; i++)
|
|
|
+ {
|
|
|
+ p = (p_usart_content->rx_rd + i) % p_usart_content->rx_buf_size;
|
|
|
+ data[i] = p_usart_content->rx_buf[p];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指定pos下一个位置
|
|
|
+ // return p_usart_content->rx_rd = (p_usart_content->rx_rd + rlen) % p_usart_content->rx_buf_size;
|
|
|
+ p = p_usart_content->rx_rd = (p_usart_content->rx_rd + rlen) % p_usart_content->rx_buf_size;
|
|
|
+ return p;
|
|
|
+}
|
|
|
+
|
|
|
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;
|
|
@@ -417,4 +583,9 @@ void USART1_IRQHandler(void)
|
|
|
void USART2_IRQHandler(void)
|
|
|
{
|
|
|
usart_it(&usart2_context);
|
|
|
+}
|
|
|
+
|
|
|
+void USART3_IRQHandler(void)
|
|
|
+{
|
|
|
+ usart_it(&usart3_context);
|
|
|
}
|