1/* USER CODE BEGIN Header */
2/**
3  ******************************************************************************
4  * @file    blue_unit_conversion.s
5  * @author  GPM WBL Application Team
6  * @brief   blue_unit_conversion implements the conversion from low speed oscillator
7  * independent units (STU) to low speed oscillator dependent units (MTU) and
8  * vice-versa according to the input parameters.
9  * The first parameter is the quantity to be translated expressed in STU or MTU.
10  * The second parameter is the frequency or the period.
11  * The third parameter is a threshold to switch to 32 bit multiplication.
12  * This routine avoids floating divisions exploiting only integer math, maintaining
13  * a good rounding error as well.
14  * It embeds a long multiplication algorithm suited for this purpose.
15  * The threshold passed as parameter allows performing a simple 32bit multiplication
16  * instead of long multiplication, saving computational time for small quantities
17  * (when it is needed that the conversion is done as fast as possible).
18  ******************************************************************************
19  * @attention
20  *
21  * Copyright (c) 2024 STMicroelectronics.
22  * All rights reserved.
23  *
24  * This software is licensed under terms that can be found in the LICENSE file
25  * in the root directory of this software component.
26  * If no LICENSE file comes with this software, it is provided AS-IS.
27  *
28  ******************************************************************************
29  */
30/* USER CODE END Header */
31
32/* Includes ------------------------------------------------------------------*/
33#include "asm.h"
34
35                __CODE__
36                __THUMB__
37                __EXPORT__ blue_unit_conversion
38
39EXPORT_FUNC(blue_unit_conversion)
40                  PUSH    {r4, lr}
41                  CMP     r0, r2
42                  BLS     mul32
43                  UXTH    r2, r1
44                  LSRS    r3, r0, #16
45                  LSRS    r1, r1, #16
46                  MOV     r4, r1
47                  MULS    r1, r3, r1
48                  UXTH    r0, r0
49                  MULS    r3, r2, r3
50                  MULS    r4, r0, r4
51                  MULS    r0, r2, r0
52                  ADDS    r3, r3, r4
53                  LSLS    r2, r3, #16
54                  LSRS    r3, r3, #16
55                  ADDS    r0, r2, r0
56                  ADCS    r1, r3
57
58                  MOVS    r2, #128
59                  LSLS    r2, r2, #13
60                  MOVS    r3, #0
61                  ADDS    r2, r2, r0
62                  ADCS    r3, r1
63                  LSRS    r2, r2, #21
64                  LSLS    r0, r3, #11
65                  ORRS    r0, r2
66
67                  POP {r4, pc}
68
69                  ENDFUNC
70
71EXPORT_FUNC(mul32)
72                  MULS    r0, r1, r0
73                  MOVS    r2, #128
74                  LSLS    r2, r2, #13
75                  ADDS    r2, r2, r0
76                  LSRS    r2, r2, #21
77                  MOV     r0, r2
78
79                  POP {r4, pc}
80
81                  ENDFUNC
82
83    ALIGN_MEM(4)
84	__END__
85