divas.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Divide and Square Root Accelerator (DIVAS) 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 DIVAS_H_INCLUDED
  47. #define DIVAS_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_divas_group SAM Divide and Square Root Accelerator (DIVAS) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides an
  52. * interface for the configuration and management of the device's Divide and
  53. * Square Root Accelerator functionality.
  54. *
  55. * The following peripherals are used by this module:
  56. * - DIVAS (Divide and Square Root Accelerator)
  57. *
  58. * The following devices can use this module:
  59. * - Atmel | SMART SAM C20/C21
  60. *
  61. * The outline of this documentation is as follows:
  62. * - \ref asfdoc_sam0_divas_prerequisites
  63. * - \ref asfdoc_sam0_divas_module_overview
  64. * - \ref asfdoc_sam0_divas_special_considerations
  65. * - \ref asfdoc_sam0_divas_extra_info
  66. * - \ref asfdoc_sam0_divas_examples
  67. * - \ref asfdoc_sam0_divas_api_overview
  68. *
  69. *
  70. * \section asfdoc_sam0_divas_prerequisites Prerequisites
  71. *
  72. * There are no prerequisites for this module.
  73. *
  74. *
  75. * \section asfdoc_sam0_divas_module_overview Module Overview
  76. *
  77. * This driver provides an interface for the Divide and Square Root Accelerator
  78. * on the device.
  79. *
  80. * The DIVAS is a programmable 32-bit signed or unsigned hardware divider and a
  81. * 32-bit unsigned square root hardware engine. When running signed division,
  82. * both the input and the result will be in two's complement format. The result of
  83. * signed division is that the remainder has the same sign as the dividend and
  84. * the quotient is negative if the dividend and divisor have opposite signs.
  85. * When the square root input register is programmed, the square root function
  86. * starts and the result will be stored in the Remainder register.
  87. *
  88. * There are two ways to calculate the results:
  89. * - Call the DIVAS API
  90. * - Overload "/" and "%" operation
  91. * \note Square root operation can't implement overload operation.
  92. * \subsection asfdoc_sam0_divas_module_overview_overload Overload Operation
  93. * The operation is implemented automatically by EABI (Enhanced Application Binary
  94. * Interface). EABI is a standard calling convention, which is defined by ARM.
  95. * The four functions interface can implement division and mod operation in EABI.
  96. *
  97. * The following prototypes for EABI division operation in ICCARM tool chain:
  98. * \code
  99. int __aeabi_idiv(int numerator, int denominator);
  100. unsigned __aeabi_uidiv(unsigned numerator, unsigned denominator);
  101. __value_in_regs idiv_return __aeabi_idivmod( int numerator, int denominator);
  102. __value_in_regs uidiv_return __aeabi_uidivmod( unsigned numerator,
  103. unsigned denominator);
  104. \endcode
  105. * The following prototypes for EABI division operation in GNUC tool chain:
  106. * \code
  107. int __aeabi_idiv(int numerator, int denominator);
  108. unsigned __aeabi_uidiv(unsigned numerator, unsigned denominator);
  109. uint64_t __aeabi_idivmod( int numerator, int denominator);
  110. uint64_t uidiv_return __aeabi_uidivmod( unsigned numerator,
  111. unsigned denominator);
  112. \endcode
  113. * No matter what kind of tool chain, by using DIVAS module in the four functions
  114. * body, the user can transparently access the DIVAS module when writing normal C
  115. * code. For example:
  116. * \code
  117. void division(int32_t b, int32_t c)
  118. {
  119. int32_t a;
  120. a = b / c;
  121. return a;
  122. }
  123. \endcode
  124. * Similarly, the user can use the "a = b % c;" symbol to implement the operation with
  125. * DIVAS, and needn't to care about the internal operation process.
  126. *
  127. * \subsection asfdoc_sam0_divas_module_overview_operand Operand Size
  128. * - Divide: The DIVAS can perform 32-bit signed and unsigned division.
  129. * - Square Root: The DIVAS can perform 32-bit unsigned division.
  130. * \subsection asfdoc_sam0_divas_module_overview_Signed Signed Division
  131. * When the signed flag is one, both the input and the result will be in two's
  132. * complement format. The result of signed division is that the remainder has
  133. * the same sign as the dividend and the quotient is negative if the dividend
  134. * and divisor have opposite signs.
  135. * \note When the maximum negative number is divided by the minimum negative
  136. * number, the resulting quotient overflows the signed integer range and will
  137. * return the maximum negative number with no indication of the overflow. This
  138. * occurs for 0x80000000 / 0xFFFFFFFF in 32-bit operation and 0x8000 / 0xFFFF
  139. * in 16-bit operation.
  140. *
  141. * \subsection asfdoc_sam0_divas_module_overview_zero Divide By Zero
  142. * A divide by zero will cause a fault if the DIVISOR is programmed to zero. The
  143. * result is that the quotient is zero and the reminder is equal to the dividend.
  144. *
  145. * \subsection asfdoc_sam0_divas_module_overview_square Unsigned Square Root
  146. * When the square root input register is programmed, the square root function
  147. * starts and the result will be stored in the Result and Remainder registers.
  148. * \note The square root function can't overload.
  149. *
  150. * \section asfdoc_sam0_divas_special_considerations Special Considerations
  151. *
  152. * There are no special considerations for this module.
  153. *
  154. * \section asfdoc_sam0_divas_extra_info Extra Information
  155. *
  156. *
  157. * For extra information, see \ref asfdoc_sam0_divas_extra. This includes:
  158. * - \ref asfdoc_sam0_divas_extra_acronyms
  159. * - \ref asfdoc_sam0_divas_extra_dependencies
  160. * - \ref asfdoc_sam0_divas_extra_errata
  161. * - \ref asfdoc_sam0_divas_extra_history
  162. *
  163. *
  164. * \section asfdoc_sam0_divas_examples Examples
  165. *
  166. * For a list of examples related to this driver, see
  167. * \ref asfdoc_sam0_divas_exqsg.
  168. *
  169. *
  170. * \section asfdoc_sam0_divas_api_overview API Overview
  171. * @{
  172. */
  173. #include <compiler.h>
  174. #include <system.h>
  175. #ifdef __cplusplus
  176. extern "C" {
  177. #endif
  178. /**
  179. * \brief DIVAS signed division operator result output structure.
  180. *
  181. * DIVAS signed division operator output data structure.
  182. */
  183. typedef struct {
  184. /** Signed division operator result: quotient */
  185. int32_t quotient;
  186. /** Signed division operator result: remainder */
  187. int32_t remainder;
  188. } idiv_return;
  189. /**
  190. * \brief DIVAS unsigned division operator result output structure.
  191. *
  192. * DIVAS unsigned division operator output data structure.
  193. */
  194. typedef struct {
  195. /** Unsigned division operator result: quotient */
  196. uint32_t quotient;
  197. /** Unsigned division operator result: remainder */
  198. uint32_t remainder;
  199. } uidiv_return;
  200. /**
  201. * \brief Enables DIVAS leading zero optimization.
  202. *
  203. * Enable leading zero optimization from the Divide and Square Root Accelerator
  204. * module. When leading zero optimization is enable, 16-bit division completes
  205. * in 2-8 cycles and 32-bit division completes in 2-16 cycles.
  206. *
  207. */
  208. static inline void divas_enable_dlz(void)
  209. {
  210. /* Enable DLZ. */
  211. DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_DLZ;
  212. }
  213. /**
  214. * \brief Disables DIVAS leading zero optimization.
  215. *
  216. * Disable leading zero optimization from the Divide and Square Root Accelerator
  217. * module. When leading zero optimization is disable, 16-bit division completes
  218. * in 8 cycles and 32-bit division completes in 16 cycles.
  219. *
  220. */
  221. static inline void divas_disable_dlz(void)
  222. {
  223. /* Disable DLZ. */
  224. DIVAS->CTRLA.reg |= DIVAS_CTRLA_DLZ;
  225. }
  226. /**
  227. * \name Call the DIVAS API Operation
  228. * @{
  229. * In this mode, the way that directly call the DIVAS API implement division or
  230. * mod operation.
  231. */
  232. int32_t divas_idiv(int32_t numerator, int32_t denominator);
  233. uint32_t divas_uidiv(uint32_t numerator, uint32_t denominator);
  234. int32_t divas_idivmod(int32_t numerator, int32_t denominator);
  235. uint32_t divas_uidivmod(uint32_t numerator, uint32_t denominator);
  236. uint32_t divas_sqrt(uint32_t radicand);
  237. /** @} */
  238. /**
  239. * \name DIVAS Overload '/' and '%' Operation
  240. * @{
  241. * In this mode, the user can transparently access the DIVAS module when writing
  242. * normal C code. E.g. "a = b / c;" or "a = b % c;" will be translated to a
  243. * subroutine call, which uses the DIVAS.
  244. */
  245. #if DIVAS_OVERLOAD_MODE == true
  246. # if defined ( __GNUC__ )
  247. int32_t __aeabi_idiv(int32_t numerator, int32_t denominator);
  248. uint32_t __aeabi_uidiv(uint32_t numerator, uint32_t denominator);
  249. uint64_t __aeabi_idivmod(int32_t numerator, int32_t denominator);
  250. uint64_t __aeabi_uidivmod(uint32_t numerator, uint32_t denominator);
  251. # elif defined ( __ICCARM__ )
  252. int32_t __aeabi_idiv(int32_t numerator, int32_t denominator);
  253. uint32_t __aeabi_uidiv(uint32_t numerator, uint32_t denominator);
  254. __value_in_regs idiv_return __aeabi_idivmod(int numerator, int denominator);
  255. __value_in_regs uidiv_return __aeabi_uidivmod(unsigned numerator, unsigned denominator);
  256. # endif
  257. #endif
  258. /** @} */
  259. #ifdef __cplusplus
  260. }
  261. #endif
  262. /** @} */
  263. /**
  264. * \page asfdoc_sam0_divas_extra Extra Information for DIVAS Driver
  265. *
  266. * \section asfdoc_sam0_divas_extra_acronyms Acronyms
  267. *
  268. * <table>
  269. * <tr>
  270. * <th>Acronym</th>
  271. * <th>Description</th>
  272. * </tr>
  273. * <tr>
  274. * <td>DIVAS</td>
  275. * <td>Divide and Square Root Accelerator</td>
  276. * </tr>
  277. * <tr>
  278. * <td>EABI</td>
  279. * <td>Enhanced Application Binary Interface</td>
  280. * </tr>
  281. * </table>
  282. *
  283. *
  284. * \section asfdoc_sam0_divas_extra_dependencies Dependencies
  285. * This driver has no dependencies.
  286. *
  287. *
  288. * \section asfdoc_sam0_divas_extra_errata Errata
  289. * There are no errata related to this driver.
  290. *
  291. *
  292. * \section asfdoc_sam0_divas_extra_history Module History
  293. * An overview of the module history is presented in the table below, with
  294. * details on the enhancements and fixes made to the module since its first
  295. * release. The current version of this corresponds to the newest version in
  296. * the table.
  297. *
  298. * <table>
  299. * <tr>
  300. * <th>Changelog</th>
  301. * </tr>
  302. * <tr>
  303. * <td>Initial Release</td>
  304. * </tr>
  305. * </table>
  306. */
  307. /**
  308. * \page asfdoc_sam0_divas_exqsg Examples for DIVAS Driver
  309. *
  310. * This is a list of the available Quick Start guides (QSGs) and example
  311. * applications for \ref asfdoc_sam0_divas_group. QSGs are simple examples with
  312. * step-by-step instructions to configure and use this driver in a selection of
  313. * use cases. Note that a QSG can be compiled as a standalone application or be
  314. * added to the user application.
  315. *
  316. * - \subpage asfdoc_sam0_divas_basic_use_case
  317. * - \subpage asfdoc_sam0_divas_overload_use_case
  318. *
  319. * \page asfdoc_sam0_divas_document_revision_history Document Revision History
  320. *
  321. * <table>
  322. * <tr>
  323. * <th>Doc. Rev.</td>
  324. * <th>Date</td>
  325. * <th>Comments</td>
  326. * </tr>
  327. * <tr>
  328. * <td>42644A</td>
  329. * <td>01/2016</td>
  330. * <td>Initial document release</td>
  331. * </tr>
  332. * </table>
  333. */
  334. #endif /* DIVAS_H_INCLUDED */