Переделано изменение направление передачи
Теперь это указатель в структуре, а не глобальный дефайн. Благодаря чему можно настраивать разные ножик для разных юартов. Также удален указатель на буфер RS, теперь он полностью созадется в структуре
This commit is contained in:
@@ -29,7 +29,7 @@
|
|||||||
//----------------Слейв модбас----------------//
|
//----------------Слейв модбас----------------//
|
||||||
#include "modbus.h"
|
#include "modbus.h"
|
||||||
|
|
||||||
MODBUS_FirstInit(&hmodbus1, &huart1, &htim3);
|
MODBUS_FirstInit(&hmodbus1, &huart1, &htim3, NULL);
|
||||||
MODBUS_Config(&hmodbus1, MODBUS_DEVICE_ID, MODBUS_TIMEOUT, MODBUS_MODE_SLAVE);
|
MODBUS_Config(&hmodbus1, MODBUS_DEVICE_ID, MODBUS_TIMEOUT, MODBUS_MODE_SLAVE);
|
||||||
MODBUS_SlaveStart(&hmodbus1, NULL);
|
MODBUS_SlaveStart(&hmodbus1, NULL);
|
||||||
@endcode
|
@endcode
|
||||||
@@ -124,7 +124,7 @@
|
|||||||
//----------------FUNCTIONS FOR USER----------------
|
//----------------FUNCTIONS FOR USER----------------
|
||||||
|
|
||||||
/* Инициализация периферии модбас. */
|
/* Инициализация периферии модбас. */
|
||||||
HAL_StatusTypeDef MODBUS_FirstInit(RS_HandleTypeDef *hmodbus, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim);
|
HAL_StatusTypeDef MODBUS_FirstInit(RS_HandleTypeDef *hmodbus, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim, void (*pSetDirection)(int Tx));
|
||||||
/* Программная конфигурация модбас. */
|
/* Программная конфигурация модбас. */
|
||||||
HAL_StatusTypeDef MODBUS_Config(RS_HandleTypeDef *hmodbus, uint8_t ID, uint16_t Timeout, uint8_t master);
|
HAL_StatusTypeDef MODBUS_Config(RS_HandleTypeDef *hmodbus, uint8_t ID, uint16_t Timeout, uint8_t master);
|
||||||
/* Запуск слейв устройства */
|
/* Запуск слейв устройства */
|
||||||
|
|||||||
@@ -68,7 +68,7 @@
|
|||||||
#define RS_Set_TX_End(_hRS_) RS_Reset_TX_Flags(_hRS_); RS_Set_TX_End_Flag(_hRS_)
|
#define RS_Set_TX_End(_hRS_) RS_Reset_TX_Flags(_hRS_); RS_Set_TX_End_Flag(_hRS_)
|
||||||
|
|
||||||
/* Clear all RS stuff */
|
/* Clear all RS stuff */
|
||||||
#define RS_Clear_All(_hRS_) RS_Clear_Buff(_hRS_->pBufferPtr); RS_Reset_RX_Flags(_hRS_); RS_Reset_TX_Flags(_hRS_);
|
#define RS_Clear_All(_hRS_) RS_Clear_Buff(_hRS_->BufferPtr); RS_Reset_RX_Flags(_hRS_); RS_Reset_TX_Flags(_hRS_);
|
||||||
|
|
||||||
//#define MB_Is_RX_Busy(_hRS_) ((_hRS_->huart->gState&HAL_USART_STATE_BUSY_RX) == HAL_USART_STATE_BUSY_RX)
|
//#define MB_Is_RX_Busy(_hRS_) ((_hRS_->huart->gState&HAL_USART_STATE_BUSY_RX) == HAL_USART_STATE_BUSY_RX)
|
||||||
//#define MB_Is_TX_Busy(_hRS_) ((_hRS_->huart->gState&HAL_USART_STATE_BUSY_RX) == HAL_USART_STATE_BUSY_TX)
|
//#define MB_Is_TX_Busy(_hRS_) ((_hRS_->huart->gState&HAL_USART_STATE_BUSY_RX) == HAL_USART_STATE_BUSY_TX)
|
||||||
@@ -229,7 +229,7 @@ typedef struct // RS_HandleTypeDef
|
|||||||
/* MESSAGE */
|
/* MESSAGE */
|
||||||
uint8_t ID; ///< ID хендла
|
uint8_t ID; ///< ID хендла
|
||||||
RS_MsgTypeDef *pMessagePtr; ///< Указатель на структуру протокола
|
RS_MsgTypeDef *pMessagePtr; ///< Указатель на структуру протокола
|
||||||
uint8_t *pBufferPtr; ///< Указатеь на буфер UART
|
uint8_t BufferPtr[RS_MSG_SIZE_MAX]; ///< Указатеь на буфер UART
|
||||||
int32_t RS_Message_Size; ///< size of whole message, not only data
|
int32_t RS_Message_Size; ///< size of whole message, not only data
|
||||||
|
|
||||||
/* HANDLERS and SETTINGS */
|
/* HANDLERS and SETTINGS */
|
||||||
@@ -237,6 +237,7 @@ typedef struct // RS_HandleTypeDef
|
|||||||
TIM_HandleTypeDef *htim; ///< Хендл TIM
|
TIM_HandleTypeDef *htim; ///< Хендл TIM
|
||||||
RS_ModeTypeDef sRS_Mode; ///< Настройка: слейв/мастер @ref RS_ModeTypeDef
|
RS_ModeTypeDef sRS_Mode; ///< Настройка: слейв/мастер @ref RS_ModeTypeDef
|
||||||
uint16_t sRS_Timeout; ///< Настройка: Таймаут в тиках таймера
|
uint16_t sRS_Timeout; ///< Настройка: Таймаут в тиках таймера
|
||||||
|
void (*pSetDirection)(int Tx); ///< Указатель на функцию: смена направления
|
||||||
void (*pCallback)(void*, void*); ///< Указатель на коллбек: принят ответ в режиме мастер
|
void (*pCallback)(void*, void*); ///< Указатель на коллбек: принят ответ в режиме мастер
|
||||||
|
|
||||||
/* FLAGS */
|
/* FLAGS */
|
||||||
@@ -280,7 +281,7 @@ RS_StatusTypeDef RS_Receive_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg);
|
|||||||
RS_StatusTypeDef RS_Transmit_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg);
|
RS_StatusTypeDef RS_Transmit_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg);
|
||||||
|
|
||||||
/* Инициалазация структуры @ref RS_HandleTypeDef */
|
/* Инициалазация структуры @ref RS_HandleTypeDef */
|
||||||
RS_StatusTypeDef RS_Init(RS_HandleTypeDef *hRS, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim, uint8_t *pRS_BufferPtr);
|
RS_StatusTypeDef RS_Init(RS_HandleTypeDef *hRS, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim, void (*pSetDirection)(int Tx));
|
||||||
|
|
||||||
/* Отменить прием/передачу RS/UART */
|
/* Отменить прием/передачу RS/UART */
|
||||||
RS_StatusTypeDef RS_Abort(RS_HandleTypeDef *hRS, RS_AbortTypeDef AbortMode);
|
RS_StatusTypeDef RS_Abort(RS_HandleTypeDef *hRS, RS_AbortTypeDef AbortMode);
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ static void MB_DefaultCallback(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef *modbus_
|
|||||||
* @param hmodbus Указатель на хендлер RS
|
* @param hmodbus Указатель на хендлер RS
|
||||||
* @param huart Указатель на хендлер UART
|
* @param huart Указатель на хендлер UART
|
||||||
* @param htim Указатель на хендлер TIM
|
* @param htim Указатель на хендлер TIM
|
||||||
|
* @param pSetDirection Указатель на функцию для смены направления RS485
|
||||||
* @details Подключает хендлы периферии к hmodbus
|
* @details Подключает хендлы периферии к hmodbus
|
||||||
* Конфигурация выставляется по умолчанию из modbus_config.h
|
* Конфигурация выставляется по умолчанию из modbus_config.h
|
||||||
*/
|
*/
|
||||||
HAL_StatusTypeDef MODBUS_FirstInit(RS_HandleTypeDef *hmodbus, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim)
|
HAL_StatusTypeDef MODBUS_FirstInit(RS_HandleTypeDef *hmodbus, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim, void (*pSetDirection)(int Tx))
|
||||||
{
|
{
|
||||||
if((hmodbus == NULL) || (huart == NULL))
|
if((hmodbus == NULL) || (huart == NULL))
|
||||||
{
|
{
|
||||||
@@ -51,9 +52,10 @@ HAL_StatusTypeDef MODBUS_FirstInit(RS_HandleTypeDef *hmodbus, UART_HandleTypeDef
|
|||||||
hmodbus->sRS_Mode = RS_SLAVE_ALWAYS_WAIT;
|
hmodbus->sRS_Mode = RS_SLAVE_ALWAYS_WAIT;
|
||||||
|
|
||||||
// INIT
|
// INIT
|
||||||
hmodbus->RS_STATUS = RS_Init(hmodbus, huart, htim, 0);
|
hmodbus->RS_STATUS = RS_Init(hmodbus, huart, htim, pSetDirection);
|
||||||
|
|
||||||
RS_EnableReceive();
|
if(hmodbus->pSetDirection)
|
||||||
|
hmodbus->pSetDirection(0);
|
||||||
|
|
||||||
if(hmodbus->RS_STATUS == RS_OK)
|
if(hmodbus->RS_STATUS == RS_OK)
|
||||||
return HAL_OK;
|
return HAL_OK;
|
||||||
|
|||||||
@@ -25,8 +25,6 @@
|
|||||||
#include "rs_message.h"
|
#include "rs_message.h"
|
||||||
#include "modbus_diag.h"
|
#include "modbus_diag.h"
|
||||||
|
|
||||||
uint8_t RS_Buffer[RS_MSG_SIZE_MAX]; // uart buffer
|
|
||||||
|
|
||||||
extern void RS_UART_Init(void);
|
extern void RS_UART_Init(void);
|
||||||
extern void RS_UART_DeInit(UART_HandleTypeDef *huart);
|
extern void RS_UART_DeInit(UART_HandleTypeDef *huart);
|
||||||
extern void RS_TIM_Init(void);
|
extern void RS_TIM_Init(void);
|
||||||
@@ -52,7 +50,8 @@ RS_StatusTypeDef RS_Receive_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
|
|
||||||
//-----------INITIALIZE RECEIVE-------------
|
//-----------INITIALIZE RECEIVE-------------
|
||||||
// if all OK: start receiving
|
// if all OK: start receiving
|
||||||
RS_EnableReceive();
|
if(hRS->pSetDirection)
|
||||||
|
hRS->pSetDirection(0);
|
||||||
RS_Set_Busy(hRS); // set RS busy
|
RS_Set_Busy(hRS); // set RS busy
|
||||||
RS_Set_RX_Flags(hRS); // initialize flags for receive
|
RS_Set_RX_Flags(hRS); // initialize flags for receive
|
||||||
hRS->pMessagePtr = RS_msg; // set pointer to message structire for filling it from UARTHandler fucntions
|
hRS->pMessagePtr = RS_msg; // set pointer to message structire for filling it from UARTHandler fucntions
|
||||||
@@ -61,7 +60,7 @@ RS_StatusTypeDef RS_Receive_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
|
|
||||||
// start receiving
|
// start receiving
|
||||||
__HAL_UART_ENABLE_IT(hRS->huart, UART_IT_IDLE);
|
__HAL_UART_ENABLE_IT(hRS->huart, UART_IT_IDLE);
|
||||||
uart_res = HAL_UART_Receive_IT(hRS->huart, &hRS->pBufferPtr[hRS->RS_Message_Size], RS_MSG_SIZE_MAX); // receive until ByteCnt+1 byte,
|
uart_res = HAL_UART_Receive_IT(hRS->huart, &hRS->BufferPtr[hRS->RS_Message_Size], RS_MSG_SIZE_MAX); // receive until ByteCnt+1 byte,
|
||||||
// then in Callback restart receive for rest bytes
|
// then in Callback restart receive for rest bytes
|
||||||
|
|
||||||
// if receive isnt started - abort RS
|
// if receive isnt started - abort RS
|
||||||
@@ -101,7 +100,7 @@ RS_StatusTypeDef RS_Transmit_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
|
|
||||||
|
|
||||||
//------------COLLECT MESSAGE---------------
|
//------------COLLECT MESSAGE---------------
|
||||||
RS_RES = RS_Collect_Message(hRS, RS_msg, hRS->pBufferPtr);
|
RS_RES = RS_Collect_Message(hRS, RS_msg, hRS->BufferPtr);
|
||||||
if (RS_RES != RS_OK) // if message isnt collect - stop RS and return error in RS_RES
|
if (RS_RES != RS_OK) // if message isnt collect - stop RS and return error in RS_RES
|
||||||
{// need collect message status, so doesnt write abort to RS_RES
|
{// need collect message status, so doesnt write abort to RS_RES
|
||||||
RS_Abort(hRS, ABORT_RS);
|
RS_Abort(hRS, ABORT_RS);
|
||||||
@@ -111,7 +110,8 @@ RS_StatusTypeDef RS_Transmit_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
{
|
{
|
||||||
|
|
||||||
//----------INITIALIZE TRANSMIT-------------
|
//----------INITIALIZE TRANSMIT-------------
|
||||||
RS_EnableTransmit();
|
if(hRS->pSetDirection)
|
||||||
|
hRS->pSetDirection(1);
|
||||||
|
|
||||||
RS_Set_Busy(hRS); // set RS busy
|
RS_Set_Busy(hRS); // set RS busy
|
||||||
RS_Set_TX_Flags(hRS); // initialize flags for transmit IT
|
RS_Set_TX_Flags(hRS); // initialize flags for transmit IT
|
||||||
@@ -123,7 +123,7 @@ RS_StatusTypeDef RS_Transmit_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
return RS_ERR;
|
return RS_ERR;
|
||||||
}
|
}
|
||||||
// if all OK: start transmitting
|
// if all OK: start transmitting
|
||||||
uart_res = HAL_UART_Transmit_IT(hRS->huart, hRS->pBufferPtr, hRS->RS_Message_Size);
|
uart_res = HAL_UART_Transmit_IT(hRS->huart, hRS->BufferPtr, hRS->RS_Message_Size);
|
||||||
// if transmit isnt started - abort RS
|
// if transmit isnt started - abort RS
|
||||||
if(uart_res != HAL_OK)
|
if(uart_res != HAL_OK)
|
||||||
{
|
{
|
||||||
@@ -153,7 +153,7 @@ RS_StatusTypeDef RS_Transmit_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
* @return RS_RES Статус о состоянии RS после инициализации.
|
* @return RS_RES Статус о состоянии RS после инициализации.
|
||||||
* @details Инициализация перефирии и структуры для приема-передачи по RS.
|
* @details Инициализация перефирии и структуры для приема-передачи по RS.
|
||||||
*/
|
*/
|
||||||
RS_StatusTypeDef RS_Init(RS_HandleTypeDef *hRS, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim, uint8_t *pRS_BufferPtr)
|
RS_StatusTypeDef RS_Init(RS_HandleTypeDef *hRS, UART_HandleTypeDef *huart, TIM_HandleTypeDef *htim, void (*pSetDirection)(int Tx))
|
||||||
{
|
{
|
||||||
// check that hRS is defined
|
// check that hRS is defined
|
||||||
if (hRS == NULL)
|
if (hRS == NULL)
|
||||||
@@ -166,13 +166,7 @@ RS_StatusTypeDef RS_Init(RS_HandleTypeDef *hRS, UART_HandleTypeDef *huart, TIM_H
|
|||||||
|
|
||||||
hRS->htim = htim;
|
hRS->htim = htim;
|
||||||
|
|
||||||
// check that buffer is defined
|
hRS->pSetDirection = pSetDirection;
|
||||||
if (hRS->pBufferPtr == NULL)
|
|
||||||
{
|
|
||||||
hRS->pBufferPtr = RS_Buffer; // if no - set default
|
|
||||||
}
|
|
||||||
else
|
|
||||||
hRS->pBufferPtr = pRS_BufferPtr; // if yes - set by user
|
|
||||||
|
|
||||||
return RS_OK;
|
return RS_OK;
|
||||||
}
|
}
|
||||||
@@ -349,7 +343,7 @@ void RS_UART_Handler(RS_HandleTypeDef *hRS)
|
|||||||
RS_Set_RX_End(hRS);
|
RS_Set_RX_End(hRS);
|
||||||
|
|
||||||
// Парсим наше сообщение
|
// Парсим наше сообщение
|
||||||
hRS->RS_STATUS = RS_Parse_Message(hRS, hRS->pMessagePtr, hRS->pBufferPtr);
|
hRS->RS_STATUS = RS_Parse_Message(hRS, hRS->pMessagePtr, hRS->BufferPtr);
|
||||||
|
|
||||||
// Если сообещине принято корректно
|
// Если сообещине принято корректно
|
||||||
if(hRS->RS_STATUS == RS_OK)
|
if(hRS->RS_STATUS == RS_OK)
|
||||||
|
|||||||
Reference in New Issue
Block a user