int main() { Solution s; const int buf_len = 10; char buf[buf_len]={0}; #define SET_INPUT(s) strcpy(input, s); p=input; printf("input data = \"%s\"\n", p); #define CLEAR_BUFFER() memset(buf, 0, sizeof(buf)/sizeof(buf[0]) ) #define READ(s, buf, n) CLEAR_BUFFER(); printf("read(%d) => ",n); s.read(buf, n); printf("%s\n", buf); printf("-----------------------------\n"); SET_INPUT("1234abcd1234abcdxyz"); for(int i=0; i<5; i++){ READ(s, buf, 4); } printf("-----------------------------\n"); SET_INPUT("ab"); READ(s, buf, 1); READ(s, buf, 2); printf("-----------------------------\n"); SET_INPUT("abcde"); for(int i=0; i<5; i++){ READ(s, buf, 1); } printf("-----------------------------\n"); SET_INPUT("1234xyzabcd00"); READ(s, buf, 4); for(int i=0; i<3; i++){ READ(s, buf, 1); } READ(s, buf, 4); printf("-----------------------------\n"); return 0; }
void Space::load_motor(int m, int32_t &addr) { // {{{ loaddebug("loading motor %d", m); uint16_t enable = motor[m]->enable_pin.write(); double old_home_pos = motor[m]->home_pos; double old_steps_per_unit = motor[m]->steps_per_unit; motor[m]->step_pin.read(read_16(addr)); motor[m]->dir_pin.read(read_16(addr)); motor[m]->enable_pin.read(read_16(addr)); motor[m]->limit_min_pin.read(read_16(addr)); motor[m]->limit_max_pin.read(read_16(addr)); motor[m]->steps_per_unit = read_float(addr); motor[m]->home_pos = read_float(addr); motor[m]->limit_v = read_float(addr); motor[m]->limit_a = read_float(addr); motor[m]->home_order = read_8(addr); arch_motors_change(); SET_OUTPUT(motor[m]->enable_pin); if (enable != motor[m]->enable_pin.write()) { if (motors_busy) SET(motor[m]->enable_pin); else { RESET(motor[m]->enable_pin); } } RESET(motor[m]->step_pin); SET_INPUT(motor[m]->limit_min_pin); SET_INPUT(motor[m]->limit_max_pin); bool must_move = false; if (!isnan(motor[m]->home_pos)) { // Axes with a limit switch. if (motors_busy && (old_home_pos != motor[m]->home_pos || old_steps_per_unit != motor[m]->steps_per_unit) && !isnan(old_home_pos)) { double ohp = old_home_pos * old_steps_per_unit; double hp = motor[m]->home_pos * motor[m]->steps_per_unit; double diff = hp - ohp; motor[m]->settings.current_pos += diff; //debug("load motor %d %d new home %f add %f", id, m, motor[m]->home_pos, diff); arch_addpos(id, m, diff); must_move = true; } } else { // Axes without a limit switch, including extruders. if (motors_busy && old_steps_per_unit != motor[m]->steps_per_unit) { debug("load motor %d %d new steps no home", id, m); double oldpos = motor[m]->settings.current_pos; double pos = oldpos / old_steps_per_unit; motor[m]->settings.current_pos = pos * motor[m]->steps_per_unit; arch_addpos(id, m, motor[m]->settings.current_pos - oldpos); // Adjust current_pos in all history. for (int h = 0; h < FRAGMENTS_PER_BUFFER; ++h) { oldpos = motor[m]->history[h].current_pos; pos = oldpos / old_steps_per_unit; motor[m]->history[h].current_pos = pos * motor[m]->steps_per_unit; } } } if (must_move) move_to_current(); } // }}}
void Space::load_motor(uint8_t m, int32_t &addr) { // {{{ loaddebug("loading motor %d", m); uint16_t enable = motor[m]->enable_pin.write(); double old_home_pos = motor[m]->home_pos; double old_steps_per_unit = motor[m]->steps_per_unit; motor[m]->step_pin.read(read_16(addr)); motor[m]->dir_pin.read(read_16(addr)); motor[m]->enable_pin.read(read_16(addr)); motor[m]->limit_min_pin.read(read_16(addr)); motor[m]->limit_max_pin.read(read_16(addr)); motor[m]->sense_pin.read(read_16(addr)); motor[m]->steps_per_unit = read_float(addr); motor[m]->max_steps = read_8(addr); motor[m]->home_pos = read_float(addr); motor[m]->limit_v = read_float(addr); motor[m]->limit_a = read_float(addr); motor[m]->home_order = read_8(addr); arch_motors_change(); SET_OUTPUT(motor[m]->enable_pin); if (enable != motor[m]->enable_pin.write()) { if (motors_busy) SET(motor[m]->enable_pin); else { RESET(motor[m]->enable_pin); } } RESET(motor[m]->step_pin); SET_INPUT(motor[m]->limit_min_pin); SET_INPUT(motor[m]->limit_max_pin); SET_INPUT(motor[m]->sense_pin); bool must_move = false; if (!isnan(motor[m]->home_pos)) { // Axes with a limit switch. if (motors_busy && (old_home_pos != motor[m]->home_pos || old_steps_per_unit != motor[m]->steps_per_unit) && !isnan(old_home_pos)) { int32_t hp = motor[m]->home_pos * motor[m]->steps_per_unit + (motor[m]->home_pos > 0 ? .49 : -.49); int32_t ohp = old_home_pos * old_steps_per_unit + (old_home_pos > 0 ? .49 : -.49); int32_t diff = hp - ohp; motor[m]->settings.current_pos += diff; cpdebug(id, m, "load motor new home add %d", diff); arch_addpos(id, m, diff); must_move = true; } } else { // Axes without a limit switch, including extruders. if (motors_busy && old_steps_per_unit != motor[m]->steps_per_unit) { int32_t cp = motor[m]->settings.current_pos; double pos = cp / old_steps_per_unit; cpdebug(id, m, "load motor new steps no home"); motor[m]->settings.current_pos = pos * motor[m]->steps_per_unit; int diff = motor[m]->settings.current_pos - cp; arch_addpos(id, m, diff); } } if (must_move) move_to_current(); } // }}}
void Space::load_motor(int m) { // {{{ loaddebug("loading motor %d", m); uint16_t enable = motor[m]->enable_pin.write(); double old_home_pos = motor[m]->home_pos; double old_steps_per_unit = motor[m]->steps_per_unit; bool old_dir_invert = motor[m]->dir_pin.inverted(); motor[m]->step_pin.read(shmem->ints[2]); motor[m]->dir_pin.read(shmem->ints[3]); motor[m]->enable_pin.read(shmem->ints[4]); motor[m]->limit_min_pin.read(shmem->ints[5]); motor[m]->limit_max_pin.read(shmem->ints[6]); motor[m]->home_order = shmem->ints[7]; motor[m]->steps_per_unit = shmem->floats[0]; if (std::isnan(motor[m]->steps_per_unit)) { debug("Trying to set NaN steps per unit for motor %d %d", id, m); motor[m]->steps_per_unit = old_steps_per_unit; } motor[m]->home_pos = shmem->floats[1]; motor[m]->limit_v = shmem->floats[2]; motor[m]->limit_a = shmem->floats[3]; arch_motors_change(); SET_OUTPUT(motor[m]->enable_pin); if (enable != motor[m]->enable_pin.write()) { if (motors_busy) SET(motor[m]->enable_pin); else { RESET(motor[m]->enable_pin); } } RESET(motor[m]->step_pin); RESET(motor[m]->dir_pin); SET_INPUT(motor[m]->limit_min_pin); SET_INPUT(motor[m]->limit_max_pin); if (motor[m]->dir_pin.inverted() != old_dir_invert) arch_invertpos(id, m); if (!std::isnan(motor[m]->home_pos)) { // Axes with a limit switch. if (motors_busy && (old_home_pos != motor[m]->home_pos || old_steps_per_unit != motor[m]->steps_per_unit) && !std::isnan(old_home_pos)) { double diff = motor[m]->home_pos - old_home_pos * old_steps_per_unit / motor[m]->steps_per_unit; motor[m]->settings.current_pos += diff; //debug("load motor %d %d new home %f add %f", id, m, motor[m]->home_pos, diff); // adjusting the arch pos is not affected by steps/unit. arch_addpos(id, m, motor[m]->home_pos - old_home_pos); } reset_pos(this); } else if (!std::isnan(motor[m]->settings.current_pos)) { // Motors without a limit switch: adjust motor position to match axes. for (int a = 0; a < num_axes; ++a) axis[a]->settings.target = axis[a]->settings.current; space_types[type].xyz2motors(this); double diff = motor[m]->settings.target_pos - motor[m]->settings.current_pos; motor[m]->settings.current_pos += diff; arch_addpos(id, m, diff); } } // }}}
/** LCD API **/ void lcd_init() { lcd_implementation_init(); #ifdef NEWPANEL SET_INPUT(BTN_EN1); SET_INPUT(BTN_EN2); WRITE(BTN_EN1,HIGH); WRITE(BTN_EN2,HIGH); #if BTN_ENC > 0 SET_INPUT(BTN_ENC); WRITE(BTN_ENC,HIGH); #endif #ifdef REPRAPWORLD_KEYPAD pinMode(SHIFT_CLK,OUTPUT); pinMode(SHIFT_LD,OUTPUT); pinMode(SHIFT_OUT,INPUT); WRITE(SHIFT_OUT,HIGH); WRITE(SHIFT_LD,HIGH); #endif #else // Not NEWPANEL #ifdef SR_LCD_2W_NL // Non latching 2 wire shift register pinMode (SR_DATA_PIN, OUTPUT); pinMode (SR_CLK_PIN, OUTPUT); #elif defined(SHIFT_CLK) pinMode(SHIFT_CLK,OUTPUT); pinMode(SHIFT_LD,OUTPUT); pinMode(SHIFT_EN,OUTPUT); pinMode(SHIFT_OUT,INPUT); WRITE(SHIFT_OUT,HIGH); WRITE(SHIFT_LD,HIGH); WRITE(SHIFT_EN,LOW); #else #ifdef ULTIPANEL #error ULTIPANEL requires an encoder #endif #endif // SR_LCD_2W_NL #endif//!NEWPANEL #if defined (SDSUPPORT) && defined(SDCARDDETECT) && (SDCARDDETECT > 0) pinMode(SDCARDDETECT,INPUT); WRITE(SDCARDDETECT, HIGH); lcd_oldcardstatus = IS_SD_INSERTED; #endif//(SDCARDDETECT > 0) #ifdef LCD_HAS_SLOW_BUTTONS slow_buttons = 0; #endif lcd_buttons_update(); #ifdef ULTIPANEL encoderDiff = 0; #endif }
/******* EXT IO ***********************/ void ZeroPi::extInit(int index, int type){ if(EXT_INPUT == type){ SET_INPUT(extios[index].pin); extios[index].function = EXT_INPUT; }else if(EXT_OUTPUT == type){ SET_OUTPUT(extios[index].pin); extios[index].function = EXT_OUTPUT; }else if(EXT_SERVO== type){ SET_OUTPUT(extios[index].pin); extios[index].function = EXT_SERVO; } }
/** Inititalise the I2C/TWI subsystem. \param address Address the system should listen to in slave mode, unused when configured for master mode. In master mode, receiver address is given to the send command. This also sets the I2C address. In slave mode it's the address we listen on. In master mode it's the communication target address. As master one can talk to different devices. Call again i2c_init() for changing the target address, then. Doing so won't interrupt ongoing transmissions and overhead is small. */ void i2c_init(uint8_t address) { // In case this is a re-inititalisation, // don't interrupt an ongoing transmission. while (i2c_state & I2C_MODE_BUSY) { delay_us(10); } i2c_address = address; #ifdef I2C_MASTER_MODE #ifdef I2C_ENABLE_PULLUPS SET_INPUT(SCL); PULLUP_ON(SCL); SET_INPUT(SDA); PULLUP_ON(SDA); #endif /* I2C_ENABLE_PULLUPS */ /** TWI Bit Rate register SCL_freq = CPU_freq / (16 + 2 * TWBR) See: page 235 of atmega328 datasheet. */ TWBR = ((F_CPU / I2C_BITRATE) - 16) / 2; /** TWI Status Register Lower two bits set the prescaler value. See: page 236 of atmega328 datasheet. */ TWSR = 0x00; #endif /* I2C_MASTER_MODE */ #ifdef I2C_SLAVE_MODE TWAR = i2c_address; // We listen to broadcasts if lowest bit is set. TWCR = (0<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(1<<TWEN)|(1<<TWIE); #endif }
void power_off() { stepper_disable(); x_disable(); y_disable(); z_disable(); e_disable(); #ifdef PS_ON_PIN SET_INPUT(PS_ON_PIN); #endif ps_is_on = 0; }
// ---------------------------------------------------------------------------- void mcp2515_spi_init(void) { // Aktivieren der Pins fuer das SPI Interface SET(MCP2515_CS); SET_OUTPUT(MCP2515_CS); // Aktivieren der Pins fuer das SPI Interface RESET(P_SCK); RESET(P_MOSI); RESET(P_MISO); SET_OUTPUT(P_SCK); SET_OUTPUT(P_MOSI); SET_INPUT(P_MISO); }
uint8_t ds18b20_read_bit(){ // synchronize SET_LOW(DS18B20_PORT, DS18B20_DQ); SET_OUTPUT(DS18B20_DDR, DS18B20_DQ); DS18B20_PRECISE_DELAY(2); // wait until thermometer puts bit SET_INPUT(DS18B20_DDR, DS18B20_DQ); DS18B20_PRECISE_DELAY(5); // read it uint8_t ret=IS_HIGH(DS18B20_PIN, DS18B20_DQ); DS18B20_PRECISE_DELAY(60); return ret; }
void ds18b20_write_bit(uint8_t bit){ // synchronize SET_OUTPUT(DS18B20_DDR, DS18B20_DQ); SET_LOW(DS18B20_PORT, DS18B20_DQ); DS18B20_PRECISE_DELAY(2); // put bit if(bit){ SET_HIGH(DS18B20_PORT, DS18B20_DQ); } DS18B20_PRECISE_DELAY(60); // release line SET_INPUT(DS18B20_DDR, DS18B20_DQ); SET_LOW(DS18B20_PORT, DS18B20_DQ); DS18B20_PRECISE_DELAY(2); }
uint8_t ds18b20_reset(){ // reset pulse SET_LOW(DS18B20_PORT, DS18B20_DQ); SET_OUTPUT(DS18B20_DDR,DS18B20_DQ); DS18B20_PRECISE_DELAY(480); // presence pulse SET_INPUT(DS18B20_DDR, DS18B20_DQ); DS18B20_PRECISE_DELAY(100); if(IS_HIGH(DS18B20_PIN, DS18B20_DQ)){ return 0; // not present } DS18B20_PRECISE_DELAY(380); if(IS_LOW(DS18B20_PIN, DS18B20_DQ)){ return 0; // not present } return 1; }
/** LCD API **/ void lcd_init() { lcd_implementation_init(); #ifdef NEWPANEL SET_INPUT(BTN_EN1); SET_INPUT(BTN_EN2); #if (SDCARDDETECT != -1) SET_INPUT(SDCARDDETECT); #endif WRITE(BTN_EN1,HIGH); WRITE(BTN_EN2,HIGH); #if BTN_ENC > 0 SET_INPUT(BTN_ENC); WRITE(BTN_ENC,HIGH); #endif #ifdef REPRAPWORLD_KEYPAD SET_OUTPUT(SHIFT_CLK); SET_OUTPUT(SHIFT_LD); SET_INPUT(SHIFT_OUT); WRITE(SHIFT_OUT,HIGH); WRITE(SHIFT_LD,HIGH); #endif #else #ifdef SR_LCD_2W_NL SET_OUTPUT(SR_DATA_PIN); SET_OUTPUT(SR_CLK_PIN); #else SET_OUTPUT(SHIFT_CLK); SET_OUTPUT(SHIFT_LD); SET_OUTPUT(SHIFT_EN); SET_INPUT(SHIFT_OUT); WRITE(SHIFT_OUT,HIGH); WRITE(SHIFT_LD,HIGH); WRITE(SHIFT_EN,LOW); #endif // SR_LCD_2W_NL #endif//!NEWPANEL #if (SDCARDDETECT > 0) WRITE(SDCARDDETECT, HIGH); lcd_oldcardstatus = IS_SD_INSERTED; #endif//(SDCARDDETECT > 0) #ifdef LCD_HAS_SLOW_BUTTONS slow_buttons = 0; #endif lcd_buttons_update(); #ifdef ULTIPANEL encoderDiff = 0; #endif }
SDCard::SDCard() { sdmode = false; sdactive = false; savetosd = false; Printer::setAutomount(false); //power to SD reader #if SDPOWER > -1 SET_OUTPUT(SDPOWER); WRITE(SDPOWER,HIGH); #endif #define SDCARDDETECT 24 #if defined(SDCARDDETECT) && SDCARDDETECT>-1 SET_INPUT(SDCARDDETECT); WRITE(SDCARDDETECT,HIGH); #endif }
/// set up temp sensors. Currently only the 'intercom' sensor needs initialisation. void temp_init() { temp_sensor_t i; for (i = 0; i < NUM_TEMP_SENSORS; i++) { switch(temp_sensors[i].temp_type) { #ifdef TEMP_MAX6675 // initialised when read /* case TT_MAX6675: break;*/ #ifdef NAL_REPRAP SET_OUTPUT(MAX6675_CS); SET_OUTPUT(MAX6675_SCK); SET_INPUT(MAX6675_SO); #endif #endif #ifdef TEMP_THERMISTOR // handled by analog_init() /* case TT_THERMISTOR: break;*/ #endif #ifdef TEMP_AD595 // handled by analog_init() /* case TT_AD595: break;*/ #endif #ifdef TEMP_INTERCOM case TT_INTERCOM: intercom_init(); send_temperature(0, 0); break; #endif default: /* prevent compiler warning */ break; } } }
/// initialise all I/O - set pins as input or output, turn off unused subsystems, etc void io_init(void) { // disable modules we don't use #ifdef PRR PRR = MASK(PRTWI) | MASK(PRADC) | MASK(PRSPI); #elif defined PRR0 PRR0 = MASK(PRTWI) | MASK(PRADC) | MASK(PRSPI); #if defined(PRUSART3) // don't use USART2 or USART3- leave USART1 for GEN3 and derivatives PRR1 |= MASK(PRUSART3) | MASK(PRUSART2); #endif #if defined(PRUSART2) // don't use USART2 or USART3- leave USART1 for GEN3 and derivatives PRR1 |= MASK(PRUSART2); #endif #endif ACSR = MASK(ACD); // setup I/O pins // X Stepper //WRITE(X_STEP_PIN, 0); SET_OUTPUT(X_STEP_PIN); //WRITE(X_DIR_PIN, 0); SET_OUTPUT(X_DIR_PIN); #ifdef X_MIN_PIN SET_INPUT(X_MIN_PIN); WRITE(X_MIN_PIN, 0); // pullup resistors off #endif #ifdef X_MAX_PIN SET_INPUT(X_MAX_PIN); WRITE(X_MAX_PIN, 0); // pullup resistors off #endif // Y Stepper //WRITE(Y_STEP_PIN, 0); SET_OUTPUT(Y_STEP_PIN); //WRITE(Y_DIR_PIN, 0); SET_OUTPUT(Y_DIR_PIN); #ifdef Y_MIN_PIN SET_INPUT(Y_MIN_PIN); WRITE(Y_MIN_PIN, 0); // pullup resistors off #endif #ifdef Y_MAX_PIN SET_INPUT(Y_MAX_PIN); WRITE(Y_MAX_PIN, 0); // pullup resistors off #endif // Z Stepper #if defined Z_STEP_PIN && defined Z_DIR_PIN WRITE(Z_STEP_PIN, 0); SET_OUTPUT(Z_STEP_PIN); WRITE(Z_DIR_PIN, 0); SET_OUTPUT(Z_DIR_PIN); #endif #ifdef Z_MIN_PIN SET_INPUT(Z_MIN_PIN); WRITE(Z_MIN_PIN, 0); // pullup resistors off #endif #ifdef Z_MAX_PIN SET_INPUT(Z_MAX_PIN); WRITE(Z_MAX_PIN, 0); // pullup resistors off #endif #if defined E_STEP_PIN && defined E_DIR_PIN WRITE(E_STEP_PIN, 0); SET_OUTPUT(E_STEP_PIN); WRITE(E_DIR_PIN, 0); SET_OUTPUT(E_DIR_PIN); #endif // Common Stepper Enable #ifdef STEPPER_ENABLE_PIN #ifdef STEPPER_INVERT_ENABLE WRITE(STEPPER_ENABLE_PIN, 0); #else WRITE(STEPPER_ENABLE_PIN, 1); #endif SET_OUTPUT(STEPPER_ENABLE_PIN); #endif // X Stepper Enable #ifdef X_ENABLE_PIN #ifdef X_INVERT_ENABLE WRITE(X_ENABLE_PIN, 0); #else WRITE(X_ENABLE_PIN, 1); #endif SET_OUTPUT(X_ENABLE_PIN); #endif // Y Stepper Enable #ifdef Y_ENABLE_PIN #ifdef Y_INVERT_ENABLE WRITE(Y_ENABLE_PIN, 0); #else WRITE(Y_ENABLE_PIN, 1); #endif SET_OUTPUT(Y_ENABLE_PIN); #endif // Z Stepper Enable #ifdef Z_ENABLE_PIN #ifdef Z_INVERT_ENABLE WRITE(Z_ENABLE_PIN, 0); #else WRITE(Z_ENABLE_PIN, 1); #endif SET_OUTPUT(Z_ENABLE_PIN); #endif // E Stepper Enable #ifdef E_ENABLE_PIN #ifdef E_INVERT_ENABLE WRITE(E_ENABLE_PIN, 0); #else WRITE(E_ENABLE_PIN, 1); #endif SET_OUTPUT(E_ENABLE_PIN); #endif #ifdef STEPPER_ENABLE_PIN power_off(); #endif #ifdef TEMP_MAX6675 // setup SPI WRITE(SCK, 0); SET_OUTPUT(SCK); WRITE(MOSI, 1); SET_OUTPUT(MOSI); WRITE(MISO, 1); SET_INPUT(MISO); WRITE(SS, 1); SET_OUTPUT(SS); #endif #ifdef TEMP_INTERCOM // Enable the RS485 transceiver SET_OUTPUT(RX_ENABLE_PIN); SET_OUTPUT(TX_ENABLE_PIN); WRITE(RX_ENABLE_PIN,0); disable_transmit(); #endif }
void probe_init() { SET_INPUT(PROBE_PIN); WRITE(PROBE_PIN,HIGH); }
void io_init(void) { // disable modules we don't use #ifdef PRR PRR = MASK(PRTWI) | MASK(PRADC) | MASK(PRSPI); #elif defined PRR0 PRR0 = MASK(PRTWI) | MASK(PRADC) | MASK(PRSPI); #ifdef PRR1 // don't use USART2 or USART3- leave USART1 for GEN3 and derivatives PRR1 = MASK(PRUSART3) | MASK(PRUSART2); #endif #endif ACSR = MASK(ACD); // setup I/O pins WRITE(X_STEP_PIN, 0); SET_OUTPUT(X_STEP_PIN); WRITE(X_DIR_PIN, 0); SET_OUTPUT(X_DIR_PIN); WRITE(X_MIN_PIN, 1); SET_INPUT(X_MIN_PIN); #ifdef X_MAX_PIN WRITE(X_MAX_PIN, 1); SET_INPUT(X_MAX_PIN); #endif #ifdef X_ENABLE_PIN WRITE(X_ENABLE_PIN, 1); SET_OUTPUT(X_ENABLE_PIN); #endif WRITE(Y_STEP_PIN, 0); SET_OUTPUT(Y_STEP_PIN); WRITE(Y_DIR_PIN, 0); SET_OUTPUT(Y_DIR_PIN); WRITE(Y_MIN_PIN, 1); SET_INPUT(Y_MIN_PIN); #ifdef Y_MAX_PIN WRITE(Y_MAX_PIN, 1); SET_INPUT(Y_MAX_PIN); #endif #ifdef Y_ENABLE_PIN WRITE(Y_ENABLE_PIN, 1); SET_OUTPUT(Y_ENABLE_PIN); #endif WRITE(Z_STEP_PIN, 0); SET_OUTPUT(Z_STEP_PIN); WRITE(Z_DIR_PIN, 0); SET_OUTPUT(Z_DIR_PIN); WRITE(Z_MIN_PIN, 1); SET_INPUT(Z_MIN_PIN); #ifdef Z_MAX_PIN WRITE(Z_MAX_PIN, 1); SET_INPUT(Z_MAX_PIN); #endif #ifdef Z_ENABLE_PIN WRITE(Z_ENABLE_PIN, 1); SET_OUTPUT(Z_ENABLE_PIN); #endif WRITE(E_STEP_PIN, 0); SET_OUTPUT(E_STEP_PIN); WRITE(E_DIR_PIN, 0); SET_OUTPUT(E_DIR_PIN); // setup PWM timers: fast PWM, no prescaler TCCR0A = MASK(WGM01) | MASK(WGM00); TCCR0B = MASK(CS00); TIMSK0 = 0; OCR0A = 0; OCR0B = 0; TCCR2A = MASK(WGM21) | MASK(WGM20); TCCR2B = MASK(CS20); TIMSK2 = 0; OCR2A = 0; OCR2B = 0; #ifdef TCCR3A TCCR3A = MASK(WGM30); TCCR3B = MASK(WGM32) | MASK(CS30); TIMSK3 = 0; OCR3A = 0; OCR3B = 0; #endif #ifdef TCCR4A TCCR4A = MASK(WGM40); TCCR4B = MASK(WGM42) | MASK(CS40); TIMSK4 = 0; OCR4A = 0; OCR4B = 0; #endif #ifdef TCCR5A TCCR5A = MASK(WGM50); TCCR5B = MASK(WGM52) | MASK(CS50); TIMSK5 = 0; OCR5A = 0; OCR5B = 0; #endif #ifdef STEPPER_ENABLE_PIN power_off(); #endif // setup SPI WRITE(SCK, 0); SET_OUTPUT(SCK); WRITE(MOSI, 1); SET_OUTPUT(MOSI); WRITE(MISO, 1); SET_INPUT(MISO); WRITE(SS, 1); SET_OUTPUT(SS); }
// ------------------------------------------------------------------------- bool mcp2515_init(can_bitrate_t bitrate) { if (bitrate >= 8) return false; SET(MCP2515_CS); SET_OUTPUT(MCP2515_CS); // Aktivieren der Pins fuer das SPI Interface RESET(P_SCK); RESET(P_MOSI); RESET(P_MISO); SET_OUTPUT(P_SCK); SET_OUTPUT(P_MOSI); SET_INPUT(P_MISO); // SPI Einstellung setzen mcp2515_spi_init(); // MCP2515 per Software Reset zuruecksetzten, // danach ist er automatisch im Konfigurations Modus RESET(MCP2515_CS); spi_putc(SPI_RESET); _delay_ms(1); SET(MCP2515_CS); // ein bisschen warten bis der MCP2515 sich neu gestartet hat _delay_ms(10); // CNF1..3 Register laden (Bittiming) RESET(MCP2515_CS); spi_putc(SPI_WRITE); spi_putc(CNF3); for (uint8_t i=0; i<3 ;i++ ) { spi_putc(pgm_read_byte(&_mcp2515_cnf[bitrate][i])); } // aktivieren/deaktivieren der Interrupts spi_putc(MCP2515_INTERRUPTS); SET(MCP2515_CS); // TXnRTS Bits als Inputs schalten mcp2515_write_register(TXRTSCTRL, 0); #if defined(MCP2515_INT) SET_INPUT(MCP2515_INT); SET(MCP2515_INT); #endif #ifdef RXnBF_FUNKTION SET_INPUT(MCP2515_RX0BF); SET_INPUT(MCP2515_RX1BF); SET(MCP2515_RX0BF); SET(MCP2515_RX1BF); // Aktivieren der Pin-Funktionen fuer RX0BF und RX1BF mcp2515_write_register(BFPCTRL, (1<<B0BFE)|(1<<B1BFE)|(1<<B0BFM)|(1<<B1BFM)); #else #ifdef MCP2515_TRANSCEIVER_SLEEP // activate the pin RX1BF as GPIO which is connected // to RS of MCP2551 and set it to 0 mcp2515_write_register(BFPCTRL, (1<<B1BFE)); #else // Deaktivieren der Pins RXnBF Pins (High Impedance State) mcp2515_write_register(BFPCTRL, 0); #endif #endif // Testen ob das auf die beschreibenen Register zugegriffen werden kann // (=> ist der Chip ueberhaupt ansprechbar?) bool error = false; if (mcp2515_read_register(CNF2) != pgm_read_byte(&_mcp2515_cnf[bitrate][1])) { error = true; } // Device zurueck in den normalen Modus versetzten // und aktivieren/deaktivieren des Clkout-Pins mcp2515_write_register(CANCTRL, CLKOUT_PRESCALER_); if (error) { return false; } else { while ((mcp2515_read_register(CANSTAT) & 0xe0) != 0) { // warten bis der neue Modus uebernommen wurde } return true; } }
void main(void) { unsigned char i=0; unsigned char status; unsigned char str[MAX_LEN]; unsigned char RC_size; unsigned char blockAddr; unsigned char mynum[20]; unsigned char serNum[7]; uint8_t RCV; int resulta=0; int fd; int a=0; fd=Init_SPI("/dev/spidev0.0"); setup_io(); SET_INPUT(25); SET_OUTPUT(25); ApiInit(fd); MFRC522_Init(); a=Read_MFRC522(VersionReg); printf("MFRC Version Read %02X \r\n",a); // for(a=0;a<10;a++) // { // RCV=Read_MFRC522(VersionReg); // //RCV=SPI_transfer(fd,a); // printf("RCV %02X \r\n",RCV); // SET_BIT(25); // sleep(1); // CLR_BIT(25); // sleep(1); // } while(1) { status = MFRC522_Request(PICC_REQIDL, str); if(resulta==status){ }else{ resulta=status; } if (status == MI_OK) { mifare_tag_type(str); //usleep(1000); } status = MFRC522_Anticoll(str); memcpy(serNum, str,5); if (status == MI_OK) { printf("UID :"); for(i=0;i<5;i++) { printf("["); printf("%02X",serNum[i]); printf("]"); } printf("\n"); //usleep(1000); } RC_size=MFRC522_SelectTag(serNum); if(RC_size !=0) { printf("Select_OK Size %02d \n",RC_size); puts(""); puts(""); } } close(fd); }
void limits_init() { ///////////////////########################## #if MotherBoard==3 && !defined(limit_int_style) ///*** MB==3 #if defined(X_MIN_PIN) && X_MIN_PIN>-1 //////############## SET_INPUT(X_MIN_PIN); #endif #if defined(Y_MIN_PIN) && Y_MIN_PIN>-1 //////############## SET_INPUT(Y_MIN_PIN); #endif #if defined(Z_MIN_PIN) && Z_MIN_PIN>-1 //////############## SET_INPUT(Z_MIN_PIN); #endif #if defined(X_MAX_PIN) && X_MAX_PIN>-1 //////############## SET_INPUT(X_MAX_PIN); #endif #if defined(Y_MAX_PIN) && Y_MAX_PIN>-1 //////############## SET_INPUT(Y_MAX_PIN); #endif #if defined(Z_MAX_PIN) && Z_MAX_PIN>-1 //////############## SET_INPUT(Z_MAX_PIN); #endif /* #if(ENDSTOP_X_MIN_INVERTING) #else #endif*/ if (bit_istrue(settings.flags,BITFLAG_INVERT_LIMIT_PINS)) { PULLUP(X_MIN_PIN,LOW); /////***** invert as machine SET LOW PULLUP(Y_MIN_PIN,LOW); PULLUP(Z_MIN_PIN,LOW); PULLUP(X_MAX_PIN,LOW); /////***** invert as machine SET LOW PULLUP(Y_MAX_PIN,LOW); PULLUP(Z_MAX_PIN,LOW); } else { PULLUP(X_MIN_PIN,HIGH); /////***** invert as machine SET HIGH PULLUP(Y_MIN_PIN,HIGH); PULLUP(Z_MIN_PIN,HIGH); PULLUP(X_MAX_PIN,HIGH); /////***** invert as machine SET HIGH PULLUP(Y_MAX_PIN,HIGH); PULLUP(Z_MAX_PIN,HIGH); } if (bit_istrue(settings.flags,BITFLAG_HARD_LIMIT_ENABLE)) { LIMIT_PCMSK |= LIMIT_MASK; // Enable specific pins of the Pin Change Interrupt ///****7=0x00000111 PCICR |= (1 << LIMIT_INT); // Enable Pin Change Interrupt } else { limits_disable(); } #else ///*** MB!=3 LIMIT_DDR &= ~(LIMIT_MASK); // Set as input pins if (bit_istrue(settings.flags,BITFLAG_INVERT_LIMIT_PINS)) { LIMIT_PORT &= ~(LIMIT_MASK); // Normal low operation. Requires external pull-down. } else { LIMIT_PORT |= (LIMIT_MASK); // Enable internal pull-up resistors. Normal high operation. } if (bit_istrue(settings.flags,BITFLAG_HARD_LIMIT_ENABLE)) { LIMIT_PCMSK |= LIMIT_MASK; // Enable specific pins of the Pin Change Interrupt PCICR |= (1 << LIMIT_INT); // Enable Pin Change Interrupt } else { limits_disable(); } #ifdef ENABLE_SOFTWARE_DEBOUNCE MCUSR &= ~(1<<WDRF); WDTCSR |= (1<<WDCE) | (1<<WDE); WDTCSR = (1<<WDP0); // Set time-out at ~32msec. #endif #endif ///*** MB==3 //////############################### }
/* * Evaluate the current game state. */ static double eval_game(game *g, int who) { player *p; card *c; int n = 0, i, j; int power, stack, bluff, bad_bluff; net *l; /* Get player's network */ l = &learner[who]; /* Check for no learner loaded */ if (!l->num_inputs) return 0.5; /* Loop over each player */ for (i = 0; i < 2; i++) { /* Get player pointer */ p = &g->p[i]; /* Loop over cards in deck */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Set input for active cards (except leadership) */ SET_INPUT(l, n++, c->active && !c->random_fake && c->d_ptr->type != TYPE_LEADERSHIP && (who == i || c->loc_known)); } /* Loop over cards in deck */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Set input for cards in hand (if known) */ SET_INPUT(l, n++, (who == i || c->loc_known) && c->where == LOC_HAND && !c->random_fake); } /* Loop over cards in deck */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Set input for used cards */ SET_INPUT(l, n++, (who == i || c->loc_known) && !c->random_fake && (c->where == LOC_DISCARD || c->where == LOC_LEADERSHIP || (c->where == LOC_COMBAT && !c->active))); } /* Loop over cards in deck */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Set input for cards loaded on ship */ SET_INPUT(l, n++, (c->ship != NULL)); } } /* Get evaluating player */ p = &g->p[who]; /* Loop over cards in deck */ for (i = 1; i < DECK_SIZE; i++) { /* Get card pointer */ c = &p->deck[i]; /* Set input for "special" card */ SET_INPUT(l, n++, c->text_boosted || c->on_bottom || c->bluff); } /* Assume no bad bluff */ bad_bluff = 0; /* Loop over cards in deck */ for (i = 1; i < DECK_SIZE; i++) { /* Get card pointer */ c = &p->deck[i]; /* Skip non-bluff cards */ if (!c->bluff) continue; /* Check for bad bluff */ if ((!g->fight_element && !(c->icons & ICON_BLUFF_F)) || (g->fight_element && !(c->icons & ICON_BLUFF_E))) { /* Bluff is bad */ bad_bluff = 1; } } /* Set input for a bad bluff */ SET_INPUT(l, n++, bad_bluff); /* Set input for game over */ SET_INPUT(l, n++, g->game_over); /* Set input for fight started */ SET_INPUT(l, n++, g->fight_started); /* Set inputs for fight element (only if started) */ SET_INPUT(l, n++, g->fight_element && g->fight_started); SET_INPUT(l, n++, !g->fight_element && g->fight_started); /* Loop over players */ for (i = 0; i < 2; i++) { /* Get player pointer */ p = &g->p[i]; /* Set input if it is this player's turn */ SET_INPUT(l, n++, g->turn == i && !g->game_over); /* Check for fight started */ if (g->fight_started) { /* Compute power level */ power = compute_power(g, i); } else { /* Assume no power */ power = 0; } /* Loop over possible power values */ for (j = 0; j < 15; j++) { /* Set input if player has this much power */ SET_INPUT(l, n++, power > j); } /* Count active cards */ stack = p->stack[LOC_COMBAT] + p->stack[LOC_SUPPORT]; /* Assume no bluff cards */ bluff = 0; /* Loop over cards */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Check for bluff card */ if (c->bluff) { /* Bluff cards do not count for dragons */ stack--; /* Count bluffs */ bluff++; } } /* Loop over stack sizes */ for (j = 0; j < 8; j++) { /* Set input if player has this many cards played */ SET_INPUT(l, n++, stack > j); } /* Loop over bluff counts */ for (j = 0; j < 4; j++) { /* Set input if player has this many bluffs */ SET_INPUT(l, n++, bluff > j); } /* Count cards in hand */ stack = p->stack[LOC_HAND]; /* Loop over hand sizes */ for (j = 0; j < 10; j++) { /* Set input if player has this many cards */ SET_INPUT(l, n++, stack > j); } /* Count cards in draw deck and hand */ stack = p->stack[LOC_DRAW] + p->stack[LOC_HAND]; /* Loop over deck sizes */ for (j = 0; j < 30; j++) { /* Set input if player has this many cards */ SET_INPUT(l, n++, stack > j); } /* Assume no characters */ stack = 0; /* Loop over deck */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Skip non-characters */ if (c->d_ptr->type != TYPE_CHARACTER) continue; /* Skip cards not in hand */ if (c->where == LOC_HAND && (who == i || c->loc_known) && !c->random_fake) { /* Add more character */ stack++; } } /* Loop over character counts */ for (j = 0; j < 5; j++) { /* Set input if player has this many characters */ SET_INPUT(l, n++, stack > j); } /* Assume no undisclosed cards */ stack = 0; /* Loop over deck */ for (j = 1; j < DECK_SIZE; j++) { /* Get card pointer */ c = &p->deck[j]; /* Skip cards not in hand */ if (c->where != LOC_HAND) continue; /* Skip disclosed cards */ if (c->disclosed) continue; /* Count undisclosed cards */ stack++; } /* Loop over disclose counts */ for (j = 0; j < 6; j++) { /* Set input if player has this cards disclosed */ SET_INPUT(l, n++, stack > j); } /* Set input if player is first to run out of cards */ SET_INPUT(l, n++, p->no_cards); /* Loop over dragon counts */ for (j = 0; j < 3; j++) { /* Set input if player has this many dragons */ SET_INPUT(l, n++, p->dragons > j); } /* Set input if player has instant victory */ SET_INPUT(l, n++, p->instant_win); } /* Compute network value */ compute_net(l); #ifdef DEBUG /* Print score and path to get here */ if (verbose && !checking_retreat && best_path_pos > 0) { printf("%.12lf: ", l->win_prob[who]); for (i = 0; i <= best_path_pos; i++) { action a; a = cur_path[i]; switch (a.act) { case ACT_NONE: break; case ACT_RETREAT: printf("Retreat "); break; case ACT_RETRIEVE: printf("Retrieve %s ", a.arg->name); break; case ACT_PLAY: printf("Play %s ", a.arg->name); break; case ACT_PLAY_NO: printf("Play (no) %s ", a.arg->name); break; case ACT_ANN_FIRE: printf("Announce fire "); break; case ACT_ANN_EARTH: printf("Announce earth "); break; case ACT_USE: printf("Use %s ", a.arg->name); break; case ACT_SATISFY: printf("Satsify %s ", a.arg->name); break; case ACT_CHOOSE: printf("Choose %d ", a.chosen); break; case ACT_LAND: printf("Land %s ", a.arg->name); break; case ACT_LOAD: printf("Load %s on %s ", a.arg->name, a.target->name); break; case ACT_BLUFF: printf("Bluff %s ", a.arg->name); break; case ACT_REVEAL: printf("Reveal %s ", a.arg->name); break; } } if (must_retreat) printf("Force retreat"); if (checking_decline) { printf("Responding %s", g->fight_element ? "earth" : "fire"); } printf("\n"); } #endif /* Return output */ return l->win_prob[who]; }
/// initialise all I/O - set pins as input or output, turn off unused subsystems, etc void io_init(void) { // disable modules we don't use #ifdef PRR PRR = MASK(PRTWI) | MASK(PRADC) | MASK(PRSPI); #elif defined PRR0 PRR0 = MASK(PRTWI) | MASK(PRADC) | MASK(PRSPI); #if defined(PRUSART3) // don't use USART2 or USART3- leave USART1 for GEN3 and derivatives PRR1 |= MASK(PRUSART3) | MASK(PRUSART2); #endif #if defined(PRUSART2) // don't use USART2 or USART3- leave USART1 for GEN3 and derivatives PRR1 |= MASK(PRUSART2); #endif #endif ACSR = MASK(ACD); // setup I/O pins // X Stepper WRITE(X_STEP_PIN, 0); SET_OUTPUT(X_STEP_PIN); WRITE(X_DIR_PIN, 0); SET_OUTPUT(X_DIR_PIN); #ifdef X_MIN_PIN SET_INPUT(X_MIN_PIN); #ifdef USE_INTERNAL_PULLUPS WRITE(X_MIN_PIN, 1); #else WRITE(X_MIN_PIN, 0); #endif #endif #ifdef X_MAX_PIN SET_INPUT(X_MAX_PIN); #ifdef USE_INTERNAL_PULLUPS WRITE(X_MAX_PIN, 1); #else WRITE(X_MAX_PIN, 0); #endif #endif // Y Stepper WRITE(Y_STEP_PIN, 0); SET_OUTPUT(Y_STEP_PIN); WRITE(Y_DIR_PIN, 0); SET_OUTPUT(Y_DIR_PIN); #ifdef Y_MIN_PIN SET_INPUT(Y_MIN_PIN); #ifdef USE_INTERNAL_PULLUPS WRITE(Y_MIN_PIN, 1); #else WRITE(Y_MIN_PIN, 0); #endif #endif #ifdef Y_MAX_PIN SET_INPUT(Y_MAX_PIN); #ifdef USE_INTERNAL_PULLUPS WRITE(Y_MAX_PIN, 1); #else WRITE(Y_MAX_PIN, 0); #endif #endif // Z Stepper #if defined Z_STEP_PIN && defined Z_DIR_PIN WRITE(Z_STEP_PIN, 0); SET_OUTPUT(Z_STEP_PIN); WRITE(Z_DIR_PIN, 0); SET_OUTPUT(Z_DIR_PIN); #endif #ifdef Z_MIN_PIN SET_INPUT(Z_MIN_PIN); #ifdef USE_INTERNAL_PULLUPS WRITE(Z_MIN_PIN, 1); #else WRITE(Z_MIN_PIN, 0); #endif #endif #ifdef Z_MAX_PIN SET_INPUT(Z_MAX_PIN); #ifdef USE_INTERNAL_PULLUPS WRITE(Z_MAX_PIN, 1); #else WRITE(Z_MAX_PIN, 0); #endif #endif #if defined E_STEP_PIN && defined E_DIR_PIN WRITE(E_STEP_PIN, 0); SET_OUTPUT(E_STEP_PIN); WRITE(E_DIR_PIN, 0); SET_OUTPUT(E_DIR_PIN); #endif // Common Stepper Enable #ifdef STEPPER_ENABLE_PIN #ifdef STEPPER_INVERT_ENABLE WRITE(STEPPER_ENABLE_PIN, 0); #else WRITE(STEPPER_ENABLE_PIN, 1); #endif SET_OUTPUT(STEPPER_ENABLE_PIN); #endif // X Stepper Enable #ifdef X_ENABLE_PIN #ifdef X_INVERT_ENABLE WRITE(X_ENABLE_PIN, 0); #else WRITE(X_ENABLE_PIN, 1); #endif SET_OUTPUT(X_ENABLE_PIN); #endif // Y Stepper Enable #ifdef Y_ENABLE_PIN #ifdef Y_INVERT_ENABLE WRITE(Y_ENABLE_PIN, 0); #else WRITE(Y_ENABLE_PIN, 1); #endif SET_OUTPUT(Y_ENABLE_PIN); #endif // Z Stepper Enable #ifdef Z_ENABLE_PIN #ifdef Z_INVERT_ENABLE WRITE(Z_ENABLE_PIN, 0); #else WRITE(Z_ENABLE_PIN, 1); #endif SET_OUTPUT(Z_ENABLE_PIN); #endif // E Stepper Enable #ifdef E_ENABLE_PIN #ifdef E_INVERT_ENABLE WRITE(E_ENABLE_PIN, 0); #else WRITE(E_ENABLE_PIN, 1); #endif SET_OUTPUT(E_ENABLE_PIN); #endif // setup PWM timers: fast PWM, no prescaler TCCR0A = MASK(WGM01) | MASK(WGM00); // PWM frequencies in TCCR0B, see page 108 of the ATmega644 reference. TCCR0B = MASK(CS00); // F_CPU / 256 (about 78(62.5) kHz on a 20(16) MHz chip) //TCCR0B = MASK(CS01); // F_CPU / 256 / 8 (about 9.8(7.8) kHz) //TCCR0B = MASK(CS00) | MASK(CS01); // F_CPU / 256 / 64 (about 1220(977) Hz) //TCCR0B = MASK(CS02); // F_CPU / 256 / 256 (about 305(244) Hz) #ifndef FAST_PWM TCCR0B = MASK(CS00) | MASK(CS02); // F_CPU / 256 / 1024 (about 76(61) Hz) #endif TIMSK0 = 0; OCR0A = 0; OCR0B = 0; TCCR2A = MASK(WGM21) | MASK(WGM20); // PWM frequencies in TCCR2B, see page 156 of the ATmega644 reference. TCCR2B = MASK(CS20); // F_CPU / 256 (about 78(62.5) kHz on a 20(16) MHz chip) //TCCR2B = MASK(CS21); // F_CPU / 256 / 8 (about 9.8(7.8) kHz) //TCCR2B = MASK(CS20) | MASK(CS21); // F_CPU / 256 / 32 (about 2.4(2.0) kHz) //TCCR2B = MASK(CS22); // F_CPU / 256 / 64 (about 1220(977) Hz) //TCCR2B = MASK(CS20) | MASK(CS22); // F_CPU / 256 / 128 (about 610(488) Hz) //TCCR2B = MASK(CS21) | MASK(CS22); // F_CPU / 256 / 256 (about 305(244) Hz) #ifndef FAST_PWM TCCR2B = MASK(CS20) | MASK(CS21) | MASK(CS22); // F_CPU / 256 / 1024 #endif TIMSK2 = 0; OCR2A = 0; OCR2B = 0; #ifdef TCCR3A TCCR3A = MASK(WGM30); TCCR3B = MASK(WGM32) | MASK(CS30); TIMSK3 = 0; OCR3A = 0; OCR3B = 0; #endif #ifdef TCCR4A TCCR4A = MASK(WGM40); TCCR4B = MASK(WGM42) | MASK(CS40); TIMSK4 = 0; OCR4A = 0; OCR4B = 0; #endif #ifdef TCCR5A TCCR5A = MASK(WGM50); TCCR5B = MASK(WGM52) | MASK(CS50); TIMSK5 = 0; OCR5A = 0; OCR5B = 0; #endif #ifdef STEPPER_ENABLE_PIN power_off(); #endif // set all heater pins to output do { #undef DEFINE_HEATER #define DEFINE_HEATER(name, pin) WRITE(pin, 0); SET_OUTPUT(pin); #include "config.h" #undef DEFINE_HEATER } while (0); #ifdef TEMP_MAX6675 // setup SPI WRITE(SCK, 0); SET_OUTPUT(SCK); WRITE(MOSI, 1); SET_OUTPUT(MOSI); WRITE(MISO, 1); SET_INPUT(MISO); WRITE(SS, 1); SET_OUTPUT(SS); #endif #ifdef TEMP_INTERCOM // Enable the RS485 transceiver SET_OUTPUT(RX_ENABLE_PIN); SET_OUTPUT(TX_ENABLE_PIN); WRITE(RX_ENABLE_PIN,0); disable_transmit(); #endif }
void Endstops::init() { #if HAS_X_MIN SET_INPUT(X_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_XMIN) WRITE(X_MIN_PIN,HIGH); #endif #endif #if HAS_Y_MIN SET_INPUT(Y_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_YMIN) WRITE(Y_MIN_PIN,HIGH); #endif #endif #if HAS_Z_MIN SET_INPUT(Z_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_ZMIN) WRITE(Z_MIN_PIN,HIGH); #endif #endif #if HAS_Z2_MIN SET_INPUT(Z2_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_ZMIN) WRITE(Z2_MIN_PIN,HIGH); #endif #endif #if HAS_X_MAX SET_INPUT(X_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_XMAX) WRITE(X_MAX_PIN,HIGH); #endif #endif #if HAS_Y_MAX SET_INPUT(Y_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_YMAX) WRITE(Y_MAX_PIN,HIGH); #endif #endif #if HAS_Z_MAX SET_INPUT(Z_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_ZMAX) WRITE(Z_MAX_PIN,HIGH); #endif #endif #if HAS_Z2_MAX SET_INPUT(Z2_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_ZMAX) WRITE(Z2_MAX_PIN,HIGH); #endif #endif #if ENABLED(Z_MIN_PROBE_ENDSTOP) SET_INPUT(Z_MIN_PROBE_PIN); #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE) WRITE(Z_MIN_PROBE_PIN,HIGH); #endif #endif } // Endstops::init
// ------------------------------------------------------------------------- uint8_t mcp2515_init(uint8_t speed) { SET(MCP2515_CS); SET_OUTPUT(MCP2515_CS); RESET(P_SCK); RESET(P_MOSI); RESET(P_MISO); SET_OUTPUT(P_SCK); SET_OUTPUT(P_MOSI); SET_INPUT(P_MISO); SET_INPUT(MCP2515_INT); SET(MCP2515_INT); // active SPI master interface SPCR = (1<<SPE)|(1<<MSTR) | (0<<SPR1)|(1<<SPR0); SPSR = 0; // reset MCP2515 by software reset. // After this he is in configuration mode. RESET(MCP2515_CS); spi_putc(SPI_RESET); SET(MCP2515_CS); // wait a little bit until the MCP2515 has restarted _delay_us(10); // load CNF1..3 Register RESET(MCP2515_CS); spi_putc(SPI_WRITE); spi_putc(CNF3); /* spi_putc((1<<PHSEG21)); // Bitrate 125 kbps at 16 MHz spi_putc((1<<BTLMODE)|(1<<PHSEG11)); spi_putc((1<<BRP2)|(1<<BRP1)|(1<<BRP0)); */ /* spi_putc((1<<PHSEG21)); // Bitrate 250 kbps at 16 MHz spi_putc((1<<BTLMODE)|(1<<PHSEG11)); spi_putc((1<<BRP1)|(1<<BRP0)); */ spi_putc((1<<PHSEG21)); // Bitrate 250 kbps at 16 MHz spi_putc((1<<BTLMODE)|(1<<PHSEG11)); //spi_putc(1<<BRP0); spi_putc(speed); // activate interrupts spi_putc((1<<RX1IE)|(1<<RX0IE)); SET(MCP2515_CS); // test if we could read back the value => is the chip accessible? if (mcp2515_read_register(CNF1) != speed) { SET(LED2_HIGH); return false; } // deaktivate the RXnBF Pins (High Impedance State) mcp2515_write_register(BFPCTRL, 0); // set TXnRTS as inputs mcp2515_write_register(TXRTSCTRL, 0); // turn off filters => receive any message mcp2515_write_register(RXB0CTRL, (1<<RXM1)|(1<<RXM0)); mcp2515_write_register(RXB1CTRL, (1<<RXM1)|(1<<RXM0)); // reset device to normal mode mcp2515_write_register(CANCTRL, 0); // SET(LED2_HIGH); return true; }
void Endstops::init() { #if HAS_X_MIN SET_INPUT(X_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_XMIN) WRITE(X_MIN_PIN,HIGH); #endif #endif #if HAS_Y_MIN SET_INPUT(Y_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_YMIN) WRITE(Y_MIN_PIN,HIGH); #endif #endif #if HAS_Z_MIN SET_INPUT(Z_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_ZMIN) WRITE(Z_MIN_PIN,HIGH); #endif #endif #if HAS_Z2_MIN SET_INPUT(Z2_MIN_PIN); #if ENABLED(ENDSTOPPULLUP_ZMIN) WRITE(Z2_MIN_PIN,HIGH); #endif #endif #if HAS_X_MAX SET_INPUT(X_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_XMAX) WRITE(X_MAX_PIN,HIGH); #endif #endif #if HAS_Y_MAX SET_INPUT(Y_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_YMAX) WRITE(Y_MAX_PIN,HIGH); #endif #endif #if HAS_Z_MAX SET_INPUT(Z_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_ZMAX) WRITE(Z_MAX_PIN,HIGH); #endif #endif #if HAS_Z2_MAX SET_INPUT(Z2_MAX_PIN); #if ENABLED(ENDSTOPPULLUP_ZMAX) WRITE(Z2_MAX_PIN,HIGH); #endif #endif #if HAS_Z_MIN_PROBE_PIN && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used. SET_INPUT(Z_MIN_PROBE_PIN); #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE) WRITE(Z_MIN_PROBE_PIN,HIGH); #endif #endif } // Endstops::init