|
1. 方案介绍9 C+ u' E9 H0 g% O, c, X% ]+ v) [" S 系统功能就是一个可以通过手机蓝牙实时查看远端采集器实时温度的系统。系统由采集器和Android手机端组成。采集器由STM32F072-Nucleo、蓝牙转串口模块(信弛达BM-S02模块)、温度传感器(MCP9808)组成。Android手机端负责和蓝牙模块通信,获取温度数据,并显示。5 C+ D: I7 R' k: [% r; r 2. 主要功能7 U2 a* P; _) ]5 @7 Z 系统重点除了完成一个特定的功能外,另一个目的在于研究CubeMX开发平台和RTOS,完成一系列通用的软件模块。已经完成了一些比较有用的模块,可供网友参考、指正。开发环境基于STM32 CubeMX+FreeRTOS(实际上是CMSIS OS的接口),所有的API调用都是基于CubeMX生成的Hal层API。完成的软件模块包括:: u9 O4 \" O, B S/ C+ C N/ Y 基于uart2和FreeROTS-cli的命令终端,支持上下翻页和命令自动补全);; m6 f; t: y o7 c5 r4 [7 O8 s$ m 基于DMA的串口驱动,适合和需要大吞吐的wifi或蓝牙模块通信的场合;# }+ j0 r& n1 }9 M7 U( g1 D 以及一个简单的MCP9808的驱动(CubeMX hal的i2c接口先赞一个)。. j- X' b2 s. E9 W 3. 实施方案现场实施: n- x: g8 H' s
蓝牙部分原理(出自官方文档)
BRTS、BCTS、GND接地,UART_TX、UART_RX接Nucleo的UART1_TX(PA9)和UART1_RX(PA10),VCC接3.3V。MCP9808原理图
MCP9808地址可编程,上下拉都接了,实际贴片时会根据需要决定是否贴上电阻,这样可以在一个i2c总线上挂多个MCP9808。 4. 软件实现# {# o: S* `, y printf到串口 重载fputc函数 #define PRINT_BUF_SIZE 2, r! T I3 g* ?, K' M static int read_bc = 0; static int __serial_send(char *buf, uint16_t size)! C8 ?3 k9 `5 w {0 V" B# A; O7 W8 Z- U2 }; N4 d HAL_UART_Transmit(&huart2, (uint8_t *)buf, size, 100); return size; }: C0 Y* |8 v" F3 m2 b8 L% g8 Q2 i int fputc(int ch, FILE *f) {, o1 X1 |( e( i2 w, [' t /* Place your implementation of fputc here */; ]4 F) J# i; Z1 A" ~( p /* e.g. write a character to the USART */ char buf[PRINT_BUF_SIZE]; buf[0] = ch; if (ch == '\r') {& L' q+ B. `1 O6 ~. u7 |* p read_bc = 1; } else if (ch == '\n') { if (!read_bc) {# l" X+ i6 j: A7 B1 [/ o- [, D4 q/ S buf[1] = '\r'; __serial_send(buf, 2); return ch;6 e$ D$ V4 `6 W1 b* {! N }8 e9 z: g8 }& G1 k read_bc = 0;" j0 A! J; G$ E } else if (read_bc) {# ~0 q, d6 v1 g7 b! U. @ read_bc = 0; } / {% m& s6 _- @, v __serial_send(buf, 1); return ch;* [# s( M( u* t( I" c6 m } k$ L/ D& c3 P+ a0 ] 命令终端 ) \/ Q2 Z1 G S k* g N) P3 h0 [' E 基于uart2和FreeRTOS-Cli实现,由于Nucleo的虚拟串口直接和uart2是相通的,因此不需要再另接串口线。* F% r! `3 q( I6 o4 z& U 总体流程
代码逻辑并不复杂,要讲有点麻烦,会把所有代码发出来,参考shell.c代码。 为了命令补全,在FreeRTOS-Cli中加了一个函数,其余FreeRTOS-Cli代码未做修改。" O ^7 N: f; e) Z1 u: G 实际效果如图所示:
显示不对齐是由于FreeRTOS的vTaskList函数实现导致,不影响调试。6 I% i/ J+ d0 s UART DMA传输 主要用于大流量的数据通信,比如:wifi、蓝牙模块和MCU之间的通信。; l. M3 I- b& C3 M G, J3 ~4 Y, r' d 网上有很多关于串口DMA的讨论,主要集中在UART DMA收上面,因为DMA接受需要固定长度的字节数,而这个限制对大部分应用是不可能做到的。所以,有人讨论了用有通信格式的数据包的方式,这个其实也不能完全满足所有需要,比如:这里用的蓝牙模块有的时候会打印一些系统信息,有的时候是远端传过来的数据,不太好区分。 这里的DMA传输无需格式,无需固定长度,实现方式参考了这个帖子http://blog.csdn.net/jdh99/article/details/8444474,现在STM32F103上调通,后来移到Nucleo平台,主要困难在于CubeMX的API比较生疏,研究完具体实现才能明白怎么做。核心在于先启动DMA RX传输,然后在IT_IDLE中断中处理接受的数据包(不完整的DMA RX接收,但一次通信已结束)。% F* f5 l/ t- p- j9 ? 初始化9 \7 U" L' Z1 B. G7 `7 v typedef struct {) v6 A; o/ r U1 i' M! P1 I uint8_t *rxbuf;" p1 V. T) n6 S! W" C/ ?, q uint8_t *txbuf;9 y" u3 m A3 ]$ e uint16_t rxbuf_size; uint16_t txbuf_size;: _3 d T% [( O! p* ~ uint32_t flag;) @. B! V- B% L0 m' I , a! L$ X) K0 p4 A7 D' Z osSemaphoreId rx_semp_id, tx_semp_id; UART_HandleTypeDef *UART_hd;; f% {6 Y( d* G5 H M; y //DMA_HandleTypeDef *DMARx_hd, *DMATx_hd; serial_rx_callback rx;3 d p8 K. t- g# ^8 [ int rxlen; } uart_dma_info_t;0 P# @8 h L1 V" h& D' W. ~ ' V6 T6 i, L$ o3 x int uart_dma_init(UART_HandleTypeDef *UART_hd, ; T8 D1 X# y' v2 Q5 E7 W serial_rx_callback rx, uint16_t rxbuf_size, uint16_t txbuf_size) {% v8 C" b4 U$ a) b7 \& l int id;7 Q B8 I! C$ D' `2 N: F. B uart_dma_info_t *info; osSemaphoreDef(uart_rx_sep); osSemaphoreDef(uart_tx_sep); osThreadDef(uart_rx_dma, uartRxTask, osPriorityHigh, 0, configMINIMAL_STACK_SIZE+100);& A& J6 a# S4 ^" [ assert_param(UART_hd != NULL);9 E9 ]* p% z( \* i& e( d' t 2 J% Z( }: v+ L( h1 Z# t! ` id = __get_uart_id(UART_hd->Instance);' _1 I/ ~0 g5 I$ `" R% z9 m assert_param(id >= 0 && id < MAX_UART_NUM);# o* k1 }" c- H8 _' b info = &uart_info[id]; info->rxbuf_size = rxbuf_size;$ U5 e& a+ H, A ^ l/ Z info->txbuf_size = txbuf_size;7 C3 t1 F$ l$ m3 b4 e, B9 s6 O' a info->UART_hd = UART_hd;3 S+ k. y( V* `+ ^. s3 P info->rx = rx;) f# C- f6 B# G. e* U if (rxbuf_size > 0) { assert_param(info->UART_hd->hdmarx); c E) A0 i0 ? info->rxbuf = pvPortMalloc(rxbuf_size); assert_param(info->rxbuf);1 k6 K; ]. f. `8 l7 {; F, y: D osThreadCreate (osThread(uart_rx_dma), info); info->rx_semp_id = osSemaphoreCreate(osSemaphore(uart_rx_sep), 1);5 m; a" P2 @+ ]) g assert_param(info->rx_semp_id); osSemaphoreWait(info->rx_semp_id, osWaitForever);//skip the first time $ H8 ?$ f$ v' f . g1 ]2 q [+ | __HAL_UART_DISABLE(info->UART_hd); info->UART_hd->Instance->CR3 |= UART_DMA_RX_ENABLE; __HAL_UART_ENABLE(info->UART_hd); HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, " P2 L5 Z Q: v+ W, d* K; b, b (uint32_t)info->rxbuf, info->rxbuf_size); } P/ B R$ J, A4 d& U- @2 p' f0 } if (txbuf_size > 0) { assert_param(info->UART_hd->hdmatx);3 Y7 o( R7 o6 [8 a4 X @7 r info->txbuf = pvPortMalloc(txbuf_size); assert_param(info->txbuf); info->tx_semp_id = osSemaphoreCreate(osSemaphore(uart_tx_sep), 1);6 @ \4 H, u, x5 W C assert_param(info->tx_semp_id); osSemaphoreWait(info->tx_semp_id, osWaitForever);// skip the first wait4 I9 Z" }$ _9 A3 S0 V0 O' X 1 j- U; c: l' c* P( F- j& ` info->UART_hd->hdmatx->XferCpltCallback = uart_dma_send_finsh_cb; info->UART_hd->hdmatx->XferErrorCallback = uart_dma_send_err_cb;7 O) q' M J, {' g& P. _; E Z. g8 e info->UART_hd->Instance->CR3 |= UART_DMA_TX_ENABLE;5 \$ K# B* x" _% [- Z% o } # b! Q2 ^' Z$ f7 n3 B6 _& N5 v return id;1 Z9 i: A6 b: @5 a- |$ H }! a i: W, R; a , ?- J. s4 \$ K. s6 U( S 中断处理 void USART1_IRQHandler(void)- E: v' f+ W' H) h {3 X+ o9 C5 \. U7 ~. v /* USER CODE BEGIN USART1_IRQn 0 */6 O( B9 Y7 V6 v; i * m) N0 n3 Y! I9 v /* USER CODE END USART1_IRQn 0 */. p( U- P! ^4 ~" c HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */9 R! M: c/ p6 Z5 Y( F' I2 U( t if (__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET) { uart_dma_recv_isr(uart1_fd);7 v# f8 _0 ?. b$ p L4 D0 ~: |/ r } /* USER CODE END USART1_IRQn 1 */ }- c4 m8 Y( j$ t+ h 8 _# S8 {# |: e! x" r9 z- m void uart_dma_recv_isr(int fd)+ J E* x9 P( z/ U& E, }( |! p2 f { uart_dma_info_t *info; uint32_t temp = 0;9 G( S* ]; f5 G8 ?0 M% S assert_param(fd >= 0 && fd < MAX_UART_NUM); * ^% X) |4 g. z# C info = &uart_info[fd]; assert_param(info->UART_hd->hdmarx);7 o. L; F0 e6 s. a* ?1 Z3 ?. L $ @6 }( C1 E( Q. F1 x __HAL_UART_CLEAR_IT(info->UART_hd, UART_CLEAR_IDLEF); SET_BIT(info->UART_hd->hdmarx->ErrorCode, HAL_DMA_ERROR_NONE); - U4 A( W0 h9 u* I# ~4 Q2 ` /* Change the DMA state */7 r. X8 d' Q, q7 G info->UART_hd->hdmarx->State = HAL_DMA_STATE_READY; % g# ~5 f% ~4 L, i2 v8 S' l /* Process Unlocked */ __HAL_UNLOCK(info->UART_hd->hdmarx); * a& m: E" N/ H8 B' @) ` __HAL_DMA_DISABLE(info->UART_hd->hdmarx); 6 S+ L |% K7 Q T& X! n. `. D$ J7 i temp = info->rxbuf_size - info->UART_hd->hdmarx->Instance->CNDTR;( Z. |& D3 p- F: c& r% [' C if (temp > 0) { info->rxlen = temp;" R7 Y& S4 B, c4 W2 w; h; N/ Z) g osSemaphoreRelease(info->rx_semp_id);) P9 B4 K* k" B6 N6 _ } HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, (uint32_t)info->rxbuf, info->rxbuf_size);* u; W/ J. ]; o$ E# @' ~& ] }+ U( _: f$ w: |) o( ]* ]- A 2 {- a. ] R- h, y/ e1 \8 L- m& ?: f 接收处理线程 static void uartRxTask(void const *args) {1 P+ G1 N: L l: l uart_dma_info_t *info = (uart_dma_info_t *)args;6 }( Q$ G3 i; f( h , A$ |& T4 J) t1 H while (1) {0 K7 U8 Y6 Z2 B0 ]" f4 l osSemaphoreWait(info->rx_semp_id, osWaitForever); if (info->rx && info->rxlen > 0) { info->rx(info->rxbuf, info->rxlen); }, L' H0 S, ?. p+ Z/ _. C } } 9 D3 Z/ c* C2 y7 f3 ]5 g+ L $ m- Y. n" x; @6 I; f 具体实现看附件把,贴代码也不太容易说清楚。/ D7 h9 m9 Q. p, M4 h! X% A( D MCP9808驱动 做了一些封装,但基本上只要调用HAL_I2C_Mem_Read就可以搞定了。 g) c9 @& D5 ~9 S ; T/ f7 ^4 ? E; F MCU全部的代码见附件' t- z& Q! g% _/ {; v6 K ~, Q2 z
f0fmw.zip
(6.88 MB, 下载次数: 2620)
|
微信公众号
手机版
嗯,其实这个东西主要是前端和后端难搞,前端指探头部分,如果就是用i2c芯片读读,确实没什么难度,但要是精度高,并且探头要非常小,这个难度一下子就大起来了,像这类的要求在实验室设备上要求比较多。如果是民用,重点还是app要搞好,单片机这里比较简单。* i; r, S& Z; O H
这次的东西其实硬件和功能都比较简单,重点还是研究基于STM32CubeMX的开发,现在毕竟文档和案例都比较少,弄个这个给大家参考参考。