• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include "duet.h"
20 #include "duet_common.h"
21 #ifdef CFG_DUET_FREERTOS
22 #include "projdefs.h"
23 #include "FreeRTOSConfig.h"
24 #include "portmacro.h"
25 #endif
26 // void jumpToApp(int addr)
27 // {
28 //    __asm("LDR SP, [R0]");
29 //    __asm("LDR PC, [R0, #4]");
30 // }
31 
duet_memset(char * buf,int value,int size)32 void duet_memset(char *buf, int value, int size)
33 {
34     if (NULL == buf) {
35         return;
36     }
37 
38     while (size--) {
39         *buf++ = value;
40     }
41 }
42 
duet_memcpy(char * dst,char * src,int size)43 void duet_memcpy(char *dst, char *src, int size)
44 {
45     while (size--) {
46         *dst++ = *src++;
47     }
48 }
49 
delay(unsigned int cycles)50 FLASH_COMMON2_SEG void delay(unsigned int cycles)
51 {
52     unsigned int us2cycles = (cycles * 52) / 4;
53     while (us2cycles > 0) {
54         us2cycles--;
55         asm("nop");
56     }
57 }
58 
59 /* please open flash cache when using this function.
60 otherwise, it is not accuratd */
udelay_pl(unsigned int us)61 void udelay_pl(unsigned int us)
62 {
63     unsigned int us2cycles = us * (SYSTEM_CLOCK / 1000000) / 9;
64     us2cycles = (us2cycles >= 6) ? (us2cycles - 6) : 0;
65     /* 9 clock cycles */
66     while (us2cycles > 0) {
67         us2cycles--;
68         asm("nop");
69     }
70 }
71 
72 #ifdef CFG_DUET_FREERTOS
lega_enter_critical_expble(void)73 void lega_enter_critical_expble(void)
74 {
75     portCLEAR_INTERRUPT_MASK_FROM_ISR((configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1) << (8 - configPRIO_BITS));
76 }
77 
lega_exit_critical_expble(void)78 void lega_exit_critical_expble(void)
79 {
80     portCLEAR_INTERRUPT_MASK_FROM_ISR(0);
81 }
82 #endif
83 
84 #if 1
convert_str_to_int(char * str)85 int convert_str_to_int(char *str)
86 {
87     int result = 0;
88     char *stop;
89     result = strtol(str, &stop, 0);
90     if (*stop != '\0') {
91         return DUET_STR_TO_INT_ERR;
92     }
93     return result;
94 }
95 #else
convert_str_to_int(char * str)96 int convert_str_to_int(char *str)
97 {
98     int val = 0, size = 0;
99     char *p = str;
100 
101     if (NULL == p) {
102         return -1;
103     }
104 
105     if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) { // it is a hex value
106         p += 2; // pass '0x'
107 
108         while (size < 8) {
109             if ('0' <= *p && *p <= '9') {
110                 val = val * 16 + (*p - '0');
111                 size++;
112             } else if ('a' <= *p && *p <= 'f') {
113                 val = val * 16 + (*p - 'a' + 10); // 'a' is 97
114                 size++;
115 
116             } else if ('A' <= *p && *p <= 'F') {
117                 val = val * 16 + (*p - 'A' + 10); // 'A' is 65
118                 size++;
119             } else {
120                 break;
121             }
122             p++;
123 
124         }
125     } else { // it is a dec. value
126         while ('0' <= *p && *p <= '9') {
127             val = val * 10 + (*p - '0');
128             p++;
129         }
130     }
131     return val;
132 }
133 #endif
134 // type = 1, val is uint8
135 // type = 2, val is uint16
136 // type = 4, val is uint32
convert_int_to_str(unsigned int val,unsigned int type,char * ch)137 void convert_int_to_str(unsigned int val, unsigned int type, char *ch)
138 {
139     int i = 0, tempVal;
140 
141     if (ch == 0) {
142         return;
143     }
144 
145     while (i < (type << 1)) {
146         tempVal = (val >> (((type << 1) - i - 1) << 2)) & 0xF;
147 
148         if (tempVal >= 0x0 && tempVal <= 0x9) {
149             ch[i] = tempVal + '0'; // 0 to 9
150         } else if ((tempVal >= 0xA && tempVal <= 0xF)) {
151             ch[i] = tempVal - 0xA + 'A'; // a to f
152         }
153         i++;
154     }
155     ch[i] = '\0';
156 }
157 
duet_write32_bit(uint32_t reg,uint8_t start_bit,uint8_t len,uint32_t src_val)158 void duet_write32_bit(uint32_t reg, uint8_t start_bit, uint8_t len, uint32_t src_val)
159 {
160     uint32_t tmp, mask, val;
161 
162     if ((start_bit < 32) && (len <= 32) && (src_val <= ((1 << len) - 1))) {
163         tmp = REG_RD(reg);
164 
165         mask = (1 << len) - 1;      // eg: len=4, mask = 0xf, 1111
166         mask = ~(mask << start_bit);
167 
168         val = tmp & mask;
169 
170         src_val = (src_val << start_bit);
171         val = val | src_val;
172 
173         REG_WR(reg, val);
174     } else {
175         printf("write32_bit input parms not support \r\n");
176     }
177 }
178 
duet_read32_bit(uint32_t reg,uint8_t start_bit,uint8_t len)179 uint32_t duet_read32_bit(uint32_t reg, uint8_t start_bit, uint8_t len)
180 {
181     uint32_t mask, val;
182 
183     if ((start_bit < 32) && (len <= 32)) {
184         val = REG_RD(reg);
185 
186         mask = (1 << len) - 1;      // eg: len =4, 0xf,1111
187         mask = mask << start_bit;
188         val = val & mask;
189 
190         val = (val >> start_bit);
191 
192         return val;
193     } else {
194         printf("read32_bit input parms not support \r\n");
195         return -1;
196     }
197 }
198 
199 /********************* end *********************/
200