hi
Fork of Nucleo_Hello_Encoder by
Encoder/EncoderMspInitF1.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 stm32f1xx_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/CD00161566.pdf |
gregeric | 1:cd7b42c99ff8 | 11 | * Table 5 has GPIO alternate function pinout mappings. |
gregeric | 1:cd7b42c99ff8 | 12 | * |
gregeric | 1:cd7b42c99ff8 | 13 | * TIM1_CH1: default PA_8, remap PE_9 |
gregeric | 1:cd7b42c99ff8 | 14 | * TIM1_CH2: default PA_9, remap PE_11 |
gregeric | 1:cd7b42c99ff8 | 15 | * |
gregeric | 1:cd7b42c99ff8 | 16 | * TIM2_CH1: default PA_0 |
gregeric | 1:cd7b42c99ff8 | 17 | * TIM2_CH2: default PA_1, remap PB_3 |
gregeric | 1:cd7b42c99ff8 | 18 | * |
gregeric | 1:cd7b42c99ff8 | 19 | * TIM3_CH1: default PA_6, remap PB_4, PC_6 |
gregeric | 1:cd7b42c99ff8 | 20 | * TIM3_CH2: default PA_7, remap PB_5, PC_7 |
gregeric | 1:cd7b42c99ff8 | 21 | * |
gregeric | 1:cd7b42c99ff8 | 22 | * TIM4_CH1: default PB_6, remap PD_12 |
gregeric | 1:cd7b42c99ff8 | 23 | * TIM4_CH2: default PB_7, remap PD_13 |
gregeric | 1:cd7b42c99ff8 | 24 | * |
gregeric | 1:cd7b42c99ff8 | 25 | * NB one of these timers will be the employed by mbed as systick, unavailable as encoder. |
gregeric | 1:cd7b42c99ff8 | 26 | */ |
gregeric | 1:cd7b42c99ff8 | 27 | |
gregeric | 1:cd7b42c99ff8 | 28 | #ifdef TARGET_STM32F1 |
gregeric | 1:cd7b42c99ff8 | 29 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
gregeric | 1:cd7b42c99ff8 | 30 | { |
gregeric | 1:cd7b42c99ff8 | 31 | GPIO_InitTypeDef GPIO_InitStruct; |
gregeric | 1:cd7b42c99ff8 | 32 | |
gregeric | 1:cd7b42c99ff8 | 33 | if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8 |
gregeric | 1:cd7b42c99ff8 | 34 | __TIM1_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 35 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 36 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
gregeric | 1:cd7b42c99ff8 | 37 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 38 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 39 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 40 | // GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
gregeric | 1:cd7b42c99ff8 | 41 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 42 | } |
gregeric | 1:cd7b42c99ff8 | 43 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
gregeric | 1:cd7b42c99ff8 | 44 | __TIM2_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 45 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 46 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
gregeric | 1:cd7b42c99ff8 | 47 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 48 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 49 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 50 | // GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
gregeric | 1:cd7b42c99ff8 | 51 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 52 | } |
gregeric | 1:cd7b42c99ff8 | 53 | else if (htim->Instance == TIM3) { //PA6 PA7 = Nucleo D12 D11 |
gregeric | 1:cd7b42c99ff8 | 54 | __TIM3_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 55 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 56 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
gregeric | 1:cd7b42c99ff8 | 57 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 58 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 59 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 60 | // GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
gregeric | 1:cd7b42c99ff8 | 61 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 62 | } |
gregeric | 1:cd7b42c99ff8 | 63 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO PB7 |
gregeric | 1:cd7b42c99ff8 | 64 | __TIM4_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 65 | __GPIOB_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 66 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
gregeric | 1:cd7b42c99ff8 | 67 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 68 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 69 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 70 | // GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
gregeric | 1:cd7b42c99ff8 | 71 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 72 | } |
gregeric | 1:cd7b42c99ff8 | 73 | } |
gregeric | 1:cd7b42c99ff8 | 74 | #endif |