417 lines
13 KiB
C
417 lines
13 KiB
C
|
||
#include "hostQueueUart.h"
|
||
#include "hostBusIdleDetection.h"
|
||
|
||
#include "FM_GPIO.h"
|
||
|
||
#include "semphr.h"
|
||
|
||
|
||
/* 队列中每个消息的大小 */
|
||
#define QUEUE_SIZE 4
|
||
|
||
/* 485队列的长度 */
|
||
#define rs485QueueLength 5
|
||
#define mcuUartQueueLength 10
|
||
#define loraQueueLength 5
|
||
#define su806QueueLength 10
|
||
|
||
|
||
/* 各个通讯口的队列 */
|
||
QueueHandle_t J0_485_Queue = NULL;
|
||
QueueHandle_t J2_485_Queue = NULL;
|
||
QueueHandle_t J4_485_Queue = NULL;
|
||
QueueHandle_t J6_485_Queue = NULL;
|
||
QueueHandle_t lora_uart_Queue = NULL;
|
||
QueueHandle_t su806_uart_Queue = NULL;
|
||
QueueHandle_t hostMcu_uart_Queue = NULL;
|
||
|
||
/* 创建二值信号量 */
|
||
static SemaphoreHandle_t J0_FreeMemorySemaphore = NULL;
|
||
static SemaphoreHandle_t J2_FreeMemorySemaphore = NULL;
|
||
static SemaphoreHandle_t J4_FreeMemorySemaphore = NULL;
|
||
static SemaphoreHandle_t J6_FreeMemorySemaphore = NULL;
|
||
static SemaphoreHandle_t lora_FreeMemorySemaphore = NULL;
|
||
static SemaphoreHandle_t su806_FreeMemorySemaphore = NULL;
|
||
static SemaphoreHandle_t hostMcu_FreeMemorySemaphore = NULL;
|
||
// 创建队列集
|
||
static QueueSetHandle_t hostBinarySemaphoreSet = NULL;
|
||
/* 创建二值信号量 */
|
||
static SemaphoreHandle_t J0_sendOverSemaphore = NULL;
|
||
static SemaphoreHandle_t J2_sendOverSemaphore = NULL;
|
||
static SemaphoreHandle_t J4_sendOverSemaphore = NULL;
|
||
static SemaphoreHandle_t J6_sendOverSemaphore = NULL;
|
||
static SemaphoreHandle_t lora_sendOverSemaphore = NULL;
|
||
static SemaphoreHandle_t su806_sendOverSemaphore = NULL;
|
||
static SemaphoreHandle_t hostMcu_sendOverSemaphore = NULL;
|
||
|
||
/* 通过该结构体接收对应的数据用来发送,结束后通过该结构体,释放数据的内存 */
|
||
typedef struct _hostQueueSendDataInfo {
|
||
hostQueueUartSendInfo *J0_485_data;
|
||
hostQueueUartSendInfo *J2_485_data;
|
||
hostQueueUartSendInfo *J4_485_data;
|
||
hostQueueUartSendInfo *J6_485_data;
|
||
hostQueueUartSendInfo *lora_uart_data;
|
||
hostQueueUartSendInfo *su806_uart_data;
|
||
hostQueueUartSendInfo *hostMcu_uart_data;
|
||
} hostQueueSendDataInfo;
|
||
static hostQueueSendDataInfo hostQueueSendData;
|
||
|
||
/**
|
||
* @brief 初始化串口发送使用到的队列和释放内存所使用的信号量
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void hostUartSendInit(void)
|
||
{
|
||
/* 初始化队列 */
|
||
J0_485_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
J2_485_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
J4_485_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
J6_485_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
lora_uart_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
su806_uart_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
hostMcu_uart_Queue = xQueueCreate(rs485QueueLength, QUEUE_SIZE);
|
||
|
||
/* 初始化释放内存二值变量 */
|
||
J0_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
J2_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
J4_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
J6_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
lora_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
su806_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
hostMcu_FreeMemorySemaphore = xSemaphoreCreateBinary();
|
||
// 初始化队列集
|
||
hostBinarySemaphoreSet = xQueueCreateSet(7);
|
||
// 将二值信号量添加到队列集中
|
||
xQueueAddToSet(J0_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
xQueueAddToSet(J2_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
xQueueAddToSet(J4_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
xQueueAddToSet(J6_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
xQueueAddToSet(lora_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
xQueueAddToSet(su806_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
xQueueAddToSet(hostMcu_FreeMemorySemaphore, hostBinarySemaphoreSet);
|
||
|
||
/* 发送完成信号量 */
|
||
J0_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(J0_sendOverSemaphore);
|
||
J2_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(J2_sendOverSemaphore);
|
||
J4_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(J4_sendOverSemaphore);
|
||
J6_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(J6_sendOverSemaphore);
|
||
lora_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(lora_sendOverSemaphore);
|
||
su806_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(su806_sendOverSemaphore);
|
||
hostMcu_sendOverSemaphore = xSemaphoreCreateBinary();
|
||
xSemaphoreGive(hostMcu_sendOverSemaphore);
|
||
}
|
||
|
||
/**
|
||
* @brief 从J0队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void J0QueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(J0_485_Queue, hostQueueSendData.J0_485_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(J0_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 总线空闲 */
|
||
if (xSemaphoreTake(J0_IDLESemaphore, portMAX_DELAY) == pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_J1_handle, hostQueueSendData.J0_485_data->data
|
||
, hostQueueSendData.J0_485_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 从J2队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void J2QueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(J2_485_Queue, hostQueueSendData.J2_485_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(J2_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 总线空闲 */
|
||
if (xSemaphoreTake(J2_IDLESemaphore, portMAX_DELAY) == pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_J2_handle, hostQueueSendData.J2_485_data->data
|
||
, hostQueueSendData.J2_485_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 从J4队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void J4QueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(J4_485_Queue, hostQueueSendData.J4_485_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(J4_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 总线空闲 */
|
||
if (xSemaphoreTake(J4_IDLESemaphore, portMAX_DELAY) == pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_J4_handle, hostQueueSendData.J2_485_data->data
|
||
, hostQueueSendData.J4_485_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 从J6队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void J6QueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(J6_485_Queue, hostQueueSendData.J6_485_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(J6_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 总线空闲 */
|
||
if (xSemaphoreTake(J6_IDLESemaphore, portMAX_DELAY) == pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_J6_handle, hostQueueSendData.J6_485_data->data
|
||
, hostQueueSendData.J6_485_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 从lora队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void LoraQueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(lora_uart_Queue, hostQueueSendData.lora_uart_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(lora_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_Lora_handle, hostQueueSendData.lora_uart_data->data
|
||
, hostQueueSendData.lora_uart_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 从su806队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void Su806QueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(su806_uart_Queue, hostQueueSendData.su806_uart_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(su806_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_Su806_handle, hostQueueSendData.su806_uart_data->data
|
||
, hostQueueSendData.su806_uart_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 从hostMcu队列中取出数据,并完成发送,无数据或不满足发送条件时,处于阻塞状态
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void HostMcuQueueSend(void)
|
||
{
|
||
/* 有数据需要发送 */
|
||
if (xQueueReceive(hostMcu_uart_Queue, hostQueueSendData.hostMcu_uart_data, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
/* 上次数据发送完成 */
|
||
if (xSemaphoreTake(hostMcu_sendOverSemaphore, portMAX_DELAY) != pdTRUE) {
|
||
return;
|
||
}
|
||
hostUartInterruptSend(g_Mcu_handle, hostQueueSendData.hostMcu_uart_data->data
|
||
, hostQueueSendData.hostMcu_uart_data->length);
|
||
}
|
||
|
||
/**
|
||
* @brief 通过二值信号量释放中断发送数据的内存
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void hostBinarySemaphoreFreeMemory(void)
|
||
{
|
||
// 等待队列集中的事件
|
||
QueueSetMemberHandle_t xActivatedMember = xQueueSelectFromSet(hostBinarySemaphoreSet, portMAX_DELAY);
|
||
|
||
if (xActivatedMember == J0_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.J0_485_data);
|
||
vTaskDelay(1);
|
||
readJ0_485;
|
||
// 处理二值信号量事件
|
||
xSemaphoreTake(J0_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(J0_sendOverSemaphore);
|
||
}
|
||
|
||
else if (xActivatedMember == J2_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.J2_485_data);
|
||
vTaskDelay(1);
|
||
readJ2_485;
|
||
xSemaphoreTake(J2_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(J2_sendOverSemaphore);
|
||
}
|
||
|
||
else if (xActivatedMember == J4_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.J4_485_data);
|
||
vTaskDelay(1);
|
||
readJ4_485;
|
||
xSemaphoreTake(J4_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(J4_sendOverSemaphore);
|
||
}
|
||
|
||
else if (xActivatedMember == J6_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.J6_485_data);
|
||
vTaskDelay(1);
|
||
readJ6_485;
|
||
xSemaphoreTake(J6_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(J6_sendOverSemaphore);
|
||
}
|
||
|
||
else if (xActivatedMember == lora_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.lora_uart_data);
|
||
xSemaphoreTake(lora_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(lora_sendOverSemaphore);
|
||
}
|
||
|
||
else if (xActivatedMember == su806_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.su806_uart_data);
|
||
xSemaphoreTake(su806_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(su806_sendOverSemaphore);
|
||
}
|
||
|
||
else if (xActivatedMember == hostMcu_FreeMemorySemaphore) {
|
||
vPortFree(hostQueueSendData.hostMcu_uart_data);
|
||
xSemaphoreTake(hostMcu_FreeMemorySemaphore, 0);
|
||
xSemaphoreGive(hostMcu_sendOverSemaphore);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 通过中断发送
|
||
* @param device 设备
|
||
data 指向数据的指针
|
||
len 数据长度
|
||
* @retval 0:成功
|
||
*/
|
||
uint8_t hostUartInterruptSend(device_handle device, uint8_t *data, uint16_t len)
|
||
{
|
||
/* 指向数据 */
|
||
uart_device_info *dev = (uart_device_info *)device;
|
||
dev->uart_send_data.data = data;
|
||
dev->uart_send_data.count = 0;
|
||
dev->uart_send_data.len = len;
|
||
|
||
/* 开始发送 */
|
||
if (device == g_J0_handle) {
|
||
xSemaphoreTake(J0_sendOverSemaphore, 0);
|
||
writeJ0_485;
|
||
USART_ITConfig(J0_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(J0_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
else if (device == g_J2_handle) {
|
||
xSemaphoreTake(J2_sendOverSemaphore, 0);
|
||
writeJ2_485;
|
||
USART_ITConfig(J2_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(J2_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
else if (device == g_J4_handle) {
|
||
xSemaphoreTake(J4_sendOverSemaphore, 0);
|
||
writeJ4_485;
|
||
USART_ITConfig(J4_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(J4_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
else if (device == g_J6_handle) {
|
||
xSemaphoreTake(J6_sendOverSemaphore, 0);
|
||
writeJ6_485;
|
||
USART_ITConfig(J6_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(J6_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
else if (device == g_Mcu_handle) {
|
||
xSemaphoreTake(hostMcu_sendOverSemaphore, 0);
|
||
USART_ITConfig(Mcu_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(Mcu_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
else if (device == g_Lora_handle) {
|
||
xSemaphoreTake(lora_sendOverSemaphore, 0);
|
||
USART_ITConfig(Lora_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(Lora_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
else if (device == g_Su806_handle) {
|
||
xSemaphoreTake(su806_sendOverSemaphore, 0);
|
||
USART_ITConfig(Su806_USART, USART_IT_TXE, ENABLE);
|
||
USART_SendData(Su806_USART, dev->uart_send_data.data[dev->uart_send_data.count++]);
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief 数据发送完成后用于清除内存
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void J0_485_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(J0_FreeMemorySemaphore, 0);
|
||
}
|
||
void J2_485_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(J2_FreeMemorySemaphore, 0);
|
||
}
|
||
void J4_485_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(J4_FreeMemorySemaphore, 0);
|
||
}
|
||
void J6_485_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(J6_FreeMemorySemaphore, 0);
|
||
}
|
||
void Lora_uart_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(lora_FreeMemorySemaphore, 0);
|
||
}
|
||
void Su806_uart_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(su806_FreeMemorySemaphore, 0);
|
||
}
|
||
void hostMcu_uart_IN_TXE(void)
|
||
{
|
||
xSemaphoreGiveFromISR(hostMcu_FreeMemorySemaphore, 0);
|
||
}
|
||
|