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

EncoderMspInitF1.cpp

00001 #include "mbed.h"
00002 /*
00003  * HAL_TIM_Encoder_MspInit()
00004  * Overrides the __weak function stub in stm32f1xx_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/CD00161566.pdf
00011  * Table 5 has GPIO alternate function pinout mappings.
00012  *
00013  * TIM1_CH1: default PA_8, remap PE_9
00014  * TIM1_CH2: default PA_9, remap PE_11
00015  *
00016  * TIM2_CH1: default PA_0 
00017  * TIM2_CH2: default PA_1, remap PB_3
00018  *
00019  * TIM3_CH1: default PA_6, remap PB_4, PC_6
00020  * TIM3_CH2: default PA_7, remap PB_5, PC_7
00021  *
00022  * TIM4_CH1: default PB_6, remap PD_12
00023  * TIM4_CH2: default PB_7, remap PD_13
00024  *
00025  * NB one of these timers will be the employed by mbed as systick, unavailable as encoder.
00026  */
00027 
00028 #ifdef TARGET_STM32F1
00029 void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
00030 {
00031     GPIO_InitTypeDef GPIO_InitStruct;
00032 
00033     if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8
00034         __TIM1_CLK_ENABLE();
00035         __GPIOA_CLK_ENABLE();
00036         GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
00037         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00038         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00039         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00040 //        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
00041         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00042     }
00043     else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1
00044         __TIM2_CLK_ENABLE();
00045         __GPIOA_CLK_ENABLE();
00046         GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
00047         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00048         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00049         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00050 //        GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
00051         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00052     }
00053     else if (htim->Instance == TIM3) { //PA6 PA7 = Nucleo D12 D11
00054         __TIM3_CLK_ENABLE();
00055         __GPIOA_CLK_ENABLE();
00056         GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
00057         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00058         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00059         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00060 //        GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
00061         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00062     }
00063     else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO PB7
00064         __TIM4_CLK_ENABLE();
00065         __GPIOB_CLK_ENABLE();
00066         GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
00067         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00068         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00069         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00070 //        GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
00071         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00072     }
00073 }
00074 #endif