Encode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * @Author: mikey.zhaopeng
  3. * @Date: 2018-12-20 19:51:02
  4. * @Last Modified by: mikey.zhaopeng
  5. * @Last Modified time: 2018-12-20 19:51:02
  6. */
  7. #include "global.h"
  8. #include "Encode.h"
  9. #define ENCODEPERIOD 4
  10. #define USE_X02_EXTI 1
  11. #define X02EXTI_DIR 0 //1->上升沿,0->下降沿 (对应外部输入)
  12. extern void ExtiAcitionX02(void);
  13. //高速输入接口X30X31
  14. #define HIGH_SPEED_PORT D
  15. #define HIGH_SPEED_PIN1 13
  16. #define HIGH_SPEED_PIN2 12
  17. static unsigned char TimerX30,TimerX31,OldTimerX30,OldTimerX31,EXTimerFilter[2];
  18. float BMGearRatio = 1;
  19. /*
  20. void hw_power_off_init(void) {
  21. RCC->AHBENR |= 1 << (((uint32_t)(POWER_OFF_PORT)-GPIOA_BASE) / 0x400);
  22. GPIO_Set(POWER_OFF_PORT, 1 << POWER_OFF_PIN,
  23. GPIO_MODE_IPU,
  24. GPIO_SPEED_2M,
  25. GPIO_PUPD_NONE); //PB0,PB1设置
  26. }
  27. int hw_power_is_off(void) {
  28. return POWER_IS_OFF();
  29. }
  30. int hw_power_is_on(void) {
  31. return POWER_IS_ON();
  32. }
  33. */
  34. typedef struct
  35. {
  36. long cRealPosi;
  37. } cEncodeAxis_t;
  38. static cEncodeAxis_t cEncodeAxis;
  39. void EncoderInit(void)
  40. {
  41. /*
  42. if USE_ENCODE
  43. {
  44. #if 0
  45. RCC->APB2ENR |= 0x20 ;
  46. GPIO_Set(GPIOD, PIN12, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE);
  47. GPIO_Set(GPIOD, PIN13, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50M, GPIO_PUPD_NONE); //
  48. AFIO->MAPR |=1<<12;
  49. RCC->APB1ENR |= 0x04;
  50. //GPIO_AF_Set(GPIOD, 12, 2);
  51. // GPIO_AF_Set(GPIOD, 13, 2);
  52. Sys_NVIC_Init(1, 1, TIM4_IRQn, 1);
  53. TIM4->CR1 = 0;
  54. TIM4->CR1 |= (0 << 8) | (0 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  55. TIM4->CR2 = 0;
  56. TIM4->PSC = 0; //72
  57. TIM4->ARR = ENCODEPERIOD - 1;
  58. TIM4->CCER = 0;
  59. TIM4->CCMR1 = 0;
  60. TIM4->SMCR = 0;
  61. TIM4->DIER = 0;
  62. TIM4->DIER |= 1; // 允许更新中断
  63. TIM4->CNT = 0;
  64. TIM4->SMCR |=(3 << 0); // 编码器模弿1
  65. TIM4->CCMR1 |= (3 << 12) | (0 << 10) | (1 << 8);
  66. TIM4->CCMR1 |= (3 << 4) | (0 << 2) | (1 << 0); //CH1 CH2 输入配置 ,一分频,滤波设罿
  67. TIM4->CCER |= (0 << 5) | (0 << 1); //CH1 CH2 输入捕获 上升沿有敿
  68. TIM4->CCER |= (1 << 4) | (1 << 0); // 弿启输入捕莿
  69. TIM4->CR1 |= 1 << 0;
  70. TIM4->SR = 0;
  71. #endif
  72. }
  73. else
  74. {
  75. #if USE_X30_EXTI
  76. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
  77. AFIO->EXTICR[3] &= 0xFF0F; // clear used pin
  78. AFIO->EXTICR[3] |= (0x00F0 & 0x0030); // set pin to use PD13
  79. EXTI->IMR |= ((1 << 13) & 0x00002000); // unmask interrupt
  80. EXTI->EMR |= ((1 << 13) & 0x00000000); // unmask event
  81. switch(X30EXTI_DIR)
  82. {
  83. case 0:
  84. {
  85. EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
  86. EXTI->FTSR |= ((1 << 13) & 0x00000000); // set falling edge
  87. }break;
  88. case 1:
  89. {
  90. EXTI->RTSR |= ((1 << 13) & 0x00000000); // set rising edge
  91. EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
  92. }break;
  93. default:
  94. {
  95. EXTI->RTSR |= ((1 << 13) & 0x00002000); // set rising edge
  96. EXTI->FTSR |= ((1 << 13) & 0x00002000); // set falling edge
  97. }
  98. }
  99. if (0x00002000 & (1 << 13)) { // interrupt used
  100. NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
  101. }
  102. #endif
  103. #if USE_X31_EXTI
  104. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
  105. AFIO->EXTICR[3] &= 0xFFF0; // clear used pin
  106. AFIO->EXTICR[3] |= (0x000F & 0x00000003); // set pin to usePD12
  107. EXTI->IMR |= ((1 << 12) & 0x00001000); // unmask interrupt
  108. EXTI->EMR |= ((1 << 12) & 0x00000000); // unmask event
  109. switch(X31EXTI_DIR)
  110. {
  111. case 0:
  112. {
  113. EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
  114. EXTI->FTSR |= ((1 << 12) & 0x00000000); // set falling edge
  115. }break;
  116. case 1:
  117. {
  118. EXTI->RTSR |= ((1 << 12) & 0x00000000); // set rising edge
  119. EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
  120. }break;
  121. default:
  122. {
  123. EXTI->RTSR |= ((1 << 12) & 0x00001000); // set rising edge
  124. EXTI->FTSR |= ((1 << 12) & 0x00001000); // set falling edge
  125. }
  126. }
  127. if (0x00001000 & (1 << 12)) { // interrupt used
  128. NVIC->ISER[1] = (1 << (0x28 & 0x1F));// enable interrupt EXTI 10..15
  129. }
  130. #endif
  131. #if (USE_TIMER_X30_EXTI | USE_TIMER_X31_EXTI)
  132. #if 0
  133. RCC->APB1ENR |= 0x04;
  134. Sys_NVIC_Init(1, 1, TIM4_IRQn, 1);
  135. TIM4->CR1 = 0;
  136. TIM4->CR1 |= (0 << 8) | (1 << 7) | (0 << 5) | (1 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  137. TIM4->CR2 = 0;
  138. TIM4->PSC = 0;
  139. TIM4->ARR = 72000000 / EXTI_TIMER_FREQ + 1;
  140. TIM4->CCER = 0;
  141. TIM4->CCMR1 = 0;
  142. TIM4->DIER = 0;
  143. TIM4->DIER |= 1; // 允许更新中断
  144. TIM4->CNT = 72000000 / EXTI_TIMER_FREQ + 1;;
  145. TIM4->CR1 |= 1 << 0;
  146. TIM4->SR = 0;
  147. #endif
  148. #endif
  149. }
  150. */
  151. #if USE_X02_EXTI //PG2
  152. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // enable clock for Alternate Function
  153. AFIO->EXTICR[0] &= 0xF0FF; // clear used pin
  154. AFIO->EXTICR[0] |= (0x0F00 & 0x0600); // set pin to use PD0
  155. EXTI->IMR |= ((1 << 2) & 0x00000004); // unmask interrupt
  156. EXTI->EMR &= ~((1 << 2) & 0x00000004); // unmask event
  157. switch(X02EXTI_DIR)
  158. {
  159. case 0:
  160. {
  161. EXTI->RTSR |= ((1 << 2) & 0x00000004); // set rising edge
  162. EXTI->FTSR &= ~((1 << 2) & 0x00000004); // set falling edge
  163. }break;
  164. case 1:
  165. {
  166. EXTI->RTSR &= ~((1 << 2) & 0x00000004); // set rising edge
  167. EXTI->FTSR |= ((1 << 2) & 0x00000004); // set falling edge
  168. }break;
  169. default:
  170. {
  171. EXTI->RTSR |= ((1 << 2) & 0x00000004); // set rising edge
  172. EXTI->FTSR &= ~((1 << 2) & 0x00000004); // set falling edge
  173. }
  174. }
  175. Sys_NVIC_Init(1, 1, EXTI2_IRQn, 1);
  176. #endif
  177. }
  178. //定时器检测高速输入接口信号
  179. /*void TIM4_IRQHandler(void)
  180. {
  181. if USE_ENCODE
  182. {
  183. if (TIM4->SR & TIM_SR_UIF)
  184. {
  185. if(X_USE_OLD_DRIVE)
  186. {
  187. if ((TIM4->CR1 & TIM_CR1_DIR))
  188. {
  189. if(JIN_SHU_DIN_CUN_CONFIG | MXC_NILONG_CONFIG);
  190. else
  191. cEncodeAxis.cRealPosi--;
  192. }
  193. else if ((TIM4->CR1 & TIM_CR1_DIR)==0)
  194. {
  195. cEncodeAxis.cRealPosi++;
  196. }
  197. }
  198. else
  199. {
  200. // pwm_handle_maps[HW_PWM_1](pwm_hanlde_parames[HW_PWM_1]);
  201. }
  202. TIM4->SR &= ~TIM_SR_UIF;
  203. }
  204. }
  205. else
  206. {
  207. if (TIM4->SR & TIM_SR_UIF)
  208. {
  209. #if USE_TIMER_X30_EXTI
  210. OldTimerX30 = TimerX30;
  211. if(hw_io_pin_input(12))
  212. {
  213. if(EXTimerFilter[0] < FILTER_P)
  214. {
  215. EXTimerFilter[0]++;
  216. }
  217. else
  218. TimerX30 = 1;
  219. }
  220. else if(EXTimerFilter[0] > FILTER_N)
  221. {
  222. EXTimerFilter[0]--;
  223. }
  224. else
  225. TimerX30 = 0;
  226. switch(X30EXTI_DIR)
  227. {
  228. case 0:if(TimerX30 && !OldTimerX30)ExtiAcitionX30();break;
  229. case 1:if(!TimerX30 && OldTimerX30)ExtiAcitionX30();break;
  230. default:if((TimerX30 && !OldTimerX30) || (!TimerX30 && OldTimerX30))ExtiAcitionX30();
  231. }
  232. #endif
  233. #if USE_TIMER_X31_EXTI
  234. OldTimerX31 = TimerX31;
  235. if(hw_io_pin_input(13))
  236. {
  237. if(EXTimerFilter[1] < FILTER_P)
  238. {
  239. EXTimerFilter[1]++;
  240. }
  241. else
  242. TimerX31 = 1;
  243. }
  244. else if(EXTimerFilter[1] > FILTER_N)
  245. {
  246. EXTimerFilter[1]--;
  247. }
  248. else
  249. TimerX31 = 0;
  250. switch(X31EXTI_DIR)
  251. {
  252. case 0:if(TimerX31 && !OldTimerX31)ExtiAcitionX31();break;
  253. case 1:if(!TimerX31 && OldTimerX31)ExtiAcitionX31();break;
  254. default:if((TimerX31 && !OldTimerX31) || (!TimerX31 && TimerX31))ExtiAcitionX31();
  255. }
  256. #endif
  257. TIM4->SR &= ~TIM_SR_UIF;
  258. }
  259. }
  260. }*/
  261. /*
  262. long GetEncodePos(void)
  263. {
  264. float pulse_buff;
  265. pulse_buff = cEncodeAxis.cRealPosi;
  266. return (long)(pulse_buff/BMGearRatio);
  267. }
  268. void SetEncodePos(long pos)
  269. {
  270. cEncodeAxis.cRealPosi = pos*BMGearRatio;
  271. }
  272. void EXTI15_10_IRQHandler(void)
  273. {
  274. if(EXTI->PR & (1 << 12))
  275. {
  276. EXTI->PR |= (1 << 12);
  277. #if USE_X31_EXTI
  278. ExtiAcitionX31();
  279. #endif
  280. }
  281. if(EXTI->PR & (1 << 13))
  282. {
  283. EXTI->PR |= (1 << 13);
  284. #if USE_X30_EXTI
  285. ExtiAcitionX30();
  286. #endif
  287. }
  288. }
  289. void EXTI0_IRQHandler(void)
  290. {
  291. if(EXTI->PR & (1 << 0))
  292. {
  293. EXTI->PR |= (1 << 0);
  294. #if USE_X20_EXTI
  295. ExtiAcitionX20();
  296. #endif
  297. }
  298. }
  299. */
  300. void EXTI2_IRQHandler(void)
  301. {
  302. if(EXTI->PR & (1 << 2))
  303. {
  304. EXTI->PR |= (1 << 2);
  305. #if USE_X02_EXTI
  306. ExtiAcitionX02();
  307. // YXExtiAcitionX02();
  308. #endif
  309. }
  310. }