|
| 1 | +#include "tpl_os.h" |
| 2 | +#include "stm32l4xx.h" |
| 3 | +#include "pinAccess.h" |
| 4 | +#include <math.h> |
| 5 | + |
| 6 | +//see machines/cortex-m/armv7em/stm32l432/lib/serial.h |
| 7 | +#include "serial.h" |
| 8 | + |
| 9 | +#define APP_Task_backgroundTask_START_SEC_CODE |
| 10 | +#include "tpl_memmap.h" |
| 11 | + |
| 12 | + |
| 13 | +void updateClock_80MHz(); |
| 14 | +//update the systick configuration |
| 15 | +void tpl_set_systick_timer(); |
| 16 | + |
| 17 | +//We use PA6 and PA7 to measure on logic analyser that the fpu computation is effectively preemted by the high priority task. |
| 18 | +FUNC(int, OS_APPL_CODE) main(void) |
| 19 | +{ |
| 20 | + pinMode(GPIOB,3,OUTPUT); //led 1 is PB3 |
| 21 | + updateClock_80MHz(); |
| 22 | + //call systick config again => System Core Clock updated. |
| 23 | + tpl_set_systick_timer(); |
| 24 | + |
| 25 | + tpl_serial_begin(); |
| 26 | + pinMode(GPIOA, 6, OUTPUT); // refered as A5 on Nucleo |
| 27 | + pinMode(GPIOA, 7, OUTPUT); // refered as A6 on Nucleo |
| 28 | + StartOS(OSDEFAULTAPPMODE); |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +volatile float shared; //float type => atomic |
| 33 | + |
| 34 | +TASK(highPrioTask) |
| 35 | +{ |
| 36 | + digitalWrite(GPIOA,6,1); |
| 37 | + volatile double tmp1; //volatile tmp value to limit compiler optimization |
| 38 | + |
| 39 | + double a = cos(shared); |
| 40 | + double b = sin(shared); |
| 41 | + tmp1 = sqrt(a*a+b*b); |
| 42 | + digitalWrite(GPIOA,6,0); |
| 43 | + TerminateTask(); |
| 44 | +} |
| 45 | + |
| 46 | +// basic FPU reated calculation |
| 47 | +// it computes the Sum of 1/(i^2), from 1 to ... |
| 48 | +// This sum converge (slowly) to pi^2/6 => we get an approx of pi |
| 49 | +// and print it every 1000 iterations |
| 50 | +TASK(backgroundTask) |
| 51 | +{ |
| 52 | + volatile double tmp1; //volatile tmp value to limit compiler optimization |
| 53 | + |
| 54 | + double n = 0.0; |
| 55 | + double un = 0.0; |
| 56 | + int idx = 0; |
| 57 | + while(1) |
| 58 | + { |
| 59 | + digitalWrite(GPIOA,7,1); |
| 60 | + n += 1.0; |
| 61 | + double term = 1/(n*n); |
| 62 | + tmp1 = term; |
| 63 | + un += term; |
| 64 | + double _pi = sqrt(un*6); |
| 65 | + shared = (float)_pi; |
| 66 | + int int_part = (int)_pi; |
| 67 | + int frac_part = (int)((_pi-int_part)*100000000UL); |
| 68 | + idx ++; |
| 69 | + digitalWrite(GPIOA,7,0); |
| 70 | + |
| 71 | + if(idx % 1000 == 0) |
| 72 | + { |
| 73 | + tpl_serial_wait_for_tx_complete(); |
| 74 | + tpl_serial_print_string("_pi: "); |
| 75 | + tpl_serial_print_int(int_part,10,1); //val, base, field width |
| 76 | + tpl_serial_putchar('.'); |
| 77 | + tpl_serial_print_int(frac_part,10,8); //val, base, field width |
| 78 | + tpl_serial_print_string("\r\n"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + // int i = 1; |
| 84 | + // un += 1/((double)i*i); |
| 85 | + // double pi = sqrt(un*6); |
| 86 | + // tpl_serial_print_string("count: "); |
| 87 | + // tpl_serial_print_int((int)(pi*1000),10,6); //val, base, field width |
| 88 | + // tpl_serial_print_string("\r\n"); |
| 89 | + // i++; |
| 90 | + // digitalToggle(GPIOB,3); //led |
| 91 | + TerminateTask(); |
| 92 | +} |
| 93 | + |
| 94 | +//tmp TODO: add ISR into library. |
| 95 | +void itUsart(); |
| 96 | +ISR(usart) |
| 97 | +{ |
| 98 | + itUsart(); |
| 99 | +} |
| 100 | + |
| 101 | +#define APP_Task_backgroundTask_STOP_SEC_CODE |
| 102 | +#include "tpl_memmap.h" |
| 103 | + |
0 commit comments