tsens.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Temperature Sensor (TSENS) Driver
  5. *
  6. * Copyright (C) 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 "tsens.h"
  47. #define WINDOW_MIN_VALUE -40
  48. #define WINDOW_MAX_VALUE 105
  49. /**
  50. * \internal Writes an TSENS configuration to the hardware module
  51. *
  52. * Writes out a given TSENS module configuration to the hardware module.
  53. *
  54. * \param[in] config Pointer to configuration struct
  55. *
  56. * \return Status of the configuration procedure.
  57. * \retval STATUS_OK The configuration was successful
  58. * \retval STATUS_ERR_INVALID_ARG Invalid argument(s) were provided
  59. */
  60. static enum status_code _tsens_set_config(struct tsens_config *const config)
  61. {
  62. /* Configure GCLK channel and enable clock */
  63. struct system_gclk_chan_config gclk_chan_conf;
  64. system_gclk_chan_get_config_defaults(&gclk_chan_conf);
  65. gclk_chan_conf.source_generator = config->clock_source;
  66. system_gclk_chan_set_config(TSENS_GCLK_ID, &gclk_chan_conf);
  67. system_gclk_chan_enable(TSENS_GCLK_ID);
  68. /* Configure run in standby */
  69. TSENS->CTRLA.reg = (config->run_in_standby << TSENS_CTRLA_RUNSTDBY_Pos);
  70. /* Check validity of window thresholds */
  71. if (config->window.window_mode != TSENS_WINDOW_MODE_DISABLE) {
  72. if((config->window.window_lower_value < WINDOW_MIN_VALUE) || \
  73. (config->window.window_upper_value > WINDOW_MAX_VALUE)) {
  74. return STATUS_ERR_INVALID_ARG;
  75. }
  76. }
  77. /* Configure CTRLC */
  78. TSENS->CTRLC.reg =
  79. (config->free_running << TSENS_CTRLC_FREERUN_Pos) | \
  80. (config->window.window_mode);
  81. #if ERRATA_14476
  82. /* Configure lower threshold */
  83. TSENS->WINLT.reg = TSENS_WINLT_WINLT(config->window.window_upper_value);
  84. /* Configure upper threshold */
  85. TSENS->WINUT.reg = TSENS_WINLT_WINLT(config->window.window_lower_value);
  86. #else
  87. /* Configure lower threshold */
  88. TSENS->WINLT.reg = TSENS_WINLT_WINLT(config->window.window_lower_value);
  89. /* Configure upper threshold */
  90. TSENS->WINUT.reg = TSENS_WINLT_WINLT(config->window.window_upper_value);
  91. #endif
  92. /* Configure events */
  93. TSENS->EVCTRL.reg = config->event_action;
  94. /* Disable all interrupts */
  95. TSENS->INTENCLR.reg =
  96. (1 << TSENS_INTENCLR_OVF_Pos) | (1 << TSENS_INTENCLR_WINMON_Pos) | \
  97. (1 << TSENS_INTENCLR_OVERRUN_Pos) | (1 << TSENS_INTENCLR_RESRDY_Pos);
  98. /* Read calibration from NVM */
  99. uint32_t tsens_bits = *((uint32_t *)NVMCTRL_TEMP_LOG);
  100. uint32_t tsens_tcal = \
  101. ((tsens_bits & TSENS_FUSES_TCAL_Msk) >> TSENS_FUSES_TCAL_Pos);
  102. uint32_t tsens_fcal = \
  103. ((tsens_bits & TSENS_FUSES_FCAL_Msk) >> TSENS_FUSES_FCAL_Pos);
  104. TSENS->CAL.reg = TSENS_CAL_TCAL(tsens_tcal) | TSENS_CAL_FCAL(tsens_fcal);
  105. TSENS->GAIN.reg = TSENS_GAIN_GAIN(config->calibration.gain);
  106. TSENS->OFFSET.reg = TSENS_OFFSET_OFFSETC(config->calibration.offset);
  107. return STATUS_OK;
  108. }
  109. /**
  110. * \brief Initializes the TSENS.
  111. *
  112. * Initializes the TSENS device struct and the hardware module based on the
  113. * given configuration struct values.
  114. *
  115. * \param[in] config Pointer to the configuration struct
  116. *
  117. * \return Status of the initialization procedure.
  118. * \retval STATUS_OK The initialization was successful
  119. * \retval STATUS_ERR_INVALID_ARG Invalid argument(s) were provided
  120. * \retval STATUS_BUSY The module is busy with a reset operation
  121. * \retval STATUS_ERR_DENIED The module is enabled
  122. */
  123. enum status_code tsens_init(struct tsens_config *config)
  124. {
  125. /* Sanity check arguments */
  126. Assert(config);
  127. /* Turn on the digital interface clock */
  128. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, MCLK_APBAMASK_TSENS);
  129. if (TSENS->CTRLA.reg & TSENS_CTRLA_SWRST) {
  130. /* We are in the middle of a reset. Abort. */
  131. return STATUS_BUSY;
  132. }
  133. if (TSENS->CTRLA.reg & TSENS_CTRLA_ENABLE) {
  134. /* Module must be disabled before initialization. Abort. */
  135. return STATUS_ERR_DENIED;
  136. }
  137. /* Write configuration to module */
  138. return _tsens_set_config(config);
  139. }
  140. /**
  141. * \brief Initializes an TSENS configuration structure to defaults.
  142. *
  143. * Initializes a given TSENS configuration struct to a set of known default
  144. * values. This function should be called on any new instance of the
  145. * configuration struct before being modified by the user application.
  146. *
  147. * The default configuration is as follows:
  148. * \li GCLK generator 0 (GCLK main) clock source
  149. * \li All events (input and generation) disabled
  150. * \li Free running disabled
  151. * \li Run in standby disabled
  152. * \li Window monitor disabled
  153. * \li Register GAIN value
  154. * \li Register OFFSET value
  155. *
  156. * \note Register GAIN and OFFSET is loaded from NVM, or can also be fixed.
  157. * If this bitfield is to be fixed, pay attention to the relationship between GCLK
  158. * frequency, GAIN, and resolution. See \ref asfdoc_sam0_tsens_module_overview
  159. * "Chapter Module Overview".
  160. *
  161. * \param[out] config Pointer to configuration struct to initialize to
  162. * default values
  163. */
  164. void tsens_get_config_defaults(struct tsens_config *const config)
  165. {
  166. Assert(config);
  167. config->clock_source = GCLK_GENERATOR_0;
  168. config->free_running = false;
  169. config->run_in_standby = false;
  170. config->window.window_mode = TSENS_WINDOW_MODE_DISABLE;
  171. config->window.window_upper_value = 0;
  172. config->window.window_lower_value = 0;
  173. config->event_action = TSENS_EVENT_ACTION_DISABLED;
  174. uint32_t tsens_bits[2];
  175. tsens_bits[0] = *((uint32_t *)NVMCTRL_TEMP_LOG);
  176. tsens_bits[1] = *(((uint32_t *)NVMCTRL_TEMP_LOG) + 1);
  177. config->calibration.offset = \
  178. ((tsens_bits[0] & TSENS_FUSES_OFFSET_Msk) >> TSENS_FUSES_OFFSET_Pos);
  179. config->calibration.gain = \
  180. ((tsens_bits[0] & TSENS_FUSES_GAIN_0_Msk) >> TSENS_FUSES_GAIN_0_Pos) | \
  181. ((tsens_bits[1] & TSENS_FUSES_GAIN_1_Msk) >> TSENS_FUSES_GAIN_1_Pos);
  182. }
  183. /**
  184. * \brief Reads the TSENS result.
  185. *
  186. * Reads the result from a TSENS conversion that was previously started.
  187. *
  188. * \param[out] result Pointer to store the result value in
  189. *
  190. * \return Status of the TSENS read request.
  191. * \retval STATUS_OK The result was retrieved successfully
  192. * \retval STATUS_BUSY A conversion result was not ready
  193. * \retval STATUS_ERR_OVERFLOW The result register has been overwritten by the
  194. * TSENS module before the result was read by the software
  195. */
  196. enum status_code tsens_read(int32_t *result)
  197. {
  198. Assert(result);
  199. if (!(tsens_get_status() & TSENS_STATUS_RESULT_READY)) {
  200. /* Result not ready */
  201. return STATUS_BUSY;
  202. }
  203. if (TSENS->STATUS.reg & TSENS_STATUS_OVF) {
  204. /* The result is not valid */
  205. return STATUS_ERR_BAD_DATA;
  206. }
  207. /* Get TSENS result */
  208. uint32_t temp = TSENS->VALUE.reg;
  209. if(temp & 0x00800000) {
  210. temp |= ~TSENS_VALUE_MASK;
  211. }
  212. #if (ERRATA_14476)
  213. *result = temp * (-1);
  214. #endif
  215. /* Reset ready flag */
  216. tsens_clear_status(TSENS_STATUS_RESULT_READY);
  217. if (tsens_get_status() & TSENS_STATUS_OVERRUN) {
  218. tsens_clear_status(TSENS_STATUS_OVERRUN);
  219. return STATUS_ERR_OVERFLOW;
  220. }
  221. return STATUS_OK;
  222. }