summaryrefslogtreecommitdiffstats
path: root/stepper_driver/m8driver/firmware/tabgen.c
blob: 8ac2ca32e96bafaf632c7604379fd7a6c1c74cbb (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *   Atmel Mega8 based LMD18245 controller
 *   Lookup table generator
 *
 *   Copyright (c) 2009-2020 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#undef M_PI
#define M_PI		3.14159265358979323846264338327


#define COMPILE_YEAR ((((__DATE__[7] - '0') * 10 +				\
			(__DATE__[8] - '0')) * 10 +				\
			(__DATE__[9] - '0')) * 10 +				\
			(__DATE__[10] - '0'))

#define COMPILE_MONTH ((__DATE__[2] == 'n' ? (__DATE__[1] == 'a' ? 0 : 5)	\
			: __DATE__[2] == 'b' ? 1				\
			: __DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? 2 : 3)	\
			: __DATE__[2] == 'y' ? 4				\
			: __DATE__[2] == 'l' ? 6				\
			: __DATE__[2] == 'g' ? 7				\
			: __DATE__[2] == 'p' ? 8				\
			: __DATE__[2] == 't' ? 9				\
			: __DATE__[2] == 'v' ? 10 : 11) + 1)

#define COMPILE_DAY ((__DATE__[4] == ' ' ? 0 : __DATE__[4] - '0') * 10 +	\
		     (__DATE__[5] - '0'))


static int gen_lmd_tab(unsigned int nr_steps)
{
	double pos;
	double s;
	unsigned int lmd1, lmd2;
	unsigned int count;
	unsigned char *buf;

	buf = malloc(nr_steps * 2);
	if (!buf) {
		fprintf(stderr, "Out of memory.\n");
		return 1;
	}

	/* Generate the lookup table. */
	/* sin() takes radians as arg. pi rad == 180deg */
	for (count = 0; count < nr_steps * 2; count++) {
		pos = (M_PI * (double)count) / ((double)nr_steps * 2.0);

		s = fabs(sin(pos)) * (double)0xF;
		lmd1 = (unsigned int)round(s) & 0xFu;

		s = fabs(cos(pos)) * (double)0xF;
		lmd2 = (unsigned int)round(s) & 0xFu;

		buf[count] = lmd1 | (lmd2 << 4);
	}

	/* Print the lookup table. */
	printf("steptable:\n");
	for (count = 0; count < nr_steps * 2; count++) {
		if (count % 8 == 0)
			printf("%s.db 0x%02X", count ? "\n" : "", buf[count]);
		else
			printf(", 0x%02X", buf[count]);
	}
	printf("\n\n");

	free(buf);

	return 0;
}

int main(int argc, char **argv)
{
	unsigned long nr_steps;
	int err;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s NR_OF_STEPS\n", argv[0]);
		return 1;
	}
	nr_steps = strtoul(argv[1], NULL, 10);
	if (nr_steps < 2 || nr_steps > 60) {
		fprintf(stderr, "Please select a step count between 2 and 60\n");
		return 1;
	}

	printf("; THIS FILE IS GENERATED. DO NOT EDIT.\n");
	printf("\n\n");
	printf(".equ NR_STEPS=%lu\n", nr_steps);
	printf("\n");
	printf(".cseg\n");

	err = gen_lmd_tab(nr_steps);
	if (err)
		return 1;

	printf("; For reference store the build parameters in the binary image\n");
	printf(".cseg\n");
	printf(".db 0,0,0,0\n");
	printf(".db \"NR_STEPS=%lu\"%s\n", nr_steps, ((nr_steps > 9) ? ",0" : ""));
	printf(".db 0,0,0,0\n");
	printf(".db \"BUILD_DATE=%04u.%02u.%02u\",0\n",
	       (unsigned int)COMPILE_YEAR,
	       (unsigned int)COMPILE_MONTH,
	       (unsigned int)COMPILE_DAY);
	printf("\n");

	return 0;
}
bues.ch cgit interface