tsens.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Temperature Sensor (TSENS) Driver
  5. *
  6. * Copyright (C) 2015-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. #ifndef TSENS_H_INCLUDED
  47. #define TSENS_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_tsens_group SAM Temperature Sensor (TSENS) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides
  52. * an interface for the configuration and management of the device's
  53. * Configurable Custom Logic functionality.
  54. *
  55. * The following peripheral is used by this module:
  56. * - TSENS (Temperature Sensor)
  57. *
  58. * The following devices can use this module:
  59. * - Atmel | SMART SAM C21
  60. *
  61. * The outline of this documentation is as follows:
  62. * - \ref asfdoc_sam0_tsens_prerequisites
  63. * - \ref asfdoc_sam0_tsens_module_overview
  64. * - \ref asfdoc_sam0_tsens_special_considerations
  65. * - \ref asfdoc_sam0_tsens_extra_info
  66. * - \ref asfdoc_sam0_tsens_examples
  67. * - \ref asfdoc_sam0_tsens_api_overview
  68. *
  69. *
  70. * \section asfdoc_sam0_tsens_prerequisites Prerequisites
  71. *
  72. * There are no prerequisites for this module.
  73. *
  74. *
  75. * \section asfdoc_sam0_tsens_module_overview Module Overview
  76. *
  77. * The Temperature Sensor (TSENS) can be used to accurately measure the operating
  78. * temperature of the device. TSENS accurately measures the operating
  79. * temperature of the device by comparing the difference in two temperature
  80. * dependent frequencies to a known frequency. The frequency of the
  81. * temperature dependent oscillator (TOSC) is measured twice: first with the
  82. * min configuration and next with the maximum configuration. The resulting signed
  83. * value is proportional to the temperature and is corrected for offset by the
  84. * contents of the OFFSET register.
  85. *
  86. * Accurately measures a temperature:
  87. * - ±1°C over 0°C ~ 60°C
  88. * - ±3°C over -40°C ~ 85°C
  89. * - ±5°C over -40°C ~ 105°C
  90. *
  91. * The number of periods of GCLK_TSENS used for the measurement is defined by
  92. * the GAIN register. The width of the resulting pulse is measured using a
  93. * counter clocked by GCLK_TSENS in the up direction for the 1<SUP>st</SUP> phase and in
  94. * the down 2<SUP>nd</SUP> phase. Register GAIN and OFFSET is loaded from NVM, or can also
  95. * be fixed by user.
  96. * \f[
  97. * VALUE = OFFSET + (\frac{f_{TOSCMIN} - f_{TOSCMAX}}{f_{GCLK}}) \times GAIN
  98. * \f]
  99. *
  100. * \note If fix this bitfield, the relationship between GCLK frequency, GAIN
  101. * and resolution as below:
  102. * <table>
  103. * <tr>
  104. * <th>Resolution (#/°C)</th>
  105. * <th>GAIN@48MHz</th>
  106. * <th>GAIN@40MHz</th>
  107. * </tr>
  108. * <tr>
  109. * <td>&times;1 (1°C)</td>
  110. * <td>960</td>
  111. * <td>800</td>
  112. * </tr>
  113. * <tr>
  114. * <td>&times;10 (0.1°C)</td>
  115. * <td>9600</td>
  116. * <td>8000</td>
  117. * </tr>
  118. * <tr>
  119. * <td>&times;100 (0.01°C)</td>
  120. * <td>96000</td>
  121. * <td>80000</td>
  122. * </tr>
  123. * </table>
  124. *
  125. * \subsection asfdoc_sam0_tsens_module_overview_window_monitor Window Monitor
  126. * The TSENS module window monitor function can be used to automatically
  127. * compare the conversion result against a predefined pair of upper and
  128. * lower threshold values.
  129. *
  130. *
  131. * \subsection asfdoc_sam0_tsens_module_overview_events Events
  132. * Event generation and event actions are configurable in the TSENS.
  133. *
  134. * The TSENS has one actions that can be triggered upon event reception:
  135. * \li Start conversion
  136. *
  137. * The TSENS can generate the following output event:
  138. * \li Window monitor
  139. *
  140. * If the event actions are enabled in the configuration, any incoming event
  141. * will trigger the action.
  142. *
  143. * If the window monitor event is enabled, an event will be generated
  144. * when the configured window condition is detected.
  145. *
  146. * \section asfdoc_sam0_tsens_special_considerations Special Considerations
  147. *
  148. * There are no special considerations for this module.
  149. *
  150. *
  151. * \section asfdoc_sam0_tsens_extra_info Extra Information
  152. *
  153. * For extra information, see \ref asfdoc_sam0_tsens_extra. This includes:
  154. * - \ref asfdoc_sam0_tsens_extra_acronyms
  155. * - \ref asfdoc_sam0_tsens_extra_dependencies
  156. * - \ref asfdoc_sam0_tsens_extra_errata
  157. * - \ref asfdoc_sam0_tsens_extra_history
  158. *
  159. *
  160. * \section asfdoc_sam0_tsens_examples Examples
  161. *
  162. * For a list of examples related to this driver, see
  163. * \ref asfdoc_sam0_tsens_exqsg.
  164. *
  165. *
  166. * \section asfdoc_sam0_tsens_api_overview API Overview
  167. * @{
  168. */
  169. #ifdef __cplusplus
  170. extern "C" {
  171. #endif
  172. #include <compiler.h>
  173. #include <system.h>
  174. /**
  175. * The magnitude of the temperature measurement value decreases with increasing
  176. * temperature, i.e. it has a negative temperature coefficient.
  177. * \ref asfdoc_sam0_tsens_extra_errata "Errata reference: 14476".
  178. */
  179. #define ERRATA_14476 true
  180. /**
  181. * \name Module Status Flags
  182. *
  183. * TSENS status flags, returned by \ref tsens_get_status() and cleared by
  184. * \ref tsens_clear_status().
  185. *
  186. * @{
  187. */
  188. /** TSENS result ready. */
  189. #define TSENS_STATUS_RESULT_READY (1UL << 0)
  190. /** TSENS result overwritten before read. */
  191. #define TSENS_STATUS_OVERRUN (1UL << 1)
  192. /** Window monitor match. */
  193. #define TSENS_STATUS_WINDOW (1UL << 2)
  194. /** TSENS result overflows. */
  195. #define TSENS_STATUS_OVERFLOW (1UL << 3)
  196. /** @} */
  197. /**
  198. * \brief TSENS window monitor mode enum.
  199. *
  200. * Enum for the possible window monitor modes for the TSENS.
  201. *
  202. */
  203. enum tsens_window_mode {
  204. /** No window mode */
  205. TSENS_WINDOW_MODE_DISABLE = TSENS_CTRLC_WINMODE_DISABLE,
  206. /** RESULT > WINLT */
  207. TSENS_WINDOW_MODE_ABOVE = TSENS_CTRLC_WINMODE_ABOVE,
  208. /** RESULT < WINUT */
  209. TSENS_WINDOW_MODE_BELOW = TSENS_CTRLC_WINMODE_BELOW,
  210. /** WINLT < RESULT < WINUT */
  211. TSENS_WINDOW_MODE_INSIDE = TSENS_CTRLC_WINMODE_INSIDE,
  212. /** !(WINLT < RESULT < WINUT) */
  213. TSENS_WINDOW_MODE_OUTSIDE = TSENS_CTRLC_WINMODE_OUTSIDE,
  214. /** VALUE > WINUT with hysteresis to WINLT */
  215. TSENS_WINDOW_MODE_HYST_ABOVE = TSENS_CTRLC_WINMODE_HYST_ABOVE,
  216. /** VALUE < WINLT with hysteresis to WINUT */
  217. TSENS_WINDOW_MODE_HYST_BELOW = TSENS_CTRLC_WINMODE_HYST_BELOW,
  218. };
  219. /**
  220. * \brief TSENS event action enum.
  221. *
  222. * Enum for the possible actions to take on an incoming event.
  223. *
  224. */
  225. enum tsens_event_action {
  226. /** Event action disabled */
  227. TSENS_EVENT_ACTION_DISABLED = 0,
  228. /** Start conversion */
  229. TSENS_EVENT_ACTION_START_CONV = TSENS_EVCTRL_STARTEI,
  230. };
  231. /**
  232. * \brief Window monitor configuration structure.
  233. *
  234. * Window monitor configuration structure.
  235. */
  236. struct tsens_window_config {
  237. /** Selected window mode */
  238. enum tsens_window_mode window_mode;
  239. /** Lower window value */
  240. int32_t window_lower_value;
  241. /** Upper window value */
  242. int32_t window_upper_value;
  243. };
  244. /**
  245. * \brief TSENS event enable/disable structure.
  246. *
  247. * Event flags for the TSENS module. This is used to enable and
  248. * disable events via \ref tsens_enable_events() and \ref tsens_disable_events().
  249. */
  250. struct tsens_events {
  251. /** Enable event generation on window monitor */
  252. bool generate_event_on_window_monitor;
  253. };
  254. /**
  255. * \brief Calibration configuration structure.
  256. *
  257. * Calibration configuration structure.
  258. */
  259. struct tsens_calibration {
  260. /** Time amplifier gain */
  261. uint32_t gain;
  262. /** Offset correction */
  263. int32_t offset;
  264. };
  265. /**
  266. * \brief TSENS configuration structure.
  267. *
  268. * Configuration structure for an TSENS instance. This structure should be
  269. * initialized by the \ref tsens_get_config_defaults() function before being
  270. * modified by the user application.
  271. */
  272. struct tsens_config {
  273. /** GCLK generator used to clock the peripheral */
  274. enum gclk_generator clock_source;
  275. /** Enables free running mode if true */
  276. bool free_running;
  277. /** Enables TSENS in standby sleep mode if true */
  278. bool run_in_standby;
  279. /** Window monitor configuration structure */
  280. struct tsens_window_config window;
  281. /** Event action to take on incoming event */
  282. enum tsens_event_action event_action;
  283. /** Calibration value */
  284. struct tsens_calibration calibration;
  285. };
  286. /**
  287. * \name Driver Initialization and Configuration
  288. * @{
  289. */
  290. enum status_code tsens_init(struct tsens_config *config);
  291. void tsens_get_config_defaults(struct tsens_config *const config);
  292. /** @} */
  293. /**
  294. * \name Status Management
  295. * @{
  296. */
  297. /**
  298. * \brief Retrieves the current module status.
  299. *
  300. * Retrieves the status of the module, giving overall state information.
  301. *
  302. * \return Bit mask of TSENS status flags.
  303. *
  304. * \retval TSENS_STATUS_RESULT_READY TSENS result is ready to be read
  305. * \retval TSENS_STATUS_OVERRUN TSENS result overwritten before read
  306. * \retval TSENS_STATUS_WINDOW TSENS has detected a value inside the set
  307. * window range
  308. * \retval TSENS_STATUS_OVERFLOW TSENS result overflows
  309. */
  310. static inline uint32_t tsens_get_status(void)
  311. {
  312. uint32_t int_flags = TSENS->INTFLAG.reg;
  313. uint32_t status_flags = 0;
  314. /* Check for TSENS Result Ready */
  315. if (int_flags & TSENS_INTFLAG_RESRDY) {
  316. status_flags |= TSENS_STATUS_RESULT_READY;
  317. }
  318. /* Check for TSENS Overrun */
  319. if (int_flags & TSENS_INTFLAG_OVERRUN) {
  320. status_flags |= TSENS_STATUS_OVERRUN;
  321. }
  322. /* Check for TSENS Window Match */
  323. if (int_flags & TSENS_INTFLAG_WINMON) {
  324. status_flags |= TSENS_STATUS_WINDOW;
  325. }
  326. /* Check for TSENS overflow */
  327. if (int_flags & TSENS_INTFLAG_OVF) {
  328. status_flags |= TSENS_STATUS_OVERFLOW;
  329. }
  330. return status_flags;
  331. }
  332. /**
  333. * \brief Clears a module status flag.
  334. *
  335. * Clears the given status flag of the module.
  336. *
  337. * \param[in] module_inst Pointer to the TSENS software instance struct
  338. * \param[in] status_flags Bitmask of \c TSENS_STATUS_* flags to clear
  339. */
  340. static inline void tsens_clear_status(const uint32_t status_flags)
  341. {
  342. uint32_t int_flags = 0;
  343. /* Check for TSENS Result Ready */
  344. if (status_flags & TSENS_INTFLAG_RESRDY) {
  345. int_flags |= TSENS_STATUS_RESULT_READY;
  346. }
  347. /* Check for TSENS Overrun */
  348. if (status_flags & TSENS_INTFLAG_OVERRUN) {
  349. int_flags |= TSENS_STATUS_OVERRUN;
  350. }
  351. /* Check for TSENS Window Match */
  352. if (status_flags & TSENS_INTFLAG_WINMON) {
  353. int_flags |= TSENS_STATUS_WINDOW;
  354. }
  355. /* Check for TSENS overflow */
  356. if (status_flags & TSENS_INTFLAG_OVF) {
  357. int_flags |= TSENS_STATUS_OVERFLOW;
  358. }
  359. /* Clear interrupt flag */
  360. TSENS->INTFLAG.reg = int_flags;
  361. }
  362. /** @} */
  363. /**
  364. * \name Enable, Disable, and Reset TSENS Module, Start Conversion and Read Result
  365. * @{
  366. */
  367. /**
  368. * \brief Determines if the hardware module is currently synchronizing to the bus.
  369. *
  370. * Checks to see if the underlying hardware peripheral module(s) are currently
  371. * synchronizing across multiple clock domains to the hardware bus. This
  372. * function can be used to delay further operations on a module until such time
  373. * that it is ready, to prevent blocking delays for synchronization in the
  374. * user application.
  375. *
  376. * \return Synchronization status of the underlying hardware module(s).
  377. *
  378. * \retval true If the module synchronization is ongoing
  379. * \retval false If the module has completed synchronization
  380. */
  381. static inline bool tsens_is_syncing(void)
  382. {
  383. if (TSENS->SYNCBUSY.reg) {
  384. return true;
  385. }
  386. return false;
  387. }
  388. /**
  389. * \brief Enables the TSENS module.
  390. *
  391. * Enables an TSENS module that has previously been configured.
  392. */
  393. static inline void tsens_enable(void)
  394. {
  395. TSENS->CTRLA.reg |= TSENS_CTRLA_ENABLE;
  396. while (tsens_is_syncing()) {
  397. /* Wait for synchronization */
  398. }
  399. }
  400. /**
  401. * \brief Disables the TSENS module.
  402. *
  403. * Disables an TSENS module that was previously enabled.
  404. *
  405. */
  406. static inline void tsens_disable(void)
  407. {
  408. TSENS->INTENCLR.reg = TSENS_INTENCLR_MASK;
  409. TSENS->INTFLAG.reg = TSENS_INTFLAG_MASK;
  410. TSENS->CTRLA.reg &= ~TSENS_CTRLA_ENABLE;
  411. while (tsens_is_syncing()) {
  412. /* Wait for synchronization */
  413. }
  414. }
  415. /**
  416. * \brief Resets the TSENS module.
  417. *
  418. * Resets an TSENS module, clearing all module state and registers to their
  419. * default values.
  420. *
  421. */
  422. static inline void tsens_reset(void)
  423. {
  424. /* Disable to make sure the pipeline is flushed before reset */
  425. tsens_disable();
  426. /* Software reset the module */
  427. TSENS->CTRLA.reg |= TSENS_CTRLA_SWRST;
  428. while (tsens_is_syncing()) {
  429. /* Wait for synchronization */
  430. }
  431. }
  432. /**
  433. * \brief Enables an TSENS event output.
  434. *
  435. * Enables one or more input or output events to or from the TSENS module. See
  436. * \ref tsens_events "tsens_events" for a list of events this module supports.
  437. *
  438. * \note Events cannot be altered while the module is enabled.
  439. *
  440. * \param[in] events Struct containing flags of events to enable
  441. */
  442. static inline void tsens_enable_events(struct tsens_events *const events)
  443. {
  444. /* Sanity check arguments */
  445. Assert(events);
  446. uint32_t event_mask = 0;
  447. /* Configure Window Monitor event */
  448. if (events->generate_event_on_window_monitor) {
  449. event_mask |= TSENS_EVCTRL_WINEO;
  450. }
  451. TSENS->EVCTRL.reg |= event_mask;
  452. }
  453. /**
  454. * \brief Disables an TSENS event output.
  455. *
  456. * Disables one or more output events to or from the TSENS module. See
  457. * \ref tsens_events "tsens_events" for a list of events this module supports.
  458. *
  459. * \note Events cannot be altered while the module is enabled.
  460. *
  461. * \param[in] events Struct containing flags of events to disable
  462. */
  463. static inline void tsens_disable_events(struct tsens_events *const events)
  464. {
  465. /* Sanity check arguments */
  466. Assert(events);
  467. uint32_t event_mask = 0;
  468. /* Configure Window Monitor event */
  469. if (events->generate_event_on_window_monitor) {
  470. event_mask |= TSENS_EVCTRL_WINEO;
  471. }
  472. TSENS->EVCTRL.reg &= ~event_mask;
  473. }
  474. /**
  475. * \brief Start a TSENS conversion.
  476. *
  477. * Start a new TSENS conversion.
  478. *
  479. */
  480. static inline void tsens_start_conversion(void)
  481. {
  482. TSENS->CTRLB.reg |= TSENS_CTRLB_START;
  483. while (tsens_is_syncing()) {
  484. /* Wait for synchronization */
  485. }
  486. }
  487. enum status_code tsens_read(int32_t *result);
  488. /** @} */
  489. /** @} */
  490. /**
  491. * \page asfdoc_sam0_tsens_extra Extra Information for TSENS Driver
  492. *
  493. * \section asfdoc_sam0_tsens_extra_acronyms Acronym
  494. * Below is a table listing the acronym used in this module, along with their
  495. * intended meaning.
  496. *
  497. * <table>
  498. * <tr>
  499. * <th>Acronym</th>
  500. * <th>Description</th>
  501. * </tr>
  502. * <tr>
  503. * <td>TSENS</td>
  504. * <td>Temperature Sensor</td>
  505. * </tr>
  506. * </table>
  507. *
  508. *
  509. * \section asfdoc_sam0_tsens_extra_dependencies Dependencies
  510. * This driver has no dependencies.
  511. *
  512. *
  513. * \section asfdoc_sam0_tsens_extra_errata Errata
  514. * Errata reference: 14476.
  515. *
  516. * The magnitude of the temperature measurement value decreases with increasing
  517. * temperature, i.e. it has a negative temperature coefficient.
  518. *
  519. *
  520. * \section asfdoc_sam0_tsens_extra_history Module History
  521. * An overview of the module history is presented in the table below, with
  522. * details on the enhancements and fixes made to the module since its first
  523. * release. The current version of this corresponds to the newest version in
  524. * the table.
  525. *
  526. * <table>
  527. * <tr>
  528. * <th>Changelog</th>
  529. * </tr>
  530. * <tr>
  531. * <td>Initial Release</td>
  532. * </tr>
  533. * </table>
  534. */
  535. /**
  536. * \page asfdoc_sam0_tsens_exqsg Examples for TSENS Driver
  537. *
  538. * This is a list of the available Quick Start guides (QSGs) and example
  539. * applications for \ref asfdoc_sam0_tsens_group. QSGs are simple examples with
  540. * step-by-step instructions to configure and use this driver in a selection of
  541. * use cases. Note that a QSG can be compiled as a standalone application or be
  542. * added to the user application.
  543. *
  544. * - \subpage asfdoc_sam0_tsens_basic_use_case
  545. * - \subpage asfdoc_sam0_tsens_basic_use_case_callback
  546. *
  547. * \page asfdoc_sam0_tsens_document_revision_history Document Revision History
  548. *
  549. * <table>
  550. * <tr>
  551. * <th>Doc. Rev.</th>
  552. * <th>Date</th>
  553. * <th>Comments</th>
  554. * </tr>
  555. * <tr>
  556. * <td>42542A</td>
  557. * <td>12/2015</td>
  558. * <td>Initial document release</td>
  559. * </tr>
  560. * </table>
  561. */
  562. #ifdef __cplusplus
  563. }
  564. #endif
  565. #endif /* TSENS_H_INCLUDED */