.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Bootloader/bootloader.h

Committer:
Sissors
Date:
2016-04-23
Revision:
8:00cefe0d59ed
Parent:
7:4ab0430d06e3
Child:
9:56ed3a56ecc3

File content as of revision 8:00cefe0d59ed:

#include "mbed.h"
#include "EthernetInterface.h"
#include "rtos.h"

#define START_ADDRESS   0x80000
#define NUM_SECTORS     120
#define SECTOR_SIZE     4096
#define BUFFER_SIZE     1024


//Bootloader functions
int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9;
int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465;
void *(*bootloader)(int) = (void *(*)(int))0x79121;


static void write(char *value)
{
    int i = 0;
    //Loop through string and send everything
    while(*(value+i) != '\0') {
        while(!(UART0->S1 & UART_S1_TDRE_MASK));
        UART0->D = *(value+i);
        i++;
    }
}

//Getting the new binary
void write_flash(void)
{

    printf("Erasing flash!\r\n");
    for (int i = 0; i < NUM_SECTORS; i++)
        erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE);

    printf("Connecting ethernet\r\n");
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\r\n", eth.getIPAddress());

    TCPSocketConnection sock;
    sock.connect("192.168.0.18", 80);
    printf("Connected to mbed\r\n");
    char http_cmd[] = "HEAD /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n";
    sock.send_all(http_cmd, sizeof(http_cmd)-1);
    printf("Send head request\r\n");

    char buffer[BUFFER_SIZE];
    printf("Receiving head (hihi)\r\n");
    int headlength = sock.receive(buffer, sizeof(buffer));
    if (headlength == BUFFER_SIZE) {
        printf("Head exceeded expected maximum, loading will fail\r\n");
        while(1);
    }
    sock.close();
    wait(2);
    sock.connect("192.168.0.18", 80);

    char http_cmd2[] = "GET /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n";

    sock.send_all(http_cmd2, sizeof(http_cmd2)-1);
    printf("Send get request\r\n");
    if (sock.receive(buffer, headlength) != headlength) {
        printf("No response\r\n");
        while(1);
    }

    int count;
    int ret;
    int received = 0;
    while (true) {
        //printf("Receiving\r\n");
        ret = sock.receive(&buffer[received], 16-received);
        received = received + ret;
        if (ret <= 0) {
            if (received != 0) {
                program_flash_boot(count+START_ADDRESS, buffer, 16);
            }
            break;
        }
        
        printf("%d\r\n", ret);
        if (received == 16) {
            __disable_irq();
            if (program_flash_boot(count+START_ADDRESS, buffer, 16) != 0) {
                write("Error!\r\n");
                while(1);
            }
            __enable_irq();
            count += received;
            received = 0;
            
        }


    }

    sock.close();

    eth.disconnect();
    printf("Done\r\n");

    bootloader(count);

}