divas.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Divide and Square Root Accelerator (DIVAS) 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 "divas.h"
  47. /**
  48. * \internal
  49. * \brief Initializes and enables the Divide and Square Root Accelerator (DIVAS) Driver.
  50. *
  51. * Enable the clocks used by Divide and Square Root Accelerator (DIVAS) Driver.
  52. * Enable leading zero optimization.
  53. *
  54. * \note When SYSTEM module is used, this function will be invoked by
  55. * \ref system_init() automatically if the module is included.
  56. */
  57. void _system_divas_init(void);
  58. void _system_divas_init(void)
  59. {
  60. /* Turn on the digital interface clock. */
  61. system_ahb_clock_set_mask(MCLK_AHBMASK_DIVAS);
  62. DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_DLZ;
  63. }
  64. /**
  65. * \brief Signed division operation
  66. *
  67. * Run the signed division operation and return the quotient.
  68. *
  69. * \param[in] numerator The dividend of the signed division operation
  70. * \param[in] denominator The divisor of the signed division operation
  71. *
  72. * \return The quotient of the DIVAS signed division operation.
  73. */
  74. int32_t divas_idiv(int32_t numerator, int32_t denominator)
  75. {
  76. /* Disable interrupt. */
  77. cpu_irq_enter_critical();
  78. /* Signed division. */
  79. DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
  80. /* Write the dividend to DIVIDEND register. */
  81. DIVAS->DIVIDEND.reg = numerator;
  82. /* Write the divisor to DIVISOR register. */
  83. DIVAS->DIVISOR.reg = denominator;
  84. while(DIVAS->STATUS.bit.BUSY){
  85. /* Wait the division is complete. */
  86. }
  87. int32_t quotient = DIVAS->RESULT.reg;
  88. /* Enable interrupt. */
  89. cpu_irq_leave_critical();
  90. return quotient;
  91. }
  92. /**
  93. * \brief Unsigned division operation
  94. *
  95. * Run the unsigned division operation and return the results.
  96. *
  97. * \param[in] numerator The dividend of the unsigned division operation
  98. * \param[in] denominator The divisor of the unsigned division operation
  99. *
  100. * \return The quotient of the DIVAS unsigned division operation.
  101. */
  102. uint32_t divas_uidiv(uint32_t numerator, uint32_t denominator)
  103. {
  104. /* Disable interrupt. */
  105. cpu_irq_enter_critical();
  106. /* Unsigned division. */
  107. DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
  108. /* Write the dividend to DIVIDEND register. */
  109. DIVAS->DIVIDEND.reg = numerator;
  110. /* Write the divisor to DIVISOR register. */
  111. DIVAS->DIVISOR.reg = denominator;
  112. while(DIVAS->STATUS.bit.BUSY){
  113. /* Wait the division is complete. */
  114. }
  115. uint32_t quotient = DIVAS->RESULT.reg;
  116. /* Enable interrupt. */
  117. cpu_irq_leave_critical();
  118. return quotient;
  119. }
  120. /**
  121. * \brief Signed division remainder operation
  122. *
  123. * Run the signed division operation and return the remainder.
  124. *
  125. * \param[in] numerator The dividend of the signed division operation
  126. * \param[in] denominator The divisor of the signed division operation
  127. *
  128. * \return The remainder of the DIVAS signed division operation.
  129. */
  130. int32_t divas_idivmod(int32_t numerator, int32_t denominator)
  131. {
  132. /* Disable interrupt. */
  133. cpu_irq_enter_critical();
  134. /* Signed division. */
  135. DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
  136. /* Write the dividend to DIVIDEND register. */
  137. DIVAS->DIVIDEND.reg = numerator;
  138. /* Write the divisor to DIVISOR register. */
  139. DIVAS->DIVISOR.reg = denominator;
  140. while(DIVAS->STATUS.bit.BUSY){
  141. /* Wait the division is complete. */
  142. }
  143. int32_t remainder = DIVAS->REM.reg;
  144. /* Enable interrupt. */
  145. cpu_irq_leave_critical();
  146. return remainder;
  147. }
  148. /**
  149. * \brief Unsigned division remainder operation
  150. *
  151. * Run the unsigned division operation and return the remainder.
  152. *
  153. * \param[in] numerator The dividend of the unsigned division operation
  154. * \param[in] denominator The divisor of the unsigned division operation
  155. *
  156. * \return The remainder of the DIVAS unsigned division operation.
  157. */
  158. uint32_t divas_uidivmod(uint32_t numerator, uint32_t denominator)
  159. {
  160. /* Disable interrupt. */
  161. cpu_irq_enter_critical();
  162. /* Unsigned division. */
  163. DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
  164. /* Write the dividend to DIVIDEND register. */
  165. DIVAS->DIVIDEND.reg = numerator;
  166. /* Write the divisor to DIVISOR register. */
  167. DIVAS->DIVISOR.reg = denominator;
  168. while(DIVAS->STATUS.bit.BUSY){
  169. /* Wait the division is complete. */
  170. }
  171. uint32_t remainder = DIVAS->REM.reg;
  172. /* Enable interrupt. */
  173. cpu_irq_leave_critical();
  174. return remainder;
  175. }
  176. /**
  177. * \brief Square root operation
  178. *
  179. * Run the square root operation and return the results.
  180. *
  181. * \param[in] radicand The radicand of the square root operation
  182. *
  183. * \return The result of the DIVAS square root operation.
  184. */
  185. uint32_t divas_sqrt(uint32_t radicand)
  186. {
  187. /* Disable interrupt. */
  188. cpu_irq_enter_critical();
  189. /* Write the radicand to DIVIDEND register. */
  190. DIVAS->SQRNUM.reg = radicand;
  191. while(DIVAS->STATUS.bit.BUSY){
  192. /* Wait the square root is complete. */
  193. }
  194. uint32_t result_sqrt = DIVAS->RESULT.reg;
  195. /* Enable interrupt. */
  196. cpu_irq_leave_critical();
  197. return result_sqrt;
  198. }
  199. /**
  200. * \name DIVAS Operation Overloading
  201. * @{
  202. */
  203. #if DIVAS_OVERLOAD_MODE == true
  204. # if defined ( __GNUC__ )
  205. /**
  206. * \brief Signed division operation overload
  207. *
  208. * Run the signed division operation and return the results.
  209. *
  210. * \param[in] numerator The dividend of the signed division operation
  211. * \param[in] denominator The divisor of the signed division operation
  212. *
  213. * \return The quotient of the DIVAS signed division operation.
  214. */
  215. int32_t __aeabi_idiv(int32_t numerator, int32_t denominator)
  216. {
  217. return divas_idiv(numerator, denominator);
  218. }
  219. /**
  220. * \brief Unsigned division operation overload
  221. *
  222. * Run the unsigned division operation and return the results.
  223. *
  224. * \param[in] numerator The dividend of the unsigned division operation
  225. * \param[in] denominator The divisor of the unsigned division operation
  226. *
  227. * \return The quotient of the DIVAS unsigned division operation.
  228. */
  229. uint32_t __aeabi_uidiv(uint32_t numerator, uint32_t denominator)
  230. {
  231. return divas_uidiv(numerator, denominator);
  232. }
  233. /**
  234. * \brief Signed division remainder operation overload
  235. *
  236. * Run the signed division operation and return the remainder.
  237. *
  238. * \param[in] numerator The dividend of the signed division operation
  239. * \param[in] denominator The divisor of the signed division operation
  240. *
  241. * \return The remainder of the DIVAS signed division operation.
  242. */
  243. uint64_t __aeabi_idivmod(int32_t numerator, int32_t denominator)
  244. {
  245. uint64_t uret;
  246. int32_t quotient, remainder;
  247. /* Disable interrupt. */
  248. cpu_irq_enter_critical();
  249. /* Signed division. */
  250. DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
  251. /* Write the dividend to DIVIDEND register. */
  252. DIVAS->DIVIDEND.reg = numerator;
  253. /* Write the divisor to DIVISOR register. */
  254. DIVAS->DIVISOR.reg = denominator;
  255. while(DIVAS->STATUS.bit.BUSY){
  256. /* Wait the division is complete. */
  257. }
  258. /* Read out the result. */
  259. quotient = DIVAS->RESULT.reg;
  260. remainder = DIVAS->REM.reg;
  261. /* quotient in r0, remainder in r1 */
  262. uret = ((uint64_t)quotient & 0x00000000FFFFFFFF ) |
  263. (((uint64_t)remainder ) << 32);
  264. /* Enable interrupt. */
  265. cpu_irq_leave_critical();
  266. return uret;
  267. }
  268. /**
  269. * \brief Unsigned division remainder operation overload
  270. *
  271. * Run the unsigned division operation and return the remainder.
  272. *
  273. * \param[in] numerator The dividend of the unsigned division operation
  274. * \param[in] denominator The divisor of the unsigned division operation
  275. *
  276. * \return The remainder of the DIVAS unsigned division operation.
  277. */
  278. uint64_t __aeabi_uidivmod(uint32_t numerator, uint32_t denominator)
  279. {
  280. uint64_t uret;
  281. uint32_t quotient, remainder;
  282. /* Disable interrupt. */
  283. cpu_irq_enter_critical();
  284. /* Unsigned division. */
  285. DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
  286. /* Write the dividend to DIVIDEND register. */
  287. DIVAS->DIVIDEND.reg = numerator;
  288. /* Write the divisor to DIVISOR register. */
  289. DIVAS->DIVISOR.reg = denominator;
  290. while(DIVAS->STATUS.bit.BUSY){
  291. /* Wait the division is complete. */
  292. }
  293. /* Read out the result. */
  294. quotient = DIVAS->RESULT.reg;
  295. remainder = DIVAS->REM.reg;
  296. /* quotient in r0, remainder in r1 */
  297. uret = quotient | (((uint64_t)remainder) << 32);
  298. /* Enable interrupt. */
  299. cpu_irq_leave_critical();
  300. return uret;
  301. }
  302. # elif defined ( __ICCARM__ )
  303. /**
  304. * \brief Signed division operation overload
  305. *
  306. * Run the signed division operation and return the results.
  307. *
  308. * \param[in] numerator The dividend of the signed division operation
  309. * \param[in] denominator The divisor of the signed division operation
  310. *
  311. * \return The quotient of the DIVAS signed division operation.
  312. */
  313. int32_t __aeabi_idiv(int32_t numerator, int32_t denominator)
  314. {
  315. return divas_idiv(numerator, denominator);
  316. }
  317. /**
  318. * \brief Unsigned division operation overload
  319. *
  320. * Run the unsigned division operation and return the results.
  321. *
  322. * \param[in] numerator The dividend of the unsigned division operation
  323. * \param[in] denominator The divisor of the unsigned division operation
  324. *
  325. * \return The quotient of the DIVAS unsigned division operation.
  326. */
  327. uint32_t __aeabi_uidiv(uint32_t numerator, uint32_t denominator)
  328. {
  329. return divas_uidiv(numerator, denominator);
  330. }
  331. /**
  332. * \brief Signed division remainder operation overload
  333. *
  334. * Run the signed division operation and return the remainder.
  335. * \param[in] numerator The dividend of the signed division operation
  336. * \param[in] denominator The divisor of the signed division operation
  337. *
  338. * \return The remainder of the DIVAS signed division operation.
  339. */
  340. __value_in_regs idiv_return __aeabi_idivmod(int numerator, int denominator)
  341. {
  342. idiv_return result;
  343. /* Disable interrupt. */
  344. cpu_irq_enter_critical();
  345. /* Signed division. */
  346. DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
  347. /* Write the dividend to DIVIDEND register. */
  348. DIVAS->DIVIDEND.reg = numerator;
  349. /* Write the divisor to DIVISOR register. */
  350. DIVAS->DIVISOR.reg = denominator;
  351. while(DIVAS->STATUS.bit.BUSY){
  352. /* Wait the division is complete. */
  353. }
  354. /* Read out the result. */
  355. result.quotient = DIVAS->RESULT.reg;
  356. result.remainder = DIVAS->REM.reg;
  357. /* Enable interrupt. */
  358. cpu_irq_leave_critical();
  359. return result;
  360. }
  361. /**
  362. * \brief Unsigned division remainder operation overload
  363. *
  364. * Run the unsigned division operation and return the remainder.
  365. * \param[in] numerator The dividend of the unsigned division operation
  366. * \param[in] denominator The divisor of the unsigned division operation
  367. *
  368. * \return The remainder of the DIVAS unsigned division operation.
  369. */
  370. __value_in_regs uidiv_return __aeabi_uidivmod(unsigned numerator, unsigned denominator)
  371. {
  372. uidiv_return result;
  373. /* Disable interrupt. */
  374. cpu_irq_enter_critical();
  375. /* Unsigned division. */
  376. DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
  377. /* Write the dividend to DIVIDEND register. */
  378. DIVAS->DIVIDEND.reg = numerator;
  379. /* Write the divisor to DIVISOR register. */
  380. DIVAS->DIVISOR.reg = denominator;
  381. while(DIVAS->STATUS.bit.BUSY){
  382. /* Wait the division is complete. */
  383. }
  384. /* Read out the result. */
  385. result.quotient = DIVAS->RESULT.reg;
  386. result.remainder = DIVAS->REM.reg;
  387. /* Enable interrupt. */
  388. cpu_irq_leave_critical();
  389. return result;
  390. }
  391. # endif
  392. #endif
  393. /** @} */