changeset 336:7be3475fc9fa

Fixing bugs in mk20dx128vlf5 support
author Jacob Alexander <haata@kiibohd.com>
date Mon, 25 May 2015 17:50:32 -0700
parents 795cc567b018
children 932ef3bbd4d7
files Bootloader/CMakeLists.txt Bootloader/flash.c Bootloader/main.c
diffstat 3 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Bootloader/CMakeLists.txt	Mon May 25 17:23:18 2015 -0700
+++ b/Bootloader/CMakeLists.txt	Mon May 25 17:50:32 2015 -0700
@@ -60,7 +60,6 @@
 #
 set( SRCS
 	main.c
-	debug.c # TODO only compile in if necessary
 	dfu.c
 	dfu.desc.c
 	flash.c
@@ -68,6 +67,14 @@
 	usb.c
 )
 
+# Only compile in if necessary
+if( CHIP STREQUAL "mk20dx256vlh7" )
+	set( SRCS ${SRCS}
+		debug.c
+	)
+endif()
+
+
 message( STATUS "Bootloader Source Files:" )
 message( "${SRCS}" )
 
--- a/Bootloader/flash.c	Mon May 25 17:23:18 2015 -0700
+++ b/Bootloader/flash.c	Mon May 25 17:50:32 2015 -0700
@@ -101,15 +101,20 @@
 
 int flash_program_sector( uintptr_t addr, size_t len )
 {
-#if defined(_mk20dx128vlf5_)
-	return (len != FLASH_SECTOR_SIZE
-		|| (addr & (FLASH_SECTOR_SIZE - 1)) != 0
-		|| flash_erase_sector( addr )
-		|| flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 ));
-#elif defined(_mk20dx256vlh7_)
 	if ( len != FLASH_SECTOR_SIZE )
 		return 1;
 
+#if defined(_mk20dx128vlf5_)
+	// Check if this is the beginning of a sector
+	// Only erase if necessary
+	if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
+		&& flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 4 )
+		&& flash_erase_sector( addr ) )
+			return 1;
+
+	// Program sector
+	return flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 );
+#elif defined(_mk20dx256vlh7_)
 	// Check if beginning of sector and erase if not empty
 	// Each sector is 2 kB in length, but we can only write to half a sector at a time
 	// We can only erase an entire sector at a time
--- a/Bootloader/main.c	Mon May 25 17:23:18 2015 -0700
+++ b/Bootloader/main.c	Mon May 25 17:50:32 2015 -0700
@@ -36,7 +36,7 @@
 
 // ----- Functions -----
 
-void sector_print( void* buf, size_t sector, size_t chunks )
+int sector_print( void* buf, size_t sector, size_t chunks )
 {
 	uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE;
 	uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE;
@@ -79,6 +79,8 @@
 
 		print( NL );
 	}
+
+	return retval;
 }
 
 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
@@ -133,9 +135,6 @@
 	if ( !target )
 		return (DFU_STATUS_errADDRESS);
 	memcpy( target, buf, len );
-	print("BUF: ");
-	printHex( off );
-	sector_print( target, 0, 16 );
 
 	// Depending on the error return a different status
 	switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )