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 EncoderMspInitL1.cpp Source File

EncoderMspInitL1.cpp

00001 #include "mbed.h"
00002 /*
00003  * HAL_TIM_Encoder_MspInit()
00004  * Overrides the __weak function stub in stm32l4xx_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/DM00098321.pdf
00011  * Table 9 has GPIO alternate function pinout mappings.
00012  *
00013  * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PE_9
00014  * TIM2_CH2: AF1 @ PA_1, PB_3, PE_10
00015  *
00016  * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6, PE_3
00017  * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7, PE_4
00018  *
00019  * TIM4_CH1: AF2 @ PB_6, PD_12
00020  * TIM4_CH2: AF2 @ PB_7, PD_13
00021  *
00022  * TIM5_CH1: AF2 @ PA_0*    *TIM5 used by mbed system ticker so unavailable
00023  * TIM5_CH2: AF2 @ PA_1*
00024  *
00025  */
00026 
00027 #ifdef TARGET_STM32L1
00028 void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
00029 {
00030     GPIO_InitTypeDef GPIO_InitStruct;
00031 
00032     if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1
00033         __TIM2_CLK_ENABLE();
00034         __GPIOA_CLK_ENABLE();
00035         GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
00036         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00037         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00038         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00039         GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
00040         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00041     }
00042     else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4
00043         __TIM3_CLK_ENABLE();
00044         __GPIOB_CLK_ENABLE();
00045         GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
00046         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00047         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00048         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00049         GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
00050         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00051     }
00052     else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7
00053         __TIM4_CLK_ENABLE();
00054         __GPIOB_CLK_ENABLE();
00055         GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
00056         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00057         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00058         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00059         GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
00060         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00061     }
00062 }
00063 #endif