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/EncoderMspInitL4.cpp@0:789510d98ade, 2017-03-08 (annotated)
- Committer:
- tonnyleonard
- Date:
- Wed Mar 08 16:59:48 2017 +0000
- Revision:
- 0:789510d98ade
- Child:
- 1:a611582bffa7
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 stm32f4xx_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/DM00108832.pdf |
tonnyleonard | 0:789510d98ade | 11 | * Table 15 has GPIOx AFx pinouts |
tonnyleonard | 0:789510d98ade | 12 | * |
tonnyleonard | 0:789510d98ade | 13 | * TIM1_CH1: AF1 @ PA8, PE9 |
tonnyleonard | 0:789510d98ade | 14 | * TIM1_CH2: AF1 @ PA9, PE11 |
tonnyleonard | 0:789510d98ade | 15 | * |
tonnyleonard | 0:789510d98ade | 16 | * TIM2_CH1: AF1 @ PA0, PA5, PA15 |
tonnyleonard | 0:789510d98ade | 17 | * TIM2_CH2: AF1 @ PA1, PB3 |
tonnyleonard | 0:789510d98ade | 18 | * |
tonnyleonard | 0:789510d98ade | 19 | * TIM3_CH1: AF2 @ PA6, PB4, PC6, PE3 |
tonnyleonard | 0:789510d98ade | 20 | * TIM3_CH2: AF2 @ PA7, PB5, PC7, PE4 |
tonnyleonard | 0:789510d98ade | 21 | * |
tonnyleonard | 0:789510d98ade | 22 | * TIM4_CH1: AF2 @ PB6, PD12 |
tonnyleonard | 0:789510d98ade | 23 | * TIM4_CH2: AF2 @ PB7, PD13 |
tonnyleonard | 0:789510d98ade | 24 | * |
tonnyleonard | 0:789510d98ade | 25 | * TIM5_CH1: AF2 @ PA0, PF6 |
tonnyleonard | 0:789510d98ade | 26 | * TIM5_CH2: AF2 @ PA1, PF7 |
tonnyleonard | 0:789510d98ade | 27 | * |
tonnyleonard | 0:789510d98ade | 28 | */ |
tonnyleonard | 0:789510d98ade | 29 | |
tonnyleonard | 0:789510d98ade | 30 | #ifdef TARGET_STM32L4 |
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_AF1_TIM1; |
tonnyleonard | 0:789510d98ade | 43 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 44 | } |
tonnyleonard | 0:789510d98ade | 45 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
tonnyleonard | 0:789510d98ade | 46 | __TIM2_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 47 | __GPIOA_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 48 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
tonnyleonard | 0:789510d98ade | 49 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 50 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 51 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 52 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
tonnyleonard | 0:789510d98ade | 53 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 54 | } |
tonnyleonard | 0:789510d98ade | 55 | else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4 |
tonnyleonard | 0:789510d98ade | 56 | __TIM3_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 57 | __GPIOB_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 58 | GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
tonnyleonard | 0:789510d98ade | 59 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 60 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 61 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 62 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
tonnyleonard | 0:789510d98ade | 63 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 64 | } |
tonnyleonard | 0:789510d98ade | 65 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
tonnyleonard | 0:789510d98ade | 66 | __TIM4_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 67 | __GPIOB_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 68 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
tonnyleonard | 0:789510d98ade | 69 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 70 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 71 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 72 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
tonnyleonard | 0:789510d98ade | 73 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 74 | } |
tonnyleonard | 0:789510d98ade | 75 | else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this |
tonnyleonard | 0:789510d98ade | 76 | __TIM5_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 77 | __GPIOA_CLK_ENABLE(); |
tonnyleonard | 0:789510d98ade | 78 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
tonnyleonard | 0:789510d98ade | 79 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
tonnyleonard | 0:789510d98ade | 80 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
tonnyleonard | 0:789510d98ade | 81 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
tonnyleonard | 0:789510d98ade | 82 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM5; |
tonnyleonard | 0:789510d98ade | 83 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
tonnyleonard | 0:789510d98ade | 84 | } |
tonnyleonard | 0:789510d98ade | 85 | } |
tonnyleonard | 0:789510d98ade | 86 | #endif |