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

Add support for smart writes, control user area and emulator #2

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = gcc
CFLAGS += $(shell pkg-config libftdi1 --cflags)
CFLAGS += -g -Wall
CFLAGS += -g -Wall -std=c99
#CFLAGS += -O2 -s
LDFLAGS = $(shell pkg-config libftdi1 --libs)
INCLUDES =
Expand All @@ -10,8 +10,8 @@ TARGET = nrf24le1_flasher
SRCS = \
src/hexfile.c \
src/main.c \
src/spi_ft232r.c

src/spi_ft232r.c
# src/spi_emulator.c
HEADERS = \
src/hexfile.h \
src/spi.h
Expand Down
18 changes: 10 additions & 8 deletions src/hexfile.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "hexfile.h"

Expand All @@ -17,13 +18,14 @@ static uint8_t checksum(const char *line, int size)
return 0x100 - res;
}

int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n)
int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n, bool address_as_offset, uint16_t address_offset)
{
char line[522];
uint8_t count = 0, type = 0, cksum = 0;
int r, i;

fgets(line, sizeof(line), fd);
if (fgets(line, sizeof(line), fd) == NULL)
return 0;

if (line[0] != ':') {
fprintf(stderr, "line can't be parsed: %s\n", line);
Expand All @@ -35,10 +37,11 @@ int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n)
fprintf(stderr, "line parse error: %s\n", line);
return -2;
}

if (count > n) {
fprintf(stderr, "internal buffer too small to handle: %s\n",
line);
*address += address_offset;
uint16_t offset = address_as_offset ? *address : 0;
if (offset + count > n) {
fprintf(stderr, "internal buffer too small to handle (%d > %zu): %s\n",
offset + count, n, line);
return -3;
}

Expand All @@ -65,7 +68,7 @@ int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n)
return -6;
}

dest[i] = b;
dest[offset + i] = b;
}
return count;
}
Expand All @@ -77,4 +80,3 @@ int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n)

return 0;
}

2 changes: 1 addition & 1 deletion src/hexfile.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef HEXFILE_H
#define HEXFILE_H

int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n);
int hexfile_getline(FILE *fd, uint16_t *address, uint8_t *dest, size_t n, bool address_as_offset, uint16_t address_offset);

#endif // HEXFILE_H

Loading