hi
Fork of Nucleo_Hello_Encoder by
Encoder/EncoderMspInitF3.cpp@2:8349e61b6152, 2016-09-01 (annotated)
- Committer:
- sakanakuuun
- Date:
- Thu Sep 01 06:37:22 2016 +0000
- Revision:
- 2:8349e61b6152
- Parent:
- 1:cd7b42c99ff8
x
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 stm32f3xx_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/DM00093333.pdf |
gregeric | 1:cd7b42c99ff8 | 11 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00097745.pdf |
gregeric | 1:cd7b42c99ff8 | 12 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00133117.pdf |
gregeric | 1:cd7b42c99ff8 | 13 | * Table 14 has GPIO alternate function pinout mappings. |
gregeric | 1:cd7b42c99ff8 | 14 | * |
gregeric | 1:cd7b42c99ff8 | 15 | * TIM1_CH1: AF2 @ PC_0, PE_9; AF6 @ PA_8 |
gregeric | 1:cd7b42c99ff8 | 16 | * TIM1_CH2: AF2 @ PC_1, PE_11; AF6 @ PA_9 |
gregeric | 1:cd7b42c99ff8 | 17 | * |
gregeric | 1:cd7b42c99ff8 | 18 | * (TIM2_CH1: AF1 @ PA_0, PA_5, PA_15; AF2 @ PD_3) TIM2 is the mbed system ticker, so unavailable as encoder. |
gregeric | 1:cd7b42c99ff8 | 19 | * (TIM2_CH2: AF1 @ PA_1, PB_3; AF2 @ PD_4) |
gregeric | 1:cd7b42c99ff8 | 20 | * |
gregeric | 1:cd7b42c99ff8 | 21 | * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6, PE_2 not for F302R8, OK @ F334R8 |
gregeric | 1:cd7b42c99ff8 | 22 | * TIM3_CH2: AF2 @ PA_4, PA_7, PB_5, PC_7, PE_3 |
gregeric | 1:cd7b42c99ff8 | 23 | * |
gregeric | 1:cd7b42c99ff8 | 24 | * TIM4_CH1: AF2 @ PB_6, PD_12; AF10 @ PA_11 not for both F302R8 & F334R8 |
gregeric | 1:cd7b42c99ff8 | 25 | * TIM4_CH2: AF2 @ PB_7, PD_13; AF10 @ PA_12 |
gregeric | 1:cd7b42c99ff8 | 26 | * |
gregeric | 1:cd7b42c99ff8 | 27 | */ |
gregeric | 1:cd7b42c99ff8 | 28 | |
gregeric | 1:cd7b42c99ff8 | 29 | #ifdef TARGET_STM32F3 |
gregeric | 1:cd7b42c99ff8 | 30 | |
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_AF6_TIM1; |
gregeric | 1:cd7b42c99ff8 | 43 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 44 | } |
gregeric | 1:cd7b42c99ff8 | 45 | #if 0 //TIM2 is the mbed system ticker |
gregeric | 1:cd7b42c99ff8 | 46 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
gregeric | 1:cd7b42c99ff8 | 47 | __TIM2_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 48 | __GPIOA_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 49 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
gregeric | 1:cd7b42c99ff8 | 50 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 51 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 52 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 53 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
gregeric | 1:cd7b42c99ff8 | 54 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 55 | } |
gregeric | 1:cd7b42c99ff8 | 56 | #endif |
gregeric | 1:cd7b42c99ff8 | 57 | #if defined TARGET_STM32F334R8 |
gregeric | 1:cd7b42c99ff8 | 58 | else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4 |
gregeric | 1:cd7b42c99ff8 | 59 | __TIM3_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 60 | __GPIOB_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 61 | GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
gregeric | 1:cd7b42c99ff8 | 62 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 63 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 64 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 65 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
gregeric | 1:cd7b42c99ff8 | 66 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 67 | } |
gregeric | 1:cd7b42c99ff8 | 68 | #endif |
gregeric | 1:cd7b42c99ff8 | 69 | #if 0 |
gregeric | 1:cd7b42c99ff8 | 70 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
gregeric | 1:cd7b42c99ff8 | 71 | __TIM4_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 72 | __GPIOB_CLK_ENABLE(); |
gregeric | 1:cd7b42c99ff8 | 73 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
gregeric | 1:cd7b42c99ff8 | 74 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 1:cd7b42c99ff8 | 75 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 1:cd7b42c99ff8 | 76 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 1:cd7b42c99ff8 | 77 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
gregeric | 1:cd7b42c99ff8 | 78 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 1:cd7b42c99ff8 | 79 | } |
gregeric | 1:cd7b42c99ff8 | 80 | #endif |
gregeric | 1:cd7b42c99ff8 | 81 | } |
gregeric | 1:cd7b42c99ff8 | 82 | #endif |