main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*!
  2. \file main.c
  3. \brief use the TSI to perform continuous acquisitions of three channels
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "gd32f3x0_eval.h"
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. /* the current cycle number array of the channel pin */
  14. uint16_t samplenum[4] = {0,0,0,0};
  15. /* reference value sample array of TSI group5 */
  16. uint16_t sample_refnum_array1[20] = {0};
  17. uint16_t sample_refnum_array2[20] = {0};
  18. uint16_t sample_refnum_array3[20] = {0};
  19. /* average value of cycles */
  20. uint16_t sample_refnum[3] = {0};
  21. void delay(uint32_t nCount);
  22. void gpio_config(void);
  23. void tsi_config(void);
  24. void led_config(void);
  25. void tsi_transfer_pin(uint32_t TSI_groupx_pin);
  26. /*!
  27. \brief main function
  28. \param[in] none
  29. \param[out] none
  30. \retval none
  31. */
  32. int main(void)
  33. {
  34. int m=0;
  35. /* TSI peripheral and GPIOB Periph clock enable */
  36. rcu_periph_clock_enable(RCU_GPIOA);
  37. rcu_periph_clock_enable(RCU_GPIOB);
  38. rcu_periph_clock_enable(RCU_GPIOC);
  39. rcu_periph_clock_enable(RCU_GPIOD);
  40. rcu_periph_clock_enable(RCU_GPIOF);
  41. rcu_periph_clock_enable(RCU_TSI);
  42. /* PB11 TSI_CHCFG_G5P0 SAMPCAP
  43. PB12 TSI_CHCFG_G5P1 CHANNEL
  44. PB13 TSI_CHCFG_G5P2 CHANNEL
  45. PB14 TSI_CHCFG_G5P3 CHANNEL */
  46. /* configure the GPIO ports */
  47. gpio_config();
  48. /* configure the TSI peripheral */
  49. tsi_config();
  50. /* configure the LED */
  51. led_config();
  52. /* reference cycle value acquisition and processing */
  53. for(m=0;m<20;m++){
  54. /* get charge transfer complete cycle number of group5 pin1 */
  55. tsi_transfer_pin(TSI_CHCFG_G5P1);
  56. /* check the TSI flag:end of acquisition interrupt */
  57. if((uint8_t)SET == tsi_flag_get(TSI_FLAG_CTCF)){
  58. /* get charge transfer complete cycle number */
  59. sample_refnum_array1[m] = tsi_group5_cycle_get();
  60. }
  61. /* disable the selected pin as channel pin */
  62. tsi_channel_pin_disable(TSI_CHCFG_G5P1);
  63. /* get charge transfer complete cycle number of group5 pin2 */
  64. tsi_transfer_pin(TSI_CHCFG_G5P2);
  65. if((uint8_t)SET == tsi_flag_get(TSI_FLAG_CTCF)){
  66. sample_refnum_array2[m] = tsi_group5_cycle_get();
  67. }
  68. tsi_channel_pin_disable(TSI_CHCFG_G5P2);
  69. /* get charge transfer complete cycle number of group5 pin3 */
  70. tsi_transfer_pin(TSI_CHCFG_G5P3);
  71. if((uint8_t)SET == tsi_flag_get(TSI_FLAG_CTCF)){
  72. sample_refnum_array3[m] = tsi_group5_cycle_get();
  73. }
  74. tsi_channel_pin_disable(TSI_CHCFG_G5P3);
  75. /* delay for a period of time while all banks have been acquired */
  76. delay(0x1000);
  77. }
  78. for(m=1;m<20;m++){
  79. sample_refnum[0] += sample_refnum_array1[m];
  80. sample_refnum[1] += sample_refnum_array2[m];
  81. sample_refnum[2] += sample_refnum_array3[m];
  82. }
  83. /* average channel cycle value are obtained */
  84. sample_refnum[0] = sample_refnum[0]/19;
  85. sample_refnum[1] = sample_refnum[1]/19;
  86. sample_refnum[2] = sample_refnum[2]/19;
  87. while (1){
  88. /* acquisition pin1 of group5 */
  89. tsi_transfer_pin(TSI_CHCFG_G5P1);
  90. /* check the TSI flag--end of acquisition interrupt */
  91. if((uint8_t)SET == tsi_flag_get(TSI_FLAG_CTCF)){
  92. /* get charge transfer complete cycle number */
  93. samplenum[1] = tsi_group5_cycle_get();
  94. }
  95. /* channel 1 touch */
  96. if((sample_refnum[0]-samplenum[1]) > 0x20){
  97. /* pin1 of group5 is touched */
  98. gd_eval_led_on(LED1);
  99. }else{
  100. gd_eval_led_off(LED1);
  101. }
  102. tsi_channel_pin_disable(TSI_CHCFG_G5P1);
  103. /* acquisition pin2 of group5 */
  104. tsi_transfer_pin(TSI_CHCFG_G5P2);
  105. /* check the TSI flag--end of acquisition interrupt */
  106. if((uint8_t)SET == tsi_flag_get(TSI_FLAG_CTCF)){
  107. samplenum[2] = tsi_group5_cycle_get();
  108. }
  109. /* light LED2 */
  110. if((sample_refnum[1]-samplenum[2]) > 0x20){
  111. /* TSI_GROUP6_PIN3 is touched */
  112. gd_eval_led_on(LED2);
  113. }else{
  114. gd_eval_led_off(LED2);
  115. }
  116. tsi_channel_pin_disable(TSI_CHCFG_G5P2);
  117. /* acquisition pin3 of group5 */
  118. tsi_transfer_pin(TSI_CHCFG_G5P3);
  119. /* check the TSI flag--end of acquisition interrupt */
  120. if((uint8_t)SET == tsi_flag_get(TSI_FLAG_CTCF)){
  121. samplenum[3] = tsi_group5_cycle_get();
  122. }
  123. /* light LED3 */
  124. if((sample_refnum[2]-samplenum[3]) > 0x20){
  125. /* pin3 of group5 is touched */
  126. gd_eval_led_on(LED3);
  127. }else{
  128. gd_eval_led_off(LED3);
  129. }
  130. tsi_channel_pin_disable(TSI_CHCFG_G5P3);
  131. }
  132. }
  133. /*!
  134. \brief insert a delay time
  135. \param[in] nCount: stall Count
  136. \param[out] none
  137. \retval none
  138. */
  139. void delay(uint32_t nCount)
  140. {
  141. for(; nCount != 0; nCount--);
  142. }
  143. /*!
  144. \brief configure the GPIO ports
  145. \param[in] none
  146. \param[out] none
  147. \retval none
  148. */
  149. void gpio_config(void)
  150. {
  151. /* GPIOB11 */
  152. /* alternate function output open-drain for sampling capacitor IO */
  153. gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_11);
  154. gpio_output_options_set(GPIOB,GPIO_OTYPE_OD,GPIO_OSPEED_2MHZ,GPIO_PIN_11);
  155. /* GPIOB12 GPIOB13 GPIOB14 */
  156. /* alternate function output push-pull for channel and shield IOs */
  157. gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_12);
  158. gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_12);
  159. gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_13);
  160. gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_13);
  161. gpio_mode_set(GPIOB,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_14);
  162. gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_14);
  163. /* connect pin to peripheral */
  164. gpio_af_set(GPIOB,GPIO_AF_3,GPIO_PIN_11);
  165. gpio_af_set(GPIOB,GPIO_AF_3,GPIO_PIN_12);
  166. gpio_af_set(GPIOB,GPIO_AF_3,GPIO_PIN_13);
  167. gpio_af_set(GPIOB,GPIO_AF_3,GPIO_PIN_14);
  168. }
  169. /*!
  170. \brief configure the TSI peripheral
  171. \param[in] none
  172. \param[out] none
  173. \retval none
  174. */
  175. void tsi_config(void)
  176. {
  177. /* TSI configure */
  178. tsi_init(TSI_CTCDIV_DIV32,TSI_CHARGE_2CTCLK,TSI_TRANSFER_2CTCLK,TSI_MAXNUM2047);
  179. tsi_sofeware_mode_config();
  180. tsi_sample_pin_enable(TSI_SAMPCFG_G5P0);
  181. tsi_group_enable(TSI_GCTL_GE5);
  182. /* disable hysteresis mode */
  183. tsi_hysteresis_off(TSI_PHM_G5P0|TSI_PHM_G5P1|TSI_PHM_G5P2|TSI_PHM_G5P3);
  184. /* enable TSI */
  185. tsi_enable();
  186. }
  187. /*!
  188. \brief configure led
  189. \param[in] none
  190. \param[out] none
  191. \retval none
  192. */
  193. void led_config(void)
  194. {
  195. /* initialize the LEDs */
  196. gd_eval_led_init(LED1);
  197. gd_eval_led_init(LED2);
  198. gd_eval_led_init(LED3);
  199. /* close all of LEDs */
  200. gd_eval_led_off(LED1);
  201. gd_eval_led_off(LED2);
  202. gd_eval_led_off(LED3);
  203. }
  204. /*!
  205. \brief acquisition pin y of group x,x=0..5,y=0..3
  206. \param[in] tsi_groupx_piny: TSI_CHCFG_GxPy,pin y of group x
  207. \param[out] none
  208. \retval none
  209. */
  210. void tsi_transfer_pin(uint32_t tsi_groupx_piny)
  211. {
  212. /* configure the TSI pin channel mode */
  213. tsi_channel_pin_enable(tsi_groupx_piny);
  214. /* wait capacitors discharge */
  215. delay(0xD00);
  216. /* clear both MNERR and CTCF flags */
  217. tsi_flag_clear(TSI_FLAG_CTCF_CLR|TSI_FLAG_MNERR_CLR);
  218. /* start a new acquisition */
  219. tsi_software_start();
  220. /* wait the specified TSI flag state: MNERR or CTCF */
  221. while(RESET==tsi_flag_get(TSI_FLAG_CTCF|TSI_FLAG_MNERR));
  222. }