This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

add functions to specify delay for RPi 0 and RPi 3

iilak (Jan 8, 2024, 9:57 PM +0100) 6d85fbbd 00c07e3c

+97 -4
+1
os/CMakeLists.txt
··· 8 8 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${ARCH_DIR}/linker.ld") 9 9 10 10 set(SOURCES 11 + src/kernel/delays.c 11 12 src/kernel/random.c 12 13 src/kernel/kerio.c 13 14 src/kernel/gpu.c
+16
os/include/kernel/delays.h
··· 1 + #ifndef DELAYS_H 2 + #define DELAYS_H 3 + 4 + #include <stdint.h> 5 + 6 + void wait_cycles(uint32_t n); 7 + 8 + #if defined(MODEL_3) 9 + void wait_msec(uint32_t n); 10 + #endif 11 + 12 + void wait_msec_st(uint32_t n); 13 + 14 + uint64_t get_system_timer(); 15 + 16 + #endif // DELAYS_H
+3 -1
os/include/kernel/random.h
··· 1 1 #ifndef RANDOM_H 2 2 #define RANDOM_H 3 3 4 + #include <stdint.h> 5 + 4 6 void rand_init(); 5 - unsigned int rand(unsigned int min, unsigned int max); 7 + uint32_t rand(const uint32_t min, const uint32_t max); 6 8 7 9 #endif
+54
os/src/kernel/delays.c
··· 1 + #include "kernel/delays.h" 2 + 3 + #include "peripherals/base.h" 4 + #include "kernel/mmio.h" 5 + 6 + 7 + enum 8 + { 9 + SYSTMR_LO = (MMIO_BASE + 0x00003004), 10 + SYSTMR_HI = (MMIO_BASE + 0x00003008), 11 + }; 12 + 13 + void wait_cycles(uint32_t n) 14 + { 15 + if(n) while(n--) { asm volatile("nop"); } 16 + } 17 + 18 + #if defined(MODEL_3) 19 + void wait_msec(uint32_t n) 20 + { 21 + register unsigned long f, t, r; 22 + // get the current counter frequency 23 + asm volatile ("mrs %0, cntfrq_el0" : "=r"(f)); 24 + // read the current counter 25 + asm volatile ("mrs %0, cntpct_el0" : "=r"(t)); 26 + // calculate required count increase 27 + unsigned long i=((f/1000)*n)/1000; 28 + // loop while counter increase is less than i 29 + do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i); 30 + } 31 + #endif 32 + 33 + uint64_t get_system_timer() 34 + { 35 + unsigned int h=-1, l; 36 + // we must read MMIO area as two separate 32 bit reads 37 + h=mmio_read(SYSTMR_HI); 38 + l=mmio_read(SYSTMR_LO); 39 + // we have to repeat it if high word changed during read 40 + if(h!=mmio_read(SYSTMR_HI)) { 41 + h=mmio_read(SYSTMR_HI); 42 + l=mmio_read(SYSTMR_LO); 43 + } 44 + // compose long int value 45 + return ((uint64_t) h << 32) | l; 46 + } 47 + 48 + void wait_msec_st(uint32_t n) 49 + { 50 + unsigned long t=get_system_timer(); 51 + // we must check if it's non-zero, because qemu does not emulate 52 + // system timer, and returning constant zero would mean infinite loop 53 + if(t) while(get_system_timer()-t < n); 54 + }
+9 -2
os/src/kernel/kernel.c
··· 4 4 #include "common/string.h" 5 5 #include "kernel/kerio.h" 6 6 #include "kernel/uart.h" 7 + #include "kernel/delays.h" 7 8 8 9 #if defined(AARCH_32) 9 10 10 11 #include "kernel/atag.h" 11 12 #include "kernel/gpu.h" 12 13 #include "kernel/memory.h" 13 - 14 + #include "kernel/random.h" 14 15 /*! 15 16 * @brief Entry point for the kernel 16 17 * @param r0 0 ··· 35 36 #endif 36 37 37 38 puts("Welcome to piOS!\n----------------\n\n"); 39 + printf("Wait for 5 secs if it works!\n"); 40 + wait_msec_st(5000000); 38 41 42 + printf("%d\n", rand(100, 500)); 43 + 39 44 uint32_t mem_size = get_mem_size((atag_t *)atags); 40 45 printf("RAM size: %d MB\n\n", mem_size / 1024 / 1024); 41 46 ··· 54 59 { 55 60 printf("ATTENTION YOU ARE IN UART MODE\n\n"); 56 61 uart_init(); 62 + printf("Wait for 5 secs if it works!\n"); 63 + wait_msec(5000000); 57 64 printf("Architecture: AARCH_%d\n\n", 64); 58 65 rand_init(); 59 - printf("%d", rand(100, 500)); 66 + printf("%d\n", rand(100, 500)); 60 67 while(1) 61 68 { 62 69 putc(getc());
+14 -1
os/src/kernel/random.c
··· 3 3 #include "kernel/mmio.h" 4 4 #include "common/stdlib.h" 5 5 6 + #ifndef MODEL_3 7 + static uint32_t next = 1; 8 + 9 + uint32_t rand(const uint32_t min, const uint32_t max) 10 + { 11 + next = next * 1103515245 + 12345; 12 + divmod_t div = divmod(next / 65536, 32768); 13 + divmod_t t = divmod(div.mod, (max - min + 1)); 14 + return t.mod + min; 15 + } 16 + 17 + #else 6 18 enum 7 19 { 8 20 RNG_CTRL = (MMIO_BASE + 0x00104000), ··· 28 40 // available for reading; require at least one 29 41 while(!(mmio_read(RNG_STATUS)>>24)) asm volatile("nop"); 30 42 31 - divmod_t rand = divmod(mmio_read(RNG_DATA), (max-min)); 43 + divmod_t rand = divmod(mmio_read(RNG_DATA), (max-min+1)); 32 44 return rand.mod + min; 33 45 } 46 + #endif