Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Commit

Permalink
update tool, progress bar, colored output
Browse files Browse the repository at this point in the history
  • Loading branch information
Ced2911 committed Jun 16, 2016
1 parent 7a534a6 commit 2774749
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 66 deletions.
2 changes: 2 additions & 0 deletions tool/ccrwrom.vcxproj
Expand Up @@ -26,6 +26,7 @@
<ClCompile Include="src\debug.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\romdb.cpp" />
<ClCompile Include="src\rom_tools.cpp" />
<ClCompile Include="src\test.cpp" />
<ClCompile Include="src\usb.cpp" />
</ItemGroup>
Expand All @@ -35,6 +36,7 @@
<ClInclude Include="src\Command.h" />
<ClInclude Include="src\debug.h" />
<ClInclude Include="src\romdb.h" />
<ClInclude Include="src\rom_tools.h" />
<ClInclude Include="src\usb.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
Expand Down
6 changes: 6 additions & 0 deletions tool/ccrwrom.vcxproj.filters
Expand Up @@ -42,6 +42,9 @@
<ClCompile Include="src\test.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="src\rom_tools.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="libs\hidapi\hidapi\hidapi.h">
Expand All @@ -62,5 +65,8 @@
<ClInclude Include="src\debug.h">
<Filter>Fichiers sources</Filter>
</ClInclude>
<ClInclude Include="src\rom_tools.h">
<Filter>Fichiers sources</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion tool/ccrwrom.vcxproj.user
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>-b</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>-r pp -m1</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
28 changes: 16 additions & 12 deletions tool/src/Command.cpp
Expand Up @@ -5,7 +5,6 @@
#ifdef WIN32
#include <Windows.h>
#endif

#include "Command.h"
#include "usb.h"
#include "debug.h"
Expand All @@ -19,21 +18,24 @@ void cmd_bootloader() {

uint32_t cmd_read_rom(uint8_t * out, uint32_t size) {
uint8_t * p = out;
cmd_position.reset();

cclock elapsed;
usb.cmd(PROM_BULK_READ, 0, size);

for (uint32_t i = 0; i < size; i += USB_PACKET_SIZE, p += USB_PACKET_SIZE) {
if (usb.read(p, sizeof(usb_packet_w_t)) == 0) {
// Error !
puts("Usb error\r\n");
output::error("Usb error\r\n");
}
#ifdef _DEBUG
if (i == 0) {
hexdump(p, USB_PACKET_SIZE);
}
#endif

cmd_position.add(USB_PACKET_SIZE);
}

cout << " Time elapsed: " << elapsed.elapsed() << " ms" << endl;
return 0;
}

Expand All @@ -42,8 +44,7 @@ uint32_t cmd_write_rom(uint8_t * in, uint32_t size) {
usb_packet_w_t read = { 0 };
uint32_t rem = size;
uint32_t w_address = 0;

cclock elapsed;
cmd_position.reset();

usb.cmd(JEDEC_WRITE, 0, size);

Expand All @@ -55,17 +56,16 @@ uint32_t cmd_write_rom(uint8_t * in, uint32_t size) {

if (usb.read(read, sizeof(usb_packet_w_t)) == 0) {
// Error !
puts("Usb error\r\n");
output::error("Usb error\r\n");
}

rem -= w_size;
w_address += w_size;
p += w_size;
} while (w_address < size);


cout << " Time elapsed: " << elapsed.elapsed() << " ms" << endl;
cmd_position.add(w_size);

} while (w_address < size);

return 0;
}

Expand Down Expand Up @@ -94,4 +94,8 @@ void cmd_erase() {
void cmd_query(uint8_t * in) {
usb.cmd(QUERY_DEVICE);
usb.read(in, sizeof(usb_packet_w_t));
}
}



atomic_value<uint32_t> cmd_position;
33 changes: 32 additions & 1 deletion tool/src/Command.h
Expand Up @@ -2,6 +2,34 @@

#include "hidapi.h"
#include "ccrwrom.h"
#include <atomic>

template <typename T> struct atomic_value {
std::atomic<T> value;

atomic_value() : value(0) {}

void increment() {
++value;
}

void decrement() {
--value;
}

void add(T a) {
value += a;
}

void reset() {
value = 0;
}

T get() {
return value.load();
}
};


//Bootloader Command From Host - Switch() State Variable Choices
#define QUERY_DEVICE 0x02 //Command that the host uses to learn about the device (what regions can be programmed, and what type of memory is the region)
Expand Down Expand Up @@ -37,4 +65,7 @@ uint32_t cmd_write_rom(uint8_t * in, uint32_t size);
void cmd_erase();
void cmd_read_id(uint8_t * out);

void cmd_query(uint8_t * in);
void cmd_query(uint8_t * in);


extern atomic_value<uint32_t> cmd_position;
9 changes: 8 additions & 1 deletion tool/src/ccrwrom.h
Expand Up @@ -15,7 +15,14 @@
#define ARRAY_SIZE(array) \
(sizeof(array) / sizeof(array[0]))


namespace output {
void error(const char * str, ...);
void warning(const char * str, ...);
void debug(const char * str, ...);
void info(const char * str, ...);
void success(const char * str, ...);
void print(const char * str, ...);
}



Expand Down
101 changes: 101 additions & 0 deletions tool/src/debug.cpp
Expand Up @@ -3,7 +3,21 @@
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdarg.h>
#include "debug.h"
#ifdef WIN32
#include <windows.h>
#endif

#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"

int verbose_flag = VERBOSE_LEVEL;

/**
* Credit: http://stahlworks.com/dev/index.php?tool=csc01
Expand Down Expand Up @@ -61,4 +75,91 @@ void hexdump(void *pAddressIn, long lSize)
buf.pData += lOutLen;
buf.lSize -= lOutLen;
}
}

#ifndef WIN32
#define outcolor(color) \
{ \
char buffer[256]; \
va_list args; \
va_start(args, str); \
fprintf(stdout,color); \
vsprintf(buffer, str, args); \
fprintf(stdout, buffer); \
fprintf(stdout,ANSI_COLOR_RESET); \
va_end(args); \
}
#else
#define outcolor(color) \
{ \
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO consoleInfo; WORD saved_attributes; \
char buffer[256]; \
va_list args; \
GetConsoleScreenBufferInfo(hConsole, &consoleInfo); saved_attributes = consoleInfo.wAttributes; \
SetConsoleTextAttribute(hConsole, color); \
va_start(args, str); \
vsprintf(buffer, str, args); \
fprintf(stdout, buffer); \
va_end(args); \
SetConsoleTextAttribute(hConsole, saved_attributes); \
}

#endif

namespace output {
void error(const char * str, ...) {
outcolor(FOREGROUND_RED | FOREGROUND_INTENSITY)
}

void success(const char * str, ...) {
outcolor(FOREGROUND_BLUE | FOREGROUND_INTENSITY)
}

void warning(const char * str, ...) {
if (verbose_flag)
outcolor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
}

void debug(const char * str, ...) {
if (verbose_flag)
outcolor(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
}

void info(const char * str, ...) {
if (verbose_flag)
outcolor(FOREGROUND_BLUE| FOREGROUND_INTENSITY | FOREGROUND_GREEN)
}

void print(const char * str, ...) {
char buffer[256];
va_list args;
va_start(args, str);
vsprintf(buffer, str, args);
fprintf(stdout, buffer);
va_end(args);
}

void reset() {
//puts(ANSI_COLOR_RESET);
}
}


int dbg_compare_buf(uint8_t * in, uint8_t * out, int size) {
for (int i = 0; i < size; i++) {
if (in[i] != out[i]) {

output::error("error at %08x %d\r\n", i, i);

output::error("read\r\n");
hexdump(&in[i], 0x40);

output::error("source\r\n");
hexdump(&out[i], 0x40);
return -1;
}
}

output::error("test ok\r\n");
return 0;
}
12 changes: 11 additions & 1 deletion tool/src/debug.h
@@ -1,2 +1,12 @@
#pragma once
void hexdump(void *pAddressIn, long lSize);

#ifdef _DEBUG
#define VERBOSE_LEVEL 0xF
#else
#define VERBOSE_LEVEL 0
#endif


void hexdump(void *pAddressIn, long lSize);
int dbg_compare_buf(uint8_t * in, uint8_t * out, int size);
extern int verbose_flag ;

0 comments on commit 2774749

Please sign in to comment.