Просмотр исходного кода

More outlining.

Signed-off-by: Jakub Duchniewicz <j.duchniewicz@gmail.com>
Jakub Duchniewicz 10 часов назад
Родитель
Сommit
21cf6ac2bb

+ 31 - 18
projects/riscv_usb/fw/fw_app.c

@@ -31,6 +31,7 @@
 #include "spi.h"
 #include "utils.h"
 #include "registers.h"
+#include "buttons.h"
 #include "generation.h"
 
 /* FLASH */
@@ -77,6 +78,26 @@ static bool timer_expired()
     return success;
 }
 
+static void write_regs_to_mailbox()
+{
+    mailbox_regs->regs.period1 = out_params.regs._period1;
+    // TODO: seems like period2 etc are missing
+    mailbox_regs->regs.delay1 = out_params.regs.D1 / FXP_SCALING;
+    //mailbox_regs->regs.period2 = out_params.regs._period2;
+    mailbox_regs->regs.delay2 = out_params.regs.D2 / FXP_SCALING;
+    //mailbox_regs->regs.duty3 = out_params.regs._du
+    mailbox_regs->regs.delay3 = out_params.regs.D3 / FXP_SCALING;
+    mailbox_regs->regs.npuls3 = out_params.regs.N3;
+    mailbox_regs->regs.odd_train_flag = N_PULSE_TRAINS; // TODO: is this proper?
+    mailbox_regs->regs.ena_odd_out3 = out_params.regs.ena;
+}
+
+static void clear_button_regs()
+{
+    for (int i = 0; i < 16; ++i)
+        mailbox_button_regs->data[i] = 0x0;
+}
+
 static void print_mailbox_contents()
 {
     int i = 0;
@@ -112,21 +133,6 @@ static void store_generation_values_flash()
     while (flash_read_sr() & 0x01) /* WIP bit */;
 }
 
-/*
-static void dummy_write_to_flash()
-{
-    printf("Dummy writing to flash\n");
-    int i = 0;
-    for (i = 0; i < 16; ++i)
-    {
-        mailbox_regs->data[i] = 0x7890;
-    }
-
-    print_mailbox_contents();
-    store_mailbox_regs_flash();
-}
-*/
-
 static void
 serial_no_init()
 {
@@ -222,7 +228,7 @@ boot_dfu(void)
 void
 usb_dfu_rt_cb_reboot(void)
 {
-        boot_dfu();
+    boot_dfu();
 }
 
 void main()
@@ -250,7 +256,10 @@ void main()
     read_generation_values_from_flash();
 
 	/* If values are wrong - fill defaults */
-	validate_generation_values(&out_params.regs);
+	if (validate_generation_values(&out_params.regs) != 0)
+        init_params(&out_params.regs);
+
+    write_regs_to_mailbox();
 
 	/* Main loop */
 	while (1)
@@ -258,11 +267,15 @@ void main()
         /* Run the timer for button updates periodically */
         if (timer_expired())
         {
-            update_three_signal_values();
+            update_three_signal_values(&out_params.regs, mailbox_button_regs);
+            // button registers have to be cleared upon reading
+            clear_button_regs();
+            write_regs_to_mailbox();
             // TODO: need a new value to write contents to flash ( probably +1 mailbox?)
 			//write_to_flash();
         }
 
+        // TODO: Add incrementing/decrementing values from console
 #ifdef USE_KEYBOARD
 		/* Prompt ? */
 		if (cmd >= 0)

+ 25 - 7
projects/riscv_usb/fw/generation.c

@@ -31,19 +31,37 @@ void init_params(tweaked_params_s * pParams) {
     pParams->_period3 = DIV_ROUND(BASE_FREQ, pParams->f3);
 }
 
-void validate_generation_values(tweaked_params_s * pParams)
+uint32_t validate_generation_values(tweaked_params_s * pParams)
 {
+    if (pParams->f1 < MIN_FREQ1 || pParams->f1 > MAX_FREQ1)
+        return -1;
 
-    init_params(pParams);
+    if (pParams->D1 < MIN_D1D2 || pParams->D1 > MAX_D1D2)
+        return -1;
+
+    // TODO: missing ph2 restrictions?
+    if (pParams->D2 < MIN_D1D2 || pParams->D2 > MAX_D1D2)
+        return -1;
+
+    if (pParams->Ph3 > MAX_PH3)
+        return -1;
+
+    if (pParams->f3 < MIN_FREQ3 || pParams->f3 > MAX_FREQ3)
+        return -1;
+
+    // TODO: missing restrictions for f3 and D3
+
+    return 0;
 }
 
-void update_three_signal_values()
+void update_three_signal_values(tweaked_params_s * pParams, wb_mailbox_button_regs_t * pButtons)
 {
-    //printf("Updating 3signal values\n");
-    // read values of updated registers
+    // TODO: fill all the remaining incrementations/decrementations
 }
 
-void inc_f1(tweaked_params_s * pParams){
+
+// TODO: all of these have to be updated by a value read from the register (how to calibrate it?)
+void inc_f1(tweaked_params_s * pParams, uint32_t val){
     int32_t f1 = pParams->f1;
 
     f1 += 5;
@@ -66,7 +84,7 @@ void inc_f1(tweaked_params_s * pParams){
     pParams->f1 = f1;
 }
 
-void dec_f1(tweaked_params_s * pParams) {
+void dec_f1(tweaked_params_s * pParams, uint32_t val) {
     int32_t f1 = pParams->f1;
 
     f1 -= 5;

+ 3 - 2
projects/riscv_usb/fw/generation.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include "config.h"
+#include "buttons.h"
 #include <stdint.h>
 
 #define FXP_SCALING 4000
@@ -49,5 +50,5 @@ typedef struct {
 } tweaked_params_union_s;
 
 void init_params(tweaked_params_s * pParams);
-void validate_generation_values(tweaked_params_s * pParams);
-void update_three_signal_values();
+uint32_t validate_generation_values(tweaked_params_s * pParams);
+void update_three_signal_values(tweaked_params_s * pParams, wb_mailbox_button_regs_t * pButtons);

+ 1 - 2
projects/riscv_usb/fw/registers.h

@@ -16,8 +16,7 @@ typedef struct {
     uint32_t npuls3;
     uint32_t odd_train_flag;
     uint32_t ena_odd_out3;
-    uint32_t write_flash;
-    uint32_t reserved[4];
+    uint32_t reserved[5];
 } three_signal_regs_t __attribute__((packed,aligned(4)));
 
 typedef struct {