at32f413_tmr.c 53 KB

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