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 #ifndef __HAL_TRNG_H__ 16 #define __HAL_TRNG_H__ 17 18 #include "plat_types.h" 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 #define HAL_TRNG_DATA_LEN 24 25 26 enum HAL_TRNG_RND_SRC_SEL 27 { 28 HAL_TRNG_RND_SRC_SHORTEST = 0, 29 HAL_TRNG_RND_SRC_SHORT, 30 HAL_TRNG_RND_SRC_LONG, 31 HAL_TRNG_RND_SRC_LONGEST, 32 }; 33 typedef unsigned int HAL_TRNG_RND_SRC_SEL_ENUM; 34 35 struct HAL_TRNG_CFG_T 36 { 37 uint32_t sample_cntr1; // must not be less than 0x11 38 HAL_TRNG_RND_SRC_SEL_ENUM rnd_src_sel; 39 }; 40 41 typedef void (*HAL_TRNG_RUN_CB_T)(const uint32_t *buf, uint32_t len, uint32_t error); 42 43 int hal_trng_open(const struct HAL_TRNG_CFG_T *cfg); 44 45 void hal_trng_close(void); 46 47 /* 48 * Attention: User random api should add lock around this function, and check the return value 49 * If this function return error or timeout, you can increase sample_cntr1 50 * TRNG module is stopped in the end of this function 51 * Don't close TRNG module between twice hal_get_trngdata, or the result is NOT TRUE RNADOM 52 * The power consumption of trng module is about 30uA@3.8V at best2003 after stopping 53 */ 54 int hal_get_trngdata(uint8_t *buf, uint32_t buf_len); 55 56 int hal_trng_run(HAL_TRNG_RUN_CB_T cb); 57 58 void hal_trng_cancel(void); 59 60 #ifdef __cplusplus 61 } 62 #endif 63 64 #endif //__HAL_TRNG_H__ 65