int main(void) { uint8_t val, totval; uint32_t val32=0; char description[48]; int error = 0; flink_dev* dev; flink_subdev* subdev; flink_subdev* sdPWM; dev = flink_open(); subdev = flink_get_subdevice_by_unique_id(dev, 3); // info device error = flink_info_get_description(subdev, description); sdPWM = flink_get_subdevice_by_unique_id(dev, 2); error = flink_pwm_get_baseclock(sdPWM,&val32); error = flink_pwm_set_period(sdPWM,0,1e6); // 50 MHz / 1 MHz = 50 Hz = 20 msec periode error = flink_pwm_set_hightime(sdPWM,0,100e3); // 50 MHz / 100 kHz = 500 Hz = 2 msec high time subdev = flink_get_subdevice_by_unique_id(dev, 1); // GPIO device error = flink_dio_set_direction(subdev, 0x02, 0x01); // LEDR0 error = flink_dio_set_direction(subdev, 0x03, 0x01); // LEDR1 while(1) { error = flink_dio_get_value(subdev,0x00,&val); // SW0 --> LEDR0 totval = val; error = flink_dio_set_value(subdev,0x02,val); error = flink_dio_get_value(subdev,0x01,&val); // SW1 --> LEDR1 totval += val<<1; error = flink_dio_set_value(subdev,0x03,val); switch(totval) { case 0: error = flink_pwm_set_hightime(sdPWM,0,100e3); // 2.00 msec high time break; case 1: error = flink_pwm_set_hightime(sdPWM,0,83e3); // 1.67 msec high time break; case 2: error = flink_pwm_set_hightime(sdPWM,0,67e3); // 1.34 msec high time break; case 3: error = flink_pwm_set_hightime(sdPWM,0,50e3); // 1.00 msec high time break; default: break; } // switch usleep(50000); } // while(1) return 0; } // main
int main(int argc, char* argv[]) { flink_dev* dev; flink_subdev* subdev; char* dev_name = DEFAULT_DEV; uint8_t subdevice_id = 0; int error = 0,e; char str[29]; // Error message if long dashes (en dash) are used int i; for (i=0; i < argc; i++) { if ((argv[i][0] == 226) && (argv[i][1] == 128) && (argv[i][2] == 147)) { fprintf(stderr, "Error: Invalid arguments. En dashes are used.\n"); return -1; } } /* Compute command line arguments */ int c; while((c = getopt(argc, argv, "d:s:c:rwhl")) != -1) { switch(c) { case 'd': // device file dev_name = optarg; break; case 's': // subdevice id subdevice_id = atoi(optarg); break; case '?': if(optopt == 'd' || optopt == 's' || optopt == 'c') fprintf(stderr, "Option -%c requires an argument.\n", optopt); else if(isprint(optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); return -1; default: abort(); } } // Open flink device dev = flink_open(dev_name); if(dev == NULL) { fprintf(stderr, "Failed to open device %s!\n", dev_name); return -1; } // Get a pointer to the choosen subdevice subdev = flink_get_subdevice_by_id(dev, subdevice_id); if(subdev == NULL) { fprintf(stderr, "Illegal subdevice id %d!\n", subdevice_id); return -1; } // Read description printf("Reading info device: subdevice %u\n", subdevice_id); error = flink_info_get_description(subdev, str); if(error != 0) { printf("Reading description failed!\n"); return -1; } else { printf("Description: %s\n", str); } // Close flink device flink_close(dev); return EXIT_SUCCESS; }