1. 方案介绍 系统功能就是一个可以通过手机蓝牙实时查看远端采集器实时温度的系统。系统由采集器和Android手机端组成。采集器由STM32F072-Nucleo、蓝牙转串口模块(信弛达BM-S02模块)、温度传感器(MCP9808)组成。Android手机端负责和蓝牙模块通信,获取温度数据,并显示。0 ~) S' Q& d0 V 2. 主要功能$ G. {$ z* D6 h6 J" ` 系统重点除了完成一个特定的功能外,另一个目的在于研究CubeMX开发平台和RTOS,完成一系列通用的软件模块。已经完成了一些比较有用的模块,可供网友参考、指正。开发环境基于STM32 CubeMX+FreeRTOS(实际上是CMSIS OS的接口),所有的API调用都是基于CubeMX生成的Hal层API。完成的软件模块包括: 基于uart2和FreeROTS-cli的命令终端,支持上下翻页和命令自动补全); 基于DMA的串口驱动,适合和需要大吞吐的wifi或蓝牙模块通信的场合; 以及一个简单的MCP9808的驱动(CubeMX hal的i2c接口先赞一个)。 3. 实施方案现场实施 蓝牙部分原理(出自官方文档) BRTS、BCTS、GND接地,UART_TX、UART_RX接Nucleo的UART1_TX(PA9)和UART1_RX(PA10),VCC接3.3V。MCP9808原理图5 f) z' k) T. N* @ MCP9808地址可编程,上下拉都接了,实际贴片时会根据需要决定是否贴上电阻,这样可以在一个i2c总线上挂多个MCP9808。0 S7 f2 R6 y/ ~4 E; ~ 4. 软件实现, H3 e V& X/ j2 A1 ?: s printf到串口 重载fputc函数 #define PRINT_BUF_SIZE 2 static int read_bc = 0;! i- j9 e. L+ \5 _. l static int __serial_send(char *buf, uint16_t size) {8 l% e! H# z8 T" | HAL_UART_Transmit(&huart2, (uint8_t *)buf, size, 100); return size;9 B+ W ~5 Q5 q% Z) r! R } int fputc(int ch, FILE *f)8 [6 c$ C I! {! d8 p) e { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ char buf[PRINT_BUF_SIZE];" ]. Y+ `/ L8 g& C% O buf[0] = ch; if (ch == '\r') {- c) _, ~) q% W# \# l read_bc = 1; } else if (ch == '\n') {$ F$ h8 P) ^9 p if (!read_bc) { buf[1] = '\r';# f# d( I' k% D3 m- j' y2 Q __serial_send(buf, 2);4 H$ W/ f. V O3 F) B6 e return ch;& q1 P+ F+ z: \7 t, c } read_bc = 0;3 J6 x1 |4 K3 R1 k } else if (read_bc) {6 U# [4 Q/ z- D3 Q6 ? read_bc = 0;& T5 _ m1 U8 B/ `. v } / B1 f9 S" @4 A0 j( {' F4 ~3 A __serial_send(buf, 1); return ch;% j0 b' R4 z! q }2 {5 ?2 z8 B7 |# | : |6 A3 H, ^: |, i" V9 d0 x 命令终端 1 v4 |% t/ l3 ^+ [% W0 H * f/ K" u3 c. j 基于uart2和FreeRTOS-Cli实现,由于Nucleo的虚拟串口直接和uart2是相通的,因此不需要再另接串口线。 总体流程 代码逻辑并不复杂,要讲有点麻烦,会把所有代码发出来,参考shell.c代码。 为了命令补全,在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接收,但一次通信已结束)。8 e K \: G4 i% y/ | 初始化' `3 {4 Q- g/ o- B: j typedef struct { uint8_t *rxbuf; uint8_t *txbuf; uint16_t rxbuf_size;; a( R& T" R' a) F. q$ g uint16_t txbuf_size;4 N5 F0 m0 e: v5 ~/ b7 h uint32_t flag;; I% {8 Z, h7 a. b! ?2 a: ^ osSemaphoreId rx_semp_id, tx_semp_id;! ?* M9 L; E% S, r$ l3 s UART_HandleTypeDef *UART_hd;' ~& f. {. Q3 Y4 y* z% F //DMA_HandleTypeDef *DMARx_hd, *DMATx_hd;: r' `) d6 U9 }( q. Z serial_rx_callback rx; int rxlen;$ r2 `: v/ S* z7 \0 g) R# B } uart_dma_info_t; int uart_dma_init(UART_HandleTypeDef *UART_hd, : R( R# K" @! q- F serial_rx_callback rx, uint16_t rxbuf_size, uint16_t txbuf_size) {( P+ M; v6 B2 ? int id;+ Q& \7 b/ M/ d' r% T. r uart_dma_info_t *info; T9 g; v$ L6 D, K4 p7 S5 B osSemaphoreDef(uart_rx_sep);; T9 r0 y" f O& B! R osSemaphoreDef(uart_tx_sep); osThreadDef(uart_rx_dma, uartRxTask, osPriorityHigh, 0, configMINIMAL_STACK_SIZE+100);* w; K0 V* N+ ^ & Q0 m9 P; A1 S1 s# Q: o! c7 Y assert_param(UART_hd != NULL); ! H; f2 b u6 M id = __get_uart_id(UART_hd->Instance); assert_param(id >= 0 && id < MAX_UART_NUM);9 K) x& r# H& K) S5 m- A$ | info = &uart_info[id];# b% C6 T2 [+ l+ `$ t9 u: G & `& U( {8 U( Z$ c3 Z" K- | info->rxbuf_size = rxbuf_size;8 A' O& O$ o$ r1 [- M" Q1 E info->txbuf_size = txbuf_size; info->UART_hd = UART_hd;3 B, V: J- Y( E$ Z0 s info->rx = rx; if (rxbuf_size > 0) { assert_param(info->UART_hd->hdmarx); info->rxbuf = pvPortMalloc(rxbuf_size);+ t- H) c L ]) c) u assert_param(info->rxbuf); osThreadCreate (osThread(uart_rx_dma), info); 8 l. t/ p& L+ R$ Z info->rx_semp_id = osSemaphoreCreate(osSemaphore(uart_rx_sep), 1); assert_param(info->rx_semp_id);& s( m* s( k% g osSemaphoreWait(info->rx_semp_id, osWaitForever);//skip the first time* x# I5 C" w8 O __HAL_UART_DISABLE(info->UART_hd); info->UART_hd->Instance->CR3 |= UART_DMA_RX_ENABLE; __HAL_UART_ENABLE(info->UART_hd);: U4 Y. Z, q# I. b ( E6 b2 A- T% `8 j# W, o HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, w# P+ z T7 B' U (uint32_t)info->rxbuf, info->rxbuf_size); 9 W. ^) q! \! P: z* B1 y }( ]/ T# n% H# e if (txbuf_size > 0) { assert_param(info->UART_hd->hdmatx); info->txbuf = pvPortMalloc(txbuf_size);; ~+ l0 @+ ^) R S% v assert_param(info->txbuf);. x( d4 t8 W' _) _: u& w2 A info->tx_semp_id = osSemaphoreCreate(osSemaphore(uart_tx_sep), 1); assert_param(info->tx_semp_id);4 ?, n4 {/ Z& K( c1 ` 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;8 x! F9 j2 X- p) W4 E) O7 h9 H info->UART_hd->Instance->CR3 |= UART_DMA_TX_ENABLE; } 6 q0 d& q Z8 Q) l return id;# _0 B1 `1 V6 e1 K7 l/ k } 中断处理8 g/ C% i& l4 k* y3 }7 w# d void USART1_IRQHandler(void) {* v- a- W* J9 h /* USER CODE BEGIN USART1_IRQn 0 */% b* r# G2 S: J. i : G( s& J0 \+ w7 A4 j; d/ \4 V, f /* USER CODE END USART1_IRQn 0 */- V H! b. A! V) }5 e0 Z HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */ g T+ d5 Y( f0 X2 G: U if (__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET) {5 C/ }1 |* }! T6 v4 F5 ] uart_dma_recv_isr(uart1_fd); } /* USER CODE END USART1_IRQn 1 *// I. D9 V- }1 q# ^ }7 M% g/ P3 |) e7 k V' ^) ? ) j- {. d" n Y5 A) u0 T/ _, m) U void uart_dma_recv_isr(int fd) { uart_dma_info_t *info; uint32_t temp = 0; 9 R1 y2 z) w& D7 |. e: o1 r assert_param(fd >= 0 && fd < MAX_UART_NUM);- q: R1 \- @5 h $ u. I$ @) J. t# t9 e/ X info = &uart_info[fd]; assert_param(info->UART_hd->hdmarx);- L. V7 y% G- K4 I% x8 |- f __HAL_UART_CLEAR_IT(info->UART_hd, UART_CLEAR_IDLEF);. L# H2 R7 `! d) } U( V SET_BIT(info->UART_hd->hdmarx->ErrorCode, HAL_DMA_ERROR_NONE);1 m2 x+ J) M& l5 A. @ /* Change the DMA state */8 [* D0 |/ H5 C1 Z info->UART_hd->hdmarx->State = HAL_DMA_STATE_READY; ; p; E* u) L) c& l6 i% V; J /* Process Unlocked */ __HAL_UNLOCK(info->UART_hd->hdmarx);+ [/ I" Q0 L; F/ J2 r6 `2 k __HAL_DMA_DISABLE(info->UART_hd->hdmarx); temp = info->rxbuf_size - info->UART_hd->hdmarx->Instance->CNDTR; if (temp > 0) {/ ?; v; C4 r7 E# s1 n; P info->rxlen = temp; osSemaphoreRelease(info->rx_semp_id); } 4 s n: G6 q1 Z" s8 \. Y HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, (uint32_t)info->rxbuf, info->rxbuf_size); } & m- k% e7 y4 |. B, ~! B/ e/ R4 n6 Z 接收处理线程$ T+ O; D) u! e4 ]# x/ q9 ` static void uartRxTask(void const *args) { uart_dma_info_t *info = (uart_dma_info_t *)args;* K* t! r* ?) n; F5 X" i3 ?5 c while (1) {0 z! V4 m0 T0 b" ^+ b osSemaphoreWait(info->rx_semp_id, osWaitForever); if (info->rx && info->rxlen > 0) {) }! D1 W; l: A( x8 @ q7 U info->rx(info->rxbuf, info->rxlen); }. T) a) e: ?5 P. p' b) E& W } }4 x6 t- d- \# m$ q3 G' L 9 l( p1 U3 b8 h 具体实现看附件把,贴代码也不太容易说清楚。* b: T: `, C; x4 i" l9 l2 L ( Q+ W' d8 r+ T4 [, T% X MCP9808驱动 做了一些封装,但基本上只要调用HAL_I2C_Mem_Read就可以搞定了。( n* |8 G! v0 H7 e5 k MCU全部的代码见附件 |
嗯,其实这个东西主要是前端和后端难搞,前端指探头部分,如果就是用i2c芯片读读,确实没什么难度,但要是精度高,并且探头要非常小,这个难度一下子就大起来了,像这类的要求在实验室设备上要求比较多。如果是民用,重点还是app要搞好,单片机这里比较简单。
这次的东西其实硬件和功能都比较简单,重点还是研究基于STM32CubeMX的开发,现在毕竟文档和案例都比较少,弄个这个给大家参考参考。