소스 검색

Fix mailbox by introducing intermediate buffer for storing and saving.

Jakub Duchniewicz 1 주 전
부모
커밋
254d9836e4
1개의 변경된 파일70개의 추가작업 그리고 40개의 파일을 삭제
  1. 70 40
      projects/riscv_usb/fw/fw_app.c

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

@@ -32,17 +32,66 @@
 #include "utils.h"
 #include "registers.h"
 
+#define FLASH_MAILBOX_STORAGE 0x0F0000
+
 /*
 double dupa;
 float kupa;
 */
 
+static uint32_t copy_array[16];
+
+static void print_mailbox_contents()
+{
+    int i = 0;
+    printf("Mailbox contents:\n");
+    for(i = 0; i < 16; ++i)
+        printf("Mailbox[%d]: %d\n", i, mailbox_regs->data[i]);
+}
+
+static void store_mailbox_regs_flash()
+{
+    // Copy current mailbox array to a temporary array for bulk memory write
+    // our Wishbone implementation does not support bulk writes
+    for (int i = 0; i < 16; ++i)
+        copy_array[i] = mailbox_regs->data[i];
+
+    flash_write_enable();
+    flash_sector_erase(FLASH_MAILBOX_STORAGE);
+    // Wait for flash to finish (poll status register)
+    while (flash_read_sr() & 0x01) /* WIP bit */;
+    flash_write_enable();
+
+    flash_page_program((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
+    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()
+{
+    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()
 {
 	uint8_t buf[8];
-	//char *id, *desc;
-	//int i;
 
 	flash_manuf_id(buf);
 	printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
@@ -50,27 +99,7 @@ serial_no_init()
 	flash_unique_id(buf);
 	printf("Flash Unique ID    : %s\n", hexstr(buf, 8, true));
 
-    //uint32_t old = *(volatile uint32_t *)(MAILBOX_BASE + 0x00);
-    //printf("Old=0x%x\n", old);
-    //*(volatile uint32_t *)(MAILBOX_BASE + 0x00) = 0x00000004;
-    //uint32_t new = *(volatile uint32_t *)(MAILBOX_BASE + 0x00);
-    //printf("New=0x%x\n", new);
-    //printf("Old=0x%x, New=0x%x\n", old, new);
-
-    // Print Mailbox contents just to be sure */
-    //printf("Mailbox period1: %d\n", mailbox_regs->regs.period1);
-    //printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay1);
-
-    //// Debug print all mailbox memory
-    //for (i = 0; i < 64; ++i) {
-    //    //printf("%x ", *((int*)MAILBOX_BASE+i));
-    //    printf("%x ", *((int*)MAILBOX_BASE+i));
-    //    if (i != 0 && i % 16)
-    //        printf("\n");
-    //}
-
-    //mailbox_regs->regs.delay3= 0x1;
-    //printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay3);
+    //dummy_write_to_flash();
 
     /* Testing floats TODO: test if calculation is correct
     dupa = 3.1415;
@@ -87,32 +116,21 @@ serial_no_init()
 static void write_period1()
 {
     printf("Writing period1 0x1\n");
-    //uint8_t *strMem = (uint8_t *)&_printf_buf;
-	//for (int i=0; i<32; i++) {
-	//   printf("%x ", strMem[i]);
-	//}
     mailbox_regs->regs.period1 = 0x1;
     printf("Done writing\n");
-    //uint8_t * strMem = (uint8_t *)&_printf_buf;
-	//for (int i=0; i<32; i++) {
-	//   printf("%x ", strMem[i]);
-	//}
-	printf("\n");
+
+    print_mailbox_contents();
+    store_mailbox_regs_flash();
 }
 
 static void write_period1_2()
 {
     printf("Writing period1 0x2\n");
-    //uint8_t *strMem = (uint8_t *)&_printf_buf;
-	//for (int i=0; i<32; i++) {
-	//   printf("%x ", strMem[i]);
-	//}
     mailbox_regs->regs.period1 = 0x2;
     printf("Done writing\n");
-    //uint8_t * strMem = (uint8_t *)&_printf_buf;
-	//for (int i=0; i<32; i++) {
-	//   printf("%x ", strMem[i]);
-	//}
+
+    print_mailbox_contents();
+    store_mailbox_regs_flash();
 }
 
 static void write_period1_4()
@@ -122,6 +140,8 @@ static void write_period1_4()
     printf("Done writing\n");
     printf("Reading now \n");
     printf("value: %d\n", mailbox_regs->regs.period1);
+    print_mailbox_contents();
+    store_mailbox_regs_flash();
 }
 
 static void clear_period1()
@@ -136,6 +156,7 @@ static void write_delay1()
     printf("Writing delay1 0x1234\n");
     mailbox_regs->regs.delay1 = 0x1234;
     printf("Done writing\n");
+    store_mailbox_regs_flash();
 }
 
 static void clear_delay1()
@@ -145,6 +166,12 @@ static void clear_delay1()
     printf("Done clearing\n");
 }
 
+static void print_flash_contents()
+{
+    read_mailbox_regs_flash();
+    print_mailbox_contents();
+}
+
 static void
 boot_dfu(void)
 {
@@ -220,6 +247,9 @@ void main()
             case 'm':
                 clear_delay1();
                 break;
+            case 'f':
+                print_flash_contents();
+                break;
 			default:
 				break;
 			}