forked

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2013-2016 Realtek Semiconductor Corp.
AnnaBridge 167:e84263d55307 3 *
AnnaBridge 167:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 167:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 167:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 167:e84263d55307 9 *
AnnaBridge 167:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 167:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 167:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 167:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 167:e84263d55307 14 * limitations under the License.
AnnaBridge 167:e84263d55307 15 */
AnnaBridge 167:e84263d55307 16 #include "trng_api.h"
AnnaBridge 167:e84263d55307 17 #include "analogin_api.h"
AnnaBridge 167:e84263d55307 18 #include "analogin_ext.h"
AnnaBridge 167:e84263d55307 19
AnnaBridge 167:e84263d55307 20 #ifdef DEVICE_TRNG
AnnaBridge 167:e84263d55307 21
AnnaBridge 167:e84263d55307 22
AnnaBridge 167:e84263d55307 23 void trng_init(trng_t *obj)
AnnaBridge 167:e84263d55307 24 {
AnnaBridge 167:e84263d55307 25 _memset((void *)obj, 0, sizeof(trng_t));
AnnaBridge 167:e84263d55307 26 analogin_init(&obj->tradcng, ADC0);
AnnaBridge 167:e84263d55307 27 obj->inited = 1;
AnnaBridge 167:e84263d55307 28 }
AnnaBridge 167:e84263d55307 29
AnnaBridge 167:e84263d55307 30 void trng_free(trng_t *obj)
AnnaBridge 167:e84263d55307 31 {
AnnaBridge 167:e84263d55307 32 if (!obj->inited) {
AnnaBridge 167:e84263d55307 33 return;
AnnaBridge 167:e84263d55307 34 }
AnnaBridge 167:e84263d55307 35
AnnaBridge 167:e84263d55307 36 obj->inited = 0;
AnnaBridge 167:e84263d55307 37 analogin_deinit(&obj->tradcng);
AnnaBridge 167:e84263d55307 38 }
AnnaBridge 167:e84263d55307 39
AnnaBridge 167:e84263d55307 40 static void trng_get_byte(trng_t *obj, unsigned char *byte)
AnnaBridge 167:e84263d55307 41 {
AnnaBridge 167:e84263d55307 42 size_t bit;
AnnaBridge 167:e84263d55307 43 uint32_t adc[2];
AnnaBridge 167:e84263d55307 44
AnnaBridge 167:e84263d55307 45 PSAL_ADC_MNGT_ADPT pSalADCMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 46 PSAL_ADC_HND pSalADCHND = NULL;
AnnaBridge 167:e84263d55307 47
AnnaBridge 167:e84263d55307 48 pSalADCMngtAdpt = &(obj->tradcng.SalADCMngtAdpt);
AnnaBridge 167:e84263d55307 49 pSalADCHND = &(pSalADCMngtAdpt->pSalHndPriv->SalADCHndPriv);
AnnaBridge 167:e84263d55307 50
AnnaBridge 167:e84263d55307 51 *byte = 0;
AnnaBridge 167:e84263d55307 52 for (bit = 0; bit < 8; bit++) {
AnnaBridge 167:e84263d55307 53 RtkADCRxManualRotate(pSalADCHND, adc);
AnnaBridge 167:e84263d55307 54 *byte |= (adc[0] & 0x01) << bit;
AnnaBridge 167:e84263d55307 55 }
AnnaBridge 167:e84263d55307 56 }
AnnaBridge 167:e84263d55307 57
AnnaBridge 167:e84263d55307 58 int trng_get_bytes(trng_t *obj, uint8_t *buf, size_t len, size_t *output_len)
AnnaBridge 167:e84263d55307 59 {
AnnaBridge 167:e84263d55307 60 size_t i;
AnnaBridge 167:e84263d55307 61
AnnaBridge 167:e84263d55307 62 if (!obj->inited) {
AnnaBridge 167:e84263d55307 63 return -1;
AnnaBridge 167:e84263d55307 64 }
AnnaBridge 167:e84263d55307 65
AnnaBridge 167:e84263d55307 66 for (i = 0; i < len; i++) {
AnnaBridge 167:e84263d55307 67 trng_get_byte(obj, buf + i);
AnnaBridge 167:e84263d55307 68 }
AnnaBridge 167:e84263d55307 69
AnnaBridge 167:e84263d55307 70 *output_len = len;
AnnaBridge 167:e84263d55307 71 return 0;
AnnaBridge 167:e84263d55307 72 }
AnnaBridge 167:e84263d55307 73 #endif