Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <io.h>
- #include <system.h>
- #include "alt_types.h"
- #include "altera_avalon_pio_regs.h"
- #include "sys/alt_irq.h"
- #include "sys/alt_timestamp.h"
- #include <unistd.h>
- #include <sys/alt_alarm.h>
- #include <math.h>
- #include "includes.h"
- #include <altera_avalon_mutex.h>
- //lab10
- #define SW0 0x00000001 /* CR1 */
- #define SW1 0x00000002 /* CR2 */
- #define SW2 0x00000004 /* CR3 */
- #define SW3 0x00000008 /* CR4 */
- #define SW4 0x00000010 /* CD1 */
- #define SW5 0x00000020 /* CD2 */
- #define SW6 0x00000040 /* CD3 */
- #define SW7 0x00000080 /* WB1 */
- #define SEGA 0x00001
- #define SEGB 0x00002
- #define SEGC 0x00004
- #define SEGD 0x00008
- #define SEGE 0x00010
- #define SEGF 0x00020
- #define SEGG 0x00040
- #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
- #define ONE SEGB | SEGC
- #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
- #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
- #define FOUR SEGB | SEGC | SEGF | SEGG
- #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
- #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
- #define E SEGA | SEGD | SEGE | SEGF | SEGG
- #define r SEGE | SEGG
- typedef enum POZIOM_TEMP {
- POZIOM_0 = 0,
- POZIOM_1 = 2,
- POZIOM_2 = 4,
- POZIOM_3 = 8,
- POZIOM_4 = 16,
- POZIOM_5 = 32,
- POZIOM_6 = 64,
- POZIOM_7 = 128,
- POZIOM_8 = 256,
- POZIOM_9 = 512,
- POZIOM_10 = 1024,
- } TEMP;
- #define LED0 0x00000001
- #define LED1 0x00000002
- #define LED2 0x00000004
- #define LED3 0x00000008
- /* Definition of Task Stacks */
- #define TASK_STACKSIZE 2048
- OS_STK task0_stk[TASK_STACKSIZE];
- OS_STK task1_stk[TASK_STACKSIZE];
- OS_STK task2_stk[TASK_STACKSIZE];
- OS_STK task3_stk[TASK_STACKSIZE];
- OS_STK task4_stk[TASK_STACKSIZE];
- OS_STK task5_stk[TASK_STACKSIZE];
- OS_STK task6_stk[TASK_STACKSIZE];
- OS_EVENT *SWBox1;
- #define VGA VGA_CHAR_BUFFER_AVALON_CHAR_BUFFER_SLAVE_BASE
- /* Definition of Task Priorities */
- #define TASK0_PRIORITY 0
- #define TASK1_PRIORITY 3
- #define TASK2_PRIORITY 2
- #define TASK3_PRIORITY 1
- #define TASK4_PRIORITY 4
- #define TASK5_PRIORITY 5
- #define TASK6_PRIORITY 6
- //alt_up_parallel_port_dev const *uchwytLEDR; = alt_up_parallel_port_open_dev("/dev/parallel_port_2");
- //alt_up_parallel_port_dev *uchwytLEDR;
- alt_u32 SWoff[6];
- alt_u32 SWon[6];
- alt_u32 errOn = -1;
- alt_u32 offBlockTime = 0;
- int STATE = -1;
- /* Prints "Hello World" and sleeps for three seconds */
- int flag = 1;
- int flag_off = 1;
- int flag_err = 1;
- int flag_err_off = 1;
- static alt_alarm alarm1;
- static alt_alarm alarmOff;
- static alt_alarm alarmErr;
- static alt_alarm alarmErrOff;
- int state = 0;
- void clear() {
- IOWR(HEX_BASE, 0, 0);
- IOWR(HEX_BASE, 1, 0);
- IOWR(HEX_BASE, 2, 0);
- IOWR(HEX_BASE, 3, 0);
- IOWR(HEX_BASE, 4, 0);
- IOWR(HEX_BASE, 5, 0);
- }
- void VGA_text(int x, int y, char * text_ptr)
- {
- int offset;
- /* Declare volatile pointer to char buffer (volatile means that IO load
- and store instructions will be used to access these pointer locations,
- instead of regular memory loads and stores) */
- volatile char * character_buffer = (char *) VGA; // VGA character buffer
- /* assume that the text string fits on one line */
- offset = (y << 7) + x;
- while ( *(text_ptr) )
- {
- *(character_buffer + offset) = *(text_ptr); // write to the character buffer
- ++text_ptr;
- ++offset;
- }
- }
- alt_u32 alarm_proc(void *context) {
- state = 0;
- flag = 1;
- if (context == 0b0100) {
- IOWR(LAB8_0_BASE,0, context);
- }
- // else if(context == 0b1000) {
- // }
- else {
- IOWR(LEDS_BASE, 0, context);
- }
- // IOWR(SHARED_MEMORY_BASE, 0, context);
- //printf(&context);
- return 0;
- }
- void task0(void* pdata) {
- while (1) {
- if (flag_err == 0) {
- if (alt_nticks() - errOn >= alt_ticks_per_second() * 3) {
- IOWR(LEDS_BASE, 0, 0b0100000000);
- flag_err = 1;
- }
- }
- INT8U err;
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if (*num != 0b1 && *num != 0b10 && *num != 0b100 && *num != 0b1000
- && *num != 0b10000 && *num != 0b100000 && *num != 0) {
- if (flag_err != 0) {
- IOWR(HEX_BASE, 2, E);
- IOWR(HEX_BASE, 1, r);
- IOWR(HEX_BASE, 0, r);
- char text1_1_row[40] = "error";
- VGA_text (30, 15, text1_1_row);
- errOn = alt_nticks();
- flag_err = 0;
- }
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task1(void* pdata) {
- while (1) {
- int sw;
- int * msg;
- sw = IORD(SW_SLIDERS_BASE, 0);
- //printf(sw);
- IOWR(SHARED_MEMORY_BASE, 0, sw);
- OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
- if (sw == 0b1) {
- clear();
- IOWR(HEX_BASE, 0, ONE);
- if (flag != 0) {
- alt_alarm_stop(&alarm1);
- // alt_alarm_stop(&alarmErrOff);
- alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
- sw);
- char text1_1_row[40] = "task1";
- VGA_text (30, 15, text1_1_row);
- flag = 0;
- }
- STATE = 0;
- }
- if (sw == 0) {
- for (int i = 0; i < 6; i++) {
- alt_u32 t1 = alt_timestamp();
- if (SWoff[i] != -1) {
- alt_u32 t2 = alt_timestamp();
- if ((t2 - SWoff[i]) / alt_timestamp_freq() >= 2) {
- float var = (t2 - SWoff[i]) / alt_timestamp_freq();
- //printf("var=%.6f", var);
- //printf("clr ");
- SWoff[i] = -1;
- IOWR(LEDS_BASE, 0, 0);
- char text1_1_row[40] = " ";
- VGA_text (30, 15, text1_1_row);
- //IOWR(SHARED_MEMORY_BASE, 0,a);
- IOWR(LAB8_0_BASE,0,0);
- }
- }
- }
- IOWR(LAB8_0_BASE,0,0);
- if (STATE != -1) {
- clear();
- //printf("off");
- SWoff[STATE] = alt_timestamp();
- }
- STATE = -1;
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- /* Prints "Hello World" and sleeps for three seconds */
- void task2(void* pdata) {
- while (1) {
- //IOWR(LAB81_1_BASE,0, 0);
- INT8U err;
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if (*num == 0b10) {
- if (flag != 0) {
- //IOWR(LAB81_1_BASE,0, 0b1000);
- alt_alarm_stop(&alarm1);
- // alt_alarm_stop(&alarmErrOff);
- alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
- *num);
- flag = 0;
- IOWR(LAB8_0_BASE,0,0);
- char text1_1_row[40] = "task2";
- VGA_text (30, 15, text1_1_row);
- }
- clear();
- IOWR(HEX_BASE, 0, TWO);
- STATE = 1;
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task3(void* pdata) {
- while (1) {
- INT8U err;
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if (*num == 0b0100) {
- printf(num);
- //printf("X");
- //clear();
- IOWR(LEDS_BASE,0, 0b0000);
- //IOWR(HEX_BASE, 0, THREE);
- if (flag != 0) {
- alt_alarm_stop(&alarm1);
- // alt_alarm_stop(&alarmErrOff);
- alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
- *num);
- flag = 0;
- //IOWR(LAB8_0_BASE,0,0);
- char text1_1_row[40] = "task3";
- VGA_text (30, 15, text1_1_row);
- }
- STATE = 2;
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task4(void* pdata) {
- while (1) {
- INT8U err;
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if (*num == 0b1000) {
- clear();
- IOWR(HEX_BASE, 0, FOUR);
- if (flag != 0) {
- alt_alarm_stop(&alarm1);
- // alt_alarm_stop(&alarmErrOff);
- alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
- *num);
- char text1_1_row[40] = "task4";
- VGA_text (30, 15, text1_1_row);
- flag = 0;
- IOWR(LAB8_0_BASE,0,0);
- }
- STATE = 3;
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task5(void* pdata) {
- while (1) {
- INT8U err;
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if (*num == 0b10000) {
- if (flag != 0) {
- alt_alarm_stop(&alarm1);
- // alt_alarm_stop(&alarmErrOff);
- alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
- *num);
- flag = 0;
- IOWR(LAB8_0_BASE,0,0);
- char text1_1_row[40] = "task5";
- VGA_text (30, 15, text1_1_row);
- }
- clear();
- IOWR(HEX_BASE, 0, FIVE);
- STATE = 4;
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task6(void* pdata) {
- while (1) {
- INT8U err;
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if (*num == 0b100000) {
- IOWR(HEX_BASE, 1, 0);
- IOWR(HEX_BASE, 2, 0);
- if (flag != 0) {
- alt_alarm_stop(&alarm1);
- // alt_alarm_stop(&alarmErrOff);
- alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
- *num);
- flag = 0;
- IOWR(LAB8_0_BASE,0,0);
- char text1_1_row[40] = "task6";
- VGA_text (30, 15, text1_1_row);
- }
- clear();
- IOWR(HEX_BASE, 0, SIX);
- STATE = 5;
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- /* The main function creates two task and starts multi-tasking */
- int main(void) {
- for (int i = 0; i < 6; i++) {
- SWoff[i] = -1;
- SWon[i] = -1;
- }
- if (alt_timestamp_start() < 0) {
- printf("Brak timera w systemie\n");
- }
- IOWR(HEX_BASE, 3, 0);
- IOWR(HEX_BASE, 4, 0);
- IOWR(HEX_BASE, 5, 0);
- //char text1_1_row[40] = "fn3";
- //VGA_text (30, 15, text1_1_row);
- SWBox1 = OSMboxCreate((void*) 0);
- OSTaskCreateExt(task0,
- NULL, (void *) &task0_stk[TASK_STACKSIZE - 1],
- TASK0_PRIORITY,
- TASK0_PRIORITY, task0_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task1,
- NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
- TASK1_PRIORITY,
- TASK1_PRIORITY, task1_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task2,
- NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
- TASK2_PRIORITY,
- TASK2_PRIORITY, task2_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task3,
- NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
- TASK3_PRIORITY,
- TASK3_PRIORITY, task3_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task4,
- NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
- TASK4_PRIORITY,
- TASK4_PRIORITY, task4_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task5,
- NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
- TASK5_PRIORITY,
- TASK5_PRIORITY, task5_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task6,
- NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
- TASK6_PRIORITY,
- TASK6_PRIORITY, task6_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSStart();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement