Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
HardwareSetup.cpp
- Committer:
- elijahsj
- Date:
- 2020-08-26
- Revision:
- 3:2f46953e7c8b
- Parent:
- 2:30503be9a375
- Child:
- 5:d2dffc88e94d
File content as of revision 3:2f46953e7c8b:
#include "mbed.h" #include "HardwareSetup.h" #include "stm32h7xx_hal.h" TIM_HandleTypeDef htim12; TIM_HandleTypeDef htim15; TIM_HandleTypeDef htim13; TIM_HandleTypeDef htim14; TIM_HandleTypeDef htim16; TIM_HandleTypeDef htim17; int PWM_PERIOD; void SystemClock_Config(void); void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); static void MX_TIM12_Init(void); static void MX_TIM13_Init(void); static void MX_TIM14_Init(void); static void MX_TIM15_Init(void); static void MX_TIM16_Init(void); static void MX_TIM17_Init(void); void initHardware(int periodTicks){ PWM_PERIOD = periodTicks; /* Initialise the HAL Layer */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_TIM12_Init(); MX_TIM13_Init(); MX_TIM14_Init(); MX_TIM15_Init(); MX_TIM16_Init(); MX_TIM17_Init(); //HAL_TIM_Base_Start(&htim15); HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1); // start pwm generation HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_2); // start pwm generation HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1); // start pwm generation HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); // start pwm generation HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1); // start pwm generation HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_2); // start pwm generation HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); // start pwm generation HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1); // start pwm generation } static void MX_TIM12_Init(void) { __HAL_RCC_TIM12_CLK_ENABLE(); TIM_OC_InitTypeDef sConfigOC = {0}; htim12.Instance = TIM12; htim12.Init.Prescaler = 0; htim12.Init.CounterMode = TIM_COUNTERMODE_UP; htim12.Init.Period = PWM_PERIOD; htim12.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim12.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_PWM_Init(&htim12) != HAL_OK) { //Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { //Error_Handler(); } if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) { //Error_Handler(); } HAL_TIM_MspPostInit(&htim12); } static void MX_TIM13_Init(void) { __HAL_RCC_TIM13_CLK_ENABLE(); TIM_OC_InitTypeDef sConfigOC = {0}; htim13.Instance = TIM13; htim13.Init.Prescaler = 0; htim13.Init.CounterMode = TIM_COUNTERMODE_UP; htim13.Init.Period = PWM_PERIOD; htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim13.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim13) != HAL_OK) { //Error_Handler(); } if (HAL_TIM_PWM_Init(&htim13) != HAL_OK) { //Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim13, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { //Error_Handler(); } HAL_TIM_MspPostInit(&htim13); } static void MX_TIM14_Init(void) { __HAL_RCC_TIM14_CLK_ENABLE(); TIM_OC_InitTypeDef sConfigOC = {0}; htim14.Instance = TIM14; htim14.Init.Prescaler = 0; htim14.Init.CounterMode = TIM_COUNTERMODE_UP; htim14.Init.Period = PWM_PERIOD; htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim14) != HAL_OK) { //Error_Handler(); } if (HAL_TIM_PWM_Init(&htim14) != HAL_OK) { //Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { //Error_Handler(); } HAL_TIM_MspPostInit(&htim14); } static void MX_TIM15_Init(void) { __HAL_RCC_TIM15_CLK_ENABLE(); TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; htim15.Instance = TIM15; htim15.Init.Prescaler = 0; htim15.Init.CounterMode = TIM_COUNTERMODE_UP; htim15.Init.Period = PWM_PERIOD; htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim15.Init.RepetitionCounter = 0; htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_PWM_Init(&htim15) != HAL_OK) { //Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK) { //Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; 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(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { //Error_Handler(); } if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != 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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; if (HAL_TIMEx_ConfigBreakDeadTime(&htim15, &sBreakDeadTimeConfig) != HAL_OK) { //Error_Handler(); } HAL_TIM_MspPostInit(&htim15); } static void MX_TIM16_Init(void) { __HAL_RCC_TIM16_CLK_ENABLE(); TIM_OC_InitTypeDef sConfigOC = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; htim16.Instance = TIM16; htim16.Init.Prescaler = 0; htim16.Init.CounterMode = TIM_COUNTERMODE_UP; htim16.Init.Period = PWM_PERIOD; htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim16.Init.RepetitionCounter = 0; htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim16) != HAL_OK) { //Error_Handler(); } if (HAL_TIM_PWM_Init(&htim16) != HAL_OK) { //Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; 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(&htim16, &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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; if (HAL_TIMEx_ConfigBreakDeadTime(&htim16, &sBreakDeadTimeConfig) != HAL_OK) { //Error_Handler(); } HAL_TIM_MspPostInit(&htim16); } static void MX_TIM17_Init(void) { __HAL_RCC_TIM17_CLK_ENABLE(); TIM_OC_InitTypeDef sConfigOC = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; htim17.Instance = TIM17; htim17.Init.Prescaler = 0; htim17.Init.CounterMode = TIM_COUNTERMODE_UP; htim17.Init.Period = PWM_PERIOD; htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim17.Init.RepetitionCounter = 0; htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim17) != HAL_OK) { //Error_Handler(); } if (HAL_TIM_PWM_Init(&htim17) != HAL_OK) { //Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; 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(&htim17, &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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK) { //Error_Handler(); } HAL_TIM_MspPostInit(&htim17); } void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) { /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct = {0}; if(htim->Instance==TIM12) { /**TIM12 GPIO Configuration PB14 ------> TIM12_CH1 PB15 ------> TIM12_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM12; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } else if(htim->Instance==TIM13) { /**TIM13 GPIO Configuration PA6 ------> TIM13_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM13; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if(htim->Instance==TIM14) { /**TIM14 GPIO Configuration PF9 ------> TIM14_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM14; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); } else if(htim->Instance==TIM15) { /**TIM15 GPIO Configuration PE5 ------> TIM15_CH1 PE6 ------> TIM15_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF4_TIM15; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); } else if(htim->Instance==TIM16) { /**TIM16 GPIO Configuration PF6 ------> TIM16_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM16; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); } else if(htim->Instance==TIM17) { /**TIM17 GPIO Configuration PF7 ------> TIM17_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM17; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); } } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; /** Supply configuration update enable */ HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY); /** Configure the main internal regulator output voltage */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} /** Macro to configure the PLL clock source */ __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI); /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_DIV1; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { //Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1; RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { //Error_Handler(); } }