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

EncoderMspInitF4.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  * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
00011  * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf
00012  *
00013  * TIM1_CH1: AF1 @ PA_8, PE_9 
00014  * TIM1_CH2: AF1 @ PA_9, PE_11 
00015  *
00016  * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8*     *F446 only
00017  * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9*            *F446 only
00018  *
00019  * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6
00020  * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7
00021  *
00022  * TIM4_CH1: AF2 @ PB_6, PD_12
00023  * TIM4_CH2: AF2 @ PB_7, PD_13
00024  *
00025  * TIM5_CH1: AF2 @ PA_0*    *TIM5 used by mbed system ticker so unavailable
00026  * TIM5_CH2: AF2 @ PA_1*
00027  *
00028  */
00029 
00030 #ifdef TARGET_STM32F4
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 }
00076 #endif