opamp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Operational Amplifier Controller (OPAMP) Driver
  5. *
  6. * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "opamp.h"
  47. void opamp_module_init(void)
  48. {
  49. struct system_clock_source_osculp32k_config config;
  50. /* Enable the OSCULP32K clock. */
  51. system_clock_source_osculp32k_get_config_defaults(&config);
  52. system_clock_source_osculp32k_set_config(&config);
  53. /* Turn on the digital interface clock. */
  54. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBD, MCLK_APBDMASK_OPAMP);
  55. /* Reset module. */
  56. opamp_module_reset();
  57. /* Enable module. */
  58. opamp_module_enable();
  59. }
  60. static inline void _opamp_get_config_common_defaults(
  61. struct opamp_config_common *const config)
  62. {
  63. /* Sanity check arguments */
  64. Assert(config);
  65. /* Default configuration values */
  66. config->potentiometer_selection = OPAMP_POT_MUX_8R_8R;
  67. config->r1_enable = false;
  68. config->r2_vcc = false;
  69. config->r2_out = false;
  70. config->on_demand = false;
  71. config->run_in_standby = false;
  72. config->bias_value = OPAMP_BIAS_MODE_0;
  73. config->analog_out = false;
  74. }
  75. void opamp0_get_config_defaults(struct opamp0_config *const config)
  76. {
  77. /* Sanity check arguments */
  78. Assert(config);
  79. _opamp_get_config_common_defaults(&(config->config_common));
  80. /* Default configuration values */
  81. config->negative_input = OPAMP0_NEG_MUX_OUT0;
  82. config->positive_input = OPAMP0_POS_MUX_PIN0;
  83. config->r1_connection = OPAMP0_RES1_MUX_GND;
  84. }
  85. void opamp1_get_config_defaults(struct opamp1_config *const config)
  86. {
  87. /* Sanity check arguments */
  88. Assert(config);
  89. _opamp_get_config_common_defaults(&(config->config_common));
  90. /* Default configuration values */
  91. config->negative_input = OPAMP1_NEG_MUX_OUT1;
  92. config->positive_input = OPAMP1_POS_MUX_PIN1;
  93. config->r1_connection = OPAMP1_RES1_MUX_GND;
  94. }
  95. void opamp2_get_config_defaults(struct opamp2_config *const config)
  96. {
  97. /* Sanity check arguments */
  98. Assert(config);
  99. _opamp_get_config_common_defaults(&(config->config_common));
  100. /* Default configuration values */
  101. config->negative_input = OPAMP2_NEG_MUX_OUT2;
  102. config->positive_input = OPAMP2_POS_MUX_PIN2;
  103. config->r1_connection = OPAMP2_RES1_MUX_GND;
  104. }
  105. void opamp0_set_config(struct opamp0_config *const config)
  106. {
  107. uint32_t temp = 0;
  108. if (config->config_common.r1_enable) {
  109. temp |= OPAMP_OPAMPCTRL_RES1EN;
  110. }
  111. if (config->config_common.r2_vcc) {
  112. temp |= OPAMP_OPAMPCTRL_RES2VCC;
  113. }
  114. if (config->config_common.r2_out) {
  115. temp |= OPAMP_OPAMPCTRL_RES2OUT;
  116. }
  117. if (config->config_common.on_demand) {
  118. temp |= OPAMP_OPAMPCTRL_ONDEMAND;
  119. }
  120. if (config->config_common.run_in_standby) {
  121. temp |= OPAMP_OPAMPCTRL_RUNSTDBY;
  122. }
  123. if (config->config_common.analog_out) {
  124. temp |= OPAMP_OPAMPCTRL_ANAOUT;
  125. }
  126. OPAMP->OPAMPCTRL[0].reg = temp |
  127. config->config_common.potentiometer_selection |
  128. config->config_common.bias_value |
  129. config->negative_input |
  130. config->positive_input|
  131. config->r1_connection;
  132. }
  133. void opamp1_set_config(struct opamp1_config *const config)
  134. {
  135. uint32_t temp = 0;
  136. if (config->config_common.r1_enable) {
  137. temp |= OPAMP_OPAMPCTRL_RES1EN;
  138. }
  139. if (config->config_common.r2_vcc) {
  140. temp |= OPAMP_OPAMPCTRL_RES2VCC;
  141. }
  142. if (config->config_common.r2_out) {
  143. temp |= OPAMP_OPAMPCTRL_RES2OUT;
  144. }
  145. if (config->config_common.on_demand) {
  146. temp |= OPAMP_OPAMPCTRL_ONDEMAND;
  147. }
  148. if (config->config_common.run_in_standby) {
  149. temp |= OPAMP_OPAMPCTRL_RUNSTDBY;
  150. }
  151. if (config->config_common.analog_out) {
  152. temp |= OPAMP_OPAMPCTRL_ANAOUT;
  153. }
  154. OPAMP->OPAMPCTRL[1].reg = temp |
  155. config->config_common.potentiometer_selection |
  156. config->config_common.bias_value |
  157. config->negative_input |
  158. config->positive_input|
  159. config->r1_connection;
  160. }
  161. void opamp2_set_config(struct opamp2_config *const config)
  162. {
  163. uint32_t temp = 0;
  164. if (config->config_common.r1_enable) {
  165. temp |= OPAMP_OPAMPCTRL_RES1EN;
  166. }
  167. if (config->config_common.r2_vcc) {
  168. temp |= OPAMP_OPAMPCTRL_RES2VCC;
  169. }
  170. if (config->config_common.r2_out) {
  171. temp |= OPAMP_OPAMPCTRL_RES2OUT;
  172. }
  173. if (config->config_common.on_demand) {
  174. temp |= OPAMP_OPAMPCTRL_ONDEMAND;
  175. }
  176. if (config->config_common.run_in_standby) {
  177. temp |= OPAMP_OPAMPCTRL_RUNSTDBY;
  178. }
  179. if (config->config_common.analog_out) {
  180. temp |= OPAMP_OPAMPCTRL_ANAOUT;
  181. }
  182. OPAMP->OPAMPCTRL[2].reg = temp |
  183. config->config_common.potentiometer_selection |
  184. config->config_common.bias_value |
  185. config->negative_input |
  186. config->positive_input|
  187. config->r1_connection;
  188. }
  189. void opamp_enable(const enum opamp_id number)
  190. {
  191. /* Sanity check arguments */
  192. Assert(number);
  193. /* Enable the OPAMP */
  194. if (number == OPAMP_0) {
  195. OPAMP->OPAMPCTRL[0].reg |= OPAMP_OPAMPCTRL_ENABLE;
  196. } else if (number == OPAMP_1) {
  197. OPAMP->OPAMPCTRL[1].reg |= OPAMP_OPAMPCTRL_ENABLE;
  198. } else if (number == OPAMP_2) {
  199. OPAMP->OPAMPCTRL[2].reg |= OPAMP_OPAMPCTRL_ENABLE;
  200. }
  201. }
  202. void opamp_disable(const enum opamp_id number)
  203. {
  204. /* Sanity check arguments */
  205. Assert(number);
  206. /* Disable the OPAMP */
  207. if (number == OPAMP_0) {
  208. OPAMP->OPAMPCTRL[0].reg &= ~OPAMP_OPAMPCTRL_ENABLE;
  209. } else if (number == OPAMP_1) {
  210. OPAMP->OPAMPCTRL[1].reg &= ~OPAMP_OPAMPCTRL_ENABLE;
  211. } else if (number == OPAMP_2) {
  212. OPAMP->OPAMPCTRL[2].reg &= ~OPAMP_OPAMPCTRL_ENABLE;
  213. }
  214. }
  215. bool opamp_is_ready(const enum opamp_id number)
  216. {
  217. /* Sanity check arguments */
  218. Assert(number);
  219. /* Get the OPAMP output ready status*/
  220. if (number == OPAMP_0) {
  221. return OPAMP->STATUS.bit.READY0;
  222. } else if (number == OPAMP_1) {
  223. return OPAMP->STATUS.bit.READY1;
  224. } else if (number == OPAMP_2) {
  225. return OPAMP->STATUS.bit.READY2;
  226. }
  227. return false;
  228. }