Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/os/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,34 @@ target_link_libraries(boot
hardware_exception
hardware_sync
pico_multicore
hagl_hal
hardware_spi
hardware_dma
hagl
hagl_hal
)

target_compile_definitions(boot PRIVATE
HAGL_HAL_USE_DOUBLE_BUFFER
HAGL_HAL_DEBUG
HAGL_HAL_USE_SINGLE_BUFFER
HAGL_HAL_USE_DMA
HAGL_HAL_DEBUG
MIPI_DISPLAY_PIN_CS=9
MIPI_DISPLAY_PIN_DC=8
MIPI_DISPLAY_PIN_RST=12
MIPI_DISPLAY_PIN_BL=13
MIPI_DISPLAY_PIN_CLK=10
MIPI_DISPLAY_PIN_MOSI=11
MIPI_DISPLAY_PIN_MISO=-1
MIPI_DISPLAY_SPI_PORT=spi1
# MIPI_DISPLAY_SPI_CLOCK_SPEED_HZ=680000

# MIPI_DISPLAY_PIXEL_FORMAT=MIPI_DCS_PIXEL_FORMAT_16BIT
# MIPI_DISPLAY_ADDRESS_MODE=MIPI_DCS_ADDRESS_MODE_RGB

MIPI_DISPLAY_WIDTH=320
MIPI_DISPLAY_HEIGHT=480
MIPI_DISPLAY_OFFSET_X=0
MIPI_DISPLAY_OFFSET_Y=0
)

# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(boot)
pico_add_extra_outputs(boot)
238 changes: 31 additions & 207 deletions src/os/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,236 +8,60 @@
#include "pico/stdlib.h"
#include <stdio.h>
#include "pico/malloc.h"
#include "hardware/structs/systick.h"
#include "hardware/exception.h"
#include "pico/multicore.h"
#include "kernel/lock_core.h"
#include "pico/sem.h"

#include "kernel/kernel.h"

const uint LED_PIN = 14;
volatile extern uint32_t tickct;
volatile extern int32_t sleep_time;
volatile extern int32_t sleep_core;
volatile extern int32_t sleep_ct;
#include <hagl_hal.h>
#include <hagl.h>
#include <stdlib.h>

semaphore_t talking_stick;
hagl_backend_t *display;

/*
* This task blinks the LED. It also holds the semaphore talking_stick
* while the LED is off. This is used to gate the reporter task and keep it
* from printing.
*/
void blinker(void) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the "blinker(void)" function has been removed to attach the screen hardware component, its not clear what Raspberry Pi Pico W pins are devoted to what inputs/outputs. "blinker(void)" could continue to demonstrate functionality with different pins later and be commented out of compilation and execution.

This comment applies to the "blinker(void)" and "reporter_task(void)" functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The screen pins are seen in the cmakelists.txt file within the os folder. The hardware library requires it to be so, but I believe that I might remove this graphics library in the future.

Copy link
Collaborator Author

@BjornTheProgrammer BjornTheProgrammer Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blinker feature actually makes sense and should be added back into the os.

gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while (true) {
gpio_put(LED_PIN, 1);
sem_release(&talking_stick);
piccolo_sleep(2000);
gpio_put(LED_PIN, 0);
sem_acquire_blocking(&talking_stick);
piccolo_sleep(2000);
}
}


int is_prime(unsigned int n)
{
unsigned int p;
if (!(n & 1) || n < 2 ) return n == 2;

// comparing p*p <= n can overflow
for (p = 3; p <= n/p; p += 2)
if (!(n % p)) return 0;
return 1;
}

/*
* Prime number computing task. Just to burn CPU time
* Note that it never calls piccolo_yield()
*/
uint32_t primes[2], totalPrimes;
piccolo_os_task_t * reporter;

void find_primes(void) {
int p;

printf("task2: Created!\n");
while (1) {
for (p=5;p;p+=2) if(is_prime(p)==1) {
totalPrimes++;
primes[get_core_num()]++;
// every 4096 prime numbers, send the reporter a signal
if(!(totalPrimes & 0xFFF)) piccolo_send_signal(reporter);
}
}
}
/*
* Report on the progress of the prime number finder. Wait until he sends a signal
* Then get the "talking stick" semaphore from the LED blinker task. We can only
* talk when the green light is on! Then print a report. We also report on
* how many tasks the garbage collector has reclaimed using a counter which
* is only there as a debug feature at the moment...
*/
extern uint32_t kills;
void reporter_task(void){
printf("Reporter Started\n");
while(1) {
piccolo_get_signal_all_blocking();
if(!sem_acquire_timeout_ms(&talking_stick,10000)) printf("sem acquire timeout SHOULD NOT have failed\n");

printf("Total primes %d, cores 0/1 %d/%d kills %d recycled %d bytes\n",
totalPrimes,primes[0], primes[1], kills, kills*sizeof(piccolo_os_task_t));
sem_release(&talking_stick);
void continuousDraw(void) {
while (1) {
sleep_ms(2000);

color_t color = rand() % 0xffff;

hagl_fill_rectangle(display, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, color);
hagl_flush(display);
}
}

/*
* The next two tasks are created periodically by the stress_tester task
* only to quickly delete themselves. "z" dies immediatly whicle "sz" yields
* once first.
*/
void sz(){
piccolo_yield();
return;
}
bool isPrime(unsigned int number) {
for (unsigned int i = 3; i <= number / i; i += 2) {
if (number % i == 0) return true;
}

void z(){
int a = 1;
return;
return false;
}

void stress_tester(void) {
/**
* Force a slew of task create and deletes by creating lots of tasks
* that die very quickly. Do this every few seconds. Note that we
* are perfectly safe creating multiple tasks running the same function
* since they all have seperate stacks.
*
*/
while(1) {
piccolo_create_task(z);
piccolo_create_task(z);
piccolo_create_task(z);
piccolo_create_task(sz);
piccolo_create_task(sz);
piccolo_create_task(sz);
piccolo_create_task(z);
piccolo_create_task(sz);
piccolo_create_task(z);
piccolo_create_task(sz);
piccolo_create_task(sz);

piccolo_sleep(3000);
void calcPrimes(void) {
unsigned int i = 3;
while (1) {
if (isPrime(i)) printf("%d\n", i);
i += 2;
}
}

int main() {

void spinner2(void){
piccolo_os_task_t * task;
task = piccolo_get_task_id();
while(!piccolo_get_signal()) piccolo_yield(); // spin until we get a signal
piccolo_get_signal_blocking(); // hang until we get a second one
// printf(" signal hang2 task %8X in %d out %d limit %d\n",task,task->signal_in,task->signal_out,task->signal_limit);
piccolo_get_signal_blocking(); // hang until we get a third one
// printf(" semaphore hang task %8X in %d out %d limit %d\n",task,task->signal_in,task->signal_out,task->signal_limit);
sem_acquire_blocking(&talking_stick); // then hang on the semaphore
// printf(" semaphore exit task %8X in %d out %d limit %d\n",task,task->signal_in,task->signal_out,task->signal_limit);
return; // and exit
}
void spinner(){
int i,yielding=1,blocking=1, semaphore;
absolute_time_t start;
uint64_t time;
piccolo_os_task_t *spin1, *spin2;
#define loops 1000
piccolo_sleep(10);
start = get_absolute_time();
for(i=0;i<loops;i++) piccolo_yield();
time = absolute_time_diff_us(start,get_absolute_time());
printf("\n");
printf("Yielding:%3d Blocking:%3d Takes %6lld nanoseconds\n",yielding,blocking,time);

spin1 = piccolo_create_task(spinner2);
yielding++;
piccolo_sleep(10);
start = get_absolute_time();
for(i=0;i<loops;i++) piccolo_yield();
time = absolute_time_diff_us(start,get_absolute_time());
printf("Yielding:%3d Blocking:%3d Takes %6lld nanoseconds\n",yielding,blocking,time);

spin2 = piccolo_create_task(spinner2);
yielding++;
piccolo_sleep(10);
start = get_absolute_time();
for(i=0;i<loops;i++) piccolo_yield();
time = absolute_time_diff_us(start,get_absolute_time());
printf("Yielding:%3d Blocking:%3d Takes %6lld nanoseconds\n",yielding,blocking,time);

piccolo_send_signal(spin1);
piccolo_send_signal(spin2); // send them both to signal blocking
yielding -=2;
blocking +=2;
piccolo_sleep(2000);
start = get_absolute_time();
for(i=0;i<loops;i++) piccolo_yield();
time = absolute_time_diff_us(start,get_absolute_time());
printf("Yielding:%3d Blocking:%3d Takes %6lld nanoseconds\n",yielding,blocking,time);

sem_try_acquire(&talking_stick); // Eat the permit that should be there
piccolo_send_signal(spin1);
piccolo_send_signal(spin2); // send them both to semaphores
yielding = 1;
blocking = 1;
semaphore = 2;
piccolo_sleep(2000);
start = get_absolute_time();
for(i=0;i<loops;i++) piccolo_yield();
time = absolute_time_diff_us(start,get_absolute_time());
printf("Yielding:%3d Blocking:%3d On Semaphore:%3d Takes %6lld nanoseconds\n",yielding,blocking,semaphore, time);

sem_release(&talking_stick); // one of them will die
semaphore--;
piccolo_sleep(2000);
start = get_absolute_time();
for(i=0;i<loops;i++) piccolo_yield();
time = absolute_time_diff_us(start,get_absolute_time());
printf("Yielding:%3d Blocking:%3d On Semaphore:%3d Takes %6lld nanoseconds\n",yielding,blocking,semaphore, time);

sem_release(&talking_stick); // one of them will die
piccolo_sleep(20);
sem_release(&talking_stick); // replace the permit

//start the LED blinker
piccolo_create_task(blinker);
printf("\nStart the prime finder, his reporter and the stress tester, and then depart!\n");
piccolo_create_task(stress_tester);
reporter = piccolo_create_task(reporter_task);
piccolo_create_task(find_primes);
}

set_sys_clock_khz(133000, true);

int main() {
stdio_init_all();
piccolo_init();

display = hagl_init();

hagl_set_clip_window(display, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);

sleep_ms(5000); // pause to give user a chance to start putty

printf("Hello World!\n");
hagl_clear(display);

// initialize the semaphore
sem_init(&talking_stick,1,1);
piccolo_create_task(continuousDraw);
piccolo_create_task(calcPrimes);

// test a few things for timing first
// the spinner task will start everything else ...
piccolo_create_task(spinner);

printf("PICCOLO OS Demo Starting...\n");
// and begin!
piccolo_start();

return 0; /* Never gonna happen */
}