Ignore:
Timestamp:
Aug 29, 2025, 6:15:42 PM (24 hours ago)
Author:
f.jahn
Message:

DMA Rx funktioniert, aber BETA und nur feste Baudrate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/fw_g473rct/SES/src/modbus.c

    r20 r22  
    6767  #define FAST_BAUDRATE_INTERFRAME_DELAY_us   (1750UL)
    6868  // --- Externe Variablen --------------------------------------------
    69   extern modbus_t modbusData;
    70   extern sys_data_t sys_data;
     69  extern volatile modbus_t modbusData;
     70  extern volatile sys_data_t sys_data;
    7171
    7272
     
    158158   
    159159    // Init aus Cube
    160     mb_data->uart->Instance = USART1;
    161     mb_data->uart->Init.BaudRate = 19200;
     160    mb_data->uart->Instance = USART2;
     161    mb_data->uart->Init.BaudRate = baudrate;
    162162    mb_data->uart->Init.WordLength = UART_WORDLENGTH_9B;
    163163    mb_data->uart->Init.StopBits = UART_STOPBITS_1;
     
    168168    mb_data->uart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    169169    mb_data->uart->Init.ClockPrescaler = UART_PRESCALER_DIV1;
    170     mb_data->uart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    171    
    172     // Init nderungen
     170    mb_data->uart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_SWAP_INIT;
     171        mb_data->uart->AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE;
     172   
     173    // Init änderungen
    173174
    174175    // Baudrate
     
    200201    }
    201202    // Init
    202     if (RS485_ModbusEx_Init(mb_data->uart, UART_DE_POLARITY_HIGH, 0, 0,TIMEOUT_FRAME_COMPLETE*nrOfBitsPerChar) != HAL_OK)
    203     {
    204       Error_Handler();
    205     }
    206     if (HAL_UARTEx_DisableFifoMode(mb_data->uart) != HAL_OK)
    207     {
    208       Error_Handler();
    209     }
    210 
    211     if(HAL_UART_Receive_IT(mb_data->uart, mb_data->rx_buffer, RXBUFFERSIZE) != HAL_OK)
     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   // }
     211        uint32_t fixedDelayInBitDurations = (FAST_BAUDRATE_INTERFRAME_DELAY_us * baudrate) / 1000000UL + 1UL;
     212//      HAL_UART_EnableReceiverTimeout( usart);
     213//      HAL_UART_ReceiverTimeout_Config(usart,  fixedDelayInBitDurations);
     214
     215    if(HAL_UARTEx_ReceiveToIdle_DMA(mb_data->uart, mb_data->rx_buffer, RXBUFFERSIZE) != HAL_OK)
    212216    {
    213217      printf("uart error \n\r");
     
    218222  }
    219223
    220   /*
    221     *
    222     * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
    223     * @param  huart: UART handle.
    224     * @retval None
    225   */
    226   void mbUartEndRxTransfer(UART_HandleTypeDef *huart)
    227   {
    228     /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
    229     //CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
    230     //CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
    231 
    232     /* At end of Rx process, restore huart->RxState to Ready */
    233     huart->RxState = HAL_UART_STATE_READY;
    234   }
    235 
    236   void mbUartRx(modbus_t* mb_data)
    237   {
    238     uint32_t  pos;
    239     uint8_t   data;
    240 
    241     pos = mb_data->rx_head; 
    242    
    243     if (pos >= RXBUFFERSIZE)
    244     {
    245       return;
    246     }
    247     //data = mb_data->uart->Instance->RDR
    248     data = mb_data->uart->Instance->RDR & (uint8_t)0x00FF;  //NOTE: mb_data->uart.Instance->DR gendert
    249     mb_data->rx_buffer[pos] = data;
    250 
    251     mb_data->rx_head++;
    252 
    253     //mbTimerStart(mb_data);
    254   }
    255 
    256   HAL_StatusTypeDef mbUartTx(UART_HandleTypeDef *huart)
    257   {
    258     uint16_t* tmp;
    259    
    260     if(huart->Init.WordLength == UART_WORDLENGTH_9B)
    261     {
    262       tmp                     =   (uint16_t*) huart->pTxBuffPtr;
    263       huart->Instance->TDR    =   (uint16_t)(*tmp & (uint16_t)0x01FF); //NOTE: huart->Instance->DR gendert
    264      
    265       if(huart->Init.Parity == UART_PARITY_NONE)
    266       {
    267         huart->pTxBuffPtr += 2;
    268       }
    269       else
    270       {
    271         huart->pTxBuffPtr += 1;
    272       }
    273     }
    274     else
    275     {
    276       huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);   //NOTE:huart->Instance->DR gendert
    277     }
    278 
    279     if(--huart->TxXferCount == 0)
    280     {
    281       /* Disable the UART Transmit Complete Interrupt */
    282       __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
    283 
    284       /* Enable the UART Transmit Complete Interrupt */   
    285       __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
    286     }
    287     return HAL_OK;
    288   }
    289 
    290   // huart->State gibt es nicht mehr. Ersetzt durch huart->gState.
    291   /**
    292     * @brief  Wraps up transmission in non blocking mode.
    293     * @param  huart: pointer to a UART_HandleTypeDef structure that contains
    294     *                the configuration information for the specified UART module.
    295     * @retval HAL status
    296     */
    297   static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
    298   {
    299     /* Disable the UART Transmit Complete Interrupt */
    300     CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
    301 
    302     /* Tx process is ended, restore huart->gState to Ready */
    303     huart->gState = HAL_UART_STATE_READY;
    304    
    305     /* Cleat TxISR function pointer */
    306     huart->TxISR = NULL;
    307 
    308   #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
    309     /*Call registered Tx complete callback*/
    310     huart->TxCpltCallback(huart);
    311   #else
    312     /*Call legacy weak Tx complete callback*/
    313     HAL_UART_TxCpltCallback(huart);
    314   #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
    315   }
    316 
    317 
    318 static HAL_StatusTypeDef RS485_ModbusEx_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime, uint32_t charReceiveTimeout)
     224
     225
     226void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
    319227{
    320   uint32_t temp;
    321 
    322   /* Check the UART handle allocation */
    323   if (huart == NULL)
    324   {
    325     return HAL_ERROR;
    326   }
    327   /* Check the Driver Enable UART instance */
    328   assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
    329 
    330   /* Check the Driver Enable polarity */
    331   assert_param(IS_UART_DE_POLARITY(Polarity));
    332 
    333   /* Check the Driver Enable assertion time */
    334   assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
    335 
    336   /* Check the Driver Enable deassertion time */
    337   assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
    338 
    339   if (huart->gState == HAL_UART_STATE_RESET)
    340   {
    341     /* Allocate lock resource and initialize it */
    342     huart->Lock = HAL_UNLOCKED;
    343 
    344 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
    345     UART_InitCallbacksToDefault(huart);
    346 
    347     if (huart->MspInitCallback == NULL)
    348     {
    349       huart->MspInitCallback = HAL_UART_MspInit;
    350     }
    351 
    352     /* Init the low level hardware */
    353     huart->MspInitCallback(huart);
    354 #else
    355     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
    356     HAL_UART_MspInit(huart);
    357 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
    358   }
    359 
    360   huart->gState = HAL_UART_STATE_BUSY;
    361 
    362   /* Disable the Peripheral */
    363   __HAL_UART_DISABLE(huart);
    364 
    365   /* Set the UART Communication parameters */
    366   if (UART_SetConfig(huart) == HAL_ERROR)
    367   {
    368     return HAL_ERROR;
    369   }
    370 
    371   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
    372   {
    373     UART_AdvFeatureConfig(huart);
    374   }
    375 
    376   /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
    377   //EDIT SMART_SWITCH: HIER wurde ein chip mit autoerkennung implementiert
    378  // SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
    379 
    380   /* Set the Driver Enable polarity */
    381  // MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
    382 
    383   /* Set the Driver Enable assertion and deassertion times */
    384 //  temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
    385 //  temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
    386 //  MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
    387 
    388 // EDIT ECS START
    389   /*Set receive timeout time*/
    390   SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
    391   SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE); 
    392   //MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, charReceiveTimeout);
    393   if (huart->Init.BaudRate <= 19200) MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, charReceiveTimeout);
    394   else
    395   {
    396       uint32_t fixedDelayInBitDurations = (FAST_BAUDRATE_INTERFRAME_DELAY_us * huart->Init.BaudRate) / 1000000UL + 1UL;
    397       MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, fixedDelayInBitDurations);
    398   }
    399 // EDIT ECS END
    400 
    401   /* Enable the Peripheral */
    402   __HAL_UART_ENABLE(huart);
    403 
    404   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
    405   return (UART_CheckIdleState(huart));
    406 }
    407 
    408 
    409 static int test;
    410 void MODBUS_UART_IRQHandler(UART_HandleTypeDef *huart)
    411 {
    412   uint32_t isrflags   = READ_REG(huart->Instance->ISR);
    413   uint32_t cr1its     = READ_REG(huart->Instance->CR1);
    414   uint32_t cr2its     = READ_REG(huart->Instance->CR2);
    415   uint32_t cr3its     = READ_REG(huart->Instance->CR3);
    416 
    417   uint32_t errorflags;
    418   uint32_t errorcode;
    419 
    420   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
    421 
    422   /* If some errors occur */
    423   if ((errorflags != 0U)
    424       && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
    425            || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
    426   {
    427     /* UART parity error interrupt occurred -------------------------------------*/
    428     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
    429     {
    430       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
    431 
    432       huart->ErrorCode |= HAL_UART_ERROR_PE;
    433     }
    434 
    435     /* UART frame error interrupt occurred --------------------------------------*/
    436     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
    437     {
    438       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
    439 
    440       huart->ErrorCode |= HAL_UART_ERROR_FE;
    441     }
    442 
    443     /* UART noise error interrupt occurred --------------------------------------*/
    444     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
    445     {
    446       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
    447 
    448       huart->ErrorCode |= HAL_UART_ERROR_NE;
    449     }
    450 
    451     /* UART Over-Run interrupt occurred -----------------------------------------*/
    452     if (((isrflags & USART_ISR_ORE) != 0U)
    453         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
    454             ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
    455     {
    456       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
    457 
    458       huart->ErrorCode |= HAL_UART_ERROR_ORE;
    459     }
    460   } /* End if some error occurs */
    461 
    462 
    463   /* UART in mode Receiver (receive Timeout occured)--------------------------*/
    464   if (((isrflags & USART_ISR_RTOF) != 0U)
    465       && (((cr1its & USART_CR1_RTOIE) != 0U)
    466           || ((cr3its & USART_CR2_RTOEN) != 0U)))
    467   {
    468     __HAL_UART_CLEAR_FLAG(huart, USART_ICR_RTOCF);
    469     huart->RxState = HAL_UART_STATE_READY;
    470     huart->gState = HAL_UART_STATE_READY;
     228  if (huart->ErrorCode == HAL_UART_ERROR_RTO)
     229  {
    471230    modbusData.mb_rx_frame_complete = 1;
    472231    modbusData.setRxLed = true;
    473 
    474   }
    475 
    476   /* UART in mode Receiver ---------------------------------------------------*/
    477   if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
    478       && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
    479           || ((cr3its & USART_CR3_RXFTIE) != 0U)))
    480   {
    481     if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && (modbusData.rx_head < RXBUFFERSIZE))    //-> empfngt dann ein byte aber das ist nicht wild
    482     {
    483       modbusData.rx_buffer[modbusData.rx_head] = huart->Instance->RDR;
    484       /*DEBUG//printf("xx%d: nr:%d  %d\n",test ++,modbusData.rx_head, modbusData.rx_buffer[modbusData.rx_head]);*/
    485       modbusData.rx_head++;
    486       modbusData.setRxLed = true;
    487     }
    488     else
    489     {
    490       __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
    491     }
    492   }
    493 
    494   /* UART in mode Transmitter ------------------------------------------------*/
    495   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
    496       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
    497           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
    498   {
    499     UART_TxISR_8BIT(modbusData.uart);
    500     modbusData.setTxLed = true;
    501   }
    502 
    503   /* UART in mode Transmitter (transmission end) -----------------------------*/
    504   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
    505   {
    506     modbusData.current_query = MB_QUERY_NOTHING;
    507     UART_EndTransmit_IT(huart);
    508     huart->RxState = HAL_UART_STATE_BUSY_RX;
    509 //    /*Reset TX complete interrupt flag*/
    510 //    __HAL_UART_CLEAR_FLAG(huart, USART_ISR_TC);
    511 //     /* Disable the UART Transmit Complete Interrupt */
    512 //    CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
    513 //    /*TX complete callback function*/
    514 //    HAL_UART_TxCpltCallback(huart);
    515 //    /* Tx process is ended, restore huart->gState to Ready */
    516 //    huart->gState = HAL_UART_STATE_READY;
    517   }
    518 
    519   /* Call UART Error Call back function if need be --------------------------*/
    520   if (huart->ErrorCode != HAL_UART_ERROR_NONE)
    521   {
    522     huart->RxState = HAL_UART_STATE_BUSY_RX;
    523     /* UART in mode Receiver ---------------------------------------------------*/
    524     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
    525         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
    526             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
    527     {
    528       __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
    529       huart->ErrorCode = HAL_UART_ERROR_NONE;
    530     }
    531     else if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
    532              && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
    533                  || ((cr3its & USART_CR3_TXFTIE) != 0U)))
    534     {
    535       modbusData.current_query = MB_QUERY_NOTHING;
    536       UART_EndTransmit_IT(huart);
    537       huart->RxState = HAL_UART_STATE_BUSY_RX;
    538       modbusData.setTxLed = true;
    539     }
    540   }
     232        modbusData.rx_head= 8;
     233        printf("MB RTO Event! \n\r");
     234  }
     235
     236  if(HAL_UARTEx_ReceiveToIdle_DMA(huart, huart->pRxBuffPtr, RXBUFFERSIZE) != HAL_OK)
     237   {
     238     printf("uart error \n\r");
     239  //   while(1)
     240  //   {
     241  //   }     
     242   }   
     243 
    541244}
    542245
    543 
    544 
    545 
    546 
    547 
    548 
    549 /**
    550   * @brief TX interrrupt handler for 7 or 8 bits data word length .
    551   * @note   Function is called under interruption only, once
    552   *         interruptions have been enabled by HAL_UART_Transmit_IT().
    553   * @param huart UART handle.
    554   * @retval None
    555   */
    556 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
     246void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
    557247{
    558   /* Check that a Tx process is ongoing */
    559   if (huart->gState == HAL_UART_STATE_BUSY_TX)
    560   {
    561     if (huart->TxXferCount == 0U)
    562     {
    563       /* Disable the UART Transmit Data Register Empty Interrupt */
    564       CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
    565 
    566       /* Enable the UART Transmit Complete Interrupt */
    567       SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
    568     }
    569     else
    570     {
    571       huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
    572       huart->pTxBuffPtr++;
    573       huart->TxXferCount--;
    574     }
    575   }
    576   else
    577   {
    578       /* Disable the UART Transmit Data Register Empty Interrupt */
    579       CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
    580 
    581       /* Enable the UART Transmit Complete Interrupt */
    582       SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
    583   }
     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 
     262
    584263}
    585264
     
    595274
    596275
    597   /**
    598     * @brief  Output Compare callback in non blocking mode
    599     * @param  htim : TIM OC handle
    600     * @retval None
    601     */
    602   void mbTimerIsr(modbus_t * mb_data)
    603   {
    604     /* Capture compare 1 event */
    605 //    if(__HAL_TIM_GET_FLAG(mb_data->timer, TIM_FLAG_CC1) != RESET)
    606 //    {
    607 //      if(__HAL_TIM_GET_IT_SOURCE(mb_data->timer, TIM_IT_CC1) !=RESET)
    608 //      {
    609 //        __HAL_TIM_CLEAR_IT(mb_data->timer, TIM_IT_CC1);
    610 //        mb_data->timer->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
    611      
    612         mb_data->mb_rx_frame_complete=true;
    613 //        mb_data->timer->Instance->CNT =0;
    614 //        HAL_TIM_OC_Stop_IT(mb_data->timer, TIM_CHANNEL_1);
    615 //        mb_data->timer->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
    616 //
    617 //      }
    618 //    }
    619   }
     276
     277
     278
     279
     280
     281
     282
    620283
    621284
Note: See TracChangeset for help on using the changeset viewer.