stm32_qenc16_0.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*!
  2. * Copyright (C) 2014 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/cm3/dev/stm/stm32_qenc16_0.c
  36. * \brief STM 32 bit quadrature encoder device 0 using 16-bit timer.
  37. *
  38. * Implement a 32 bit quadrature encode with an STM32 16-bit timer with at
  39. * least two channels. Quadrature pulses must be connected to Ch1/CH2.
  40. *
  41. * .._get() must be run before 1<<15 counts have elapsed, or otherwise
  42. * a revolution will be lost. No need to care if total range is
  43. * limited to < (1<<16) - 1. Some interrupt generated from encoder
  44. * signals, like timer overflow or index impulse may cause interrupt storms.
  45. * Using such interrupt as source for update is not recommended.
  46. *
  47. * Attention : Configured values for the used pins are not checked here!
  48. *
  49. * Typical usage:
  50. * \code
  51. * #include <arch/cm3/stm/stm32_qenc16.h>
  52. * stm32_qenc16_0_init();
  53. * ...
  54. * stm32_qenc16_0_update();
  55. * ...
  56. * value = stm32_qenc16_0_get();
  57. * ...
  58. * \endcode
  59. *
  60. * ..set() may be used e.g. to set the counter value to some know value when
  61. * some limit switch has been hit.
  62. *
  63. * \verbatim
  64. * $Id: stm32_qenc16_0.c 5638 2014-04-07 13:58:48Z u_bonnes $
  65. * \endverbatim
  66. */
  67. #include <cfg/arch.h>
  68. #include <arch/cm3/stm/stm32xxxx.h>
  69. #include <cfg/qenc.h>
  70. #include <sys/atom.h>
  71. #include <dev/gpio.h>
  72. #include <arch/cm3/stm/stm32_gpio.h>
  73. #include <dev/hwtimer_stm32.h>
  74. /* Only compile code if needed defines are given*/
  75. #if defined(STM32_QENC16_0_I_PORT) && defined(STM32_QENC16_0_I_PIN) &&\
  76. defined(STM32_QENC16_0_Q_PORT) && defined(STM32_QENC16_0_Q_PIN) &&\
  77. defined(STM32_QENC16_0_TIMER_ID)
  78. #if defined(STM32_QENC16_0_I_PORT)
  79. #define STM32_QENC16_I_PORT STM32_QENC16_0_I_PORT
  80. #endif
  81. #if defined(STM32_QENC16_0_I_PIN)
  82. #define STM32_QENC16_I_PIN STM32_QENC16_0_I_PIN
  83. #endif
  84. #if defined(STM32_QENC16_0_Q_PORT)
  85. #define STM32_QENC16_Q_PORT STM32_QENC16_0_Q_PORT
  86. #endif
  87. #if defined(STM32_QENC16_0_Q_PIN)
  88. #define STM32_QENC16_Q_PIN STM32_QENC16_0_Q_PIN
  89. #endif
  90. #if defined(STM32_QENC16_0_INVERT)
  91. #define STM32_QENC16_INVERT STM32_QENC16_0_INVERT
  92. #endif
  93. #if defined(STM32_QENC16_0_TIMER_ID)
  94. #undef STM32TIMER_ID
  95. #define STM32TIMER_ID STM32_QENC16_0_TIMER_ID
  96. #include <arch/cm3/stm/stm32timertran.h>
  97. #define STM32_QENC16_BASE STM32TIMER_BASE
  98. #define STM32_QENC16_CLK() STM32TIMER_CLK()
  99. #define STM32_QENC16_RST() STM32TIMER_RST()
  100. #define STM32_QENC16_I_AF STM32TIMER_AF(STM32_QENC16_I_PORT, STM32_QENC16_I_PIN)
  101. #define STM32_QENC16_Q_AF STM32TIMER_AF(STM32_QENC16_Q_PORT, STM32_QENC16_Q_PIN)
  102. static uint16_t stm32_qenc16_0_last_value;
  103. static int32_t stm32_qenc16_0_total_value;
  104. #define QENC16_LAST_VALUE stm32_qenc16_0_last_value
  105. #define QENC16_TOTAL_VALUE stm32_qenc16_0_total_value
  106. #endif
  107. /*!
  108. * \brief Initialize the quadrature encoder
  109. *
  110. * No checking is done here!
  111. *
  112. * \param None.
  113. *
  114. * \return None.
  115. */
  116. void stm32_qenc16_0_init(void)
  117. {
  118. STM32_QENC16_CLK();
  119. STM32_QENC16_RST();
  120. QENC16_LAST_VALUE = 0;
  121. /* AN4013.pdf*/
  122. /* Select Input T1->TI1FP1, T2->TI2FP2*/
  123. /* Input polarity TI1FP1 non-inverted, TI1FP1=TI1, TI1FP2 non-inverted, TI1FP2= TI2*/
  124. /* Register reset already done by reset */
  125. /* Encoder mode 3*/
  126. #if defined(STM32_QENC16_INVERT)
  127. TIM_CCEnable(STM32_QENC16_BASE) = TIM_CCER_CC1P;
  128. #endif
  129. TIM_SlaveModeControl(STM32_QENC16_BASE)= TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0;
  130. TIM_AutoReloadValue(STM32_QENC16_BASE) = (uint32_t) -1;
  131. TIM_EventGeneration(STM32_QENC16_BASE) = TIM_EGR_UG;
  132. GpioPinConfigSet(STM32_QENC16_I_PORT, STM32_QENC16_I_PIN,
  133. GPIO_CFG_PULLDOWN | GPIO_CFG_PERIPHAL);
  134. GpioPinConfigSet(STM32_QENC16_Q_PORT, STM32_QENC16_Q_PIN,
  135. GPIO_CFG_PULLDOWN | GPIO_CFG_PERIPHAL);
  136. GPIO_PinAFConfig((GPIO_TypeDef*)STM32_QENC16_I_PORT, STM32_QENC16_I_PIN,
  137. STM32_QENC16_I_AF );
  138. GPIO_PinAFConfig((GPIO_TypeDef*)STM32_QENC16_Q_PORT, STM32_QENC16_Q_PIN,
  139. STM32_QENC16_Q_AF );
  140. TIM_StartTimer(STM32_QENC16_BASE);
  141. }
  142. /*!
  143. * \brief Return quadrature encoder value
  144. *
  145. * \param None.
  146. *
  147. * \return .
  148. */
  149. int32_t stm32_qenc16_0_get(void)
  150. {
  151. uint16_t actual_value = TIM_Counter(STM32_QENC16_BASE);
  152. int16_t diff;
  153. NutEnterCritical();
  154. diff = actual_value - QENC16_LAST_VALUE;
  155. QENC16_LAST_VALUE = actual_value;
  156. QENC16_TOTAL_VALUE += diff;
  157. NutExitCritical();
  158. return QENC16_TOTAL_VALUE;
  159. }
  160. /*!
  161. * \brief Set quadrature encoder value
  162. *
  163. * \param Value to set.
  164. *
  165. * \return None.
  166. */
  167. void stm32_qenc16_0_set(int32_t value)
  168. {
  169. TIM_StopTimer(STM32_QENC16_BASE);
  170. TIM_Counter(STM32_QENC16_BASE) = 0;
  171. NutEnterCritical();
  172. QENC16_TOTAL_VALUE = value;
  173. NutExitCritical();
  174. TIM_Update(STM32_QENC16_BASE);
  175. TIM_StartTimer(STM32_QENC16_BASE);
  176. }
  177. #endif