ccl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Configurable Custom Logic (CCL) Driver
  5. *
  6. * Copyright (C) 2014-2015 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 "ccl.h"
  47. void ccl_init(struct ccl_config *const config)
  48. {
  49. #if (SAML22) || (SAMC20) || (SAMC21)
  50. /* Turn on the digital interface clock. */
  51. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_CCL);
  52. #else
  53. /* Turn on the digital interface clock. */
  54. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBD, MCLK_APBDMASK_CCL);
  55. #endif
  56. /* Reset module. */
  57. ccl_module_reset();
  58. /* Configure GCLK channel and enable clock */
  59. struct system_gclk_chan_config gclk_chan_conf;
  60. system_gclk_chan_get_config_defaults(&gclk_chan_conf);
  61. gclk_chan_conf.source_generator = config->clock_source;
  62. system_gclk_chan_set_config(CCL_GCLK_ID, &gclk_chan_conf);
  63. system_gclk_chan_enable(CCL_GCLK_ID);
  64. if(config->run_in_standby) {
  65. /* Enable run in standy mode. */
  66. CCL->CTRL.reg |= CCL_CTRL_RUNSTDBY;
  67. } else {
  68. /* Disable run in standy mode. */
  69. CCL->CTRL.reg &= ~ CCL_CTRL_RUNSTDBY;
  70. }
  71. }
  72. void ccl_lut_get_config_defaults(struct ccl_lut_config *const config)
  73. {
  74. /* Sanity check arguments */
  75. Assert(config);
  76. /* Default configuration values */
  77. config->truth_table_value = 0x00;
  78. config->event_output_enable = false;
  79. config->event_input_enable = false;
  80. config->event_input_inverted_enable = false;
  81. config->input0_src_sel = CCL_LUT_INPUT_SRC_MASK;
  82. config->input1_src_sel = CCL_LUT_INPUT_SRC_MASK;
  83. config->input2_src_sel = CCL_LUT_INPUT_SRC_MASK;
  84. config->edge_selection_enable = false;
  85. config->filter_sel = CCL_LUT_FILTER_DISABLE;
  86. }
  87. enum status_code ccl_lut_set_config(const enum ccl_lut_id number,
  88. struct ccl_lut_config *const config)
  89. {
  90. /* Sanity check arguments */
  91. Assert(config);
  92. uint32_t temp = 0;
  93. if(CCL->CTRL.reg & CCL_CTRL_ENABLE)
  94. return STATUS_BUSY;
  95. if (config->event_output_enable) {
  96. temp |= CCL_LUTCTRL_LUTEO;
  97. }
  98. if (config->event_input_enable) {
  99. temp |= CCL_LUTCTRL_LUTEI;
  100. }
  101. if (config->event_input_inverted_enable) {
  102. temp |= CCL_LUTCTRL_INVEI;
  103. }
  104. if (config->edge_selection_enable) {
  105. temp |= CCL_LUTCTRL_EDGESEL;
  106. }
  107. CCL->LUTCTRL[number].reg = temp |
  108. CCL_LUTCTRL_INSEL0(config->input0_src_sel) |
  109. CCL_LUTCTRL_INSEL1(config->input1_src_sel) |
  110. CCL_LUTCTRL_INSEL2(config->input2_src_sel) |
  111. CCL_LUTCTRL_TRUTH(config->truth_table_value) |
  112. config->filter_sel;
  113. return STATUS_OK;
  114. }
  115. enum status_code ccl_seq_config(const enum ccl_seq_id number,
  116. const enum ccl_seq_selection seq_selection)
  117. {
  118. if(CCL->CTRL.reg & CCL_CTRL_ENABLE)
  119. return STATUS_BUSY;
  120. CCL->SEQCTRL[number].reg = seq_selection;
  121. return STATUS_OK;
  122. }
  123. void ccl_lut_enable(const enum ccl_lut_id number)
  124. {
  125. /* Enable the LUTx */
  126. CCL->LUTCTRL[number].reg |= CCL_LUTCTRL_ENABLE;
  127. }
  128. void ccl_lut_disable(const enum ccl_lut_id number)
  129. {
  130. /* Disable the LUTx */
  131. CCL->LUTCTRL[number].reg &= ~CCL_LUTCTRL_ENABLE;
  132. }