Bladeren bron

Add timer expiring in SW.

Signed-off-by: Jakub Duchniewicz <j.duchniewicz@gmail.com>
Jakub Duchniewicz 4 dagen geleden
bovenliggende
commit
30e995fcc2
2 gewijzigde bestanden met toevoegingen van 30 en 3 verwijderingen
  1. 29 2
      projects/riscv_usb/fw/fw_app.c
  2. 1 1
      projects/riscv_usb/rtl/soc_picorv32_base.v

+ 29 - 2
projects/riscv_usb/fw/fw_app.c

@@ -41,16 +41,40 @@ float kupa;
 
 static uint32_t copy_array[16];
 
+static inline uint32_t rdcycle(void)
+{
+    uint32_t cycle;
+    __asm__ volatile ("rdcycle %0" : "=r" (cycle));
+    return cycle;
+}
+
 /* Timer fuction */
+#define CYCLES_DIV 24000000.0f
+#define UPDATE_MS 3.0f
+// TODO: integers not floats?
+
+static uint32_t last_rdcycle;
 static bool timer_expired()
 {
-
+    bool success = false;
+    uint32_t current = rdcycle();
+    uint32_t diff = current - last_rdcycle;
+    //printf(" diff %d last: %d current: %d\n", diff, last_rdcycle, current);
+    // TODO: overflow handling
+    float elapsed_seconds = (float)(diff) / CYCLES_DIV;
+    if (elapsed_seconds > UPDATE_MS)
+    {
+        last_rdcycle = current;
+        printf("Timer expired!\n");
+        success = true;;
+    }
+    return success;
 }
 
 /* three signal generation params */
 static void update_three_signal_values()
 {
-
+    //printf("Updating 3signal values\n");
 }
 
 static void print_mailbox_contents()
@@ -242,6 +266,9 @@ void main()
 
 			switch (cmd)
 			{
+            case 'q':
+                printf("Current rdcycle %d\n", rdcycle());
+                break;
 			case 'b':
 				boot_dfu();
 				break;

+ 1 - 1
projects/riscv_usb/rtl/soc_picorv32_base.v

@@ -70,7 +70,7 @@ module soc_picorv32_base #(
 		.STACKADDR(32'h 0000_0400),
 		.BARREL_SHIFTER(0),
 		.COMPRESSED_ISA(0),
-		.ENABLE_COUNTERS(0),
+		.ENABLE_COUNTERS(1),
 		.ENABLE_MUL(0),
 		.ENABLE_DIV(0),
 		.ENABLE_IRQ(0),