Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张亚玄
/
STM32Test
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
bbbcab84
authored
2019-11-01 23:06:38 +0800
by
张亚玄
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add new usart
1 parent
037006bb
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
254 additions
and
80 deletions
Core/Inc/ringbuffer.h
Core/Inc/usart.h
Core/Src/freertos.c
Core/Src/main.c
Core/Src/ringbuffer.c
Core/Src/stm32f0xx_it.c
Core/Src/usart.c
Core/Inc/ringbuffer.h
0 → 100644
View file @
bbbcab8
#ifndef _RINGBUF_H_
#define _RINGBUF_H_
#include "cmsis_os.h"
#if !defined(__WINDOWS__) && !defined (FREERTOS) && !defined (LINUX)
#define FREERTOS
#endif
/**
* Put data in a specific length.
* @param *buffer the position of buffer specified by user.
* @param len the length of data which user wants to put.
* @return WRITE_SUCCEED if write succeed,FAIL_LACK_OF_SPACE if no enough space to write in.
*/
int8_t
RINGBUF_SEND
(
const
char
*
buffer
,
uint16_t
len
);
/**
* Take data out in a specific length.
* @param *buffer the position of buffer specified by user.
* @param len the length of data which user wants to take.
* @return RINGBUF_READ if read succeed,FAIL_READ_TOO_FAST if readpoint would pass writepoint.
*/
int8_t
RINGBUF_READ
(
uint8_t
*
buffer
,
uint16_t
len
);
/**
* Get data from a specific length.
* @param *buffer the position of buffer specified by user.
* @param len the length of data which user wants to get,not take out!!
* @return RINGBUF_READ if read succeed,FAIL_READ_TOO_FAST if readpoint would pass writepoint.
*/
int8_t
RINGBUF_PEEK
(
uint8_t
*
buffer
,
uint16_t
len
);
/**
* use buffer specified by user as ringbuffer.
* @param *buffer the position of buffer specified by user.
* @param len the length of buffer which user wants.
* @return NULL.
*/
void
RINGBUF_INIT
(
uint8_t
*
ringbuffer
,
uint16_t
len
);
/**
* check if read too fast.
* @param NULL
* @return 1 if read gradually,0 if readpoint catch writepoint.
*/
uint8_t
RINGBUF_CHECKIO
(
void
);
/**
* request for space avaliable to write.
* @param NULL
* @return len the space avaliable to use of this ringbuffer.
*/
uint16_t
RINGBUF_AVALIABLE_SPACE
(
void
);
#endif
Core/Inc/usart.h
View file @
bbbcab8
...
...
@@ -28,21 +28,19 @@
/* USER CODE BEGIN Includes */
#include <mprintf.h>
#include "ringbuffer.h"
/* USER CODE END Includes */
extern
UART_HandleTypeDef
huart1
;
extern
UART_HandleTypeDef
huart2
;
/* USER CODE BEGIN Private defines */
#define USART
_REC
_LEN 200
#define USART
1_BUF
_LEN 200
extern
uint8_t
USART_RX_BUF
[
USART_REC_LEN
];
// 接收缓冲,最大USART_REC_LEN个字节,末字节为换行
extern
uint16_t
USART_RX_STA
;
extern
uint8_t
USART1_RingBuff
[
USART1_BUF_LEN
];
// 接收缓冲
#define RXBUFFERSIZE 1 // 缓存大小
extern
uint8_t
aRxBuffer
[
RXBUFFERSIZE
];
// HAL库USART接收Buffer
#define DEBUG_PRINT(str, len) HAL_UART_Transmit(&huart1, (uint8_t*)str, len, 1000);
#define AT_RESP(str, len) HAL_UART_Transmit(&huart2, (uint8_t*)str, len, 1000)
#define DEBUG_PRINT(str, len) HAL_UART_Transmit(&huart1, (uint8_t*)str, len, 1000)
/* USER CODE END Private defines */
void
MX_USART1_UART_Init
(
void
);
...
...
Core/Src/freertos.c
View file @
bbbcab8
...
...
@@ -260,34 +260,24 @@ DONE:
void
StartUartTask
(
void
*
argument
)
{
/* USER CODE BEGIN StartUartTask */
uint8_t
len
;
uint8_t
temp
;
uint16_t
times
;
/* Infinite loop */
for
(;;)
{
if
(
USART_RX_STA
&
0x8000
)
while
(
RINGBUF_CHECKIO
()
)
{
len
=
USART_RX_STA
&
0x3fff
;
// 获取接收到的数据长度
mprintf
(
"
\r\n
Received:
\r\n
"
);
HAL_UART_Transmit
(
&
huart1
,
(
uint8_t
*
)
USART_RX_BUF
,
len
,
1000
);
while
(
__HAL_UART_GET_FLAG
(
&
huart1
,
UART_FLAG_TC
)
!=
SET
);
mprintf
(
"
\r\n\r\n
"
);
USART_RX_STA
=
0
;
RINGBUF_READ
(
&
temp
,
1
);
mprintf
(
"%c"
,
temp
);
}
else
{
times
++
;
if
(
times
%
5000
==
0
)
{
mprintf
(
"
\r\n
UART Test
\r\n
"
);
}
// if (times % 1000 == 0)
// {
// mprintf("print char\r\n");
// }
}
osDelay
(
1
);
osDelay
(
5
);
}
/* USER CODE END StartUartTask */
}
...
...
Core/Src/main.c
View file @
bbbcab8
...
...
@@ -98,8 +98,9 @@ int main(void)
/* USER CODE BEGIN 2 */
// 使能TIM3中断
HAL_TIM_Base_Start_IT
(
&
htim3
);
// 使能UART2中断
HAL_UART_Receive_IT
(
&
huart2
,
(
uint8_t
*
)
aRxBuffer
,
RXBUFFERSIZE
);
// 使能RingBuf和UART2中断
RINGBUF_INIT
(
USART1_RingBuff
,
USART1_BUF_LEN
);
__HAL_UART_ENABLE_IT
(
&
huart2
,
UART_IT_RXNE
);
/* USER CODE END 2 */
/* Call init function for freertos objects (in freertos.c) */
...
...
Core/Src/ringbuffer.c
0 → 100644
View file @
bbbcab8
#include "ringbuffer.h"
#include "string.h"
#ifdef FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#include "semphr.h"
#include "portmacro.h"
xSemaphoreHandle
xSemaphore_RINGBUF
=
NULL
;
#endif
#define WRITE_SUCCEED 0
#define READ_SUCCESS 0
#define FAIL_LACK_OF_SPACE -1
#define FAIL_READ_TOO_FAST -2
static
uint16_t
WRITE_ID
;
static
uint16_t
READ_ID
;
static
uint16_t
BUF_LEN
;
static
uint8_t
*
RING_BUFFER
;
uint16_t
RingBuffer_everMAX
=
0
;
/***************************RINGBUF_SEND***************************/
/*
*warning:this API is not safe for interrupt operation,please ensure only one IRQ functions call this function.
*/
int8_t
RINGBUF_SEND
(
const
char
*
buffer
,
uint16_t
len
)
{
if
(
len
>
RINGBUF_AVALIABLE_SPACE
())
{
return
FAIL_LACK_OF_SPACE
;
}
uint16_t
distance
=
BUF_LEN
-
WRITE_ID
;
if
(
distance
>
len
)
{
memcpy
((
void
*
)
&
RING_BUFFER
[
WRITE_ID
],
buffer
,
len
);
WRITE_ID
+=
len
;
}
else
{
memcpy
((
void
*
)
&
RING_BUFFER
[
WRITE_ID
],
buffer
,
distance
);
WRITE_ID
=
0
;
if
(
len
-
distance
)
{
memcpy
((
void
*
)
&
RING_BUFFER
[
WRITE_ID
],
buffer
+
distance
,
len
-
distance
);
WRITE_ID
=
len
-
distance
;
}
}
(
WRITE_ID
+
BUF_LEN
-
READ_ID
)
%
BUF_LEN
>
RingBuffer_everMAX
?
RingBuffer_everMAX
=
(
WRITE_ID
+
BUF_LEN
-
READ_ID
)
%
BUF_LEN
:
1
;
return
WRITE_SUCCEED
;
}
/***************************RINGBUF_READ***************************/
int8_t
RINGBUF_READ
(
uint8_t
*
buffer
,
uint16_t
len
)
{
#ifdef FREERTOS
xSemaphoreTake
(
xSemaphore_RINGBUF
,
portMAX_DELAY
);
//take Mutex for read
#endif
if
(
len
>
(
BUF_LEN
-
RINGBUF_AVALIABLE_SPACE
()))
{
#ifdef FREERTOS
xSemaphoreGive
(
xSemaphore_RINGBUF
);
#endif
return
FAIL_READ_TOO_FAST
;
}
uint16_t
distance
=
BUF_LEN
-
READ_ID
;
if
(
distance
>
len
)
{
memcpy
((
void
*
)
buffer
,
&
RING_BUFFER
[
READ_ID
],
len
);
READ_ID
+=
len
;
}
else
{
memcpy
((
void
*
)
buffer
,
&
RING_BUFFER
[
READ_ID
],
distance
);
READ_ID
=
0
;
if
(
len
-
distance
)
{
memcpy
((
void
*
)(
buffer
+
distance
),
&
RING_BUFFER
[
READ_ID
],
len
-
distance
);
READ_ID
=
len
-
distance
;
}
}
*
(
buffer
+
len
)
=
NULL
;
#ifdef FREERTOS
xSemaphoreGive
(
xSemaphore_RINGBUF
);
//give Mutex for read
#endif
return
READ_SUCCESS
;
}
/***************************RINGBUF_PEEK***************************/
int8_t
RINGBUF_PEEK
(
uint8_t
*
buffer
,
uint16_t
len
)
{
#ifdef FREERTOS
xSemaphoreTake
(
xSemaphore_RINGBUF
,
portMAX_DELAY
);
//take Mutex for read
#endif
if
(
len
>
(
BUF_LEN
-
RINGBUF_AVALIABLE_SPACE
()))
{
#ifdef FREERTOS
xSemaphoreGive
(
xSemaphore_RINGBUF
);
#endif
return
FAIL_READ_TOO_FAST
;
}
uint16_t
readpoint
=
READ_ID
;
uint16_t
distance
=
BUF_LEN
-
readpoint
;
if
(
distance
>
len
)
{
memcpy
((
void
*
)
buffer
,
&
RING_BUFFER
[
readpoint
],
len
);
readpoint
+=
len
;
}
else
{
memcpy
((
void
*
)
buffer
,
&
RING_BUFFER
[
readpoint
],
distance
);
readpoint
=
0
;
if
(
len
-
distance
)
{
memcpy
((
void
*
)(
buffer
+
distance
),
&
RING_BUFFER
[
readpoint
],
len
-
distance
);
readpoint
=
len
-
distance
;
}
}
*
(
buffer
+
len
)
=
NULL
;
#ifdef FREERTOS
xSemaphoreGive
(
xSemaphore_RINGBUF
);
#endif
return
READ_SUCCESS
;
}
/***************************RINGBUF_INIT***************************/
void
RINGBUF_INIT
(
uint8_t
*
ringbuffer
,
uint16_t
len
)
{
#ifdef FREERTOS
xSemaphore_RINGBUF
=
xSemaphoreCreateMutex
();
#endif
WRITE_ID
=
0
;
READ_ID
=
0
;
RING_BUFFER
=
ringbuffer
;
BUF_LEN
=
len
;
memset
(
RING_BUFFER
,
0x00
,
BUF_LEN
);
}
/***************************RINGBUF_CHECKIO************************/
uint8_t
RINGBUF_CHECKIO
(
void
)
{
if
(
READ_ID
==
WRITE_ID
)
return
0
;
else
return
1
;
}
/****************************RINGBUF_AVALIABLE_SPACE***************/
uint16_t
RINGBUF_AVALIABLE_SPACE
()
{
uint16_t
len
=
0
;
if
(
WRITE_ID
<
READ_ID
)
{
len
=
READ_ID
-
WRITE_ID
;
}
else
{
len
=
BUF_LEN
-
(
WRITE_ID
-
READ_ID
);
}
return
len
;
}
Core/Src/stm32f0xx_it.c
View file @
bbbcab8
...
...
@@ -168,24 +168,14 @@ void TIM6_IRQHandler(void)
void
USART2_IRQHandler
(
void
)
{
/* USER CODE BEGIN USART2_IRQn 0 */
uint
16_t
timeout
=
0
;
uint
8_t
res
;
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler
(
&
huart2
);
//
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
timeout
=
0
;
while
(
HAL_UART_GetState
(
&
huart2
)
!=
HAL_UART_STATE_READY
)
if
(
__HAL_UART_GET_FLAG
(
&
huart2
,
UART_FLAG_RXNE
)
==
pdTRUE
)
{
timeout
++
;
if
(
timeout
>
HAL_MAX_DELAY
)
break
;
}
timeout
=
0
;
while
(
HAL_UART_Receive_IT
(
&
huart2
,
(
uint8_t
*
)
aRxBuffer
,
RXBUFFERSIZE
)
!=
HAL_OK
)
{
timeout
++
;
if
(
timeout
>
HAL_MAX_DELAY
)
break
;
res
=
huart2
.
Instance
->
RDR
;
RINGBUF_SEND
((
const
char
*
)
&
res
,
1
);
}
/* USER CODE END USART2_IRQn 1 */
}
...
...
Core/Src/usart.c
View file @
bbbcab8
...
...
@@ -21,11 +21,7 @@
#include "usart.h"
/* USER CODE BEGIN 0 */
uint8_t
USART_RX_BUF
[
USART_REC_LEN
];
uint16_t
USART_RX_STA
=
0
;
uint8_t
aRxBuffer
[
RXBUFFERSIZE
];
uint8_t
USART1_RingBuff
[
USART1_BUF_LEN
];
/* USER CODE END 0 */
...
...
@@ -176,41 +172,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
}
/* USER CODE BEGIN 1 */
/**
* 串口接收回调函数
*/
void
HAL_UART_RxCpltCallback
(
UART_HandleTypeDef
*
huart
)
{
if
(
huart
->
Instance
==
USART2
)
// 串口1
{
if
((
USART_RX_STA
&
0x8000
)
==
0
)
// 接收未完?????
{
if
(
USART_RX_STA
&
0x4000
)
// 接收到了0x0D
{
if
(
aRxBuffer
[
0
]
!=
0x0a
)
USART_RX_STA
=
0
;
// 接收错误,重新接?????
else
USART_RX_STA
|=
0x8000
;
// 接收完成
}
else
// 还没接收?????0x0D
{
if
(
aRxBuffer
[
0
]
==
0x0d
)
{
USART_RX_STA
|=
0x4000
;
}
else
{
USART_RX_BUF
[
USART_RX_STA
&
0X3FFF
]
=
aRxBuffer
[
0
]
;
USART_RX_STA
++
;
if
(
USART_RX_STA
>
(
USART_REC_LEN
-
1
))
{
USART_RX_STA
=
0
;
// 接收错误,重新开始接?????
}
}
}
}
}
}
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
...
...
Please
register
or
sign in
to post a comment