From 8e4775ab4ac06d7e792a28c62b546cc1a791182f Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Wed, 23 May 2012 12:16:31 +0900 Subject: [PATCH] improve regnual --- ChangeLog | 6 +++++ regnual/regnual.c | 59 +++++++++++++++++++++++----------------------- regnual/regnual.ld | 2 +- regnual/sys.c | 25 +------------------- 4 files changed, 38 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 347d52e..d20bd8b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2012-05-23 Niibe Yutaka + + * regnual/regnual.c (regnual_setup): Don't call flash_write here. + (regnual_ctrl_write_finish): But call here. + (USB_REGNUAL_RESULT): New. + 2012-05-22 Niibe Yutaka * src/configure (../regnual/sys.h): Create symblic link. diff --git a/regnual/regnual.c b/regnual/regnual.c index 21fab3f..779ecab 100644 --- a/regnual/regnual.c +++ b/regnual/regnual.c @@ -33,8 +33,7 @@ extern void *memset (void *s, int c, size_t n); extern void set_led (int); extern uint8_t _flash_start, _flash_end; extern int flash_write (uint32_t dst_addr, const uint8_t *src, size_t len); -extern int flash_erase_page (uint32_t addr); -extern int flash_protect (void); +extern int flash_protect (uint16_t protection); extern void nvic_system_reset (void); @@ -123,15 +122,15 @@ regnual_device_reset (void) 64); } -#define USB_REGNUAL_MEMINFO 0 -#define USB_REGNUAL_SEND 1 -#define USB_REGNUAL_CRC32 2 -#define USB_REGNUAL_FLASH 3 -#define USB_REGNUAL_ERASE 4 -#define USB_REGNUAL_PROTECT 5 -#define USB_REGNUAL_FINISH 6 +#define USB_REGNUAL_MEMINFO 0 +#define USB_REGNUAL_SEND 1 +#define USB_REGNUAL_RESULT 2 +#define USB_REGNUAL_FLASH 3 +#define USB_REGNUAL_PROTECT 4 +#define USB_REGNUAL_FINISH 5 static uint8_t mem[256]; +static uint32_t result; static const uint8_t *const mem_info[] = { &_flash_start, &_flash_end, }; @@ -145,9 +144,21 @@ static void regnual_ctrl_write_finish (uint8_t req, uint8_t req_no, if (type_rcp == (VENDOR_REQUEST | DEVICE_RECIPIENT) && USB_SETUP_SET (req) && len == 0) { - if (req_no == USB_REGNUAL_FINISH && value == 0 && index == 0) + if (req_no == USB_REGNUAL_SEND && index == 0) + { + result = 0; // calculate crc32 here!!! + } + else if (req_no == USB_REGNUAL_FLASH && index == 0) + { + uint32_t dst_addr = (0x08000000 + value * 0x100); + + result = flash_write (dst_addr, mem, 256); + } + else if (req_no == USB_REGNUAL_PROTECT && index == 0) + result = flash_protect (value); + else if (req_no == USB_REGNUAL_FINISH && value == 0 && index == 0) nvic_system_reset (); - } + } } static int @@ -165,11 +176,9 @@ regnual_setup (uint8_t req, uint8_t req_no, usb_lld_set_data_to_send (mem_info, sizeof (mem_info)); return USB_SUCCESS; } - else if (req_no == USB_REGNUAL_CRC32) + else if (req_no == USB_REGNUAL_RESULT) { - static uint32_t crc32_check = 0; /* calculate CRC32 for mem */ - - usb_lld_set_data_to_send (&crc32_check, sizeof (uint32_t)); + usb_lld_set_data_to_send (&result, sizeof (uint32_t)); return USB_SUCCESS; } } @@ -180,30 +189,22 @@ regnual_setup (uint8_t req, uint8_t req_no, if (value != 0 || index + len > 256) return USB_UNSUPPORT; - if (index != 0) - memset (mem, 0xff, 256); + if (index + len < 256) + memset (mem + index + len, 0xff, 256 - (index + len)); usb_lld_set_data_to_recv (mem + index, len); return USB_SUCCESS; } - else if (req_no == USB_REGNUAL_FLASH && len == 0) + else if (req_no == USB_REGNUAL_FLASH && len == 0 && index == 0) { uint32_t dst_addr = (0x08000000 + value * 0x100); - if (flash_write (dst_addr, mem, 256) == 0) + if (dst_addr + 256 <= (uint32_t)&_flash_end) return USB_SUCCESS; } - else if (req_no == USB_REGNUAL_ERASE && len == 0 && index == 0) + else if (req_no == USB_REGNUAL_PROTECT && len == 0 && index == 0) { - uint32_t dst_addr = (0x08000000 + value * 0x100); - - if (flash_erase_page (dst_addr)) - return USB_SUCCESS; - } - else if (req_no == USB_REGNUAL_PROTECT && len == 0 - && value == 0 && index == 0) - { - if (flash_protect ()) + if (value == 0) return USB_SUCCESS; } else if (req_no == USB_REGNUAL_FINISH && len == 0 diff --git a/regnual/regnual.ld b/regnual/regnual.ld index d7a73e9..c2f2df4 100644 --- a/regnual/regnual.ld +++ b/regnual/regnual.ld @@ -12,7 +12,7 @@ MEMORY } _flash_start = 0x08000000; -_flash_end = 0x08010000; +_flash_end = 0x08020000; __ram_start__ = ORIGIN(ram0); __ram_size__ = 20k; diff --git a/regnual/sys.c b/regnual/sys.c index a8454db..254dd2f 100644 --- a/regnual/sys.c +++ b/regnual/sys.c @@ -431,30 +431,7 @@ flash_write (uint32_t dst_addr, const uint8_t *src, size_t len) } int -flash_erase_page (uint32_t addr) -{ - int status; - - status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT); - - intr_disable (); - if (status == FLASH_COMPLETE) - { - FLASH->CR |= FLASH_CR_PER; - FLASH->AR = addr; - FLASH->CR |= FLASH_CR_STRT; - - status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT); - if (status != FLASH_TIMEOUT) - FLASH->CR &= ~FLASH_CR_PER; - } - intr_enable (); - - return status == FLASH_COMPLETE; -} - -int -flash_protect (void) +flash_protect (uint16_t protection) { /* Not yet implemented */ return 1;