guowenxue
2024-06-25 5fc803d51ca097f07b4efbe0290ccc540b0660df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
 
#include <string.h>
#include <stdio.h>
 
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include "usart.h"
#include "utilities.h"
#include "tim.h"
#include "board_common.h"
 
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
static uint8_t aRxBuffer[UART_RINGBUF_SIZE] = {0};
static uint8_t aTxBuffer[UART_RINGBUF_SIZE] = {0};
static UartBuffer_t UartRev;
static UartBuffer_t UartSnd;
 
static uint8_t RxBuffer = 0;
static char TxBuffer[UART_RINGBUF_SIZE];
/* buffer write index*/
static __IO uint16_t iw=0;
/* buffer read index*/
Uart_Rxbuffer_t Debug_Rxbuffer;
 
void Board_USART2RX_Enable(void);
 
 
/* USART1 init function */
void Board_USART1Init(void)
{
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
}
 
void Board_USART1DeInit(void)
{
    HAL_UART_DeInit(&huart1);
}
 
void Board_USART1RX_Enable(void)
{
    HAL_UART_Receive_IT(&huart1, (uint8_t *)&RxBuffer, 1);   //再开启接收中断
}
 
void Board_USART1RX_Disable(void)
{
    CLEAR_BIT(USART1->CR1, USART_CR1_RXNEIE); 
}
 
 
static void Board_USART2BufferInit(void)
{
    /*
     * 初始化缓存
     * 两个指针指向相同的地址空间
     */
    memset(aRxBuffer, 0, sizeof(aRxBuffer));
    memset(aTxBuffer, 0, sizeof(aTxBuffer));
    UartRev.pFront = aRxBuffer;
    UartRev.pRear = aRxBuffer;
    UartSnd.pFront = aTxBuffer;
    UartSnd.pRear = aTxBuffer;    //两个指针指向相同的地址空间
}
 
/* USART3 init function */
void Board_USART2Init(void)
{
    huart2.Instance = USART2;
    huart2.Init.BaudRate = 115200;
    huart2.Init.WordLength = UART_WORDLENGTH_8B;
    huart2.Init.StopBits = UART_STOPBITS_1;
    huart2.Init.Parity = UART_PARITY_NONE;
    huart2.Init.Mode = UART_MODE_TX_RX;
    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart2.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart2) != HAL_OK)
    {
        Error_Handler();
    }
 
    Board_USART2BufferInit();
    Board_USART2RX_Enable();
}
 
 
void Board_USART2DeInit(void)
{
    HAL_UART_DeInit(&huart2);
}
 
 
void Board_USART2RX_Enable(void)
{
    HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, 1);   //再开启接收中断
}
 
void Board_USART2RX_Disable(void)
{
    CLEAR_BIT(USART2->CR1, USART_CR1_RXNEIE); 
}
 
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    if(uartHandle->Instance==USART1)
    {
        /* USER CODE BEGIN USART1_MspInit 0 */
 
        /* USER CODE END USART1_MspInit 0 */
        /* USART1 clock enable */
        __HAL_RCC_USART1_CLK_ENABLE();
 
        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**USART1 GPIO Configuration    
        PA9     ------> USART1_TX
        PA10     ------> USART1_RX 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
        GPIO_InitStruct.Pin = GPIO_PIN_10;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
        /* USART1 interrupt Init */
        HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
        HAL_NVIC_EnableIRQ(USART1_IRQn);
        /* USER CODE BEGIN USART1_MspInit 1 */
 
        /* USER CODE END USART1_MspInit 1 */
    }
    else if (uartHandle->Instance==USART2)
    {
        __HAL_RCC_USART2_CLK_ENABLE();
      
        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**USART2 GPIO Configuration    
        PA2     ------> USART2_TX
        PA3     ------> USART2_RX 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_2;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
      
        GPIO_InitStruct.Pin = GPIO_PIN_3;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
      
        /* USART2 interrupt Init */
        HAL_NVIC_SetPriority(USART2_IRQn, 1, 0);
        HAL_NVIC_EnableIRQ(USART2_IRQn);
    }
}
 
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
    if(uartHandle->Instance==USART1)
    {
        /* USER CODE BEGIN USART1_MspDeInit 0 */
 
        /* USER CODE END USART1_MspDeInit 0 */
        /* Peripheral clock disable */
        __HAL_RCC_USART1_CLK_DISABLE();
 
        /**USART1 GPIO Configuration    
        PA9     ------> USART1_TX
        PA10     ------> USART1_RX 
        */
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
 
        /* USART1 interrupt Deinit */
        HAL_NVIC_DisableIRQ(USART1_IRQn);
        /* USER CODE BEGIN USART1_MspDeInit 1 */
 
        /* USER CODE END USART1_MspDeInit 1 */
    }
    else if (uartHandle->Instance==USART2)
    {
        __HAL_RCC_USART2_CLK_DISABLE();
        
        /**USART2 GPIO Configuration    
        PA2     ------> USART2_TX
        PA3     ------> USART2_RX 
        */
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
        
        GPIO_InitTypeDef GPIO_InitStruct = {0};
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        
        GPIO_InitStruct.Pin = GPIO_PIN_2 ;
        HW_GPIO_Init( GPIOA, GPIO_PIN_2, &GPIO_InitStruct );
        HW_GPIO_Write( GPIOA, GPIO_PIN_2, 1);
        
        
        //GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
        GPIO_InitStruct.Pin = GPIO_PIN_3 ;
        HW_GPIO_Init(GPIOA, GPIO_PIN_3, &GPIO_InitStruct );
        HW_GPIO_Write(GPIOA, GPIO_PIN_3, 1);
        
        /* USART2 interrupt Deinit */
        HAL_NVIC_DisableIRQ(USART2_IRQn);
    }
 
/* modifes only ir*/
void VcomPrint( void)
{
    static uint16_t ir=0;
 
    char* CurChar;
    while(((iw+UART_RINGBUF_SIZE-ir)%UART_RINGBUF_SIZE) > 0)
    {
        BACKUP_PRIMASK();
        DISABLE_IRQ();
        CurChar = &TxBuffer[ir];
 
        ir= (ir+1) %UART_RINGBUF_SIZE;
 
        RESTORE_PRIMASK();
        HAL_UART_Transmit(&huart1, (uint8_t *) CurChar, 1, 300);
    }
 
    HAL_NVIC_ClearPendingIRQ(USART1_IRQn);
}
 
void VcomSend( char *format, ... )
{
    va_list args;
    va_start(args, format);
    uint8_t len;
    uint8_t lenTop;
    char tempBuff[255];
 
    BACKUP_PRIMASK();
    DISABLE_IRQ();
 
    /*convert into string at buff[0] of length iw*/
    len = vsprintf(&tempBuff[0], format, args);
 
    if (iw+len<UART_RINGBUF_SIZE)
    {
        memcpy( &TxBuffer[iw], &tempBuff[0], len);
        iw+=len;
    }
    else
    {
        lenTop=UART_RINGBUF_SIZE-iw;
        memcpy( &TxBuffer[iw], &tempBuff[0], lenTop);
        len-=lenTop;
        memcpy( &TxBuffer[0], &tempBuff[lenTop], len);
        iw = len;
    }
    RESTORE_PRIMASK();
 
    HAL_NVIC_SetPendingIRQ(USART1_IRQn);
 
    va_end(args);
    VcomPrint();
}
 
int8_t Board_USART2Recv(uint8_t *pFmt, uint16_t u16TimeOut)
{
    while(u16TimeOut)
    {
        if(UartRev.pFront != UartRev.pRear)
        {
            /* 如果队首指针和队尾指针不同表明缓冲区中有数据还未收取*/
            *pFmt = *UartRev.pFront;            
            UartRev.pFront++;
 
            if (UartRev.pFront >= (aRxBuffer+UART_RINGBUF_SIZE))
                UartRev.pFront = aRxBuffer;
 
            return 0;
        }
        u16TimeOut--;
    }
    return -1;
}
 
 
/**********************************************************************
函数:Uart2SendChar()
函数作用:发送一个字节数据
参数:
    uint8_t *pFmt--------------------------------需要发送的数据
返回值:
上一版本:无
当前版本:1.0
作者:
最后修改时间:2015-04-08
说明:
**********************************************************************/
 
static void Uart2SendChar(uint8_t u8Fmt)
{
    uint8_t u8Ret = HAL_OK;
    if(UartSnd.pRear == UartSnd.pFront)
    {
        //队首指针和队尾指针相等表明当前没有数据需要发送,这里需要手动开启发送请求
        *UartSnd.pRear = u8Fmt;
        UartSnd.pRear++;
        if(UartSnd.pRear >= aTxBuffer + UART_RINGBUF_SIZE)
            UartSnd.pRear = aTxBuffer;
        do
        {
            u8Ret = HAL_UART_Transmit_IT(&huart2, UartSnd.pFront, 1);//请求发送下一个数据
        }while(u8Ret != HAL_OK);
    }
    else
    {
        *UartSnd.pRear = u8Fmt;
        UartSnd.pRear++;
        if(UartSnd.pRear >= aTxBuffer + UART_RINGBUF_SIZE)
            UartSnd.pRear = aTxBuffer;
    }
}
 
int8_t Board_USART2Send(const uint8_t *pFmt, uint8_t u8Len)
{
    while(u8Len)
    {
        Uart2SendChar(*pFmt);
        pFmt++;
        u8Len--;
    }
    
    //HAL_Delay(10);
    return 0;
}
 
//#define DATA_OVERFLOW "Buffer locked or data overflow.\r\n"
#define DATA_OVERFLOW "122.\r\n"
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)
    {
        /*
         * 溢出判断
         */
         if (Debug_Rxbuffer.Locked || (Debug_Rxbuffer.RxSize >= sizeof(Debug_Rxbuffer.RxBuffer)))
         {          
            //HAL_UART_Transmit(&huart1, (uint8_t *)DATA_OVERFLOW, strlen(DATA_OVERFLOW), 0xFFFF); //将收到的信息发送出去
            //while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);//检测UART发送结束
            
            FreeUart1Buffer(); // 清除buffer
            HAL_UART_Receive_IT(&huart1, (uint8_t *)&RxBuffer, 1);   //再开启接收中断
        
            return ;
         }
        
        /*
         * 取数据
         */
        Debug_Rxbuffer.RxBuffer[Debug_Rxbuffer.RxSize++] = RxBuffer;
        Debug_Rxbuffer.Time = jiffies;
        
        /*
         * 判断结束
         */
        if ((Debug_Rxbuffer.RxBuffer[Debug_Rxbuffer.RxSize-1] == 0x0A) && (Debug_Rxbuffer.RxBuffer[Debug_Rxbuffer.RxSize-2] == 0x0D))
        {
           //HAL_UART_Transmit(&huart1, (uint8_t *)Uart1_Rxbuffer.RxBuffer, Uart1_Rxbuffer.RxSize, 0xFFFF); //将收到的信息发送出去
          // while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);//检测UART发送结束
           Debug_Rxbuffer.Locked = 1;
        }
        HAL_UART_Receive_IT(&huart1, (uint8_t *)&RxBuffer, 1);   //再开启接收中断
    }
    else if(huart->Instance == USART2)
    {
        uint8_t u8Ret = HAL_OK;
        
        //PRINTF("--%d-\r\n", __LINE__);
        UartRev.pRear++;
        if(UartRev.pRear >= (aRxBuffer + UART_RINGBUF_SIZE)) /* 判断队列是否满*/
            UartRev.pRear = aRxBuffer; /* 从头开始存储*/
        do
        {
            u8Ret= HAL_UART_Receive_IT(huart, UartRev.pRear, 1);
        }while(u8Ret != HAL_OK);
    }
}
 
 
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *pHuart)
{
    uint8_t u8Ret = HAL_OK;
    UartSnd.pFront++;     /* 更新rear指针*/
    if(UartSnd.pFront >= (aTxBuffer + UART_RINGBUF_SIZE))
        UartSnd.pFront = aTxBuffer;
    if(UartSnd.pFront != UartSnd.pRear)
    {
        /* 如果队首指针和队尾指针不同表明缓冲区中有数据还未发送*/
        do
        {
            u8Ret = HAL_UART_Transmit_IT(pHuart, UartSnd.pFront, 1);/* 请求发送下一个数据*/
        }while(u8Ret != HAL_OK);
    }
}
 
#if 0
int8_t Board_USART2Test(void)
{
    uint8_t u8Rx = 0;
    uint32_t u32TestTimeOut = jiffies + 5000;
    uint32_t u32RecvTimeOut = jiffies + 200;
    const uint8_t u8SendBuffer[32] = "Recv OK\r\n";
    uint8_t u8RecvBuffer[32] = {0};
    uint8_t u8Index = 0;
 
    //RS485_RX_ENABLE();
    //Board_RS485RecvEnable();
 
    while(1)
    {
        while( !Timeout_Happened(u32RecvTimeOut))
        {
            if (!Board_USART2Recv(&u8Rx, 200))
            {
                PRINTF("--%d-\r\n", __LINE__);
                u8RecvBuffer[u8Index] = u8Rx;
                if (u8Index <= (sizeof(u8RecvBuffer)-1))
                    u8Index++;
                else
                    break;
            }
        }
        
        //if(strstr((char *)u8RecvBuffer, (const char *)u8SendBuffer))
        if(u8Index > 0)
        {
            PRINTF("--%d-\r\n", __LINE__);
            Board_USART2Send(u8SendBuffer, strlen((const char *)u8SendBuffer));
            //return 0;
        }
        u8Index = 0;
        memset(u8RecvBuffer, 0, sizeof(u8RecvBuffer));
        u32RecvTimeOut = jiffies + 200;
    }
    return -1;
}
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/