📄 hal_m5i20.c
字号:
// Init pin. *(this->in[channel].pValue) = 0; *(this->in[channel].pValueNot) = 1; } // Export functions. if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.digital-in-read", boardId); halError = hal_export_funct(name, Device_DigitalInRead, this, 0, 0, componentId); } if(halError){ rtapi_print_msg(RTAPI_MSG_ERR, "M5I20: ERROR: export digital in failed\n"); return(-1); } return(0);}static intDevice_ExportDigitalOutPinsParametersFunctions(Device *this, int componentId, int boardId){ int halError=0, channel; char name[HAL_NAME_LEN + 2]; // Export pins and parameters. for(channel = 0; channel < M5I20_NUM_DIGITAL_OUTPUTS; channel++){ // Pins. rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.out-%02d", boardId, channel); if((halError = hal_pin_bit_new(name, HAL_RD, &(this->out[channel].pValue), componentId)) != 0) break; // Parameters. rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.out-%02d-invert", boardId, channel); if((halError = hal_param_bit_new(name, HAL_WR, &(this->out[channel].invert), componentId)) != 0) break; // Init pin. *(this->out[channel].pValue) = 0; this->out[channel].invert = 1; } // Export functions. if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.digital-out-write", boardId); halError = hal_export_funct(name, Device_DigitalOutWrite, this, 0, 0, componentId); } if(halError){ rtapi_print_msg(RTAPI_MSG_ERR, "M5I20: ERROR: export digital out failed\n"); return(-1); } return(0);}static intDevice_ExportMiscPinsParametersFunctions(Device *this, int componentId, int boardId){ int halError; char name[HAL_NAME_LEN + 2]; // Export Pins. rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.estop-in", boardId); halError = hal_pin_bit_new(name, HAL_WR, &(this->misc.pEstopIn), componentId); if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.estop-in-not", boardId); halError = hal_pin_bit_new(name, HAL_WR, &(this->misc.pEstopInNot), componentId); } if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.watchdog-reset", boardId); halError = hal_pin_bit_new(name, HAL_RD_WR, &(this->misc.pWatchdogReset), componentId); } // Export Parameters. if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.watchdog-control", boardId); halError = hal_param_u16_new(name, HAL_WR, &(this->misc.watchdogControl), componentId); } if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.watchdog-timeout", boardId); halError = hal_param_u16_new(name, HAL_WR, &(this->misc.watchdogTimeout), componentId); } if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.led-view", boardId); halError = hal_param_u16_new(name, HAL_WR, &(this->misc.ledView), componentId); } // Init pins. if(!halError){ *(this->misc.pEstopIn) = 0; *(this->misc.pEstopInNot) = 1; *(this->misc.pWatchdogReset) = 0; this->misc.watchdogControl = 0; this->misc.watchdogTimeout = 16000; this->misc.ledView = 0; } // Export functions. if(!halError){ rtapi_snprintf(name, HAL_NAME_LEN, "m5i20.%d.misc-update", boardId); halError = hal_export_funct(name, Device_MiscUpdate, this, 0, 0, componentId); } if(halError){ rtapi_print_msg(RTAPI_MSG_ERR, "M5I20: ERROR: export miscellaneous failed\n"); return(-1); } return(0);}/* * HAL EXPORTED FUNCTIONS */static voidDevice_EncoderRead(void *arg, long period){ Device *this = (Device *)arg; M5i20HostMotRegMap *pCard16 = this->pCard16; M5i20HostMotRegMap *pCard32 = this->pCard32; EncoderPinsParams *pEncoder; int i, j; hal_u16_t status; pEncoder = &this->encoder[0]; // For each encoder. for(i = 0, j = 0; j < M5I20_NUM_ENCODER_CHANNELS; j++, pEncoder++){ // Check reset pin. if(*(pEncoder->pResetCount)){ // Clear pin. *(pEncoder->pResetCount) = 0; // Reset encoder. pCard16->encoderControl[i] |= M5I20_ENC_CTL_LOCAL_CLEAR; } // Check index latch pin. if(*(pEncoder->pLatchIndex)){ // Clear pins. *(pEncoder->pLatchIndex) = 0; *(pEncoder->pIndexLatch) = 0; // Enable index latch. pCard16->encoderControl[i] &= ~M5I20_ENC_CTL_LATCH_ON_READ; pCard16->encoderControl[i] |= M5I20_ENC_CTL_LATCH_ON_INDEX | M5I20_ENC_CTL_LATCH_ONCE; } // Read encoder status. status = pCard16->encoderControl[i]; // Read encoder count. *(pEncoder->pCount) = pCard32->encoderCount[i]; // Scale count to make floating point position. if(pEncoder->scale) *(pEncoder->pPosition) = *(pEncoder->pCount) / pEncoder->scale; // Update index and index latched pins. *(pEncoder->pIndex) = (status & M5I20_ENC_CTL_INDEX)? 1: 0; if((*(pEncoder->pIndexLatch) == 0) && !(status & M5I20_ENC_CTL_LATCH_ON_INDEX)){ *(pEncoder->pIndexLatch) = 1; *(pEncoder->pCountLatch) = *(pEncoder->pCount); *(pEncoder->pPositionLatch) = *(pEncoder->pPosition); pCard16->encoderControl[i] |= M5I20_ENC_CTL_LATCH_ON_READ; } // Loop house keeping. if(++i == M5I20_NUM_PRIMARY_ENCODERS) i = M5I20_MAX_PRIMARY_ENCODERS; }}static voidDevice_DacWrite(void *arg, long period){ Device *this = (Device *)arg; M5i20HostMotRegMap *pCard16 = this->pCard16; DacPinsParams *pDac; int i; hal_float_t volts; hal_s16_t dacCount; if(this->misc.watchdogControl & WDT_CONTROL_AUTO_RESET){ // Reset the watchdog timer. Device_WdtReset(this); } pDac = &this->dac[0]; // For each DAC. for(i = 0; i < M5I20_NUM_PWM_CHANNELS; i++, pDac++){ // Check for mode change. if(pDac->interlaced != this->lastDacInterlaced[i]){ if((this->lastDacInterlaced[i] = pDac->interlaced) == 0){ pCard16->pwmControl[i] &= ~M5I20_PWM_CTL_INTERLACED; }else{ pCard16->pwmControl[i] |= M5I20_PWM_CTL_INTERLACED; } } // Calculate hardware register value. volts = (*(pDac->pValue) - pDac->offset) * pDac->gain; // Truncate volts to DAC limits. if(volts > M5I20_DAC_VOLTS_MAX){ volts = M5I20_DAC_VOLTS_MAX; }else if(volts < M5I20_DAC_VOLTS_MIN){ volts = M5I20_DAC_VOLTS_MIN; } // Transform volts to counts. dacCount = (hal_s16_t)(volts * M5I20_DAC_SCALE_MULTIPLY / M5I20_DAC_SCALE_DIVIDE); // Write DAC. pCard16->pwmValue[i] = dacCount; // Check enable pin. if(*(pDac->pEnable)){ pCard16->pwmControl[i] |= M5I20_PWM_CTL_ENABLE; }else{ pCard16->pwmControl[i] &= ~M5I20_PWM_CTL_ENABLE; } }}static voidDevice_DigitalInRead(void *arg, long period){ Device *this = (Device *)arg; M5i20HostMotRegMap *pCard32 = this->pCard32; DigitalInPinsParams *pDigitalIn; int i, j; hal_u32_t pins; pDigitalIn = &this->in[0]; // For each port. for(i = 0; i < M5I20_NUM_DIGITAL_IO_PORTS; i++){ // Read digital I/O register. pins = pCard32->digitalIo[i].data >> M5I20_DIGITAL_IN_SHFT; // For each pin. for(j = 0; j < 16; j++, pDigitalIn++){ // Update pins. *(pDigitalIn->pValue) = pins & 1; *(pDigitalIn->pValueNot) = !*(pDigitalIn->pValue); pins >>= 1; } }}static voidDevice_DigitalOutWrite(void *arg, long period){ Device *this = (Device *)arg; M5i20HostMotRegMap *pCard32 = this->pCard32; DigitalOutPinsParams *pDigitalOut; int i, j; hal_u32_t pins, mask; pDigitalOut = &this->out[0]; // For each port. for(i = 0; i < M5I20_NUM_DIGITAL_IO_PORTS; i++){ pins = 0; mask = 1; // For each pin. for(j = 0; j < 8; j++, pDigitalOut++){ // Build hardware register value. if(*(pDigitalOut->pValue) != pDigitalOut->invert){ pins |= mask; } mask <<=1; } // Write digital I/O register. pCard32->digitalIo[i].data = pins << M5I20_DIGITAL_OUT_SHFT; }}static voidDevice_MiscUpdate(void *arg, long period){ Device *this = (Device *)arg; M5i20HostMotRegMap *pCard16 = this->pCard16; // Write watchdog timer configuration to hardware. if(this->misc.watchdogControl & WDT_CONTROL_ENABLE){ pCard16->wdTimeout = this->misc.watchdogTimeout; pCard16->mode |= M5I20_MODE_STOP_ON_WDT; }else{ pCard16->mode &= ~M5I20_MODE_STOP_ON_WDT; } // Check watchdog timer reset pin. if(*(this->misc.pWatchdogReset)){ // Clear pin. *(this->misc.pWatchdogReset) = 0; // Reset the watchdog timer. Device_WdtReset(this); } // Update E-STOP pin. *(this->misc.pEstopIn) = ((pCard16->mode & M5I20_MODE_STOP_ON_WDT) && (pCard16->wdTimer == 0))? 1: 0; *(this->misc.pEstopInNot) = !*(this->misc.pEstopIn); // Write LED view channel to hardware. pCard16->ledView = this->misc.ledView;}/* * PRIVATE HELPER FUNCTIONS */static voidDevice_WdtReset(Device *this){ M5i20HostMotRegMap *pCard16 = this->pCard16; // Check if watchdog timer is enabled. if(!(pCard16->mode & M5I20_MODE_STOP_ON_WDT)) return; // Check for timeout. if(pCard16->wdTimer == 0){ rtapi_print_msg(RTAPI_MSG_ERR, "M5I20: ERROR: watchdog timeout\n"); // Reset the watchdog timer. pCard16->control = M5I20_CONTROL_RESET_WDT; // For error recovery. pCard16->mode |= M5I20_MODE_PWM_ENABLE; }else{ // Reset the watchdog timer. pCard16->control = M5I20_CONTROL_RESET_WDT; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -