@@ -485,8 +485,23 @@ void hal_prepare_boot(void)
485485
486486}
487487
488+ #ifdef __CCRX__
489+ /* copy RAM functions from ROM to RAM */
490+ static void copyfuncs (void )
491+ {
492+ unsigned char * dst , * src ;
493+ src = __sectop ("PFRAM" );
494+ dst = __sectop ("RPFRAM" );
495+ while (src < __secend ("PFRAM" )) {
496+ * dst ++ = * src ++ ;
497+ }
498+ }
499+ #endif
488500int hal_flash_init (void )
489501{
502+ #ifdef __CCRX__
503+ copyfuncs ();
504+ #endif
490505 /* Flash Write Enable */
491506 FLASH_FWEPROR = FLASH_FWEPROR_FLWE ;
492507
@@ -506,23 +521,41 @@ int hal_flash_init(void)
506521/* write up to 128 bytes at a time */
507522#define FLASH_FACI_CODE_BLOCK_SZ \
508523 (FLASH_FACI_CMD_PROGRAM_CODE_LENGTH * FLASH_FACI_CMD_PROGRAM_DATA_LENGTH)
524+ #define FLASH_OK 0
525+ #define FLASH_ERR_ALIGN -1
526+ #define FLASH_ERR_PRG -2
527+ #define FLASH_ERR_ILGL -3
528+ #define FLASH_ERR_ERS -4
529+ #ifdef __CCRX__
530+ #pragma section FRAM
531+ #define MEMCPY ram_memcpy
532+ #else
533+ #define MEMCPY memcpy
534+ #endif
509535int RAMFUNCTION hal_flash_write (uint32_t addr , const uint8_t * data , int len )
510536{
511- int ret , i , chunk ;
512- uint8_t codeblock [FLASH_FACI_CODE_BLOCK_SZ ];
537+ int i ;
538+ uint8_t codeblock [FLASH_FACI_CODE_BLOCK_SZ ] = { 0 } ;
513539 uint16_t * data16 = (uint16_t * )data ;
540+ uint32_t block_base ;
541+ uint32_t offset ;
542+ int write_size ;
514543
515544 while (len > 0 ) {
516- /* handle partial remainder */
517- if ( len < FLASH_FACI_CODE_BLOCK_SZ ) {
518- uint8_t * src = ( uint8_t * ) addr ;
519- int remain = FLASH_FACI_CODE_BLOCK_SZ - len ;
520- memcpy (codeblock , data16 , len );
521- memcpy ( codeblock + len , src + len , remain ) ;
522- data16 = ( uint16_t * ) codeblock ;
523- }
545+ /* Align address to 128-byte boundary */
546+ block_base = addr & ~( FLASH_FACI_CODE_BLOCK_SZ - 1 );
547+ offset = addr - block_base ;
548+
549+ MEMCPY (codeblock , ( uint8_t * ) block_base , FLASH_FACI_CODE_BLOCK_SZ );
550+ write_size = FLASH_FACI_CODE_BLOCK_SZ - offset ;
551+ if ( write_size > len )
552+ write_size = len ;
524553
525- FLASH_FSADDR = addr ;
554+ MEMCPY (& codeblock [offset ], data , write_size );
555+ data16 = (uint16_t * )codeblock ;
556+
557+
558+ FLASH_FSADDR = block_base ;
526559 /* flash program command */
527560 FLASH_FACI_CMD8 = FLASH_FACI_CMD_PROGRAM ;
528561 /* number of 16-bit blocks: for code blocks is always 0x40 (64) */
@@ -539,9 +572,18 @@ int RAMFUNCTION hal_flash_write(uint32_t addr, const uint8_t *data, int len)
539572
540573 /* Wait for FCU operation to complete */
541574 while ((FLASH_FSTATR & FLASH_FSTATR_FRDY ) == 0 );
542-
543- len -= FLASH_FACI_CODE_BLOCK_SZ ;
544- addr += FLASH_FACI_CODE_BLOCK_SZ ;
575+ if (FLASH_FSTATR & FLASH_FSTATR_ILGLERR ) {
576+ return FLASH_ERR_ILGL ;
577+ }
578+ if (FLASH_FSTATR & FLASH_FSTATR_PRGERR ) {
579+ return FLASH_ERR_PRG ;
580+ }
581+ if (FLASH_FSTATR & FLASH_FSTATR_ERSERR ) {
582+ return FLASH_ERR_PRG ;
583+ }
584+ len -= write_size ;
585+ addr += write_size ;
586+ data += write_size ;
545587 }
546588 return 0 ;
547589}
@@ -639,7 +681,9 @@ void RAMFUNCTION hal_flash_lock(void)
639681 FLASH_FENTRYR_CODE_READ | FLASH_FENTRYR_DATA_READ );
640682 return ;
641683}
642-
684+ #ifdef __CCRX__
685+ #pragma section
686+ #endif
643687#if !defined(WOLFBOOT_NO_PARTITIONS ) && !defined(TARGET_library )
644688void * hal_get_primary_address (void )
645689{
0 commit comments