-
Notifications
You must be signed in to change notification settings - Fork 23
/
simplemotion_private.h
169 lines (142 loc) · 6.4 KB
/
simplemotion_private.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//Internal functions & definitions, not for library user
//Copyright (c) Granite Devices Oy
#ifndef SIMPLEMOTION_PRIVATE_H
#define SIMPLEMOTION_PRIVATE_H
#include "simplemotion.h"
#include "busdevice.h"
#include "user_options.h"
#include <stdio.h>
#define SM_VERSION 0x020700
//bus device types
#define BUSDEV_NONE 0
#define BUSDEV_RS 1 /* rs232 like com port support */
#define BUSDEV_FTDI 2 /*not implemented yet: direct FTDI lib support*/
#define SM_BUSDEVICENAME_LEN 64
extern unsigned long SMBusBaudrate; //the next opened port (with smOpenBus) will be opened with the PBS defined here (default 460800 BPS)
//default timeout in ms
//Argon drive's worst case response time should be ~20ms with max length packets
#define SM_READ_TIMEOUT 500
extern const smuint8 table_crc16_hi[];
extern const smuint8 table_crc16_lo[];
extern const smuint8 table_crc8[];
extern FILE *smDebugOut; //such as stderr or file handle. if NULL, debug info disbled
extern smuint16 readTimeoutMs;
#define DEBUG_PRINT_RAW 0x524157
//smDebug: prints debug info to smDebugOut stream. If no handle available, set it to -1, or if wish to print as raw text, set handle to DEBUG_PRINT_RAW.
//set verbositylevel according to frequency of prints made.
//I.e SMDebugLow=low frequency, so it gets displayed when global verbosity level is set to at least Low or set it to Trace which gets filtered
//out if global verbisity level is set less than SMDebugTrace
#ifdef ENABLE_DEBUG_PRINTS
void smDebug( smbus handle, smVerbosityLevel verbositylevel, char *format, ...);
#else
#define smDebug(...) {}
#endif
//accumulates status to internal variable by ORing the bits. returns same value that is fed as paramter
SM_STATUS recordStatus( const smbus handle, const SM_STATUS stat );
SM_STATUS smRawCmd( const char *axisname, smuint8 cmd, smuint16 val, smuint32 *retdata );
/*Workaround to have packed structs that compile on GCC and MSVC*/
#ifdef __GNUC__
#define PACKED __attribute__ ((__packed__))
#else/*Assuming MSVC*/
#define PACKED
#pragma pack(push,1)
#endif
typedef struct {
/* ID=0 param size 30 bits (cmd total 4 bytes)
* ID=1 param size 22 bits (cmd total 3 bytes)
* ID=2 set parameter store address, param: 14 bits=register address (cmd total 2 bytes)
* ID=3 reserved
*/
long param :30; //LSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommand32;
typedef struct {
/* ID=0 param size 30 bits (cmd total 4 bytes)
* ID=1 param size 22 bits (cmd total 3 bytes)
* ID=2 set parameter store address, param: 14 bits=register address (cmd total 2 bytes)
* ID=3 reserved
*/
long param :14; //LSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommand16;
typedef struct {
/* ID=0 param size 30 bits (cmd total 4 bytes)
* ID=1 param size 22 bits (cmd total 3 bytes)
* ID=2 set parameter store address, param: 14 bits=register address (cmd total 2 bytes)
* ID=3 reserved
*/
long param :22; //MSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommand24;
//SM payload command return data structure
typedef struct {
/* ID=0 ret data 30 bits (tot 4 bytes)
* ID=1 ret data 22 bits (tot 3 bytes)
* ID=2 ret data 16 bits (tot 2 bytes), i.e. ACK/NACK.
* ID=3 reserved
*/
long retData: 30; //LSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet32;
//SM payload command return data structure
typedef struct {
/* ID=0 ret data 30 bits (tot 4 bytes)
* ID=1 ret data 22 bits (tot 3 bytes)
* ID=2 ret data 16 bits (tot 2 bytes), i.e. ACK/NACK.
* ID=3 reserved
*/
long retData: 22; //LSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet24;
//SM payload command return data structure
typedef struct {
/* ID=0 ret data 30 bits (tot 4 bytes)
* ID=1 ret data 22 bits (tot 3 bytes)
* ID=2 ret data 16 bits (tot 2 bytes), i.e. ACK/NACK.
* ID=3 reserved
*/
long retData: 14; //LSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet16;
//SM payload command return data structure
typedef struct {
/* ID=0 ret data 30 bits (tot 4 bytes)
* ID=1 ret data 22 bits (tot 3 bytes)
* ID=2 ret data 16 bits (tot 2 bytes), i.e. ACK/NACK.
* ID=3 reserved
*/
long retData: 6; //LSB 30 bits
unsigned long ID:2; //MSB 2 bits. when serailzied to bytestream byte4 must be transmitted first to contain ID
} PACKED SMPayloadCommandRet8;
typedef union
{
smuint8 U8[4];
smuint16 U16[2];
smuint32 U32;
} UnionOf4Bytes;
/*Workaround to have packed structs that compile on GCC and MSVC*/
#ifdef __GNUC__
#else/*Assuming MSVC*/
#pragma pack(pop)
#undef PACKED
#endif
/** Clear pending (stray) bytes in bus device reception buffer and reset receiver state. This may be needed i.e. after restarting device to eliminate clitches that appear in serial line.
-return value: a SM_STATUS value, i.e. SM_OK if command succeed
*/
LIB SM_STATUS smPurge( const smbus bushandle );
/** Block until pending TX bytes are phyiscally out. Max blocking time is same that is set with smSetTimeout
-return value: a SM_STATUS value, i.e. SM_OK if command succeed
*/
LIB SM_STATUS smFlushTX( const smbus bushandle );
/* OS independent sleep function for SM internal use
*
* SM lib has implementation for unix/win systems (incl linux & mac). For other systems, please add your own smSleepMs implementation in your application.
* i.e. write function in your main.c or any other .c file that get's compiled and linked:
*
* void smSleepMs(int millisecs)
* {
* // do your delay here
* }
*/
void smSleepMs(int millisecs);
#endif // SIMPLEMOTION_PRIVATE_H