encoder
Dependencies: mbed
EncoderMspInitF4.cpp@0:5067873a2400, 2017-04-26 (annotated)
- Committer:
- schille
- Date:
- Wed Apr 26 08:26:20 2017 +0000
- Revision:
- 0:5067873a2400
mmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
schille | 0:5067873a2400 | 1 | #include "mbed.h" |
schille | 0:5067873a2400 | 2 | /* |
schille | 0:5067873a2400 | 3 | * HAL_TIM_Encoder_MspInit() |
schille | 0:5067873a2400 | 4 | * Overrides the __weak function stub in stm32f4xx_hal_tim.h |
schille | 0:5067873a2400 | 5 | * |
schille | 0:5067873a2400 | 6 | * Edit the below for your preferred pin wiring & pullup/down |
schille | 0:5067873a2400 | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
schille | 0:5067873a2400 | 8 | * Encoder A&B outputs connected directly to GPIOs. |
schille | 0:5067873a2400 | 9 | * |
schille | 0:5067873a2400 | 10 | * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf |
schille | 0:5067873a2400 | 11 | * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf |
schille | 0:5067873a2400 | 12 | * |
schille | 0:5067873a2400 | 13 | * TIM1_CH1: AF1 @ PA_8, PE_9 |
schille | 0:5067873a2400 | 14 | * TIM1_CH2: AF1 @ PA_9, PE_11 |
schille | 0:5067873a2400 | 15 | * |
schille | 0:5067873a2400 | 16 | * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8* *F446 only |
schille | 0:5067873a2400 | 17 | * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9* *F446 only |
schille | 0:5067873a2400 | 18 | * |
schille | 0:5067873a2400 | 19 | * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6 |
schille | 0:5067873a2400 | 20 | * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7 |
schille | 0:5067873a2400 | 21 | * |
schille | 0:5067873a2400 | 22 | * TIM4_CH1: AF2 @ PB_6, PD_12 |
schille | 0:5067873a2400 | 23 | * TIM4_CH2: AF2 @ PB_7, PD_13 |
schille | 0:5067873a2400 | 24 | * |
schille | 0:5067873a2400 | 25 | * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable |
schille | 0:5067873a2400 | 26 | * TIM5_CH2: AF2 @ PA_1* |
schille | 0:5067873a2400 | 27 | * |
schille | 0:5067873a2400 | 28 | */ |
schille | 0:5067873a2400 | 29 | |
schille | 0:5067873a2400 | 30 | #ifdef TARGET_STM32F4 |
schille | 0:5067873a2400 | 31 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *timer) |
schille | 0:5067873a2400 | 32 | { |
schille | 0:5067873a2400 | 33 | GPIO_InitTypeDef GPIO_InitStruct; |
schille | 0:5067873a2400 | 34 | |
schille | 0:5067873a2400 | 35 | if (timer->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8 |
schille | 0:5067873a2400 | 36 | __TIM1_CLK_ENABLE(); |
schille | 0:5067873a2400 | 37 | __GPIOA_CLK_ENABLE(); |
schille | 0:5067873a2400 | 38 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
schille | 0:5067873a2400 | 39 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
schille | 0:5067873a2400 | 40 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
schille | 0:5067873a2400 | 41 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
schille | 0:5067873a2400 | 42 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
schille | 0:5067873a2400 | 43 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
schille | 0:5067873a2400 | 44 | } |
schille | 0:5067873a2400 | 45 | else if (timer->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
schille | 0:5067873a2400 | 46 | __TIM2_CLK_ENABLE(); |
schille | 0:5067873a2400 | 47 | __GPIOA_CLK_ENABLE(); |
schille | 0:5067873a2400 | 48 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
schille | 0:5067873a2400 | 49 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
schille | 0:5067873a2400 | 50 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
schille | 0:5067873a2400 | 51 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
schille | 0:5067873a2400 | 52 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
schille | 0:5067873a2400 | 53 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
schille | 0:5067873a2400 | 54 | } |
schille | 0:5067873a2400 | 55 | else if (timer->Instance == TIM3) { //PA6 PA7 = Nucleo D12 D11 |
schille | 0:5067873a2400 | 56 | __TIM3_CLK_ENABLE(); |
schille | 0:5067873a2400 | 57 | __GPIOB_CLK_ENABLE(); |
schille | 0:5067873a2400 | 58 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
schille | 0:5067873a2400 | 59 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
schille | 0:5067873a2400 | 60 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
schille | 0:5067873a2400 | 61 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
schille | 0:5067873a2400 | 62 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
schille | 0:5067873a2400 | 63 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
schille | 0:5067873a2400 | 64 | } |
schille | 0:5067873a2400 | 65 | else if (timer->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
schille | 0:5067873a2400 | 66 | __TIM4_CLK_ENABLE(); |
schille | 0:5067873a2400 | 67 | __GPIOB_CLK_ENABLE(); |
schille | 0:5067873a2400 | 68 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
schille | 0:5067873a2400 | 69 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
schille | 0:5067873a2400 | 70 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
schille | 0:5067873a2400 | 71 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
schille | 0:5067873a2400 | 72 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
schille | 0:5067873a2400 | 73 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
schille | 0:5067873a2400 | 74 | } |
schille | 0:5067873a2400 | 75 | } |
schille | 0:5067873a2400 | 76 | #endif |