-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoKeysLibAdvancedRTAsync.c
More file actions
276 lines (235 loc) · 9.01 KB
/
PoKeysLibAdvancedRTAsync.c
File metadata and controls
276 lines (235 loc) · 9.01 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* PoKeysLibAdvancedRTAsync.c
*
* Advanced RT-compatible async functions for LinuxCNC applications.
* These functions provide enhanced capabilities for real-time CNC control
* that aren't covered by the standard async implementations.
*/
#include "PoKeysLibHal.h"
#include "PoKeysLibAsync.h"
#include <string.h>
/* Advanced Digital IO Functions ------------------------------------------------ */
/**
* @brief Set multiple digital outputs in a single efficient operation (async)
*
* Updates only specified pins without affecting others. More efficient than
* updating all pins when only a few need changes.
*
* @param device Target device handle
* @param pinList Array of pin numbers to update
* @param valueList Array of values corresponding to pins
* @param count Number of pins to update
* @return PK_OK on success, error code on failure
*/
int PK_DigitalOutputSetMultipleAsync(sPoKeysDevice* device, const uint8_t* pinList,
const uint8_t* valueList, uint8_t count) {
if (!device || !pinList || !valueList) return PK_ERR_GENERIC;
if (count == 0 || count > 32) return PK_ERR_GENERIC;
// Update the specified pins in device structure
for (uint8_t i = 0; i < count; i++) {
uint8_t pin = pinList[i];
if (pin < device->info.iPinCount) {
*(device->Pins[pin].DigitalValueSet.out) = valueList[i] ? 1 : 0;
}
}
// Use standard set/get operation to commit changes
return PK_DigitalIOSetGetAsync(device);
}
/**
* @brief Get digital counter values for specific pins only (async)
*
* More efficient than reading all counters when only specific ones are needed.
*
* @param device Target device handle
* @param pinList Array of pin numbers to read
* @param count Number of pins to read
* @return PK_OK on success, error code on failure
*/
int PK_DigitalCounterGetSelectedAsync(sPoKeysDevice* device, const uint8_t* pinList, uint8_t count) {
if (!device || !pinList) return PK_ERR_GENERIC;
if (count == 0 || count > 13) return PK_ERR_GENERIC;
if (device->info.iDigitalCounters == 0) return PK_ERR_NOT_SUPPORTED;
// Verify all pins have counter capability
for (uint8_t i = 0; i < count; i++) {
if (!PK_IsCounterAvailable(device, pinList[i])) {
return PK_ERR_GENERIC;
}
}
return CreateAndSendRequestAsync(device, PK_CMD_DIGITAL_COUNTERS_VALUES,
NULL, 0, (void*)pinList, count, PK_DigitalCounterParse);
}
/* Advanced PWM Functions ------------------------------------------------------ */
/**
* @brief Set PWM duty cycles for selected channels only (async)
*
* Updates only specified PWM channels without affecting others.
* Efficient for applications that don't use all 6 PWM channels.
*
* @param device Target device handle
* @param channelMask Bitmask of channels to update (bit 0 = channel 0, etc.)
* @param dutyCycles Array of duty cycle values for enabled channels
* @return PK_OK on success, error code on failure
*/
int PK_PWMUpdateSelectedChannelsAsync(sPoKeysDevice* device, uint8_t channelMask, const uint32_t* dutyCycles) {
if (!device || !dutyCycles) return PK_ERR_GENERIC;
uint8_t payload[37] = {0};
payload[0] = channelMask;
uint8_t dutyIndex = 0;
for (uint32_t n = 0; n < 6; n++) {
if (channelMask & (1 << n)) {
uint32_t duty = dutyCycles[dutyIndex++];
payload[1 + n * 4] = (uint8_t)(duty & 0xFF);
payload[2 + n * 4] = (uint8_t)((duty >> 8) & 0xFF);
payload[3 + n * 4] = (uint8_t)((duty >> 16) & 0xFF);
payload[4 + n * 4] = (uint8_t)((duty >> 24) & 0xFF);
// Update device structure
*(device->PWM.PWMduty[n]) = duty;
} else {
// Keep current duty cycle for unchanged channels
uint32_t duty = (uint32_t)*(device->PWM.PWMduty[n]);
payload[1 + n * 4] = (uint8_t)(duty & 0xFF);
payload[2 + n * 4] = (uint8_t)((duty >> 8) & 0xFF);
payload[3 + n * 4] = (uint8_t)((duty >> 16) & 0xFF);
payload[4 + n * 4] = (uint8_t)((duty >> 24) & 0xFF);
}
}
// Use current period
uint32_t period = device->PWM.PWMperiod;
payload[33] = (uint8_t)(period & 0xFF);
payload[34] = (uint8_t)((period >> 8) & 0xFF);
payload[35] = (uint8_t)((period >> 16) & 0xFF);
payload[36] = (uint8_t)((period >> 24) & 0xFF);
return CreateAndSendRequestAsync(device, PK_CMD_PWM_CONFIGURATION,
(const uint8_t[]){1, 1}, 2, payload, sizeof(payload), NULL);
}
/* System Monitoring Functions ------------------------------------------------ */
/**
* @brief Get comprehensive system status in one operation (async)
*
* Combines device status, load monitoring, and connection health into
* a single efficient async operation for system monitoring.
*
* @param device Target device handle
* @return PK_OK on success, error code on failure
*/
int PK_SystemStatusGetAllAsync(sPoKeysDevice* device) {
if (!device) return PK_ERR_NOT_CONNECTED;
// Queue multiple status requests efficiently
int ret;
// 1. Device alive check
ret = PK_DeviceAliveCheckAsync(device);
if (ret != PK_OK) return ret;
// 2. Load status (if supported)
if (device->info.iLoadStatus) {
ret = PK_DeviceLoadStatusAsync(device);
if (ret != PK_OK) return ret;
}
// 3. Error status
ret = PK_DeviceErrorStatusAsync(device);
if (ret != PK_OK) return ret;
// 4. Basic device data
ret = PK_DeviceDataGetAsync(device);
if (ret != PK_OK) return ret;
return PK_OK;
}
/* RT-Safe Batch Operations --------------------------------------------------- */
/**
* @brief Perform complete IO update cycle (async)
*
* Combines all common IO operations into a single efficient batch:
* - Digital IO set/get
* - Analog input reading
* - PWM updates
* - Counter reading
*
* @param device Target device handle
* @return PK_OK on success, error code on failure
*/
int PK_IOUpdateCycleAsync(sPoKeysDevice* device) {
if (!device) return PK_ERR_NOT_CONNECTED;
int ret;
// 1. Digital IO update (set outputs, read inputs)
ret = PK_DigitalIOSetGetAsync(device);
if (ret != PK_OK) return ret;
// 2. Analog inputs (if supported)
if (device->info.iAnalogInputs) {
ret = PK_AnalogIOGetAsync(device);
if (ret != PK_OK) return ret;
}
// 3. PWM updates (if any channels enabled)
int pwmActive = 0;
for (int i = 0; i < 6; i++) {
if (device->PWM.PWMenabledChannels[i]) {
pwmActive = 1;
break;
}
}
if (pwmActive) {
ret = PK_PWMUpdateAsync(device);
if (ret != PK_OK) return ret;
}
// 4. Digital counters (if supported and any available)
if (device->info.iDigitalCounters) {
ret = PK_DigitalCounterGetAsync(device);
if (ret != PK_OK) return ret;
}
// 5. Encoder values
ret = PK_EncoderValuesGetAsync(device);
if (ret != PK_OK) return ret;
return PK_OK;
}
/**
* @brief Emergency stop all motion and outputs (async)
*
* Immediately stops all motion, disables PWM outputs, and sets
* all digital outputs to safe state. Critical for emergency situations.
*
* @param device Target device handle
* @return PK_OK on success, error code on failure
*/
int PK_EmergencyStopAllAsync(sPoKeysDevice* device) {
if (!device) return PK_ERR_NOT_CONNECTED;
int ret;
// 1. Stop PulseEngine v2 if available
if (device->info.iPulseEnginev2) {
device->PEv2.PulseEngineState = 0; // Stop engine
ret = PK_PEv2_StatusGetAsync(device); // This will apply the state change
if (ret != PK_OK) return ret;
}
// 2. Disable all PWM outputs
for (int i = 0; i < 6; i++) {
device->PWM.PWMenabledChannels[i] = 0;
*(device->PWM.PWMduty[i]) = 0;
}
ret = PK_PWMUpdateAsync(device);
if (ret != PK_OK) return ret;
// 3. Set all digital outputs to safe state (low)
for (uint32_t i = 0; i < device->info.iPinCount; i++) {
if ((device->Pins[i].PinFunction & PK_PinCap_digitalOutput)) {
*(device->Pins[i].DigitalValueSet.out) = 0;
}
}
ret = PK_DigitalIOSetGetAsync(device);
if (ret != PK_OK) return ret;
return PK_OK;
}
/**
* @brief Test device communication and RT timing (async)
*
* Performs communication test by sending ping commands and measuring
* response times. Useful for RT system validation.
*
* @param device Target device handle
* @param testCount Number of ping tests to perform
* @return PK_OK on success, error code on failure
*/
int PK_CommunicationTestAsync(sPoKeysDevice* device, uint8_t testCount) {
if (!device) return PK_ERR_NOT_CONNECTED;
if (testCount == 0 || testCount > 10) return PK_ERR_GENERIC;
// Perform multiple alive checks to test communication
for (uint8_t i = 0; i < testCount; i++) {
int ret = PK_DeviceAliveCheckAsync(device);
if (ret != PK_OK) return ret;
}
return PK_OK;
}