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

 ##########
 File path: hw/drivers/trng/trng_k64f/src/trng_k64f.c
 ##########
 @@ -0,0 +1,192 @@
+/*
+ * 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 "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 struct os_mutex rng_cache_mu;
+static os_stack_t *pstack;
+static bool running;
+static struct os_eventq rng_evtq;
+
+#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)
+{
+    struct os_event evt;
+
+    RNGA_SetMode(RNG, kRNGA_ModeNormal);
+    running = true;
+
+    evt.ev_queued = 0;
+    evt.ev_arg = NULL;
+    (void)os_eventq_put(&rng_evtq, &evt);
+}
+
+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)
+{
+    size_t num_read;
+
+    os_mutex_pend(&rng_cache_mu, OS_TIMEOUT_NEVER);
+
+    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;
 
 Review comment:
   I avoid doing math with void *, as it's not portable.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to