stm32f10x_dac.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name : stm32f10x_dac.c
  3. * Author : MCD Application Team
  4. * Version : V2.0.3
  5. * Date : 09/22/2008
  6. * Description : This file provides all the DAC firmware functions.
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/
  15. /* Includes ------------------------------------------------------------------*/
  16. #include "stm32f10x_dac.h"
  17. #include "stm32f10x_rcc.h"
  18. /* Private typedef -----------------------------------------------------------*/
  19. /* Private define ------------------------------------------------------------*/
  20. /* DAC EN mask */
  21. #define CR_EN_Set ((u32)0x00000001)
  22. /* DAC DMAEN mask */
  23. #define CR_DMAEN_Set ((u32)0x00001000)
  24. /* CR register Mask */
  25. #define CR_CLEAR_Mask ((u32)0x00000FFE)
  26. /* DAC SWTRIG mask */
  27. #define SWTRIGR_SWTRIG_Set ((u32)0x00000001)
  28. /* DAC Dual Channels SWTRIG masks */
  29. #define DUAL_SWTRIG_Set ((u32)0x00000003)
  30. #define DUAL_SWTRIG_Reset ((u32)0xFFFFFFFC)
  31. /* DHR registers offsets */
  32. #define DHR12R1_Offset ((u32)0x00000008)
  33. #define DHR12R2_Offset ((u32)0x00000014)
  34. #define DHR12RD_Offset ((u32)0x00000020)
  35. /* DOR register offset */
  36. #define DOR_Offset ((u32)0x0000002C)
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. /* Private function prototypes -----------------------------------------------*/
  40. /* Private functions ---------------------------------------------------------*/
  41. /*******************************************************************************
  42. * Function Name : DAC_DeInit
  43. * Description : Deinitializes the DAC peripheral registers to their default
  44. * reset values.
  45. * Input : None.
  46. * Output : None
  47. * Return : None
  48. *******************************************************************************/
  49. void DAC_DeInit(void)
  50. {
  51. /* Enable DAC reset state */
  52. RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
  53. /* Release DAC from reset state */
  54. RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
  55. }
  56. /*******************************************************************************
  57. * Function Name : DAC_Init
  58. * Description : Initializes the DAC peripheral according to the specified
  59. * parameters in the DAC_InitStruct.
  60. * Input : - DAC_Channel: the selected DAC channel.
  61. * This parameter can be one of the following values:
  62. * - DAC_Channel_1: DAC Channel1 selected
  63. * - DAC_Channel_2: DAC Channel2 selected
  64. * - DAC_InitStruct: pointer to a DAC_InitTypeDef structure that
  65. * contains the configuration information for the specified
  66. * DAC channel.
  67. * Output : None
  68. * Return : None
  69. *******************************************************************************/
  70. void DAC_Init(u32 DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
  71. {
  72. u32 tmpreg1 = 0, tmpreg2 = 0;
  73. /* Check the DAC parameters */
  74. assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));
  75. assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->DAC_WaveGeneration));
  76. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude));
  77. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));
  78. /*---------------------------- DAC CR Configuration --------------------------*/
  79. /* Get the DAC CR value */
  80. tmpreg1 = DAC->CR;
  81. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  82. tmpreg1 &= ~(CR_CLEAR_Mask << DAC_Channel);
  83. /* Configure for the selected DAC channel: buffer output, trigger, wave genration,
  84. mask/amplitude for wave genration */
  85. /* Set TSELx and TENx bits according to DAC_Trigger value */
  86. /* Set WAVEx bits according to DAC_WaveGeneration value */
  87. /* Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value */
  88. /* Set BOFFx bit according to DAC_OutputBuffer value */
  89. tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
  90. DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);
  91. /* Calculate CR register value depending on DAC_Channel */
  92. tmpreg1 |= tmpreg2 << DAC_Channel;
  93. /* Write to DAC CR */
  94. DAC->CR = tmpreg1;
  95. }
  96. /*******************************************************************************
  97. * Function Name : DAC_StructInit
  98. * Description : Fills each DAC_InitStruct member with its default value.
  99. * Input : - DAC_InitStruct : pointer to a DAC_InitTypeDef structure
  100. * which will be initialized.
  101. * Output : None
  102. * Return : None
  103. *******************************************************************************/
  104. void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
  105. {
  106. /*--------------- Reset DAC init structure parameters values -----------------*/
  107. /* Initialize the DAC_Trigger member */
  108. DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
  109. /* Initialize the DAC_WaveGeneration member */
  110. DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;
  111. /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */
  112. DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
  113. /* Initialize the DAC_OutputBuffer member */
  114. DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  115. }
  116. /*******************************************************************************
  117. * Function Name : DAC_Cmd
  118. * Description : Enables or disables the specified DAC channel.
  119. * Input - DAC_Channel: the selected DAC channel.
  120. * This parameter can be one of the following values:
  121. * - DAC_Channel_1: DAC Channel1 selected
  122. * - DAC_Channel_2: DAC Channel2 selected
  123. * - NewState: new state of the DAC channel.
  124. * This parameter can be: ENABLE or DISABLE.
  125. * Output : None
  126. * Return : None
  127. *******************************************************************************/
  128. void DAC_Cmd(u32 DAC_Channel, FunctionalState NewState)
  129. {
  130. /* Check the parameters */
  131. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  132. assert_param(IS_FUNCTIONAL_STATE(NewState));
  133. if (NewState != DISABLE)
  134. {
  135. /* Enable the selected DAC channel */
  136. DAC->CR |= CR_EN_Set << DAC_Channel;
  137. }
  138. else
  139. {
  140. /* Disable the selected DAC channel */
  141. DAC->CR &= ~(CR_EN_Set << DAC_Channel);
  142. }
  143. }
  144. /*******************************************************************************
  145. * Function Name : DAC_DMACmd
  146. * Description : Enables or disables the specified DAC channel DMA request.
  147. * Input - DAC_Channel: the selected DAC channel.
  148. * This parameter can be one of the following values:
  149. * - DAC_Channel_1: DAC Channel1 selected
  150. * - DAC_Channel_2: DAC Channel2 selected
  151. * - NewState: new state of the selected DAC channel DMA request.
  152. * This parameter can be: ENABLE or DISABLE.
  153. * Output : None
  154. * Return : None
  155. *******************************************************************************/
  156. void DAC_DMACmd(u32 DAC_Channel, FunctionalState NewState)
  157. {
  158. /* Check the parameters */
  159. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  160. assert_param(IS_FUNCTIONAL_STATE(NewState));
  161. if (NewState != DISABLE)
  162. {
  163. /* Enable the selected DAC channel DMA request */
  164. DAC->CR |= CR_DMAEN_Set << DAC_Channel;
  165. }
  166. else
  167. {
  168. /* Disable the selected DAC channel DMA request */
  169. DAC->CR &= ~(CR_DMAEN_Set << DAC_Channel);
  170. }
  171. }
  172. /*******************************************************************************
  173. * Function Name : DAC_SoftwareTriggerCmd
  174. * Description : Enables or disables the selected DAC channel software trigger.
  175. * Input - DAC_Channel: the selected DAC channel.
  176. * This parameter can be one of the following values:
  177. * - DAC_Channel_1: DAC Channel1 selected
  178. * - DAC_Channel_2: DAC Channel2 selected
  179. * - NewState: new state of the selected DAC channel software trigger.
  180. * This parameter can be: ENABLE or DISABLE.
  181. * Output : None
  182. * Return : None
  183. *******************************************************************************/
  184. void DAC_SoftwareTriggerCmd(u32 DAC_Channel, FunctionalState NewState)
  185. {
  186. /* Check the parameters */
  187. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  188. assert_param(IS_FUNCTIONAL_STATE(NewState));
  189. if (NewState != DISABLE)
  190. {
  191. /* Enable software trigger for the selected DAC channel */
  192. DAC->SWTRIGR |= SWTRIGR_SWTRIG_Set << (DAC_Channel >> 4);
  193. }
  194. else
  195. {
  196. /* Disable software trigger for the selected DAC channel */
  197. DAC->SWTRIGR &= ~(SWTRIGR_SWTRIG_Set << (DAC_Channel >> 4));
  198. }
  199. }
  200. /*******************************************************************************
  201. * Function Name : DAC_DualSoftwareTriggerCmd
  202. * Description : Enables or disables simultaneously the two DAC channels software
  203. * triggers.
  204. * Input - NewState: new state of the DAC channels software triggers.
  205. * This parameter can be: ENABLE or DISABLE.
  206. * Output : None
  207. * Return : None
  208. *******************************************************************************/
  209. void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
  210. {
  211. /* Check the parameters */
  212. assert_param(IS_FUNCTIONAL_STATE(NewState));
  213. if (NewState != DISABLE)
  214. {
  215. /* Enable software trigger for both DAC channels */
  216. DAC->SWTRIGR |= DUAL_SWTRIG_Set ;
  217. }
  218. else
  219. {
  220. /* Disable software trigger for both DAC channels */
  221. DAC->SWTRIGR &= DUAL_SWTRIG_Reset;
  222. }
  223. }
  224. /*******************************************************************************
  225. * Function Name : DAC_WaveGenerationCmd
  226. * Description : Enables or disables the selected DAC channel wave generation.
  227. * Input - DAC_Channel: the selected DAC channel.
  228. * This parameter can be one of the following values:
  229. * - DAC_Channel_1: DAC Channel1 selected
  230. * - DAC_Channel_2: DAC Channel2 selected
  231. * - DAC_Wave: Specifies the wave type to enable or disable.
  232. * This parameter can be one of the following values:
  233. * - DAC_Wave_Noise: noise wave generation
  234. * - DAC_Wave_Triangle: triangle wave generation
  235. * - NewState: new state of the selected DAC channel wave generation.
  236. * This parameter can be: ENABLE or DISABLE.
  237. * Output : None
  238. * Return : None
  239. *******************************************************************************/
  240. void DAC_WaveGenerationCmd(u32 DAC_Channel, u32 DAC_Wave, FunctionalState NewState)
  241. {
  242. /* Check the parameters */
  243. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  244. assert_param(IS_DAC_WAVE(DAC_Wave));
  245. assert_param(IS_FUNCTIONAL_STATE(NewState));
  246. if (NewState != DISABLE)
  247. {
  248. /* Enable the selected wave generation for the selected DAC channel */
  249. DAC->CR |= DAC_Wave << DAC_Channel;
  250. }
  251. else
  252. {
  253. /* Disable the selected wave generation for the selected DAC channel */
  254. DAC->CR &= ~(DAC_Wave << DAC_Channel);
  255. }
  256. }
  257. /*******************************************************************************
  258. * Function Name : DAC_SetChannel1Data
  259. * Description : Set the specified data holding register value for DAC channel1.
  260. * Input : - DAC_Align: Specifies the data alignement for DAC channel1.
  261. * This parameter can be one of the following values:
  262. * - DAC_Align_8b_R: 8bit right data alignement selected
  263. * - DAC_Align_12b_L: 12bit left data alignement selected
  264. * - DAC_Align_12b_R: 12bit right data alignement selected
  265. * - Data : Data to be loaded in the selected data holding
  266. * register.
  267. * Output : None
  268. * Return : None
  269. *******************************************************************************/
  270. void DAC_SetChannel1Data(u32 DAC_Align, u16 Data)
  271. {
  272. /* Check the parameters */
  273. assert_param(IS_DAC_ALIGN(DAC_Align));
  274. assert_param(IS_DAC_DATA(Data));
  275. /* Set the DAC channel1 selected data holding register */
  276. *((vu32 *)(DAC_BASE + DHR12R1_Offset + DAC_Align)) = (u32)Data;
  277. }
  278. /*******************************************************************************
  279. * Function Name : DAC_SetChannel2Data
  280. * Description : Set the specified data holding register value for DAC channel2.
  281. * Input : - DAC_Align: Specifies the data alignement for DAC channel2.
  282. * This parameter can be one of the following values:
  283. * - DAC_Align_8b_R: 8bit right data alignement selected
  284. * - DAC_Align_12b_L: 12bit left data alignement selected
  285. * - DAC_Align_12b_R: 12bit right data alignement selected
  286. * - Data : Data to be loaded in the selected data holding
  287. * register.
  288. * Output : None
  289. * Return : None
  290. *******************************************************************************/
  291. void DAC_SetChannel2Data(u32 DAC_Align, u16 Data)
  292. {
  293. /* Check the parameters */
  294. assert_param(IS_DAC_ALIGN(DAC_Align));
  295. assert_param(IS_DAC_DATA(Data));
  296. /* Set the DAC channel2 selected data holding register */
  297. *((vu32 *)(DAC_BASE + DHR12R2_Offset + DAC_Align)) = (u32)Data;
  298. }
  299. /*******************************************************************************
  300. * Function Name : DAC_SetDualChannelData
  301. * Description : Set the specified data holding register value for dual channel
  302. * DAC.
  303. * Input : - DAC_Align: Specifies the data alignement for dual channel DAC.
  304. * This parameter can be one of the following values:
  305. * - DAC_Align_8b_R: 8bit right data alignement selected
  306. * - DAC_Align_12b_L: 12bit left data alignement selected
  307. * - DAC_Align_12b_R: 12bit right data alignement selected
  308. * - Data2: Data for DAC Channel2 to be loaded in the selected data
  309. * holding register.
  310. * - Data1: Data for DAC Channel1 to be loaded in the selected data
  311. * holding register.
  312. * Output : None
  313. * Return : None
  314. *******************************************************************************/
  315. void DAC_SetDualChannelData(u32 DAC_Align, u16 Data2, u16 Data1)
  316. {
  317. u32 data = 0;
  318. /* Check the parameters */
  319. assert_param(IS_DAC_ALIGN(DAC_Align));
  320. assert_param(IS_DAC_DATA(Data1));
  321. assert_param(IS_DAC_DATA(Data2));
  322. /* Calculate and set dual DAC data holding register value */
  323. if (DAC_Align == DAC_Align_8b_R)
  324. {
  325. data = ((u32)Data2 << 8) | Data1;
  326. }
  327. else
  328. {
  329. data = ((u32)Data2 << 16) | Data1;
  330. }
  331. /* Set the dual DAC selected data holding register */
  332. *((vu32 *)(DAC_BASE + DHR12RD_Offset + DAC_Align)) = data;
  333. }
  334. /*******************************************************************************
  335. * Function Name : DAC_GetDataOutputValue
  336. * Description : Returns the last data output value of the selected DAC cahnnel.
  337. * Input - DAC_Channel: the selected DAC channel.
  338. * This parameter can be one of the following values:
  339. * - DAC_Channel_1: DAC Channel1 selected
  340. * - DAC_Channel_2: DAC Channel2 selected
  341. * Output : None
  342. * Return : The selected DAC channel data output value.
  343. *******************************************************************************/
  344. u16 DAC_GetDataOutputValue(u32 DAC_Channel)
  345. {
  346. /* Check the parameters */
  347. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  348. /* Returns the DAC channel data output register value */
  349. return (u16) (*(vu32*)(DAC_BASE + DOR_Offset + ((u32)DAC_Channel >> 2)));
  350. }
  351. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/