// filename: coretest.c // // run a c-program on a specific core // // 2024-06-16 ch // // https://github.com/WiringPi/WiringPi // gcc -o coretest coretest.c -l wiringPi /* https://forums.raspberrypi.com/viewtopic.php?t=228727 add this argument to /boot/cmdline.txt: isolcpus=3 This prevents Linux from scheduling processes on core 3. But interrupts still happen on it, and there is an essential kernel task running on it. Still, it is a good start, but not at all sufficient. */ #define _GNU_SOURCE #include #include #include #include #include #include #include // LED Pin - wiringPi pin 0 is BCM_GPIO 17. #define LED 0 #define DELAY_US 100 void togglePin() { static bool flag=false; if(flag)digitalWrite (LED, HIGH) ; else digitalWrite (LED, LOW) ; flag=!flag; } void setup() { printf ("fastPinToggle\n") ; wiringPiSetup (); pinMode (LED, OUTPUT) ; } uint32_t currentTime = 0; uint32_t startTime = 1; void loop() { togglePin(); do { currentTime = micros(); } while ((uint32_t)(currentTime - startTime) <= DELAY_US); startTime = currentTime; } int main() { cpu_set_t cpuset; //int core_id = 1; // Change this to the core you want to run on (0-indexed) int core_id = 3; // Change this to the core you want to run on (0-indexed) CPU_ZERO(&cpuset); CPU_SET(core_id, &cpuset); pid_t pid = getpid(); // Get the PID of the current process if (sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset) == -1) { perror("sched_setaffinity"); exit(EXIT_FAILURE); } // Your program logic here printf("Running on core %d\n", core_id); while (1) { loop(); //sleep(1); } return 0; }