Updated for the next revision of the motor board
HardwareSetup.cpp
- Committer:
- elijahsj
- Date:
- 2020-08-26
- Revision:
- 5:d2dffc88e94d
- Parent:
- 3:2f46953e7c8b
- Child:
- 6:417655779dc5
File content as of revision 5:d2dffc88e94d:
#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 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(); /* 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); } }