on test and not completed based on nucleo_hello_encoder library

Dependencies:   mbed

Fork of Nucleo_Hello_Encoder by David Lowe

Files at this revision

API Documentation at this revision

Comitter:
c128
Date:
Wed Sep 30 08:15:19 2015 +0000
Parent:
3:5c895f9199d6
Commit message:
.

Changed in this revision

Encoder/CounterInit.cpp Show diff for this revision Revisions of this file
Encoder/CounterMspInitF4.cpp Show diff for this revision Revisions of this file
Encoder/Encoder.h Show annotated file Show diff for this revision Revisions of this file
Encoder/EncoderInit.cpp Show annotated file Show diff for this revision Revisions of this file
Encoder/EncoderMspInitF4.cpp Show annotated file Show diff for this revision Revisions of this file
Encoder/counter.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Encoder/CounterInit.cpp	Tue Sep 29 06:56:28 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-#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; //TIM_COUNTERMODE_CENTERALIGNED3 for up/down ...
-    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;
-
-}
-
--- a/Encoder/CounterMspInitF4.cpp	Tue Sep 29 06:56:28 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +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.
- *
- */
- // STM32 F4 HAL https://github.com/mbedmicro/mbed/blob/03fff9249b0ca5efb2e12506ce8fb3e6cc258d9d/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal_tim.h
-
-#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;   //test one pin for now without direction
-        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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder/Encoder.h	Wed Sep 30 08:15:19 2015 +0000
@@ -0,0 +1,6 @@
+#ifndef ENCODER_H
+#define ENCODER_H
+
+void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_IC_InitTypeDef counterSet, TIM_TypeDef *TIMx, uint32_t maxcount, uint32_t encmode);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder/EncoderInit.cpp	Wed Sep 30 08:15:19 2015 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+
+void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_IC_InitTypeDef counterSet, 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;
+
+    if (encmode == 0) {
+        printf("counterset");
+    counterSet.ICFilter = 0x0;
+    counterSet.ICPolarity = TIM_ICPOLARITY_RISING;
+    counterSet.ICPrescaler = TIM_ICPSC_DIV1;
+    counterSet.ICSelection = TIM_ICSELECTION_DIRECTTI;
+    } else {
+        printf("encodermode");
+    encoder.EncoderMode = encmode;
+
+    encoder.IC1Filter = 0x0;
+    encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
+    encoder.IC1Prescaler = TIM_ICPSC_DIV1;
+    encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;
+
+    encoder.IC2Filter = 0x0;
+    encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
+    encoder.IC2Prescaler = TIM_ICPSC_DIV1;
+    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) {}
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder/EncoderMspInitF4.cpp	Wed Sep 30 08:15:19 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/counter.h	Tue Sep 29 06:56:28 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6 +0,0 @@
-#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	Tue Sep 29 06:56:28 2015 +0000
+++ b/main.cpp	Wed Sep 30 08:15:19 2015 +0000
@@ -19,25 +19,36 @@
  */
 
 #include "mbed.h"
-#include "counter.h"
+#include "Encoder.h"
 
-TIM_IC_InitTypeDef counterSet1;
-TIM_HandleTypeDef timer1;
- 
+TIM_Encoder_InitTypeDef encoder2;
+TIM_HandleTypeDef timer2;
+TIM_IC_InitTypeDef counterSet2;
+
 int main()
 {
-
-    CounterInit(counterSet1, timer1, TIM1, 0xffff);
-   
+    //examples
+    
+    //counting on A-input only, 2 ticks per cycle, rolls over at 100
+    //EncoderInit(encoder1, timer1, TIM1, 0xffff, TIM_ENCODERMODE_TI1);
     
-    printf("STM HAL encoder demo\n\r");
-    
-    while(1) {
+    //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
+    EncoderInit(encoder2, timer2, counterSet2, TIM2, 0xffff, 0);
+    TIM2->SMCR |= 0x0007; // Ext. clk mode 1
+    TIM2->SMCR |= 0x0060; // TI2FP2 as ext. clock
+    TIM2->CR1 |= 0x0001; // enable counting
+    TIM2->CNT = 0; // reset counter
+
+ while (1) {
         int16_t count1;
-        
-        count1=TIM1->CNT; //OK 401 411 TICKER 030
+        count1=TIM2->CNT; 
 
         printf("%d\r\n", count1);
         wait(1.0);
-    }
+   if (GPIOE->IDR & 0x01) TIM2->CNT = 0; // reset counter
+   if (1 * TIM_CR1_DIR) TIM2->CR1 |= 0;  // 0 - count up; 1 - count down
+
+ //if (GPIOE->IDR & 0x02) TIM2->CR1 |= 0x01; // enable counter
+ //if (GPIOE->IDR & 0x04) TIM2->CR1 &= ~0x01; // disable counter
+ }
 }