Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ microcontrollers:

- Atmel SAME70/S70/V70/V71

Communication with SAM-BA bootloader is only supported through USB.
Communication with SAM-BA bootloader is possible over
- USB
- RS232 (native)

Before using the tool, the MCU must be put in "Boot from ROM" mode, either by
fully erasing it using the ERASE pin or by clearing GPVNM1 (see datasheet for
Expand Down
34 changes: 28 additions & 6 deletions comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ static bool configure_tty(int fd, int speed)
tty.c_cflag |= CS8 | CLOCAL | CREAD;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 20;
tty.c_iflag &= ~(ICRNL | IGNBRK | IXON | IXOFF | IXANY);

if (tcsetattr(fd, TCSANOW, &tty) != 0) {
Expand All @@ -57,8 +57,10 @@ static bool configure_tty(int fd, int speed)
static bool switch_to_binary(int fd)
{
char cmd[] = "N#";
printf("start switch to binary\n");
if (write(fd, cmd, strlen(cmd)) != strlen(cmd))
return false;
printf("write done\n");
return read(fd, cmd, 2) == 2;
}

Expand All @@ -69,17 +71,23 @@ int samba_open(const char* device)
perror("Could not open device");
return -1;
}
printf("port open\n");

if (!configure_tty(fd, B4000000)) {
printf("Configuring\n");
if (!configure_tty(fd,B115200)) {
perror("Could not configure");
close(fd);
return -1;
}

printf("switch to binary\n");
if (!switch_to_binary(fd)) {
perror("Could not set to binary");
close(fd);
return -1;
}

printf("finished port open\n");
return fd;
}

Expand Down Expand Up @@ -108,15 +116,29 @@ bool samba_read(int fd, uint8_t* buffer, uint32_t addr, uint32_t size)
{
char cmd[20];
while (size > 0) {
printf("reading addr %#x\n", addr);
uint32_t count = MIN(size, 1024);
printf("count is %d\n", count);
// workaround for bug when size is exactly 512
if (count == 512)
if (count == 512){
printf("apply workaround \n");
count = 1;
}
snprintf(cmd, sizeof(cmd), "R%08x,%08x#", addr, count);
if (write(fd, cmd, strlen(cmd)) != strlen(cmd))
printf("write-cmd is %s \n", cmd);
if (write(fd, cmd, strlen(cmd)) != strlen(cmd)){
printf("cmd-write failed\n");
return false;
if (read(fd, buffer, count) != count)
} else {
printf("cmd-write done\n");
}
printf("read-cmd start...\n");
if (read(fd, buffer, count) != count){
printf("cmd-read failed\n");
return false;
} else {
printf("cmd-read done\n");
}
addr += count;
buffer += count;
size -= count;
Expand Down
12 changes: 9 additions & 3 deletions eefc.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ bool eefc_erase_16pages(int fd, const struct _chip* chip,
bool eefc_read(int fd, const struct _chip* chip,
uint8_t* buffer, uint32_t addr, uint32_t size)
{
if (addr + size > chip->flash_size * 1024)
return false;

if (addr + size > chip->flash_size * 1024){
printf("invalid addr/size vs chip size parameter\n");
printf("addr/size is %#x\n", (addr + size));
printf("chip size is %#x\n", chip->flash_size * 1024);
printf("workaround active!!!\n");
// return false;
}
return samba_read(fd, buffer, chip->flash_addr + addr, size);
}

Expand All @@ -250,6 +254,8 @@ bool eefc_write(int fd, const struct _chip* chip,
uint32_t head = addr & (PAGE_SIZE - 1);
uint32_t count = MIN(size, PAGE_SIZE - head);

printf("writing addr %#.8x\n", addr);

// write to latch buffer
// we cannot use the SAM-BA Monitor send command because it
// does byte writes and the flash controller needs word writes
Expand Down
13 changes: 12 additions & 1 deletion usamba.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static bool read_flash(int fd, const struct _chip* chip, uint32_t addr, uint32_t
while (total < size) {
uint32_t count = MIN(BUFFER_SIZE, size - total);
if (!eefc_read(fd, chip, buffer, addr, count)) {
fprintf(stderr, "Error while reading from %#x\n", addr);
fclose(file);
return false;
}
Expand Down Expand Up @@ -261,9 +262,15 @@ int main(int argc, char *argv[])

printf("Port: %s\n", port);
fd = samba_open(port);
if (fd < 0)
if (fd < 0){
printf(">>>failed to open port\n");
fprintf(stderr, "Could not open port\n");
return -1;
} else {
printf(">>>port is open\n");
}

perror("reading chip id");
// Identify chip
const struct _chip* chip;
if (!chipid_identity_serie(fd, &chip)) {
Expand All @@ -284,6 +291,7 @@ int main(int argc, char *argv[])
switch (command) {
case CMD_READ:
{
printf("CMD: READ\n");
printf("Reading %d bytes at 0x%08x to file '%s'\n", size, addr, filename);
if (read_flash(fd, chip, addr, size, filename)) {
err = false;
Expand All @@ -293,6 +301,7 @@ int main(int argc, char *argv[])

case CMD_WRITE:
{
printf("CMD: WRITE\n");
if (get_file_size(filename, &size)) {
printf("Unlocking %d bytes at 0x%08x\n", size, addr);
if (eefc_unlock(fd, chip, &locks, addr, size)) {
Expand All @@ -307,6 +316,7 @@ int main(int argc, char *argv[])

case CMD_VERIFY:
{
printf("CMD: VERIFY\n");
if (get_file_size(filename, &size)) {
printf("Verifying %d bytes at 0x%08x with file '%s'\n", size, addr, filename);
if (verify_flash(fd, chip, filename, addr, size)) {
Expand All @@ -318,6 +328,7 @@ int main(int argc, char *argv[])

case CMD_ERASE_ALL:
{
printf("CMD: ERASE_ALL\n");
printf("Unlocking all pages\n");
if (eefc_unlock(fd, chip, &locks, 0, chip->flash_size * 1024)) {
printf("Erasing all pages\n");
Expand Down