hi
Fork of Nucleo_Hello_Encoder by
Encoder/EncoderMspInitF4.cpp@0:ee5cb967aa17, 2015-01-10 (annotated)
- Committer:
- gregeric
- Date:
- Sat Jan 10 20:06:07 2015 +0000
- Revision:
- 0:ee5cb967aa17
- Child:
- 1:cd7b42c99ff8
Hello World example using the STM32's timer hardware interface, no interrupts.; ; Supporting Nucleo F030, F401, F411 (others can be added - L0xx included but untested).; ; Encoder with TIM4 not working on F411, OK on F401?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gregeric | 0:ee5cb967aa17 | 1 | #include "mbed.h" |
gregeric | 0:ee5cb967aa17 | 2 | /* |
gregeric | 0:ee5cb967aa17 | 3 | * HAL_TIM_Encoder_MspInit() |
gregeric | 0:ee5cb967aa17 | 4 | * Overrides the __weak function stub in stm32f4xx_hal_tim.h |
gregeric | 0:ee5cb967aa17 | 5 | * |
gregeric | 0:ee5cb967aa17 | 6 | * Edit the below for your preferred pin wiring & pullup/down |
gregeric | 0:ee5cb967aa17 | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
gregeric | 0:ee5cb967aa17 | 8 | * Encoder A&B outputs connected directly to GPIOs. |
gregeric | 0:ee5cb967aa17 | 9 | * |
gregeric | 0:ee5cb967aa17 | 10 | */ |
gregeric | 0:ee5cb967aa17 | 11 | |
gregeric | 0:ee5cb967aa17 | 12 | #ifdef TARGET_STM32F4 |
gregeric | 0:ee5cb967aa17 | 13 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
gregeric | 0:ee5cb967aa17 | 14 | { |
gregeric | 0:ee5cb967aa17 | 15 | GPIO_InitTypeDef GPIO_InitStruct; |
gregeric | 0:ee5cb967aa17 | 16 | |
gregeric | 0:ee5cb967aa17 | 17 | if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8, poss PB0 PB1 usable too (complementary?) |
gregeric | 0:ee5cb967aa17 | 18 | __TIM1_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 19 | __GPIOA_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 20 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
gregeric | 0:ee5cb967aa17 | 21 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 0:ee5cb967aa17 | 22 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 0:ee5cb967aa17 | 23 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 0:ee5cb967aa17 | 24 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
gregeric | 0:ee5cb967aa17 | 25 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 0:ee5cb967aa17 | 26 | } |
gregeric | 0:ee5cb967aa17 | 27 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1, PA5 PB3 = D13 D3 poss too |
gregeric | 0:ee5cb967aa17 | 28 | __TIM2_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 29 | __GPIOA_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 30 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
gregeric | 0:ee5cb967aa17 | 31 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 0:ee5cb967aa17 | 32 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 0:ee5cb967aa17 | 33 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 0:ee5cb967aa17 | 34 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
gregeric | 0:ee5cb967aa17 | 35 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 0:ee5cb967aa17 | 36 | } |
gregeric | 0:ee5cb967aa17 | 37 | else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4, PA6 PA7 & PC6 PC7 also an option for Nucleo |
gregeric | 0:ee5cb967aa17 | 38 | __TIM3_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 39 | __GPIOB_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 40 | GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
gregeric | 0:ee5cb967aa17 | 41 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 0:ee5cb967aa17 | 42 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 0:ee5cb967aa17 | 43 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 0:ee5cb967aa17 | 44 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
gregeric | 0:ee5cb967aa17 | 45 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 0:ee5cb967aa17 | 46 | } |
gregeric | 0:ee5cb967aa17 | 47 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
gregeric | 0:ee5cb967aa17 | 48 | __TIM4_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 49 | __GPIOB_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 50 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
gregeric | 0:ee5cb967aa17 | 51 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 0:ee5cb967aa17 | 52 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 0:ee5cb967aa17 | 53 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 0:ee5cb967aa17 | 54 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
gregeric | 0:ee5cb967aa17 | 55 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
gregeric | 0:ee5cb967aa17 | 56 | } |
gregeric | 0:ee5cb967aa17 | 57 | else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this |
gregeric | 0:ee5cb967aa17 | 58 | __TIM5_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 59 | __GPIOA_CLK_ENABLE(); |
gregeric | 0:ee5cb967aa17 | 60 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
gregeric | 0:ee5cb967aa17 | 61 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
gregeric | 0:ee5cb967aa17 | 62 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
gregeric | 0:ee5cb967aa17 | 63 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
gregeric | 0:ee5cb967aa17 | 64 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM5; |
gregeric | 0:ee5cb967aa17 | 65 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
gregeric | 0:ee5cb967aa17 | 66 | } |
gregeric | 0:ee5cb967aa17 | 67 | } |
gregeric | 0:ee5cb967aa17 | 68 | #endif |