| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
#include <nrk.h> |
|---|
| 25 |
#include <include.h> |
|---|
| 26 |
#include <ulib.h> |
|---|
| 27 |
#include <stdio.h> |
|---|
| 28 |
#include <hal.h> |
|---|
| 29 |
#include <nrk_error.h> |
|---|
| 30 |
#include <nrk_timer.h> |
|---|
| 31 |
#include <nrk_stack_check.h> |
|---|
| 32 |
#include <nrk_stats.h> |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
NRK_STK Stack1[NRK_APP_STACKSIZE]; |
|---|
| 36 |
nrk_task_type TaskOne; |
|---|
| 37 |
void Task1(void); |
|---|
| 38 |
|
|---|
| 39 |
NRK_STK Stack2[NRK_APP_STACKSIZE]; |
|---|
| 40 |
nrk_task_type TaskTwo; |
|---|
| 41 |
void Task2 (void); |
|---|
| 42 |
|
|---|
| 43 |
NRK_STK Stack3[NRK_APP_STACKSIZE]; |
|---|
| 44 |
nrk_task_type TaskThree; |
|---|
| 45 |
void Task3 (void); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
NRK_STK Stack4[NRK_APP_STACKSIZE]; |
|---|
| 49 |
nrk_task_type TaskFour; |
|---|
| 50 |
void Task4 (void); |
|---|
| 51 |
|
|---|
| 52 |
void nrk_create_taskset(); |
|---|
| 53 |
uint8_t kill_stack(uint8_t val); |
|---|
| 54 |
|
|---|
| 55 |
int |
|---|
| 56 |
main () |
|---|
| 57 |
{ |
|---|
| 58 |
nrk_setup_ports(); |
|---|
| 59 |
nrk_setup_uart(UART_BAUDRATE_115K2); |
|---|
| 60 |
|
|---|
| 61 |
nrk_init(); |
|---|
| 62 |
|
|---|
| 63 |
nrk_led_clr(ORANGE_LED); |
|---|
| 64 |
nrk_led_clr(BLUE_LED); |
|---|
| 65 |
nrk_led_clr(GREEN_LED); |
|---|
| 66 |
nrk_led_clr(RED_LED); |
|---|
| 67 |
|
|---|
| 68 |
nrk_time_set(0,0); |
|---|
| 69 |
nrk_create_taskset (); |
|---|
| 70 |
nrk_start(); |
|---|
| 71 |
|
|---|
| 72 |
return 0; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
nrk_task_stat_t my_stats; |
|---|
| 77 |
|
|---|
| 78 |
void Task1() |
|---|
| 79 |
{ |
|---|
| 80 |
nrk_time_t t; |
|---|
| 81 |
uint16_t cnt; |
|---|
| 82 |
cnt=0; |
|---|
| 83 |
nrk_kprintf( PSTR("Nano-RK Version ") ); |
|---|
| 84 |
printf( "%d\r\n",NRK_VERSION ); |
|---|
| 85 |
|
|---|
| 86 |
printf( "My node's address is %u\r\n",NODE_ADDR ); |
|---|
| 87 |
|
|---|
| 88 |
printf( "Task1 PID=%u\r\n",nrk_get_pid()); |
|---|
| 89 |
|
|---|
| 90 |
while(1) { |
|---|
| 91 |
nrk_led_toggle(ORANGE_LED); |
|---|
| 92 |
nrk_gpio_toggle(NRK_DEBUG_0); |
|---|
| 93 |
printf( "Task1 cnt=%u\r\n",cnt ); |
|---|
| 94 |
nrk_wait_until_next_period(); |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
if(cnt==50) { |
|---|
| 101 |
nrk_stats_display_all(); |
|---|
| 102 |
|
|---|
| 103 |
nrk_halt(); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
if( cnt==10 ) { |
|---|
| 108 |
nrk_stats_get(nrk_get_pid(), &my_stats); |
|---|
| 109 |
nrk_kprintf( PSTR( "\r\n Total CPU: ")); |
|---|
| 110 |
t=_nrk_ticks_to_time(my_stats.total_ticks); |
|---|
| 111 |
printf( "%lu secs %lu ms", t.secs, t.nano_secs/NANOS_PER_MS ); |
|---|
| 112 |
nrk_kprintf( PSTR( "\r\n Time [Min,Last,Max]: ")); |
|---|
| 113 |
t=_nrk_ticks_to_time(my_stats.min_exec_ticks); |
|---|
| 114 |
printf( "%lu secs %lu ms, ", t.secs, t.nano_secs/NANOS_PER_MS ); |
|---|
| 115 |
t=_nrk_ticks_to_time(my_stats.last_exec_ticks); |
|---|
| 116 |
printf( "%lu secs %lu ms, ", t.secs, t.nano_secs/NANOS_PER_MS ); |
|---|
| 117 |
t=_nrk_ticks_to_time(my_stats.max_exec_ticks); |
|---|
| 118 |
printf( "%lu secs %lu ms", t.secs, t.nano_secs/NANOS_PER_MS ); |
|---|
| 119 |
nrk_kprintf( PSTR( "\r\n Swap-ins: ")); |
|---|
| 120 |
printf( "%lu",my_stats.swapped_in ); |
|---|
| 121 |
nrk_kprintf( PSTR( "\r\n Preemptions: ")); |
|---|
| 122 |
printf( "%lu",my_stats.preempted); |
|---|
| 123 |
nrk_kprintf( PSTR( "\r\n Kernel Violations: ")); |
|---|
| 124 |
printf( "%u",my_stats.violations); |
|---|
| 125 |
nrk_kprintf( PSTR( "\r\n Overflow Error Status: ")); |
|---|
| 126 |
printf( "%u",my_stats.overflow); |
|---|
| 127 |
nrk_kprintf( PSTR("\r\n") ); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
cnt++; |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
void Task2() |
|---|
| 135 |
{ |
|---|
| 136 |
int16_t cnt; |
|---|
| 137 |
printf( "Task2 PID=%u\r\n",nrk_get_pid()); |
|---|
| 138 |
cnt=0; |
|---|
| 139 |
while(1) { |
|---|
| 140 |
nrk_led_toggle(BLUE_LED); |
|---|
| 141 |
nrk_gpio_toggle(NRK_DEBUG_1); |
|---|
| 142 |
printf( "Task2 signed cnt=%d\r\n",cnt ); |
|---|
| 143 |
nrk_wait_until_next_period(); |
|---|
| 144 |
|
|---|
| 145 |
cnt--; |
|---|
| 146 |
} |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
void Task3() |
|---|
| 150 |
{ |
|---|
| 151 |
uint16_t cnt; |
|---|
| 152 |
printf( "Task3 PID=%u\r\n",nrk_get_pid()); |
|---|
| 153 |
cnt=0; |
|---|
| 154 |
while(1) { |
|---|
| 155 |
nrk_led_toggle(GREEN_LED); |
|---|
| 156 |
nrk_gpio_toggle(NRK_DEBUG_2); |
|---|
| 157 |
printf( "Task3 cnt=%u\r\n",cnt ); |
|---|
| 158 |
nrk_wait_until_next_period(); |
|---|
| 159 |
cnt++; |
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
void Task4() |
|---|
| 164 |
{ |
|---|
| 165 |
uint16_t cnt; |
|---|
| 166 |
|
|---|
| 167 |
printf( "Task4 PID=%u\r\n",nrk_get_pid()); |
|---|
| 168 |
cnt=0; |
|---|
| 169 |
while(1) { |
|---|
| 170 |
nrk_led_toggle(RED_LED); |
|---|
| 171 |
nrk_gpio_toggle(NRK_DEBUG_3); |
|---|
| 172 |
printf( "Task4 cnt=%u\r\n",cnt ); |
|---|
| 173 |
nrk_wait_until_next_period(); |
|---|
| 174 |
cnt++; |
|---|
| 175 |
} |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
void |
|---|
| 179 |
nrk_create_taskset() |
|---|
| 180 |
{ |
|---|
| 181 |
nrk_task_set_entry_function( &TaskOne, Task1); |
|---|
| 182 |
nrk_task_set_stk( &TaskOne, Stack1, NRK_APP_STACKSIZE); |
|---|
| 183 |
TaskOne.prio = 1; |
|---|
| 184 |
TaskOne.FirstActivation = TRUE; |
|---|
| 185 |
TaskOne.Type = BASIC_TASK; |
|---|
| 186 |
TaskOne.SchType = PREEMPTIVE; |
|---|
| 187 |
TaskOne.period.secs = 0; |
|---|
| 188 |
TaskOne.period.nano_secs = 250*NANOS_PER_MS; |
|---|
| 189 |
TaskOne.cpu_reserve.secs = 1; |
|---|
| 190 |
TaskOne.cpu_reserve.nano_secs = 50*NANOS_PER_MS; |
|---|
| 191 |
TaskOne.offset.secs = 0; |
|---|
| 192 |
TaskOne.offset.nano_secs= 0; |
|---|
| 193 |
nrk_activate_task (&TaskOne); |
|---|
| 194 |
|
|---|
| 195 |
nrk_task_set_entry_function( &TaskTwo, Task2); |
|---|
| 196 |
nrk_task_set_stk( &TaskTwo, Stack2, NRK_APP_STACKSIZE); |
|---|
| 197 |
TaskTwo.prio = 2; |
|---|
| 198 |
TaskTwo.FirstActivation = TRUE; |
|---|
| 199 |
TaskTwo.Type = BASIC_TASK; |
|---|
| 200 |
TaskTwo.SchType = PREEMPTIVE; |
|---|
| 201 |
TaskTwo.period.secs = 0; |
|---|
| 202 |
TaskTwo.period.nano_secs = 500*NANOS_PER_MS; |
|---|
| 203 |
TaskTwo.cpu_reserve.secs = 0; |
|---|
| 204 |
TaskTwo.cpu_reserve.nano_secs = 100*NANOS_PER_MS; |
|---|
| 205 |
TaskTwo.offset.secs = 0; |
|---|
| 206 |
TaskTwo.offset.nano_secs= 0; |
|---|
| 207 |
nrk_activate_task (&TaskTwo); |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
nrk_task_set_entry_function( &TaskThree, Task3); |
|---|
| 211 |
nrk_task_set_stk( &TaskThree, Stack3, NRK_APP_STACKSIZE); |
|---|
| 212 |
TaskThree.prio = 3; |
|---|
| 213 |
TaskThree.FirstActivation = TRUE; |
|---|
| 214 |
TaskThree.Type = BASIC_TASK; |
|---|
| 215 |
TaskThree.SchType = PREEMPTIVE; |
|---|
| 216 |
TaskThree.period.secs = 1; |
|---|
| 217 |
TaskThree.period.nano_secs = 0; |
|---|
| 218 |
TaskThree.cpu_reserve.secs = 0; |
|---|
| 219 |
TaskThree.cpu_reserve.nano_secs = 100*NANOS_PER_MS; |
|---|
| 220 |
TaskThree.offset.secs = 0; |
|---|
| 221 |
TaskThree.offset.nano_secs= 0; |
|---|
| 222 |
nrk_activate_task (&TaskThree); |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
nrk_task_set_entry_function( &TaskFour, Task4); |
|---|
| 226 |
nrk_task_set_stk( &TaskFour, Stack4, NRK_APP_STACKSIZE); |
|---|
| 227 |
TaskFour.prio = 4; |
|---|
| 228 |
TaskFour.FirstActivation = TRUE; |
|---|
| 229 |
TaskFour.Type = BASIC_TASK; |
|---|
| 230 |
TaskFour.SchType = PREEMPTIVE; |
|---|
| 231 |
TaskFour.period.secs = 2; |
|---|
| 232 |
TaskFour.period.nano_secs = 0; |
|---|
| 233 |
TaskFour.cpu_reserve.secs = 0; |
|---|
| 234 |
TaskFour.cpu_reserve.nano_secs = 100*NANOS_PER_MS; |
|---|
| 235 |
TaskFour.offset.secs = 0; |
|---|
| 236 |
TaskFour.offset.nano_secs= 0; |
|---|
| 237 |
nrk_activate_task (&TaskFour); |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
uint8_t kill_stack(uint8_t val) |
|---|
| 243 |
{ |
|---|
| 244 |
char bad_memory[10]; |
|---|
| 245 |
uint8_t i; |
|---|
| 246 |
for(i=0; i<10; i++ ) bad_memory[i]=i; |
|---|
| 247 |
for(i=0; i<10; i++ ) printf( "%d ", bad_memory[i]); |
|---|
| 248 |
printf( "Die Stack %d\r\n",val ); |
|---|
| 249 |
if(val>1) kill_stack(val-1); |
|---|
| 250 |
return 0; |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
|
|---|