Skip to main content
JKong.3
Associate III
July 6, 2023
Solved

sending signal (UART) while running MotorControl

  • July 6, 2023
  • 7 replies
  • 4545 views

Hi,

I'm currently working on sending signals through FDCAN or UART of B-G431B-ESC1 while driving a motor.

I have written a debug code which sends UART data when a button is pressed.

However, the function is called once and never called again, nor the motor won't start.

"stm32g4xx_mc_it"

uint8_t mc_it_debug[64];

void EXTI15_10_IRQHandler (void)
{
/* USER CODE BEGIN START_STOP_BTN */
  sprintf(mc_it_debug, "StartStop Button Pressed\r\n");
  HAL_UART_Transmit_DMA(&huart2, mc_it_debug, sizeof(mc_it_debug));
  if (  0U == LL_EXTI_ReadFlag_0_31(LL_EXTI_LINE_10) )
  {
    /* Nothing to do */
  }
  else
  {
    LL_EXTI_ClearFlag_0_31 (LL_EXTI_LINE_10);
    ( void )UI_HandleStartStopButton_cb ();
  }
 
}
 
in the main.c file, 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_COMP1_Init();
MX_COMP2_Init();
MX_COMP4_Init();
MX_CORDIC_Init();
MX_DAC3_Init();
MX_OPAMP1_Init();
MX_OPAMP2_Init();
MX_OPAMP3_Init();
MX_TIM1_Init();
MX_MotorControl_Init();
MX_FDCAN1_Init();
MX_USART2_UART_Init();

/* Initialize interrupts */
MX_NVIC_Init();

I am assuming it is a priority issue, which is set as 

JKong3_0-1688626066429.png

Any advice will be helpful.

 

 

 

    This topic has been closed for replies.
    Best answer by LCE

    Search the web for basics about switches and debouncing.

    And not only debouncing:
    If you only check the level of the switch GPIO, think about the time it is actually pressed vs. the few micro-/ milliseconds it actually takes the MCU to perform stuff.
    So before you release the switch, everything might be triggered several times, which might create several problems.

    I would do the following:
    - reduce the ISR to just set a flag and save the time (SysTick or some other timer)
    - block or ignore the button interrupt for some time (check flag and SysTick)

    7 replies

    JKong.3
    JKong.3Author
    Associate III
    July 6, 2023

    Update : HAL_UART_Transmit_IT() works

    I'm guessing there's some conflict btw DMA and MotorControl

     

    JKong.3
    JKong.3Author
    Associate III
    July 6, 2023

    Update : FDCAN also works, so anyone who tries to use FDCAN, 

    my setting is (for 170MHz clock)

    JKong3_0-1688641434230.png

    I don't know what's wrong with DMA really.

    mƎALLEm
    Technical Moderator
    July 6, 2023

    Hello,

    Regarding your FDCAN bitrate config, I see 999999bit/s. So please try to find another values for NSEG1 and NSEG2 to have exactly 1000000bit/s (1Mb/s)

    "To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
    JKong.3
    JKong.3Author
    Associate III
    July 7, 2023

    Ok, will do

    LCE
    Principal II
    July 6, 2023

    I cannot imagine that 1ppm deviation of the FDCAN bitrate is the problem here...

    Do you do any analog debouncing of the button ? If not, do it in the source code, so suppress any button action for some time after each button action.

    JKong.3
    JKong.3Author
    Associate III
    July 7, 2023

    Can you tell me the meaning of debouncing? I'm assuming what you're saying is create another callback for rising-edge interrupt of a button

    UART_Transmit_IT and CANFD works fine, it's just that when I want to check if FDCAN is working by sending UART signals in FDCAN_TxBufferCompleteCallback by 

    sprintf(debug_main, "FDCAN_TxCallback Called\r\n");
    HAL_UART_Transmit_DMA(&huart2, debug_main, sizeof(debug_main));

    DMA doesn't work(meaning it does not send data and the motor doesn't run), but when I change it to HAL_UART_Transmit_IT, it works fine.

    LCE
    LCEBest answer
    Principal II
    July 7, 2023

    Search the web for basics about switches and debouncing.

    And not only debouncing:
    If you only check the level of the switch GPIO, think about the time it is actually pressed vs. the few micro-/ milliseconds it actually takes the MCU to perform stuff.
    So before you release the switch, everything might be triggered several times, which might create several problems.

    I would do the following:
    - reduce the ISR to just set a flag and save the time (SysTick or some other timer)
    - block or ignore the button interrupt for some time (check flag and SysTick)

    JKong.3
    JKong.3Author
    Associate III
    July 7, 2023

    I will research more on that. Thanks for your help.

    JKong.3
    JKong.3Author
    Associate III
    July 17, 2023

    In the software side, it works fine, but when I try to debug using oscilloscope, the waveform won't show.

    In TSK_HighFrequencyTask()  (16 kHz), I downsampled to 8kHz, then stores alphabeta current and voltage (8 bytes per cycle) to a 64 byte buffer then sends a FDCAN signal storing that buffer. (so tx frequency is 1kHz)  

     

    my code :

    // in fdcan.c, 

    /* FDCAN1 init function */
    void MX_FDCAN1_Init(void)
    {
     
      /* USER CODE BEGIN FDCAN1_Init 0 */
     
      /* USER CODE END FDCAN1_Init 0 */
     
      /* USER CODE BEGIN FDCAN1_Init 1 */
     
      /* USER CODE END FDCAN1_Init 1 */
      hfdcan1.Instance = FDCAN1;
      hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1;
      hfdcan1.Init.FrameFormat = FDCAN_FRAME_FD_NO_BRS;
      hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
      hfdcan1.Init.AutoRetransmission = DISABLE;
      hfdcan1.Init.TransmitPause = DISABLE;
      hfdcan1.Init.ProtocolException = DISABLE;
      hfdcan1.Init.NominalPrescaler = 1;
      hfdcan1.Init.NominalSyncJumpWidth = 1;
      hfdcan1.Init.NominalTimeSeg1 = 5;
      hfdcan1.Init.NominalTimeSeg2 = 2;
      hfdcan1.Init.DataPrescaler = 1;
      hfdcan1.Init.DataSyncJumpWidth = 1;
      hfdcan1.Init.DataTimeSeg1 = 1;
      hfdcan1.Init.DataTimeSeg2 = 1;
      hfdcan1.Init.StdFiltersNbr = 0;
      hfdcan1.Init.ExtFiltersNbr = 0;
      hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
      if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
      {
        Error_Handler();
      }
      /* USER CODE BEGIN FDCAN1_Init 2 */
      /* configure reception filter to Rx buffer 0*/
     
      sFilterConfig.IdType = FDCAN_STANDARD_ID;
      sFilterConfig.FilterIndex = 0;
      sFilterConfig.FilterType = FDCAN_FILTER_MASK;
      sFilterConfig.FilterID1 = 0x11;
      sFilterConfig.FilterID2 = 0x11;
      sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
     
      if(HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig)!= HAL_OK)
      {
      Error_Handler();
      }
     
      /* configure Tx buffer message */
     
      // Configure Tx Header
      TxHeader.Identifier = 0x11;
      TxHeader.IdType = FDCAN_STANDARD_ID;
      TxHeader.TxFrameType = FDCAN_DATA_FRAME;
      TxHeader.DataLength = FDCAN_DLC_BYTES_64;
      TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
      TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
      TxHeader.FDFormat = FDCAN_FD_CAN;
      TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
      TxHeader.MessageMarker = 0;
     
      /* Start FDCAN module */
      if(HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
      {
      Error_Handler();
      }
      /* USER CODE END FDCAN1_Init 2 */
     
    }

     

    // in mc_tasks.c, TSK_HighFrequencyTask()

      /* USER CODE BEGIN HighFrequencyTask 1 */
    (void)TSK_StoreToBuffer(TxData, buffer_index);
    /* USER CODE END HighFrequencyTask 1 */

    void TSK_StoreToBuffer(uint8_t * const buffer, uint8_t idx)
    {
    if (buffer_flag == BUFFER_READY)
    {
    // alphabeta_t data1, data2;
     
    // data1 = FOCVars[M1].Ialphabeta;
    // data2 = FOCVars[M1].Valphabeta;
    //
    // buffer[idx] = (uint8_t)(data1.alpha & 0xff);
    // buffer[idx+1] = (uint8_t)((data1.alpha)>>8 & 0xff);
    //
    // buffer[idx + 16] = (uint8_t)(data1.beta & 0xff);
    // buffer[idx + 17] = (uint8_t)((data1.beta)>>8 & 0xff);
    //
    // buffer[idx + 32] = (uint8_t)(data2.alpha & 0xff);
    // buffer[idx + 33] = (uint8_t)((data2.alpha)>>8 & 0xff);
    //
    // buffer[idx + 48] = (uint8_t)(data2.beta & 0xff);
    // buffer[idx + 49] = (uint8_t)((data2.beta)>>8 & 0xff);
     
    buffer[idx] = (uint8_t)(0x31);
    buffer[idx+1] = (uint8_t)(0x32);
     
    buffer[idx + 16] = (uint8_t)(0x33);
    buffer[idx + 17] = (uint8_t)(0x34);
     
    buffer[idx + 32] = (uint8_t)(0x35);
    buffer[idx + 33] = (uint8_t)(0x36);
     
    buffer[idx + 48] = (uint8_t)(0x37);
    buffer[idx + 49] = (uint8_t)(0x38);
     
     
     
    idx += 2;
     
    buffer_flag = BUFFER_BUSY;
    }
    else
    {
    if (idx == 16) // transmit message
    {
    //HAL_UART_Transmit_IT(&huart2, buffer, sizeof(buffer));
    HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, buffer);
     
          idx = 0;
    }
    buffer_flag++; // BUFFER_READY = (uint8_t) 1, BUFFER_BUSY = (uint8_t) 0
    }
    }
     
    I also set CAN_TERM pin to high
     
    JKong3_1-1689565797262.png

    Any thoughts on what am I missing?


     

     

     

     
    JKong.3
    JKong.3Author
    Associate III
    July 17, 2023

    Sorry, it was a programming issue.

    Piranha
    Principal III
    July 19, 2023

    So why the wrong answer was selected as a solution?

    JKong.3
    JKong.3Author
    Associate III
    July 20, 2023

    Sorry, got mixed up with my main goal (FDCAN + motorcontrol) with this topic (UART_DMA + GPIO interrupt(button))

    I didn't mention that at first I sent FDCAN messages when button is pressed, then the FDCAN RX interrupt will send UART_DMA message. Which didn't work (also motorcontrol stopped operating), so I decided to send UART DMA message directly when button is pressed, which is the question stated here. 

    So can you edit this topic as not solved?

    LCE
    Principal II
    July 17, 2023

    Okay...

    Did the debouncing do the trick for the first basic problem?

    JKong.3
    JKong.3Author
    Associate III
    July 18, 2023

    I couldn't due to my lack of programming skills. I will keep in mind when I start another project using UART.