1. 方案介绍 系统功能就是一个可以通过手机蓝牙实时查看远端采集器实时温度的系统。系统由采集器和Android手机端组成。采集器由STM32F072-Nucleo、蓝牙转串口模块(信弛达BM-S02模块)、温度传感器(MCP9808)组成。Android手机端负责和蓝牙模块通信,获取温度数据,并显示。) H: M% @1 A3 a( r9 @ 2. 主要功能7 {5 ~2 O: p3 e6 T9 y7 U% ^ 系统重点除了完成一个特定的功能外,另一个目的在于研究CubeMX开发平台和RTOS,完成一系列通用的软件模块。已经完成了一些比较有用的模块,可供网友参考、指正。开发环境基于STM32 CubeMX+FreeRTOS(实际上是CMSIS OS的接口),所有的API调用都是基于CubeMX生成的Hal层API。完成的软件模块包括: 基于uart2和FreeROTS-cli的命令终端,支持上下翻页和命令自动补全);, M# V& y) j7 ~ 基于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原理图# c4 X7 g+ B3 D MCP9808地址可编程,上下拉都接了,实际贴片时会根据需要决定是否贴上电阻,这样可以在一个i2c总线上挂多个MCP9808。, u7 C+ W$ J6 v; x0 |' T+ G 4. 软件实现+ U$ x' O# l' ^0 Y% z printf到串口* a9 ~' K; W& P4 J& C3 a# g# J 重载fputc函数( R- ]. l9 y9 W' y8 A9 `7 s6 p #define PRINT_BUF_SIZE 27 @) B3 a# j: ` static int read_bc = 0;( d9 E4 {# I4 K& R4 R3 s6 e static int __serial_send(char *buf, uint16_t size)# ^; L# @3 r2 I5 P" ] { HAL_UART_Transmit(&huart2, (uint8_t *)buf, size, 100);4 d% X5 }4 u/ n return size;) N; U) _# u7 o5 X7 T7 ~7 c }( ]/ F: |$ s) d/ ?& j- c" R int fputc(int ch, FILE *f)& q' l. r Y k# p0 T {7 Z% ?7 l! P6 Q3 S1 G /* Place your implementation of fputc here */ /* e.g. write a character to the USART */* ~+ d' M( a0 w1 v9 X char buf[PRINT_BUF_SIZE]; buf[0] = ch; if (ch == '\r') { read_bc = 1; } else if (ch == '\n') {# @/ l# T/ i, J A$ [ if (!read_bc) { buf[1] = '\r';3 c! M9 {* M/ ?! m8 q* K. m __serial_send(buf, 2); return ch;7 @6 |( g; v/ e6 ?; e( J } read_bc = 0; } else if (read_bc) { read_bc = 0; } 9 R( j7 a3 K" f6 k1 y+ U __serial_send(buf, 1); return ch; } 命令终端' |' ~0 [# ]/ n 基于uart2和FreeRTOS-Cli实现,由于Nucleo的虚拟串口直接和uart2是相通的,因此不需要再另接串口线。 总体流程# H V0 l& a. Z- E5 f 代码逻辑并不复杂,要讲有点麻烦,会把所有代码发出来,参考shell.c代码。: \: ^- a+ M! C5 W 为了命令补全,在FreeRTOS-Cli中加了一个函数,其余FreeRTOS-Cli代码未做修改。 实际效果如图所示: 显示不对齐是由于FreeRTOS的vTaskList函数实现导致,不影响调试。 UART DMA传输3 T( q Z: l+ m, X4 t* N 主要用于大流量的数据通信,比如:wifi、蓝牙模块和MCU之间的通信。9 U6 M }2 q* p- w9 T) l B 网上有很多关于串口DMA的讨论,主要集中在UART DMA收上面,因为DMA接受需要固定长度的字节数,而这个限制对大部分应用是不可能做到的。所以,有人讨论了用有通信格式的数据包的方式,这个其实也不能完全满足所有需要,比如:这里用的蓝牙模块有的时候会打印一些系统信息,有的时候是远端传过来的数据,不太好区分。 这里的DMA传输无需格式,无需固定长度,实现方式参考了这个帖子http://blog.csdn.net/jdh99/article/details/8444474,现在STM32F103上调通,后来移到Nucleo平台,主要困难在于CubeMX的API比较生疏,研究完具体实现才能明白怎么做。核心在于先启动DMA RX传输,然后在IT_IDLE中断中处理接受的数据包(不完整的DMA RX接收,但一次通信已结束)。 初始化 |6 o; ?" V( o, i typedef struct {7 a' e% p, f r0 N0 k7 I% |: W uint8_t *rxbuf; uint8_t *txbuf;( I0 H4 b) h9 W$ O uint16_t rxbuf_size;, }# Y F% ~3 v7 D# R uint16_t txbuf_size; uint32_t flag;) p8 @! Z, h+ G2 ]$ |+ z0 }) ?* n 2 s5 ^1 E. W; S3 w8 d9 x osSemaphoreId rx_semp_id, tx_semp_id; UART_HandleTypeDef *UART_hd;! @! ]. A- J* N5 q( Q //DMA_HandleTypeDef *DMARx_hd, *DMATx_hd;1 G! P: E" z' T* X serial_rx_callback rx; int rxlen;/ h+ I" G. h% X' y1 m } } uart_dma_info_t;1 x' C- V# ]* @& D int uart_dma_init(UART_HandleTypeDef *UART_hd, 9 v: x2 s2 m3 O8 V+ F9 ^ serial_rx_callback rx, uint16_t rxbuf_size, uint16_t txbuf_size) J* Q' h$ Q: }. `1 \( } {/ q9 j! z2 `4 R) e$ y1 E9 K) I int id;' q$ T! s. U* W9 ] uart_dma_info_t *info;1 Q8 d1 g* |+ R/ R7 E& B1 K$ l osSemaphoreDef(uart_rx_sep);8 ?3 M9 [9 A3 `) C- P Y osSemaphoreDef(uart_tx_sep); osThreadDef(uart_rx_dma, uartRxTask, osPriorityHigh, 0, configMINIMAL_STACK_SIZE+100);5 M# _3 @+ `& [8 s4 G7 J . @3 h: o9 c, |' F assert_param(UART_hd != NULL); 6 B$ A3 o0 U4 g, i5 X& P id = __get_uart_id(UART_hd->Instance); assert_param(id >= 0 && id < MAX_UART_NUM); info = &uart_info[id]; info->rxbuf_size = rxbuf_size; info->txbuf_size = txbuf_size;! s" y2 T, S; e' y# D info->UART_hd = UART_hd; info->rx = rx; if (rxbuf_size > 0) {5 k W7 v1 `3 ^, ?* u) i2 [6 e* P 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);: K! E) S2 {% [; o assert_param(info->rx_semp_id);, a3 y7 x) W! f osSemaphoreWait(info->rx_semp_id, osWaitForever);//skip the first time' {8 z5 _: i" {9 ^" J+ G; z % ]# {% B3 s( S1 @( U! }$ o5 K __HAL_UART_DISABLE(info->UART_hd);) W& A" r. }6 u) B4 a/ F+ D 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, ( s1 X) G/ P- ?, N3 T: W (uint32_t)info->rxbuf, info->rxbuf_size); 7 s0 Q8 j4 M: r# v! T/ j' I } if (txbuf_size > 0) { assert_param(info->UART_hd->hdmatx);+ r) \: j- G! j0 G) S info->txbuf = pvPortMalloc(txbuf_size);* ] p, o2 X8 f" h1 a4 k assert_param(info->txbuf); info->tx_semp_id = osSemaphoreCreate(osSemaphore(uart_tx_sep), 1); assert_param(info->tx_semp_id);: _% z) l0 y! C# M% l osSemaphoreWait(info->tx_semp_id, osWaitForever);// skip the first wait % o4 h" o( S( ?+ \$ z; ?1 u info->UART_hd->hdmatx->XferCpltCallback = uart_dma_send_finsh_cb;% H" ~7 n3 q+ {0 w' X+ P3 t0 m info->UART_hd->hdmatx->XferErrorCallback = uart_dma_send_err_cb;7 M& t4 K0 H* i5 W+ ` w$ p5 l info->UART_hd->Instance->CR3 |= UART_DMA_TX_ENABLE; }, A. F! c* u; s4 U return id;- q3 x' C" r/ E' R( I% B }- |1 i* ]2 d) O+ `, S) Z; t + M& X# _' v, ^+ Y+ v4 @ 中断处理0 ~. E! o! O5 P' P6 L/ L) g l void USART1_IRQHandler(void): ]% {: v X1 |; l5 Q {0 ~6 h5 v5 }" H, N. `6 V6 U s /* USER CODE BEGIN USART1_IRQn 0 */ /* USER CODE END USART1_IRQn 0 */) l% a% a* y% J0 ?6 H* ] HAL_UART_IRQHandler(&huart1);. d6 w2 Q! p' Y6 N& w7 q3 [* O$ ? /* USER CODE BEGIN USART1_IRQn 1 */& V; {: s, Z* {3 s* J1 Y5 ] if (__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET) { uart_dma_recv_isr(uart1_fd);2 Z- V$ f1 f0 y+ @; O; Q }9 C' U. h3 @) W4 P4 { /* USER CODE END USART1_IRQn 1 */) S( P+ q3 Z- o* M1 a, d$ h } 7 E* a: i5 g4 \& d% h& ^3 n1 P void uart_dma_recv_isr(int fd) {% L$ w/ |7 M+ u uart_dma_info_t *info; uint32_t temp = 0; assert_param(fd >= 0 && fd < MAX_UART_NUM); info = &uart_info[fd];. [( r0 w" ?# X. W2 O assert_param(info->UART_hd->hdmarx);2 X, y- G7 g9 k) M% U $ |. f% i% K1 a% X __HAL_UART_CLEAR_IT(info->UART_hd, UART_CLEAR_IDLEF); SET_BIT(info->UART_hd->hdmarx->ErrorCode, HAL_DMA_ERROR_NONE); 9 s3 r' R. }2 Q" [4 t /* Change the DMA state */ info->UART_hd->hdmarx->State = HAL_DMA_STATE_READY; 9 e7 O. \+ ?8 [/ h) B /* Process Unlocked */ __HAL_UNLOCK(info->UART_hd->hdmarx); - M6 x. I0 o$ t& ~; o, W# s7 A# s% e __HAL_DMA_DISABLE(info->UART_hd->hdmarx);. W* d! _) |' F. H0 ~- S* x temp = info->rxbuf_size - info->UART_hd->hdmarx->Instance->CNDTR;6 H7 p" e" @5 H9 J, U$ g2 Q if (temp > 0) {" \& s a' ~7 r info->rxlen = temp;7 Z! Y/ {6 ^4 g2 a$ h5 Y osSemaphoreRelease(info->rx_semp_id); h7 G9 X! Y/ H7 k6 P# u7 M+ H) v }( C& A" ?* G/ m2 H- s7 j, {) U HAL_DMA_Start(info->UART_hd->hdmarx, (uint32_t)&info->UART_hd->Instance->RDR, (uint32_t)info->rxbuf, info->rxbuf_size); } 接收处理线程 static void uartRxTask(void const *args)8 M0 _( U9 @# U3 R% y. t {0 f& f1 y0 C: f$ u8 b uart_dma_info_t *info = (uart_dma_info_t *)args;: g( r; I3 f# J while (1) { a! B( g* g# e; W$ Z osSemaphoreWait(info->rx_semp_id, osWaitForever);- [+ ?& ?" g, }# J5 R a9 M if (info->rx && info->rxlen > 0) {* a( A4 K2 Q: c$ `: D$ c- A+ F info->rx(info->rxbuf, info->rxlen);! F5 {: V6 h3 v* T% _! F } } } 4 S, ~- b% t3 ~, m, h6 ]9 a0 q( ? 具体实现看附件把,贴代码也不太容易说清楚。 $ j/ \, l3 S3 S MCP9808驱动 做了一些封装,但基本上只要调用HAL_I2C_Mem_Read就可以搞定了。3 z/ e7 W$ W7 B- Z5 E MCU全部的代码见附件 |
嗯,其实这个东西主要是前端和后端难搞,前端指探头部分,如果就是用i2c芯片读读,确实没什么难度,但要是精度高,并且探头要非常小,这个难度一下子就大起来了,像这类的要求在实验室设备上要求比较多。如果是民用,重点还是app要搞好,单片机这里比较简单。
这次的东西其实硬件和功能都比较简单,重点还是研究基于STM32CubeMX的开发,现在毕竟文档和案例都比较少,弄个这个给大家参考参考。