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

EncoderMspInitF0.cpp

00001 #include "mbed.h"
00002 /*
00003  * HAL_TIM_Encoder_MspInit()
00004  * Overrides the __weak function stub in stm32f0xx_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/DM00088500.pdf
00011  * Table 11 has GPIO alternate function pinout mappings.
00012  *
00013  * TIM1_CH1: AF2 @ PA_8 - TIM1 used as system ticker under mbed, so unavailable
00014  * TIM1_CH2: AF2 @ PA_9
00015  *
00016  * TIM3_CH1: AF1 @ PA_6, PB_4; AF0 @ PC_6*  *only for F030xC devices
00017  * TIM3_CH2: AF1 @ PA_7, PB_5; AF0 @ PC_7*
00018  *
00019  */
00020 
00021 #ifdef TARGET_STM32F0
00022 void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
00023 {
00024     GPIO_InitTypeDef GPIO_InitStruct;
00025 
00026     if (htim->Instance == TIM3) { //PB_4 PB_5 = Nucleo D5 D4
00027         __TIM3_CLK_ENABLE();
00028         __GPIOB_CLK_ENABLE();
00029         GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
00030         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00031         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00032         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00033         GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
00034         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00035     }
00036 }
00037 #endif