Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Encoder/EncoderMspInitL1.cpp@0:e3161ef4ffd9, 2019-09-16 (annotated)
- Committer:
- Ferryy
- Date:
- Mon Sep 16 10:17:12 2019 +0000
- Revision:
- 0:e3161ef4ffd9
Working version of rotary encoder readout software.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Ferryy | 0:e3161ef4ffd9 | 1 | #include "mbed.h" |
| Ferryy | 0:e3161ef4ffd9 | 2 | /* |
| Ferryy | 0:e3161ef4ffd9 | 3 | * HAL_TIM_Encoder_MspInit() |
| Ferryy | 0:e3161ef4ffd9 | 4 | * Overrides the __weak function stub in stm32l4xx_hal_tim.h |
| Ferryy | 0:e3161ef4ffd9 | 5 | * |
| Ferryy | 0:e3161ef4ffd9 | 6 | * Edit the below for your preferred pin wiring & pullup/down |
| Ferryy | 0:e3161ef4ffd9 | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
| Ferryy | 0:e3161ef4ffd9 | 8 | * Encoder A&B outputs connected directly to GPIOs. |
| Ferryy | 0:e3161ef4ffd9 | 9 | * |
| Ferryy | 0:e3161ef4ffd9 | 10 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00098321.pdf |
| Ferryy | 0:e3161ef4ffd9 | 11 | * Table 9 has GPIO alternate function pinout mappings. |
| Ferryy | 0:e3161ef4ffd9 | 12 | * |
| Ferryy | 0:e3161ef4ffd9 | 13 | * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PE_9 |
| Ferryy | 0:e3161ef4ffd9 | 14 | * TIM2_CH2: AF1 @ PA_1, PB_3, PE_10 |
| Ferryy | 0:e3161ef4ffd9 | 15 | * |
| Ferryy | 0:e3161ef4ffd9 | 16 | * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6, PE_3 |
| Ferryy | 0:e3161ef4ffd9 | 17 | * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7, PE_4 |
| Ferryy | 0:e3161ef4ffd9 | 18 | * |
| Ferryy | 0:e3161ef4ffd9 | 19 | * TIM4_CH1: AF2 @ PB_6, PD_12 |
| Ferryy | 0:e3161ef4ffd9 | 20 | * TIM4_CH2: AF2 @ PB_7, PD_13 |
| Ferryy | 0:e3161ef4ffd9 | 21 | * |
| Ferryy | 0:e3161ef4ffd9 | 22 | * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable |
| Ferryy | 0:e3161ef4ffd9 | 23 | * TIM5_CH2: AF2 @ PA_1* |
| Ferryy | 0:e3161ef4ffd9 | 24 | * |
| Ferryy | 0:e3161ef4ffd9 | 25 | */ |
| Ferryy | 0:e3161ef4ffd9 | 26 | |
| Ferryy | 0:e3161ef4ffd9 | 27 | #ifdef TARGET_STM32L1 |
| Ferryy | 0:e3161ef4ffd9 | 28 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
| Ferryy | 0:e3161ef4ffd9 | 29 | { |
| Ferryy | 0:e3161ef4ffd9 | 30 | GPIO_InitTypeDef GPIO_InitStruct; |
| Ferryy | 0:e3161ef4ffd9 | 31 | |
| Ferryy | 0:e3161ef4ffd9 | 32 | if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
| Ferryy | 0:e3161ef4ffd9 | 33 | __TIM2_CLK_ENABLE(); |
| Ferryy | 0:e3161ef4ffd9 | 34 | __GPIOA_CLK_ENABLE(); |
| Ferryy | 0:e3161ef4ffd9 | 35 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
| Ferryy | 0:e3161ef4ffd9 | 36 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| Ferryy | 0:e3161ef4ffd9 | 37 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| Ferryy | 0:e3161ef4ffd9 | 38 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| Ferryy | 0:e3161ef4ffd9 | 39 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
| Ferryy | 0:e3161ef4ffd9 | 40 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| Ferryy | 0:e3161ef4ffd9 | 41 | } |
| Ferryy | 0:e3161ef4ffd9 | 42 | // else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4 |
| Ferryy | 0:e3161ef4ffd9 | 43 | // __TIM3_CLK_ENABLE(); |
| Ferryy | 0:e3161ef4ffd9 | 44 | // __GPIOB_CLK_ENABLE(); |
| Ferryy | 0:e3161ef4ffd9 | 45 | // GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
| Ferryy | 0:e3161ef4ffd9 | 46 | // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| Ferryy | 0:e3161ef4ffd9 | 47 | // GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
| Ferryy | 0:e3161ef4ffd9 | 48 | // GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| Ferryy | 0:e3161ef4ffd9 | 49 | // GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
| Ferryy | 0:e3161ef4ffd9 | 50 | // HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| Ferryy | 0:e3161ef4ffd9 | 51 | // } |
| Ferryy | 0:e3161ef4ffd9 | 52 | // else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
| Ferryy | 0:e3161ef4ffd9 | 53 | // __TIM4_CLK_ENABLE(); |
| Ferryy | 0:e3161ef4ffd9 | 54 | // __GPIOB_CLK_ENABLE(); |
| Ferryy | 0:e3161ef4ffd9 | 55 | // GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
| Ferryy | 0:e3161ef4ffd9 | 56 | // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| Ferryy | 0:e3161ef4ffd9 | 57 | // GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
| Ferryy | 0:e3161ef4ffd9 | 58 | // GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
| Ferryy | 0:e3161ef4ffd9 | 59 | // GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
| Ferryy | 0:e3161ef4ffd9 | 60 | // HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| Ferryy | 0:e3161ef4ffd9 | 61 | // } |
| Ferryy | 0:e3161ef4ffd9 | 62 | } |
| Ferryy | 0:e3161ef4ffd9 | 63 | #endif |