-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoKeysLibPoNETHal.c
More file actions
252 lines (225 loc) · 9.86 KB
/
PoKeysLibPoNETHal.c
File metadata and controls
252 lines (225 loc) · 9.86 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
/**
* @file PoKeysLibPoNETHal.c
* @brief HAL interface functions for PoNET devices (including kbd48CNC)
*
* This file provides HAL pin export and management functions for PoNET devices.
* The kbd48CNC functionality uses the basic PoNET statusIn[] and statusOut[] arrays.
*
* ## Features:
* - Exports PoNET status arrays for direct access
* - Exports PoNET module control pins
* - Compatible with kbd48CNC devices via statusIn/statusOut mapping
* - Follows established pokeys_async naming conventions
*
* ## HAL Pin Naming Convention:
* - Status arrays: `pokeys_async.N.ponet.status-in.K` / `pokeys_async.N.ponet.status-out.K`
* - Module info: `pokeys_async.N.ponet.module-type` / `pokeys_async.N.ponet.module-id`
* - Control: `pokeys_async.N.ponet.pwm-duty` / `pokeys_async.N.ponet.light-value`
* - Status: `pokeys_async.N.ponet.status`
*
* ## kbd48CNC Usage:
* kbd48CNC devices use the statusIn[] array for button states and statusOut[] for LED states.
* External HAL components can map these arrays to specific kbd48CNC functions.
*
* @author zarfld
*/
#include "hal.h"
#include "PoKeysLibHal.h"
#include "PoKeysLibAsync.h"
/**
* @brief Update PoNET HAL pins from device data.
*
* This function copies data between the device PoNET structure and the HAL pins.
* Called during the update cycle to synchronize HAL with hardware state.
*
* @param device Pointer to sPoKeysDevice structure
*/
POKEYSDECL void update_ponet_hal_pins(sPoKeysDevice *device)
{
if (!device) return;
// Copy statusIn array from device to HAL (hardware to HAL)
for (int i = 0; i < 16; i++) {
if (device->PoNETmodule.statusIn_pins[i]) {
*(device->PoNETmodule.statusIn_pins[i]) = device->PoNETmodule.statusIn[i];
}
}
// Copy statusOut array from HAL to device (HAL to hardware)
for (int i = 0; i < 16; i++) {
if (device->PoNETmodule.statusOut_pins[i]) {
device->PoNETmodule.statusOut[i] = (uint8_t)(*(device->PoNETmodule.statusOut_pins[i]));
}
}
// Update single-value pins
if (device->PoNETmodule.PoNETstatus_pin) {
*(device->PoNETmodule.PoNETstatus_pin) = device->PoNETmodule.PoNETstatus;
}
if (device->PoNETmodule.moduleType_pin) {
*(device->PoNETmodule.moduleType_pin) = device->PoNETmodule.moduleType;
}
if (device->PoNETmodule.moduleID_pin) {
*(device->PoNETmodule.moduleID_pin) = device->PoNETmodule.moduleID;
}
if (device->PoNETmodule.lightValue_pin) {
*(device->PoNETmodule.lightValue_pin) = device->PoNETmodule.lightValue;
}
if (device->PoNETmodule.PWMduty_pin) {
device->PoNETmodule.PWMduty = (uint8_t)(*(device->PoNETmodule.PWMduty_pin));
}
}
/**
* @brief Export basic PoNET HAL pins for raw access.
*
* This function creates HAL pins for basic PoNET communication arrays.
* The kbd48CNC functionality can be implemented on top of these basic pins
* using HAL configuration or external mapping components.
*
* Each statusIn/statusOut byte becomes a separate u32 pin for HAL compatibility.
*
* @param prefix HAL component prefix (e.g., "pokeys_async.0")
* @param comp_id HAL component ID
* @param device Pointer to sPoKeysDevice structure
* @return 0 on success, negative HAL error code on failure
*/
POKEYSDECL int export_ponet_basic_pins(const char *prefix, long comp_id, sPoKeysDevice *device)
{
int r = 0;
if (!device || !prefix) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: Invalid parameters\n", __FILE__, __FUNCTION__);
return -1;
}
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.status-in [count 16]\n",
__FILE__, __FUNCTION__, prefix);
// Export PoNET status input array (16 bytes) - hardware to HAL
for (int byte_idx = 0; byte_idx < 16; byte_idx++) {
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.status-in.%02d\n",
__FILE__, __FUNCTION__, prefix, byte_idx);
r = hal_pin_u32_newf(HAL_OUT, &device->PoNETmodule.statusIn_pins[byte_idx],
comp_id, "%s.ponet.status-in.%02d", prefix, byte_idx);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"PoKeys: %s:%s: %s.ponet.status-in.%02d failed\n",
__FILE__, __FUNCTION__, prefix, byte_idx);
return r;
}
}
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.status-out [count 16]\n",
__FILE__, __FUNCTION__, prefix);
// Export PoNET status output array (16 bytes) - HAL to hardware
for (int byte_idx = 0; byte_idx < 16; byte_idx++) {
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.status-out.%02d\n",
__FILE__, __FUNCTION__, prefix, byte_idx);
r = hal_pin_u32_newf(HAL_IN, &device->PoNETmodule.statusOut_pins[byte_idx],
comp_id, "%s.ponet.status-out.%02d", prefix, byte_idx);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"PoKeys: %s:%s: %s.ponet.status-out.%02d failed\n",
__FILE__, __FUNCTION__, prefix, byte_idx);
return r;
}
}
return 0;
}
/**
* @brief Export general PoNET status and control pins.
*
* This function exports general PoNET bus status pins that are not device-specific.
*
* @param prefix HAL component prefix (e.g., "pokeys_async.0")
* @param comp_id HAL component ID
* @param device Pointer to sPoKeysDevice structure
* @return 0 on success, negative HAL error code on failure
*/
POKEYSDECL int export_ponet_status_pins(const char *prefix, long comp_id, sPoKeysDevice *device)
{
int r = 0;
if (!device || !prefix) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: Invalid parameters\n", __FILE__, __FUNCTION__);
return -1;
}
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet [status/type/id/light/pwm]\n",
__FILE__, __FUNCTION__, prefix);
// Export PoNET status pin
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.status\n", __FILE__, __FUNCTION__, prefix);
r = hal_pin_u32_newf(HAL_OUT, &device->PoNETmodule.PoNETstatus_pin,
comp_id, "%s.ponet.status", prefix);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.status failed\n",
__FILE__, __FUNCTION__, prefix);
return r;
}
// Export PoNET module type pin
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.module-type\n", __FILE__, __FUNCTION__, prefix);
r = hal_pin_u32_newf(HAL_OUT, &device->PoNETmodule.moduleType_pin,
comp_id, "%s.ponet.module-type", prefix);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.module-type failed\n",
__FILE__, __FUNCTION__, prefix);
return r;
}
// Export PoNET module ID pin
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.module-id\n", __FILE__, __FUNCTION__, prefix);
r = hal_pin_u32_newf(HAL_OUT, &device->PoNETmodule.moduleID_pin,
comp_id, "%s.ponet.module-id", prefix);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.module-id failed\n",
__FILE__, __FUNCTION__, prefix);
return r;
}
// Export light sensor value pin - output from hardware to HAL
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.light-value\n", __FILE__, __FUNCTION__, prefix);
r = hal_pin_u32_newf(HAL_OUT, &device->PoNETmodule.lightValue_pin,
comp_id, "%s.ponet.light-value", prefix);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.light-value failed\n",
__FILE__, __FUNCTION__, prefix);
return r;
}
// Export PWM duty control pin - input from HAL to hardware
rtapi_print_msg(RTAPI_MSG_DBG, "PoKeys: %s:%s: %s.ponet.pwm-duty\n", __FILE__, __FUNCTION__, prefix);
r = hal_pin_u32_newf(HAL_IN, &device->PoNETmodule.PWMduty_pin,
comp_id, "%s.ponet.pwm-duty", prefix);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: %s.ponet.pwm-duty failed\n",
__FILE__, __FUNCTION__, prefix);
return r;
}
return 0;
}
/**
* @brief Main export function for all PoNET HAL pins.
*
* This is the main entry point for exporting PoNET-related HAL pins.
* It exports both basic PoNET communication arrays and general status pins.
*
* @param prefix HAL component prefix (e.g., "pokeys_async.0")
* @param comp_id HAL component ID
* @param device Pointer to sPoKeysDevice structure
* @return 0 on success, negative HAL error code on failure
*/
POKEYSDECL int export_ponet_pins(const char *prefix, long comp_id, sPoKeysDevice *device)
{
int r = 0;
if (!device || !prefix) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: Invalid parameters\n", __FILE__, __FUNCTION__);
return -1;
}
rtapi_print_msg(RTAPI_MSG_INFO, "PoKeys: %s:%s: Exporting PoNET pins for %s\n",
__FILE__, __FUNCTION__, prefix);
// Export PoNET status pins
r = export_ponet_status_pins(prefix, comp_id, device);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: export_ponet_status_pins failed: %d\n",
__FILE__, __FUNCTION__, r);
return r;
}
// Export basic PoNET communication pins
r = export_ponet_basic_pins(prefix, comp_id, device);
if (r != 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "PoKeys: %s:%s: export_ponet_basic_pins failed: %d\n",
__FILE__, __FUNCTION__, r);
return r;
}
rtapi_print_msg(RTAPI_MSG_INFO, "PoKeys: %s:%s: Successfully exported all PoNET pins for %s\n",
__FILE__, __FUNCTION__, prefix);
return 0;
}