|
| 1 | +// Copyright (C) 2023 Andy Pugh |
| 2 | + |
| 3 | +// This program is free software; you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation; either version 2 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program; if not, write to the Free Software |
| 15 | +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | +// |
| 17 | + |
| 18 | +/* A configurable component to use Mesa PktUART for modbus control */ |
| 19 | + |
| 20 | + |
| 21 | +#include "rtapi.h" |
| 22 | +#include "rtapi_slab.h" |
| 23 | +#include "rtapi_app.h" |
| 24 | +#include "rtapi_string.h" |
| 25 | +#include "hal.h" |
| 26 | + |
| 27 | +#if !defined(__KERNEL__) |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#endif |
| 31 | + |
| 32 | +/* module information */ |
| 33 | +MODULE_AUTHOR("Andy Pugh"); |
| 34 | +MODULE_DESCRIPTION("convert enumerated types to HAL_BIT pins"); |
| 35 | +MODULE_LICENSE("GPL"); |
| 36 | + |
| 37 | +#define MAX_CHAN 256 |
| 38 | + |
| 39 | +typedef struct { |
| 40 | + hal_bit_t *bit; |
| 41 | + hal_u32_t *en; // note use index 0 differently |
| 42 | +} enum_hal_t; |
| 43 | + |
| 44 | +typedef struct{ |
| 45 | + int dir; |
| 46 | + int num_pins; |
| 47 | + enum_hal_t *hal; |
| 48 | +} enum_inst_t; |
| 49 | + |
| 50 | +typedef struct { |
| 51 | + int num_insts; |
| 52 | + enum_inst_t *insts; |
| 53 | +} enum_t; |
| 54 | + |
| 55 | +static int comp_id; |
| 56 | + |
| 57 | +static enum_t e; |
| 58 | + |
| 59 | +static char *enums[MAX_CHAN] = {0,}; |
| 60 | +RTAPI_MP_ARRAY_STRING(enums, MAX_CHAN, "states, ; delimited"); |
| 61 | +static char *names[MAX_CHAN] = {0,}; |
| 62 | +RTAPI_MP_ARRAY_STRING(names, MAX_CHAN, "component names (optional)"); |
| 63 | + |
| 64 | +static void decode(void *inst, long period); |
| 65 | +static void encode(void *inst, long period); |
| 66 | + |
| 67 | +int rtapi_app_main(void){ |
| 68 | + int i, j, v; |
| 69 | + int retval; |
| 70 | + char *token; |
| 71 | + |
| 72 | + if (!enums[0]) { |
| 73 | + rtapi_print_msg(RTAPI_MSG_ERR, "The enum_decode component requires at least" |
| 74 | + " one enumeration list\n"); |
| 75 | + return -EINVAL; |
| 76 | + } |
| 77 | + |
| 78 | + // count instances |
| 79 | + e.num_insts = MAX_CHAN; |
| 80 | + for (i = 0; i < MAX_CHAN; i++){ |
| 81 | + if (! enums[i] && ! names[i]){ |
| 82 | + e.num_insts = i; |
| 83 | + rtapi_print_msg(RTAPI_MSG_ERR, "making %i instances\n", e.num_insts); |
| 84 | + break; |
| 85 | + } |
| 86 | + if ((! enums[i] && names[i]) || ( ! names[i] && names[0] && enums[i])){ |
| 87 | + rtapi_print_msg(RTAPI_MSG_ERR, "Inconsistent number of names and enums\n"); |
| 88 | + return -EINVAL; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + comp_id = hal_init("enum"); |
| 93 | + |
| 94 | + if (comp_id < 0) { |
| 95 | + rtapi_print_msg(RTAPI_MSG_ERR, "ERROR: hal_init() failed\n"); |
| 96 | + return -EINVAL; |
| 97 | + } |
| 98 | + // allocate memory for the base struct |
| 99 | + e.insts = (enum_inst_t *)rtapi_kmalloc(e.num_insts * sizeof(enum_inst_t), RTAPI_GFP_KERNEL); |
| 100 | + for (i = 0; i < e.num_insts; i++){ |
| 101 | + enum_inst_t *inst = &(e.insts[i]); |
| 102 | + char this[HAL_NAME_LEN]; |
| 103 | + char func[HAL_NAME_LEN]; |
| 104 | + |
| 105 | + // Count the pins |
| 106 | + inst->num_pins = 0; |
| 107 | + inst->dir = HAL_OUT; // direction of bit pin, out for decode |
| 108 | + for (j = strlen(enums[i]); j > 0; j--){ |
| 109 | + if (enums[i][j] == ';'){ |
| 110 | + if (enums[i][j-1] != ';' ) inst->num_pins++; |
| 111 | + // insert a string terminator |
| 112 | + enums[i][j] = 0; |
| 113 | + } |
| 114 | + } |
| 115 | + inst->hal = (enum_hal_t *)hal_malloc((inst->num_pins + 1) * sizeof(enum_hal_t)); |
| 116 | + token = enums[i]; |
| 117 | + switch (*token){ |
| 118 | + case 'E': |
| 119 | + case 'e': // encode |
| 120 | + inst->dir = HAL_IN; |
| 121 | + break; |
| 122 | + case 'D': |
| 123 | + case 'd': |
| 124 | + inst->dir = HAL_OUT; |
| 125 | + break; |
| 126 | + default: |
| 127 | + rtapi_print_msg(RTAPI_MSG_ERR, "Each enum string must start" |
| 128 | + "with either E; or D; to define the mode\n"); |
| 129 | + goto fail0; |
| 130 | + } |
| 131 | + |
| 132 | + if (names[i]) { |
| 133 | + rtapi_snprintf(this, HAL_NAME_LEN, "%s", names[i]); |
| 134 | + } else if (inst->dir == HAL_IN) { |
| 135 | + rtapi_snprintf(this, HAL_NAME_LEN, "enum-encode.%02i", i); |
| 136 | + } else { |
| 137 | + rtapi_snprintf(this, HAL_NAME_LEN, "enum-decode.%02i", i); |
| 138 | + } |
| 139 | + |
| 140 | + // create single per-instance int pin in index 0 |
| 141 | + if (inst->dir == HAL_OUT) { |
| 142 | + retval = hal_pin_u32_newf(HAL_IN, &(inst->hal[0].en), comp_id, |
| 143 | + "%s.input", this); |
| 144 | + } else { |
| 145 | + retval = hal_pin_u32_newf(HAL_OUT, &(inst->hal[0].en), comp_id, |
| 146 | + "%s.output", this); |
| 147 | + } |
| 148 | + v = 0; |
| 149 | + for (j = 1; j <= inst->num_pins; j++){ // 1-based indexing |
| 150 | + // skip to the next pin name |
| 151 | + while (*(++token) != 0){} |
| 152 | + //increment for skipped enumerations |
| 153 | + while (*(++token) == 0) v++; |
| 154 | + |
| 155 | + retval = hal_pin_bit_newf(inst->dir, &(inst->hal[j].bit), |
| 156 | + comp_id, "%s.%s-%s",this, token, |
| 157 | + (inst->dir == HAL_IN)?"in":"out"); |
| 158 | + retval += hal_pin_u32_newf(HAL_IN, &(inst->hal[j].en), |
| 159 | + comp_id, "%s.%s-val",this, token); |
| 160 | + *(inst->hal[j].en) = v++; |
| 161 | + |
| 162 | + if (retval < 0){ |
| 163 | + rtapi_print_msg(RTAPI_MSG_ERR, "Failed to create HAL pins\n"); |
| 164 | + goto fail0; |
| 165 | + } |
| 166 | + } |
| 167 | + if (inst->dir == HAL_OUT){ |
| 168 | + retval = rtapi_snprintf(func, HAL_NAME_LEN, "%s.decode", this); |
| 169 | + hal_export_funct(func, decode, inst, 0, 0, comp_id); |
| 170 | + } else { |
| 171 | + retval = rtapi_snprintf(func, HAL_NAME_LEN, "%s.encode", this); |
| 172 | + hal_export_funct(func, encode, inst, 0, 0, comp_id); |
| 173 | + } |
| 174 | + if (retval < 0){ |
| 175 | + rtapi_print_msg(RTAPI_MSG_ERR, "Failed to export functions\n"); |
| 176 | + goto fail0; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + hal_ready(comp_id); |
| 182 | + return 0; |
| 183 | + |
| 184 | + fail0: |
| 185 | + free(e.insts); |
| 186 | + hal_exit(comp_id); |
| 187 | + return -1; |
| 188 | + |
| 189 | +} |
| 190 | + |
| 191 | +static void decode(void *v_inst, long period){; |
| 192 | + enum_inst_t *inst = v_inst; |
| 193 | + for (int i = 1; i <= inst->num_pins; i++){ |
| 194 | + if (*(inst->hal[0].en) == *(inst->hal[i].en)){ |
| 195 | + *(inst->hal[i].bit) = 1; |
| 196 | + } else { |
| 197 | + *(inst->hal[i].bit) = 0; |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | +static void encode(void *v_inst, long period){; |
| 202 | + enum_inst_t *inst = v_inst; |
| 203 | + *(inst->hal[0].en) = 0; |
| 204 | + for (int i = 1; i <= inst->num_pins; i++){ |
| 205 | + if (*(inst->hal[i].bit)){ |
| 206 | + *(inst->hal[0].en) = *(inst->hal[i].en); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments