You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2019/02/08 13:56:00 UTC

[GitHub] kasjer commented on a change in pull request #1638: [TRNG] Add K64F driver

kasjer commented on a change in pull request #1638: [TRNG] Add K64F driver
URL: https://github.com/apache/mynewt-core/pull/1638#discussion_r255064483
 
 

 ##########
 File path: hw/drivers/trng/trng_k64f/src/trng_k64f.c
 ##########
 @@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include "mcu/cmsis_nvic.h"
+#include "fsl_rnga.h"
+#include "trng/trng.h"
+#include "trng_k64f/trng_k64f.h"
+
+static uint8_t rng_cache[ MYNEWT_VAL(K64F_TRNG_CACHE_LEN) ];
+static uint16_t rng_cache_out;
+static uint16_t rng_cache_in;
+static os_stack_t *pstack;
+static bool running;
+
+#define RNGA_POLLER_PRIO (8)
+#define RNGA_POLLER_STACK_SIZE OS_STACK_ALIGN(64)
+static struct os_task poller_task;
+
+static inline void
+k64f_rnga_start(void)
+{
+    RNGA_SetMode(RNG, kRNGA_ModeNormal);
+    running = true;
+}
+
+static inline void
+k64f_rnga_stop(void)
+{
+   RNGA_SetMode(RNG, kRNGA_ModeSleep);
+   running = false;
+}
+
+static size_t
+k64f_trng_read(struct trng_dev *trng, void *ptr, size_t size)
+{
+    os_sr_t sr;
+    size_t num_read;
+
+    OS_ENTER_CRITICAL(sr);
+
+    if (rng_cache_out <= rng_cache_in) {
+        size = min(size, rng_cache_in - rng_cache_out);
+        memcpy(ptr, &rng_cache[rng_cache_out], size);
+        num_read = size;
+    } else if (rng_cache_out + size <= sizeof(rng_cache)) {
+        memcpy(ptr, &rng_cache[rng_cache_out], size);
+        num_read = size;
+    } else {
+        num_read = sizeof(rng_cache) - rng_cache_out;
+        memcpy(ptr, &rng_cache[rng_cache_out], num_read);
+
+        size -= num_read;
+        ptr += num_read;
+
+        size = min(size, rng_cache_in);
+        memcpy(ptr, rng_cache, size);
+        num_read += size;
+    }
+
+    rng_cache_out = (rng_cache_out + num_read) % sizeof(rng_cache);
+
+    if (num_read > 0) {
+        k64f_rnga_start();
+    }
+
+    OS_EXIT_CRITICAL(sr);
+
+    return num_read;
+}
+
+static uint32_t
+k64f_trng_get_u32(struct trng_dev *trng)
+{
+    union {
+        uint32_t v32;
+        uint8_t v8[4];
+    } val;
+    size_t num;
+
+    num = k64f_trng_read(trng, &val.v8, sizeof(val.v8));
+    while (num < sizeof(val.v8)) {
+        os_sched(NULL);
+        num += k64f_trng_read(trng, &val.v8[num], sizeof(val.v8) - num);
+    }
+
+    return val.v32;
+}
+
+static void
+rnga_poller_handler(void *arg)
+{
+    int8_t i;
+    uint8_t data[4];
+    int rc;
+
+    while (1) {
+        if (running) {
+            rc = RNGA_GetRandomData(RNG, data, sizeof(uint32_t));
+            if (rc == 0) {
 
 Review comment:
   it probably should be (rc == kStatus_Success)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services