at32f403a_407_tmr.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. /**
  2. **************************************************************************
  3. * @file at32f403a_407_tmr.c
  4. * @brief contains all the functions for the tmr firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f403a_407_conf.h"
  25. /** @addtogroup AT32F403A_407_periph_driver
  26. * @{
  27. */
  28. /** @defgroup TMR
  29. * @brief TMR driver modules
  30. * @{
  31. */
  32. #ifdef TMR_MODULE_ENABLED
  33. /** @defgroup TMR_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief tmr reset by crm reset register
  38. * @param tmr_x: select the tmr peripheral.
  39. * this parameter can be one of the following values:
  40. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  41. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  42. * @retval none
  43. */
  44. void tmr_reset(tmr_type *tmr_x)
  45. {
  46. if(tmr_x == TMR1)
  47. {
  48. crm_periph_reset(CRM_TMR1_PERIPH_RESET, TRUE);
  49. crm_periph_reset(CRM_TMR1_PERIPH_RESET, FALSE);
  50. }
  51. else if(tmr_x == TMR2)
  52. {
  53. crm_periph_reset(CRM_TMR2_PERIPH_RESET, TRUE);
  54. crm_periph_reset(CRM_TMR2_PERIPH_RESET, FALSE);
  55. }
  56. else if(tmr_x == TMR3)
  57. {
  58. crm_periph_reset(CRM_TMR3_PERIPH_RESET, TRUE);
  59. crm_periph_reset(CRM_TMR3_PERIPH_RESET, FALSE);
  60. }
  61. else if(tmr_x == TMR4)
  62. {
  63. crm_periph_reset(CRM_TMR4_PERIPH_RESET, TRUE);
  64. crm_periph_reset(CRM_TMR4_PERIPH_RESET, FALSE);
  65. }
  66. else if(tmr_x == TMR5)
  67. {
  68. crm_periph_reset(CRM_TMR5_PERIPH_RESET, TRUE);
  69. crm_periph_reset(CRM_TMR5_PERIPH_RESET, FALSE);
  70. }
  71. else if(tmr_x == TMR6)
  72. {
  73. crm_periph_reset(CRM_TMR6_PERIPH_RESET, TRUE);
  74. crm_periph_reset(CRM_TMR6_PERIPH_RESET, FALSE);
  75. }
  76. else if(tmr_x == TMR7)
  77. {
  78. crm_periph_reset(CRM_TMR7_PERIPH_RESET, TRUE);
  79. crm_periph_reset(CRM_TMR7_PERIPH_RESET, FALSE);
  80. }
  81. else if(tmr_x == TMR8)
  82. {
  83. crm_periph_reset(CRM_TMR8_PERIPH_RESET, TRUE);
  84. crm_periph_reset(CRM_TMR8_PERIPH_RESET, FALSE);
  85. }
  86. else if(tmr_x == TMR9)
  87. {
  88. crm_periph_reset(CRM_TMR9_PERIPH_RESET, TRUE);
  89. crm_periph_reset(CRM_TMR9_PERIPH_RESET, FALSE);
  90. }
  91. else if(tmr_x == TMR10)
  92. {
  93. crm_periph_reset(CRM_TMR10_PERIPH_RESET, TRUE);
  94. crm_periph_reset(CRM_TMR10_PERIPH_RESET, FALSE);
  95. }
  96. else if(tmr_x == TMR11)
  97. {
  98. crm_periph_reset(CRM_TMR11_PERIPH_RESET, TRUE);
  99. crm_periph_reset(CRM_TMR11_PERIPH_RESET, FALSE);
  100. }
  101. else if(tmr_x == TMR12)
  102. {
  103. crm_periph_reset(CRM_TMR12_PERIPH_RESET, TRUE);
  104. crm_periph_reset(CRM_TMR12_PERIPH_RESET, FALSE);
  105. }
  106. else if(tmr_x == TMR13)
  107. {
  108. crm_periph_reset(CRM_TMR13_PERIPH_RESET, TRUE);
  109. crm_periph_reset(CRM_TMR13_PERIPH_RESET, FALSE);
  110. }
  111. else if(tmr_x == TMR14)
  112. {
  113. crm_periph_reset(CRM_TMR14_PERIPH_RESET, TRUE);
  114. crm_periph_reset(CRM_TMR14_PERIPH_RESET, FALSE);
  115. }
  116. }
  117. /**
  118. * @brief enable or disable tmr counter
  119. * @param tmr_x: select the tmr peripheral.
  120. * this parameter can be one of the following values:
  121. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  122. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  123. * @param new_state (TRUE or FALSE)
  124. * @retval none
  125. */
  126. void tmr_counter_enable(tmr_type *tmr_x, confirm_state new_state)
  127. {
  128. /* tmr counter enable */
  129. tmr_x->ctrl1_bit.tmren = new_state;
  130. }
  131. /**
  132. * @brief init tmr output default para
  133. * @param tmr_output_struct
  134. * - to the structure of tmr_output_config_type
  135. * @retval none
  136. */
  137. void tmr_output_default_para_init(tmr_output_config_type *tmr_output_struct)
  138. {
  139. tmr_output_struct->oc_mode = TMR_OUTPUT_CONTROL_OFF;
  140. tmr_output_struct->oc_idle_state = FALSE;
  141. tmr_output_struct->occ_idle_state = FALSE;
  142. tmr_output_struct->oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
  143. tmr_output_struct->occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
  144. tmr_output_struct->oc_output_state = FALSE;
  145. tmr_output_struct->occ_output_state = FALSE;
  146. }
  147. /**
  148. * @brief init tmr input default para
  149. * @param tmr_input_struct
  150. * - to the structure of tmr_input_config_type
  151. * @retval none
  152. */
  153. void tmr_input_default_para_init(tmr_input_config_type *tmr_input_struct)
  154. {
  155. tmr_input_struct->input_channel_select = TMR_SELECT_CHANNEL_1;
  156. tmr_input_struct->input_polarity_select = TMR_INPUT_RISING_EDGE;
  157. tmr_input_struct->input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
  158. tmr_input_struct->input_filter_value = 0x0;
  159. }
  160. /**
  161. * @brief init tmr brkdt default para
  162. * @param tmr_brkdt_struct
  163. * - to the structure of tmr_brkdt_config_type
  164. * @retval none
  165. */
  166. void tmr_brkdt_default_para_init(tmr_brkdt_config_type *tmr_brkdt_struct)
  167. {
  168. tmr_brkdt_struct->deadtime = 0x0;
  169. tmr_brkdt_struct->brk_polarity = TMR_BRK_INPUT_ACTIVE_LOW;
  170. tmr_brkdt_struct->wp_level = TMR_WP_OFF;
  171. tmr_brkdt_struct->auto_output_enable = FALSE ;
  172. tmr_brkdt_struct->fcsoen_state = FALSE ;
  173. tmr_brkdt_struct->fcsodis_state = FALSE ;
  174. tmr_brkdt_struct->brk_enable = FALSE ;
  175. }
  176. /**
  177. * @brief init tmr base
  178. * @param tmr_x: select the tmr peripheral.
  179. * this parameter can be one of the following values:
  180. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  181. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  182. * @param tmr_pr (for 16 bit tmr 0x0000~0xFFFF,
  183. * for 32 bit tmr 0x0000_0000~0xFFFF_FFFF)
  184. * @param tmr_div (timer div value:0x0000~0xFFFF)
  185. * @retval none
  186. */
  187. void tmr_base_init(tmr_type* tmr_x, uint32_t tmr_pr, uint32_t tmr_div)
  188. {
  189. /* set the pr value */
  190. tmr_x->pr = tmr_pr;
  191. /* set the div value */
  192. tmr_x->div = tmr_div;
  193. /* trigger the overflow event to immediately reload pr value and div value */
  194. tmr_x->swevt_bit.ovfswtr = TRUE;
  195. }
  196. /**
  197. * @brief set tmr clock source division
  198. * @param tmr_x: select the tmr peripheral.
  199. * this parameter can be one of the following values:
  200. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  201. * TMR11, TMR12, TMR13, TMR14
  202. * @param tmr_clock_div
  203. * this parameter can be one of the following values:
  204. * - TMR_CLOCK_DIV1
  205. * - TMR_CLOCK_DIV2
  206. * - TMR_CLOCK_DIV4
  207. * @retval none
  208. */
  209. void tmr_clock_source_div_set(tmr_type *tmr_x, tmr_clock_division_type tmr_clock_div)
  210. {
  211. /* set tmr clock source division */
  212. tmr_x->ctrl1_bit.clkdiv = tmr_clock_div;
  213. }
  214. /**
  215. * @brief set tmr counter count direction
  216. * @param tmr_x: select the tmr peripheral.
  217. * this parameter can be one of the following values:
  218. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  219. * TMR11, TMR12, TMR13, TMR14
  220. * @param tmr_cnt_dir
  221. * this parameter can be one of the following values:
  222. * - TMR_COUNT_UP
  223. * - TMR_COUNT_DOWN
  224. * - TMR_COUNT_TWO_WAY_1
  225. * - TMR_COUNT_TWO_WAY_2
  226. * - TMR_COUNT_TWO_WAY_3
  227. * @retval none
  228. */
  229. void tmr_cnt_dir_set(tmr_type *tmr_x, tmr_count_mode_type tmr_cnt_dir)
  230. {
  231. /* set the cnt direct */
  232. tmr_x->ctrl1_bit.cnt_dir = tmr_cnt_dir;
  233. }
  234. /**
  235. * @brief set the repetition counter register(rpr) value
  236. * @param tmr_x: select the tmr peripheral.
  237. * this parameter can be one of the following values:
  238. * TMR1, TMR8
  239. * @param tmr_rpr_value (0x00~0xFF)
  240. * @retval none
  241. */
  242. void tmr_repetition_counter_set(tmr_type *tmr_x, uint8_t tmr_rpr_value)
  243. {
  244. /* set the repetition counter value */
  245. tmr_x->rpr_bit.rpr = tmr_rpr_value;
  246. }
  247. /**
  248. * @brief set tmr counter value
  249. * @param tmr_x: select the tmr peripheral.
  250. * this parameter can be one of the following values:
  251. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  252. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  253. * @param tmr_cnt_value (for 16 bit tmr 0x0000~0xFFFF,
  254. * for 32 bit tmr 0x0000_0000~0xFFFF_FFFF)
  255. * @retval none
  256. */
  257. void tmr_counter_value_set(tmr_type *tmr_x, uint32_t tmr_cnt_value)
  258. {
  259. /* set the tmr counter value */
  260. tmr_x->cval = tmr_cnt_value;
  261. }
  262. /**
  263. * @brief get tmr counter value
  264. * @param tmr_x: select the tmr peripheral.
  265. * this parameter can be one of the following values:
  266. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  267. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  268. * @retval tmr counter value
  269. */
  270. uint32_t tmr_counter_value_get(tmr_type *tmr_x)
  271. {
  272. return tmr_x->cval;
  273. }
  274. /**
  275. * @brief set tmr div value
  276. * @param tmr_x: select the tmr peripheral.
  277. * this parameter can be one of the following values:
  278. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  279. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  280. * @param tmr_div_value (0x0000~0xFFFF)
  281. * @retval none
  282. */
  283. void tmr_div_value_set(tmr_type *tmr_x, uint32_t tmr_div_value)
  284. {
  285. /* set the tmr div value */
  286. tmr_x->div = tmr_div_value;
  287. }
  288. /**
  289. * @brief get tmr div value
  290. * @param tmr_x: select the tmr peripheral.
  291. * this parameter can be one of the following values:
  292. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  293. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  294. * @retval tmr div value
  295. */
  296. uint32_t tmr_div_value_get(tmr_type *tmr_x)
  297. {
  298. return tmr_x->div;
  299. }
  300. /**
  301. * @brief config tmr output channel
  302. * @param tmr_x: select the tmr peripheral.
  303. * this parameter can be one of the following values:
  304. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  305. * TMR11, TMR12, TMR13, TMR14
  306. * @param tmr_channel
  307. * this parameter can be one of the following values:
  308. * - TMR_SELECT_CHANNEL_1
  309. * - TMR_SELECT_CHANNEL_2
  310. * - TMR_SELECT_CHANNEL_3
  311. * - TMR_SELECT_CHANNEL_4
  312. * @param tmr_output_struct
  313. * - to the structure of tmr_output_config_type
  314. * @retval none
  315. */
  316. void tmr_output_channel_config(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  317. tmr_output_config_type *tmr_output_struct)
  318. {
  319. uint16_t channel_index = 0, channel_c_index = 0, channel = 0, chx_offset, chcx_offset;
  320. chx_offset = (8 + tmr_channel);
  321. chcx_offset = (9 + tmr_channel);
  322. /* get channel idle state bit position in ctrl2 register */
  323. channel_index = (uint16_t)(tmr_output_struct->oc_idle_state << chx_offset);
  324. /* get channel complementary idle state bit position in ctrl2 register */
  325. channel_c_index = (uint16_t)(tmr_output_struct->occ_idle_state << chcx_offset);
  326. /* set output channel complementary idle state */
  327. tmr_x->ctrl2 &= ~(1<<chcx_offset);
  328. tmr_x->ctrl2 |= channel_c_index;
  329. /* set output channel idle state */
  330. tmr_x->ctrl2 &= ~(1<<chx_offset);
  331. tmr_x->ctrl2 |= channel_index;
  332. /* set channel output mode */
  333. channel = tmr_channel;
  334. switch(channel)
  335. {
  336. case TMR_SELECT_CHANNEL_1:
  337. tmr_x->cm1_output_bit.c1octrl = tmr_output_struct->oc_mode;
  338. break;
  339. case TMR_SELECT_CHANNEL_2:
  340. tmr_x->cm1_output_bit.c2octrl = tmr_output_struct->oc_mode;
  341. break;
  342. case TMR_SELECT_CHANNEL_3:
  343. tmr_x->cm2_output_bit.c3octrl = tmr_output_struct->oc_mode;
  344. break;
  345. case TMR_SELECT_CHANNEL_4:
  346. tmr_x->cm2_output_bit.c4octrl = tmr_output_struct->oc_mode;
  347. break;
  348. default:
  349. break;
  350. }
  351. chx_offset = ((tmr_channel * 2) + 1);
  352. chcx_offset = ((tmr_channel * 2) + 3);
  353. /* get channel polarity bit position in cctrl register */
  354. channel_index = (uint16_t)(tmr_output_struct->oc_polarity << chx_offset);
  355. /* get channel complementary polarity bit position in cctrl register */
  356. channel_c_index = (uint16_t)(tmr_output_struct->occ_polarity << chcx_offset);
  357. /* set output channel complementary polarity */
  358. tmr_x->cctrl &= ~(1<<chcx_offset);
  359. tmr_x->cctrl |= channel_c_index;
  360. /* set output channel polarity */
  361. tmr_x->cctrl &= ~(1<<chx_offset);
  362. tmr_x->cctrl |= channel_index;
  363. chx_offset = (tmr_channel * 2);
  364. chcx_offset = ((tmr_channel * 2) + 2);
  365. /* get channel enable bit position in cctrl register */
  366. channel_index = (uint16_t)(tmr_output_struct->oc_output_state << (tmr_channel * 2));
  367. /* get channel complementary enable bit position in cctrl register */
  368. channel_c_index = (uint16_t)(tmr_output_struct->occ_output_state << ((tmr_channel * 2) + 2));
  369. /* set output channel complementary enable bit */
  370. tmr_x->cctrl &= ~(1<<chcx_offset);
  371. tmr_x->cctrl |= channel_c_index;
  372. /* set output channel enable bit */
  373. tmr_x->cctrl &= ~(1<<chx_offset);
  374. tmr_x->cctrl |= channel_index;
  375. }
  376. /**
  377. * @brief select tmr output channel mode
  378. * @param tmr_x: select the tmr peripheral.
  379. * this parameter can be one of the following values:
  380. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  381. * TMR11, TMR12, TMR13, TMR14
  382. * @param tmr_channel
  383. * this parameter can be one of the following values:
  384. * - TMR_SELECT_CHANNEL_1
  385. * - TMR_SELECT_CHANNEL_2
  386. * - TMR_SELECT_CHANNEL_3
  387. * - TMR_SELECT_CHANNEL_4
  388. * @param oc_mode
  389. * this parameter can be one of the following values:
  390. * - TMR_OUTPUT_CONTROL_OFF
  391. * - TMR_OUTPUT_CONTROL_HIGH
  392. * - TMR_OUTPUT_CONTROL_LOW
  393. * - TMR_OUTPUT_CONTROL_SWITCH
  394. * - TMR_OUTPUT_CONTROL_FORCE_HIGH
  395. * - TMR_OUTPUT_CONTROL_FORCE_LOW
  396. * - TMR_OUTPUT_CONTROL_PWM_MODE_A
  397. * - TMR_OUTPUT_CONTROL_PWM_MODE_B
  398. * @retval none
  399. */
  400. void tmr_output_channel_mode_select(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  401. tmr_output_control_mode_type oc_mode)
  402. {
  403. uint16_t channel;
  404. channel = tmr_channel;
  405. switch(channel)
  406. {
  407. case TMR_SELECT_CHANNEL_1:
  408. tmr_x->cm1_output_bit.c1octrl = oc_mode;
  409. break;
  410. case TMR_SELECT_CHANNEL_2:
  411. tmr_x->cm1_output_bit.c2octrl = oc_mode;
  412. break;
  413. case TMR_SELECT_CHANNEL_3:
  414. tmr_x->cm2_output_bit.c3octrl = oc_mode;
  415. break;
  416. case TMR_SELECT_CHANNEL_4:
  417. tmr_x->cm2_output_bit.c4octrl = oc_mode;
  418. break;
  419. default:
  420. break;
  421. }
  422. }
  423. /**
  424. * @brief set tmr period value
  425. * @param tmr_x: select the tmr peripheral.
  426. * this parameter can be one of the following values:
  427. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  428. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  429. * @param tmr_pr_value: timer period register value of counter
  430. * (for 16 bit tmr 0x0000~0xFFFF,
  431. * for 32 bit tmr 0x0000_0000~0xFFFF_FFFF)
  432. * @retval none
  433. */
  434. void tmr_period_value_set(tmr_type *tmr_x, uint32_t tmr_pr_value)
  435. {
  436. /* set tmr period value */
  437. tmr_x->pr = tmr_pr_value;
  438. }
  439. /**
  440. * @brief get tmr period value
  441. * @param tmr_x: select the tmr peripheral.
  442. * this parameter can be one of the following values:
  443. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  444. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  445. * @retval timer period register value of counter
  446. * (for 16 bit tmr 0x0000~0xFFFF, for 32 bit tmr
  447. * 0x0000_0000~0xFFFF_FFFF)
  448. */
  449. uint32_t tmr_period_value_get(tmr_type *tmr_x)
  450. {
  451. return tmr_x->pr;
  452. }
  453. /**
  454. * @brief set tmr channel value
  455. * @param tmr_x: select the tmr peripheral.
  456. * this parameter can be one of the following values:
  457. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  458. * TMR11, TMR12, TMR13, TMR14
  459. * @param tmr_channel
  460. * this parameter can be one of the following values:
  461. * - TMR_SELECT_CHANNEL_1
  462. * - TMR_SELECT_CHANNEL_2
  463. * - TMR_SELECT_CHANNEL_3
  464. * - TMR_SELECT_CHANNEL_4
  465. * @param tmr_channel_value (for 16 bit tmr 0x0000~0xFFFF,
  466. * for 32 bit tmr 0x0000_0000~0xFFFF_FFFF)
  467. * @retval none
  468. */
  469. void tmr_channel_value_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  470. uint32_t tmr_channel_value)
  471. {
  472. uint16_t channel;
  473. channel = tmr_channel;
  474. /* set tmr channel value */
  475. switch(channel)
  476. {
  477. case TMR_SELECT_CHANNEL_1:
  478. tmr_x->c1dt = tmr_channel_value;
  479. break;
  480. case TMR_SELECT_CHANNEL_2:
  481. tmr_x->c2dt = tmr_channel_value;
  482. break;
  483. case TMR_SELECT_CHANNEL_3:
  484. tmr_x->c3dt = tmr_channel_value;
  485. break;
  486. case TMR_SELECT_CHANNEL_4:
  487. tmr_x->c4dt = tmr_channel_value;
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. /**
  494. * @brief get tmr channel value
  495. * @param tmr_x: select the tmr peripheral.
  496. * this parameter can be one of the following values:
  497. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  498. * TMR11, TMR12, TMR13, TMR14
  499. * @param tmr_channel
  500. * this parameter can be one of the following values:
  501. * - TMR_SELECT_CHANNEL_1
  502. * - TMR_SELECT_CHANNEL_2
  503. * - TMR_SELECT_CHANNEL_3
  504. * - TMR_SELECT_CHANNEL_4
  505. * @retval tmr channel value
  506. */
  507. uint32_t tmr_channel_value_get(tmr_type *tmr_x, tmr_channel_select_type tmr_channel)
  508. {
  509. uint32_t cc_value_get = 0;
  510. uint16_t channel;
  511. channel = tmr_channel;
  512. /* get tmr channel value */
  513. switch(channel)
  514. {
  515. case TMR_SELECT_CHANNEL_1:
  516. cc_value_get = tmr_x->c1dt;
  517. break;
  518. case TMR_SELECT_CHANNEL_2:
  519. cc_value_get = tmr_x->c2dt;
  520. break;
  521. case TMR_SELECT_CHANNEL_3:
  522. cc_value_get = tmr_x->c3dt;
  523. break;
  524. case TMR_SELECT_CHANNEL_4:
  525. cc_value_get = tmr_x->c4dt;
  526. break;
  527. default:
  528. break;
  529. }
  530. return cc_value_get;
  531. }
  532. /**
  533. * @brief enable tmr period buffer
  534. * @param tmr_x: select the tmr peripheral.
  535. * this parameter can be one of the following values:
  536. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  537. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  538. * @param new_state (TRUE or FALSE)
  539. * @retval none
  540. */
  541. void tmr_period_buffer_enable(tmr_type *tmr_x, confirm_state new_state)
  542. {
  543. /* tmr period buffer set */
  544. tmr_x->ctrl1_bit.prben = new_state;
  545. }
  546. /**
  547. * @brief enable tmr output channel buffer
  548. * @param tmr_x: select the tmr peripheral.
  549. * this parameter can be one of the following values:
  550. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  551. * TMR11, TMR12, TMR13, TMR14
  552. * @param tmr_channel
  553. * this parameter can be one of the following values:
  554. * - TMR_SELECT_CHANNEL_1
  555. * - TMR_SELECT_CHANNEL_2
  556. * - TMR_SELECT_CHANNEL_3
  557. * - TMR_SELECT_CHANNEL_4
  558. * @param new_state (TRUE or FALSE)
  559. * @retval none
  560. */
  561. void tmr_output_channel_buffer_enable(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  562. confirm_state new_state)
  563. {
  564. uint16_t channel;
  565. channel = tmr_channel;
  566. /* get tmr channel value */
  567. switch(channel)
  568. {
  569. case TMR_SELECT_CHANNEL_1:
  570. tmr_x->cm1_output_bit.c1oben = new_state;
  571. break;
  572. case TMR_SELECT_CHANNEL_2:
  573. tmr_x->cm1_output_bit.c2oben = new_state;
  574. break;
  575. case TMR_SELECT_CHANNEL_3:
  576. tmr_x->cm2_output_bit.c3oben = new_state;
  577. break;
  578. case TMR_SELECT_CHANNEL_4:
  579. tmr_x->cm2_output_bit.c4oben = new_state;
  580. break;
  581. default:
  582. break;
  583. }
  584. }
  585. /**
  586. * @brief set tmr output channel immediately
  587. * @param tmr_x: select the tmr peripheral.
  588. * this parameter can be one of the following values:
  589. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  590. * TMR11, TMR12, TMR13, TMR14
  591. * @param tmr_channel
  592. * this parameter can be one of the following values:
  593. * - TMR_SELECT_CHANNEL_1
  594. * - TMR_SELECT_CHANNEL_2
  595. * - TMR_SELECT_CHANNEL_3
  596. * - TMR_SELECT_CHANNEL_4
  597. * @param new_state (TRUE or FALSE)
  598. * @retval none
  599. */
  600. void tmr_output_channel_immediately_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  601. confirm_state new_state)
  602. {
  603. uint16_t channel;
  604. channel = tmr_channel;
  605. /* get tmr channel value */
  606. switch(channel)
  607. {
  608. case TMR_SELECT_CHANNEL_1:
  609. tmr_x->cm1_output_bit.c1oien = new_state;
  610. break;
  611. case TMR_SELECT_CHANNEL_2:
  612. tmr_x->cm1_output_bit.c2oien = new_state;
  613. break;
  614. case TMR_SELECT_CHANNEL_3:
  615. tmr_x->cm2_output_bit.c3oien = new_state;
  616. break;
  617. case TMR_SELECT_CHANNEL_4:
  618. tmr_x->cm2_output_bit.c4oien = new_state;
  619. break;
  620. default:
  621. break;
  622. }
  623. }
  624. /**
  625. * @brief set tmr output channel switch
  626. * @param tmr_x: select the tmr peripheral.
  627. * this parameter can be one of the following values:
  628. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  629. * TMR11, TMR12, TMR13, TMR14
  630. * @param tmr_channel
  631. * this parameter can be one of the following values:
  632. * - TMR_SELECT_CHANNEL_1
  633. * - TMR_SELECT_CHANNEL_2
  634. * - TMR_SELECT_CHANNEL_3
  635. * - TMR_SELECT_CHANNEL_4
  636. * @param new_state (TRUE or FALSE)
  637. * @retval none
  638. */
  639. void tmr_output_channel_switch_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  640. confirm_state new_state)
  641. {
  642. uint16_t channel;
  643. channel = tmr_channel;
  644. /* get tmr channel value */
  645. switch(channel)
  646. {
  647. case TMR_SELECT_CHANNEL_1:
  648. tmr_x->cm1_output_bit.c1osen = new_state;
  649. break;
  650. case TMR_SELECT_CHANNEL_2:
  651. tmr_x->cm1_output_bit.c2osen = new_state;
  652. break;
  653. case TMR_SELECT_CHANNEL_3:
  654. tmr_x->cm2_output_bit.c3osen = new_state;
  655. break;
  656. case TMR_SELECT_CHANNEL_4:
  657. tmr_x->cm2_output_bit.c4osen = new_state;
  658. break;
  659. default:
  660. break;
  661. }
  662. }
  663. /**
  664. * @brief enable or disable tmr one cycle mode
  665. * @param tmr_x: select the tmr peripheral.
  666. * this parameter can be one of the following values:
  667. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  668. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  669. * @param new_state (TRUE or FALSE)
  670. * @retval none
  671. */
  672. void tmr_one_cycle_mode_enable(tmr_type *tmr_x, confirm_state new_state)
  673. {
  674. /* tmr one cycle mode enable */
  675. tmr_x->ctrl1_bit.ocmen = new_state;
  676. }
  677. /**
  678. * @brief enable or disable tmr 32 bit function(plus mode)
  679. * @param tmr_x: select the tmr peripheral.
  680. * this parameter can be one of the following values:
  681. * TMR2, TMR5
  682. * @param new_state (TRUE or FALSE)
  683. * @retval none
  684. */
  685. void tmr_32_bit_function_enable (tmr_type *tmr_x, confirm_state new_state)
  686. {
  687. /* tmr 32 bit function(plus mode) enable,only for TMR2/TMR5 */
  688. if((tmr_x == TMR2) || (tmr_x == TMR5))
  689. {
  690. tmr_x->ctrl1_bit.pmen = new_state;
  691. }
  692. }
  693. /**
  694. * @brief select tmr the overflow event sources
  695. * @param tmr_x: select the tmr peripheral.
  696. * this parameter can be one of the following values:
  697. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  698. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  699. * @param new_state (TRUE or FALSE)
  700. * @retval none
  701. */
  702. void tmr_overflow_request_source_set(tmr_type *tmr_x, confirm_state new_state)
  703. {
  704. tmr_x->ctrl1_bit.ovfs = new_state;
  705. }
  706. /**
  707. * @brief enable or disable tmr overflow event generation
  708. * @param tmr_x: select the tmr peripheral.
  709. * this parameter can be one of the following values:
  710. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  711. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  712. * @param new_state (TRUE or FALSE)
  713. * @retval none
  714. */
  715. void tmr_overflow_event_disable(tmr_type *tmr_x, confirm_state new_state)
  716. {
  717. tmr_x->ctrl1_bit.ovfen = new_state;
  718. }
  719. /**
  720. * @brief init tmr input channel
  721. * @param tmr_x: select the tmr peripheral.
  722. * this parameter can be one of the following values:
  723. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  724. * TMR11, TMR12, TMR13, TMR14
  725. * @param input_struct
  726. * - to the structure of tmr_input_config_type
  727. * @param divider_factor
  728. * this parameter can be one of the following values:
  729. * - TMR_CHANNEL_INPUT_DIV_1
  730. * - TMR_CHANNEL_INPUT_DIV_2
  731. * - TMR_CHANNEL_INPUT_DIV_4
  732. * - TMR_CHANNEL_INPUT_DIV_8
  733. * @retval none
  734. */
  735. void tmr_input_channel_init(tmr_type *tmr_x, tmr_input_config_type *input_struct,
  736. tmr_channel_input_divider_type divider_factor)
  737. {
  738. uint16_t channel = 0;
  739. /* get channel selected */
  740. channel = input_struct->input_channel_select;
  741. switch(channel)
  742. {
  743. case TMR_SELECT_CHANNEL_1:
  744. tmr_x->cctrl_bit.c1en = FALSE;
  745. tmr_x->cctrl_bit.c1p = (uint32_t)input_struct->input_polarity_select;
  746. tmr_x->cctrl_bit.c1cp = (input_struct->input_polarity_select & 0x2) >> 1;
  747. tmr_x->cm1_input_bit.c1c = input_struct->input_mapped_select;
  748. tmr_x->cm1_input_bit.c1df = input_struct->input_filter_value;
  749. tmr_x->cm1_input_bit.c1idiv = divider_factor;
  750. tmr_x->cctrl_bit.c1en = TRUE;
  751. break;
  752. case TMR_SELECT_CHANNEL_2:
  753. tmr_x->cctrl_bit.c2en = FALSE;
  754. tmr_x->cctrl_bit.c2p = (uint32_t)input_struct->input_polarity_select;
  755. tmr_x->cctrl_bit.c2cp = (input_struct->input_polarity_select & 0x2) >> 1;
  756. tmr_x->cm1_input_bit.c2c = input_struct->input_mapped_select;
  757. tmr_x->cm1_input_bit.c2df = input_struct->input_filter_value;
  758. tmr_x->cm1_input_bit.c2idiv = divider_factor;
  759. tmr_x->cctrl_bit.c2en = TRUE;
  760. break;
  761. case TMR_SELECT_CHANNEL_3:
  762. tmr_x->cctrl_bit.c3en = FALSE;
  763. tmr_x->cctrl_bit.c3p = (uint32_t)input_struct->input_polarity_select;
  764. tmr_x->cctrl_bit.c3cp = (input_struct->input_polarity_select & 0x2) >> 1;
  765. tmr_x->cm2_input_bit.c3c = input_struct->input_mapped_select;
  766. tmr_x->cm2_input_bit.c3df = input_struct->input_filter_value;
  767. tmr_x->cm2_input_bit.c3idiv = divider_factor;
  768. tmr_x->cctrl_bit.c3en = TRUE;
  769. break;
  770. case TMR_SELECT_CHANNEL_4:
  771. tmr_x->cctrl_bit.c4en = FALSE;
  772. tmr_x->cctrl_bit.c4p = (uint32_t)input_struct->input_polarity_select;
  773. tmr_x->cm2_input_bit.c4c = input_struct->input_mapped_select;
  774. tmr_x->cm2_input_bit.c4df = input_struct->input_filter_value;
  775. tmr_x->cm2_input_bit.c4idiv = divider_factor;
  776. tmr_x->cctrl_bit.c4en = TRUE;
  777. break;
  778. default:
  779. break;
  780. }
  781. }
  782. /**
  783. * @brief tmr channel enable
  784. * @param tmr_x: select the tmr peripheral.
  785. * this parameter can be one of the following values:
  786. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  787. * TMR11, TMR12, TMR13, TMR14
  788. * @param tmr_channel
  789. * this parameter can be one of the following values:
  790. * - TMR_SELECT_CHANNEL_1
  791. * - TMR_SELECT_CHANNEL_1C
  792. * - TMR_SELECT_CHANNEL_2
  793. * - TMR_SELECT_CHANNEL_2C
  794. * - TMR_SELECT_CHANNEL_3
  795. * - TMR_SELECT_CHANNEL_3C
  796. * - TMR_SELECT_CHANNEL_4
  797. * @param new_state (TRUE or FALSE)
  798. * @retval none
  799. */
  800. void tmr_channel_enable(tmr_type *tmr_x, tmr_channel_select_type tmr_channel, confirm_state new_state)
  801. {
  802. uint16_t channel;
  803. channel = tmr_channel;
  804. switch(channel)
  805. {
  806. case TMR_SELECT_CHANNEL_1:
  807. tmr_x->cctrl_bit.c1en = new_state;
  808. break;
  809. case TMR_SELECT_CHANNEL_1C:
  810. tmr_x->cctrl_bit.c1cen = new_state;
  811. break;
  812. case TMR_SELECT_CHANNEL_2:
  813. tmr_x->cctrl_bit.c2en = new_state;
  814. break;
  815. case TMR_SELECT_CHANNEL_2C:
  816. tmr_x->cctrl_bit.c2cen = new_state;
  817. break;
  818. case TMR_SELECT_CHANNEL_3:
  819. tmr_x->cctrl_bit.c3en = new_state;
  820. break;
  821. case TMR_SELECT_CHANNEL_3C:
  822. tmr_x->cctrl_bit.c3cen = new_state;
  823. break;
  824. case TMR_SELECT_CHANNEL_4:
  825. tmr_x->cctrl_bit.c4en = new_state;
  826. break;
  827. default:
  828. break;
  829. }
  830. }
  831. /**
  832. * @brief set tmr input channel filter
  833. * @param tmr_x: select the tmr peripheral.
  834. * this parameter can be one of the following values:
  835. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  836. * TMR11, TMR12, TMR13, TMR14
  837. * @param tmr_channel
  838. * this parameter can be one of the following values:
  839. * - TMR_SELECT_CHANNEL_1
  840. * - TMR_SELECT_CHANNEL_2
  841. * - TMR_SELECT_CHANNEL_3
  842. * - TMR_SELECT_CHANNEL_4
  843. * @param filter_value (0x0~0xf)
  844. * @retval none
  845. */
  846. void tmr_input_channel_filter_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  847. uint16_t filter_value)
  848. {
  849. uint16_t channel;
  850. channel = tmr_channel;
  851. switch(channel)
  852. {
  853. case TMR_SELECT_CHANNEL_1:
  854. tmr_x->cm1_input_bit.c1df = filter_value;
  855. break;
  856. case TMR_SELECT_CHANNEL_2:
  857. tmr_x->cm1_input_bit.c2df = filter_value;
  858. break;
  859. case TMR_SELECT_CHANNEL_3:
  860. tmr_x->cm2_input_bit.c3df = filter_value;
  861. break;
  862. case TMR_SELECT_CHANNEL_4:
  863. tmr_x->cm2_input_bit.c4df = filter_value;
  864. break;
  865. default:
  866. break;
  867. }
  868. }
  869. /**
  870. * @brief config tmr pwm input
  871. * @param tmr_x: select the tmr peripheral.
  872. * this parameter can be one of the following values:
  873. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  874. * TMR11, TMR12, TMR13, TMR14
  875. * @param input_struct
  876. * - to the structure of tmr_input_config_type
  877. * @param divider_factor
  878. * this parameter can be one of the following values:
  879. * - TMR_CHANNEL_INPUT_DIV_1
  880. * - TMR_CHANNEL_INPUT_DIV_2
  881. * - TMR_CHANNEL_INPUT_DIV_4
  882. * - TMR_CHANNEL_INPUT_DIV_8
  883. * @retval none
  884. */
  885. void tmr_pwm_input_config(tmr_type *tmr_x, tmr_input_config_type *input_struct,
  886. tmr_channel_input_divider_type divider_factor)
  887. {
  888. uint16_t channel = 0;
  889. /* get channel selected */
  890. channel = input_struct->input_channel_select;
  891. switch(channel)
  892. {
  893. case TMR_SELECT_CHANNEL_1:
  894. if(input_struct->input_polarity_select == TMR_INPUT_RISING_EDGE)
  895. {
  896. /* set channel polarity */
  897. tmr_x->cctrl_bit.c1p = TMR_INPUT_RISING_EDGE;
  898. tmr_x->cctrl_bit.c2p = TMR_INPUT_FALLING_EDGE;
  899. }
  900. else if(input_struct->input_polarity_select == TMR_INPUT_FALLING_EDGE)
  901. {
  902. /* set channel polarity */
  903. tmr_x->cctrl_bit.c1p = TMR_INPUT_FALLING_EDGE;
  904. tmr_x->cctrl_bit.c2p = TMR_INPUT_RISING_EDGE;
  905. }
  906. if(input_struct->input_mapped_select == TMR_CC_CHANNEL_MAPPED_DIRECT)
  907. {
  908. /* ic1 is mapped on ti1 */
  909. tmr_x->cm1_input_bit.c1c = TMR_CC_CHANNEL_MAPPED_DIRECT;
  910. /* ic1 is mapped on ti2 */
  911. tmr_x->cm1_input_bit.c2c = TMR_CC_CHANNEL_MAPPED_INDIRECT;
  912. }
  913. else if(input_struct->input_mapped_select == TMR_CC_CHANNEL_MAPPED_INDIRECT)
  914. {
  915. /* ic1 is mapped on ti1 */
  916. tmr_x->cm1_input_bit.c1c = TMR_CC_CHANNEL_MAPPED_INDIRECT;
  917. /* ic1 is mapped on ti2 */
  918. tmr_x->cm1_input_bit.c2c = TMR_CC_CHANNEL_MAPPED_DIRECT;
  919. }
  920. /* set input ch1 and ch2 filter value*/
  921. tmr_x->cm1_input_bit.c1df = input_struct->input_filter_value;
  922. tmr_x->cm1_input_bit.c2df = input_struct->input_filter_value;
  923. /*set input ch1 and ch2 divider value*/
  924. tmr_x->cm1_input_bit.c1idiv = divider_factor;
  925. tmr_x->cm1_input_bit.c2idiv = divider_factor;
  926. tmr_x->cctrl_bit.c1en = TRUE;
  927. tmr_x->cctrl_bit.c2en = TRUE;
  928. break;
  929. case TMR_SELECT_CHANNEL_2:
  930. if(input_struct->input_polarity_select == TMR_INPUT_RISING_EDGE)
  931. {
  932. /* set channel polarity */
  933. tmr_x->cctrl_bit.c2p = TMR_INPUT_RISING_EDGE;
  934. tmr_x->cctrl_bit.c1p = TMR_INPUT_FALLING_EDGE;
  935. }
  936. else if(input_struct->input_polarity_select == TMR_INPUT_FALLING_EDGE)
  937. {
  938. /* set channel polarity */
  939. tmr_x->cctrl_bit.c2p = TMR_INPUT_FALLING_EDGE;
  940. tmr_x->cctrl_bit.c1p = TMR_INPUT_RISING_EDGE;
  941. }
  942. if(input_struct->input_mapped_select == TMR_CC_CHANNEL_MAPPED_DIRECT)
  943. {
  944. /* set mapped direct */
  945. tmr_x->cm1_input_bit.c2c = TMR_CC_CHANNEL_MAPPED_DIRECT;
  946. tmr_x->cm1_input_bit.c1c = TMR_CC_CHANNEL_MAPPED_INDIRECT;
  947. }
  948. else if(input_struct->input_mapped_select == TMR_CC_CHANNEL_MAPPED_INDIRECT)
  949. {
  950. /* set mapped direct */
  951. tmr_x->cm1_input_bit.c2c = TMR_CC_CHANNEL_MAPPED_INDIRECT;
  952. tmr_x->cm1_input_bit.c1c = TMR_CC_CHANNEL_MAPPED_DIRECT;
  953. }
  954. /* set input ch1 and ch2 filter value*/
  955. tmr_x->cm1_input_bit.c1df = input_struct->input_filter_value;
  956. tmr_x->cm1_input_bit.c2df = input_struct->input_filter_value;
  957. /*set input ch1 and ch2 divider value*/
  958. tmr_x->cm1_input_bit.c1idiv = divider_factor;
  959. tmr_x->cm1_input_bit.c2idiv = divider_factor;
  960. tmr_x->cctrl_bit.c1en = TRUE;
  961. tmr_x->cctrl_bit.c2en = TRUE;
  962. break;
  963. default:
  964. break;
  965. }
  966. }
  967. /**
  968. * @brief select tmr channel1 input
  969. * @param tmr_x: select the tmr peripheral.
  970. * this parameter can be one of the following values:
  971. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8
  972. * @param ch1_connect
  973. * this parameter can be one of the following values:
  974. * - TMR_CHANEL1_CONNECTED_C1IRAW
  975. * - TMR_CHANEL1_2_3_CONNECTED_C1IRAW_XOR
  976. * @retval none
  977. */
  978. void tmr_channel1_input_select(tmr_type *tmr_x, tmr_channel1_input_connected_type ch1_connect)
  979. {
  980. tmr_x->ctrl2_bit.c1insel = ch1_connect;
  981. }
  982. /**
  983. * @brief set tmr input channel divider
  984. * @param tmr_x: select the tmr peripheral.
  985. * this parameter can be one of the following values:
  986. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  987. * TMR11, TMR12, TMR13, TMR14
  988. * @param tmr_channel
  989. * this parameter can be one of the following values:
  990. * - TMR_SELECT_CHANNEL_1
  991. * - TMR_SELECT_CHANNEL_2
  992. * - TMR_SELECT_CHANNEL_3
  993. * - TMR_SELECT_CHANNEL_4
  994. * @param divider_factor
  995. * this parameter can be one of the following values:
  996. * - TMR_CHANNEL_INPUT_DIV_1
  997. * - TMR_CHANNEL_INPUT_DIV_2
  998. * - TMR_CHANNEL_INPUT_DIV_4
  999. * - TMR_CHANNEL_INPUT_DIV_8
  1000. * @retval none
  1001. */
  1002. void tmr_input_channel_divider_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  1003. tmr_channel_input_divider_type divider_factor)
  1004. {
  1005. uint16_t channel;
  1006. channel = tmr_channel;
  1007. switch(channel)
  1008. {
  1009. case TMR_SELECT_CHANNEL_1:
  1010. tmr_x->cm1_input_bit.c1idiv = divider_factor;
  1011. break;
  1012. case TMR_SELECT_CHANNEL_2:
  1013. tmr_x->cm1_input_bit.c2idiv = divider_factor;
  1014. break;
  1015. case TMR_SELECT_CHANNEL_3:
  1016. tmr_x->cm2_input_bit.c3idiv = divider_factor;
  1017. break;
  1018. case TMR_SELECT_CHANNEL_4:
  1019. tmr_x->cm2_input_bit.c4idiv = divider_factor;
  1020. break;
  1021. default:
  1022. break;
  1023. }
  1024. }
  1025. /**
  1026. * @brief select tmr primary mode
  1027. * @param tmr_x: select the tmr peripheral.
  1028. * this parameter can be one of the following values:
  1029. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8
  1030. * @param primary_mode
  1031. * this parameter can be one of the following values:
  1032. * - TMR_PRIMARY_SEL_RESET
  1033. * - TMR_PRIMARY_SEL_ENABLE
  1034. * - TMR_PRIMARY_SEL_OVERFLOW
  1035. * - TMR_PRIMARY_SEL_COMPARE
  1036. * - TMR_PRIMARY_SEL_C1ORAW
  1037. * - TMR_PRIMARY_SEL_C2ORAW
  1038. * - TMR_PRIMARY_SEL_C3ORAW
  1039. * - TMR_PRIMARY_SEL_C4ORAW
  1040. * @retval none
  1041. */
  1042. void tmr_primary_mode_select(tmr_type *tmr_x, tmr_primary_select_type primary_mode)
  1043. {
  1044. tmr_x->ctrl2_bit.ptos = primary_mode;
  1045. }
  1046. /**
  1047. * @brief select tmr subordinate mode
  1048. * @param tmr_x: select the tmr peripheral.
  1049. * this parameter can be one of the following values:
  1050. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR12
  1051. * @param sub_mode
  1052. * this parameter can be one of the following values:
  1053. * - TMR_SUB_MODE_DIABLE
  1054. * - TMR_SUB_ENCODER_MODE_A
  1055. * - TMR_SUB_ENCODER_MODE_B
  1056. * - TMR_SUB_ENCODER_MODE_C
  1057. * - TMR_SUB_RESET_MODE
  1058. * - TMR_SUB_HANG_MODE
  1059. * - TMR_SUB_TRIGGER_MODE
  1060. * - TMR_SUB_EXTERNAL_CLOCK_MODE_A
  1061. * @retval none
  1062. */
  1063. void tmr_sub_mode_select(tmr_type *tmr_x, tmr_sub_mode_select_type sub_mode)
  1064. {
  1065. tmr_x->stctrl_bit.smsel = sub_mode;
  1066. }
  1067. /**
  1068. * @brief select tmr channel dma request source
  1069. * @param tmr_x: select the tmr peripheral.
  1070. * this parameter can be one of the following values:
  1071. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR12
  1072. * @param cc_dma_select
  1073. * this parameter can be one of the following values:
  1074. * - TMR_DMA_REQUEST_BY_CHANNEL
  1075. * - TMR_DMA_REQUEST_BY_OVERFLOW
  1076. * @retval none
  1077. */
  1078. void tmr_channel_dma_select(tmr_type *tmr_x, tmr_dma_request_source_type cc_dma_select)
  1079. {
  1080. tmr_x->ctrl2_bit.drs = cc_dma_select;
  1081. }
  1082. /**
  1083. * @brief select tmr hall
  1084. * @param tmr_x: select the tmr peripheral.
  1085. * this parameter can be one of the following values:
  1086. * TMR1, TMR8
  1087. * @param new_state (TRUE or FALSE)
  1088. * @retval none
  1089. */
  1090. void tmr_hall_select(tmr_type *tmr_x, confirm_state new_state)
  1091. {
  1092. tmr_x->ctrl2_bit.ccfs = new_state;
  1093. }
  1094. /**
  1095. * @brief enable tmr channel buffer
  1096. * @param tmr_x: select the tmr peripheral.
  1097. * this parameter can be one of the following values:
  1098. * TMR1, TMR8
  1099. * @param new_state (TRUE or FALSE)
  1100. * @retval none
  1101. */
  1102. void tmr_channel_buffer_enable(tmr_type *tmr_x, confirm_state new_state)
  1103. {
  1104. tmr_x->ctrl2_bit.cbctrl = new_state;
  1105. }
  1106. /**
  1107. * @brief select tmr sub-trigger
  1108. * @param tmr_x: select the tmr peripheral.
  1109. * this parameter can be one of the following values:
  1110. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR12
  1111. * @param trigger_select
  1112. * this parameter can be one of the following values:
  1113. * - TMR_SUB_INPUT_SEL_IS0
  1114. * - TMR_SUB_INPUT_SEL_IS1
  1115. * - TMR_SUB_INPUT_SEL_IS2
  1116. * - TMR_SUB_INPUT_SEL_IS3
  1117. * - TMR_SUB_INPUT_SEL_C1INC
  1118. * - TMR_SUB_INPUT_SEL_C1DF1
  1119. * - TMR_SUB_INPUT_SEL_C2DF2
  1120. * - TMR_SUB_INPUT_SEL_EXTIN
  1121. * @retval none
  1122. */
  1123. void tmr_trigger_input_select(tmr_type *tmr_x, sub_tmr_input_sel_type trigger_select)
  1124. {
  1125. tmr_x->stctrl_bit.stis = trigger_select;
  1126. }
  1127. /**
  1128. * @brief set tmr subordinate synchronization mode
  1129. * @param tmr_x: select the tmr peripheral.
  1130. * this parameter can be one of the following values:
  1131. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR12
  1132. * @param new_state (TRUE or FALSE)
  1133. * @retval none
  1134. */
  1135. void tmr_sub_sync_mode_set(tmr_type *tmr_x, confirm_state new_state)
  1136. {
  1137. tmr_x->stctrl_bit.sts = new_state;
  1138. }
  1139. /**
  1140. * @brief enable or disable tmr dma request
  1141. * @param tmr_x: select the tmr peripheral.
  1142. * this parameter can be one of the following values:
  1143. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  1144. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  1145. * @param dma_request
  1146. * this parameter can be one of the following values:
  1147. * - TMR_OVERFLOW_DMA_REQUEST
  1148. * - TMR_C1_DMA_REQUEST
  1149. * - TMR_C2_DMA_REQUEST
  1150. * - TMR_C3_DMA_REQUEST
  1151. * - TMR_C4_DMA_REQUEST
  1152. * - TMR_HALL_DMA_REQUEST
  1153. * - TMR_TRIGGER_DMA_REQUEST
  1154. * @param new_state (TRUE or FALSE)
  1155. * @retval none
  1156. */
  1157. void tmr_dma_request_enable(tmr_type *tmr_x, tmr_dma_request_type dma_request, confirm_state new_state)
  1158. {
  1159. if(new_state == TRUE)
  1160. {
  1161. tmr_x->iden |= dma_request;
  1162. }
  1163. else if(new_state == FALSE)
  1164. {
  1165. tmr_x->iden &= ~dma_request;
  1166. }
  1167. }
  1168. /**
  1169. * @brief enable or disable tmr interrupt
  1170. * @param tmr_x: select the tmr peripheral.
  1171. * this parameter can be one of the following values:
  1172. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  1173. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  1174. * @param tmr_interrupt
  1175. * this parameter can be one of the following values:
  1176. * - TMR_OVF_INT
  1177. * - TMR_C1_INT
  1178. * - TMR_C2_INT
  1179. * - TMR_C3_INT
  1180. * - TMR_C4_INT
  1181. * - TMR_HALL_INT
  1182. * - TMR_TRIGGER_INT
  1183. * - TMR_BRK_INT
  1184. * @param new_state (TRUE or FALSE)
  1185. * @retval none
  1186. */
  1187. void tmr_interrupt_enable(tmr_type *tmr_x, uint32_t tmr_interrupt, confirm_state new_state)
  1188. {
  1189. if(new_state == TRUE)
  1190. {
  1191. tmr_x->iden |= tmr_interrupt;
  1192. }
  1193. else if(new_state == FALSE)
  1194. {
  1195. tmr_x->iden &= ~tmr_interrupt;
  1196. }
  1197. }
  1198. /**
  1199. * @brief get tmr interrupt flag
  1200. * @param tmr_x: select the tmr peripheral.
  1201. * this parameter can be one of the following values:
  1202. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  1203. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  1204. * @param tmr_flag
  1205. * this parameter can be one of the following values:
  1206. * - TMR_OVF_FLAG
  1207. * - TMR_C1_FLAG
  1208. * - TMR_C2_FLAG
  1209. * - TMR_C3_FLAG
  1210. * - TMR_C4_FLAG
  1211. * - TMR_HALL_FLAG
  1212. * - TMR_TRIGGER_FLAG
  1213. * - TMR_BRK_FLAG
  1214. * @retval state of tmr interrupt flag
  1215. */
  1216. flag_status tmr_interrupt_flag_get(tmr_type *tmr_x, uint32_t tmr_flag)
  1217. {
  1218. flag_status status = RESET;
  1219. if((tmr_x->ists & tmr_flag) && (tmr_x->iden & tmr_flag))
  1220. {
  1221. status = SET;
  1222. }
  1223. else
  1224. {
  1225. status = RESET;
  1226. }
  1227. return status;
  1228. }
  1229. /**
  1230. * @brief get tmr flag
  1231. * @param tmr_x: select the tmr peripheral.
  1232. * this parameter can be one of the following values:
  1233. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  1234. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  1235. * @param tmr_flag
  1236. * this parameter can be one of the following values:
  1237. * - TMR_OVF_FLAG
  1238. * - TMR_C1_FLAG
  1239. * - TMR_C2_FLAG
  1240. * - TMR_C3_FLAG
  1241. * - TMR_C4_FLAG
  1242. * - TMR_HALL_FLAG
  1243. * - TMR_TRIGGER_FLAG
  1244. * - TMR_BRK_FLAG
  1245. * - TMR_C1_RECAPTURE_FLAG
  1246. * - TMR_C2_RECAPTURE_FLAG
  1247. * - TMR_C3_RECAPTURE_FLAG
  1248. * - TMR_C4_RECAPTURE_FLAG
  1249. * @retval state of tmr flag
  1250. */
  1251. flag_status tmr_flag_get(tmr_type *tmr_x, uint32_t tmr_flag)
  1252. {
  1253. flag_status status = RESET;
  1254. if((tmr_x->ists & tmr_flag) != RESET)
  1255. {
  1256. status = SET;
  1257. }
  1258. else
  1259. {
  1260. status = RESET;
  1261. }
  1262. return status;
  1263. }
  1264. /**
  1265. * @brief clear tmr flag
  1266. * @param tmr_x: select the tmr peripheral.
  1267. * this parameter can be one of the following values:
  1268. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  1269. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  1270. * @param tmr_flag
  1271. * this parameter can be any combination of the following values:
  1272. * - TMR_OVF_FLAG
  1273. * - TMR_C1_FLAG
  1274. * - TMR_C2_FLAG
  1275. * - TMR_C3_FLAG
  1276. * - TMR_C4_FLAG
  1277. * - TMR_HALL_FLAG
  1278. * - TMR_TRIGGER_FLAG
  1279. * - TMR_BRK_FLAG
  1280. * - TMR_C1_RECAPTURE_FLAG
  1281. * - TMR_C2_RECAPTURE_FLAG
  1282. * - TMR_C3_RECAPTURE_FLAG
  1283. * - TMR_C4_RECAPTURE_FLAG
  1284. * @retval none
  1285. */
  1286. void tmr_flag_clear(tmr_type *tmr_x, uint32_t tmr_flag)
  1287. {
  1288. tmr_x->ists = ~tmr_flag;
  1289. }
  1290. /**
  1291. * @brief generate tmr event
  1292. * @param tmr_x: select the tmr peripheral.
  1293. * this parameter can be one of the following values:
  1294. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8,
  1295. * TMR9, TMR10, TMR11, TMR12, TMR13, TMR14
  1296. * @param tmr_event
  1297. * this parameter can be one of the following values:
  1298. * - TMR_OVERFLOW_SWTRIG
  1299. * - TMR_C1_SWTRIG
  1300. * - TMR_C2_SWTRIG
  1301. * - TMR_C3_SWTRIG
  1302. * - TMR_C4_SWTRIG
  1303. * - TMR_HALL_SWTRIG
  1304. * - TMR_TRIGGER_SWTRIG
  1305. * - TMR_BRK_SWTRIG
  1306. * @retval none
  1307. */
  1308. void tmr_event_sw_trigger(tmr_type *tmr_x, tmr_event_trigger_type tmr_event)
  1309. {
  1310. tmr_x->swevt |= tmr_event;
  1311. }
  1312. /**
  1313. * @brief tmr output enable(oen),this function is important for advtm output enable
  1314. * @param tmr_x: select the tmr peripheral.
  1315. * this parameter can be one of the following values:
  1316. * TMR1, TMR8
  1317. * @param new_state (TRUE or FALSE)
  1318. * @retval none
  1319. */
  1320. void tmr_output_enable(tmr_type *tmr_x, confirm_state new_state)
  1321. {
  1322. tmr_x->brk_bit.oen = new_state;
  1323. }
  1324. /**
  1325. * @brief set tmr select internal clock
  1326. * @param tmr_x: select the tmr peripheral.
  1327. * this parameter can be one of the following values:
  1328. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR12
  1329. * @retval none
  1330. */
  1331. void tmr_internal_clock_set(tmr_type *tmr_x)
  1332. {
  1333. tmr_x->stctrl_bit.smsel = TMR_SUB_MODE_DIABLE;
  1334. }
  1335. /**
  1336. * @brief set tmr output channel polarity
  1337. * @param tmr_x: select the tmr peripheral.
  1338. * this parameter can be one of the following values:
  1339. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  1340. * TMR11, TMR12, TMR13, TMR14
  1341. * @param tmr_channel
  1342. * this parameter can be one of the following values:
  1343. * - TMR_SELECT_CHANNEL_1
  1344. * - TMR_SELECT_CHANNEL_2
  1345. * - TMR_SELECT_CHANNEL_3
  1346. * - TMR_SELECT_CHANNEL_4
  1347. * - TMR_SELECT_CHANNEL_1C
  1348. * - TMR_SELECT_CHANNEL_2C
  1349. * - TMR_SELECT_CHANNEL_3C
  1350. * @param oc_polarity
  1351. * this parameter can be one of the following values:
  1352. * - TMR_POLARITY_ACTIVE_HIGH
  1353. * - TMR_POLARITY_ACTIVE_LOW
  1354. * @retval none
  1355. */
  1356. void tmr_output_channel_polarity_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  1357. tmr_polarity_active_type oc_polarity)
  1358. {
  1359. uint16_t channel;
  1360. channel = tmr_channel;
  1361. switch(channel)
  1362. {
  1363. case TMR_SELECT_CHANNEL_1:
  1364. tmr_x->cctrl_bit.c1p = (uint32_t)oc_polarity;
  1365. break;
  1366. case TMR_SELECT_CHANNEL_2:
  1367. tmr_x->cctrl_bit.c2p = (uint32_t)oc_polarity;
  1368. break;
  1369. case TMR_SELECT_CHANNEL_3:
  1370. tmr_x->cctrl_bit.c3p = (uint32_t)oc_polarity;
  1371. break;
  1372. case TMR_SELECT_CHANNEL_4:
  1373. tmr_x->cctrl_bit.c4p = (uint32_t)oc_polarity;
  1374. break;
  1375. case TMR_SELECT_CHANNEL_1C:
  1376. tmr_x->cctrl_bit.c1cp = (uint32_t)oc_polarity;
  1377. break;
  1378. case TMR_SELECT_CHANNEL_2C:
  1379. tmr_x->cctrl_bit.c2cp = (uint32_t)oc_polarity;
  1380. break;
  1381. case TMR_SELECT_CHANNEL_3C:
  1382. tmr_x->cctrl_bit.c3cp = (uint32_t)oc_polarity;
  1383. break;
  1384. default:
  1385. break;
  1386. }
  1387. }
  1388. /**
  1389. * @brief config tmr external clock
  1390. * @param tmr_x: select the tmr peripheral.
  1391. * this parameter can be one of the following values:
  1392. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8
  1393. * @param es_divide
  1394. * this parameter can be one of the following values:
  1395. * - TMR_ES_FREQUENCY_DIV_1
  1396. * - TMR_ES_FREQUENCY_DIV_2
  1397. * - TMR_ES_FREQUENCY_DIV_4
  1398. * - TMR_ES_FREQUENCY_DIV_8
  1399. * @param es_polarity
  1400. * this parameter can be one of the following values:
  1401. * - TMR_ES_POLARITY_NON_INVERTED
  1402. * - TMR_ES_POLARITY_INVERTED
  1403. * @param es_filter (0x0~0xf)
  1404. * @retval none
  1405. */
  1406. void tmr_external_clock_config(tmr_type *tmr_x, tmr_external_signal_divider_type es_divide,
  1407. tmr_external_signal_polarity_type es_polarity, uint16_t es_filter)
  1408. {
  1409. tmr_x->stctrl_bit.esdiv = es_divide;
  1410. tmr_x->stctrl_bit.esp = es_polarity;
  1411. tmr_x->stctrl_bit.esf = es_filter;
  1412. }
  1413. /**
  1414. * @brief config tmr external clock mode1
  1415. * @param tmr_x: select the tmr peripheral.
  1416. * this parameter can be one of the following values:
  1417. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR12
  1418. * @param es_divide
  1419. * this parameter can be one of the following values:
  1420. * - TMR_ES_FREQUENCY_DIV_1
  1421. * - TMR_ES_FREQUENCY_DIV_2
  1422. * - TMR_ES_FREQUENCY_DIV_4
  1423. * - TMR_ES_FREQUENCY_DIV_8
  1424. * @param es_polarity
  1425. * this parameter can be one of the following values:
  1426. * - TMR_ES_POLARITY_NON_INVERTED
  1427. * - TMR_ES_POLARITY_INVERTED
  1428. * @param es_filter (0x0~0xf)
  1429. * @retval none
  1430. */
  1431. void tmr_external_clock_mode1_config(tmr_type *tmr_x, tmr_external_signal_divider_type es_divide,
  1432. tmr_external_signal_polarity_type es_polarity, uint16_t es_filter)
  1433. {
  1434. tmr_external_clock_config(tmr_x, es_divide, es_polarity, es_filter);
  1435. tmr_x->stctrl_bit.smsel = TMR_SUB_EXTERNAL_CLOCK_MODE_A;
  1436. tmr_x->stctrl_bit.stis = TMR_SUB_INPUT_SEL_EXTIN;
  1437. }
  1438. /**
  1439. * @brief config tmr external clock mode2
  1440. * @param tmr_x: select the tmr peripheral.
  1441. * this parameter can be one of the following values:
  1442. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8
  1443. * @param es_divide
  1444. * this parameter can be one of the following values:
  1445. * - TMR_ES_FREQUENCY_DIV_1
  1446. * - TMR_ES_FREQUENCY_DIV_2
  1447. * - TMR_ES_FREQUENCY_DIV_4
  1448. * - TMR_ES_FREQUENCY_DIV_8
  1449. * @param es_polarity
  1450. * this parameter can be one of the following values:
  1451. * - TMR_ES_POLARITY_NON_INVERTED
  1452. * - TMR_ES_POLARITY_INVERTED
  1453. * @param es_filter (0x0~0xf)
  1454. * @retval none
  1455. */
  1456. void tmr_external_clock_mode2_config(tmr_type *tmr_x, tmr_external_signal_divider_type es_divide,
  1457. tmr_external_signal_polarity_type es_polarity, uint16_t es_filter)
  1458. {
  1459. tmr_external_clock_config(tmr_x, es_divide, es_polarity, es_filter);
  1460. tmr_x->stctrl_bit.ecmben = TRUE;
  1461. }
  1462. /**
  1463. * @brief config tmr encoder mode
  1464. * @param tmr_x: select the tmr peripheral.
  1465. * this parameter can be one of the following values:
  1466. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8
  1467. * @param encoder_mode
  1468. * this parameter can be one of the following values:
  1469. * - TMR_ENCODER_MODE_A
  1470. * - TMR_ENCODER_MODE_B
  1471. * - TMR_ENCODER_MODE_C
  1472. * @param ic1_polarity
  1473. * this parameter can be one of the following values:
  1474. * - TMR_INPUT_RISING_EDGE
  1475. * - TMR_INPUT_FALLING_EDGE
  1476. * - TMR_INPUT_BOTH_EDGE
  1477. * @param ic2_polarity
  1478. * this parameter can be one of the following values:
  1479. * - TMR_INPUT_RISING_EDGE
  1480. * - TMR_INPUT_FALLING_EDGE
  1481. * - TMR_INPUT_BOTH_EDGE
  1482. * @retval none
  1483. */
  1484. void tmr_encoder_mode_config(tmr_type *tmr_x, tmr_encoder_mode_type encoder_mode, tmr_input_polarity_type
  1485. ic1_polarity, tmr_input_polarity_type ic2_polarity)
  1486. {
  1487. tmr_x->stctrl_bit.smsel = encoder_mode;
  1488. /* set ic1 polarity */
  1489. tmr_x->cctrl_bit.c1p = (ic1_polarity & 0x1);
  1490. tmr_x->cctrl_bit.c1cp = (ic1_polarity >> 1);
  1491. /* set ic1 as input channel */
  1492. tmr_x->cm1_input_bit.c1c = TMR_CC_CHANNEL_MAPPED_DIRECT;
  1493. /* set ic2 polarity */
  1494. tmr_x->cctrl_bit.c2p = (ic2_polarity & 0x1);
  1495. tmr_x->cctrl_bit.c2cp = (ic2_polarity >> 1);
  1496. /* set ic2 as input channel */
  1497. tmr_x->cm1_input_bit.c2c = TMR_CC_CHANNEL_MAPPED_DIRECT;
  1498. }
  1499. /**
  1500. * @brief set tmr force output
  1501. * @param tmr_x: select the tmr peripheral.
  1502. * this parameter can be one of the following values:
  1503. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8, TMR9, TMR10,
  1504. * TMR11, TMR12, TMR13, TMR14
  1505. * @param tmr_channel
  1506. * this parameter can be one of the following values:
  1507. * - TMR_SELECT_CHANNEL_1
  1508. * - TMR_SELECT_CHANNEL_2
  1509. * - TMR_SELECT_CHANNEL_3
  1510. * - TMR_SELECT_CHANNEL_4
  1511. * @param force_output
  1512. * this parameter can be one of the following values:
  1513. * - TMR_FORCE_OUTPUT_HIGH
  1514. * - TMR_FORCE_OUTPUT_LOW
  1515. * @retval none
  1516. */
  1517. void tmr_force_output_set(tmr_type *tmr_x, tmr_channel_select_type tmr_channel,
  1518. tmr_force_output_type force_output)
  1519. {
  1520. uint16_t channel;
  1521. channel = tmr_channel;
  1522. switch(channel)
  1523. {
  1524. case TMR_SELECT_CHANNEL_1:
  1525. tmr_x->cm1_output_bit.c1octrl = force_output;
  1526. break;
  1527. case TMR_SELECT_CHANNEL_2:
  1528. tmr_x->cm1_output_bit.c2octrl = force_output;
  1529. break;
  1530. case TMR_SELECT_CHANNEL_3:
  1531. tmr_x->cm2_output_bit.c3octrl = force_output;
  1532. break;
  1533. case TMR_SELECT_CHANNEL_4:
  1534. tmr_x->cm2_output_bit.c4octrl = force_output;
  1535. break;
  1536. default:
  1537. break;
  1538. }
  1539. }
  1540. /**
  1541. * @brief config tmr dma control
  1542. * @param tmr_x: select the tmr peripheral.
  1543. * this parameter can be one of the following values:
  1544. * TMR1, TMR2, TMR3, TMR4, TMR5, TMR8
  1545. * @param dma_length
  1546. * this parameter can be one of the following values:
  1547. * - TMR_DMA_TRANSFER_1BYTE
  1548. * - TMR_DMA_TRANSFER_2BYTES
  1549. * - TMR_DMA_TRANSFER_3BYTES
  1550. * ...
  1551. * - TMR_DMA_TRANSFER_17BYTES
  1552. * - TMR_DMA_TRANSFER_18BYTES
  1553. * @param dma_base_address
  1554. * this parameter can be one of the following values:
  1555. * - TMR_CTRL1_ADDRESS
  1556. * - TMR_CTRL2_ADDRESS
  1557. * - TMR_STCTRL_ADDRESS
  1558. * - TMR_IDEN_ADDRESS
  1559. * - TMR_ISTS_ADDRESS
  1560. * - TMR_SWEVT_ADDRESS
  1561. * - TMR_CM1_ADDRESS
  1562. * - TMR_CM2_ADDRESS
  1563. * - TMR_CCTRL_ADDRESS
  1564. * - TMR_CVAL_ADDRESS
  1565. * - TMR_DIV_ADDRESS
  1566. * - TMR_PR_ADDRESS
  1567. * - TMR_RPR_ADDRESS
  1568. * - TMR_C1DT_ADDRESS
  1569. * - TMR_C2DT_ADDRESS
  1570. * - TMR_C3DT_ADDRESS
  1571. * - TMR_C4DT_ADDRESS
  1572. * - TMR_BRK_ADDRESS
  1573. * - TMR_DMACTRL_ADDRESS
  1574. * @retval none
  1575. */
  1576. void tmr_dma_control_config(tmr_type *tmr_x, tmr_dma_transfer_length_type dma_length,
  1577. tmr_dma_address_type dma_base_address)
  1578. {
  1579. tmr_x->dmactrl_bit.dtb = dma_length;
  1580. tmr_x->dmactrl_bit.addr = dma_base_address;
  1581. }
  1582. /**
  1583. * @brief config tmr brake mode and dead-time
  1584. * @param tmr_x: select the tmr peripheral.
  1585. * this parameter can be one of the following values:
  1586. * TMR1, TMR8
  1587. * @param brkdt_struct
  1588. * - to the structure of tmr_brkdt_config_type
  1589. * @retval none
  1590. */
  1591. void tmr_brkdt_config(tmr_type *tmr_x, tmr_brkdt_config_type *brkdt_struct)
  1592. {
  1593. tmr_x->brk_bit.brken = brkdt_struct->brk_enable;
  1594. tmr_x->brk_bit.dtc = brkdt_struct->deadtime;
  1595. tmr_x->brk_bit.fcsodis = brkdt_struct->fcsodis_state;
  1596. tmr_x->brk_bit.fcsoen = brkdt_struct->fcsoen_state;
  1597. tmr_x->brk_bit.brkv = brkdt_struct->brk_polarity;
  1598. tmr_x->brk_bit.aoen = brkdt_struct->auto_output_enable;
  1599. tmr_x->brk_bit.wpc = brkdt_struct->wp_level;
  1600. }
  1601. /**
  1602. * @}
  1603. */
  1604. #endif
  1605. /**
  1606. * @}
  1607. */
  1608. /**
  1609. * @}
  1610. */