/* * Start printing */ int unicorn_start(int fd_rd, int fd_wr, int fd_rd_emerg) { /* Turn on fans */ printf("unicorn_start\n"); channel_tag fan = NULL; fan = fan_lookup_by_name("fan_3"); if (fan) { fan_set_level(fan, DEFAULT_FAN_MAX_LEVEL); fan_enable(fan); } fan = fan_lookup_by_name("fan_4"); if (fan) { fan_set_level(fan, DEFAULT_FAN_MAX_LEVEL); fan_enable(fan); } heater_start(); plan_start(); stepper_start(); gcode_start(fd_rd, fd_wr, fd_rd_emerg); if (hPause_printing) { Pause_off(hPause_printing); } printf("unicorn_start ok!!!\n"); return 0; }
/* * Restore un-controlled fans state on exit. */ void restore_fans(void) { struct fan_control *current_control; current_control = fan_controls_head; while(current_control) { if(fan_enable(current_control->address, 0)) { fan_enable(current_control->address, 1); set_fan_pwm(current_control, 255); } current_control = current_control->next; } }
int unicorn_init(void) { channel_tag fan = NULL; int ret = 0; board_info_t board_info; printf("unicorn_init...\n"); ret = eeprom_read_board_info(EEPROM_DEV, &board_info); if (ret < 0) { printf("Read eeprom board info failed\n"); return -1; } if (!strncasecmp("bbp1s", board_info.name, 5)) { bbp_board_type = BOARD_BBP1S; } else if(!strncasecmp("bbp1", board_info.name, 4)){ bbp_board_type = BOARD_BBP1; } printf("board name:%s, version:%s, get board type:%d \n", board_info.name, board_info.version, bbp_board_type); /* * Create fifo * fifo_plan2st, fifo_st2plan * fifo_plan2gc, fifo_gc2plan */ Fifo_Attrs fAttrs = Fifo_Attrs_DEFAULT; hFifo_plan2st = Fifo_create(&fAttrs); hFifo_st2plan = Fifo_create(&fAttrs); if ((hFifo_st2plan == NULL) || (hFifo_plan2st == NULL)) { printf("Create Fifo failed\n"); return -1; } Pause_Attrs pAttrs = Pause_Attrs_DEFAULT; hPause_printing = Pause_create(&pAttrs); if (hPause_printing == NULL) { printf("Create pause err\n"); return -1; } else { Pause_on(hPause_printing); } /* set up unicorn system */ ret = unicorn_setup(); if (ret < 0) { return ret; } /* init sub systems of unicorn */ ret = parameter_init(EEPROM_DEV); if (ret < 0) { printf("parameter_init failed\n"); return ret; } ret = analog_init(); if (ret < 0) { printf("analog_init failed\n"); return ret; } ret = temp_init(); if (ret < 0) { printf("temp_init failed\n"); return ret; } ret = pwm_init(); if (ret < 0) { printf("pwm_init failed\n"); return ret; } ret = fan_init(); if (ret < 0) { printf("fan_init failed\n"); return ret; } ret = heater_init(); if (ret < 0) { printf("heater_init failed\n"); return ret; } #ifdef SERVO if (bbp_board_type == BOARD_BBP1S) { ret = servo_init(); if (ret < 0) { printf("servo_init failed\n"); return ret; } } #endif ret = lmsw_init(); if (ret < 0) { printf("lmsw_init failed\n"); return ret; } ret = plan_init(); if (ret < 0) { printf("plan_init failed\n"); return ret; } ret = stepper_init(); if (ret < 0) { printf("stepper_init failed\n"); return ret; } ret = gcode_init(); if (ret < 0) { printf("gcode_init failed\n"); return ret; } fan = fan_lookup_by_name("fan_3"); if (fan) { fan_set_level(fan, DEFAULT_FAN_MAX_LEVEL); fan_enable(fan); } fan = fan_lookup_by_name("fan_4"); if (fan) { fan_enable(fan); fan_set_level(fan, DEFAULT_FAN_MAX_LEVEL); } fan = fan_lookup_by_name("fan_5"); if (fan) { fan_enable(fan); fan_set_level(fan, DEFAULT_FAN_MAX_LEVEL); } fan = fan_lookup_by_name("fan_6"); if (fan) { fan_enable(fan); fan_set_level(fan, DEFAULT_FAN_MAX_LEVEL); } printf("unicorn_init ok!!!\n"); return ret; }
int unicorn_test(void) { int i = 0; int idx = 0; int num_fans = 0; double celsius = 0.0; channel_tag heater, fan, pwm; if (bbp_board_type == BOARD_BBP1S) { num_fans = 6; } else if (bbp_board_type == BOARD_BBP1) { num_fans = 5; } #if 0 start_test_thread(); int count = 10; while (1) { printf("wait evtout count=%d\n", count); prussdrv_pru_wait_event(PRU_EVTOUT_1); prussdrv_pru_clear_event(PRU_EVTOUT_1, PRU1_ARM_INTERRUPT); if(count-- == 0){ return; } } #endif printf("Test 1: Fan testing...\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 1: Fan testing...\n"); printf(" Please watch the led lights and leds on board\n"); printf(" 1.1 close all fans\n"); for (idx = 0; idx < num_fans; idx++) { fan = fan_lookup_by_index(idx); if (fan) { fan_disable(fan); } } printf(" 1.2 open each fan for 1s then close one by one\n"); for (idx = 0; idx < num_fans; idx++) { fan = fan_lookup_by_index(idx); if (fan) { printf(" Open %s\n", tag_name(fan)); fan_enable(fan); fan_set_level(fan, 80); sleep(1); printf(" Close %s\n", tag_name(fan)); fan_set_level(fan, 0); fan_disable(fan); } } printf(" 1.3 turn on all fans for 1s, then turn off, repeat 3 times\n"); for (i = 0; i < 2; i++) { for (idx = 0; idx < num_fans; idx++) { fan = fan_lookup_by_index(idx); if (fan) { fan_enable(fan); fan_set_level(fan, 80); } } sleep(1); for (idx = 0; idx < num_fans; idx++) { fan = fan_lookup_by_index(idx); if (fan) { fan_disable(fan); } } sleep(1); } printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 2: LMSW testing...\n"); printf(" 2.1 Please press min x: \n"); wait_lmsw_min_test(X_AXIS); printf(" min_x ok\n"); printf(" 2.2 Please press min y: \n"); wait_lmsw_min_test(Y_AXIS); printf(" min_y ok\n"); printf(" 2.3 Please press min z: \n"); wait_lmsw_min_test(Z_AXIS); printf(" min_z ok\n"); printf(" 2.4 Please press max x: \n"); wait_lmsw_max(X_AXIS); printf(" max_x ok\n"); printf(" 2.5 Please press max y: \n"); wait_lmsw_max(Y_AXIS); printf(" max_y ok\n"); printf(" 2.6 Please press max z: \n"); wait_lmsw_max(Z_AXIS); printf(" max_z ok\n"); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 3: Heater testing...\n"); printf(" open each heater for 1s then close\n"); printf(" Please watch the led beside the heater interface!\n"); if (bbp_board_type == BOARD_BBP1S) { pwm = pwm_lookup_by_name("pwm_ext"); if (pwm) { pwm_set_output(pwm, 80); } sleep(1); pwm_set_output(pwm, 0); sleep(1); pwm = pwm_lookup_by_name("pwm_ext2"); if (pwm) { pwm_set_output(pwm, 80); } sleep(1); pwm_set_output(pwm, 0); sleep(1); pwm = pwm_lookup_by_name("pwm_bed"); if (pwm) { pwm_set_output(pwm, 80); } sleep(1); pwm_set_output(pwm, 0); sleep(1); for (i = 0; i < 2; i++) { pwm = pwm_lookup_by_name("pwm_ext"); if (pwm) { pwm_set_output(pwm, 80); } pwm = pwm_lookup_by_name("pwm_ext2"); if (pwm) { pwm_set_output(pwm, 80); } pwm = pwm_lookup_by_name("pwm_bed"); if (pwm) { pwm_set_output(pwm, 80); } sleep(4); pwm = pwm_lookup_by_name("pwm_ext"); if (pwm) { pwm_set_output(pwm, 0); } pwm = pwm_lookup_by_name("pwm_ext2"); if (pwm) { pwm_set_output(pwm, 0); } pwm = pwm_lookup_by_name("pwm_bed"); if (pwm) { pwm_set_output(pwm, 0); } sleep(2); } } if (bbp_board_type == BOARD_BBP1) { for (idx = 0; idx < NUM_HEATERS; idx++) { heater = heater_lookup_by_index(idx); if (heater) { printf(" Open %s\n", tag_name(heater)); heater_enable(heater); heater_set_raw_pwm(heater, 40); sleep(5); printf(" Close %s\n", tag_name(heater)); heater_set_raw_pwm(heater, 0); heater_disable(heater); } } } printf("done.\n"); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 4: Stepper testing...\n"); printf(" 4.1 Turn on all stepper\n"); stepper_test(); printf(" 4.2 Stop Stepper\n"); printf(" Please press min x to stop stepper\n"); wait_lmsw_min(X_AXIS); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 5: USB Host testing...\n"); printf(" 5.1 please insert U mass storage\n"); if(unicorn_test_usb_storage() == -1){ halt_test("Usb mass storage is BAD!!!!!!!!!!!!!!!\n"); } printf("Usb mass storage is OK\n"); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 6: Internal emmc testing...\n"); if(unicorn_test_emmc() == -1){ halt_test("Internal emmc is BAD!!!!!!!!!!!!!!\n"); } printf("Internal emmc is OK\n"); printf("done.\n"); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 7: eeprom testing...\n"); if(unicorn_test_eeprom() == -1){ halt_test("eeprom is BAD!!!!!!!!!!!!\n"); } printf("eeprom is OK\n"); printf("done.\n"); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 8: rtc testing...\n"); if(unicorn_test_rtc() == -1) { halt_test("rtc is BAD!!!!!!!!!!!!\n"); } printf("rtc is OK\n"); printf("done.\n"); printf("-------------------------------------------------------------\n"); printf("\n-------------------------------------------------------------\n"); printf("Test 9: Network testing...\n"); printf(" Start to ping the router..\n"); if(unicorn_test_network() == -1) { halt_test("network is BAD!!!!!!!!!!!!\n"); } printf("done.\n"); printf("-------------------------------------------------------------\n"); printf("Test 10: USB OTG testing...\n"); printf(" 11.1 Please connect usb otg line to PC\n"); printf(" 11.2 Could you see the boot partion? If yes, Please press min_x..\n"); wait_lmsw_min_test(X_AXIS); printf("done.\n"); printf("-------------------------------------------------------------\n"); if (bbp_board_type == BOARD_BBP1S) { printf("Test 11: Test max6675 temperature...\n"); printf("long press min y key to exit\n"); if(unicorn_test_max6675() == -1) { halt_test("max6675 is BAD!!!!!!!!!!!!\n"); } printf("\n-------------------------------------------------------------\n"); } #if 0 if (bbp_board_type == BOARD_BBP1S) { #ifdef SERVO printf("Test 12: Test servo...\n"); if (unicorn_test_servo()) { halt_test("servo is BAD!!!!!!!!!!!!\n"); } printf("\n-------------------------------------------------------------\n"); #endif } #endif printf("\n-------------------------------------------------------------\n"); printf("Test 12: Temp adc testing...\n"); heater_start(); if (bbp_board_type == BOARD_BBP1S) { char *heater_array_bbp1s[] = {"heater_ext", "heater_ext2", "heater_bed"}; for (idx = 0; idx < sizeof(heater_array_bbp1s)/sizeof(heater_array_bbp1s[0]); idx++) { heater = heater_lookup_by_name(heater_array_bbp1s[idx]); if (heater) { heater_enable(heater); heater_set_setpoint(heater, TARGET_TEMP); } } for (i = 0; i < 80 && stepper_check_lmsw(X_AXIS) == 0; i++) { for (idx = 0; idx < sizeof(heater_array_bbp1s)/sizeof(heater_array_bbp1s[0]); idx++) { heater = heater_lookup_by_name(heater_array_bbp1s[idx]); if (heater) { celsius = 0.0; heater_get_celsius(heater, &celsius); printf("%s -> %f\n", tag_name(heater), celsius); sleep(1); } } } } else if (bbp_board_type == BOARD_BBP1) { for (idx = 0; idx < NUM_HEATERS; idx++) { heater = heater_lookup_by_index(idx); if (heater) { heater_enable(heater); heater_set_setpoint(heater, TARGET_TEMP); } } printf("long press min x key to exit\n"); heater = heater_lookup_by_name("heater_bed"); for (i = 0; i < 80 && stepper_check_lmsw(X_AXIS) == 0; i++) { if (heater_temp_reached(heater)) { printf("ok\n"); break; } else { sleep(1); } for (idx = 0; idx < NUM_HEATERS; idx++) { heater = heater_lookup_by_index(idx); if (heater) { celsius = 0.0; heater_get_celsius(heater, &celsius); printf("%s -> %f\n", tag_name(heater), celsius); } } printf("\n"); } } heater_stop(); printf("done.\n"); printf("-------------------------------------------------------------\n"); halt_test("All is Good!\n"); return 0; }
/* * main */ int main(int argc, char **argv) { int configfile; int nlines, i, keyword; char *configfilename, *configbuffer, *p, **lines; int interval = 10; int dev_mismatch = 0; struct stat statbuf; /* Read configuration file. */ if(argc > 1) configfilename = argv[1]; else configfilename = "/etc/fancontrol"; configfile = open(configfilename, O_RDONLY); if(configfile < 0) { perror(configfilename); return 1; } if(fstat(configfile, &statbuf) < 0) { perror(configfilename); close(configfile); return 1; } if(statbuf.st_size <= 0) { fprintf(stderr, "Configuration file %s is empty.\n", configfilename); close(configfile); return 1; } configbuffer = (char*) malloc(statbuf.st_size + 1); if(configbuffer == NULL) { fprintf(stderr, "Insufficient memory.\n"); close(configfile); return 0; } if(read_whole_persist(configfile, configbuffer, statbuf.st_size) != statbuf.st_size) { fprintf(stderr, "Can't read the configuration file %s.\n", configfilename); close(configfile); return 1; } configbuffer[statbuf.st_size] = '\0'; close(configfile); /* Parse configuration. */ p = configbuffer; nlines = 1; while(*p) if(*p++ == '\n') nlines++; lines = (char**) malloc(nlines * sizeof(char*)); if(lines == NULL) { fprintf(stderr, "Insufficient memory.\n"); free(configbuffer); return 1; } for(p = configbuffer, i = 0; i < nlines ; i++) { lines[i] = p; p = strchr(p, '\n'); if(p != NULL) *p++ = '\0'; } /* Parse lines, create descriptors. */ for(i = 0; i < nlines; i++) { p = strchr(lines[i], '#'); if(p != NULL) *p = '\0'; p = strchr(lines[i], '='); if(p != NULL) { *p = '\0'; p++; keyword = find_keyword(lines[i]); switch(keyword) { case INTERVAL: if((sscanf(p, "%i", &interval) != 1) || (interval < 1) || (interval > 100)) interval = 10; break; case DEVPATH: dev_mismatch |= check_devpaths(p); break; case DEVNAME: dev_mismatch |= check_devnames(p); break; case FCTEMPS: configure_fctemps(p); break; case FCFANS: configure_fcfans(p); break; case MINTEMP: case MAXTEMP: configure_common_param(p, keyword); break; case MINSTART: case MINSTOP: case MINPWM: case MAXPWM: configure_fan_control_param(p, keyword); break; } } } if(dev_mismatch) { fprintf(stderr, "Devices do not match saved paths and names, exiting.\n"); return 1; } /* Do not try to run if configuration does not contain at least one sensor and at least one control. */ if((temp_sensors_head == NULL) || (fan_controls_head == NULL)) { fprintf(stderr, "No devices defined, exiting.\n"); return 1; } /* Set handler for restoring the fans uncontrolled state */ struct sigaction act; memset(&act, 0, sizeof(act)); act.sa_handler = sighandler; sigaction(SIGTERM, &act, NULL); sigaction(SIGINT, &act, NULL); sigaction(SIGQUIT, &act, NULL); /* Enable fan control */ struct fan_control *current_control; current_control = fan_controls_head; while(current_control) { fan_enable(current_control->address, 1); current_control = current_control->next; } /* Main loop. */ while(running) { /* Read all temperatures. */ struct temp_sensor *current_temp_sensor; current_temp_sensor = temp_sensors_head; while(current_temp_sensor) { read_temperature(current_temp_sensor); current_temp_sensor = current_temp_sensor->next; } /* Read all fan speeds */ struct fan_sensor *current_fan_sensor; current_fan_sensor = fan_sensors_head; while(current_fan_sensor) { read_fan_speed(current_fan_sensor); current_fan_sensor = current_fan_sensor->next; } /* Read current PWM value for controls, determine and set new PWM values. */ current_control = fan_controls_head; while(current_control) { int min_fan_speed, new_pwm; read_fan_pwm(current_control); min_fan_speed = get_min_fan_speed(current_control); new_pwm = get_max_pwm_by_temperature(current_control); /* Check if we are starting a stopped fan */ if((new_pwm > current_control->minpwm) && ((min_fan_speed == 0) || (current_control->value <= current_control->minpwm))) new_pwm = current_control->minstart; /* Set new PWM if it changed */ if(new_pwm != current_control->value) set_fan_pwm(current_control, new_pwm); current_control = current_control->next; } sleep(interval); } /* Restore fans */ restore_fans(); return 0; }