From eceb844e87163b69fe2eb59f032faccce556ce22 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Mon, 3 Apr 2023 04:39:26 +0000 Subject: [PATCH] kernel --- firmware/rtl/waveform/waveform.v | 15 ++++++++++++++- software/src/access.c | 32 +++++++++++++++++++++++--------- software/src/main.c | 31 +++++++++++++++++++++++-------- software/src/upsilon.h | 3 +++ 4 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 software/src/upsilon.h diff --git a/firmware/rtl/waveform/waveform.v b/firmware/rtl/waveform/waveform.v index 9336217..4bea01f 100644 --- a/firmware/rtl/waveform/waveform.v +++ b/firmware/rtl/waveform/waveform.v @@ -1,5 +1,6 @@ /* Write a waveform to a DAC. */ -/* TODO: Add reset pin. */ +/* TODO: Add reset pin. + * Add "how many values to go" counter. */ module waveform #( parameter DAC_WID = 24, parameter DAC_WID_SIZ = 5, @@ -20,6 +21,18 @@ module waveform #( input clk, input arm, input halt_on_finish, + /* NOTE: + * finished is used when a module wants to wait for a + * waveform with the halt_on_finish flag finishes + * one waveform. + * + * running is used when a module wants to know when + * the waveform module has finished running after + * deasserting arm. + * + * When in doubt, deassert arm and wait for running + * to be deasserted. + */ output reg finished, output running, input [TIMER_WID-1:0] time_to_wait, diff --git a/software/src/access.c b/software/src/access.c index c756f40..8d8c21d 100644 --- a/software/src/access.c +++ b/software/src/access.c @@ -11,7 +11,7 @@ #include #include -#include "converters.h" +#include "access.h" #include "pin_io.h" LOG_MODULE_REGISTER(access); @@ -244,6 +244,16 @@ waveform_take(int waveform, k_timeout_t timeout) return e; } +static void +waveform_disarm_wait(int wf) +{ + *wf_arm[wf] = 0; + if (*wf_running[wf]) { + k_sleep(K_NSEC(10* *wf_time_to_wait[wf]); + while (*wf_running[wf]); + } +} + int waveform_release(int waveform) { @@ -251,8 +261,7 @@ waveform_release(int waveform) return -EFAULT; if (waveform_locked[waveform] == 1) { - *wf_arm[waveform] = 0; - while (*wf_running[waveform]); + waveform_disarm_wait(waveform); } int e k_mutex_unlock(waveform_mutex + waveform); @@ -294,6 +303,12 @@ waveform_load(uint32_t buf[MAX_WL_SIZE], int slot, k_timeout_t timeout) return 1; } +int +waveform_halt_until_finished(int slot) +{ + // stub +} + int waveform_arm(int slot, bool halt_on_finish, uint32_t wait, k_timeout_t timeout) { @@ -314,8 +329,7 @@ waveform_arm(int slot, bool halt_on_finish, uint32_t wait, k_timeout_t timeout) int waveform_disarm(int slot) { - *wf_arm[slot] = 0; - while (*wf_running[slot]); + waveform_disarm_wait(slot); waveform_release(slot); dac_release(slot); return 1; @@ -331,7 +345,7 @@ access_release_thread(void) while (cloop_release() == 0) cloop_locked--; if (cloop_locked != 0) { - LOG_WRN("cloop mutex counter mismatch"); + LOG_WRN("%s: cloop mutex counter mismatch", get_thread_name()); cloop_locked = 0; } @@ -339,14 +353,14 @@ access_release_thread(void) while (dac_release(i) == 0); dac_locked[i]--; if (dac_locked[i] != 0) { - LOG_WRN("dac mutex %d counter mismatch", i); + LOG_WRN("%s: dac mutex %d counter mismatch", get_thread_name(), i); dac_locked[i] = 0; } while (waveform_release(i) == 0) waveform_locked[i]--; if (waveform_locked[i] != 0) { - LOG_WRN("waveform mutex %d counter mismatch", i); + LOG_WRN("%s: waveform mutex %d counter mismatch", get_thread_name(), i); waveform_locked[i] = 0; } } @@ -358,7 +372,7 @@ access_release_thread(void) while (adc_release(i) == 0) adc_locked[i]--; if (adc_locked[i] != 0) { - LOG_WRN("adc mutex %d counter mismatch", i); + LOG_WRN("%s: adc mutex %d counter mismatch", get_thread_name(), i); adc_locked[i] = 0; } } diff --git a/software/src/main.c b/software/src/main.c index 27f55f7..7cead20 100644 --- a/software/src/main.c +++ b/software/src/main.c @@ -4,6 +4,8 @@ #include #include +#include "upsilon.h" +#include "access.h" #include "sock.h" #include "buf.h" @@ -23,9 +25,9 @@ read_size(int s) { char buf[2]; struct bufptr bp = {buf, sizeof(buf)}; - - if (!sock_read_buf(s, &bp, true)) - return -1; + int e = sock_read_buf(s, &bp, true); + if (e != 0) + return e; return (unsigned char)buf[0] | (unsigned char) buf[1] << 8; } @@ -54,8 +56,6 @@ exec_creole(unsigned char *buf, int size, int sock) } -/* TODO: error messages */ - static void exec_entry(void *client_p, void *threadnum_p, void *unused __attribute__((unused))) @@ -64,13 +64,22 @@ exec_entry(void *client_p, void *threadnum_p, intptr_t threadnum = threadnum_p; int size = read_size(client); + const char thread_name[32]; + vsnprintk(thread_name, sizeof(thread_name), "%d", client); + k_thread_name_set(k_current_get(), thread_name); + + LOG_INF("%s: Connection initiated", thread_name); + if (size < 0) { + LOG_WRN("%s: error in read size: %d", get_thread_name(), size); zsock_close(client); return; } struct bufptr bp = {readbuf[threadnum], size}; - if (!sock_read_buf(client, &bp, true)) { + int e = sock_read_buf(client, &bp, true); + if (e != 0) { + LOG_WRN("%s: error in read body: %d", get_thread_name(), e); zsock_close(client); return; } @@ -83,6 +92,7 @@ exec_entry(void *client_p, void *threadnum_p, static void main_loop(int srvsock) { + static unsigned int connection_counter = 0; for (;;) { int client = server_accept_client(srvsock); int i; @@ -90,9 +100,11 @@ main_loop(int srvsock) for (i = 0; i < THREADNUM; i++) { if (!thread_ever_used[i] || k_thread_join(threads[i], 0) == 0) { + connection_counter++; k_thread_create(threads[i], stacks[i], THREAD_STACK_SIZ, exec_entry, - (uintptr_t) client, (uintptr_t) i, NULL, + (uintptr_t) client, (uintptr_t) i, + (uintptr_t) connection_counter, 1, 0, K_NO_WAIT); } } @@ -100,6 +112,7 @@ main_loop(int srvsock) if (i == THREADNUM) { LOG_INF("Too many connections (max %d)", THREADNUM); + zsock_close(client); } } } @@ -107,9 +120,11 @@ main_loop(int srvsock) void main(void) { + access_init(); + k_thread_name_get(k_current_get(), "main thread"); for (;;) { int sock = server_init_sock(6626); main_loop(sock); - close(sock); + zsock_close(sock); } } diff --git a/software/src/upsilon.h b/software/src/upsilon.h new file mode 100644 index 0000000..ab6b521 --- /dev/null +++ b/software/src/upsilon.h @@ -0,0 +1,3 @@ +#pragma once + +#define get_thread_name() k_thread_name_get(k_current_get())