1. 方案介绍' A0 B4 B& k4 ?9 i* r2 r 系统功能就是一个可以通过手机蓝牙实时查看远端采集器实时温度的系统。系统由采集器和Android手机端组成。采集器由STM32F072-Nucleo、蓝牙转串口模块(信弛达BM-S02模块)、温度传感器(MCP9808)组成。Android手机端负责和蓝牙模块通信,获取温度数据,并显示。) \4 \7 L! B* N1 V! R) R 2. 主要功能 系统重点除了完成一个特定的功能外,另一个目的在于研究CubeMX开发平台和RTOS,完成一系列通用的软件模块。已经完成了一些比较有用的模块,可供网友参考、指正。开发环境基于STM32 CubeMX+FreeRTOS(实际上是CMSIS OS的接口),所有的API调用都是基于CubeMX生成的Hal层API。完成的软件模块包括:# r9 i% l9 x6 ?5 i3 O4 d' B) G 基于uart2和FreeROTS-cli的命令终端,支持上下翻页和命令自动补全); 基于DMA的串口驱动,适合和需要大吞吐的wifi或蓝牙模块通信的场合;, R) [ |+ n+ E9 x1 u% N 以及一个简单的MCP9808的驱动(CubeMX hal的i2c接口先赞一个)。 3. 实施方案现场实施 蓝牙部分原理(出自官方文档)% m$ o8 n: _+ W/ S7 l7 J BRTS、BCTS、GND接地,UART_TX、UART_RX接Nucleo的UART1_TX(PA9)和UART1_RX(PA10),VCC接3.3V。MCP9808原理图! i% |: ^; G$ \$ @; {/ \5 N MCP9808地址可编程,上下拉都接了,实际贴片时会根据需要决定是否贴上电阻,这样可以在一个i2c总线上挂多个MCP9808。 4. 软件实现 printf到串口 重载fputc函数* s2 V8 m: c3 T; f$ t #define PRINT_BUF_SIZE 2/ P5 O9 |; Q- H' T static int read_bc = 0;1 o' ^" ]2 n i% w0 h* _ static int __serial_send(char *buf, uint16_t size) { HAL_UART_Transmit(&huart2, (uint8_t *)buf, size, 100);$ a7 o7 ?. [2 o. ` return size; } int fputc(int ch, FILE *f), n6 C$ R' S4 z3 n, i w" x! F {1 W4 X$ d8 J! Z /* Place your implementation of fputc here */' ]# c: b# X) N. I /* e.g. write a character to the USART */, G, S: B; e/ p; v3 x char buf[PRINT_BUF_SIZE];% F5 h- {' o" P; a& v7 d buf[0] = ch;+ F$ A7 G* C! A1 G/ i) [ 7 A) ^! g; V8 v2 @- r4 Z6 y if (ch == '\r') {" H3 @- G4 K, _! N6 c read_bc = 1; } else if (ch == '\n') { if (!read_bc) {$ n( \0 `" u' C% B" n buf[1] = '\r';! J/ t4 k& Z+ ]8 J __serial_send(buf, 2);$ F6 L- h: I& i7 @% {* [* C+ n% u return ch;* L4 @; d2 J: ^6 a& A% I }+ ^" {2 a' K9 ]- F N read_bc = 0;+ ?7 R6 r7 K, A- C6 W: U& }( o } else if (read_bc) { read_bc = 0; } __serial_send(buf, 1);# r4 u& P+ f' w return ch;! L) J5 A: _+ b2 C0 f } 命令终端- v9 g z' V3 y+ F: W! a 3 B7 m* t# b* V+ f & Y7 r! w0 x* F, v 基于uart2和FreeRTOS-Cli实现,由于Nucleo的虚拟串口直接和uart2是相通的,因此不需要再另接串口线。 总体流程6 a$ L8 I/ M) [. \" a/ m9 `1 S) O 代码逻辑并不复杂,要讲有点麻烦,会把所有代码发出来,参考shell.c代码。; v* P1 L5 \3 }# g 为了命令补全,在FreeRTOS-Cli中加了一个函数,其余FreeRTOS-Cli代码未做修改。 实际效果如图所示: 显示不对齐是由于FreeRTOS的vTaskList函数实现导致,不影响调试。 UART DMA传输 主要用于大流量的数据通信,比如:wifi、蓝牙模块和MCU之间的通信。 网上有很多关于串口DMA的讨论,主要集中在UART DMA收上面,因为DMA接受需要固定长度的字节数,而这个限制对大部分应用是不可能做到的。所以,有人讨论了用有通信格式的数据包的方式,这个其实也不能完全满足所有需要,比如:这里用的蓝牙模块有的时候会打印一些系统信息,有的时候是远端传过来的数据,不太好区分。 这里的DMA传输无需格式,无需固定长度,实现方式参考了这个帖子http://blog.csdn.net/jdh99/article/details/8444474,现在STM32F103上调通,后来移到Nucleo平台,主要困难在于CubeMX的API比较生疏,研究完具体实现才能明白怎么做。核心在于先启动DMA RX传输,然后在IT_IDLE中断中处理接受的数据包(不完整的DMA RX接收,但一次通信已结束)。 初始化 typedef struct { uint8_t *rxbuf; uint8_t *txbuf; uint16_t rxbuf_size; uint16_t txbuf_size; uint32_t flag; 1 Q6 a& W' R1 b3 Q8 d osSemaphoreId rx_semp_id, tx_semp_id; UART_HandleTypeDef *UART_hd; //DMA_HandleTypeDef *DMARx_hd, *DMATx_hd;( L% g. `6 `, C- V# k2 B6 v! y serial_rx_callback rx; int rxlen; } uart_dma_info_t; 8 E1 @; E& X& t- z; l/ ? int uart_dma_init(UART_HandleTypeDef *UART_hd, serial_rx_callback rx, uint16_t rxbuf_size, uint16_t txbuf_size)( U- v! ~9 W' {5 ^# l6 G {+ N6 d" \# g% y* ~$ F int id; uart_dma_info_t *info; osSemaphoreDef(uart_rx_sep); osSemaphoreDef(uart_tx_sep); osThreadDef(uart_rx_dma, uartRxTask, osPriorityHigh, 0, configMINIMAL_STACK_SIZE+100);- v. C0 `, l) K+ A& P! ] / h7 x$ \# o! D. R$ {8 W assert_param(UART_hd != NULL); $ T( d+ _, O: D9 B- J id = __get_uart_id(UART_hd->Instance);% ?* [# o# `3 L2 ~6 H) N; q assert_param(id >= 0 && id < MAX_UART_NUM); info = &uart_info[id];- H" W( I; C1 m, v$ K$ s; Z 3 r9 M* H+ _4 I6 P. p( _3 [& J6 M0 L info->rxbuf_size = rxbuf_size;. D! A- n& b. b2 W a info->txbuf_size = txbuf_size;$ U" X8 H3 w( m" V info->UART_hd = UART_hd;3 Y, ]/ i- f# ?; b info->rx = rx;3 b4 L8 r F; ?, \4 y 8 ?7 @: m) x0 W* T' { if (rxbuf_size > 0) {2 b( E) A% \1 D5 u5 q assert_param(info->UART_hd->hdmarx); info->rxbuf = pvPortMalloc(rxbuf_size); assert_param(info->rxbuf); osThreadCreate (osThread(uart_rx_dma), info); info->rx_semp_id = osSemaphoreCreate(osSemaphore(uart_rx_sep), 1);! U* o" k) W) m( y assert_param(info->rx_semp_id);; J: y9 W/ N9 h# Z osSemaphoreWait(info->rx_semp_id, osWaitForever);//skip the first time ; n& u# m+ R' A$ I( p: ~ 4 n8 n2 W, t- V$ }: u |5 b* I5 x __HAL_UART_DISABLE(info->UART_hd); info->UART_hd->Instance->CR3 |= UART_DMA_RX_ENABLE; __HAL_UART_ENABLE(info->UART_hd);# N5 b7 F/ F% `* K HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, (uint32_t)info->rxbuf, info->rxbuf_size); + G; k/ r+ p. C7 R5 _, u } if (txbuf_size > 0) { assert_param(info->UART_hd->hdmatx);6 {) T% Y9 P) X9 n, s info->txbuf = pvPortMalloc(txbuf_size); assert_param(info->txbuf); info->tx_semp_id = osSemaphoreCreate(osSemaphore(uart_tx_sep), 1);1 X, p( Z3 t0 ? K assert_param(info->tx_semp_id);& k4 z5 A1 Y# }/ q osSemaphoreWait(info->tx_semp_id, osWaitForever);// skip the first wait% _; b; t& r. C0 x% E: N info->UART_hd->hdmatx->XferCpltCallback = uart_dma_send_finsh_cb; info->UART_hd->hdmatx->XferErrorCallback = uart_dma_send_err_cb; info->UART_hd->Instance->CR3 |= UART_DMA_TX_ENABLE;& `5 h+ F; {$ Y. r4 A$ @% ? }' ], n9 u: r' E4 Q' W return id; } 中断处理3 m* W7 N7 `" g5 y" \( G0 V void USART1_IRQHandler(void); l4 i" ?$ W1 n6 @: U) t& t4 P% e { /* USER CODE BEGIN USART1_IRQn 0 */4 B. J: E. a* C7 A3 ~. Z /* USER CODE END USART1_IRQn 0 */ HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */ if (__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET) {2 y& v: g! N m5 n uart_dma_recv_isr(uart1_fd); } /* USER CODE END USART1_IRQn 1 */. A& z2 `4 |* d, N1 d } void uart_dma_recv_isr(int fd) {4 h8 d% d h8 U1 O, g uart_dma_info_t *info; uint32_t temp = 0; assert_param(fd >= 0 && fd < MAX_UART_NUM); info = &uart_info[fd];3 b' H5 M* B0 r. y- ]# i assert_param(info->UART_hd->hdmarx);, M4 s1 |! J. w, W" H 2 ?: O. j8 e( }" G3 ~2 q! T3 ] __HAL_UART_CLEAR_IT(info->UART_hd, UART_CLEAR_IDLEF);% R8 `7 }/ J* ]- ?! x4 d; j8 Y SET_BIT(info->UART_hd->hdmarx->ErrorCode, HAL_DMA_ERROR_NONE); 2 v' x+ V* t6 Y; n /* Change the DMA state */ info->UART_hd->hdmarx->State = HAL_DMA_STATE_READY; /* Process Unlocked */ __HAL_UNLOCK(info->UART_hd->hdmarx); $ f3 i1 d. N0 i __HAL_DMA_DISABLE(info->UART_hd->hdmarx); temp = info->rxbuf_size - info->UART_hd->hdmarx->Instance->CNDTR;; u3 `+ q! B5 |/ ?! f if (temp > 0) { info->rxlen = temp; c6 [ Q6 k5 ^9 i# C7 m4 A8 M) m osSemaphoreRelease(info->rx_semp_id); }" `& x }0 C0 y* o3 V! ~" D ' y7 m; p6 ?5 B; G% o( E HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, ( I' t7 Q2 r4 c: z (uint32_t)info->rxbuf, info->rxbuf_size); } 接收处理线程" L! z' C1 n* m/ ]4 O. a& k) | static void uartRxTask(void const *args) {9 }/ s/ X8 k( Y/ e) ?1 h! W3 ] uart_dma_info_t *info = (uart_dma_info_t *)args; while (1) { osSemaphoreWait(info->rx_semp_id, osWaitForever);' x$ d( v i- f" |$ \. I if (info->rx && info->rxlen > 0) {& C( f' w! C5 l) Z# A$ t* ~) R info->rx(info->rxbuf, info->rxlen);' |8 u$ i3 d. b' G% h* K }" w1 M# _) q) D2 _ } } 具体实现看附件把,贴代码也不太容易说清楚。 MCP9808驱动) m$ H, {1 H5 ~& r) d( `/ B 做了一些封装,但基本上只要调用HAL_I2C_Mem_Read就可以搞定了。# p% r$ g% r5 V0 Y6 S. l' | ' u& h3 j7 |6 H: F* c( H9 }2 M& N MCU全部的代码见附件 |
嗯,其实这个东西主要是前端和后端难搞,前端指探头部分,如果就是用i2c芯片读读,确实没什么难度,但要是精度高,并且探头要非常小,这个难度一下子就大起来了,像这类的要求在实验室设备上要求比较多。如果是民用,重点还是app要搞好,单片机这里比较简单。
这次的东西其实硬件和功能都比较简单,重点还是研究基于STM32CubeMX的开发,现在毕竟文档和案例都比较少,弄个这个给大家参考参考。