hi
Fork of Nucleo_Hello_Encoder by
Encoder/EncoderMspInitL4.cpp@1:cd7b42c99ff8, 2015-10-04 (annotated)
- Committer:
- gregeric
- Date:
- Sun Oct 04 11:58:58 2015 +0000
- Revision:
- 1:cd7b42c99ff8
Added more targets, use HAL macros to read counter value & direction.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gregeric | 1:cd7b42c99ff8 | 1 | #include "mbed.h" |
gregeric | 1:cd7b42c99ff8 | 2 | /* |
gregeric | 1:cd7b42c99ff8 | 3 | * HAL_TIM_Encoder_MspInit() |
gregeric | 1:cd7b42c99ff8 | 4 | * Overrides the __weak function stub in stm32f4xx_hal_tim.h |
gregeric | 1:cd7b42c99ff8 | 5 | * |
gregeric | 1:cd7b42c99ff8 | 6 | * Edit the below for your preferred pin wiring & pullup/down |
gregeric | 1:cd7b42c99ff8 | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
gregeric | 1:cd7b42c99ff8 | 8 | * Encoder A&B outputs connected directly to GPIOs. |
gregeric | 1:cd7b42c99ff8 | 9 | * |
gregeric | 1:cd7b42c99ff8 | 10 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00108832.pdf |
gregeric | 1:cd7b42c99ff8 | 11 | * Table 15 has GPIOx AFx pinouts |
gregeric | 1:cd7b42c99ff8 | 12 | * |
gregeric | 1:cd7b42c99ff8 | 13 | * TIM1_CH1: AF1 @ PA8, PE9 |
gregeric | 1:cd7b42c99ff8 | 14 | * TIM1_CH2: AF1 @ PA9, PE11 |
gregeric | 1:cd7b42c99ff8 | 15 | * |
gregeric | 1:cd7b42c99ff8 | 16 | * TIM2_CH1: AF1 @ PA0, PA5, PA15 |
gregeric | 1:cd7b42c99ff8 | 17 | * TIM2_CH2: AF1 @ PA1, PB3 |
gregeric | 1:cd7b42c99ff8 | 18 | * |
gregeric | 1:cd7b42c99ff8 | 19 | * TIM3_CH1: AF2 @ PA6, PB4, PC6, PE3 |
gregeric | 1:cd7b42c99ff8 | 20 | * TIM3_CH2: AF2 @ PA7, PB5, PC7, PE4 |
gregeric | 1:cd7b42c99ff8 | 21 | * |
gregeric | 1:cd7b42c99ff8 | 22 | * TIM4_CH1: AF2 @ PB6, PD12 |
gregeric | 1:cd7b42c99ff8 | 23 | * TIM4_CH2: AF2 @ PB7, PD13 |
gregeric | 1:cd7b42c99ff8 | 24 | * |
gregeric | 1:cd7b42c99ff8 | 25 | * TIM5_CH1: AF2 @ PA0, PF6 |
gregeric | 1:cd7b42c99ff8 | 26 | * TIM5_CH2: AF2 @ PA1, PF7 |
gregeric | 1:cd7b42c99ff8 | 27 | * |
gregeric | 1:cd7b42c99ff8 | 28 | */ |
gregeric | 1:cd7b42c99ff8 | 29 | |
gregeric | 1:cd7b42c99ff8 | 30 | #ifdef TARGET_STM32L4 |
gregeric | 1:cd7b42c99ff8 | 31 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
gregeric | 1:cd7b42c99ff8 | 32 | { |
gregeric | 1:cd7b42c99ff8 | 33 | GPIO_InitTypeDef GPIO_InitStruct; |
gregeric | 1:cd7b42c99ff8 | 34 | |
gregeric | 1:cd7b42c99ff8 | 35 | if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8 |
gregeric | 1:cd7b42c99ff8 | 36 | __TIM1_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 37 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 38 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
gregeric | 1:cd7b42c99ff8 | 39 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 40 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 41 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 42 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
gregeric | 1:cd7b42c99ff8 | 43 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 44 | } |
gregeric | 1:cd7b42c99ff8 | 45 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
gregeric | 1:cd7b42c99ff8 | 46 | __TIM2_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 47 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 48 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
gregeric | 1:cd7b42c99ff8 | 49 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 50 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 51 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 52 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
gregeric | 1:cd7b42c99ff8 | 53 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 54 | } |
gregeric | 1:cd7b42c99ff8 | 55 | else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4 |
gregeric | 1:cd7b42c99ff8 | 56 | __TIM3_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 57 | __GPIOB_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 58 | GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
gregeric | 1:cd7b42c99ff8 | 59 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 60 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 61 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 62 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
gregeric | 1:cd7b42c99ff8 | 63 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 64 | } |
gregeric | 1:cd7b42c99ff8 | 65 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
gregeric | 1:cd7b42c99ff8 | 66 | __TIM4_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 67 | __GPIOB_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 68 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
gregeric | 1:cd7b42c99ff8 | 69 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 70 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 71 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 72 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
gregeric | 1:cd7b42c99ff8 | 73 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 74 | } |
gregeric | 1:cd7b42c99ff8 | 75 | else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this |
gregeric | 1:cd7b42c99ff8 | 76 | __TIM5_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 77 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 78 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
gregeric | 1:cd7b42c99ff8 | 79 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 80 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 81 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 82 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM5; |
gregeric | 1:cd7b42c99ff8 | 83 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 84 | } |
gregeric | 1:cd7b42c99ff8 | 85 | } |
gregeric | 1:cd7b42c99ff8 | 86 | #endif |