Hello World example for interfacing up to four rotary encoders to the STM32's timer/counter hardware, without interrupts.

Dependencies:   mbed-dev

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EncoderMspInitL4.cpp Source File

EncoderMspInitL4.cpp

00001 #include "mbed.h"
00002 /*
00003  * HAL_TIM_Encoder_MspInit()
00004  * Overrides the __weak function stub in stm32f4xx_hal_tim.h
00005  *
00006  * Edit the below for your preferred pin wiring & pullup/down
00007  * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
00008  * Encoder A&B outputs connected directly to GPIOs.
00009  *
00010  * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00108832.pdf
00011  * Table 15 has GPIOx AFx pinouts
00012  *
00013  * TIM1_CH1: AF1 @ PA8, PE9
00014  * TIM1_CH2: AF1 @ PA9, PE11
00015  *
00016  * TIM2_CH1: AF1 @ PA0, PA5, PA15
00017  * TIM2_CH2: AF1 @ PA1, PB3
00018  *
00019  * TIM3_CH1: AF2 @ PA6, PB4, PC6, PE3
00020  * TIM3_CH2: AF2 @ PA7, PB5, PC7, PE4
00021  *
00022  * TIM4_CH1: AF2 @ PB6, PD12
00023  * TIM4_CH2: AF2 @ PB7, PD13
00024  *
00025  * TIM5_CH1: AF2 @ PA0, PF6
00026  * TIM5_CH2: AF2 @ PA1, PF7
00027  *
00028  */
00029 
00030 #ifdef TARGET_STM32L4
00031 void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
00032 {
00033     GPIO_InitTypeDef GPIO_InitStruct;
00034 
00035     if (htim->Instance == TIM1) {       //PA8 PA9 = Nucleo D7 D8
00036         __TIM1_CLK_ENABLE();
00037         __GPIOA_CLK_ENABLE();
00038         GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
00039         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00040         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00041         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00042         GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
00043         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00044     }
00045     else if (htim->Instance == TIM2) {  //PA0 PA1 = Nucleo A0 A1
00046         __TIM2_CLK_ENABLE();
00047         __GPIOA_CLK_ENABLE();
00048         GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
00049         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00050         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00051         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00052         GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
00053         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00054     }
00055     else if (htim->Instance == TIM3) {  //PB4 PB5 = Nucleo D5 D4
00056         __TIM3_CLK_ENABLE();
00057         __GPIOB_CLK_ENABLE();
00058         GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
00059         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00060         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00061         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00062         GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
00063         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00064     }
00065     else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7
00066         __TIM4_CLK_ENABLE();
00067         __GPIOB_CLK_ENABLE();
00068         GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
00069         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00070         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00071         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00072         GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
00073         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00074     }
00075     else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this
00076         __TIM5_CLK_ENABLE();
00077         __GPIOA_CLK_ENABLE();
00078         GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
00079         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00080         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00081         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00082         GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
00083         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00084     }
00085 }
00086 #endif