]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/acc.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained / libchip_samv7 / source / acc.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 acc_module Working with ACC\r
31  *  \ingroup peripherals_module\r
32  * The ACC driver provides the interface to configure and use the ACC peripheral.\n\r
33  *\r
34  * It applies comparison on two inputs and gives a compare output.\r
35  *\r
36  * To Enable a ACC Comparison,the user has to follow these few steps:\r
37  * <ul>\r
38  * <li> Enable ACC peripheral clock by setting the corresponding bit in PMC_PCER1\r
39  *      (PMC Peripheral Clock Enable Register 1)\r
40  * </li>\r
41  * <li> Reset the controller by asserting ACC_CR_SWRST in ACC_CR(ACC Control Register)\r
42  </li>\r
43  * <li> Configure the mode as following steps:  </li>\r
44  * -#   Select inputs for SELMINUS and SELPLUS in ACC_MR (ACC Mode Register).\r
45  * -#   Enable Analog Comparator by setting ACEN in ACC_MR.\r
46  * -#   Configure Edge Type to detect different compare output.\r
47  * </li>\r
48  * <li> Wait until the automatic mask period expires by polling MASK bit in\r
49  *      ACC_ISR.\r
50  * </ul>\r
51  *\r
52  * For more accurate information, please look at the ACC section of the\r
53  * Datasheet.\r
54  *\r
55  * Related files :\n\r
56  * \ref acc.c\n\r
57  * \ref acc.h\n\r
58  */\r
59 /*@{*/\r
60 /*@}*/\r
61 /**\r
62  * \file\r
63  *\r
64  * Implementation of Analog Comparator Controller (ACC).\r
65  *\r
66  */\r
67 /*----------------------------------------------------------------------------\r
68  *        Headers\r
69  *----------------------------------------------------------------------------*/\r
70 \r
71 #include "chip.h"\r
72 \r
73 /*----------------------------------------------------------------------------\r
74  *        Exported functions\r
75  *----------------------------------------------------------------------------*/\r
76 \r
77 /**\r
78  * \brief Initialize the ACC controller\r
79  *\r
80  * \param pAcc Pointer to an Acc instance.\r
81  * \param idAcc ACC identifier\r
82  * \param ucSelplus input connected to inp, 0~7\r
83  * \param ucSelminus input connected to inm,0~7\r
84  * \param wAc_en Analog comprator enabled/disabled\r
85  * \param wEdge CF flag triggering mode\r
86  * \param wInvert INVert comparator output,use pattern defined in the device header file\r
87  */\r
88 extern void ACC_Configure( Acc *pAcc, uint8_t idAcc, uint8_t ucSelplus, uint8_t ucSelminus,\r
89                            uint16_t wAc_en, uint16_t wEdge, uint16_t wInvert )\r
90 {\r
91     /* Enable peripheral clock*/\r
92     PMC->PMC_PCER1 = 1 << (idAcc - 32) ;\r
93 \r
94     /*  Reset the controller */\r
95     pAcc->ACC_CR |= ACC_CR_SWRST ;\r
96 \r
97     /*  Write to the MR register */\r
98     ACC_CfgModeReg( pAcc,\r
99                     ( (ucSelplus<<ACC_MR_SELPLUS_Pos) & ACC_MR_SELPLUS_Msk ) |\r
100                     ( (ucSelminus<<ACC_MR_SELMINUS_Pos) & ACC_MR_SELMINUS_Msk ) |\r
101                     ( (wAc_en<<8) & ACC_MR_ACEN ) |\r
102                     ( (wEdge<<ACC_MR_EDGETYP_Pos) & ACC_MR_EDGETYP_Msk ) |\r
103                     ( (wInvert<<12) & ACC_MR_INV ) ) ;\r
104     /* set hysteresis and current option*/\r
105     pAcc->ACC_ACR = (ACC_ACR_ISEL_HISP | ((0x01 << ACC_ACR_HYST_Pos) & ACC_ACR_HYST_Msk));\r
106 \r
107     /* Automatic Output Masking Period*/\r
108     while ( pAcc->ACC_ISR & (uint32_t)ACC_ISR_MASK ) ;\r
109 }\r
110 \r
111 /**\r
112  * Return the Channel Converted Data\r
113  * \param pAcc Pointer to an Acc instance.\r
114  * \param selplus input applied on ACC SELPLUS\r
115  * \param selminus input applied on ACC SELMINUS\r
116  */\r
117 extern void ACC_SetComparisonPair( Acc *pAcc, uint8_t ucSelplus, uint8_t ucSelminus )\r
118 {\r
119     uint32_t dwTemp ;\r
120 \r
121     assert( ucSelplus < 8 && ucSelminus < 8 ) ;\r
122 \r
123     dwTemp = pAcc->ACC_MR ;\r
124 \r
125     pAcc->ACC_MR = dwTemp & (uint32_t) ((~ACC_MR_SELMINUS_Msk) & (~ACC_MR_SELPLUS_Msk));\r
126 \r
127     pAcc->ACC_MR |= ( ((ucSelplus << ACC_MR_SELPLUS_Pos) & ACC_MR_SELPLUS_Msk) |\r
128                       ((ucSelminus << ACC_MR_SELMINUS_Pos) & ACC_MR_SELMINUS_Msk) ) ;\r
129 \r
130 }\r
131 /**\r
132  * Return Comparison Result\r
133  * \param pAcc Pointer to an Acc instance.\r
134  * \param status value of ACC_ISR\r
135  */\r
136 extern uint32_t ACC_GetComparisonResult( Acc *pAcc, uint32_t dwStatus )\r
137 {\r
138     uint32_t dwTemp = pAcc->ACC_MR ;\r
139 \r
140     if ( (dwTemp & ACC_MR_INV) == ACC_MR_INV )\r
141     {\r
142         if ( dwStatus & ACC_ISR_SCO )\r
143         {\r
144             return 0 ; /* inn>inp*/\r
145         }\r
146         else\r
147         {\r
148             return 1 ;/* inp>inn*/\r
149         }\r
150     }\r
151     else\r
152     {\r
153         if ( dwStatus & ACC_ISR_SCO )\r
154         {\r
155             return 1 ; /* inp>inn*/\r
156         }\r
157         else\r
158         {\r
159             return 0 ;/* inn>inp*/\r
160         }\r
161     }\r
162 }\r
163 \r