Add CRC16 with reverse bit order
This commit is contained in:
parent
5c3d6461a1
commit
0d6cba4530
5 changed files with 450 additions and 4 deletions
73
metawatch.c
73
metawatch.c
|
@ -7,23 +7,90 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "metawatch_protocol.h"
|
||||
#include "crc16ccitt.h"
|
||||
|
||||
|
||||
void dump_frame(unsigned char *frame, int len)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<len; i++)
|
||||
fprintf(stderr, "0x%02x ", frame[i]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
|
||||
void mw_send_packet(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
|
||||
{
|
||||
unsigned short crc;
|
||||
unsigned char frame[64];
|
||||
|
||||
memset(frame, 0, 64);
|
||||
frame[0] = MW_SOF;
|
||||
frame[1] = len + 6;
|
||||
frame[2] = msg_type;
|
||||
frame[3] = options;
|
||||
memcpy(frame+4, data, len);
|
||||
|
||||
crc = crc16ccitt(frame, len+4);
|
||||
*(unsigned short *)(frame+len+4) = crc;
|
||||
|
||||
dump_frame(frame, len+6);
|
||||
|
||||
write(mw_fd, frame, len+6);
|
||||
}
|
||||
|
||||
void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
|
||||
{
|
||||
time_t mtime;
|
||||
struct tm mtm;
|
||||
unsigned short year;
|
||||
unsigned char data[32];
|
||||
|
||||
mtime = time(NULL);
|
||||
localtime_r(&mtime, &mtm);
|
||||
|
||||
year = mtm.tm_year + 1900;
|
||||
data[0] = (year & 0x0f00) >> 8;
|
||||
data[1] = (year & 0x00ff);
|
||||
data[2] = mtm.tm_mon + 1;
|
||||
data[3] = mtm.tm_mday;
|
||||
data[4] = mtm.tm_wday;
|
||||
data[5] = mtm.tm_hour;
|
||||
data[6] = mtm.tm_min;
|
||||
data[7] = mtm.tm_sec;
|
||||
data[8] = clk1224;
|
||||
data[9] = date_fmt;
|
||||
|
||||
mw_send_packet(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int mwfd;
|
||||
int mw_fd;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
|
||||
return 1;
|
||||
};
|
||||
|
||||
mwfd = open(argv[1], O_RDWR);
|
||||
if (mwfd < 0) {
|
||||
crc16ccitt_init();
|
||||
|
||||
mw_fd = open(argv[1], O_RDWR);
|
||||
if (mw_fd < 0) {
|
||||
perror("open");
|
||||
return 1;
|
||||
};
|
||||
|
||||
mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
|
||||
|
||||
fsync(mw_fd);
|
||||
|
||||
close(mw_fd);
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue