]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/drivers/peripherals/pwmc.c
Add SAMA5D2 Xplained IAR demo.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / AtmelFiles / drivers / peripherals / pwmc.c
diff --git a/FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/drivers/peripherals/pwmc.c b/FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/AtmelFiles/drivers/peripherals/pwmc.c
new file mode 100644 (file)
index 0000000..20a7815
--- /dev/null
@@ -0,0 +1,147 @@
+/* ----------------------------------------------------------------------------\r
+ *         SAM Software Package License\r
+ * ----------------------------------------------------------------------------\r
+ * Copyright (c) 2015, 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 pwm_module Working with PWM\r
+ * \section Purpose\r
+ * The PWM driver provides the interface to configure and use the PWM\r
+ * peripheral.\r
+ *\r
+ * The PWM macrocell controls square output waveforms of 4 channels.\r
+ * Characteristics of output waveforms such as period, duty-cycle can be configured.\n\r
+ *\r
+ * Before enabling the channels, they must have been configured first.\r
+ * The main settings include:\r
+ * <ul>\r
+ * <li>Configuration of the clock generator.</li>\r
+ * <li>Selection of the clock for each channel.</li>\r
+ * <li>Configuration of output waveform characteristics, such as period, duty-cycle etc.</li>\r
+ * </ul>\r
+ *\r
+ * After the channels is enabled, the user must use respective update registers\r
+ * to change the wave characteristics to prevent unexpected output waveform.\r
+ * i.e. PWM_CUPDx register should be used if user want to change duty-cycle\r
+ * when the channel is enabled.\r
+ *\r
+ * \section Usage\r
+ * <ul>\r
+ * <li>  Configure PWM clock using pwmc_configure_clocks().\r
+ * <li>  Enable & disable given PWM channel using pwmc_enable_channel() and pwmc_disable_channel().\r
+ * <li>  Enable & disable interrupt of given PWM channel using pwmc_enable_channel_it()\r
+ * and pwmc_disable_channel_it().\r
+ * <li>  Set feature of the given PWM channel's output signal using pwmc_set_period()\r
+ * and pwmc_set_duty_cycle().\r
+ * </li>\r
+ * </ul>\r
+ *\r
+ * For more accurate information, please look at the PWM section of the\r
+ * Datasheet.\r
+ *\r
+ * Related files :\n\r
+ * \ref pwmc.c\n\r
+ * \ref pwmc.h.\n\r
+ */\r
+/*@{*/\r
+/*@}*/\r
+\r
+/**\r
+ * \file\r
+ *\r
+ * Implementation of the Pulse Width Modulation Controller (PWM) peripheral.\r
+ *\r
+ */\r
+\r
+/*----------------------------------------------------------------------------\r
+ *        Headers\r
+ *----------------------------------------------------------------------------*/\r
+\r
+#include "chip.h"\r
+#include "peripherals/pwmc.h"\r
+\r
+#include <stdint.h>\r
+#include <assert.h>\r
+\r
+/*----------------------------------------------------------------------------\r
+ *        Exported functions\r
+ *----------------------------------------------------------------------------*/\r
+\r
+void pwmc_configure_clocks(Pwm * p_pwm, uint32_t mode)\r
+{\r
+       p_pwm->PWM_CLK = mode;\r
+}\r
+\r
+void pwmc_enable_channel(Pwm * p_pwm, uint8_t channel)\r
+{\r
+       p_pwm->PWM_ENA = 0x1ul << channel;\r
+}\r
+\r
+void pwmc_disable_channel(Pwm * p_pwm, uint8_t channel)\r
+{\r
+       p_pwm->PWM_DIS = 0x1ul << channel;\r
+}\r
+\r
+void pwmc_enable_channel_it(Pwm * p_pwm, uint8_t channel)\r
+{\r
+       p_pwm->PWM_IER1 = 0x1ul << channel;\r
+}\r
+\r
+void pwmc_disable_channel_it(Pwm * p_pwm, uint8_t channel)\r
+{\r
+       p_pwm->PWM_IDR1 = 0x1ul << channel;\r
+}\r
+\r
+void pwmc_configure_channel(Pwm * p_pwm, uint8_t channel, uint32_t mode)\r
+{\r
+       p_pwm->PWM_CH_NUM[channel].PWM_CMR = mode;\r
+}\r
+\r
+void pwmc_set_period(Pwm * p_pwm, uint8_t channel, uint16_t period)\r
+{\r
+       /* If channel is disabled, write to CPRD */\r
+       if ((p_pwm->PWM_SR & (1 << channel)) == 0) {\r
+               p_pwm->PWM_CH_NUM[channel].PWM_CPRD = period;\r
+       }\r
+       /* Otherwise use update register */\r
+       else {\r
+               p_pwm->PWM_CH_NUM[channel].PWM_CPRDUPD = period;\r
+       }\r
+}\r
+\r
+void pwmc_set_duty_cycle(Pwm * p_pwm, uint8_t channel, uint16_t duty)\r
+{\r
+       assert(duty <= p_pwm->PWM_CH_NUM[channel].PWM_CPRD);\r
+\r
+       /* If channel is disabled, write to CDTY */\r
+       if ((p_pwm->PWM_SR & (1 << channel)) == 0) {\r
+               p_pwm->PWM_CH_NUM[channel].PWM_CDTY = duty;\r
+       }\r
+       /* Otherwise use update register */\r
+       else {\r
+               p_pwm->PWM_CH_NUM[channel].PWM_CDTYUPD = duty;\r
+       }\r
+}\r