Skip to content

Commit 1d98194

Browse files
committed
USB-CDC support added for bluepill
1 parent 3f4db2c commit 1d98194

File tree

136 files changed

+121249
-45
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+121249
-45
lines changed

MODBUS-LIB/Inc/Modbus.h

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "timers.h"
1919

2020

21+
#define ENABLE_USB_CDC 0 //Enable the USB CDC support and examples
22+
2123
#define T35 5
2224
#define MAX_BUFFER 64 //!< maximum size for the communication buffer in bytes
2325
#define TIMEOUT_MODBUS 1000
@@ -43,6 +45,17 @@ typedef struct
4345
}
4446
modbus_t;
4547

48+
49+
enum
50+
{
51+
USART_HW = 1,
52+
#if ENABLE_USB_CDC == 1
53+
USB_CDC_HW = 2,
54+
#endif
55+
56+
};
57+
58+
4659
/**
4760
* @struct modbusHandler_t
4861
* @brief
@@ -83,6 +96,10 @@ typedef struct
8396
xTimerHandle xTimerTimeout;
8497
//Semaphore for Modbus data
8598
osSemaphoreId_t ModBusSphrHandle;
99+
#if ENABLE_USB_CDC == 1
100+
uint8_t u8TypeHW;
101+
//int16_t i16LenRx;
102+
#endif
86103

87104
}
88105
modbusHandler_t;
@@ -190,6 +207,11 @@ modbusHandler_t *mHandlers[MAX_M_HANDLERS];
190207
// Function prototypes
191208
void ModbusInit(modbusHandler_t * modH);
192209
void ModbusStart(modbusHandler_t * modH);
210+
211+
#if ENABLE_USB_CDC == 1
212+
void ModbusStartCDC(modbusHandler_t * modH);
213+
#endif
214+
193215
void setTimeOut( uint16_t u16timeOut); //!<write communication watch-dog timer
194216
uint16_t getTimeOut(); //!<get communication watch-dog timer value
195217
bool getTimeOutState(); //!<get communication watch-dog timer state

MODBUS-LIB/Src/Modbus.c

+127-39
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#define highByte(w) ((w) >> 8)
2525

2626

27+
28+
2729
///Queue Modbus RX
2830
//osMessageQueueId_t QueueModbusHandle;
2931
const osMessageQueueAttr_t QueueModbus_attributes = {
@@ -180,7 +182,8 @@ void ModbusInit(modbusHandler_t * modH)
180182
*/
181183
void ModbusStart(modbusHandler_t * modH)
182184
{
183-
if (modH->EN_Port != NULL )
185+
186+
if (modH->EN_Port != NULL )
184187
{
185188
// return RS485 transceiver to transmit mode
186189
HAL_GPIO_WritePin(modH->EN_Port, modH->EN_Pin, GPIO_PIN_RESET);
@@ -205,7 +208,29 @@ void ModbusStart(modbusHandler_t * modH)
205208

206209
modH->u8lastRec = modH->u8BufferSize = 0;
207210
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;
208232
}
233+
#endif
209234

210235

211236
void vTimerCallbackT35(TimerHandle_t *pxTimer)
@@ -248,23 +273,39 @@ void StartTaskModbusSlave(void *argument)
248273
{
249274

250275
modbusHandler_t *modH = (modbusHandler_t *)argument;
276+
int8_t i8state;
251277

252278
for(;;)
253279
{
254280
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); /* Block indefinitely until a Modbus Frame arrives */
255281

256282
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+
257295
modH->u8BufferSize = uxQueueMessagesWaiting(modH->QueueModbusHandle);
258296
if (modH->EN_Port != NULL )
259297
{
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?
261299
}
300+
i8state = getRxBuffer(modH);
301+
302+
#endif
262303

263-
int8_t i8state = getRxBuffer(modH);
264304

265305
if (i8state < 7){
266306
//The size of the frame is invalid
267-
modH->i8lastError = ERR_BAD_SIZE;;
307+
modH->i8lastError = ERR_BAD_SIZE;
308+
modH->u16errCnt++;
268309
xQueueGenericReset(modH->QueueModbusHandle, pdFALSE);
269310
continue;
270311
}
@@ -333,8 +374,14 @@ void StartTaskModbusSlave(void *argument)
333374
void ModbusQuery(modbusHandler_t * modH, modbus_t telegram )
334375
{
335376
//Add the telegram to the TX tail Queue of Modbus
377+
if (modH->uiModbusType == MASTER_RTU)
378+
{
336379
telegram.u32CurrentTask = (uint32_t *) osThreadGetId();
337380
xQueueSendToBack(modH->QueueTelegramHandle, &telegram, 0);
381+
}
382+
else{
383+
while(1);// error a master slave cannot send queries
384+
}
338385
}
339386

340387

@@ -472,6 +519,7 @@ void StartTaskModbusMaster(void *argument)
472519
modbusHandler_t *modH = (modbusHandler_t *)argument;
473520
uint32_t ulNotificationValue;
474521
modbus_t telegram;
522+
int8_t i8state;
475523

476524
for(;;)
477525
{
@@ -484,6 +532,7 @@ void StartTaskModbusMaster(void *argument)
484532
/* Block indefinitely until a Modbus Frame arrives or query timeouts*/
485533
ulNotificationValue = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
486534
modH->i8lastError = 0;
535+
487536
if(ulNotificationValue == NO_REPLY)
488537
{
489538
modH->i8state = COM_IDLE;
@@ -493,9 +542,30 @@ void StartTaskModbusMaster(void *argument)
493542
continue;
494543
}
495544

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
497568

498-
int8_t i8state = getRxBuffer(modH);
499569
//modH->u8lastError = i8state;
500570

501571
if (i8state < 6){
@@ -827,6 +897,10 @@ void buildException( uint8_t u8exception, modbusHandler_t *modH )
827897
}
828898

829899

900+
#if ENABLE_USB_CDC == 1
901+
extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
902+
#endif
903+
830904
/**
831905
* @brief
832906
* This method transmits au8Buffer to Serial line.
@@ -846,47 +920,61 @@ void sendTxBuffer(modbusHandler_t *modH)
846920
modH->u8BufferSize++;
847921
modH->au8Buffer[ modH->u8BufferSize ] = u16crc & 0x00ff;
848922
modH->u8BufferSize++;
849-
850-
if (modH->EN_Port != NULL)
923+
#if ENABLE_USB_CDC ==1
924+
if(modH->u8TypeHW == USART_HW)
851925
{
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+
}
860932

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+
}
862962

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+
}
863971

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
868974

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-
}
879975

880976
xQueueGenericReset(modH->QueueModbusHandle, pdFALSE);
881-
882977
modH->u8BufferSize = 0;
883-
884-
// set timeout for master query
885-
if(modH->uiModbusType == MASTER_RTU )
886-
{
887-
xTimerReset(modH->xTimerTimeout,0);
888-
}
889-
890978
// increase message counter
891979
modH->u16OutCnt++;
892980

0 commit comments

Comments
 (0)