Changeset 23 for trunk/fw_g473rct/SES/src/modbus.c
- Timestamp:
- Aug 29, 2025, 7:18:36 PM (15 hours ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/fw_g473rct/SES/src/modbus.c
r22 r23 67 67 #define FAST_BAUDRATE_INTERFRAME_DELAY_us (1750UL) 68 68 // --- Externe Variablen -------------------------------------------- 69 extern volatilemodbus_t modbusData;70 extern volatilesys_data_t sys_data;69 extern modbus_t modbusData; 70 extern sys_data_t sys_data; 71 71 72 72 … … 109 109 if (stopBits < 1U || stopBits > 2U) stopBits = 1U; 110 110 111 // Berechne Stop Bits 112 /*if ((parityMode== MODBUS_UART_PARITY_EVEN) || (parityMode == MODBUS_UART_PARITY_ODD)) 113 { 114 stopBits = 1; 115 } 116 else 117 { 118 stopBits = 2; 119 }*/ 120 111 121 112 // Berechne Anzahl der Bits per Char 122 113 numberOfBitsPerChar = NUMBER_OF_STARTBITS + NUMBER_OF_DATABITS + stopBits; … … 157 148 mb_data->uart = usart; 158 149 159 // Init aus Cube160 mb_data->uart->Instance = USART2;161 mb_data->uart->Init.BaudRate = baudrate;162 mb_data->uart->Init.WordLength = UART_WORDLENGTH_9B;163 mb_data->uart->Init.StopBits = UART_STOPBITS_1;164 mb_data->uart->Init.Parity = UART_PARITY_EVEN;165 mb_data->uart->Init.Mode = UART_MODE_TX_RX;166 mb_data->uart->Init.HwFlowCtl = UART_HWCONTROL_NONE;167 mb_data->uart->Init.OverSampling = UART_OVERSAMPLING_16;168 mb_data->uart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;169 mb_data->uart->Init.ClockPrescaler = UART_PRESCALER_DIV1;170 mb_data->uart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_SWAP_INIT;171 mb_data->uart->AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE;172 173 // Init änderungen174 175 150 // Baudrate 176 151 mb_data->uart->Init.BaudRate = baudrate; 152 177 153 // Parity Mode // Word length 178 154 if(parityMode == MODBUS_UART_PARITY_EVEN) … … 191 167 mb_data->uart->Init.WordLength = UART_WORDLENGTH_8B; 192 168 } 169 193 170 // Stopbits 194 171 if (stopBits == 1) … … 200 177 mb_data->uart->Init.StopBits = UART_STOPBITS_2; 201 178 } 202 // Init 203 // if (RS485_ModbusEx_Init(mb_data->uart, UART_DE_POLARITY_HIGH, 0, 0,TIMEOUT_FRAME_COMPLETE*nrOfBitsPerChar) != HAL_OK) 204 // { 205 // Error_Handler(); 206 // } 207 // if (HAL_UARTEx_DisableFifoMode(mb_data->uart) != HAL_OK) 208 // { 209 // Error_Handler(); 210 // } 179 180 if (HAL_UART_Init(mb_data->uart) != HAL_OK) 181 { 182 Error_Handler(); 183 } 184 211 185 uint32_t fixedDelayInBitDurations = (FAST_BAUDRATE_INTERFRAME_DELAY_us * baudrate) / 1000000UL + 1UL; 212 186 // HAL_UART_EnableReceiverTimeout( usart); … … 226 200 void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 227 201 { 202 modbusData.mb_rx_frame_complete = 1; 203 modbusData.setRxLed = false; 204 modbusData.rx_head= 0; 205 206 228 207 if (huart->ErrorCode == HAL_UART_ERROR_RTO) 229 208 { 230 modbusData.mb_rx_frame_complete = 1;231 modbusData.setRxLed = true;232 modbusData.rx_head= 8;233 209 printf("MB RTO Event! \n\r"); 234 210 } 211 if (huart->ErrorCode == HAL_UART_ERROR_FE) 212 { 213 printf("MB FE Error! \n\r"); 214 } 215 216 if (huart->ErrorCode == HAL_UART_ERROR_PE) 217 { 218 printf("MB PE Error! \n\r"); 219 } 220 221 if (huart->ErrorCode == HAL_UART_ERROR_NE) 222 { 223 printf("MB NE Error! \n\r"); 224 } 225 226 if (huart->ErrorCode == HAL_UART_ERROR_DMA) 227 { 228 printf("MB DMA Error! \n\r"); 229 } 230 231 if (huart->ErrorCode == HAL_UART_ERROR_DMA) 232 { 233 printf("MB DMA Error! \n\r"); 234 } 235 236 if (huart->ErrorCode == HAL_UART_ERROR_ORE) 237 { 238 printf("MB ORE Error! \n\r"); 239 } 240 241 242 if(HAL_UARTEx_ReceiveToIdle_DMA(huart, huart->pRxBuffPtr, RXBUFFERSIZE) != HAL_OK) 243 { 244 printf("Uart Error bei neustart nach Fehler \n\r"); 245 // while(1) 246 // { 247 // } 248 } 249 250 } 251 252 void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) 253 { 254 //printf("MB rxEvent!RX=%d \n\r",Size); 255 modbusData.setRxLed = true; 256 257 modbusData.mb_rx_frame_complete = 1; 258 modbusData.rx_head= Size +1; 235 259 236 260 if(HAL_UARTEx_ReceiveToIdle_DMA(huart, huart->pRxBuffPtr, RXBUFFERSIZE) != HAL_OK) … … 242 266 } 243 267 268 244 269 } 245 270 246 void HAL_UART Ex_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)271 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 247 272 { 248 printf("MB rxEvent!RX=%d \n\r",Size); 249 modbusData.setRxLed = true; 250 251 modbusData.mb_rx_frame_complete = 1; 252 modbusData.rx_head= Size +1; 253 254 if(HAL_UARTEx_ReceiveToIdle_DMA(huart, huart->pRxBuffPtr, RXBUFFERSIZE) != HAL_OK) 255 { 256 printf("uart error \n\r"); 257 // while(1) 258 // { 259 // } 260 } 261 273 //printf("uart complete \n\r"); 274 modbusData.current_query = MB_QUERY_NOTHING; 262 275 263 276 } … … 286 299 { 287 300 mb_data->current_query = MB_QUERY_SEND_DATA; 288 HAL_UART_Transmit_ IT(mb_data->uart, mb_data->tx_buffer, mb_data->tx_head);301 HAL_UART_Transmit_DMA(mb_data->uart, mb_data->tx_buffer, mb_data->tx_head); 289 302 } 290 303
Note: See TracChangeset
for help on using the changeset viewer.