⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m7i43_hm2.comp

📁 CNC 的开放码,EMC2 V2.2.8版
💻 COMP
📖 第 1 页 / 共 5 页
字号:
    }    if (board->watchdog.num_instances != 0) {        PRINT(            RTAPI_MSG_ERR,            "Module Descriptor contains duplicate %s (inconsistent firmware), not loading driver\n",            hm2_get_general_function_name(m->gtag)        );        return -1;    }    if (m->instances != 1) {        PRINT(RTAPI_MSG_WARN, "MD declares %d watchdogs!  only using the first one...\n", m->instances);    }    board->watchdog.num_instances = 1;    board->watchdog.instance = (hm2_watchdog_instance_t *)hal_malloc(board->watchdog.num_instances * sizeof(hm2_watchdog_instance_t));    if (board->watchdog.instance == NULL) {        PRINT(RTAPI_MSG_ERR, "out of memory!\n");        return -1;    }    if (m->clock_tag == 1) {        board->watchdog.clock_frequency = board->idrom.clock_low;    } else if (m->clock_tag == 2) {        board->watchdog.clock_frequency = board->idrom.clock_high;    } else {        PRINT(RTAPI_MSG_ERR, "%s MD has invalid Clock Tag %d\n", hm2_get_general_function_name(m->gtag), m->clock_tag);    }    board->watchdog.version = m->version;    board->watchdog.timer_addr = m->base_address + (0 * m->register_stride);    board->watchdog.status_addr = m->base_address + (1 * m->register_stride);    board->watchdog.reset_addr = m->base_address + (2 * m->register_stride);    // export to HAL    {        int r;        // pins        r = hal_pin_bit_newf(            HAL_IO,            &(board->watchdog.instance[0].hal.pin.has_bit),            comp_id,            "m7i43_hm2.%d.watchdog.has-bit",            0        );        if (r != HAL_SUCCESS) {            PRINT(RTAPI_MSG_ERR, "error adding pin, aborting\n");            return -1;        }    }    //    // initialize the watchdog registers    //    (*board->watchdog.instance[0].hal.pin.has_bit) = 0;    // timeout_s = (timer_counts + 1) / clock_hz    // (timeout_s * clock_hz) - 1 = timer_counts    // (timeout_ns * (1 s/1e9 ns) * clock_hz) - 1 = timer_counts    {        __u64 tmp;        board->watchdog.instance[0].timeout_ns = watchdog_timeout_ns;        tmp = (board->watchdog.instance[0].timeout_ns * ((double)board->watchdog.clock_frequency / (double)(1000 * 1000 * 1000))) - 1;        if (tmp >= 0x80000000) {            PRINT(RTAPI_MSG_ERR, "watchdog timeout %u ns is too long!", board->watchdog.instance[0].timeout_ns);            return -1;        }        board->watchdog.instance[0].hw.timer = tmp;    }    // set the watchdog timeout    m7i43_epp_addr16(board->watchdog.timer_addr + M7I43_HM2_ADDR_AUTOINCREMENT);    m7i43_epp_write32(board->watchdog.instance[0].hw.timer);    // clear the has-bit bit    m7i43_epp_addr16(board->watchdog.status_addr + M7I43_HM2_ADDR_AUTOINCREMENT);    m7i43_epp_write32(0);    return board->watchdog.num_instances;}static int m7i43_hm2_parse_module_descriptors(m7i43_t *board) {    int i;    for (i = 0; i < board->num_mds; i ++) {        hm2_module_descriptor_t *m = &board->md[i];        int md_accepted;        if (m->gtag == 0) {            return 1;        }        if ((m->clock_tag != 1) && (m->clock_tag != 2)) {            PRINT(                RTAPI_MSG_ERR,                "Module Descriptor %d (gtag=%d (%s), version=%d) has invalid ClockTag %d, skipping\n",                i,                m->gtag,                hm2_get_general_function_name(m->gtag),                m->version,                m->clock_tag            );            continue;        }        md_accepted = 0;  // will be set by the switch        switch (m->gtag) {            case HM2_GTAG_ENCODER:                md_accepted = hm2_md_parse_encoder(board, m);                break;            case HM2_GTAG_PWMGEN:                md_accepted = hm2_md_parse_pwmgen(board, m);                break;            case HM2_GTAG_IOPORT:                md_accepted = hm2_md_parse_ioport(board, m);                break;            case HM2_GTAG_STEPGEN:                md_accepted = hm2_md_parse_stepgen(board, m);                break;            case HM2_GTAG_WATCHDOG:                md_accepted = hm2_md_parse_watchdog(board, m);                break;            default:                PRINT(                    RTAPI_MSG_WARN,                    "MD %d: %dx %s v%d: ignored\n",                    i,                    m->instances,                    hm2_get_general_function_name(m->gtag),                    m->version                );                continue;        }        if (md_accepted >= 0)  {            PRINT(                RTAPI_MSG_INFO,                "MD %d: %dx %s v%d: accepted, using %d\n",                i,                m->instances,                hm2_get_general_function_name(m->gtag),                m->version,                md_accepted            );        } else {            PRINT(RTAPI_MSG_ERR, "failed to parse Module Descriptor %d\n", i);            return 0;  // fail...        }    }    return 1;  // success!}// // here come the functions to deal with pins// static const char* hm2_get_pin_secondary_name(hm2_pin_t *pin) {    static char unknown[100];    int sec_pin = pin->sec_pin & 0x7F;  // turn off the "pin is an output" bit    switch (pin->sec_tag) {        case HM2_GTAG_ENCODER:            switch (sec_pin) {                case 1: return "A";                case 2: return "B";                case 3: return "Index";            }            break;        case HM2_GTAG_PWMGEN:            switch (sec_pin) {                case 1: return "Out0 (PWM or Up)";                case 2: return "Out1 (Dir or Down)";                case 3: return "Not-Enable";            }            break;        case HM2_GTAG_STEPGEN:            // FIXME: these depend on the stepgen mode            switch (sec_pin) {                case 1: return "Step";                case 2: return "Direction";                case 3: return "(unused)";                case 4: return "(unused)";                case 5: return "(unused)";                case 6: return "(unused)";            }            break;    }    rtapi_snprintf(unknown, sizeof(unknown), "unknown-pin-%d", sec_pin & 0x7F);    return unknown;}static int m7i43_hm2_read_pin_descriptors(m7i43_t *board) {    int i;    int addr = board->idrom_offset + board->idrom.offset_to_pin_desc;    m7i43_epp_addr16(addr + M7I43_HM2_ADDR_AUTOINCREMENT);    i = 0;    do {        __u32 d;        d = m7i43_epp_read32();        board->pin[i].sec_pin     = (d >>  0) & 0x000000FF;        board->pin[i].sec_tag     = (d >>  8) & 0x000000FF;        board->pin[i].sec_unit    = (d >> 16) & 0x000000FF;        board->pin[i].primary_tag = (d >> 24) & 0x000000FF;        if (board->pin[i].primary_tag == 0) {            board->num_pins = i;            return 1;        }        if (board->pin[i].primary_tag != HM2_GTAG_IOPORT) {            PRINT(                RTAPI_MSG_ERR,                "pin %d primary tag is %d (%s), not IOPort!\n",                i,                board->pin[i].primary_tag,                hm2_get_general_function_name(board->pin[i].primary_tag)            );            return 0;        }        i++;        addr += 4;  // just for debugging, the actual board is using address auto-increment    } while (i < M7I43_HM2_MAX_PIN_DESCRIPTORS);    return 1;}static void hm2_print_pin_descriptors(int msg_level, m7i43_t *board) {    int i;    PRINT(msg_level, "%d HM2 Pin Descriptors:\n", board->num_pins);    for (i = 0; i < board->num_pins; i ++) {        PRINT(msg_level, "    pin %d:\n", i);        PRINT(            msg_level,            "        Secondary Pin: 0x%02X (%s, %s)\n",            board->pin[i].sec_pin,            hm2_get_pin_secondary_name(&board->pin[i]),            ((board->pin[i].sec_pin & 0x80) ? "Output" : "Input")        );        PRINT(            msg_level,            "        Secondary Tag: 0x%02X (%s)\n",            board->pin[i].sec_tag,            hm2_get_general_function_name(board->pin[i].sec_tag)        );        PRINT(msg_level, "        Secondary Unit: 0x%02X\n", board->pin[i].sec_unit);        PRINT(            msg_level,            "        Primary Tag: 0x%02X (%s)\n",            board->pin[i].primary_tag,            hm2_get_general_function_name(board->pin[i].primary_tag)        );    }}static void hm2_set_pin_source(m7i43_t *board, int pin_number, int source) {    int ioport_number;    int bit_number;    ioport_number = pin_number / 24;    bit_number = pin_number % 24;    if ((pin_number < 0) || (ioport_number > board->ioport.num_instances)) {        PRINT(RTAPI_MSG_ERR, "invalid pin number %d\n", pin_number);        return;    }    if (source == HM2_PIN_SOURCE_IS_PRIMARY) {        board->ioport.instance[ioport_number].alt_source &= ~(1 << bit_number);        board->pin[pin_number].gtag = board->pin[pin_number].primary_tag;    } else if (source == HM2_PIN_SOURCE_IS_SECONDARY) {        board->ioport.instance[ioport_number].alt_source |= (1 << bit_number);        board->pin[pin_number].gtag = board->pin[pin_number].sec_tag;    } else {        PRINT(RTAPI_MSG_ERR, "invalid pin source 0x%08X\n", source);        return;    }}static void hm2_set_pin_direction(m7i43_t *board, int pin_number, int direction) {    int ioport_number;    int bit_number;    ioport_number = pin_number / 24;    bit_number = pin_number % 24;    if ((pin_number < 0) || (ioport_number > board->ioport.num_instances)) {        PRINT(RTAPI_MSG_ERR, "invalid pin number %d\n", pin_number);        return;    }    if (direction == HM2_PIN_DIR_IS_INPUT) {        board->ioport.instance[ioport_number].ddr &= ~(1 << bit_number);    } else if (direction == HM2_PIN_DIR_IS_OUTPUT) {        board->ioport.instance[ioport_number].ddr |= (1 << bit_number);    } else {        PRINT(RTAPI_MSG_ERR, "invalid pin direction 0x%08X\n", direction);    }}static void hm2_print_pin_usage(int msg_level, m7i43_t *board) {    int i;    PRINT(msg_level, "%d I/O Pins used:\n", board->num_pins);    for (i = 0; i < board->num_pins; i ++) {        if (board->pin[i].gtag == board->pin[i].sec_tag) {            PRINT(                msg_level,                "    I/O Pin % 3d: %s #%d, pin %s (%s)\n",                i,                hm2_get_general_function_name(board->pin[i].gtag),                board->pin[i].sec_unit,                hm2_get_pin_secondary_name(&board->pin[i]),                ((board->pin[i].sec_pin & 0x80) ? "Output" : "Input")            );        } else {            PRINT(                msg_level,                "    I/O Pin % 3d: %s\n",                i,                hm2_get_general_function_name(board->pin[i].gtag)            );        }    }}static int m7i43_hm2_configure_pins(m7i43_t *board) {    int i;        static int created_hal_stuff_for_gpio = 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -