Updated for the next revision of the motor board

Committer:
elijahsj
Date:
Tue Aug 25 23:38:26 2020 +0000
Revision:
1:4c3c2b7337a6
Child:
2:30503be9a375
Fixed HAL libraries;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elijahsj 1:4c3c2b7337a6 1 #include "mbed.h"
elijahsj 1:4c3c2b7337a6 2 #include "HardwareSetup.h"
elijahsj 1:4c3c2b7337a6 3 #include "stm32h7xx_hal.h"
elijahsj 1:4c3c2b7337a6 4
elijahsj 1:4c3c2b7337a6 5 TIM_HandleTypeDef htim12;
elijahsj 1:4c3c2b7337a6 6 TIM_HandleTypeDef htim15;
elijahsj 1:4c3c2b7337a6 7 TIM_HandleTypeDef htim13;
elijahsj 1:4c3c2b7337a6 8 TIM_HandleTypeDef htim14;
elijahsj 1:4c3c2b7337a6 9 TIM_HandleTypeDef htim16;
elijahsj 1:4c3c2b7337a6 10 TIM_HandleTypeDef htim17;
elijahsj 1:4c3c2b7337a6 11
elijahsj 1:4c3c2b7337a6 12 void SystemClock_Config(void);
elijahsj 1:4c3c2b7337a6 13 void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);
elijahsj 1:4c3c2b7337a6 14 static void MX_TIM12_Init(void);
elijahsj 1:4c3c2b7337a6 15
elijahsj 1:4c3c2b7337a6 16 void initHardware(){
elijahsj 1:4c3c2b7337a6 17 /* Initialise the HAL Layer */
elijahsj 1:4c3c2b7337a6 18 HAL_Init();
elijahsj 1:4c3c2b7337a6 19
elijahsj 1:4c3c2b7337a6 20 /* Configure the system clock */
elijahsj 1:4c3c2b7337a6 21 SystemClock_Config();
elijahsj 1:4c3c2b7337a6 22
elijahsj 1:4c3c2b7337a6 23 /* Initialize all configured peripherals */
elijahsj 1:4c3c2b7337a6 24 MX_TIM12_Init();
elijahsj 1:4c3c2b7337a6 25
elijahsj 1:4c3c2b7337a6 26 HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1); // start pwm generation
elijahsj 1:4c3c2b7337a6 27 HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_2); // start pwm generation
elijahsj 1:4c3c2b7337a6 28 }
elijahsj 1:4c3c2b7337a6 29
elijahsj 1:4c3c2b7337a6 30 static void MX_TIM12_Init(void)
elijahsj 1:4c3c2b7337a6 31 {
elijahsj 1:4c3c2b7337a6 32
elijahsj 1:4c3c2b7337a6 33 /* USER CODE BEGIN TIM12_Init 0 */
elijahsj 1:4c3c2b7337a6 34
elijahsj 1:4c3c2b7337a6 35 /* USER CODE END TIM12_Init 0 */
elijahsj 1:4c3c2b7337a6 36
elijahsj 1:4c3c2b7337a6 37 __HAL_RCC_TIM12_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 38 TIM_OC_InitTypeDef sConfigOC = {0};
elijahsj 1:4c3c2b7337a6 39
elijahsj 1:4c3c2b7337a6 40 /* USER CODE BEGIN TIM12_Init 1 */
elijahsj 1:4c3c2b7337a6 41
elijahsj 1:4c3c2b7337a6 42 /* USER CODE END TIM12_Init 1 */
elijahsj 1:4c3c2b7337a6 43 htim12.Instance = TIM12;
elijahsj 1:4c3c2b7337a6 44 htim12.Init.Prescaler = 0;
elijahsj 1:4c3c2b7337a6 45 htim12.Init.CounterMode = TIM_COUNTERMODE_UP;
elijahsj 1:4c3c2b7337a6 46 htim12.Init.Period = PWM_PERIOD;
elijahsj 1:4c3c2b7337a6 47 htim12.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
elijahsj 1:4c3c2b7337a6 48 htim12.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
elijahsj 1:4c3c2b7337a6 49 if (HAL_TIM_PWM_Init(&htim12) != HAL_OK)
elijahsj 1:4c3c2b7337a6 50 {
elijahsj 1:4c3c2b7337a6 51 //Error_Handler();
elijahsj 1:4c3c2b7337a6 52 }
elijahsj 1:4c3c2b7337a6 53 sConfigOC.OCMode = TIM_OCMODE_PWM1;
elijahsj 1:4c3c2b7337a6 54 sConfigOC.Pulse = 0;
elijahsj 1:4c3c2b7337a6 55 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
elijahsj 1:4c3c2b7337a6 56 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
elijahsj 1:4c3c2b7337a6 57 if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
elijahsj 1:4c3c2b7337a6 58 {
elijahsj 1:4c3c2b7337a6 59 //Error_Handler();
elijahsj 1:4c3c2b7337a6 60 }
elijahsj 1:4c3c2b7337a6 61 if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
elijahsj 1:4c3c2b7337a6 62 {
elijahsj 1:4c3c2b7337a6 63 //Error_Handler();
elijahsj 1:4c3c2b7337a6 64 }
elijahsj 1:4c3c2b7337a6 65 /* USER CODE BEGIN TIM12_Init 2 */
elijahsj 1:4c3c2b7337a6 66
elijahsj 1:4c3c2b7337a6 67 /* USER CODE END TIM12_Init 2 */
elijahsj 1:4c3c2b7337a6 68 HAL_TIM_MspPostInit(&htim12);
elijahsj 1:4c3c2b7337a6 69 }
elijahsj 1:4c3c2b7337a6 70
elijahsj 1:4c3c2b7337a6 71 void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
elijahsj 1:4c3c2b7337a6 72 {
elijahsj 1:4c3c2b7337a6 73 /* GPIO Ports Clock Enable */
elijahsj 1:4c3c2b7337a6 74 __HAL_RCC_GPIOE_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 75 __HAL_RCC_GPIOC_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 76 __HAL_RCC_GPIOF_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 77 __HAL_RCC_GPIOH_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 78 __HAL_RCC_GPIOA_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 79 __HAL_RCC_GPIOB_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 80 __HAL_RCC_GPIOD_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 81 __HAL_RCC_GPIOG_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 82
elijahsj 1:4c3c2b7337a6 83 GPIO_InitTypeDef GPIO_InitStruct = {0};
elijahsj 1:4c3c2b7337a6 84 if(htim->Instance==TIM12)
elijahsj 1:4c3c2b7337a6 85 {
elijahsj 1:4c3c2b7337a6 86
elijahsj 1:4c3c2b7337a6 87 /**TIM12 GPIO Configuration
elijahsj 1:4c3c2b7337a6 88 PB14 ------> TIM12_CH1
elijahsj 1:4c3c2b7337a6 89 PB15 ------> TIM12_CH2
elijahsj 1:4c3c2b7337a6 90 */
elijahsj 1:4c3c2b7337a6 91 GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
elijahsj 1:4c3c2b7337a6 92 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
elijahsj 1:4c3c2b7337a6 93 GPIO_InitStruct.Pull = GPIO_NOPULL;
elijahsj 1:4c3c2b7337a6 94 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
elijahsj 1:4c3c2b7337a6 95 GPIO_InitStruct.Alternate = GPIO_AF2_TIM12;
elijahsj 1:4c3c2b7337a6 96 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
elijahsj 1:4c3c2b7337a6 97
elijahsj 1:4c3c2b7337a6 98 /* USER CODE BEGIN TIM12_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 99
elijahsj 1:4c3c2b7337a6 100 /* USER CODE END TIM12_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 101 }
elijahsj 1:4c3c2b7337a6 102 else if(htim->Instance==TIM13)
elijahsj 1:4c3c2b7337a6 103 {
elijahsj 1:4c3c2b7337a6 104 /* USER CODE BEGIN TIM13_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 105
elijahsj 1:4c3c2b7337a6 106 /* USER CODE END TIM13_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 107
elijahsj 1:4c3c2b7337a6 108 __HAL_RCC_GPIOA_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 109 /**TIM13 GPIO Configuration
elijahsj 1:4c3c2b7337a6 110 PA6 ------> TIM13_CH1
elijahsj 1:4c3c2b7337a6 111 */
elijahsj 1:4c3c2b7337a6 112 GPIO_InitStruct.Pin = GPIO_PIN_6;
elijahsj 1:4c3c2b7337a6 113 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
elijahsj 1:4c3c2b7337a6 114 GPIO_InitStruct.Pull = GPIO_NOPULL;
elijahsj 1:4c3c2b7337a6 115 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
elijahsj 1:4c3c2b7337a6 116 GPIO_InitStruct.Alternate = GPIO_AF9_TIM13;
elijahsj 1:4c3c2b7337a6 117 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
elijahsj 1:4c3c2b7337a6 118
elijahsj 1:4c3c2b7337a6 119 /* USER CODE BEGIN TIM13_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 120
elijahsj 1:4c3c2b7337a6 121 /* USER CODE END TIM13_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 122 }
elijahsj 1:4c3c2b7337a6 123 else if(htim->Instance==TIM14)
elijahsj 1:4c3c2b7337a6 124 {
elijahsj 1:4c3c2b7337a6 125 /* USER CODE BEGIN TIM14_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 126
elijahsj 1:4c3c2b7337a6 127 /* USER CODE END TIM14_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 128
elijahsj 1:4c3c2b7337a6 129 __HAL_RCC_GPIOF_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 130 /**TIM14 GPIO Configuration
elijahsj 1:4c3c2b7337a6 131 PF9 ------> TIM14_CH1
elijahsj 1:4c3c2b7337a6 132 */
elijahsj 1:4c3c2b7337a6 133 GPIO_InitStruct.Pin = GPIO_PIN_9;
elijahsj 1:4c3c2b7337a6 134 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
elijahsj 1:4c3c2b7337a6 135 GPIO_InitStruct.Pull = GPIO_NOPULL;
elijahsj 1:4c3c2b7337a6 136 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
elijahsj 1:4c3c2b7337a6 137 GPIO_InitStruct.Alternate = GPIO_AF9_TIM14;
elijahsj 1:4c3c2b7337a6 138 HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
elijahsj 1:4c3c2b7337a6 139
elijahsj 1:4c3c2b7337a6 140 /* USER CODE BEGIN TIM14_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 141
elijahsj 1:4c3c2b7337a6 142 /* USER CODE END TIM14_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 143 }
elijahsj 1:4c3c2b7337a6 144 else if(htim->Instance==TIM15)
elijahsj 1:4c3c2b7337a6 145 {
elijahsj 1:4c3c2b7337a6 146 /* USER CODE BEGIN TIM15_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 147
elijahsj 1:4c3c2b7337a6 148 /* USER CODE END TIM15_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 149
elijahsj 1:4c3c2b7337a6 150 __HAL_RCC_GPIOE_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 151 /**TIM15 GPIO Configuration
elijahsj 1:4c3c2b7337a6 152 PE5 ------> TIM15_CH1
elijahsj 1:4c3c2b7337a6 153 PE6 ------> TIM15_CH2
elijahsj 1:4c3c2b7337a6 154 */
elijahsj 1:4c3c2b7337a6 155 GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
elijahsj 1:4c3c2b7337a6 156 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
elijahsj 1:4c3c2b7337a6 157 GPIO_InitStruct.Pull = GPIO_NOPULL;
elijahsj 1:4c3c2b7337a6 158 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
elijahsj 1:4c3c2b7337a6 159 GPIO_InitStruct.Alternate = GPIO_AF4_TIM15;
elijahsj 1:4c3c2b7337a6 160 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
elijahsj 1:4c3c2b7337a6 161
elijahsj 1:4c3c2b7337a6 162 /* USER CODE BEGIN TIM15_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 163
elijahsj 1:4c3c2b7337a6 164 /* USER CODE END TIM15_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 165 }
elijahsj 1:4c3c2b7337a6 166 else if(htim->Instance==TIM16)
elijahsj 1:4c3c2b7337a6 167 {
elijahsj 1:4c3c2b7337a6 168 /* USER CODE BEGIN TIM16_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 169
elijahsj 1:4c3c2b7337a6 170 /* USER CODE END TIM16_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 171
elijahsj 1:4c3c2b7337a6 172 __HAL_RCC_GPIOF_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 173 /**TIM16 GPIO Configuration
elijahsj 1:4c3c2b7337a6 174 PF6 ------> TIM16_CH1
elijahsj 1:4c3c2b7337a6 175 */
elijahsj 1:4c3c2b7337a6 176 GPIO_InitStruct.Pin = GPIO_PIN_6;
elijahsj 1:4c3c2b7337a6 177 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
elijahsj 1:4c3c2b7337a6 178 GPIO_InitStruct.Pull = GPIO_NOPULL;
elijahsj 1:4c3c2b7337a6 179 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
elijahsj 1:4c3c2b7337a6 180 GPIO_InitStruct.Alternate = GPIO_AF1_TIM16;
elijahsj 1:4c3c2b7337a6 181 HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
elijahsj 1:4c3c2b7337a6 182
elijahsj 1:4c3c2b7337a6 183 /* USER CODE BEGIN TIM16_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 184
elijahsj 1:4c3c2b7337a6 185 /* USER CODE END TIM16_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 186 }
elijahsj 1:4c3c2b7337a6 187 else if(htim->Instance==TIM17)
elijahsj 1:4c3c2b7337a6 188 {
elijahsj 1:4c3c2b7337a6 189 /* USER CODE BEGIN TIM17_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 190
elijahsj 1:4c3c2b7337a6 191 /* USER CODE END TIM17_MspPostInit 0 */
elijahsj 1:4c3c2b7337a6 192
elijahsj 1:4c3c2b7337a6 193 __HAL_RCC_GPIOF_CLK_ENABLE();
elijahsj 1:4c3c2b7337a6 194 /**TIM17 GPIO Configuration
elijahsj 1:4c3c2b7337a6 195 PF7 ------> TIM17_CH1
elijahsj 1:4c3c2b7337a6 196 */
elijahsj 1:4c3c2b7337a6 197 GPIO_InitStruct.Pin = GPIO_PIN_7;
elijahsj 1:4c3c2b7337a6 198 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
elijahsj 1:4c3c2b7337a6 199 GPIO_InitStruct.Pull = GPIO_NOPULL;
elijahsj 1:4c3c2b7337a6 200 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
elijahsj 1:4c3c2b7337a6 201 GPIO_InitStruct.Alternate = GPIO_AF1_TIM17;
elijahsj 1:4c3c2b7337a6 202 HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
elijahsj 1:4c3c2b7337a6 203
elijahsj 1:4c3c2b7337a6 204 /* USER CODE BEGIN TIM17_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 205
elijahsj 1:4c3c2b7337a6 206 /* USER CODE END TIM17_MspPostInit 1 */
elijahsj 1:4c3c2b7337a6 207 }
elijahsj 1:4c3c2b7337a6 208
elijahsj 1:4c3c2b7337a6 209 }
elijahsj 1:4c3c2b7337a6 210
elijahsj 1:4c3c2b7337a6 211 void SystemClock_Config(void)
elijahsj 1:4c3c2b7337a6 212 {
elijahsj 1:4c3c2b7337a6 213 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
elijahsj 1:4c3c2b7337a6 214 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
elijahsj 1:4c3c2b7337a6 215 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
elijahsj 1:4c3c2b7337a6 216
elijahsj 1:4c3c2b7337a6 217 /** Supply configuration update enable
elijahsj 1:4c3c2b7337a6 218 */
elijahsj 1:4c3c2b7337a6 219 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
elijahsj 1:4c3c2b7337a6 220 /** Configure the main internal regulator output voltage
elijahsj 1:4c3c2b7337a6 221 */
elijahsj 1:4c3c2b7337a6 222 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
elijahsj 1:4c3c2b7337a6 223
elijahsj 1:4c3c2b7337a6 224 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
elijahsj 1:4c3c2b7337a6 225 /** Macro to configure the PLL clock source
elijahsj 1:4c3c2b7337a6 226 */
elijahsj 1:4c3c2b7337a6 227 __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI);
elijahsj 1:4c3c2b7337a6 228 /** Initializes the CPU, AHB and APB busses clocks
elijahsj 1:4c3c2b7337a6 229 */
elijahsj 1:4c3c2b7337a6 230 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
elijahsj 1:4c3c2b7337a6 231 RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
elijahsj 1:4c3c2b7337a6 232 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
elijahsj 1:4c3c2b7337a6 233 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
elijahsj 1:4c3c2b7337a6 234 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
elijahsj 1:4c3c2b7337a6 235 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
elijahsj 1:4c3c2b7337a6 236 {
elijahsj 1:4c3c2b7337a6 237 //Error_Handler();
elijahsj 1:4c3c2b7337a6 238 }
elijahsj 1:4c3c2b7337a6 239 /** Initializes the CPU, AHB and APB busses clocks
elijahsj 1:4c3c2b7337a6 240 */
elijahsj 1:4c3c2b7337a6 241 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
elijahsj 1:4c3c2b7337a6 242 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
elijahsj 1:4c3c2b7337a6 243 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
elijahsj 1:4c3c2b7337a6 244 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
elijahsj 1:4c3c2b7337a6 245 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
elijahsj 1:4c3c2b7337a6 246 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
elijahsj 1:4c3c2b7337a6 247 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
elijahsj 1:4c3c2b7337a6 248 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
elijahsj 1:4c3c2b7337a6 249 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
elijahsj 1:4c3c2b7337a6 250 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
elijahsj 1:4c3c2b7337a6 251
elijahsj 1:4c3c2b7337a6 252 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
elijahsj 1:4c3c2b7337a6 253 {
elijahsj 1:4c3c2b7337a6 254 //Error_Handler();
elijahsj 1:4c3c2b7337a6 255 }
elijahsj 1:4c3c2b7337a6 256
elijahsj 1:4c3c2b7337a6 257 }
elijahsj 1:4c3c2b7337a6 258
elijahsj 1:4c3c2b7337a6 259
elijahsj 1:4c3c2b7337a6 260
elijahsj 1:4c3c2b7337a6 261