Skip to content

Instantly share code, notes, and snippets.

@dmitrodem
Created February 10, 2021 15:38
Show Gist options
  • Save dmitrodem/8cdb962a493c98a8c5b93623f6d16ff1 to your computer and use it in GitHub Desktop.
Save dmitrodem/8cdb962a493c98a8c5b93623f6d16ff1 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#define BUF_SIZE 1024
int readn (int serial_port, uint8_t *buf, size_t nbytes) {
uint8_t rxbuf[BUF_SIZE];
uint8_t *p = buf;
int rc;
size_t i = 0, j = 0;
int escape = 0;
do {
rc = read(serial_port, rxbuf, BUF_SIZE);
if (rc == -1) {
exit(EXIT_FAILURE);
}
for (i = 0; i < rc; i++) {
if (rxbuf[i] == 0x3a) {
escape = 1;
} else {
if (escape == 1) {
escape = 0;
if (rxbuf[i] == 0x3b) {
buf[j++] = 0x3a;
} else {
buf[j++] = rxbuf[i];
}
} else {
buf[j++] = rxbuf[i];
}
}
}
} while (j < nbytes);
return 0;
}
int writen(int serial_port, uint8_t *buf, size_t nbytes) {
uint8_t tbuf[BUF_SIZE];
int rc;
size_t i = 0, j = 0;
tbuf[j++] = 0x3a;
tbuf[j++] = 0x3c;
for (i = 0; i < nbytes; i++) {
tbuf[j++] = buf[i];
if (buf[i] == 0x3a) {
tbuf[j++] = 0x3b;
}
}
uint8_t *p = tbuf;
do {
rc = write(serial_port, p, j);
if (rc == -1) {
perror("");
exit(EXIT_FAILURE);
}
p += rc;
j -= rc;
} while (j > 0);
return 0;
}
int main(int argc, char **argv) {
int rc;
int serial_port = open(argv[1], O_RDWR);
if (serial_port == -1) {
perror("");
exit(EXIT_FAILURE);
}
struct termios tp;
if (tcgetattr(serial_port, &tp) != 0) {
perror("");
exit(EXIT_FAILURE);
}
cfmakeraw(&tp);
cfsetspeed(&tp, B2500000);
rc = tcsetattr(serial_port, TCSANOW, &tp);
uint8_t rbuf[BUF_SIZE];
uint8_t tbuf[BUF_SIZE];
size_t i = 0, j = 0;
/* 1. cmd = 0xff */
tbuf[j++] = 0xab; /* src */
tbuf[j++] = 0x00; /* dst */
tbuf[j++] = 0x1c; /* flags */
tbuf[j++] = 0xff; /* cmd */
tbuf[j++] = 0xdb;
tbuf[j++] = 0xcf;
tbuf[j++] = 0xc0;
tbuf[j++] = 0xde;
tbuf[j++] = 0x27;
tbuf[j++] = 0x18;
tbuf[j++] = 0x28;
tbuf[j++] = 0x18;
writen(serial_port, tbuf, 12);
readn(serial_port, rbuf, 15);
/* 1. cmd = 0xfe */
tbuf[3] = 0xfe; /* cmd */
tbuf[4] = rbuf[1+4];
tbuf[5] = rbuf[1+5];
tbuf[6] = rbuf[1+6];
tbuf[7] = rbuf[1+7];
tbuf[8] = rbuf[1+8];
tbuf[9] = rbuf[1+9];
tbuf[10] = (rbuf[1+10] & 0xf0) | 0x7;
tbuf[11] = 0x70;
writen(serial_port, tbuf, 12);
readn(serial_port, rbuf, 15);
time_t start, stop;
start = time(NULL);
size_t memptr;
for (memptr = 0; memptr < 256*1024; memptr += 4) {
/* cmd = 0xfc */
uint32_t a = 0xa0000000 + memptr;
uint32_t d;
tbuf[1] = 0x77;
tbuf[3] = 0xfc; /* cmd */
tbuf[4] = (a >> 24) & 0xff;
tbuf[5] = (a >> 16) & 0xff;
tbuf[6] = (a >> 8) & 0xff;
tbuf[7] = (a >> 0) & 0xff;
tbuf[8] = 0;
tbuf[9] = 0;
tbuf[10] = 0;
tbuf[11] = 0;
writen(serial_port, tbuf, 12);
readn(serial_port, rbuf, 15);
d = (rbuf[9] << 24) | (rbuf[10] << 16) | (rbuf[11] << 8) | rbuf[12];
if (0) {
printf("%08x = %08x\n", a, d);
}
}
stop = time(NULL);
printf("Total time = %ld s\n", stop - start);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment