hal_ftm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <mk20dx128.h>
  28. #include "teensy_hal.h"
  29. void HAL_FTM_Base_Init(FTM_HandleTypeDef *hftm) {
  30. /* Check the parameters */
  31. FTM_TypeDef *FTMx = hftm->Instance;
  32. assert_param(IS_FTM_INSTANCE(FTMx));
  33. assert_param(IS_FTM_PRESCALERSHIFT(hftm->Init.PrescalerShift));
  34. assert_param(IS_FTM_COUNTERMODE(hftm->Init.CounterMode));
  35. assert_param(IS_FTM_PERIOD(hftm->Init.Period));
  36. hftm->State = HAL_FTM_STATE_BUSY;
  37. FTMx->MODE = FTM_MODE_WPDIS;
  38. FTMx->SC = 0;
  39. FTMx->MOD = hftm->Init.Period;
  40. uint32_t sc = FTM_SC_PS(hftm->Init.PrescalerShift);
  41. if (hftm->Init.CounterMode == FTM_COUNTERMODE_CENTER) {
  42. sc |= FTM_SC_CPWMS;
  43. }
  44. FTMx->SC = sc;
  45. hftm->State = HAL_FTM_STATE_READY;
  46. }
  47. void HAL_FTM_Base_Start(FTM_HandleTypeDef *hftm) {
  48. FTM_TypeDef *FTMx = hftm->Instance;
  49. assert_param(IS_FTM_INSTANCE(FTMx));
  50. hftm->State = HAL_FTM_STATE_BUSY;
  51. FTMx->CNT = 0;
  52. FTMx->SC &= ~FTM_SC_CLKS(3);
  53. FTMx->SC |= FTM_SC_CLKS(1);
  54. hftm->State = HAL_FTM_STATE_READY;
  55. }
  56. void HAL_FTM_Base_Start_IT(FTM_HandleTypeDef *hftm) {
  57. FTM_TypeDef *FTMx = hftm->Instance;
  58. assert_param(IS_FTM_INSTANCE(FTMx));
  59. hftm->State = HAL_FTM_STATE_BUSY;
  60. FTMx->CNT = 0;
  61. FTMx->SC |= FTM_SC_CLKS(1) | FTM_SC_TOIE;
  62. hftm->State = HAL_FTM_STATE_READY;
  63. }
  64. void HAL_FTM_Base_DeInit(FTM_HandleTypeDef *hftm) {
  65. assert_param(IS_FTM_INSTANCE(hftm->Instance));
  66. hftm->State = HAL_FTM_STATE_BUSY;
  67. __HAL_FTM_DISABLE_TOF_IT(hftm);
  68. hftm->State = HAL_FTM_STATE_RESET;
  69. }
  70. void HAL_FTM_OC_Init(FTM_HandleTypeDef *hftm) {
  71. HAL_FTM_Base_Init(hftm);
  72. }
  73. void HAL_FTM_OC_ConfigChannel(FTM_HandleTypeDef *hftm, FTM_OC_InitTypeDef* sConfig, uint32_t channel) {
  74. FTM_TypeDef *FTMx = hftm->Instance;
  75. assert_param(IS_FTM_INSTANCE(FTMx));
  76. assert_param(IS_FTM_CHANNEL(channel));
  77. assert_param(IS_FTM_OC_MODE(sConfig->OCMode));
  78. assert_param(IS_FTM_OC_PULSE(sConfig->Pulse));
  79. assert_param(IS_FTM_OC_POLARITY(sConfig->OCPolarity));
  80. hftm->State = HAL_FTM_STATE_BUSY;
  81. FTMx->channel[channel].CSC = sConfig->OCMode;
  82. FTMx->channel[channel].CV = sConfig->Pulse;
  83. if (sConfig->OCPolarity & 1) {
  84. FTMx->POL |= (1 << channel);
  85. } else {
  86. FTMx->POL &= ~(1 << channel);
  87. }
  88. hftm->State = HAL_FTM_STATE_READY;
  89. }
  90. void HAL_FTM_OC_Start(FTM_HandleTypeDef *hftm, uint32_t channel) {
  91. // Nothing else to do
  92. }
  93. void HAL_FTM_OC_Start_IT(FTM_HandleTypeDef *hftm, uint32_t channel) {
  94. FTM_TypeDef *FTMx = hftm->Instance;
  95. assert_param(IS_FTM_INSTANCE(FTMx));
  96. FTMx->channel[channel].CSC |= FTM_CSC_CHIE;
  97. }
  98. void HAL_FTM_OC_DeInit(FTM_HandleTypeDef *hftm) {
  99. HAL_FTM_Base_DeInit(hftm);
  100. }
  101. void HAL_FTM_PWM_Init(FTM_HandleTypeDef *hftm) {
  102. HAL_FTM_Base_Init(hftm);
  103. }
  104. void HAL_FTM_PWM_ConfigChannel(FTM_HandleTypeDef *hftm, FTM_OC_InitTypeDef* sConfig, uint32_t channel) {
  105. FTM_TypeDef *FTMx = hftm->Instance;
  106. assert_param(IS_FTM_INSTANCE(FTMx));
  107. assert_param(IS_FTM_CHANNEL(channel));
  108. assert_param(IS_FTM_PWM_MODE(sConfig->OCMode));
  109. assert_param(IS_FTM_OC_PULSE(sConfig->Pulse));
  110. assert_param(IS_FTM_OC_POLARITY(sConfig->OCPolarity));
  111. hftm->State = HAL_FTM_STATE_BUSY;
  112. FTMx->channel[channel].CSC = sConfig->OCMode;
  113. FTMx->channel[channel].CV = sConfig->Pulse;
  114. if (sConfig->OCPolarity & 1) {
  115. FTMx->POL |= (1 << channel);
  116. } else {
  117. FTMx->POL &= ~(1 << channel);
  118. }
  119. hftm->State = HAL_FTM_STATE_READY;
  120. }
  121. void HAL_FTM_PWM_Start(FTM_HandleTypeDef *hftm, uint32_t channel) {
  122. // Nothing else to do
  123. }
  124. void HAL_FTM_PWM_Start_IT(FTM_HandleTypeDef *hftm, uint32_t channel) {
  125. FTM_TypeDef *FTMx = hftm->Instance;
  126. assert_param(IS_FTM_INSTANCE(FTMx));
  127. FTMx->channel[channel].CSC |= FTM_CSC_CHIE;
  128. }
  129. void HAL_FTM_PWM_DeInit(FTM_HandleTypeDef *hftm) {
  130. HAL_FTM_Base_DeInit(hftm);
  131. }
  132. void HAL_FTM_IC_Init(FTM_HandleTypeDef *hftm) {
  133. HAL_FTM_Base_Init(hftm);
  134. }
  135. void HAL_FTM_IC_ConfigChannel(FTM_HandleTypeDef *hftm, FTM_IC_InitTypeDef* sConfig, uint32_t channel) {
  136. FTM_TypeDef *FTMx = hftm->Instance;
  137. assert_param(IS_FTM_INSTANCE(FTMx));
  138. assert_param(IS_FTM_CHANNEL(channel));
  139. assert_param(IS_FTM_IC_POLARITY(sConfig->ICPolarity));
  140. hftm->State = HAL_FTM_STATE_BUSY;
  141. FTMx->channel[channel].CSC = sConfig->ICPolarity;
  142. hftm->State = HAL_FTM_STATE_READY;
  143. }
  144. void HAL_FTM_IC_Start(FTM_HandleTypeDef *hftm, uint32_t channel) {
  145. //FTM_TypeDef *FTMx = hftm->Instance;
  146. //assert_param(IS_FTM_INSTANCE(FTMx));
  147. // Nothing else to do
  148. }
  149. void HAL_FTM_IC_Start_IT(FTM_HandleTypeDef *hftm, uint32_t channel) {
  150. FTM_TypeDef *FTMx = hftm->Instance;
  151. assert_param(IS_FTM_INSTANCE(FTMx));
  152. FTMx->channel[channel].CSC |= FTM_CSC_CHIE;
  153. }
  154. void HAL_FTM_IC_DeInit(FTM_HandleTypeDef *hftm) {
  155. HAL_FTM_Base_DeInit(hftm);
  156. }