src/cpu/arm/at91sam7/serialtoflash.c
int serial_to_flash(u_char *flash_adr)
{
    u_char pagebuf[MAX_FLASH_PAGE];
    int page_offset;
    int pagelen;
    u_char c;
    u_long *flash_ptr;

    pagelen = (1 << FLASH_PAGE_BITS);

    do {
        if (timed_getchar(&c, LONG_TIMEOUT) != 0) {
            return -1;
        }
//    } while (c != START_CHAR && c != IMAGE_CHAR);
    } while (c == '\r');

    if (c == START_CHAR) {
        if (timed_getchar(&c, LONG_TIMEOUT) != 0) {
            return -1;
        }
    }

    flash_ptr = (u_long *)((u_long)flash_adr & ~(pagelen-1));

    for (page_offset = 0; page_offset < pagelen; page_offset++) {
        pagebuf[page_offset] = *((u_char *)flash_ptr)++;
    }

    flash_ptr = (u_long *)((u_long)flash_adr & ~(pagelen-1));
    page_offset = (u_long)flash_adr & (pagelen-1);

    do {
        pagebuf[page_offset++] = c;
        if (page_offset == pagelen) {
            if (page_to_flash((u_long *)pagebuf, flash_ptr) != 0) {
                return -2;
            }
            flash_ptr += (pagelen/sizeof(long));
            page_offset = 0;
        }
    } while (timed_getchar(&c, SHORT_TIMEOUT) == 0);
    
    wait_flash();
    if (page_offset != 0) {
        if (page_to_flash((u_long *)pagebuf, flash_ptr) != 0) {
            return -2;
        }
    }
    wait_flash();

    if (((int)flash_adr & 0xfffff) == 0) {   // Can't return because we clobbered the program
        RSTC_CR = PROCRST | PERRST;  // Reset processor and peripherals
    }

    return (u_char *)flash_ptr - flash_adr + page_offset;
}
// Assumes that the serial port is already initialized
// flash_adr must be page-aligned
Assumes that the serial port is already initialized
flash_adr must be page-aligned
Assumes that the serial port is already initialized flash_adr must be page - aligned
int serial_to_flash(u_char *flash_adr)
{
    u_char pagebuf[MAX_FLASH_PAGE];
    int page_offset;
    int pagelen;
    u_char c;
    u_long *flash_ptr;
    pagelen = (1 << FLASH_PAGE_BITS);
    do {
        if (timed_getchar(&c, LONG_TIMEOUT) != 0) {
            return -1;
        }
    } while (c == '\r');
    if (c == START_CHAR) {
        if (timed_getchar(&c, LONG_TIMEOUT) != 0) {
            return -1;
        }
    }
    flash_ptr = (u_long *)((u_long)flash_adr & ~(pagelen-1));
    for (page_offset = 0; page_offset < pagelen; page_offset++) {
        pagebuf[page_offset] = *((u_char *)flash_ptr)++;
    }
    flash_ptr = (u_long *)((u_long)flash_adr & ~(pagelen-1));
    page_offset = (u_long)flash_adr & (pagelen-1);
    do {
        pagebuf[page_offset++] = c;
        if (page_offset == pagelen) {
            if (page_to_flash((u_long *)pagebuf, flash_ptr) != 0) {
                return -2;
            }
            flash_ptr += (pagelen/sizeof(long));
            page_offset = 0;
        }
    } while (timed_getchar(&c, SHORT_TIMEOUT) == 0);
    wait_flash();
    if (page_offset != 0) {
        if (page_to_flash((u_long *)pagebuf, flash_ptr) != 0) {
            return -2;
        }
    }
    wait_flash();
    if (((int)flash_adr & 0xfffff) == 0) {   
        RSTC_CR = PROCRST | PERRST;  
    }
    return (u_char *)flash_ptr - flash_adr + page_offset;
}
int serial_to_flash ( u_char * flash_adr ) { u_char pagebuf [ MAX_FLASH_PAGE ] ; int page_offset ; int pagelen ; u_char c ; u_long * flash_ptr ; pagelen = ( 1 << FLASH_PAGE_BITS ) ; do { if ( timed_getchar ( & c , LONG_TIMEOUT ) != 0 ) { return -1 ; } } while ( c == ' \r ' ) ; if ( c == START_CHAR ) { if ( timed_getchar ( & c , LONG_TIMEOUT ) != 0 ) { return -1 ; } } flash_ptr = ( u_long * ) ( ( u_long ) flash_adr & ~ ( pagelen - 1 ) ) ; for ( page_offset = 0 ; page_offset < pagelen ; page_offset ++ ) { pagebuf [ page_offset ] = * ( ( u_char * ) flash_ptr ) ++ ; } flash_ptr = ( u_long * ) ( ( u_long ) flash_adr & ~ ( pagelen - 1 ) ) ; page_offset = ( u_long ) flash_adr & ( pagelen - 1 ) ; do { pagebuf [ page_offset ++ ] = c ; if ( page_offset == pagelen ) { if ( page_to_flash ( ( u_long * ) pagebuf , flash_ptr ) != 0 ) { return -2 ; } flash_ptr += ( pagelen / sizeof ( long ) ) ; page_offset = 0 ; } } while ( timed_getchar ( & c , SHORT_TIMEOUT ) == 0 ) ; wait_flash ( ) ; if ( page_offset != 0 ) { if ( page_to_flash ( ( u_long * ) pagebuf , flash_ptr ) != 0 ) { return -2 ; } } wait_flash ( ) ; if ( ( ( int ) flash_adr & 0xfffff ) == 0 ) { RSTC_CR = PROCRST | PERRST ; } return ( u_char * ) flash_ptr - flash_adr + page_offset ; }
Assumes that the serial port is already initialized
flash_adr must be page-aligned
Assumes that the serial port is already initialized flash_adr must be page - aligned
//    } while (c != START_CHAR && c != IMAGE_CHAR); // Can't return because we clobbered the program // Reset processor and peripherals