Azq2 04 Feb 2013 include atfs using namespace std bool atfs_reset ComP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* (c) Azq2, 04 Feb 2013 */
#include "atfs.h"
using namespace std;
bool atfs_reset(ComPort *com) {
if (com -> writeString(ATFS_PREFIX "!\r") > 0)
return com -> readByte() == ATFS_ANSWER_OK;
return false;
}
bool atfs_exec(ComPort *com, const char *path, unsigned int *error, unsigned int *start, unsigned int *ret) {
char buff[24];
if (com -> writeStringFormatted(ATFS_PREFIXF "X%s\r", path) > 0)
if (com -> read(buff, 24) == 24 && sscanf(buff, "%08X%08X%08X", error, start, ret) == 3)
return true;
return false;
}
bool atfs_fopen(ComPort *com, const char *path, const char *mode) {
if (com -> writeStringFormatted(ATFS_PREFIXF "O%s,%s\r", mode, path) > 0)
return com -> readByte() == ATFS_ANSWER_OK;
return false;
}
bool atfs_fwrite(ComPort *com, unsigned char *buff, unsigned int offset, unsigned int size) {
int c = -1, errors_cnt = 0;
unsigned char *buffer_hex = new unsigned char[size * 2];
for (unsigned int i = 0, j = 0; i < size; i++) {
buffer_hex[j++] = _char2hex[(buff[i] >> 4) & 0xF];
buffer_hex[j++] = _char2hex[buff[i] & 0xF];
}
do {
if (c == ATFS_ANSWER_FATAL_ERROR || errors_cnt >= ATFS_WRITE_MAX_ERRORS)
return false;
com -> writeStringFormatted(ATFS_PREFIXF "W%08X%08X%08X", offset, size, crc32(buffer_hex, size * 2));
com -> write(buffer_hex, size * 2);
com -> write("\r", 1);
errors_cnt++;
} while ((c = com -> readByte()) == -1 || c == ATFS_ANSWER_ERROR);
delete[] buffer_hex;
return c == ATFS_ANSWER_OK;
}