]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libchip_samv7/source/dacc.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_IAR_Keil / libchip_samv7 / source / dacc.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2011, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /** \addtogroup dacc_module Working with DACC\r
31  *  \ingroup peripherals_module\r
32  * The DACC driver provides the interface to configure and use the DACC peripheral.\n\r
33  *\r
34  * The DACC(Digital-to-Analog Converter Controller) converts digital code to analog output.\r
35  * The data to be converted are sent in a common register for all channels. It offers up to 2\r
36  * analog outputs.The output voltage ranges from (1/6)ADVREF to (5/6)ADVREF.\r
37  *\r
38  * To Enable a DACC conversion,the user has to follow these few steps:\r
39  * <ul>\r
40  * <li> Select an appropriate reference voltage on ADVREF   </li>\r
41  * <li> Configure the DACC according to its requirements and special needs,which could be\r
42         broken down into several parts:\r
43  * -#   Enable DACC in free running mode by clearing TRGEN in DACC_MR;\r
44  * -#   Configure Startup Time and Refresh Period through setting STARTUP and REFRESH fields\r
45  *      in DACC_MR; The refresh mechanism is used to protect the output analog value from\r
46  *      decreasing.\r
47  * -#   Enable channels and write digital code to DACC_CDR,in free running mode, the conversion\r
48  *      is started right after at least one channel is enabled and data is written .\r
49    </li>\r
50  * </ul>\r
51  *\r
52  * For more accurate information, please look at the DACC section of the\r
53  * Datasheet.\r
54  *\r
55  * Related files :\n\r
56  * \ref DACC.c\n\r
57  * \ref DACC.h\n\r
58 */\r
59 /*@{*/\r
60 /*@}*/\r
61 /**\r
62  * \file\r
63  *\r
64  * Implementation of Digital-to-Analog Converter Controller (DACC).\r
65  *\r
66  */\r
67 /*----------------------------------------------------------------------------\r
68  *        Headers\r
69  *----------------------------------------------------------------------------*/\r
70 \r
71 #include "chip.h"\r
72 \r
73 #include <stdint.h>\r
74 #include <assert.h>\r
75 \r
76 /*----------------------------------------------------------------------------\r
77  *        Exported functions\r
78  *----------------------------------------------------------------------------*/\r
79 \r
80 /**\r
81   * \brief Initialize the DACC controller\r
82   * \param pDACC Pointer to an DACC instance.\r
83   * \param idDACC identifier of DAC peripheral\r
84   * \param trgEn trigger mode, free running mode or external Hardware trigger\r
85   * \param word transfer size,word or half word\r
86   * \param trgSel hardware trigger selection\r
87   * \param sleepMode sleep mode selection\r
88   * \param mck value of MCK in Hz\r
89   * \param refresh refresh period\r
90   * \param user_sel user channel selection ,0 or 1\r
91   * \param tag_mode tag for channel number\r
92   * \param startup value of the start up time (in DACCClock) (see datasheet)\r
93 */\r
94 extern void DACC_Initialize( Dacc* pDACC,\r
95                              uint8_t idDACC,\r
96                              uint8_t trgEn,\r
97                              uint8_t trgSel,\r
98                              uint8_t word,\r
99                              uint8_t sleepMode,\r
100                              uint32_t mck,\r
101                              uint8_t refresh,    /* refresh period */\r
102                              uint8_t user_sel,   /* user channel selection */\r
103                              uint32_t tag_mode,  /* using tag for channel number */\r
104                              uint32_t startup\r
105                             )\r
106 {\r
107     /* Stop warning */\r
108     mck = mck;\r
109 \r
110     /* Enable peripheral clock*/\r
111     PMC->PMC_PCER0 = 1 << idDACC;\r
112 \r
113     /*  Reset the controller */\r
114     DACC_SoftReset(pDACC);\r
115 \r
116     /*  Write to the MR register */\r
117     DACC_CfgModeReg( pDACC,\r
118           ( trgEn & DACC_MR_TRGEN)\r
119         |   DACC_MR_TRGSEL(trgSel)\r
120         | ( word & DACC_MR_WORD)\r
121         | ( sleepMode & DACC_MR_SLEEP)\r
122         |   DACC_MR_REFRESH(refresh)\r
123         | ( user_sel & DACC_MR_USER_SEL_Msk)\r
124         | ( tag_mode &  DACC_MR_TAG)\r
125         | ( startup & DACC_MR_STARTUP_Msk));\r
126 }\r
127 \r
128 \r
129 /**\r
130  * Set the Conversion Data\r
131  * \param pDACC Pointer to an Dacc instance.\r
132  * \param data  date to be converted.\r
133  */\r
134 extern void DACC_SetConversionData( Dacc* pDACC, uint32_t dwData )\r
135 {\r
136     uint32_t dwMR = pDACC->DACC_MR ;\r
137 \r
138     if ( dwMR & DACC_MR_WORD )\r
139     {\r
140         pDACC->DACC_CDR = dwData ;\r
141     }\r
142     else\r
143     {\r
144         pDACC->DACC_CDR = (dwData&0xFFFF) ;\r
145     }\r
146 }\r
147 \r
148 \r
149 /**\r
150   * \brief Write converted data through PDC channel\r
151   * \param pDACC the pointer of DACC peripheral\r
152   * \param pBuffer the destination buffer\r
153   * \param size the size of the buffer\r
154 */\r
155 extern uint32_t DACC_WriteBuffer( Dacc* pDACC, uint16_t *pwBuffer, uint32_t dwSize )\r
156 {\r
157 \r
158     /* Check if the first PDC bank is free*/\r
159     if ( (pDACC->DACC_TCR == 0) && (pDACC->DACC_TNCR == 0) )\r
160     {\r
161         pDACC->DACC_TPR = (uint32_t)pwBuffer ;\r
162         pDACC->DACC_TCR = dwSize ;\r
163         pDACC->DACC_PTCR = DACC_PTCR_TXTEN ;\r
164 \r
165         return 1 ;\r
166     }\r
167     /* Check if the second PDC bank is free*/\r
168     else\r
169     {\r
170         if (pDACC->DACC_TNCR == 0)\r
171         {\r
172             pDACC->DACC_TNPR = (uint32_t)pwBuffer ;\r
173             pDACC->DACC_TNCR = dwSize ;\r
174 \r
175             return 1 ;\r
176         }\r
177         else\r
178         {\r
179             return 0 ;\r
180         }\r
181     }\r
182 \r
183 }\r
184 \r