59 views
in System Integration by
I just configured a pin on the STM32F746G Discovery board as a PWM using the CubeMx. Generated code by selecting the CubeIDE environment. Using Cube IDE went into the main.c and  initialized the timer instance and loaded a value in the timer control register in the main.c file.

Now I want to pass a value from EmbededWizard into my native code to change the value of the PWM counter threshold by modifying the defined variable dutyCycle  The native code section looks like this: htim1.Instance->CCR1 = dutyCycle;

 Should I take the code that initialized the PWM and Timer from  main.c and put it in the driver structure DeviceDriver.c under ,,/Application/Source? Then I can try to follow the tutorials "Interfacing with a Device"

 

See snippet of code in the main.c that was generated and modified in CubeMx and CubeIDE.

 

TIM_HandleTypeDef htim1;

:

MX_TIM1_Init();

:

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);

:

htim1.Instance->CCR1 = dutyCycle;

 

 

/**
  * @brief TIM1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM1_Init(void)
{

  /* USER CODE BEGIN TIM1_Init 0 */

  /* USER CODE END TIM1_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

  /* USER CODE BEGIN TIM1_Init 1 */

  /* USER CODE END TIM1_Init 1 */
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 200;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 100;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 10;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.BreakFilter = 0;
  sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  sBreakDeadTimeConfig.Break2Filter = 0;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM1_Init 2 */

  /* USER CODE END TIM1_Init 2 */
  HAL_TIM_MspPostInit(&htim1);

}

1 Answer

0 votes
by

Hi there,

basically this a question more related to the use of STM32CubeMX itself instead of Embedded Wizard, and currently we do not officially support this tool. However, we have already took a look onto it and did a small proof of concept for an STM32F746G Discovery.

You can find it in this thread.

I hope this helps anyways,

best regards,

Tim

by

Hi Tim , Thanks for the response.

 I went to the thread you sent me  Link In here, but  I could not find the example that referenced the API ‘ew_bsp_serial.c/.h’ which should have been part of my target specific (BSP) code. See my "TargetSpecific" directory below. Also, I could not find any Embedded Wizard generated target specific API for PWM or ADC  for the target specific (BSP) code. If I want to use the PWM, I need to generate some code that Embedded Wizard has not included in the Target Specific directory for the BSP. There must be a way to use my own MiddleWare and I am having difficulty finding this information of how to include my generated code in Embedded Wizard. I have looked in the example "DeviceIntegration.ewp" but this just turns and LED on/off and reads a switch. I could not see any API to control the PWM nor read an ADC value. What am I missing?

Embedded Wizard Website | Privacy Policy | Imprint

...