|
| 1 | +#include "jsd/jsd_el3314.h" |
| 2 | + |
| 3 | +#include <assert.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include "jsd/jsd_sdo.h" |
| 7 | + |
| 8 | +const char* jsd_el3314_element_strings[] = { |
| 9 | + [JSD_EL3314_ELEMENT_TYPE_K] = "TYPE_K", |
| 10 | + [JSD_EL3314_ELEMENT_TYPE_J] = "TYPE_J", |
| 11 | + [JSD_EL3314_ELEMENT_TYPE_L] = "TYPE_L", |
| 12 | + [JSD_EL3314_ELEMENT_TYPE_E] = "TYPE_E", |
| 13 | + [JSD_EL3314_ELEMENT_TYPE_T] = "TYPE_T", |
| 14 | + [JSD_EL3314_ELEMENT_TYPE_N] = "TYPE_N", |
| 15 | + [JSD_EL3314_ELEMENT_TYPE_U] = "TYPE_U", |
| 16 | + [JSD_EL3314_ELEMENT_TYPE_B] = "TYPE_B", |
| 17 | + [JSD_EL3314_ELEMENT_TYPE_R] = "TYPE_R", |
| 18 | + [JSD_EL3314_ELEMENT_TYPE_S] = "TYPE_S", |
| 19 | + [JSD_EL3314_ELEMENT_TYPE_C] = "TYPE_C", |
| 20 | +}; |
| 21 | + |
| 22 | +const char* jsd_el3314_presentation_strings[] = { |
| 23 | + [JSD_EL3314_PRESENTATION_SIGNED] = "Signed", |
| 24 | + [1] = "unused presentation", |
| 25 | + [JSD_EL3314_PRESENTATION_HIGH_RES] = "High Res", |
| 26 | +}; |
| 27 | + |
| 28 | +/**************************************************** |
| 29 | + * Public functions |
| 30 | + ****************************************************/ |
| 31 | +const jsd_el3314_state_t* jsd_el3314_get_state(jsd_t* self, uint16_t slave_id) { |
| 32 | + assert(self); |
| 33 | + assert(jsd_el3314_product_code_is_compatible( |
| 34 | + self->ecx_context.slavelist[slave_id].eep_id)); |
| 35 | + |
| 36 | + return &self->slave_states[slave_id].el3314; |
| 37 | +} |
| 38 | + |
| 39 | +void jsd_el3314_read(jsd_t* self, uint16_t slave_id) { |
| 40 | + assert(self); |
| 41 | + assert(jsd_el3314_product_code_is_compatible( |
| 42 | + self->ecx_context.slavelist[slave_id].eep_id)); |
| 43 | + |
| 44 | + jsd_el3314_state_t* state = &self->slave_states[slave_id].el3314; |
| 45 | + jsd_el3314_config_t* config = &self->slave_configs[slave_id].el3314; |
| 46 | + |
| 47 | + jsd_el3314_txpdo_t* txpdo = |
| 48 | + (jsd_el3314_txpdo_t*)self->ecx_context.slavelist[slave_id].inputs; |
| 49 | + |
| 50 | + int ch; |
| 51 | + for (ch = 0; ch < JSD_EL3314_NUM_CHANNELS; ch++) { |
| 52 | + state->adc_value[ch] = txpdo->channel[ch].value; |
| 53 | + state->output_eu[ch] = |
| 54 | + jsd_el3314_output_from_config(txpdo->channel[ch].value, config, ch); |
| 55 | + |
| 56 | + state->underrange[ch] = (txpdo->channel[ch].flags >> 0) & 0x01; |
| 57 | + state->overrange[ch] = (txpdo->channel[ch].flags >> 1) & 0x01; |
| 58 | + state->error[ch] = (txpdo->channel[ch].flags >> 6) & 0x01; |
| 59 | + state->txPDO_state[ch] = (txpdo->channel[ch].flags >> 14) & 0x01; |
| 60 | + state->txPDO_toggle[ch] = (txpdo->channel[ch].flags >> 15) & 0x01; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/**************************************************** |
| 65 | + * Private functions |
| 66 | + ****************************************************/ |
| 67 | + |
| 68 | +bool jsd_el3314_init(jsd_t* self, uint16_t slave_id) { |
| 69 | + assert(self); |
| 70 | + assert(jsd_el3314_product_code_is_compatible( |
| 71 | + self->ecx_context.slavelist[slave_id].eep_id)); |
| 72 | + assert(self->ecx_context.slavelist[slave_id].eep_man == |
| 73 | + JSD_BECKHOFF_VENDOR_ID); |
| 74 | + |
| 75 | + ec_slavet* slaves = self->ecx_context.slavelist; |
| 76 | + ec_slavet* slave = &slaves[slave_id]; |
| 77 | + |
| 78 | + slave->PO2SOconfigx = jsd_el3314_PO2SO_config; |
| 79 | + |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +int jsd_el3314_PO2SO_config(ecx_contextt* ecx_context, uint16_t slave_id) { |
| 84 | + assert(ecx_context); |
| 85 | + assert(jsd_el3314_product_code_is_compatible( |
| 86 | + ecx_context->slavelist[slave_id].eep_id)); |
| 87 | + |
| 88 | + // cast the void* to slave_config |
| 89 | + jsd_slave_config_t* slave_configs = |
| 90 | + (jsd_slave_config_t*)ecx_context->userdata; |
| 91 | + |
| 92 | + jsd_slave_config_t* config = &slave_configs[slave_id]; |
| 93 | + |
| 94 | + // Reset to factory default |
| 95 | + uint32_t reset_word = JSD_BECKHOFF_RESET_WORD; |
| 96 | + if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, JSD_BECKHOFF_RESET_SDO, |
| 97 | + JSD_BECKHOFF_RESET_SUBIND, JSD_SDO_DATA_U32, |
| 98 | + &reset_word)) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + MSG("Configuring slave no: %u, SII inferred name: %s", slave_id, |
| 103 | + ecx_context->slavelist[slave_id].name); |
| 104 | + MSG("\t Configured name: %s", config->name); |
| 105 | + |
| 106 | + int ch; |
| 107 | + for (ch = 0; ch < JSD_EL3314_NUM_CHANNELS; ch++) { |
| 108 | + MSG("\t Ch%d Config:", ch); |
| 109 | + MSG("\t\tElement: %s", |
| 110 | + jsd_el3314_element_strings[config->el3314.element[ch]]); |
| 111 | + MSG("\t\tFilter: %s", |
| 112 | + jsd_beckhoff_filter_strings[config->el3314.filter[ch]]); |
| 113 | + MSG("\t\tPresentation: %s", |
| 114 | + jsd_el3314_presentation_strings[config->el3314.presentation[ch]]); |
| 115 | + |
| 116 | + // register is 0x80n0, where n is channel number (e.g. ch4 = 0x8040) |
| 117 | + uint32_t sdo_channel_index = 0x8000 + (0x10 * ch); |
| 118 | + |
| 119 | + uint16_t element = config->el3314.element[ch]; |
| 120 | + if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, sdo_channel_index, |
| 121 | + 0x19, JSD_SDO_DATA_U16, &element)) { |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + uint16_t filter = config->el3314.filter[ch]; |
| 126 | + if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, sdo_channel_index, |
| 127 | + 0x15, JSD_SDO_DATA_U16, &filter)) { |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + uint8_t presentation = config->el3314.presentation[ch]; |
| 132 | + if (!jsd_sdo_set_param_blocking(ecx_context, slave_id, sdo_channel_index, |
| 133 | + 0x02, JSD_SDO_DATA_U8, &presentation)) { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + config->PO2SO_success = true; |
| 139 | + return 1; |
| 140 | +} |
| 141 | + |
| 142 | +double jsd_el3314_output_from_config(int16_t adc_value, |
| 143 | + jsd_el3314_config_t* config, |
| 144 | + uint16_t channel) { |
| 145 | + assert(config->presentation[channel] < JSD_EL3314_NUM_PRESENTATIONS); |
| 146 | + assert(config->element[channel] < JSD_EL3314_NUM_ELEMENTS); |
| 147 | + |
| 148 | + double temp_scale_factor = 1; |
| 149 | + |
| 150 | + switch (config->presentation[channel]) { |
| 151 | + case JSD_EL3314_PRESENTATION_SIGNED: |
| 152 | + temp_scale_factor = 0.1; |
| 153 | + break; |
| 154 | + case JSD_EL3314_PRESENTATION_HIGH_RES: |
| 155 | + temp_scale_factor = 0.01; |
| 156 | + break; |
| 157 | + default: |
| 158 | + WARNING("Bad/Unsupported presentation setting"); |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + return (double)adc_value * temp_scale_factor; |
| 163 | +} |
| 164 | + |
| 165 | +bool jsd_el3314_product_code_is_compatible(uint32_t product_code) { |
| 166 | + return product_code == JSD_EL3314_PRODUCT_CODE; |
| 167 | +} |
0 commit comments