1 /*
2 * Copyright (c) 2021 Bestechnic (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 #include "platform.h"
16 #include "cmsis.h"
17 #include "cmsis_os.h"
18 #include "hal_timer.h"
19 #include "hal_trace.h"
20 #include "hal_trng.h"
21 #include "stdlib.h"
22
23 #define BUFSIZE 256
24
print_string(const char * fmt,...)25 void print_string(const char *fmt, ...)
26 {
27 if (!fmt)
28 return;
29 char buf[BUFSIZE] = {0};
30 int len;
31 va_list ap;
32 va_start(ap, fmt);
33 len = vsnprintf(buf, BUFSIZE, fmt, ap);
34 va_end(ap);
35 if (len > 0) {
36 hal_trace_output((const unsigned char *)buf, len);
37 #ifdef LOG_FLUSH
38 hal_trace_flush_buffer();
39 #endif
40 }
41 }
42
print_array(const char * name,const void * array,uint16_t len)43 void print_array(const char *name, const void *array, uint16_t len)
44 {
45 if (!array || !len)
46 return;
47 char buf[BUFSIZE] = {0};
48 const uint8_t *ptr = (const uint8_t *)array;
49 uint16_t i, j = 0;
50 if (name) {
51 int n = P_MIN(BUFSIZE, strlen(name));
52 strncpy(buf, name, BUFSIZE);
53 j += n;
54 }
55 for (i = 0; i < len && j < BUFSIZE; ++i) {
56 int n = snprintf(&buf[j], BUFSIZE - j, "%02X ", ptr[i]);
57 j += n;
58 }
59 if (j <= BUFSIZE - 1) {
60 buf[j] = '\n';
61 j++;
62 }
63 hal_trace_output((const unsigned char *)buf, j);
64 #ifdef LOG_FLUSH
65 hal_trace_flush_buffer();
66 #endif
67 }
68
GetSysTime(void)69 uint32_t GetSysTime(void)
70 {
71 return hal_fast_sys_timer_get();
72 }
73
SysTimeDiff(uint32_t start)74 uint32_t SysTimeDiff(uint32_t start)
75 {
76 return FAST_TICKS_TO_US(hal_fast_sys_timer_get() - start) / 1000;
77 }
78
DelayMs(uint32_t ms)79 void DelayMs(uint32_t ms)
80 {
81 #if 1
82 osDelay(ms); // os 1000 ticks/s here
83 #else
84 hal_sys_timer_delay(MS_TO_TICKS(ms));
85 #endif
86 }
87
DelayUs(uint32_t us)88 void DelayUs(uint32_t us)
89 {
90 if (us >= 1000) {
91 DelayMs(us / 1000);
92 us = us % 1000;
93 }
94 uint32_t flags = int_lock();
95 uint32_t now = hal_fast_sys_timer_get();
96 while (hal_fast_sys_timer_get() - now < US_TO_FAST_TICKS(us));
97 int_unlock(flags);
98 }
99
mbedtls_hardware_poll(void * data,unsigned char * output,size_t len,size_t * olen)100 int mbedtls_hardware_poll( void *data,
101 unsigned char *output, size_t len, size_t *olen )
102 {
103 int ret;
104 TRACE(0,"%s %d start",__func__,__LINE__);
105
106 // ret = hal_get_trngdata(output, len);
107 // if (ret) {
108 uint32_t i;
109 for (i = 0; i < len; i++) {
110 output[i] = rand() & 0xFF;
111 }
112 // }
113 *olen = len;
114 TRACE(0,"%s %d done",__func__,__LINE__);
115 return 0;
116 }
117
118 // implementation for js_app_host.h: LP_TaskBegin
LP_TaskBegin()119 void LP_TaskBegin() {
120 }
121
122 // implementation for js_app_host.h: LP_TaskEnd
LP_TaskEnd()123 void LP_TaskEnd() {
124 }
125