
1. 方案介绍 系统功能就是一个可以通过手机蓝牙实时查看远端采集器实时温度的系统。系统由采集器和Android手机端组成。采集器由STM32F072-Nucleo、蓝牙转串口模块(信弛达BM-S02模块)、温度传感器(MCP9808)组成。Android手机端负责和蓝牙模块通信,获取温度数据,并显示。 m0 v/ D9 u5 R4 h( [& K7 R7 _ 2. 主要功能8 F* u/ F5 r' D% d6 ?* {- a, { 系统重点除了完成一个特定的功能外,另一个目的在于研究CubeMX开发平台和RTOS,完成一系列通用的软件模块。已经完成了一些比较有用的模块,可供网友参考、指正。开发环境基于STM32 CubeMX+FreeRTOS(实际上是CMSIS OS的接口),所有的API调用都是基于CubeMX生成的Hal层API。完成的软件模块包括: 基于uart2和FreeROTS-cli的命令终端,支持上下翻页和命令自动补全);$ ?) C- ?4 [( W* w- t 基于DMA的串口驱动,适合和需要大吞吐的wifi或蓝牙模块通信的场合;, ^# z9 a: {1 A/ i4 f' J/ G- H 以及一个简单的MCP9808的驱动(CubeMX hal的i2c接口先赞一个)。 3. 实施方案现场实施 ![]() 蓝牙部分原理(出自官方文档)+ A! `0 g- P# [3 r9 M$ k ![]() BRTS、BCTS、GND接地,UART_TX、UART_RX接Nucleo的UART1_TX(PA9)和UART1_RX(PA10),VCC接3.3V。MCP9808原理图 ![]() MCP9808地址可编程,上下拉都接了,实际贴片时会根据需要决定是否贴上电阻,这样可以在一个i2c总线上挂多个MCP9808。8 n: l+ g/ L* K r. } 4. 软件实现( l' L3 g1 [; @, F8 ^8 q, P printf到串口 重载fputc函数 #define PRINT_BUF_SIZE 2 static int read_bc = 0; static int __serial_send(char *buf, uint16_t size)6 ]% G* I" A5 ] L6 i { HAL_UART_Transmit(&huart2, (uint8_t *)buf, size, 100);9 K* C$ k$ @: Y' t3 E6 U return size;3 x# ^7 D$ G6 N0 M9 _/ i }7 ^( f. f0 g; N9 E n: z int fputc(int ch, FILE *f)% ^# c7 T$ u3 [% Y, l. p {; L" [4 C8 h; i# [- {/ J6 f /* Place your implementation of fputc here */- {( e* a5 z9 m4 R1 B /* e.g. write a character to the USART */ char buf[PRINT_BUF_SIZE]; buf[0] = ch; if (ch == '\r') { read_bc = 1;/ f+ r$ F0 N8 |- j } else if (ch == '\n') {! q4 v8 y; m: B1 J$ i! i if (!read_bc) {7 _) a$ @ a) G% ^ buf[1] = '\r'; __serial_send(buf, 2); return ch; } read_bc = 0;& o2 q* i5 i% f: M/ O } else if (read_bc) { read_bc = 0; }5 k9 T! d2 c+ Z: \2 `! X6 Z ! z: m" `, {1 | __serial_send(buf, 1); return ch;# X0 h/ L" {4 C% \' ]8 x! I* _ }. E9 t8 L: J1 ]. j 2 b: F! g/ b1 w( X: w$ N 命令终端 P; K# @( k8 j: B+ c4 |9 [4 q0 R/ E ' `) p- e. g' C% y# m, O& w 基于uart2和FreeRTOS-Cli实现,由于Nucleo的虚拟串口直接和uart2是相通的,因此不需要再另接串口线。 总体流程 ![]() 代码逻辑并不复杂,要讲有点麻烦,会把所有代码发出来,参考shell.c代码。 为了命令补全,在FreeRTOS-Cli中加了一个函数,其余FreeRTOS-Cli代码未做修改。$ Y, O( j# [" d$ P- F 实际效果如图所示: ![]() 显示不对齐是由于FreeRTOS的vTaskList函数实现导致,不影响调试。 UART DMA传输+ P& U6 I( X7 M1 O- A6 m: h2 g6 Y 主要用于大流量的数据通信,比如:wifi、蓝牙模块和MCU之间的通信。4 `. l& j3 a% H: U8 U" g* b 网上有很多关于串口DMA的讨论,主要集中在UART DMA收上面,因为DMA接受需要固定长度的字节数,而这个限制对大部分应用是不可能做到的。所以,有人讨论了用有通信格式的数据包的方式,这个其实也不能完全满足所有需要,比如:这里用的蓝牙模块有的时候会打印一些系统信息,有的时候是远端传过来的数据,不太好区分。 这里的DMA传输无需格式,无需固定长度,实现方式参考了这个帖子http://blog.csdn.net/jdh99/article/details/8444474,现在STM32F103上调通,后来移到Nucleo平台,主要困难在于CubeMX的API比较生疏,研究完具体实现才能明白怎么做。核心在于先启动DMA RX传输,然后在IT_IDLE中断中处理接受的数据包(不完整的DMA RX接收,但一次通信已结束)。 初始化3 {% W& N) t# \7 f typedef struct { uint8_t *rxbuf; uint8_t *txbuf;8 M2 ~- G! l5 E; p1 R6 i. `# E; l uint16_t rxbuf_size; uint16_t txbuf_size; uint32_t flag;/ I$ \7 m& D7 J9 |3 m. m( ? {/ P ; n) G S1 ]; B: b4 i2 q" t- m) U osSemaphoreId rx_semp_id, tx_semp_id; UART_HandleTypeDef *UART_hd; //DMA_HandleTypeDef *DMARx_hd, *DMATx_hd;6 p% N5 J8 C2 V+ i serial_rx_callback rx; int rxlen; } uart_dma_info_t;) N& E+ S7 F- Q( H: d4 {) F int uart_dma_init(UART_HandleTypeDef *UART_hd, serial_rx_callback rx, uint16_t rxbuf_size, uint16_t txbuf_size) { int id;* l) a& ^( m# Q0 s+ c uart_dma_info_t *info;; L1 o3 h7 G% x4 D/ i' ?6 d osSemaphoreDef(uart_rx_sep);' W; c6 |; c% B6 _' r, R+ g osSemaphoreDef(uart_tx_sep); osThreadDef(uart_rx_dma, uartRxTask, osPriorityHigh, 0, configMINIMAL_STACK_SIZE+100); - n) i9 l+ k5 } w) I& C5 | assert_param(UART_hd != NULL);) k( Y, {: x H5 q, [2 x id = __get_uart_id(UART_hd->Instance); assert_param(id >= 0 && id < MAX_UART_NUM);8 W: t; W, M2 E( E! s4 f % q& p$ t, Q" k: a( ?- ? info = &uart_info[id];) p# y( o* e0 y, E* v0 ? info->rxbuf_size = rxbuf_size; info->txbuf_size = txbuf_size; info->UART_hd = UART_hd; info->rx = rx; if (rxbuf_size > 0) { assert_param(info->UART_hd->hdmarx); info->rxbuf = pvPortMalloc(rxbuf_size);7 i7 x6 R( M$ T! ]" ` assert_param(info->rxbuf);; S! q( n. x, B# U! C osThreadCreate (osThread(uart_rx_dma), info); info->rx_semp_id = osSemaphoreCreate(osSemaphore(uart_rx_sep), 1);; R2 P/ A, ~5 t$ U/ P: }% @ Z& J& U assert_param(info->rx_semp_id);' D& `4 `* {# k- f v6 t! m( |7 v osSemaphoreWait(info->rx_semp_id, osWaitForever);//skip the first time % R' r% [0 d/ u __HAL_UART_DISABLE(info->UART_hd);2 z. _. E/ C$ M8 h! w+ Q info->UART_hd->Instance->CR3 |= UART_DMA_RX_ENABLE; __HAL_UART_ENABLE(info->UART_hd);$ m, m6 z8 Z( s HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, & r( }2 V" d5 m! c2 g (uint32_t)info->rxbuf, info->rxbuf_size); }9 u" N; e7 j. n% A/ { if (txbuf_size > 0) {. r( L; V" P7 \8 a, x7 O' w& X assert_param(info->UART_hd->hdmatx);, f$ c( k: s; U0 a4 y& J info->txbuf = pvPortMalloc(txbuf_size);6 l. }( g' Z& Z9 ^( o& C assert_param(info->txbuf); info->tx_semp_id = osSemaphoreCreate(osSemaphore(uart_tx_sep), 1); assert_param(info->tx_semp_id); osSemaphoreWait(info->tx_semp_id, osWaitForever);// skip the first wait 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; }6 o: l$ m, V5 [% f; V9 C' h5 f & G' H& u6 Q0 b# I( G* K return id; }9 O9 w' k$ ^. S0 _- F 9 G# M0 Q% @! R* ~6 C 中断处理2 Y, Q! `5 U/ T# F void USART1_IRQHandler(void)1 ?6 x# @; q4 {, U3 I- O) ~ { /* USER CODE BEGIN USART1_IRQn 0 */ /* USER CODE END USART1_IRQn 0 */ HAL_UART_IRQHandler(&huart1);( Q2 I+ ]# c7 _$ w/ m( u2 ^4 t' n9 m /* USER CODE BEGIN USART1_IRQn 1 */; p7 h1 o1 z2 U if (__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET) { uart_dma_recv_isr(uart1_fd);* B) l" q* e$ _) g, J) B) y0 i }3 H- _, u" m t* c* T $ {5 l, B- i6 q( D' u3 e /* USER CODE END USART1_IRQn 1 */0 V4 `! b5 k: z7 L } ' v* F! L# n$ N' \) B( Y& q8 x void uart_dma_recv_isr(int fd) {, _0 T7 S1 T: G X* ~' _ uart_dma_info_t *info;5 {+ D: s, z( g. I' }& J uint32_t temp = 0; assert_param(fd >= 0 && fd < MAX_UART_NUM);1 M+ T; W' C1 j. d info = &uart_info[fd]; C9 G/ l. R# I% [. z; c assert_param(info->UART_hd->hdmarx);) W+ r4 C3 l9 S" g1 P- ? 7 A' B1 s# x& b' ]) x7 I* |1 a __HAL_UART_CLEAR_IT(info->UART_hd, UART_CLEAR_IDLEF);7 k4 r$ r+ B; t' l M* T9 ]( V SET_BIT(info->UART_hd->hdmarx->ErrorCode, HAL_DMA_ERROR_NONE); 1 f4 K" e- q, [# d' s2 ~$ ` /* Change the DMA state */ info->UART_hd->hdmarx->State = HAL_DMA_STATE_READY; & V( |1 ]3 L5 R! T4 t8 S /* Process Unlocked */ __HAL_UNLOCK(info->UART_hd->hdmarx);+ ~7 e5 p" ^/ n ! [* S, @+ t2 J# F* @# O __HAL_DMA_DISABLE(info->UART_hd->hdmarx);1 |4 \! T4 R% Y- I. B% J% w + P0 _8 f% w) z1 h1 n- p s temp = info->rxbuf_size - info->UART_hd->hdmarx->Instance->CNDTR;4 U" c2 U& L7 w1 G if (temp > 0) { info->rxlen = temp;6 ~2 @& D+ J- Z+ d h3 d; X5 Q osSemaphoreRelease(info->rx_semp_id); } 8 r1 Z6 ~6 W; l7 j* x" J+ T: m HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, * O; s' k) v: N: m4 y' R( o4 v (uint32_t)info->rxbuf, info->rxbuf_size); } 1 a% ~" K7 Z/ z9 }; n" q 接收处理线程8 d. y. f* s( a static void uartRxTask(void const *args) {# R# ~$ [$ O: Q. q' h' v7 Y uart_dma_info_t *info = (uart_dma_info_t *)args;- J1 d" Y' J d9 b$ a" P& w ' | U, [: \: g6 p. m8 Z! m8 i while (1) { osSemaphoreWait(info->rx_semp_id, osWaitForever); if (info->rx && info->rxlen > 0) { info->rx(info->rxbuf, info->rxlen); } }# ~: z0 L, o" x' E }% c" Y% x) t( t1 `& W: l & k( A7 o: T) X- k* f4 T $ i% Q, D# Z8 H5 c5 n6 r8 U$ e 具体实现看附件把,贴代码也不太容易说清楚。 MCP9808驱动; C% L C% \* l4 ?/ F 做了一些封装,但基本上只要调用HAL_I2C_Mem_Read就可以搞定了。 MCU全部的代码见附件 ![]() |
嗯,其实这个东西主要是前端和后端难搞,前端指探头部分,如果就是用i2c芯片读读,确实没什么难度,但要是精度高,并且探头要非常小,这个难度一下子就大起来了,像这类的要求在实验室设备上要求比较多。如果是民用,重点还是app要搞好,单片机这里比较简单。
这次的东西其实硬件和功能都比较简单,重点还是研究基于STM32CubeMX的开发,现在毕竟文档和案例都比较少,弄个这个给大家参考参考。