A program to monitor some parameters for a motor
Dependencies: mbed-dev BufferSerial
Thanks to David Lowe for https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/ which I adapted for the use of TIM2 32bit timer as an encoder reader on the Nucleo L432KC board.
Encoder/EncoderMspInitF3.cpp@0:789510d98ade, 2017-03-08 (annotated)
- Committer:
- tonnyleonard
- Date:
- Wed Mar 08 16:59:48 2017 +0000
- Revision:
- 0:789510d98ade
Adapting attempt to L432KC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tonnyleonard | 0:789510d98ade | 1 | #include "mbed.h" |
tonnyleonard | 0:789510d98ade | 2 | /* |
tonnyleonard | 0:789510d98ade | 3 | * HAL_TIM_Encoder_MspInit() |
tonnyleonard | 0:789510d98ade | 4 | * Overrides the __weak function stub in stm32f3xx_hal_tim.h |
tonnyleonard | 0:789510d98ade | 5 | * |
tonnyleonard | 0:789510d98ade | 6 | * Edit the below for your preferred pin wiring & pullup/down |
tonnyleonard | 0:789510d98ade | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
tonnyleonard | 0:789510d98ade | 8 | * Encoder A&B outputs connected directly to GPIOs. |
tonnyleonard | 0:789510d98ade | 9 | * |
tonnyleonard | 0:789510d98ade | 10 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00093333.pdf |
tonnyleonard | 0:789510d98ade | 11 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00097745.pdf |
tonnyleonard | 0:789510d98ade | 12 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00133117.pdf |
tonnyleonard | 0:789510d98ade | 13 | * Table 14 has GPIO alternate function pinout mappings. |
tonnyleonard | 0:789510d98ade | 14 | * |
tonnyleonard | 0:789510d98ade | 15 | * TIM1_CH1: AF2 @ PC_0, PE_9; AF6 @ PA_8 |
tonnyleonard | 0:789510d98ade | 16 | * TIM1_CH2: AF2 @ PC_1, PE_11; AF6 @ PA_9 |
tonnyleonard | 0:789510d98ade | 17 | * |
tonnyleonard | 0:789510d98ade | 18 | * (TIM2_CH1: AF1 @ PA_0, PA_5, PA_15; AF2 @ PD_3) TIM2 is the mbed system ticker, so unavailable as encoder. |
tonnyleonard | 0:789510d98ade | 19 | * (TIM2_CH2: AF1 @ PA_1, PB_3; AF2 @ PD_4) |
tonnyleonard | 0:789510d98ade | 20 | * |
tonnyleonard | 0:789510d98ade | 21 | * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6, PE_2 not for F302R8, OK @ F334R8 |
tonnyleonard | 0:789510d98ade | 22 | * TIM3_CH2: AF2 @ PA_4, PA_7, PB_5, PC_7, PE_3 |
tonnyleonard | 0:789510d98ade | 23 | * |
tonnyleonard | 0:789510d98ade | 24 | * TIM4_CH1: AF2 @ PB_6, PD_12; AF10 @ PA_11 not for both F302R8 & F334R8 |
tonnyleonard | 0:789510d98ade | 25 | * TIM4_CH2: AF2 @ PB_7, PD_13; AF10 @ PA_12 |
tonnyleonard | 0:789510d98ade | 26 | * |
tonnyleonard | 0:789510d98ade | 27 | */ |
tonnyleonard | 0:789510d98ade | 28 | |
tonnyleonard | 0:789510d98ade | 29 | #ifdef TARGET_STM32F3 |
tonnyleonard | 0:789510d98ade | 30 | |
tonnyleonard | 0:789510d98ade | 31 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
tonnyleonard | 0:789510d98ade | 32 | { |
tonnyleonard | 0:789510d98ade | 33 | GPIO_InitTypeDef GPIO_InitStruct; |
tonnyleonard | 0:789510d98ade | 34 | |
tonnyleonard | 0:789510d98ade | 35 | if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8 |
tonnyleonard | 0:789510d98ade | 36 | __TIM1_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 37 | __GPIOA_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 38 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
tonnyleonard | 0:789510d98ade | 39 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 40 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 41 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 42 | GPIO_InitStruct.Alternate = GPIO_AF6_TIM1; |
tonnyleonard | 0:789510d98ade | 43 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 44 | } |
tonnyleonard | 0:789510d98ade | 45 | #if 0 //TIM2 is the mbed system ticker |
tonnyleonard | 0:789510d98ade | 46 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
tonnyleonard | 0:789510d98ade | 47 | __TIM2_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 48 | __GPIOA_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 49 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
tonnyleonard | 0:789510d98ade | 50 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 51 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 52 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 53 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
tonnyleonard | 0:789510d98ade | 54 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 55 | } |
tonnyleonard | 0:789510d98ade | 56 | #endif |
tonnyleonard | 0:789510d98ade | 57 | #if defined TARGET_STM32F334R8 |
tonnyleonard | 0:789510d98ade | 58 | else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4 |
tonnyleonard | 0:789510d98ade | 59 | __TIM3_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 60 | __GPIOB_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 61 | GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
tonnyleonard | 0:789510d98ade | 62 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 63 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 64 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 65 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
tonnyleonard | 0:789510d98ade | 66 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 67 | } |
tonnyleonard | 0:789510d98ade | 68 | #endif |
tonnyleonard | 0:789510d98ade | 69 | #if 0 |
tonnyleonard | 0:789510d98ade | 70 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
tonnyleonard | 0:789510d98ade | 71 | __TIM4_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 72 | __GPIOB_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 73 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
tonnyleonard | 0:789510d98ade | 74 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 75 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 76 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 77 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
tonnyleonard | 0:789510d98ade | 78 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 79 | } |
tonnyleonard | 0:789510d98ade | 80 | #endif |
tonnyleonard | 0:789510d98ade | 81 | } |
tonnyleonard | 0:789510d98ade | 82 | #endif |