Browse Source

Port most register management code.

Signed-off-by: Jakub Duchniewicz <j.duchniewicz@gmail.com>
Jakub Duchniewicz 11 hours ago
parent
commit
762321e7b8

+ 3 - 2
projects/riscv_usb/fw/Makefile

@@ -15,7 +15,8 @@ HEADERS_common=\
 	mini-printf.h \
 	mini-printf.h \
 	spi.h \
 	spi.h \
 	utils.h \
 	utils.h \
-	registers.h
+	registers.h \
+	generation.h
 
 
 SOURCES_common=\
 SOURCES_common=\
 	start.S \
 	start.S \
@@ -24,7 +25,7 @@ SOURCES_common=\
 	mini-printf.c  \
 	mini-printf.c  \
 	spi.c \
 	spi.c \
 	utils.c \
 	utils.c \
-	registers.c
+	generation.c
 
 
 HEADERS_app=\
 HEADERS_app=\
 	#usb_str_app.gen.h \
 	#usb_str_app.gen.h \

+ 13 - 0
projects/riscv_usb/fw/config.h

@@ -23,8 +23,21 @@
 
 
 #pragma once
 #pragma once
 
 
+// 100 MHz base frequency
+#define BASE_FREQ 100000000
+
 #define UART_BASE	         0x81000000
 #define UART_BASE	         0x81000000
 #define SPI_BASE	         0x82000000
 #define SPI_BASE	         0x82000000
 #define LED_BASE	         0x83000000
 #define LED_BASE	         0x83000000
 #define MAILBOX_REGS_BASE    0x84000000
 #define MAILBOX_REGS_BASE    0x84000000
 #define MAILBOX_BUTTONS_BASE 0x85000000
 #define MAILBOX_BUTTONS_BASE 0x85000000
+
+#define SUPPORT_ODD_PULSE_TRAIN
+
+#if defined(SUPPORT_ODD_PULSE_TRAIN)
+    #define N_PULSE_TRAINS 2
+#else
+    #define N_PULSE_TRAINS 1
+#endif
+
+#define USE_KEYBOARD (1)

+ 40 - 25
projects/riscv_usb/fw/fw_app.c

@@ -31,15 +31,21 @@
 #include "spi.h"
 #include "spi.h"
 #include "utils.h"
 #include "utils.h"
 #include "registers.h"
 #include "registers.h"
+#include "generation.h"
 
 
+/* FLASH */
 #define FLASH_MAILBOX_STORAGE 0x0F0000
 #define FLASH_MAILBOX_STORAGE 0x0F0000
 
 
+static uint32_t copy_array[16];
+
 /*
 /*
 double dupa;
 double dupa;
 float kupa;
 float kupa;
 */
 */
 
 
-static uint32_t copy_array[16];
+/* Local generation params for tweaking actual HW generation params */
+static tweaked_params_union_s out_params;
+
 
 
 static inline uint32_t rdcycle(void)
 static inline uint32_t rdcycle(void)
 {
 {
@@ -50,7 +56,7 @@ static inline uint32_t rdcycle(void)
 
 
 /* Timer fuction */
 /* Timer fuction */
 #define CYCLES_DIV 24000000.0f
 #define CYCLES_DIV 24000000.0f
-#define UPDATE_MS 3.0f
+#define UPDATE_MS 0.1f
 // TODO: integers not floats?
 // TODO: integers not floats?
 
 
 static uint32_t last_rdcycle;
 static uint32_t last_rdcycle;
@@ -65,18 +71,12 @@ static bool timer_expired()
     if (elapsed_seconds > UPDATE_MS)
     if (elapsed_seconds > UPDATE_MS)
     {
     {
         last_rdcycle = current;
         last_rdcycle = current;
-        printf("Timer expired!\n");
+        //printf("Timer expired!\n");
         success = true;;
         success = true;;
     }
     }
     return success;
     return success;
 }
 }
 
 
-/* three signal generation params */
-static void update_three_signal_values()
-{
-    //printf("Updating 3signal values\n");
-}
-
 static void print_mailbox_contents()
 static void print_mailbox_contents()
 {
 {
     int i = 0;
     int i = 0;
@@ -85,12 +85,22 @@ static void print_mailbox_contents()
         printf("Mailbox[%d]: %d\n", i, mailbox_regs->data[i]);
         printf("Mailbox[%d]: %d\n", i, mailbox_regs->data[i]);
 }
 }
 
 
-static void store_mailbox_regs_flash()
+static void read_generation_values_from_flash()
 {
 {
+    flash_read((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
+
+    // Copy data to actual registers
+    for (int i = 0; i < 14; ++i)
+        out_params.data[i] = copy_array[i];
+}
+
+static void store_generation_values_flash()
+{
+	// TODO: can we remove it already as we don't read directly to mailbox
     // Copy current mailbox array to a temporary array for bulk memory write
     // Copy current mailbox array to a temporary array for bulk memory write
     // our Wishbone implementation does not support bulk writes
     // our Wishbone implementation does not support bulk writes
     for (int i = 0; i < 16; ++i)
     for (int i = 0; i < 16; ++i)
-        copy_array[i] = mailbox_regs->data[i];
+        copy_array[i] = out_params.data[i];
 
 
     flash_write_enable();
     flash_write_enable();
     flash_sector_erase(FLASH_MAILBOX_STORAGE);
     flash_sector_erase(FLASH_MAILBOX_STORAGE);
@@ -102,15 +112,7 @@ static void store_mailbox_regs_flash()
     while (flash_read_sr() & 0x01) /* WIP bit */;
     while (flash_read_sr() & 0x01) /* WIP bit */;
 }
 }
 
 
-static void read_mailbox_regs_flash()
-{
-    flash_read((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
-
-    // Copy data to actual mailbox
-    for (int i = 0; i < 16; ++i)
-        mailbox_regs->data[i] = copy_array[i];
-}
-
+/*
 static void dummy_write_to_flash()
 static void dummy_write_to_flash()
 {
 {
     printf("Dummy writing to flash\n");
     printf("Dummy writing to flash\n");
@@ -123,6 +125,7 @@ static void dummy_write_to_flash()
     print_mailbox_contents();
     print_mailbox_contents();
     store_mailbox_regs_flash();
     store_mailbox_regs_flash();
 }
 }
+*/
 
 
 static void
 static void
 serial_no_init()
 serial_no_init()
@@ -156,7 +159,7 @@ static void write_period1()
     printf("Done writing\n");
     printf("Done writing\n");
 
 
     print_mailbox_contents();
     print_mailbox_contents();
-    store_mailbox_regs_flash();
+    //store_mailbox_regs_flash();
 }
 }
 
 
 static void write_period1_2()
 static void write_period1_2()
@@ -166,7 +169,7 @@ static void write_period1_2()
     printf("Done writing\n");
     printf("Done writing\n");
 
 
     print_mailbox_contents();
     print_mailbox_contents();
-    store_mailbox_regs_flash();
+    //store_mailbox_regs_flash();
 }
 }
 
 
 static void write_period1_4()
 static void write_period1_4()
@@ -177,7 +180,7 @@ static void write_period1_4()
     printf("Reading now \n");
     printf("Reading now \n");
     printf("value: %d\n", mailbox_regs->regs.period1);
     printf("value: %d\n", mailbox_regs->regs.period1);
     print_mailbox_contents();
     print_mailbox_contents();
-    store_mailbox_regs_flash();
+    //store_mailbox_regs_flash();
 }
 }
 
 
 static void clear_period1()
 static void clear_period1()
@@ -192,7 +195,7 @@ static void write_delay1()
     printf("Writing delay1 0x1234\n");
     printf("Writing delay1 0x1234\n");
     mailbox_regs->regs.delay1 = 0x1234;
     mailbox_regs->regs.delay1 = 0x1234;
     printf("Done writing\n");
     printf("Done writing\n");
-    store_mailbox_regs_flash();
+    //store_mailbox_regs_flash();
 }
 }
 
 
 static void clear_delay1()
 static void clear_delay1()
@@ -204,7 +207,7 @@ static void clear_delay1()
 
 
 static void print_flash_contents()
 static void print_flash_contents()
 {
 {
-    read_mailbox_regs_flash();
+    read_generation_values_from_flash();
     print_mailbox_contents();
     print_mailbox_contents();
 }
 }
 
 
@@ -243,13 +246,24 @@ void main()
 	/* Enable USB directly */
 	/* Enable USB directly */
 	serial_no_init();
 	serial_no_init();
 
 
+    /* Read generation reg values from flash */
+    read_generation_values_from_flash();
+
+	/* If values are wrong - fill defaults */
+	validate_generation_values(&out_params.regs);
+
 	/* Main loop */
 	/* Main loop */
 	while (1)
 	while (1)
 	{
 	{
         /* Run the timer for button updates periodically */
         /* Run the timer for button updates periodically */
         if (timer_expired())
         if (timer_expired())
+        {
             update_three_signal_values();
             update_three_signal_values();
+            // TODO: need a new value to write contents to flash ( probably +1 mailbox?)
+			//write_to_flash();
+        }
 
 
+#ifdef USE_KEYBOARD
 		/* Prompt ? */
 		/* Prompt ? */
 		if (cmd >= 0)
 		if (cmd >= 0)
 			printf("Command> ");
 			printf("Command> ");
@@ -297,5 +311,6 @@ void main()
 				break;
 				break;
 			}
 			}
 		}
 		}
+#endif
 	}
 	}
 }
 }

+ 261 - 0
projects/riscv_usb/fw/generation.c

@@ -0,0 +1,261 @@
+#include "generation.h"
+
+/* Default values of client's registers
+ * f1: 16kHz
+ * D1: 49.2%
+ * Ph2: 0.0
+ * D2: 49.2%
+ * Ph3: 0.0
+ * f3: 1.15MHz
+ * N3: 10 impules
+ * D3: 4%
+ */
+void init_params(tweaked_params_s * pParams) {
+    pParams->f1 = 16000;
+    pParams->D1 = 0.492 * FXP_SCALING;//49.2 %
+    pParams->D2 = 0.492 * FXP_SCALING;//49.2 %
+
+    pParams->Ph2 = 0.00 * FXP_SCALING;
+
+    pParams->Ph3 = 0.00 * FXP_SCALING;
+    pParams->f3 = 1150000;
+    pParams->D3 = 40;//4%
+    pParams->N3 = 10;
+
+    pParams->ena = ENABLE_FORCE_NONE;
+
+    pParams->_period1 = DIV_ROUND(BASE_FREQ, pParams->f1);
+    pParams->_duty1 = pParams->_period1 * pParams->D1 / FXP_SCALING;
+    pParams->_duty2 = pParams->_period1 * pParams->D2 / FXP_SCALING;
+    pParams->_phase2 = pParams->Ph2 * pParams->_period1 / FXP_SCALING;
+    pParams->_period3 = DIV_ROUND(BASE_FREQ, pParams->f3);
+}
+
+void validate_generation_values(tweaked_params_s * pParams)
+{
+
+    init_params(pParams);
+}
+
+void update_three_signal_values()
+{
+    //printf("Updating 3signal values\n");
+    // read values of updated registers
+}
+
+void inc_f1(tweaked_params_s * pParams){
+    int32_t f1 = pParams->f1;
+
+    f1 += 5;
+    if(f1>MAX_FREQ1)
+        return;
+    int32_t _period1 = DIV_ROUND(BASE_FREQ, f1);
+
+    uint32_t pulses_time = pParams->_period3*(pParams->N3+2)*N_PULSE_TRAINS;
+    if(pulses_time>_period1)
+        return;
+
+    int32_t _duty1 = _period1*pParams->D1/4000;
+    int32_t _duty2 = _period1*pParams->D2/4000;
+    int32_t _phase2 = _period1*pParams->Ph2/4000;
+
+    pParams->_duty1 = _duty1;
+    pParams->_duty2 = _duty2;
+    pParams->_phase2 = _phase2;
+    pParams->_period1 = _period1;
+    pParams->f1 = f1;
+}
+
+void dec_f1(tweaked_params_s * pParams) {
+    int32_t f1 = pParams->f1;
+
+    f1 -= 5;
+    if(f1<MIN_FREQ1)
+        return;
+    int32_t _period1 = DIV_ROUND(BASE_FREQ, f1);
+
+    int32_t _duty1 = _period1*pParams->D1/4000;
+    int32_t _duty2 = _period1*pParams->D2/4000;
+    int32_t _phase2 = _period1*pParams->Ph2/4000;
+
+    pParams->_duty1 = _duty1;
+    pParams->_duty2 = _duty2;
+    pParams->_phase2 = _phase2;
+    pParams->_period1 = _period1;
+    pParams->f1 = f1;
+}
+
+void inc_D1(tweaked_params_s * pParams) {
+    int32_t D1 = pParams->D1;
+
+    D1++;
+    if(D1>MAX_D1D2)
+        return;
+    int32_t _duty1 = pParams->_period1*D1/4000;
+    if(_duty1+pParams->_phase2 >= pParams->_period1/2)
+        return;
+
+    pParams->D1 = D1;
+    pParams->_duty1 = _duty1;
+}
+
+void dec_D1(tweaked_params_s * pParams) {
+    int32_t D1 = pParams->D1;
+
+    D1--;
+    if(D1<MIN_D1D2)
+        return;
+    int32_t _duty1 = pParams->_period1*D1/4000;
+
+    pParams->D1 = D1;
+    pParams->_duty1 = _duty1;
+}
+
+
+void inc_D2(tweaked_params_s * pParams) {
+    int32_t D2 = pParams->D2;
+
+    D2++;
+    if(D2>MAX_D1D2)
+        return;
+    int32_t _duty2 = pParams->_period1*D2/4000;
+
+    pParams->D2 = D2;
+    pParams->_duty2 = _duty2;
+}
+
+void dec_D2(tweaked_params_s * pParams) {
+    int32_t D2 = pParams->D2;
+
+    D2--;
+    if(D2<MIN_D1D2)
+        return;
+    int32_t _duty2 = pParams->_period1*D2/4000;
+
+    pParams->D2 = D2;
+    pParams->_duty2 = _duty2;
+}
+
+void inc_Ph2(tweaked_params_s * pParams) {
+    int32_t Ph2 = pParams->Ph2;
+
+    Ph2++;
+    int32_t _phase2 = Ph2*pParams->_period1/4000;
+    if(pParams->_duty1+_phase2 >= pParams->_period1/2)
+        return;
+
+    pParams->Ph2 = Ph2;
+    pParams->_phase2 = _phase2;
+}
+
+void dec_Ph2(tweaked_params_s * pParams) {
+    int32_t Ph2 = pParams->Ph2;
+
+    Ph2--;
+    if(Ph2<0)
+        return;//limit value
+    int32_t _phase2 = Ph2*pParams->_period1/4000;
+
+    pParams->Ph2 = Ph2;
+    pParams->_phase2 = _phase2;
+}
+
+void inc_Ph3(tweaked_params_s * pParams) {
+    int32_t Ph3 = pParams->Ph3;
+
+    Ph3++;
+    if(Ph3>MAX_PH3)
+        return;//limit value
+
+    pParams->Ph3 = Ph3;
+}
+
+void dec_Ph3(tweaked_params_s * pParams) {
+    int32_t Ph3 = pParams->Ph3;
+
+    Ph3--;
+    if(Ph3<0)
+        return;//limit value
+
+    pParams->Ph3 = Ph3;
+}
+
+void inc_f3(tweaked_params_s * pParams) {
+    int32_t f3 = pParams->f3;
+
+    f3 += 10000;
+    if(f3>MAX_FREQ3)
+        return;
+    int32_t _period3 = DIV_ROUND(BASE_FREQ, f3);
+
+    pParams->f3 = f3;
+    pParams->_period3 = _period3;
+}
+
+void dec_f3(tweaked_params_s * pParams) {
+    int32_t f3 = pParams->f3;
+
+    f3 -= 10000;
+    if(f3<MIN_FREQ3)
+        return;
+    int32_t _period3 = DIV_ROUND(BASE_FREQ, f3);
+
+    uint32_t pulses_time = _period3*(pParams->N3+2)*N_PULSE_TRAINS;
+    if(pulses_time>pParams->_period1)
+        return;
+
+    pParams->f3 = f3;
+    pParams->_period3 = _period3;
+}
+
+void inc_D3(tweaked_params_s * pParams) {
+    int32_t D3 = pParams->D3;
+
+    D3 += 5;
+    if(D3>1000)
+        return;
+
+    pParams->D3 = D3;
+}
+
+void dec_D3(tweaked_params_s * pParams) {
+    int32_t D3 = pParams->D3;
+
+    D3 -= 5;
+    if(D3<0)
+        return;
+
+    pParams->D3 = D3;
+}
+
+void inc_N3(tweaked_params_s * pParams) {
+    int32_t N3 = pParams->N3;
+
+    N3++;
+    if(N3>254)
+        return;
+
+    uint32_t pulses_time = pParams->_period3*(N3+2)*N_PULSE_TRAINS;
+    if(pulses_time>pParams->_period1)
+        return;
+
+    pParams->N3 = N3;
+}
+
+void dec_N3(tweaked_params_s * pParams) {
+    int32_t N3 = pParams->N3;
+
+    N3--;
+    if(N3<1)
+        return;
+
+    pParams->N3 = N3;
+}
+
+void ena_force_toggle(tweaked_params_s * pParams) {
+    enable_force_t ena = pParams->ena;
+
+    ena = ena==ENABLE_FORCE_DISABLE?ENABLE_FORCE_ENABLE:ENABLE_FORCE_DISABLE;
+
+    pParams->ena = ena;
+}

+ 53 - 0
projects/riscv_usb/fw/generation.h

@@ -0,0 +1,53 @@
+#pragma once
+
+#include "config.h"
+#include <stdint.h>
+
+#define FXP_SCALING 4000
+#define DIV_ROUND(a,b) (((a)+(b)/2)/(b))
+
+static const int32_t MIN_FREQ1 = 7000;
+static const int32_t MAX_FREQ1 = 30000;
+static const int32_t MIN_FREQ3 = 1000000;
+static const int32_t MAX_FREQ3 = 3000000;
+static const int32_t MIN_D1D2 = 0.005 * FXP_SCALING;//0.5%
+static const int32_t MAX_D1D2 = 0.495 * FXP_SCALING;//49.5%
+static const int32_t MAX_PH3 = 3000;
+
+
+typedef enum {
+    ENABLE_FORCE_NONE = 0,
+    ENABLE_FORCE_ENABLE,
+    ENABLE_FORCE_DISABLE
+} enable_force_t;
+
+typedef struct {
+    uint32_t f1;//Hz
+    uint32_t _period1;//steps
+    uint32_t D1;//4000/100%
+    uint32_t _duty1;//steps
+    uint32_t D2;//4000/100%
+    uint32_t _duty2;//steps
+
+    uint32_t Ph2;//4000steps/360*
+    uint32_t _phase2;//steps
+
+    uint32_t Ph3;//4000steps/360*
+    uint32_t f3;//Hz
+    uint32_t _period3;//steps
+    uint32_t D3;//*0.1%
+    uint32_t N3;//n
+
+    enable_force_t ena;
+} tweaked_params_s;
+
+typedef struct {
+	union {
+		uint32_t data[14];
+		tweaked_params_s regs;
+	};
+} tweaked_params_union_s;
+
+void init_params(tweaked_params_s * pParams);
+void validate_generation_values(tweaked_params_s * pParams);
+void update_three_signal_values();

+ 0 - 4
projects/riscv_usb/fw/registers.c

@@ -1,4 +0,0 @@
-
-#pragma once
-
-#include "registers.h"

+ 0 - 6
projects/riscv_usb/fw/registers.h

@@ -28,9 +28,3 @@ typedef struct {
 } wb_mailbox_regs_t __attribute__((packed,aligned(4)));
 } wb_mailbox_regs_t __attribute__((packed,aligned(4)));
 
 
 static volatile wb_mailbox_regs_t * const mailbox_regs = (void*)(MAILBOX_REGS_BASE);
 static volatile wb_mailbox_regs_t * const mailbox_regs = (void*)(MAILBOX_REGS_BASE);
-// TOOD: we need deafults somewhere
-//static const wb_mailbox_regs_t mailbox_defaults = {
-//    .regs = {
-//        .period1 = 10
-//    }
-//};