123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- /**
- * \file
- *
- * \brief SAM Divide and Square Root Accelerator (DIVAS) Driver
- *
- * Copyright (c) 2015 Atmel Corporation. All rights reserved.
- *
- * \asf_license_start
- *
- * \page License
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. The name of Atmel may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * 4. This software may only be redistributed and used in connection with an
- * Atmel microcontroller product.
- *
- * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
- * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * \asf_license_stop
- *
- */
- /*
- * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
- */
- #include "divas.h"
- /**
- * \internal
- * \brief Initializes and enables the Divide and Square Root Accelerator (DIVAS) Driver.
- *
- * Enable the clocks used by Divide and Square Root Accelerator (DIVAS) Driver.
- * Enable leading zero optimization.
- *
- * \note When SYSTEM module is used, this function will be invoked by
- * \ref system_init() automatically if the module is included.
- */
- void _system_divas_init(void);
- void _system_divas_init(void)
- {
- /* Turn on the digital interface clock. */
- system_ahb_clock_set_mask(MCLK_AHBMASK_DIVAS);
- DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_DLZ;
- }
- /**
- * \brief Signed division operation
- *
- * Run the signed division operation and return the quotient.
- *
- * \param[in] numerator The dividend of the signed division operation
- * \param[in] denominator The divisor of the signed division operation
- *
- * \return The quotient of the DIVAS signed division operation.
- */
- int32_t divas_idiv(int32_t numerator, int32_t denominator)
- {
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Signed division. */
- DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- int32_t quotient = DIVAS->RESULT.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return quotient;
- }
- /**
- * \brief Unsigned division operation
- *
- * Run the unsigned division operation and return the results.
- *
- * \param[in] numerator The dividend of the unsigned division operation
- * \param[in] denominator The divisor of the unsigned division operation
- *
- * \return The quotient of the DIVAS unsigned division operation.
- */
- uint32_t divas_uidiv(uint32_t numerator, uint32_t denominator)
- {
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Unsigned division. */
- DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- uint32_t quotient = DIVAS->RESULT.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return quotient;
- }
- /**
- * \brief Signed division remainder operation
- *
- * Run the signed division operation and return the remainder.
- *
- * \param[in] numerator The dividend of the signed division operation
- * \param[in] denominator The divisor of the signed division operation
- *
- * \return The remainder of the DIVAS signed division operation.
- */
- int32_t divas_idivmod(int32_t numerator, int32_t denominator)
- {
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Signed division. */
- DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- int32_t remainder = DIVAS->REM.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return remainder;
- }
- /**
- * \brief Unsigned division remainder operation
- *
- * Run the unsigned division operation and return the remainder.
- *
- * \param[in] numerator The dividend of the unsigned division operation
- * \param[in] denominator The divisor of the unsigned division operation
- *
- * \return The remainder of the DIVAS unsigned division operation.
- */
- uint32_t divas_uidivmod(uint32_t numerator, uint32_t denominator)
- {
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Unsigned division. */
- DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- uint32_t remainder = DIVAS->REM.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return remainder;
- }
- /**
- * \brief Square root operation
- *
- * Run the square root operation and return the results.
- *
- * \param[in] radicand The radicand of the square root operation
- *
- * \return The result of the DIVAS square root operation.
- */
- uint32_t divas_sqrt(uint32_t radicand)
- {
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Write the radicand to DIVIDEND register. */
- DIVAS->SQRNUM.reg = radicand;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the square root is complete. */
- }
- uint32_t result_sqrt = DIVAS->RESULT.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return result_sqrt;
- }
- /**
- * \name DIVAS Operation Overloading
- * @{
- */
- #if DIVAS_OVERLOAD_MODE == true
- # if defined ( __GNUC__ )
- /**
- * \brief Signed division operation overload
- *
- * Run the signed division operation and return the results.
- *
- * \param[in] numerator The dividend of the signed division operation
- * \param[in] denominator The divisor of the signed division operation
- *
- * \return The quotient of the DIVAS signed division operation.
- */
- int32_t __aeabi_idiv(int32_t numerator, int32_t denominator)
- {
- return divas_idiv(numerator, denominator);
- }
- /**
- * \brief Unsigned division operation overload
- *
- * Run the unsigned division operation and return the results.
- *
- * \param[in] numerator The dividend of the unsigned division operation
- * \param[in] denominator The divisor of the unsigned division operation
- *
- * \return The quotient of the DIVAS unsigned division operation.
- */
- uint32_t __aeabi_uidiv(uint32_t numerator, uint32_t denominator)
- {
- return divas_uidiv(numerator, denominator);
- }
- /**
- * \brief Signed division remainder operation overload
- *
- * Run the signed division operation and return the remainder.
- *
- * \param[in] numerator The dividend of the signed division operation
- * \param[in] denominator The divisor of the signed division operation
- *
- * \return The remainder of the DIVAS signed division operation.
- */
- uint64_t __aeabi_idivmod(int32_t numerator, int32_t denominator)
- {
- uint64_t uret;
- int32_t quotient, remainder;
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Signed division. */
- DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- /* Read out the result. */
- quotient = DIVAS->RESULT.reg;
- remainder = DIVAS->REM.reg;
- /* quotient in r0, remainder in r1 */
- uret = ((uint64_t)quotient & 0x00000000FFFFFFFF ) |
- (((uint64_t)remainder ) << 32);
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return uret;
- }
- /**
- * \brief Unsigned division remainder operation overload
- *
- * Run the unsigned division operation and return the remainder.
- *
- * \param[in] numerator The dividend of the unsigned division operation
- * \param[in] denominator The divisor of the unsigned division operation
- *
- * \return The remainder of the DIVAS unsigned division operation.
- */
- uint64_t __aeabi_uidivmod(uint32_t numerator, uint32_t denominator)
- {
- uint64_t uret;
- uint32_t quotient, remainder;
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Unsigned division. */
- DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- /* Read out the result. */
- quotient = DIVAS->RESULT.reg;
- remainder = DIVAS->REM.reg;
- /* quotient in r0, remainder in r1 */
- uret = quotient | (((uint64_t)remainder) << 32);
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return uret;
- }
- # elif defined ( __ICCARM__ )
- /**
- * \brief Signed division operation overload
- *
- * Run the signed division operation and return the results.
- *
- * \param[in] numerator The dividend of the signed division operation
- * \param[in] denominator The divisor of the signed division operation
- *
- * \return The quotient of the DIVAS signed division operation.
- */
- int32_t __aeabi_idiv(int32_t numerator, int32_t denominator)
- {
- return divas_idiv(numerator, denominator);
- }
- /**
- * \brief Unsigned division operation overload
- *
- * Run the unsigned division operation and return the results.
- *
- * \param[in] numerator The dividend of the unsigned division operation
- * \param[in] denominator The divisor of the unsigned division operation
- *
- * \return The quotient of the DIVAS unsigned division operation.
- */
- uint32_t __aeabi_uidiv(uint32_t numerator, uint32_t denominator)
- {
- return divas_uidiv(numerator, denominator);
- }
- /**
- * \brief Signed division remainder operation overload
- *
- * Run the signed division operation and return the remainder.
- * \param[in] numerator The dividend of the signed division operation
- * \param[in] denominator The divisor of the signed division operation
- *
- * \return The remainder of the DIVAS signed division operation.
- */
- __value_in_regs idiv_return __aeabi_idivmod(int numerator, int denominator)
- {
- idiv_return result;
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Signed division. */
- DIVAS->CTRLA.reg |= DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- /* Read out the result. */
- result.quotient = DIVAS->RESULT.reg;
- result.remainder = DIVAS->REM.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return result;
- }
- /**
- * \brief Unsigned division remainder operation overload
- *
- * Run the unsigned division operation and return the remainder.
- * \param[in] numerator The dividend of the unsigned division operation
- * \param[in] denominator The divisor of the unsigned division operation
- *
- * \return The remainder of the DIVAS unsigned division operation.
- */
- __value_in_regs uidiv_return __aeabi_uidivmod(unsigned numerator, unsigned denominator)
- {
- uidiv_return result;
- /* Disable interrupt. */
- cpu_irq_enter_critical();
- /* Unsigned division. */
- DIVAS->CTRLA.reg &= ~DIVAS_CTRLA_SIGNED;
- /* Write the dividend to DIVIDEND register. */
- DIVAS->DIVIDEND.reg = numerator;
- /* Write the divisor to DIVISOR register. */
- DIVAS->DIVISOR.reg = denominator;
- while(DIVAS->STATUS.bit.BUSY){
- /* Wait the division is complete. */
- }
- /* Read out the result. */
- result.quotient = DIVAS->RESULT.reg;
- result.remainder = DIVAS->REM.reg;
- /* Enable interrupt. */
- cpu_irq_leave_critical();
- return result;
- }
- # endif
- #endif
- /** @} */
|