24
24
#define highByte (w ) ((w) >> 8)
25
25
26
26
27
+
28
+
27
29
///Queue Modbus RX
28
30
//osMessageQueueId_t QueueModbusHandle;
29
31
const osMessageQueueAttr_t QueueModbus_attributes = {
@@ -180,7 +182,8 @@ void ModbusInit(modbusHandler_t * modH)
180
182
*/
181
183
void ModbusStart (modbusHandler_t * modH )
182
184
{
183
- if (modH -> EN_Port != NULL )
185
+
186
+ if (modH -> EN_Port != NULL )
184
187
{
185
188
// return RS485 transceiver to transmit mode
186
189
HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_RESET );
@@ -205,7 +208,29 @@ void ModbusStart(modbusHandler_t * modH)
205
208
206
209
modH -> u8lastRec = modH -> u8BufferSize = 0 ;
207
210
modH -> u16InCnt = modH -> u16OutCnt = modH -> u16errCnt = 0 ;
211
+ #if ENABLE_USB_CDC == 1
212
+ modH -> u8TypeHW = USART_HW ;
213
+ #endif
214
+ }
215
+
216
+ #if ENABLE_USB_CDC == 1
217
+ extern void MX_USB_DEVICE_Init (void );
218
+ void ModbusStartCDC (modbusHandler_t * modH )
219
+ {
220
+
221
+
222
+ if (modH -> uiModbusType == SLAVE_RTU && modH -> au16regs == NULL )
223
+ {
224
+ while (1 ); //ERROR define the DATA pointer shared through Modbus
225
+ }
226
+
227
+ // MX_USB_DEVICE_Init();
228
+
229
+ modH -> u8lastRec = modH -> u8BufferSize = 0 ;
230
+ modH -> u16InCnt = modH -> u16OutCnt = modH -> u16errCnt = 0 ;
231
+ modH -> u8TypeHW = USB_CDC_HW ;
208
232
}
233
+ #endif
209
234
210
235
211
236
void vTimerCallbackT35 (TimerHandle_t * pxTimer )
@@ -248,23 +273,39 @@ void StartTaskModbusSlave(void *argument)
248
273
{
249
274
250
275
modbusHandler_t * modH = (modbusHandler_t * )argument ;
276
+ int8_t i8state ;
251
277
252
278
for (;;)
253
279
{
254
280
ulTaskNotifyTake (pdTRUE , portMAX_DELAY ); /* Block indefinitely until a Modbus Frame arrives */
255
281
256
282
modH -> i8lastError = 0 ;
283
+
284
+ #if ENABLE_USB_CDC == 1
285
+ i8state = modH -> u8BufferSize ;
286
+ if (i8state == ERR_BUFF_OVERFLOW )
287
+ {
288
+ modH -> i8lastError = ERR_BUFF_OVERFLOW ;
289
+ modH -> u16errCnt ++ ;
290
+ continue ;
291
+ }
292
+
293
+ #else
294
+
257
295
modH -> u8BufferSize = uxQueueMessagesWaiting (modH -> QueueModbusHandle );
258
296
if (modH -> EN_Port != NULL )
259
297
{
260
- HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_RESET ); // is this required?
298
+ HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_RESET ); // is this required?
261
299
}
300
+ i8state = getRxBuffer (modH );
301
+
302
+ #endif
262
303
263
- int8_t i8state = getRxBuffer (modH );
264
304
265
305
if (i8state < 7 ){
266
306
//The size of the frame is invalid
267
- modH -> i8lastError = ERR_BAD_SIZE ;;
307
+ modH -> i8lastError = ERR_BAD_SIZE ;
308
+ modH -> u16errCnt ++ ;
268
309
xQueueGenericReset (modH -> QueueModbusHandle , pdFALSE );
269
310
continue ;
270
311
}
@@ -333,8 +374,14 @@ void StartTaskModbusSlave(void *argument)
333
374
void ModbusQuery (modbusHandler_t * modH , modbus_t telegram )
334
375
{
335
376
//Add the telegram to the TX tail Queue of Modbus
377
+ if (modH -> uiModbusType == MASTER_RTU )
378
+ {
336
379
telegram .u32CurrentTask = (uint32_t * ) osThreadGetId ();
337
380
xQueueSendToBack (modH -> QueueTelegramHandle , & telegram , 0 );
381
+ }
382
+ else {
383
+ while (1 );// error a master slave cannot send queries
384
+ }
338
385
}
339
386
340
387
@@ -472,6 +519,7 @@ void StartTaskModbusMaster(void *argument)
472
519
modbusHandler_t * modH = (modbusHandler_t * )argument ;
473
520
uint32_t ulNotificationValue ;
474
521
modbus_t telegram ;
522
+ int8_t i8state ;
475
523
476
524
for (;;)
477
525
{
@@ -484,6 +532,7 @@ void StartTaskModbusMaster(void *argument)
484
532
/* Block indefinitely until a Modbus Frame arrives or query timeouts*/
485
533
ulNotificationValue = ulTaskNotifyTake (pdTRUE , portMAX_DELAY );
486
534
modH -> i8lastError = 0 ;
535
+
487
536
if (ulNotificationValue == NO_REPLY )
488
537
{
489
538
modH -> i8state = COM_IDLE ;
@@ -493,9 +542,30 @@ void StartTaskModbusMaster(void *argument)
493
542
continue ;
494
543
}
495
544
496
- modH -> u8BufferSize = uxQueueMessagesWaiting (modH -> QueueModbusHandle );
545
+ //modH->u8BufferSize = uxQueueMessagesWaiting(modH->QueueModbusHandle);
546
+
547
+ #if ENABLE_USB_CDC == 1
548
+ if (modH -> u8TypeHW == USB_CDC_HW )
549
+ {
550
+ i8state = modH -> u8BufferSize ;
551
+ if (i8state == ERR_BUFF_OVERFLOW )
552
+ {
553
+ modH -> i8state = COM_IDLE ;
554
+ modH -> i8lastError = NO_REPLY ;
555
+ modH -> u16errCnt ++ ;
556
+ xTaskNotify ((TaskHandle_t )telegram .u32CurrentTask , modH -> i8lastError , eSetValueWithOverwrite );
557
+ continue ;
558
+ }
559
+ }
560
+ else
561
+ {
562
+ i8state = getRxBuffer (modH );
563
+ }
564
+
565
+ #else
566
+ i8state = getRxBuffer (modH );
567
+ #endif
497
568
498
- int8_t i8state = getRxBuffer (modH );
499
569
//modH->u8lastError = i8state;
500
570
501
571
if (i8state < 6 ){
@@ -827,6 +897,10 @@ void buildException( uint8_t u8exception, modbusHandler_t *modH )
827
897
}
828
898
829
899
900
+ #if ENABLE_USB_CDC == 1
901
+ extern uint8_t CDC_Transmit_FS (uint8_t * Buf , uint16_t Len );
902
+ #endif
903
+
830
904
/**
831
905
* @brief
832
906
* This method transmits au8Buffer to Serial line.
@@ -846,47 +920,61 @@ void sendTxBuffer(modbusHandler_t *modH)
846
920
modH -> u8BufferSize ++ ;
847
921
modH -> au8Buffer [ modH -> u8BufferSize ] = u16crc & 0x00ff ;
848
922
modH -> u8BufferSize ++ ;
849
-
850
- if (modH -> EN_Port != NULL )
923
+ #if ENABLE_USB_CDC == 1
924
+ if (modH -> u8TypeHW == USART_HW )
851
925
{
852
- // set RS485 transceiver to transmit mode
853
- HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_SET );
854
- }
855
-
856
- // transfer buffer to serial line
857
- //port->write( au8Buffer, u8BufferSize );
858
- //HAL_UART_Transmit(modH->port, modH->au8Buffer , modH->u8BufferSize, 100);
859
- HAL_UART_Transmit_IT (modH -> port , modH -> au8Buffer , modH -> u8BufferSize );
926
+ #endif
927
+ if (modH -> EN_Port != NULL )
928
+ {
929
+ // set RS485 transceiver to transmit mode
930
+ HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_SET );
931
+ }
860
932
861
- ulTaskNotifyTake (pdTRUE , portMAX_DELAY ); //wait notification from TXE interrupt
933
+ // transfer buffer to serial line
934
+ HAL_UART_Transmit_IT (modH -> port , modH -> au8Buffer , modH -> u8BufferSize );
935
+ ulTaskNotifyTake (pdTRUE , portMAX_DELAY ); //wait notification from TXE interrupt
936
+
937
+
938
+ if (modH -> EN_Port != NULL )
939
+ {
940
+ // must wait transmission end before changing pin state
941
+ //return RS485 transceiver to receive mode
942
+
943
+ #if defined(STM32H745xx ) || defined(STM32H743xx ) || defined(STM32F303xE )
944
+ while ((modH -> port -> Instance -> ISR & USART_ISR_TC ) == 0 )
945
+ #else
946
+ while ((modH -> port -> Instance -> SR & USART_SR_TC ) == 0 )
947
+ #endif
948
+ {
949
+ taskYIELD ();
950
+ }
951
+ HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_RESET );
952
+ }
953
+
954
+
955
+ // set timeout for master query
956
+ if (modH -> uiModbusType == MASTER_RTU )
957
+ {
958
+ xTimerReset (modH -> xTimerTimeout ,0 );
959
+ }
960
+ #if ENABLE_USB_CDC == 1
961
+ }
862
962
963
+ else if (modH -> u8TypeHW == USB_CDC_HW )
964
+ {
965
+ CDC_Transmit_FS (modH -> au8Buffer , modH -> u8BufferSize );
966
+ // set timeout for master query
967
+ if (modH -> uiModbusType == MASTER_RTU )
968
+ {
969
+ xTimerReset (modH -> xTimerTimeout ,0 );
970
+ }
863
971
864
- if (modH -> EN_Port != NULL )
865
- {
866
- // must wait transmission end before changing pin state
867
- //return RS485 transceiver to receive mode
972
+ }
973
+ #endif
868
974
869
- #if defined(STM32H745xx ) || defined(STM32H743xx ) || defined(STM32F303xE )
870
- while ((modH -> port -> Instance -> ISR & USART_ISR_TC ) == 0 )
871
- #else
872
- while ((modH -> port -> Instance -> SR & USART_SR_TC ) == 0 )
873
- #endif
874
- {
875
- taskYIELD ();
876
- }
877
- HAL_GPIO_WritePin (modH -> EN_Port , modH -> EN_Pin , GPIO_PIN_RESET );
878
- }
879
975
880
976
xQueueGenericReset (modH -> QueueModbusHandle , pdFALSE );
881
-
882
977
modH -> u8BufferSize = 0 ;
883
-
884
- // set timeout for master query
885
- if (modH -> uiModbusType == MASTER_RTU )
886
- {
887
- xTimerReset (modH -> xTimerTimeout ,0 );
888
- }
889
-
890
978
// increase message counter
891
979
modH -> u16OutCnt ++ ;
892
980
0 commit comments