Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
HardwareSetup.cpp@5:d2dffc88e94d, 2020-08-26 (annotated)
- Committer:
- elijahsj
- Date:
- Wed Aug 26 14:37:16 2020 +0000
- Revision:
- 5:d2dffc88e94d
- Parent:
- 3:2f46953e7c8b
- Child:
- 6:417655779dc5
PWM + Encoders work;
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:2f46953e7c8b | 12 | int PWM_PERIOD; |
elijahsj | 1:4c3c2b7337a6 | 13 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); |
elijahsj | 1:4c3c2b7337a6 | 14 | static void MX_TIM12_Init(void); |
elijahsj | 2:30503be9a375 | 15 | static void MX_TIM13_Init(void); |
elijahsj | 2:30503be9a375 | 16 | static void MX_TIM14_Init(void); |
elijahsj | 2:30503be9a375 | 17 | static void MX_TIM15_Init(void); |
elijahsj | 2:30503be9a375 | 18 | static void MX_TIM16_Init(void); |
elijahsj | 2:30503be9a375 | 19 | static void MX_TIM17_Init(void); |
elijahsj | 1:4c3c2b7337a6 | 20 | |
elijahsj | 3:2f46953e7c8b | 21 | void initHardware(int periodTicks){ |
elijahsj | 3:2f46953e7c8b | 22 | PWM_PERIOD = periodTicks; |
elijahsj | 1:4c3c2b7337a6 | 23 | /* Initialise the HAL Layer */ |
elijahsj | 1:4c3c2b7337a6 | 24 | HAL_Init(); |
elijahsj | 1:4c3c2b7337a6 | 25 | |
elijahsj | 1:4c3c2b7337a6 | 26 | /* Initialize all configured peripherals */ |
elijahsj | 1:4c3c2b7337a6 | 27 | MX_TIM12_Init(); |
elijahsj | 2:30503be9a375 | 28 | MX_TIM13_Init(); |
elijahsj | 2:30503be9a375 | 29 | MX_TIM14_Init(); |
elijahsj | 2:30503be9a375 | 30 | MX_TIM15_Init(); |
elijahsj | 2:30503be9a375 | 31 | MX_TIM16_Init(); |
elijahsj | 2:30503be9a375 | 32 | MX_TIM17_Init(); |
elijahsj | 2:30503be9a375 | 33 | //HAL_TIM_Base_Start(&htim15); |
elijahsj | 2:30503be9a375 | 34 | |
elijahsj | 1:4c3c2b7337a6 | 35 | HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1); // start pwm generation |
elijahsj | 1:4c3c2b7337a6 | 36 | HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_2); // start pwm generation |
elijahsj | 2:30503be9a375 | 37 | HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1); // start pwm generation |
elijahsj | 2:30503be9a375 | 38 | HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); // start pwm generation |
elijahsj | 2:30503be9a375 | 39 | HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1); // start pwm generation |
elijahsj | 2:30503be9a375 | 40 | HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_2); // start pwm generation |
elijahsj | 2:30503be9a375 | 41 | HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); // start pwm generation |
elijahsj | 2:30503be9a375 | 42 | HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1); // start pwm generation |
elijahsj | 2:30503be9a375 | 43 | |
elijahsj | 2:30503be9a375 | 44 | |
elijahsj | 1:4c3c2b7337a6 | 45 | } |
elijahsj | 1:4c3c2b7337a6 | 46 | |
elijahsj | 1:4c3c2b7337a6 | 47 | static void MX_TIM12_Init(void) |
elijahsj | 1:4c3c2b7337a6 | 48 | { |
elijahsj | 1:4c3c2b7337a6 | 49 | __HAL_RCC_TIM12_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 50 | TIM_OC_InitTypeDef sConfigOC = {0}; |
elijahsj | 1:4c3c2b7337a6 | 51 | htim12.Instance = TIM12; |
elijahsj | 1:4c3c2b7337a6 | 52 | htim12.Init.Prescaler = 0; |
elijahsj | 1:4c3c2b7337a6 | 53 | htim12.Init.CounterMode = TIM_COUNTERMODE_UP; |
elijahsj | 1:4c3c2b7337a6 | 54 | htim12.Init.Period = PWM_PERIOD; |
elijahsj | 1:4c3c2b7337a6 | 55 | htim12.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
elijahsj | 1:4c3c2b7337a6 | 56 | htim12.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
elijahsj | 1:4c3c2b7337a6 | 57 | if (HAL_TIM_PWM_Init(&htim12) != HAL_OK) |
elijahsj | 1:4c3c2b7337a6 | 58 | { |
elijahsj | 1:4c3c2b7337a6 | 59 | //Error_Handler(); |
elijahsj | 1:4c3c2b7337a6 | 60 | } |
elijahsj | 1:4c3c2b7337a6 | 61 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
elijahsj | 1:4c3c2b7337a6 | 62 | sConfigOC.Pulse = 0; |
elijahsj | 1:4c3c2b7337a6 | 63 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
elijahsj | 1:4c3c2b7337a6 | 64 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
elijahsj | 1:4c3c2b7337a6 | 65 | if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
elijahsj | 1:4c3c2b7337a6 | 66 | { |
elijahsj | 1:4c3c2b7337a6 | 67 | //Error_Handler(); |
elijahsj | 1:4c3c2b7337a6 | 68 | } |
elijahsj | 1:4c3c2b7337a6 | 69 | if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) |
elijahsj | 1:4c3c2b7337a6 | 70 | { |
elijahsj | 1:4c3c2b7337a6 | 71 | //Error_Handler(); |
elijahsj | 1:4c3c2b7337a6 | 72 | } |
elijahsj | 2:30503be9a375 | 73 | HAL_TIM_MspPostInit(&htim12); |
elijahsj | 2:30503be9a375 | 74 | } |
elijahsj | 2:30503be9a375 | 75 | |
elijahsj | 2:30503be9a375 | 76 | static void MX_TIM13_Init(void) |
elijahsj | 2:30503be9a375 | 77 | { |
elijahsj | 2:30503be9a375 | 78 | __HAL_RCC_TIM13_CLK_ENABLE(); |
elijahsj | 2:30503be9a375 | 79 | TIM_OC_InitTypeDef sConfigOC = {0}; |
elijahsj | 2:30503be9a375 | 80 | htim13.Instance = TIM13; |
elijahsj | 2:30503be9a375 | 81 | htim13.Init.Prescaler = 0; |
elijahsj | 2:30503be9a375 | 82 | htim13.Init.CounterMode = TIM_COUNTERMODE_UP; |
elijahsj | 2:30503be9a375 | 83 | htim13.Init.Period = PWM_PERIOD; |
elijahsj | 2:30503be9a375 | 84 | htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
elijahsj | 2:30503be9a375 | 85 | htim13.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
elijahsj | 2:30503be9a375 | 86 | if (HAL_TIM_Base_Init(&htim13) != HAL_OK) |
elijahsj | 2:30503be9a375 | 87 | { |
elijahsj | 2:30503be9a375 | 88 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 89 | } |
elijahsj | 2:30503be9a375 | 90 | if (HAL_TIM_PWM_Init(&htim13) != HAL_OK) |
elijahsj | 2:30503be9a375 | 91 | { |
elijahsj | 2:30503be9a375 | 92 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 93 | } |
elijahsj | 2:30503be9a375 | 94 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
elijahsj | 2:30503be9a375 | 95 | sConfigOC.Pulse = 0; |
elijahsj | 2:30503be9a375 | 96 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 97 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
elijahsj | 2:30503be9a375 | 98 | if (HAL_TIM_PWM_ConfigChannel(&htim13, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
elijahsj | 2:30503be9a375 | 99 | { |
elijahsj | 2:30503be9a375 | 100 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 101 | } |
elijahsj | 2:30503be9a375 | 102 | HAL_TIM_MspPostInit(&htim13); |
elijahsj | 2:30503be9a375 | 103 | } |
elijahsj | 2:30503be9a375 | 104 | |
elijahsj | 2:30503be9a375 | 105 | static void MX_TIM14_Init(void) |
elijahsj | 2:30503be9a375 | 106 | { |
elijahsj | 2:30503be9a375 | 107 | __HAL_RCC_TIM14_CLK_ENABLE(); |
elijahsj | 2:30503be9a375 | 108 | TIM_OC_InitTypeDef sConfigOC = {0}; |
elijahsj | 2:30503be9a375 | 109 | htim14.Instance = TIM14; |
elijahsj | 2:30503be9a375 | 110 | htim14.Init.Prescaler = 0; |
elijahsj | 2:30503be9a375 | 111 | htim14.Init.CounterMode = TIM_COUNTERMODE_UP; |
elijahsj | 2:30503be9a375 | 112 | htim14.Init.Period = PWM_PERIOD; |
elijahsj | 2:30503be9a375 | 113 | htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
elijahsj | 2:30503be9a375 | 114 | htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
elijahsj | 2:30503be9a375 | 115 | if (HAL_TIM_Base_Init(&htim14) != HAL_OK) |
elijahsj | 2:30503be9a375 | 116 | { |
elijahsj | 2:30503be9a375 | 117 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 118 | } |
elijahsj | 2:30503be9a375 | 119 | if (HAL_TIM_PWM_Init(&htim14) != HAL_OK) |
elijahsj | 2:30503be9a375 | 120 | { |
elijahsj | 2:30503be9a375 | 121 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 122 | } |
elijahsj | 2:30503be9a375 | 123 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
elijahsj | 2:30503be9a375 | 124 | sConfigOC.Pulse = 0; |
elijahsj | 2:30503be9a375 | 125 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 126 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
elijahsj | 2:30503be9a375 | 127 | if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
elijahsj | 2:30503be9a375 | 128 | { |
elijahsj | 2:30503be9a375 | 129 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 130 | } |
elijahsj | 2:30503be9a375 | 131 | HAL_TIM_MspPostInit(&htim14); |
elijahsj | 2:30503be9a375 | 132 | } |
elijahsj | 1:4c3c2b7337a6 | 133 | |
elijahsj | 2:30503be9a375 | 134 | static void MX_TIM15_Init(void) |
elijahsj | 2:30503be9a375 | 135 | { |
elijahsj | 2:30503be9a375 | 136 | __HAL_RCC_TIM15_CLK_ENABLE(); |
elijahsj | 2:30503be9a375 | 137 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
elijahsj | 2:30503be9a375 | 138 | TIM_OC_InitTypeDef sConfigOC = {0}; |
elijahsj | 2:30503be9a375 | 139 | TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; |
elijahsj | 2:30503be9a375 | 140 | htim15.Instance = TIM15; |
elijahsj | 2:30503be9a375 | 141 | htim15.Init.Prescaler = 0; |
elijahsj | 2:30503be9a375 | 142 | htim15.Init.CounterMode = TIM_COUNTERMODE_UP; |
elijahsj | 2:30503be9a375 | 143 | htim15.Init.Period = PWM_PERIOD; |
elijahsj | 2:30503be9a375 | 144 | htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
elijahsj | 2:30503be9a375 | 145 | htim15.Init.RepetitionCounter = 0; |
elijahsj | 2:30503be9a375 | 146 | htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
elijahsj | 2:30503be9a375 | 147 | if (HAL_TIM_PWM_Init(&htim15) != HAL_OK) |
elijahsj | 2:30503be9a375 | 148 | { |
elijahsj | 2:30503be9a375 | 149 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 150 | } |
elijahsj | 2:30503be9a375 | 151 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
elijahsj | 2:30503be9a375 | 152 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
elijahsj | 2:30503be9a375 | 153 | if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK) |
elijahsj | 2:30503be9a375 | 154 | { |
elijahsj | 2:30503be9a375 | 155 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 156 | } |
elijahsj | 2:30503be9a375 | 157 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
elijahsj | 2:30503be9a375 | 158 | sConfigOC.Pulse = 0; |
elijahsj | 2:30503be9a375 | 159 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 160 | sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 161 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
elijahsj | 2:30503be9a375 | 162 | sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; |
elijahsj | 2:30503be9a375 | 163 | sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
elijahsj | 2:30503be9a375 | 164 | if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
elijahsj | 2:30503be9a375 | 165 | { |
elijahsj | 2:30503be9a375 | 166 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 167 | } |
elijahsj | 2:30503be9a375 | 168 | if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) |
elijahsj | 2:30503be9a375 | 169 | { |
elijahsj | 2:30503be9a375 | 170 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 171 | } |
elijahsj | 2:30503be9a375 | 172 | sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; |
elijahsj | 2:30503be9a375 | 173 | sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; |
elijahsj | 2:30503be9a375 | 174 | sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; |
elijahsj | 2:30503be9a375 | 175 | sBreakDeadTimeConfig.DeadTime = 0; |
elijahsj | 2:30503be9a375 | 176 | sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; |
elijahsj | 2:30503be9a375 | 177 | sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 178 | sBreakDeadTimeConfig.BreakFilter = 0; |
elijahsj | 2:30503be9a375 | 179 | sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; |
elijahsj | 2:30503be9a375 | 180 | if (HAL_TIMEx_ConfigBreakDeadTime(&htim15, &sBreakDeadTimeConfig) != HAL_OK) |
elijahsj | 2:30503be9a375 | 181 | { |
elijahsj | 2:30503be9a375 | 182 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 183 | } |
elijahsj | 2:30503be9a375 | 184 | HAL_TIM_MspPostInit(&htim15); |
elijahsj | 2:30503be9a375 | 185 | |
elijahsj | 2:30503be9a375 | 186 | } |
elijahsj | 2:30503be9a375 | 187 | |
elijahsj | 2:30503be9a375 | 188 | static void MX_TIM16_Init(void) |
elijahsj | 2:30503be9a375 | 189 | { |
elijahsj | 2:30503be9a375 | 190 | __HAL_RCC_TIM16_CLK_ENABLE(); |
elijahsj | 2:30503be9a375 | 191 | TIM_OC_InitTypeDef sConfigOC = {0}; |
elijahsj | 2:30503be9a375 | 192 | TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; |
elijahsj | 2:30503be9a375 | 193 | htim16.Instance = TIM16; |
elijahsj | 2:30503be9a375 | 194 | htim16.Init.Prescaler = 0; |
elijahsj | 2:30503be9a375 | 195 | htim16.Init.CounterMode = TIM_COUNTERMODE_UP; |
elijahsj | 2:30503be9a375 | 196 | htim16.Init.Period = PWM_PERIOD; |
elijahsj | 2:30503be9a375 | 197 | htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
elijahsj | 2:30503be9a375 | 198 | htim16.Init.RepetitionCounter = 0; |
elijahsj | 2:30503be9a375 | 199 | htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
elijahsj | 2:30503be9a375 | 200 | if (HAL_TIM_Base_Init(&htim16) != HAL_OK) |
elijahsj | 2:30503be9a375 | 201 | { |
elijahsj | 2:30503be9a375 | 202 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 203 | } |
elijahsj | 2:30503be9a375 | 204 | if (HAL_TIM_PWM_Init(&htim16) != HAL_OK) |
elijahsj | 2:30503be9a375 | 205 | { |
elijahsj | 2:30503be9a375 | 206 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 207 | } |
elijahsj | 2:30503be9a375 | 208 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
elijahsj | 2:30503be9a375 | 209 | sConfigOC.Pulse = 0; |
elijahsj | 2:30503be9a375 | 210 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 211 | sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 212 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
elijahsj | 2:30503be9a375 | 213 | sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; |
elijahsj | 2:30503be9a375 | 214 | sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
elijahsj | 2:30503be9a375 | 215 | if (HAL_TIM_PWM_ConfigChannel(&htim16, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
elijahsj | 2:30503be9a375 | 216 | { |
elijahsj | 2:30503be9a375 | 217 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 218 | } |
elijahsj | 2:30503be9a375 | 219 | sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; |
elijahsj | 2:30503be9a375 | 220 | sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; |
elijahsj | 2:30503be9a375 | 221 | sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; |
elijahsj | 2:30503be9a375 | 222 | sBreakDeadTimeConfig.DeadTime = 0; |
elijahsj | 2:30503be9a375 | 223 | sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; |
elijahsj | 2:30503be9a375 | 224 | sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 225 | sBreakDeadTimeConfig.BreakFilter = 0; |
elijahsj | 2:30503be9a375 | 226 | sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; |
elijahsj | 2:30503be9a375 | 227 | if (HAL_TIMEx_ConfigBreakDeadTime(&htim16, &sBreakDeadTimeConfig) != HAL_OK) |
elijahsj | 2:30503be9a375 | 228 | { |
elijahsj | 2:30503be9a375 | 229 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 230 | } |
elijahsj | 2:30503be9a375 | 231 | HAL_TIM_MspPostInit(&htim16); |
elijahsj | 2:30503be9a375 | 232 | |
elijahsj | 2:30503be9a375 | 233 | } |
elijahsj | 2:30503be9a375 | 234 | |
elijahsj | 2:30503be9a375 | 235 | static void MX_TIM17_Init(void) |
elijahsj | 2:30503be9a375 | 236 | { |
elijahsj | 2:30503be9a375 | 237 | __HAL_RCC_TIM17_CLK_ENABLE(); |
elijahsj | 2:30503be9a375 | 238 | TIM_OC_InitTypeDef sConfigOC = {0}; |
elijahsj | 2:30503be9a375 | 239 | TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; |
elijahsj | 2:30503be9a375 | 240 | htim17.Instance = TIM17; |
elijahsj | 2:30503be9a375 | 241 | htim17.Init.Prescaler = 0; |
elijahsj | 2:30503be9a375 | 242 | htim17.Init.CounterMode = TIM_COUNTERMODE_UP; |
elijahsj | 2:30503be9a375 | 243 | htim17.Init.Period = PWM_PERIOD; |
elijahsj | 2:30503be9a375 | 244 | htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
elijahsj | 2:30503be9a375 | 245 | htim17.Init.RepetitionCounter = 0; |
elijahsj | 2:30503be9a375 | 246 | htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
elijahsj | 2:30503be9a375 | 247 | if (HAL_TIM_Base_Init(&htim17) != HAL_OK) |
elijahsj | 2:30503be9a375 | 248 | { |
elijahsj | 2:30503be9a375 | 249 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 250 | } |
elijahsj | 2:30503be9a375 | 251 | if (HAL_TIM_PWM_Init(&htim17) != HAL_OK) |
elijahsj | 2:30503be9a375 | 252 | { |
elijahsj | 2:30503be9a375 | 253 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 254 | } |
elijahsj | 2:30503be9a375 | 255 | sConfigOC.OCMode = TIM_OCMODE_PWM1; |
elijahsj | 2:30503be9a375 | 256 | sConfigOC.Pulse = 0; |
elijahsj | 2:30503be9a375 | 257 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 258 | sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 259 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
elijahsj | 2:30503be9a375 | 260 | sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; |
elijahsj | 2:30503be9a375 | 261 | sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
elijahsj | 2:30503be9a375 | 262 | if (HAL_TIM_PWM_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
elijahsj | 2:30503be9a375 | 263 | { |
elijahsj | 2:30503be9a375 | 264 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 265 | } |
elijahsj | 2:30503be9a375 | 266 | sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; |
elijahsj | 2:30503be9a375 | 267 | sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; |
elijahsj | 2:30503be9a375 | 268 | sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; |
elijahsj | 2:30503be9a375 | 269 | sBreakDeadTimeConfig.DeadTime = 0; |
elijahsj | 2:30503be9a375 | 270 | sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; |
elijahsj | 2:30503be9a375 | 271 | sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; |
elijahsj | 2:30503be9a375 | 272 | sBreakDeadTimeConfig.BreakFilter = 0; |
elijahsj | 2:30503be9a375 | 273 | sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; |
elijahsj | 2:30503be9a375 | 274 | if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK) |
elijahsj | 2:30503be9a375 | 275 | { |
elijahsj | 2:30503be9a375 | 276 | //Error_Handler(); |
elijahsj | 2:30503be9a375 | 277 | } |
elijahsj | 2:30503be9a375 | 278 | HAL_TIM_MspPostInit(&htim17); |
elijahsj | 2:30503be9a375 | 279 | |
elijahsj | 1:4c3c2b7337a6 | 280 | } |
elijahsj | 1:4c3c2b7337a6 | 281 | |
elijahsj | 1:4c3c2b7337a6 | 282 | void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) |
elijahsj | 1:4c3c2b7337a6 | 283 | { |
elijahsj | 2:30503be9a375 | 284 | /* GPIO Ports Clock Enable */ |
elijahsj | 1:4c3c2b7337a6 | 285 | __HAL_RCC_GPIOE_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 286 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 287 | __HAL_RCC_GPIOF_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 288 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 289 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 290 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 291 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 292 | __HAL_RCC_GPIOG_CLK_ENABLE(); |
elijahsj | 1:4c3c2b7337a6 | 293 | |
elijahsj | 1:4c3c2b7337a6 | 294 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
elijahsj | 1:4c3c2b7337a6 | 295 | if(htim->Instance==TIM12) |
elijahsj | 1:4c3c2b7337a6 | 296 | { |
elijahsj | 1:4c3c2b7337a6 | 297 | |
elijahsj | 1:4c3c2b7337a6 | 298 | /**TIM12 GPIO Configuration |
elijahsj | 1:4c3c2b7337a6 | 299 | PB14 ------> TIM12_CH1 |
elijahsj | 1:4c3c2b7337a6 | 300 | PB15 ------> TIM12_CH2 |
elijahsj | 1:4c3c2b7337a6 | 301 | */ |
elijahsj | 1:4c3c2b7337a6 | 302 | GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15; |
elijahsj | 1:4c3c2b7337a6 | 303 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
elijahsj | 1:4c3c2b7337a6 | 304 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
elijahsj | 1:4c3c2b7337a6 | 305 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
elijahsj | 1:4c3c2b7337a6 | 306 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM12; |
elijahsj | 1:4c3c2b7337a6 | 307 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
elijahsj | 1:4c3c2b7337a6 | 308 | } |
elijahsj | 1:4c3c2b7337a6 | 309 | else if(htim->Instance==TIM13) |
elijahsj | 1:4c3c2b7337a6 | 310 | { |
elijahsj | 1:4c3c2b7337a6 | 311 | /**TIM13 GPIO Configuration |
elijahsj | 1:4c3c2b7337a6 | 312 | PA6 ------> TIM13_CH1 |
elijahsj | 1:4c3c2b7337a6 | 313 | */ |
elijahsj | 1:4c3c2b7337a6 | 314 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
elijahsj | 1:4c3c2b7337a6 | 315 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
elijahsj | 1:4c3c2b7337a6 | 316 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
elijahsj | 1:4c3c2b7337a6 | 317 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
elijahsj | 1:4c3c2b7337a6 | 318 | GPIO_InitStruct.Alternate = GPIO_AF9_TIM13; |
elijahsj | 1:4c3c2b7337a6 | 319 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
elijahsj | 1:4c3c2b7337a6 | 320 | } |
elijahsj | 1:4c3c2b7337a6 | 321 | else if(htim->Instance==TIM14) |
elijahsj | 1:4c3c2b7337a6 | 322 | { |
elijahsj | 1:4c3c2b7337a6 | 323 | /**TIM14 GPIO Configuration |
elijahsj | 1:4c3c2b7337a6 | 324 | PF9 ------> TIM14_CH1 |
elijahsj | 1:4c3c2b7337a6 | 325 | */ |
elijahsj | 1:4c3c2b7337a6 | 326 | GPIO_InitStruct.Pin = GPIO_PIN_9; |
elijahsj | 1:4c3c2b7337a6 | 327 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
elijahsj | 1:4c3c2b7337a6 | 328 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
elijahsj | 1:4c3c2b7337a6 | 329 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
elijahsj | 1:4c3c2b7337a6 | 330 | GPIO_InitStruct.Alternate = GPIO_AF9_TIM14; |
elijahsj | 1:4c3c2b7337a6 | 331 | HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); |
elijahsj | 1:4c3c2b7337a6 | 332 | } |
elijahsj | 1:4c3c2b7337a6 | 333 | else if(htim->Instance==TIM15) |
elijahsj | 1:4c3c2b7337a6 | 334 | { |
elijahsj | 1:4c3c2b7337a6 | 335 | /**TIM15 GPIO Configuration |
elijahsj | 1:4c3c2b7337a6 | 336 | PE5 ------> TIM15_CH1 |
elijahsj | 1:4c3c2b7337a6 | 337 | PE6 ------> TIM15_CH2 |
elijahsj | 1:4c3c2b7337a6 | 338 | */ |
elijahsj | 1:4c3c2b7337a6 | 339 | GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6; |
elijahsj | 1:4c3c2b7337a6 | 340 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
elijahsj | 1:4c3c2b7337a6 | 341 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
elijahsj | 1:4c3c2b7337a6 | 342 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
elijahsj | 1:4c3c2b7337a6 | 343 | GPIO_InitStruct.Alternate = GPIO_AF4_TIM15; |
elijahsj | 1:4c3c2b7337a6 | 344 | HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); |
elijahsj | 1:4c3c2b7337a6 | 345 | } |
elijahsj | 1:4c3c2b7337a6 | 346 | else if(htim->Instance==TIM16) |
elijahsj | 1:4c3c2b7337a6 | 347 | { |
elijahsj | 1:4c3c2b7337a6 | 348 | /**TIM16 GPIO Configuration |
elijahsj | 1:4c3c2b7337a6 | 349 | PF6 ------> TIM16_CH1 |
elijahsj | 1:4c3c2b7337a6 | 350 | */ |
elijahsj | 1:4c3c2b7337a6 | 351 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
elijahsj | 1:4c3c2b7337a6 | 352 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
elijahsj | 1:4c3c2b7337a6 | 353 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
elijahsj | 1:4c3c2b7337a6 | 354 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
elijahsj | 1:4c3c2b7337a6 | 355 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM16; |
elijahsj | 1:4c3c2b7337a6 | 356 | HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); |
elijahsj | 1:4c3c2b7337a6 | 357 | } |
elijahsj | 1:4c3c2b7337a6 | 358 | else if(htim->Instance==TIM17) |
elijahsj | 1:4c3c2b7337a6 | 359 | { |
elijahsj | 1:4c3c2b7337a6 | 360 | /**TIM17 GPIO Configuration |
elijahsj | 1:4c3c2b7337a6 | 361 | PF7 ------> TIM17_CH1 |
elijahsj | 1:4c3c2b7337a6 | 362 | */ |
elijahsj | 1:4c3c2b7337a6 | 363 | GPIO_InitStruct.Pin = GPIO_PIN_7; |
elijahsj | 1:4c3c2b7337a6 | 364 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
elijahsj | 1:4c3c2b7337a6 | 365 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
elijahsj | 1:4c3c2b7337a6 | 366 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
elijahsj | 1:4c3c2b7337a6 | 367 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM17; |
elijahsj | 1:4c3c2b7337a6 | 368 | HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); |
elijahsj | 1:4c3c2b7337a6 | 369 | } |
elijahsj | 1:4c3c2b7337a6 | 370 | |
elijahsj | 1:4c3c2b7337a6 | 371 | } |
elijahsj | 1:4c3c2b7337a6 | 372 | |
elijahsj | 1:4c3c2b7337a6 | 373 | |
elijahsj | 1:4c3c2b7337a6 | 374 | |
elijahsj | 1:4c3c2b7337a6 | 375 |