1 /******************************************************************************
2  *  Filename:       cpu.c
3  *
4  *  Description:    Instruction wrappers for special CPU instructions needed by
5  *                  the drivers.
6  *
7  *  Copyright (c) 2022-2023 Texas Instruments Incorporated
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions are met:
11  *
12  *  1) Redistributions of source code must retain the above copyright notice,
13  *     this list of conditions and the following disclaimer.
14  *
15  *  2) Redistributions in binary form must reproduce the above copyright notice,
16  *     this list of conditions and the following disclaimer in the documentation
17  *     and/or other materials provided with the distribution.
18  *
19  *  3) Neither the name of the copyright holder nor the names of its
20  *     contributors may be used to endorse or promote products derived from this
21  *     software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  *  POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************/
36 
37 #include "cpu.h"
38 
39 //*****************************************************************************
40 //
41 // Provide a small delay
42 //
43 //*****************************************************************************
44 #if defined(DOXYGEN)
CPUDelay(uint32_t count)45 void CPUDelay(uint32_t count)
46 {
47     // This function is written in assembly. See cpu.c for compiler specific implementation.
48 }
49 #elif defined(__IAR_SYSTEMS_ICC__)
CPUDelay(uint32_t count)50 void CPUDelay(uint32_t count)
51 {
52     // Loop the specified number of times
53     __asm("CPUDelay:\n"
54           "    subs    r0, #1\n"
55           "    bne.n   CPUDelay\n"
56           "    bx      lr");
57     #pragma diag_suppress = Pe940
58 }
59     #pragma diag_default  = Pe940
60 #elif defined(__clang__)
CPUDelay(uint32_t count)61 void __attribute__((naked)) CPUDelay(uint32_t count)
62 {
63     // Loop the specified number of times.
64     // The naked attribute tells the compiler that the function is effectively
65     // hand-coded assembly implemented using inlined assembly without operands.
66     // As such, it assumes that the C calling conventions are obeyed, and we can
67     // assume count is in R0.
68     __asm volatile("CPUdel%=:\n"
69                    "    subs r0, #1\n"
70                    "    bne   CPUdel%=\n"
71                    "    bx    lr\n"
72                    :            /* No output */
73                    :            /* No input */
74                    : "r0", "cc" /* Clobbers. "cc" is the flags */
75     );
76 }
77 #elif defined(__GNUC__)
CPUDelay(uint32_t count)78 void __attribute__((naked)) CPUDelay(uint32_t count)
79 {
80     // Loop the specified number of times
81     __asm volatile(".syntax unified\n"
82                    "CPUdel%=:\n"
83                    "    subs  %0, #1\n"
84                    "    bne   CPUdel%=\n"
85                    "    bx    lr\n"
86                    :            /* No output */
87                    : "r"(count) /* Input */
88     );
89 }
90 #else
91     #error "Unsupported toolchain!"
92 #endif
93