Our simple starting point is to check whether a number is prime.
The first cut was to loop round trying all possible divisors upto the square root of the subject number.
The program was written on an RPi running linux first. Then it was transferred to WSL where we compile using arm-none-eabi-gcc for bare metal. In its simplest form it was quick to implement and test.
The bare metal programs we previously had working were re-organised so that the initial program (notmain.c) sets up uart i/o. It calls a function jmain.c, which is intentionally not called main to avoid the compiler misinterpreting it. jmain.c contains our program code with printf statements replaced by uart_puts to output strings to the serial terminal.
The bare metal programs we previously had working were re-organised so that the initial program (notmain.c) sets up uart i/o. It calls a function jmain.c, which is intentionally not called main to avoid the compiler misinterpreting it. jmain.c contains our program code with printf statements replaced by uart_puts to output strings to the serial terminal.
The second iteration improves the algorithm slightly, don't test even divisors, make test_prime() a function. We also loop to ask the user for prime numbers. We change the linux version of the program so it is easy to convert:
use sprintf(buffer,.......); printf(buffer); if variable values need to be output for uart_puts convesion
use gets(buffer); sscanf(buffer,.....) for input so uart_gets can be added on conversion.
use sprintf(buffer,.......); printf(buffer); if variable values need to be output for uart_puts convesion
use gets(buffer); sscanf(buffer,.....) for input so uart_gets can be added on conversion.
Now we can easily enter and test programs under linux before a minimal conversion to run bare metal.
It is much more appropriate to calculate lots of prime numbers. The program was amended to ask the user now many primes are required. These are then calculated and printed. To allow a flexible number of primes malloc is used to create an array of upto 1,000,000 primes which can then be calculated and printed on the screen.
Previously I had no luck in implementing printf but for some unknown reason this started working so I updated my program to use printf and scanf, which makes life simpler.
To calculate 1,000,000 takes approximately 30 minutes on bare metal RPI 1B.