n32g45x_dac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_dac.c
  29. * @author Nations
  30. * @version v1.0.1
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_dac.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup DAC
  40. * @brief DAC driver modules
  41. * @{
  42. */
  43. /** @addtogroup DAC_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup DAC_Private_Defines
  50. * @{
  51. */
  52. /* CTRL register Mask */
  53. #define CTRL_CLEAR_MASK ((uint32_t)0x00000FFE)
  54. /* DAC Dual Channels SWTRIG masks */
  55. #define DUAL_SWTRIG_SET ((uint32_t)0x00000003)
  56. #define DUAL_SWTRIG_RESET ((uint32_t)0xFFFFFFFC)
  57. /* DCH registers offsets */
  58. #define DR12CH1_OFFSET ((uint32_t)0x00000008)
  59. #define DR12CH2_OFFSET ((uint32_t)0x00000014)
  60. #define DR12DCH_OFFSET ((uint32_t)0x00000020)
  61. /* DATO register offset */
  62. #define DATO1_OFFSET ((uint32_t)0x0000002C)
  63. /**
  64. * @}
  65. */
  66. /** @addtogroup DAC_Private_Macros
  67. * @{
  68. */
  69. /**
  70. * @}
  71. */
  72. /** @addtogroup DAC_Private_Variables
  73. * @{
  74. */
  75. /**
  76. * @}
  77. */
  78. /** @addtogroup DAC_Private_FunctionPrototypes
  79. * @{
  80. */
  81. /**
  82. * @}
  83. */
  84. /** @addtogroup DAC_Private_Functions
  85. * @{
  86. */
  87. /**
  88. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  89. */
  90. void DAC_DeInit(void)
  91. {
  92. /* Enable DAC reset state */
  93. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_DAC, ENABLE);
  94. /* Release DAC from reset state */
  95. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_DAC, DISABLE);
  96. }
  97. /**
  98. * @brief Initializes the DAC peripheral according to the specified
  99. * parameters in the DAC_InitStruct.
  100. * @param DAC_Channel the selected DAC channel.
  101. * This parameter can be one of the following values:
  102. * @arg DAC_CHANNEL_1 DAC Channel1 selected
  103. * @arg DAC_CHANNEL_2 DAC Channel2 selected
  104. * @param DAC_InitStruct pointer to a DAC_InitType structure that
  105. * contains the configuration information for the specified DAC channel.
  106. */
  107. void DAC_Init(uint32_t DAC_Channel, DAC_InitType* DAC_InitStruct)
  108. {
  109. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  110. /* Check the DAC parameters */
  111. assert_param(IS_DAC_TRIGGER(DAC_InitStruct->Trigger));
  112. assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->WaveGen));
  113. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->LfsrUnMaskTriAmp));
  114. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->BufferOutput));
  115. /*---------------------------- DAC CTRL Configuration --------------------------*/
  116. /* Get the DAC CTRL value */
  117. tmpreg1 = DAC->CTRL;
  118. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  119. tmpreg1 &= ~(CTRL_CLEAR_MASK << DAC_Channel);
  120. /* Configure for the selected DAC channel: buffer output, trigger, wave generation,
  121. mask/amplitude for wave generation */
  122. /* Set TSELx and TENx bits according to Trigger value */
  123. /* Set WAVEx bits according to WaveGen value */
  124. /* Set MAMPx bits according to LfsrUnMaskTriAmp value */
  125. /* Set BOFFx bit according to BufferOutput value */
  126. tmpreg2 = (DAC_InitStruct->Trigger | DAC_InitStruct->WaveGen | DAC_InitStruct->LfsrUnMaskTriAmp
  127. | DAC_InitStruct->BufferOutput);
  128. /* Calculate CTRL register value depending on DAC_Channel */
  129. tmpreg1 |= tmpreg2 << DAC_Channel;
  130. /* Write to DAC CTRL */
  131. DAC->CTRL = tmpreg1;
  132. }
  133. /**
  134. * @brief Fills each DAC_InitStruct member with its default value.
  135. * @param DAC_InitStruct pointer to a DAC_InitType structure which will
  136. * be initialized.
  137. */
  138. void DAC_ClearStruct(DAC_InitType* DAC_InitStruct)
  139. {
  140. /*--------------- Reset DAC init structure parameters values -----------------*/
  141. /* Initialize the Trigger member */
  142. DAC_InitStruct->Trigger = DAC_TRG_NONE;
  143. /* Initialize the WaveGen member */
  144. DAC_InitStruct->WaveGen = DAC_WAVEGEN_NONE;
  145. /* Initialize the LfsrUnMaskTriAmp member */
  146. DAC_InitStruct->LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
  147. /* Initialize the BufferOutput member */
  148. DAC_InitStruct->BufferOutput = DAC_BUFFOUTPUT_ENABLE;
  149. }
  150. /**
  151. * @brief Enables or disables the specified DAC channel.
  152. * @param DAC_Channel the selected DAC channel.
  153. * This parameter can be one of the following values:
  154. * @arg DAC_CHANNEL_1 DAC Channel1 selected
  155. * @arg DAC_CHANNEL_2 DAC Channel2 selected
  156. * @param Cmd new state of the DAC channel.
  157. * This parameter can be: ENABLE or DISABLE.
  158. */
  159. void DAC_Enable(uint32_t DAC_Channel, FunctionalState Cmd)
  160. {
  161. /* Check the parameters */
  162. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  163. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  164. if (Cmd != DISABLE)
  165. {
  166. /* Enable the selected DAC channel */
  167. DAC->CTRL |= (DAC_CTRL_CH1EN << DAC_Channel);
  168. }
  169. else
  170. {
  171. /* Disable the selected DAC channel */
  172. DAC->CTRL &= ~(DAC_CTRL_CH1EN << DAC_Channel);
  173. }
  174. }
  175. /**
  176. * @brief Enables or disables the specified DAC channel DMA request.
  177. * @param DAC_Channel the selected DAC channel.
  178. * This parameter can be one of the following values:
  179. * @arg DAC_CHANNEL_1 DAC Channel1 selected
  180. * @arg DAC_CHANNEL_2 DAC Channel2 selected
  181. * @param Cmd new state of the selected DAC channel DMA request.
  182. * This parameter can be: ENABLE or DISABLE.
  183. */
  184. void DAC_DmaEnable(uint32_t DAC_Channel, FunctionalState Cmd)
  185. {
  186. /* Check the parameters */
  187. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  188. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  189. if (Cmd != DISABLE)
  190. {
  191. /* Enable the selected DAC channel DMA request */
  192. DAC->CTRL |= (DAC_CTRL_DMA1EN << DAC_Channel);
  193. }
  194. else
  195. {
  196. /* Disable the selected DAC channel DMA request */
  197. DAC->CTRL &= ~(DAC_CTRL_DMA1EN << DAC_Channel);
  198. }
  199. }
  200. /**
  201. * @brief Enables or disables the selected DAC channel software trigger.
  202. * @param DAC_Channel the selected DAC channel.
  203. * This parameter can be one of the following values:
  204. * @arg DAC_CHANNEL_1 DAC Channel1 selected
  205. * @arg DAC_CHANNEL_2 DAC Channel2 selected
  206. * @param Cmd new state of the selected DAC channel software trigger.
  207. * This parameter can be: ENABLE or DISABLE.
  208. */
  209. void DAC_SoftTrgEnable(uint32_t DAC_Channel, FunctionalState Cmd)
  210. {
  211. /* Check the parameters */
  212. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  213. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  214. if (Cmd != DISABLE)
  215. {
  216. /* Enable software trigger for the selected DAC channel */
  217. DAC->SOTTR |= (uint32_t)DAC_SOTTR_TR1EN << (DAC_Channel >> 4);
  218. }
  219. else
  220. {
  221. /* Disable software trigger for the selected DAC channel */
  222. DAC->SOTTR &= ~((uint32_t)DAC_SOTTR_TR1EN << (DAC_Channel >> 4));
  223. }
  224. }
  225. /**
  226. * @brief Enables or disables simultaneously the two DAC channels software
  227. * triggers.
  228. * @param Cmd new state of the DAC channels software triggers.
  229. * This parameter can be: ENABLE or DISABLE.
  230. */
  231. void DAC_DualSoftwareTrgEnable(FunctionalState Cmd)
  232. {
  233. /* Check the parameters */
  234. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  235. if (Cmd != DISABLE)
  236. {
  237. /* Enable software trigger for both DAC channels */
  238. DAC->SOTTR |= DUAL_SWTRIG_SET;
  239. }
  240. else
  241. {
  242. /* Disable software trigger for both DAC channels */
  243. DAC->SOTTR &= DUAL_SWTRIG_RESET;
  244. }
  245. }
  246. /**
  247. * @brief Enables or disables the selected DAC channel wave generation.
  248. * @param DAC_Channel the selected DAC channel.
  249. * This parameter can be one of the following values:
  250. * @arg DAC_CHANNEL_1 DAC Channel1 selected
  251. * @arg DAC_CHANNEL_2 DAC Channel2 selected
  252. * @param DAC_Wave Specifies the wave type to enable or disable.
  253. * This parameter can be one of the following values:
  254. * @arg DAC_WAVE_NOISE noise wave generation
  255. * @arg DAC_WAVE_TRIANGLE triangle wave generation
  256. * @param Cmd new state of the selected DAC channel wave generation.
  257. * This parameter can be: ENABLE or DISABLE.
  258. */
  259. void DAC_WaveGenerationEnable(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState Cmd)
  260. {
  261. /* Check the parameters */
  262. __IO uint32_t tmp = 0;
  263. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  264. assert_param(IS_DAC_WAVE(DAC_Wave));
  265. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  266. tmp=DAC->CTRL;
  267. tmp&=~(3<<(DAC_Channel+6));
  268. if (Cmd != DISABLE)
  269. {
  270. /* Enable the selected wave generation for the selected DAC channel */
  271. tmp |= DAC_Wave << DAC_Channel;
  272. }
  273. else
  274. {
  275. /* Disable the selected wave generation for the selected DAC channel */
  276. tmp &=~(3<<(DAC_Channel+6));
  277. }
  278. DAC->CTRL = tmp;
  279. }
  280. /**
  281. * @brief Set the specified data holding register value for DAC channel1.
  282. * @param DAC_Align Specifies the data alignment for DAC channel1.
  283. * This parameter can be one of the following values:
  284. * @arg DAC_ALIGN_R_8BIT 8bit right data alignment selected
  285. * @arg DAC_ALIGN_L_12BIT 12bit left data alignment selected
  286. * @arg DAC_ALIGN_R_12BIT 12bit right data alignment selected
  287. * @param Data Data to be loaded in the selected data holding register.
  288. */
  289. void DAC_SetCh1Data(uint32_t DAC_Align, uint16_t Data)
  290. {
  291. __IO uint32_t tmp = 0;
  292. /* Check the parameters */
  293. assert_param(IS_DAC_ALIGN(DAC_Align));
  294. assert_param(IS_DAC_DATA(Data));
  295. tmp = (uint32_t)DAC_BASE;
  296. tmp += DR12CH1_OFFSET + DAC_Align;
  297. /* Set the DAC channel1 selected data holding register */
  298. *(__IO uint32_t*)tmp = Data;
  299. }
  300. /**
  301. * @brief Set the specified data holding register value for DAC channel2.
  302. * @param DAC_Align Specifies the data alignment for DAC channel2.
  303. * This parameter can be one of the following values:
  304. * @arg DAC_ALIGN_R_8BIT 8bit right data alignment selected
  305. * @arg DAC_ALIGN_L_12BIT 12bit left data alignment selected
  306. * @arg DAC_ALIGN_R_12BIT 12bit right data alignment selected
  307. * @param Data Data to be loaded in the selected data holding register.
  308. */
  309. void DAC_SetCh2Data(uint32_t DAC_Align, uint16_t Data)
  310. {
  311. __IO uint32_t tmp = 0;
  312. /* Check the parameters */
  313. assert_param(IS_DAC_ALIGN(DAC_Align));
  314. assert_param(IS_DAC_DATA(Data));
  315. tmp = (uint32_t)DAC_BASE;
  316. tmp += DR12CH2_OFFSET + DAC_Align;
  317. /* Set the DAC channel2 selected data holding register */
  318. *(__IO uint32_t*)tmp = Data;
  319. }
  320. /**
  321. * @brief Set the specified data holding register value for dual channel
  322. * DAC.
  323. * @param DAC_Align Specifies the data alignment for dual channel DAC.
  324. * This parameter can be one of the following values:
  325. * @arg DAC_ALIGN_R_8BIT 8bit right data alignment selected
  326. * @arg DAC_ALIGN_L_12BIT 12bit left data alignment selected
  327. * @arg DAC_ALIGN_R_12BIT 12bit right data alignment selected
  328. * @param Data2 Data for DAC Channel2 to be loaded in the selected data
  329. * holding register.
  330. * @param Data1 Data for DAC Channel1 to be loaded in the selected data
  331. * holding register.
  332. */
  333. void DAC_SetDualChData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
  334. {
  335. uint32_t data = 0, tmp = 0;
  336. /* Check the parameters */
  337. assert_param(IS_DAC_ALIGN(DAC_Align));
  338. assert_param(IS_DAC_DATA(Data1));
  339. assert_param(IS_DAC_DATA(Data2));
  340. /* Calculate and set dual DAC data holding register value */
  341. if (DAC_Align == DAC_ALIGN_R_8BIT)
  342. {
  343. data = ((uint32_t)Data2 << 8) | Data1;
  344. }
  345. else
  346. {
  347. data = ((uint32_t)Data2 << 16) | Data1;
  348. }
  349. tmp = (uint32_t)DAC_BASE;
  350. tmp += DR12DCH_OFFSET + DAC_Align;
  351. /* Set the dual DAC selected data holding register */
  352. *(__IO uint32_t*)tmp = data;
  353. }
  354. /**
  355. * @brief Returns the last data output value of the selected DAC channel.
  356. * @param DAC_Channel the selected DAC channel.
  357. * This parameter can be one of the following values:
  358. * @arg DAC_CHANNEL_1 DAC Channel1 selected
  359. * @arg DAC_CHANNEL_2 DAC Channel2 selected
  360. * @return The selected DAC channel data output value.
  361. */
  362. uint16_t DAC_GetOutputDataVal(uint32_t DAC_Channel)
  363. {
  364. __IO uint32_t tmp = 0;
  365. /* Check the parameters */
  366. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  367. tmp = (uint32_t)DAC_BASE;
  368. tmp += DATO1_OFFSET + ((uint32_t)DAC_Channel >> 2);
  369. /* Returns the DAC channel data output register value */
  370. return (uint16_t)(*(__IO uint32_t*)tmp);
  371. }
  372. /**
  373. * @}
  374. */
  375. /**
  376. * @}
  377. */
  378. /**
  379. * @}
  380. */