You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.4 KiB

/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: Spoolend.c
: 稿
: radkil
: 2026610
:
: 线
:
1. : 2026610
: radkil
:
******************************************************************************/
#include "bsp_uart.h"
#include "bsp_CAN.h"
#include "fdcan.h"
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
TComCtrl *g_ptRS485_1;
TComCtrl *g_ptRS485_2;
TComCtrl *g_ptFDCAN1;
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
/*----------------------------------------------*
* *
*----------------------------------------------*/
static int RS485_1_Send(char *_pBuffer, uint32_t _iSize)
{
HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_SET);
TUartUserData *ptUartUserData = (TUartUserData *)g_ptRS485_1->m_pUserData;
int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100);
HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_RESET);
return iRet;
}
static int RS485_2_Send(char *_pBuffer, uint32_t _iSize)
{
HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_SET);
TUartUserData *ptUartUserData = (TUartUserData *)g_ptRS485_2->m_pUserData;
int iRet = HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100);
HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_RESET);
return iRet;
}
#ifndef CONFIG_UART_IT_IDLEDMA
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == LPUART1)
{
UART_RX_IRQHandler(g_ptRS485_1);
}
else if (huart->Instance == USART1)
{
UART_RX_IRQHandler(g_ptRS485_2);
}
}
#endif
static int check_RS485_1(char *_pBuffer, uint32_t _iSize)
{
rd_ComSend(g_ptRS485_1, _pBuffer, _iSize);
return _iSize;
}
static int check_RS485_2(char *_pBuffer, uint32_t _iSize)
{
rd_ComSend(g_ptRS485_2, _pBuffer, _iSize);
return _iSize;
}
static int check_Spoolend(char *_pBuffer, uint32_t _iSize)
{
TCANUserData *ptCANUserData = (TCANUserData *)g_ptFDCAN1->m_pUserData;
uint32_t iCANId = (uint8_t)ptCANUserData->m_canrx->Identifier;
rd_ComIDSend(g_ptFDCAN1, iCANId, _pBuffer, _iSize);
return _iSize;
}
static int FDCAN1_Send(char *_pBuffer, uint32_t _iSize)
{
return CAN_TX_FIFOQ(g_ptFDCAN1, _pBuffer[0], &_pBuffer[4], _iSize);
}
void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *hfdcan, uint32_t ErrorStatusITs)
{
if (hfdcan->Instance == FDCAN1)
{
MX_FDCAN1_Init();
}
}
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{
if (hfdcan->Instance == FDCAN1)
{
CAN_RX_IRQHandler(g_ptFDCAN1);
}
}
void Myprint_Init(void)
{
TUartUserData *ptUartUserData = UART_userdata_init(1, 115200, 4096);
g_ptRS485_2 = rd_ComCreate(NULL, NULL, RS485_2_Send, ptUartUserData->m_buf_size, ptUartUserData);
UART_IT_init(g_ptRS485_2);
}
int Myprint_putchar(uint8_t ch)
{
char tx_buf[2];
if (ch == '\n')
{
// 遇到换行,发送 \r\n
tx_buf[0] = '\r';
tx_buf[1] = '\n';
if (rd_ComSend(g_ptRS485_2, tx_buf, 2) < 0) return EOF;
return 2;
}
else
{
// 普通字符
tx_buf[0] = ch;
if (rd_ComSend(g_ptRS485_2, tx_buf, 1) < 0) return EOF;
return 1;
}
}
void SpoolendInit(void)
{
TUartUserData *ptUartUserData = UART_userdata_init(0, 115200, 512);
g_ptRS485_1 = rd_ComCreate(check_RS485_1, NULL, RS485_1_Send, ptUartUserData->m_buf_size, ptUartUserData);
UART_IT_init(g_ptRS485_1);
4 days ago
// TUartUserData *ptUartUserData1 = UART_userdata_init(0, 115200, 512);
// g_ptRS485_2 = rd_ComCreate(check_RS485_2, NULL, RS485_2_Send, ptUartUserData1->m_buf_size, ptUartUserData1);
// UART_IT_init(g_ptRS485_2);
TCANUserData *ptCANUserData = CAN_userdata_init(&hfdcan1, 8, 0);
g_ptFDCAN1 = rd_ComCreate(check_Spoolend, NULL, FDCAN1_Send, CONFIG_UART_BUFFER_SIZE, ptCANUserData);
CAN_IT_init(g_ptFDCAN1);
}
void SpoolendTask(void)
{
char pcBuffer1[52] = {0};
4 days ago
// char pcBuffer2[52] = {0};
char pcBuffer3[52] = {0};
__disable_irq();
rd_ComRead(g_ptRS485_1, pcBuffer1, sizeof(pcBuffer1));
4 days ago
// rd_ComRead(g_ptRS485_2, pcBuffer2, sizeof(pcBuffer2));
rd_ComRead(g_ptFDCAN1, pcBuffer3, sizeof(pcBuffer3));
__enable_irq();
}