From a43065c58291d46028a4d49b2c16a2e215bbf457 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sat, 27 Jul 2019 00:02:18 +0200
Subject: [PATCH] feat(bootloader): Show ready/writing message

---
 bootloader/bootloader-usb.c | 13 +++++++++++++
 bootloader/main.c           | 10 ++++------
 bootloader/mscmem.c         | 17 +++++++++++++++++
 lib/mx25lba/mx25lba.h       |  1 +
 4 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/bootloader/bootloader-usb.c b/bootloader/bootloader-usb.c
index 2ba7d32d..003d295a 100644
--- a/bootloader/bootloader-usb.c
+++ b/bootloader/bootloader-usb.c
@@ -52,6 +52,8 @@
 #include "descriptors.h"
 #include "mscmem.h"
 #include "card10.h"
+#include "display.h"
+#include "GUI_Paint.h"
 
 /***** Definitions *****/
 #define EVENT_ENUM_COMP MAXUSB_NUM_EVENTS
@@ -112,6 +114,17 @@ void delay_us(unsigned int usec)
 	mxc_delay(usec);
 }
 
+void bootloader_dirty(void)
+{
+	Paint_DrawString_EN(0, 16 * 3, "Writing.", &Font16, 0x0000, 0xf000);
+	LCD_Update();
+}
+void bootloader_clean(void)
+{
+	Paint_DrawString_EN(0, 16 * 3, "Ready.  ", &Font16, 0x0000, 0xffff);
+	LCD_Update();
+}
+
 /******************************************************************************/
 void run_usbmsc(void)
 {
diff --git a/bootloader/main.c b/bootloader/main.c
index 3bb2ba17..187ebcc7 100644
--- a/bootloader/main.c
+++ b/bootloader/main.c
@@ -238,12 +238,10 @@ int main(void)
 	// If the button is pressed, we go into MSC mode.
 	if (PB_Get(3)) {
 		Paint_DrawString_EN(
-			0,
-			16 * 2,
-			"USB activated. Waiting.",
-			&Font16,
-			0x0000,
-			0xffff
+			0, 16 * 2, "USB activated.", &Font16, 0x0000, 0xffff
+		);
+		Paint_DrawString_EN(
+			0, 16 * 3, "Ready.", &Font16, 0x0000, 0xffff
 		);
 		LCD_Update();
 		run_usbmsc();
diff --git a/bootloader/mscmem.c b/bootloader/mscmem.c
index 8e2febd8..82d6ce88 100644
--- a/bootloader/mscmem.c
+++ b/bootloader/mscmem.c
@@ -3,6 +3,11 @@
 #include <stdio.h>
 #include "mx25lba.h"
 
+void bootloader_dirty(void);
+void bootloader_clean(void);
+
+static int dirty = 0;
+
 /******************************************************************************/
 int mscmem_init()
 {
@@ -27,6 +32,10 @@ int mscmem_read(uint32_t lba, uint8_t *buffer)
 int mscmem_write(uint32_t lba, uint8_t *buffer)
 {
 	//printf("%s\n", __func__);
+	if (dirty == 0) {
+		bootloader_dirty();
+	}
+	dirty = 2;
 	return mx25_write(lba, buffer);
 }
 
@@ -48,5 +57,13 @@ int mscmem_stop()
 int mscmem_ready()
 {
 	//printf("%s\n", __func__);
+	if (dirty) {
+		dirty--;
+		if (dirty == 0) {
+			printf("sync\n");
+			mx25_sync();
+			bootloader_clean();
+		}
+	}
 	return mx25_ready();
 }
diff --git a/lib/mx25lba/mx25lba.h b/lib/mx25lba/mx25lba.h
index 11c0176d..1aa09c2c 100644
--- a/lib/mx25lba/mx25lba.h
+++ b/lib/mx25lba/mx25lba.h
@@ -52,5 +52,6 @@ uint32_t mx25_size(void);
 int mx25_read(uint32_t lba, uint8_t* buffer);
 int mx25_write(uint32_t lba, uint8_t* buffer);
 int mx25_ready(void);
+int mx25_sync(void);
 
 #endif  /* __MX25_H__ */
-- 
GitLab