Skip to content

Commit

Permalink
console and boot exchanging files
Browse files Browse the repository at this point in the history
  • Loading branch information
jjts committed Feb 11, 2021
1 parent 012a424 commit 84db182
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ software/firmware/*
software/console/*
!software/console/console.c
!software/console/console.h
!software/console/rs232comm.c
!software/console/socketcomm.c
!software/console/Makefile

document/presentation/*
Expand Down
33 changes: 21 additions & 12 deletions software/bootloader/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@
#include "interconnect.h"
#include "iob-uart.h"

#if (USE_DDR==1 && RUN_DDR==1)
#if (USE_DDR_SW==1 && RUN_DDR_SW==1)
#include "iob-cache.h"
#endif

#define UART_BASE (1<<P) |(UART<<(ADDR_W-2-N_SLAVES_W))
//why not include periphs.h instead?

// address to copy firmware to
#if (USE_DDR==0 || (USE_DDR==1 && RUN_DDR==0))
char *prog_start_addr = (char *) (1<<BOOTROM_ADDR_W);
#else
char *prog_start_addr = (char *) EXTRA_BASE;
#endif

#define LOAD FRX
#define PROGNAME "IOb-Bootloader"

int main() {

//init uart
Expand All @@ -30,7 +24,7 @@ int main() {

//welcome message
uart_puts (PROGNAME);
uart_puts (": Welcome!\n");
uart_puts (": connected!\n");

if(USE_DDR_SW){
uart_puts (PROGNAME);
Expand All @@ -41,12 +35,27 @@ int main() {
uart_puts(": Program to run from DDR\n");
}

if (uart_getc() == LOAD) {//load firmware
uart_loadfw(prog_start_addr);
// address to copy firmware to
char **prog_start_addr;
if (USE_DDR_SW==0 || (USE_DDR_SW==1 && RUN_DDR_SW==0))
*prog_start_addr = (char *) (1<<BOOTROM_ADDR_W);
else
*prog_start_addr = (char *) EXTRA_BASE;

int file_size = 0;
char *fw_name = "firmware.bin";
if (uart_getc() == FRX) {//load firmware
file_size = uart_recvfile(fw_name, prog_start_addr);
uart_puts (PROGNAME);
uart_puts (": Loading firmware...\n");
}

//sending firmware back for debug
if(file_size) {
uart_putc(FTX);
uart_sendfile("fw.bin", file_size, *prog_start_addr);
}

//run firmware
uart_puts (PROGNAME);
uart_puts (": Restart CPU to run user program...\n");
Expand Down
72 changes: 47 additions & 25 deletions software/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "console.h"

//to do: is this really a uart thing ?
//seems to be bootloader related and not uart related

#include "iob-uart-ascii.h"

#include "iob-uart.h"

#define PROGNAME "IOb-Console"

Expand All @@ -17,20 +12,30 @@ void cnsl_perror (char * mesg) {
exit(1);
}

//receive file name
void cnsl_recvstr(char *name) {
int i=0;
do name[i] = cnsl_getchar(); while (name[i++]);
printf(PROGNAME); printf(": file name %s\n", name);
}

//receive file from target
void cnsl_recvfile(char *name) {
void cnsl_recvfile() {
FILE *fp;
unsigned int file_size = 0;
int file_size = 0;
char *buf;
unsigned int i;
char name[80];
int i;

//receive file name
cnsl_recvstr(name);

//open data file
fp = fopen(name, "wb");
if (!fp)
cnsl_perror("can't open file to store received file\n");


printf(PROGNAME); printf(": receiving file...%s\n", name);
printf(PROGNAME); printf(": receiving file...\n");

//receive file size
file_size = cnsl_getint();
Expand All @@ -42,14 +47,11 @@ void cnsl_recvfile(char *name) {

//receive file into buffer
for (i=0; i<file_size; i++)
cnsl_getchar( &buf[i]);
buf[i] = cnsl_getchar();

//save buffer into file
if( fwrite(buf, sizeof(char), file_size, fp) <= 0)
cnsl_perror("failed to write file\n");

//DEBUG
//printf("buffer[%u] = %x\n", i, byte);

printf (PROGNAME); printf(": file of size %d bytes received\n", file_size);

Expand All @@ -59,16 +61,23 @@ void cnsl_recvfile(char *name) {
}

//send file to target
void cnsl_sendfile(char *name) {
void cnsl_sendfile() {
FILE *fp;
int file_size;
char *name;
char *buf;
int i;

//receive file name
name = malloc(80);
cnsl_recvstr(name);

printf(PROGNAME); printf(": file name: %s\n", name);

//open file to sent
fp = fopen(name, "rb");
if (!fp)
cnsl_perror("can't open file to send%s\n");
cnsl_perror("can't open file to send\n");

//get file size
fseek(fp, 0L, SEEK_END);
Expand All @@ -79,7 +88,7 @@ void cnsl_sendfile(char *name) {
if( (buf = malloc(file_size)) == NULL)
cnsl_perror("memory allocation failed\n");

printf(PROGNAME); printf(": sending file of size %d bytes...\n", file_size);
printf(PROGNAME); printf(": file size: %d bytes\n", file_size);

//send file size
cnsl_putint(file_size);
Expand All @@ -100,6 +109,7 @@ void cnsl_sendfile(char *name) {

void usage(char *message){
cnsl_perror("usage: ./console -s <serial port> [ -f <firmware file> ]\n");
cnsl_perror(message);
}

//
Expand All @@ -109,8 +119,8 @@ void usage(char *message){
int main(int argc, char* argv[]) {

char *devname = 0;
char *fwFile = 0;
int i;
int load_fw = 0;

if (argc < 3)
usage("PROGNAME: not enough program arguments\n");
Expand All @@ -120,18 +130,20 @@ int main(int argc, char* argv[]) {
if (argv[i][1] == 's') {
devname = argv[++i];
} else if (argv[i][1] == 'f') {
fwFile = "firmware.bin";
load_fw = 1;
} else usage("PROGNAME: unexpected argument\n");
} else usage("PROGNAME: unexpected argument\n");
}

//open serial port
//open connection
cnsl_open(devname);

//server loop
char byte;
int gotENQ = 0;

printf(PROGNAME); printf(": connecting");

while (1) {

//get byte from target
Expand All @@ -141,12 +153,12 @@ int main(int argc, char* argv[]) {
switch (byte) {

case ENQ:
printf(".");
if(!gotENQ) {
gotENQ = 1;
if(fwFile) {
if(load_fw)
cnsl_putchar(FRX);
cnsl_sendfile(fwFile);
} else
else
cnsl_putchar(ACK);
}
break;
Expand All @@ -156,10 +168,20 @@ int main(int argc, char* argv[]) {
exit(0);
break;

case FRX:
printf(PROGNAME); printf(": sending file\n");
cnsl_sendfile();
break;

case FTX:
printf(PROGNAME); printf(": receiving file\n");
cnsl_recvfile();
break;

default:
printf("%c", byte);
fflush(stdout);

}

}
Expand Down
3 changes: 1 addition & 2 deletions software/firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ $(foreach p, $(PERIPHERALS), $(eval include $(SUBMODULES_DIR)/$p/software/embedd
HDR+=periphs.h

#SOURCES
SRC+= firmware.S firmware.c $(UART_DIR)/software/uart_io.c
SRC+= $(UART_DIR)/software/printf.c
SRC+= firmware.S firmware.c $(UART_DIR)/software/printf.c

#RULES
run: firmware.elf
Expand Down
2 changes: 1 addition & 1 deletion software/pc-emul/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $(foreach p, $(PERIPHERALS), $(eval include $(SUBMODULES_DIR)/$p/software/pc/pc.
HDR+=periphs.h

#SOURCES
SRC+= $(FIRM_DIR)/firmware.c $(UART_DIR)/software/printf.c $(UART_DIR)/software/uart_io.c
SRC+= $(FIRM_DIR)/firmware.c $(UART_DIR)/software/printf.c

#RULES
run: firmware.out
Expand Down
2 changes: 1 addition & 1 deletion system_config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN_DDR ?=0
DCACHE_ADDR_W:=24

#ROM SIZE (LOG2)
BOOTROM_ADDR_W:=11
BOOTROM_ADDR_W:=13

#PRE-INIT MEMORY WITH PROGRAM AND DATA
INIT_MEM ?=1
Expand Down

0 comments on commit 84db182

Please sign in to comment.