1414#include "freertos/task.h" // for task api access
1515#include "freertos/event_groups.h" // for event groups
1616#include "freertos/queue.h" // for queue api access
17+ #include "freertos/semphr.h" // for semaphore
1718#include "mb_m.h" // for modbus stack master types definition
1819#include "port.h" // for port callback functions
1920#include "mbutils.h" // for mbutils functions definition for stack callback
@@ -28,7 +29,7 @@ extern BOOL xMBMasterPortSerialTxPoll(void);
2829
2930/*-----------------------Master mode use these variables----------------------*/
3031// Actual wait time depends on the response timer
31- #define MB_SERIAL_API_RESP_TICS (pdMS_TO_TICKS(MB_MAX_RESPONSE_TIME_MS))
32+ #define MB_SERIAL_API_RESP_TICS (pdMS_TO_TICKS(MB_MAX_RESPONSE_TIME_MS))
3233
3334static mb_master_interface_t * mbm_interface_ptr = NULL ;
3435static const char * TAG = "MB_CONTROLLER_MASTER" ;
@@ -127,6 +128,8 @@ static esp_err_t mbc_serial_master_destroy(void)
127128 MB_MASTER_CHECK ((mb_error == MB_ENOERR ), ESP_ERR_INVALID_STATE , "mb stack disable failure." );
128129 (void )vTaskDelete (mbm_opts -> mbm_task_handle );
129130 (void )vEventGroupDelete (mbm_opts -> mbm_event_group );
131+ vSemaphoreDelete (mbm_opts -> mbm_sema );
132+ mbm_opts -> mbm_sema = NULL ;
130133 mb_error = eMBMasterClose ();
131134 MB_MASTER_CHECK ((mb_error == MB_ENOERR ), ESP_ERR_INVALID_STATE ,
132135 "mb stack close failure returned (0x%x)." , (int )mb_error );
@@ -176,22 +179,22 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
176179 eMBMasterReqErrCode mb_error = MB_MRE_MASTER_BUSY ;
177180 esp_err_t error = ESP_FAIL ;
178181
179- if (xMBMasterRunResTake (MB_SERIAL_API_RESP_TICS )) {
180-
182+ if (xSemaphoreTake (mbm_opts -> mbm_sema , MB_SERIAL_API_RESP_TICS ) == pdTRUE ) {
181183 uint8_t mb_slave_addr = request -> slave_addr ;
182184 uint8_t mb_command = request -> command ;
183185 uint16_t mb_offset = request -> reg_start ;
184186 uint16_t mb_size = request -> reg_size ;
185-
187+
186188 // Set the buffer for callback function processing of received data
187189 mbm_opts -> mbm_reg_buffer_ptr = (uint8_t * )data_ptr ;
188190 mbm_opts -> mbm_reg_buffer_size = mb_size ;
189191
190- vMBMasterRunResRelease ();
191-
192192 // Calls appropriate request function to send request and waits response
193193 switch (mb_command )
194194 {
195+ case MB_FUNC_OTHER_REPORT_SLAVEID :
196+ mb_error = eMBMasterReqReportSlaveID ((UCHAR )mb_slave_addr , (LONG )MB_SERIAL_API_RESP_TICS );
197+ break ;
195198 case MB_FUNC_READ_COILS :
196199 mb_error = eMBMasterReqReadCoils ((UCHAR )mb_slave_addr , (USHORT )mb_offset ,
197200 (USHORT )mb_size , (LONG )MB_SERIAL_API_RESP_TICS );
@@ -216,7 +219,6 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
216219 mb_error = eMBMasterReqWriteHoldingRegister ( (UCHAR )mb_slave_addr , (USHORT )mb_offset ,
217220 * (USHORT * )data_ptr , (LONG )MB_SERIAL_API_RESP_TICS );
218221 break ;
219-
220222 case MB_FUNC_WRITE_MULTIPLE_REGISTERS :
221223 mb_error = eMBMasterReqWriteMultipleHoldingRegister ( (UCHAR )mb_slave_addr ,
222224 (USHORT )mb_offset , (USHORT )mb_size ,
@@ -237,6 +239,8 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
237239 mb_error = MB_MRE_NO_REG ;
238240 break ;
239241 }
242+ } else {
243+ ESP_LOGD (TAG , "%s:MBC semaphore take fail." , __func__ );
240244 }
241245
242246 // Propagate the Modbus errors to higher level
@@ -264,11 +268,13 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
264268 break ;
265269
266270 default :
267- ESP_LOGE (TAG , "%s: Incorrect return code (%x) " , __FUNCTION__ , (int )mb_error );
271+ ESP_LOGE (TAG , "%s: Incorrect return code (0x %x) " , __FUNCTION__ , (int )mb_error );
268272 error = ESP_FAIL ;
269273 break ;
270274 }
271275
276+ (void )xSemaphoreGive ( mbm_opts -> mbm_sema );
277+
272278 return error ;
273279}
274280
@@ -508,7 +514,7 @@ eMBErrorCode eMBRegInputCBSerialMaster(UCHAR * pucRegBuffer, USHORT usAddress,
508514 // If input or configuration parameters are incorrect then return an error to stack layer
509515 if ((pucInputBuffer != NULL )
510516 && (usNRegs >= 1 )
511- && (usRegInputNregs == usRegs )) {
517+ && (( usRegInputNregs == usRegs ) || (! usAddress ) )) {
512518 while (usRegs > 0 ) {
513519 _XFER_2_RD (pucInputBuffer , pucRegBuffer );
514520 usRegs -= 1 ;
@@ -698,6 +704,10 @@ esp_err_t mbc_serial_master_create(void** handler)
698704 mbm_opts -> mbm_event_group = xEventGroupCreate ();
699705 MB_MASTER_CHECK ((mbm_opts -> mbm_event_group != NULL ),
700706 ESP_ERR_NO_MEM , "mb event group error." );
707+ mbm_opts -> mbm_sema = xSemaphoreCreateBinary ();
708+ MB_MASTER_CHECK ((mbm_opts -> mbm_sema != NULL ), ESP_ERR_NO_MEM , "%s: mbm resource create error." , __func__ );
709+ (void )xSemaphoreGive ( mbm_opts -> mbm_sema );
710+
701711 // Create modbus controller task
702712 status = xTaskCreatePinnedToCore ((void * )& modbus_master_task ,
703713 "modbus_matask" ,
0 commit comments