on test and not completed based on nucleo_hello_encoder library
Fork of Nucleo_Hello_Encoder by
Revision 1:3d2fffa6e19f, committed 2015-09-29
- Comitter:
- c128
- Date:
- Tue Sep 29 06:42:15 2015 +0000
- Parent:
- 0:ee5cb967aa17
- Child:
- 2:70f92ce7d983
- Commit message:
- on test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Encoder/CounterInit.cpp Tue Sep 29 06:42:15 2015 +0000 @@ -0,0 +1,18 @@ +#include "mbed.h" + +void CounterInit(TIM_IC_InitTypeDef counterSet, TIM_HandleTypeDef timer, TIM_TypeDef *TIMx, uint32_t maxcount) +{ + timer.Instance = TIMx; + timer.Init.Period = maxcount; + timer.Init.CounterMode = TIM_COUNTERMODE_UP; + timer.Init.Prescaler = 0; + timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + timer.Init.RepetitionCounter = 0; + + counterSet.ICFilter = 0; + counterSet.ICPolarity = TIM_ICPOLARITY_RISING; + counterSet.ICPrescaler = TIM_ICPSC_DIV1; + counterSet.ICSelection = TIM_ICSELECTION_DIRECTTI; + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Encoder/CounterMspInitF4.cpp Tue Sep 29 06:42:15 2015 +0000 @@ -0,0 +1,68 @@ +#include "mbed.h" +/* + * HAL_TIM_Encoder_MspInit() + * Overrides the __weak function stub in stm32f4xx_hal_tim.h + * + * Edit the below for your preferred pin wiring & pullup/down + * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. + * Encoder A&B outputs connected directly to GPIOs. + * + */ + +#ifdef TARGET_STM32F4 +void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8, poss PB0 PB1 usable too (complementary?) + __TIM1_CLK_ENABLE(); + __GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_8;// | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1, PA5 PB3 = D13 D3 poss too + __TIM2_CLK_ENABLE(); + __GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4, PA6 PA7 & PC6 PC7 also an option for Nucleo + __TIM3_CLK_ENABLE(); + __GPIOB_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + } + else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 + __TIM4_CLK_ENABLE(); + __GPIOB_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + } + else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this + __TIM5_CLK_ENABLE(); + __GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF2_TIM5; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } +} +#endif \ No newline at end of file
--- a/Encoder/Encoder.h Sat Jan 10 20:06:07 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -#ifndef ENCODER_H -#define ENCODER_H - -void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_TypeDef *TIMx, uint32_t maxcount, uint32_t encmode); - -#endif \ No newline at end of file
--- a/Encoder/EncoderInit.cpp Sat Jan 10 20:06:07 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -#include "mbed.h" - -void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_TypeDef *TIMx, uint32_t maxcount, uint32_t encmode) -{ - timer.Instance = TIMx; - timer.Init.Period = maxcount; - timer.Init.CounterMode = TIM_COUNTERMODE_UP; - timer.Init.Prescaler = 0; - timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - - encoder.EncoderMode = encmode; - - encoder.IC1Filter = 0x0F; - encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; - encoder.IC1Prescaler = TIM_ICPSC_DIV4; - encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI; - - encoder.IC2Filter = 0x0F; - encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING; - encoder.IC2Prescaler = TIM_ICPSC_DIV4; - encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI; - - - if (HAL_TIM_Encoder_Init(&timer, &encoder) != HAL_OK) { - printf("Couldn't Init Encoder\r\n"); - while (1) {} - } - - if(HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1)!=HAL_OK) { - printf("Couldn't Start Encoder\r\n"); - while (1) {} - } -} -
--- a/Encoder/EncoderMspInitF0.cpp Sat Jan 10 20:06:07 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -#include "mbed.h" -/* - * HAL_TIM_Encoder_MspInit() - * Overrides the __weak function stub in stm32f4xx_hal_tim.h - * - * Edit the below for your preferred pin wiring & pullup/down - * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. - * Encoder A&B outputs connected directly to GPIOs. - * - * All Nucleos support encoders, from 030 up. - * On 030, there's only TIM3 @ AF1 PA6 PA7 (D11 D12) - */ - -#ifdef TARGET_STM32F0 -void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) -{ - GPIO_InitTypeDef GPIO_InitStruct; - - if (htim->Instance == TIM3) { //PA6 PA7 = Nucleo D12 D11 - __TIM3_CLK_ENABLE(); - __GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF1_TIM3; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - } -} -#endif \ No newline at end of file
--- a/Encoder/EncoderMspInitF4.cpp Sat Jan 10 20:06:07 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -#include "mbed.h" -/* - * HAL_TIM_Encoder_MspInit() - * Overrides the __weak function stub in stm32f4xx_hal_tim.h - * - * Edit the below for your preferred pin wiring & pullup/down - * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. - * Encoder A&B outputs connected directly to GPIOs. - * - */ - -#ifdef TARGET_STM32F4 -void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) -{ - GPIO_InitTypeDef GPIO_InitStruct; - - if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8, poss PB0 PB1 usable too (complementary?) - __TIM1_CLK_ENABLE(); - __GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - } - else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1, PA5 PB3 = D13 D3 poss too - __TIM2_CLK_ENABLE(); - __GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - } - else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4, PA6 PA7 & PC6 PC7 also an option for Nucleo - __TIM3_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - } - else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 - __TIM4_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - } - else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this - __TIM5_CLK_ENABLE(); - __GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF2_TIM5; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - } -} -#endif \ No newline at end of file
--- a/Encoder/EncoderMspInitL0.cpp Sat Jan 10 20:06:07 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -#include "mbed.h" -/* - * HAL_TIM_Encoder_MspInit() - * Overrides the __weak function stub in stm32f4xx_hal_tim.h - * - * Edit the below for your preferred pin wiring & pullup/down - * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. - * Encoder A&B outputs connected directly to GPIOs. - * - */ - -#ifdef TARGET_STM32L0 -void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) -{ - GPIO_InitTypeDef GPIO_InitStruct; - - if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 - __TIM2_CLK_ENABLE(); - __GPIOA_CLK_ENABLE(); - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - } -} -#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Encoder/counter.h Tue Sep 29 06:42:15 2015 +0000 @@ -0,0 +1,6 @@ +#ifndef COUNTER_H +#define COUNTER_H + +void CounterInit(TIM_IC_InitTypeDef counterSet, TIM_HandleTypeDef timer, TIM_TypeDef *TIMx, uint32_t maxcount); + +#endif \ No newline at end of file
--- a/main.cpp Sat Jan 10 20:06:07 2015 +0000 +++ b/main.cpp Tue Sep 29 06:42:15 2015 +0000 @@ -19,26 +19,26 @@ */ #include "mbed.h" -#include "Encoder.h" +#include "counter.h" -TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4; -TIM_HandleTypeDef timer1, timer2, timer3, timer4; +TIM_IC_InitTypeDef counterSet1; +TIM_HandleTypeDef timer1; int main() { //examples - + //EncoderInit(TIM_IC_InitTypeDef counterSet, TIM_HandleTypeDef timer, TIM_TypeDef *TIMx, uint32_t maxcount); //counting on A-input only, 2 ticks per cycle, rolls over at 100 - EncoderInit(encoder1, timer1, TIM1, 99, TIM_ENCODERMODE_TI1); + CounterInit(counterSet1, timer1, TIM1, 0xffff); //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count - EncoderInit(encoder2, timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); + //EncoderInit(encoder2, timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); //counting on B-input only, 2 ticks per cycle, full 16-bit count - EncoderInit(encoder3, timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2); + //EncoderInit(encoder3, timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2); //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count - EncoderInit(encoder4, timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12); + //EncoderInit(encoder4, timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12); //TIM5 is used by mbed for systick //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12); @@ -46,15 +46,14 @@ printf("STM HAL encoder demo\n\r"); while(1) { - int16_t count1=0, count3=0, count4=0; - int32_t count2=0, count5=0; + int16_t count1; count1=TIM1->CNT; //OK 401 411 TICKER 030 - count2=TIM2->CNT; //OK 401 411 N/A 030 - count3=TIM3->CNT; //OK 401 411 030 - count4=TIM4->CNT; //OK 401 NOK 411 N/A 030 - count5=TIM5->CNT; //TICKER 401 411 N/A 030 - printf("%d %d %d %d %d\r\n", count1, count2, count3, count4, count5); + //count2=TIM2->CNT; //OK 401 411 N/A 030 + //count3=TIM3->CNT; //OK 401 411 030 + //count4=TIM4->CNT; //OK 401 NOK 411 N/A 030 + //count5=TIM5->CNT; //TICKER 401 411 N/A 030 + printf("%d\r\n", count1); wait(1.0); } }