]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/acc.c
Kernel changes:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained / libchip_samv7 / source / acc.c
diff --git a/FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/acc.c b/FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/acc.c
new file mode 100644 (file)
index 0000000..7a825de
--- /dev/null
@@ -0,0 +1,163 @@
+/* ----------------------------------------------------------------------------\r
+ *         SAM Software Package License\r
+ * ----------------------------------------------------------------------------\r
+ * Copyright (c) 2011, Atmel Corporation\r
+ *\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ *\r
+ * - Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the disclaimer below.\r
+ *\r
+ * Atmel's name may not be used to endorse or promote products derived from\r
+ * this software without specific prior written permission.\r
+ *\r
+ * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * ----------------------------------------------------------------------------\r
+ */\r
+\r
+/** \addtogroup acc_module Working with ACC\r
+ *  \ingroup peripherals_module\r
+ * The ACC driver provides the interface to configure and use the ACC peripheral.\n\r
+ *\r
+ * It applies comparison on two inputs and gives a compare output.\r
+ *\r
+ * To Enable a ACC Comparison,the user has to follow these few steps:\r
+ * <ul>\r
+ * <li> Enable ACC peripheral clock by setting the corresponding bit in PMC_PCER1\r
+ *      (PMC Peripheral Clock Enable Register 1)\r
+ * </li>\r
+ * <li> Reset the controller by asserting ACC_CR_SWRST in ACC_CR(ACC Control Register)\r
+ </li>\r
+ * <li> Configure the mode as following steps:  </li>\r
+ * -#   Select inputs for SELMINUS and SELPLUS in ACC_MR (ACC Mode Register).\r
+ * -#   Enable Analog Comparator by setting ACEN in ACC_MR.\r
+ * -#   Configure Edge Type to detect different compare output.\r
+ * </li>\r
+ * <li> Wait until the automatic mask period expires by polling MASK bit in\r
+ *      ACC_ISR.\r
+ * </ul>\r
+ *\r
+ * For more accurate information, please look at the ACC section of the\r
+ * Datasheet.\r
+ *\r
+ * Related files :\n\r
+ * \ref acc.c\n\r
+ * \ref acc.h\n\r
+ */\r
+/*@{*/\r
+/*@}*/\r
+/**\r
+ * \file\r
+ *\r
+ * Implementation of Analog Comparator Controller (ACC).\r
+ *\r
+ */\r
+/*----------------------------------------------------------------------------\r
+ *        Headers\r
+ *----------------------------------------------------------------------------*/\r
+\r
+#include "chip.h"\r
+\r
+/*----------------------------------------------------------------------------\r
+ *        Exported functions\r
+ *----------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * \brief Initialize the ACC controller\r
+ *\r
+ * \param pAcc Pointer to an Acc instance.\r
+ * \param idAcc ACC identifier\r
+ * \param ucSelplus input connected to inp, 0~7\r
+ * \param ucSelminus input connected to inm,0~7\r
+ * \param wAc_en Analog comprator enabled/disabled\r
+ * \param wEdge CF flag triggering mode\r
+ * \param wInvert INVert comparator output,use pattern defined in the device header file\r
+ */\r
+extern void ACC_Configure( Acc *pAcc, uint8_t idAcc, uint8_t ucSelplus, uint8_t ucSelminus,\r
+                           uint16_t wAc_en, uint16_t wEdge, uint16_t wInvert )\r
+{\r
+    /* Enable peripheral clock*/\r
+    PMC->PMC_PCER1 = 1 << (idAcc - 32) ;\r
+\r
+    /*  Reset the controller */\r
+    pAcc->ACC_CR |= ACC_CR_SWRST ;\r
+\r
+    /*  Write to the MR register */\r
+    ACC_CfgModeReg( pAcc,\r
+                    ( (ucSelplus<<ACC_MR_SELPLUS_Pos) & ACC_MR_SELPLUS_Msk ) |\r
+                    ( (ucSelminus<<ACC_MR_SELMINUS_Pos) & ACC_MR_SELMINUS_Msk ) |\r
+                    ( (wAc_en<<8) & ACC_MR_ACEN ) |\r
+                    ( (wEdge<<ACC_MR_EDGETYP_Pos) & ACC_MR_EDGETYP_Msk ) |\r
+                    ( (wInvert<<12) & ACC_MR_INV ) ) ;\r
+    /* set hysteresis and current option*/\r
+    pAcc->ACC_ACR = (ACC_ACR_ISEL_HISP | ((0x01 << ACC_ACR_HYST_Pos) & ACC_ACR_HYST_Msk));\r
+\r
+    /* Automatic Output Masking Period*/\r
+    while ( pAcc->ACC_ISR & (uint32_t)ACC_ISR_MASK ) ;\r
+}\r
+\r
+/**\r
+ * Return the Channel Converted Data\r
+ * \param pAcc Pointer to an Acc instance.\r
+ * \param selplus input applied on ACC SELPLUS\r
+ * \param selminus input applied on ACC SELMINUS\r
+ */\r
+extern void ACC_SetComparisonPair( Acc *pAcc, uint8_t ucSelplus, uint8_t ucSelminus )\r
+{\r
+    uint32_t dwTemp ;\r
+\r
+    assert( ucSelplus < 8 && ucSelminus < 8 ) ;\r
+\r
+    dwTemp = pAcc->ACC_MR ;\r
+\r
+    pAcc->ACC_MR = dwTemp & (uint32_t) ((~ACC_MR_SELMINUS_Msk) & (~ACC_MR_SELPLUS_Msk));\r
+\r
+    pAcc->ACC_MR |= ( ((ucSelplus << ACC_MR_SELPLUS_Pos) & ACC_MR_SELPLUS_Msk) |\r
+                      ((ucSelminus << ACC_MR_SELMINUS_Pos) & ACC_MR_SELMINUS_Msk) ) ;\r
+\r
+}\r
+/**\r
+ * Return Comparison Result\r
+ * \param pAcc Pointer to an Acc instance.\r
+ * \param status value of ACC_ISR\r
+ */\r
+extern uint32_t ACC_GetComparisonResult( Acc *pAcc, uint32_t dwStatus )\r
+{\r
+    uint32_t dwTemp = pAcc->ACC_MR ;\r
+\r
+    if ( (dwTemp & ACC_MR_INV) == ACC_MR_INV )\r
+    {\r
+        if ( dwStatus & ACC_ISR_SCO )\r
+        {\r
+            return 0 ; /* inn>inp*/\r
+        }\r
+        else\r
+        {\r
+            return 1 ;/* inp>inn*/\r
+        }\r
+    }\r
+    else\r
+    {\r
+        if ( dwStatus & ACC_ISR_SCO )\r
+        {\r
+            return 1 ; /* inp>inn*/\r
+        }\r
+        else\r
+        {\r
+            return 0 ;/* inn>inp*/\r
+        }\r
+    }\r
+}\r
+\r