int hpg_pwmgen_init(hal_pru_generic_t *hpg){ int r,i; if (hpg->config.num_pwmgens <= 0) return 0; rtapi_print("hpg_pwm_init\n"); // FIXME: Support multiple PWMs like so: num_pwmgens=3,4,2,5 // hpg->pwmgen.num_instances = hpg->config.num_pwmgens; hpg->pwmgen.num_instances = 1; // Allocate HAL shared memory for instance state data hpg->pwmgen.instance = (hpg_pwmgen_instance_t *) hal_malloc(sizeof(hpg_pwmgen_instance_t) * hpg->pwmgen.num_instances); if (hpg->pwmgen.instance == 0) { HPG_ERR("ERROR: hal_malloc() failed\n"); return -1; } // Clear memory memset(hpg->pwmgen.instance, 0, (sizeof(hpg_pwmgen_instance_t) * hpg->pwmgen.num_instances) ); for (i=0; i < hpg->pwmgen.num_instances; i++) { // FIXME: Support multiple PWMs like so: num_pwmgens=3,4,2,5 hpg->pwmgen.instance[i].num_outputs = hpg->config.num_pwmgens; // Allocate HAL shared memory for output state data hpg->pwmgen.instance[i].out = (hpg_pwmgen_output_instance_t *) hal_malloc(sizeof(hpg_pwmgen_output_instance_t) * hpg->pwmgen.instance[i].num_outputs); if (hpg->pwmgen.instance[i].out == 0) { HPG_ERR("ERROR: hal_malloc() failed\n"); return -1; } int len = sizeof(hpg->pwmgen.instance[i].pru) + (sizeof(PRU_pwm_output_t) * hpg->pwmgen.instance[i].num_outputs); hpg->pwmgen.instance[i].task.addr = pru_malloc(hpg, len); hpg->pwmgen.instance[i].pru.task.hdr.mode = eMODE_PWM; pru_task_add(hpg, &(hpg->pwmgen.instance[i].task)); if ((r = export_pwmgen(hpg,i)) != 0){ HPG_ERR("ERROR: failed to export pwmgen %i: %i\n",i,r); return -1; } } return 0; }
int rtapi_app_main(void) { int n, retval; for (n = 0; n < MAX_CHAN && output_type[n] != -1 ; n++) { if ((output_type[n] > MAX_OUTPUT_TYPE) || (output_type[n] < 0)) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: bad output type '%i', channel %i\n", output_type[n], n); return -1; } else { num_chan++; } } if (num_chan == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: no channels configured\n"); return -1; } /* periodns will be set to the proper value when 'make_pulses()' runs for the first time. We load a default value here to avoid glitches at startup */ periodns = 50000; /* have good config info, connect to the HAL */ comp_id = hal_init("pwmgen"); if (comp_id < 0) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: hal_init() failed\n"); return -1; } /* allocate shared memory for generator data */ pwmgen_array = hal_malloc(num_chan * sizeof(pwmgen_t)); if (pwmgen_array == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: hal_malloc() failed\n"); hal_exit(comp_id); return -1; } /* export all the variables for each PWM generator */ for (n = 0; n < num_chan; n++) { /* export all vars */ retval = export_pwmgen(n, &(pwmgen_array[n]), output_type[n]); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: pwmgen %d var export failed\n", n); hal_exit(comp_id); return -1; } } /* export functions */ retval = hal_export_funct("pwmgen.make-pulses", make_pulses, pwmgen_array, 0, 0, comp_id); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: makepulses funct export failed\n"); hal_exit(comp_id); return -1; } retval = hal_export_funct("pwmgen.update", update, pwmgen_array, 1, 0, comp_id); if (retval != 0) { rtapi_print_msg(RTAPI_MSG_ERR, "PWMGEN: ERROR: update funct export failed\n"); hal_exit(comp_id); return -1; } rtapi_print_msg(RTAPI_MSG_INFO, "PWMGEN: installed %d PWM/PDM generators\n", num_chan); hal_ready(comp_id); return 0; }