Project

General

Profile

Revision 261

stuff

View differences:

proj/include/errors.h
18 18
    ALLOC_ERROR,        /** @brief Memory allocation error */
19 19
    LOGIC_ERROR,        /** @brief Logic error */
20 20
    INVALID_ARG,        /** @brief Invalid argument */
21
    NOK,                /** @brief Not OK */
22
    TRANS_REFUSED,      /** @brief Transmission refused */
23
    TRANS_FAILED,       /** @brief Transmission failed */
21 24
    OTHER_ERROR         /** @brief Unspecified error */
22 25
};
23 26

  
proj/include/graph.h
1 1
#ifndef GRAPH_H_INCLUDED
2 2
#define GRAPH_H_INCLUDED
3 3

  
4
// Graphics modes
5
#define INDEXED_1024_768        0x105
6
#define DIRECT_640_480_888      0x110
7
#define DIRECT_800_600_888      0x115
8
#define DIRECT_1024_768_888     0x118
9
#define DIRECT_1280_1024_565    0x11A
10
#define DIRECT_1280_1024_888    0x11B
11
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
12

  
4 13
// Colors in RBG (8 bit)
5 14
#define GRAPH_BLACK               0x000000
6 15
#define GRAPH_WHITE               0xFFFFFF
proj/include/uart.h
47 47
int uart_enable_int_tx (int base_addr);
48 48
int uart_disable_int_tx(int base_addr);
49 49

  
50
int uart_get_char_poll   (int base_addr, uint8_t *p);
51
int uart_send_char_poll  (int base_addr, uint8_t  c);
52

  
53 50
#endif //UART_H_INCLUDED
proj/src/graph.c
22 22
#define AH_FUNC_NOT_SUPP    0x02    /** @brief Function call is not supported in current HW configuration */
23 23
#define AH_FUNC_INVALID     0x03    /** @brief Invalid function in current video mode */
24 24

  
25
// Graphics modes
26
#define INDEXED_1024_768        0x105
27
#define DIRECT_640_480_888      0x110
28
#define DIRECT_800_600_888      0x115
29
#define DIRECT_1024_768_888     0x118
30
#define DIRECT_1280_1024_565    0x11A
31
#define DIRECT_1280_1024_888    0x11B
32
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
33

  
34

  
35

  
36

  
37

  
38 25
/// MACROS
39 26
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
40 27

  
proj/src/proj.c
52 52
int(proj_main_loop)(int argc, char *argv[]) {
53 53

  
54 54
    int r;
55
    #ifndef DIOGO
56
        font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
57
        if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
58 55

  
59
        /// subscribe interrupts
60
        if (subscribe_all()) { return 1; }
56
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
57
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
61 58

  
62
        /// initialize graphics
63
        if(graph_init(GRAPH_MODE)){
64
            printf("%s: failed to initalize graphics.\n", __func__);
65
            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
66
            return 1;
67
        }
59
    /// subscribe interrupts
60
    if (subscribe_all()) { return 1; }
68 61

  
69
        /// Load stuff
70
        basic_sprite_t       *bsp_crosshair = NULL;
71
        basic_sprite_t       *bsp_shooter   = NULL;
72
        basic_sprite_t       *bsp_pistol    = NULL;
73
        basic_sprite_t       *bsp_nothing   = NULL;
74
        basic_sprite_t       *bsp_bullet    = NULL;
75
        map_t                *map1          = NULL;
76
        sprite_t             *sp_crosshair  = NULL;
77
        {
78
            graph_clear_screen();
79
            text_t *txt = text_ctor(consolas, "Loading...");
80
            text_draw(txt);
81
            text_dtor(txt);
82
            graph_draw();
62
    /// initialize graphics
63
    if(graph_init(GRAPH_MODE)){
64
        printf("%s: failed to initalize graphics.\n", __func__);
65
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
66
        return 1;
67
    }
83 68

  
84
            bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
85
            bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
86
            bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
87
            bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
88
            bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
89
            map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
90

  
91
            sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
92
        }
93
    #endif
94

  
95
    #ifdef DIOGO
96
        /*
69
    /// Load stuff
70
    basic_sprite_t       *bsp_crosshair = NULL;
71
    basic_sprite_t       *bsp_shooter   = NULL;
72
    basic_sprite_t       *bsp_pistol    = NULL;
73
    basic_sprite_t       *bsp_nothing   = NULL;
74
    basic_sprite_t       *bsp_bullet    = NULL;
75
    map_t                *map1          = NULL;
76
    sprite_t             *sp_crosshair  = NULL;
77
    {
97 78
        graph_clear_screen();
98

  
99
        rectangle_t *rect = rectangle_ctor(0,0,400,100);
100
        rectangle_set_pos(rect,
101
                          graph_get_XRes()/2    - rectangle_get_w(rect)/2,
102
                          graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
103
        rectangle_set_fill_color(rect, BLACK);
104
        rectangle_set_outline_width(rect, 2);
105
        rectangle_set_outline_color(rect, WHITE);
106
        rectangle_draw(rect);
107

  
108
        text_t *txt  = text_ctor(consolas, "Hello world!");
109
        text_set_color(txt, 0x888888);
110

  
111
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
112
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
113
        text_set_valign(txt, text_valign_center);
114
        text_set_halign(txt, text_halign_center);
79
        text_t *txt = text_ctor(consolas, "Loading...");
115 80
        text_draw(txt);
116 81
        text_dtor(txt);
117

  
118 82
        graph_draw();
119
        rectangle_dtor(rect);
120 83

  
121
        list_t *l = list_ctor();
122
        int *p = NULL;
123
        for(int i = 10; i < 20; ++i){
124
            p = malloc(sizeof(int));
125
            *p = i;
126
            printf("INSERTING %d\n", i);
127
            list_insert(l, list_end(l), p);
128
            printf("INSERTED, SIZE=%d\n", list_size(l));
129
        }
130
        list_node_t *it = list_begin(l);
131
        while(it != list_end(l)){
132
            printf("%d\n", **(int**)list_node_val(it));
133
            it = list_node_next(it);
134
        }
135
        while(list_size(l) > 0){
136
            printf("ERASING\n");
137
            void *p = list_erase(l, list_begin(l));
138
            printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l));
139
            free(p);
140
        }
141
        printf("DONE\n");
142
        if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n");
84
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
85
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
86
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
87
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
88
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
89
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
143 90

  
91
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
92
    }
144 93

  
145
        tickdelay(micros_to_ticks(1000000));*/
94
    ent_set_scale(DEFAULT_SCALE);
146 95

  
147
        // RTC
148
        /*
149
        uint8_t date[4], time[3];
96
    text_timer_t *in_game_timer = timer_ctor(consolas);
150 97

  
151
        if (rtc_read_time(time)) return 1;
98
    menu_t *main_menu = menu_ctor(consolas);
152 99

  
153
        if (rtc_read_date(date)) return 1;
100
    list_t *shooter_list = list_ctor();
154 101

  
155
        printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
102
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
103
    gunner_set_spawn(shooter1, 75, 75);
104
    gunner_set_pos(shooter1, 75, 75);
156 105

  
157
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
158
        */
159
        //UART
160
        unsigned long bits = 8;
161
        unsigned long stop = 2;
162
        long parity = 0;
163
        unsigned long rate = 9600;
164
        unsigned char tx = 0; (void)tx;
165
        int stringc = 2; (void)stringc;
166
        char *strings[] = {"Hello", "world"}; (void)strings;
167
        //if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
168
        //if((r = ser_test_conf(COM1_ADDR))) return r;
169
        if((r = ser_test_poll(COM1_ADDR, tx, bits, stop, parity, rate, stringc, strings))) return r;
170
        printf("DONE\n");
171
    #endif
172
    #ifndef DIOGO
173
        ent_set_scale(DEFAULT_SCALE);
106
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
107
    gunner_set_spawn(shooter2, 975, 75);
108
    gunner_set_pos(shooter2, 775, 75);
174 109

  
175
        text_timer_t *in_game_timer = timer_ctor(consolas);
110
    list_insert(shooter_list, list_end(shooter_list), shooter1);
111
    list_insert(shooter_list, list_end(shooter_list), shooter2);
176 112

  
177
        menu_t *main_menu = menu_ctor(consolas);
113
    list_t *bullet_list  = list_ctor();
178 114

  
179
        list_t *shooter_list = list_ctor();
115
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
116
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
180 117

  
181
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
182
        gunner_set_spawn(shooter1, 75, 75);
183
        gunner_set_pos(shooter1, 75, 75);
118
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
119
    double angle; // mouse angle
120
    int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
121
    uint8_t last_lb = 0;
122
    struct packet pp;
123
    keys_t *keys = get_key_presses();
124
    /// loop stuff
125
    int ipc_status;
126
    message msg;
127
    int game_state = MENU, click = 0; (void)click;
184 128

  
185
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
186
        gunner_set_spawn(shooter2, 975, 75);
187
        gunner_set_pos(shooter2, 775, 75);
129
    while (game_state != EXIT) {
130
        /* Get a request message. */
131
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
132
            printf("driver_receive failed with %d", r);
133
            continue;
134
        }
135
        if (is_ipc_notify(ipc_status)) { /* received notification */
136
            switch (_ENDPOINT_P(msg.m_source)) {
137
                case HARDWARE: /* hardware interrupt notification */
138
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
139
                        if (msg.m_notify.interrupts & n) {
140
                            interrupt_handler(i);
141
                            switch (i) {
142
                            #ifndef DIOGO
143
                            case TIMER0_IRQ:
144
                                switch (game_state) {
145
                                case MENU:
146
                                    if (no_interrupts % 2 == 0){
147
                                        graph_clear_screen();
148
                                        game_state = menu_update_state(main_menu, click);
149
                                        menu_draw(main_menu);
188 150

  
189
        list_insert(shooter_list, list_end(shooter_list), shooter1);
190
        list_insert(shooter_list, list_end(shooter_list), shooter2);
151
                                        click = 0;
191 152

  
192
        list_t *bullet_list  = list_ctor();
153
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
154
                                        sprite_draw(sp_crosshair);
155
                                        graph_draw();
156
                                    }
157
                                    break;
158
                                case GAME:
159
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
160
                                    update_movement(map1, shooter1, keys, shooter_list);
193 161

  
194
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
195
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
162
                                    update_game_state(map1, shooter_list, bullet_list);
196 163

  
197
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
198
        double angle; // mouse angle
199
        int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
200
        uint8_t last_lb = 0;
201
        struct packet pp;
202
        keys_t *keys = get_key_presses();
164
                                    //update_scale();
165
                                    angle = get_mouse_angle(shooter1);
166
                                    gunner_set_angle(shooter1, angle - M_PI_2);
203 167

  
204
        /// loop stuff
205
        int ipc_status;
206
        message msg;
207
        int game_state = MENU, click = 0;
168
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
169
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
208 170

  
209
        while (game_state != EXIT) {
210
            /* Get a request message. */
211
            if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
212
                printf("driver_receive failed with %d", r);
213
                continue;
214
            }
215
            if (is_ipc_notify(ipc_status)) { /* received notification */
216
                switch (_ENDPOINT_P(msg.m_source)) {
217
                    case HARDWARE: /* hardware interrupt notification */
218
                        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
219
                            if (msg.m_notify.interrupts & n) {
220
                                interrupt_handler(i);
221
                                switch (i) {
222
                                case TIMER0_IRQ:
223
                                    switch (game_state) {
224
                                    case MENU:
225
                                        if (no_interrupts % 2 == 0){
226
                                            graph_clear_screen();
227
                                            game_state = menu_update_state(main_menu, click);
228
                                            menu_draw(main_menu);
171
                                    graph_clear_screen();
172
                                    map_draw   (map1);
173
                                    gunner_draw_list(shooter_list);
174
                                    bullet_draw_list(bullet_list);
229 175

  
230
                                            click = 0;
176
                                    text_draw(in_game_timer->text);
231 177

  
232
                                            sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
233
                                            sprite_draw(sp_crosshair);
234
                                            graph_draw();
178
                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
179
                                    sprite_draw(sp_crosshair);
180
                                    graph_draw();
181
                                    break;
182
                                }
183
                                break;
184
                            case KBC_IRQ:
185
                                switch (game_state) {
186
                                case MENU:
187
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
188
                                    break;
189
                                case GAME:
190
                                    if ((scancode[0]) == ESC_BREAK_CODE) {
191
                                        game_state = MENU;
192
                                        // reset game
193
                                        while(list_size(bullet_list) > 0){
194
                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
195
                                            bullet_dtor(p);
235 196
                                        }
236
                                        break;
237
                                    case GAME:
238
                                        if (no_interrupts % 60 == 0) timer_update(in_game_timer);
239
                                        update_movement(map1, shooter1, keys, shooter_list);
240

  
241
                                        update_game_state(map1, shooter_list, bullet_list);
242

  
243
                                        //update_scale();
244
                                        angle = get_mouse_angle(shooter1);
245
                                        gunner_set_angle(shooter1, angle - M_PI_2);
246

  
247
                                        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
248
                                                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
249

  
250
                                        graph_clear_screen();
251
                                        map_draw   (map1);
252
                                        gunner_draw_list(shooter_list);
253
                                        bullet_draw_list(bullet_list);
254

  
255
                                        text_draw(in_game_timer->text);
256

  
257
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
258
                                        sprite_draw(sp_crosshair);
259
                                        graph_draw();
260
                                        break;
197
                                        list_node_t *it = list_begin(shooter_list);
198
                                        while (it != list_end(shooter_list)) {
199
                                            gunner_t *p = *(gunner_t**)list_node_val(it);
200
                                            get_random_spawn(map1, p);
201
                                            gunner_set_curr_health(p, gunner_get_health(p));
202
                                            it = list_node_next(it);
203
                                        }
204
                                        timer_reset(in_game_timer);
261 205
                                    }
262 206
                                    break;
263
                                case KBC_IRQ:
207
                                }
208
                                break;
209
                            case MOUSE_IRQ:
210
                                if (counter_mouse_ih >= 3) {
211
                                    mouse_parse_packet(packet_mouse_ih, &pp);
212
                                    update_mouse(&pp);
264 213
                                    switch (game_state) {
265 214
                                    case MENU:
266
                                        if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
215
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
267 216
                                        break;
268 217
                                    case GAME:
269
                                        if ((scancode[0]) == ESC_BREAK_CODE) {
270
                                            game_state = MENU;
271
                                            // reset game
272
                                            while(list_size(bullet_list) > 0){
273
                                                bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
274
                                                bullet_dtor(p);
275
                                            }
276
                                            list_node_t *it = list_begin(shooter_list);
277
                                            while (it != list_end(shooter_list)) {
278
                                                gunner_t *p = *(gunner_t**)list_node_val(it);
279
                                                get_random_spawn(map1, p);
280
                                                gunner_set_curr_health(p, gunner_get_health(p));
281
                                                it = list_node_next(it);
282
                                            }
283
                                            timer_reset(in_game_timer);
284
                                        }
218
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
219
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
285 220
                                        break;
286 221
                                    }
287
                                    break;
288
                                case MOUSE_IRQ:
289
                                    if (counter_mouse_ih >= 3) {
290
                                        mouse_parse_packet(packet_mouse_ih, &pp);
291
                                        update_mouse(&pp);
292
                                        switch (game_state) {
293
                                        case MENU:
294
                                            if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
295
                                            break;
296
                                        case GAME:
297
                                            if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
298
                                                shoot_bullet(shooter1, bullet_list, bsp_bullet);
299
                                            break;
300
                                        }
301
                                        last_lb = keys->lb_pressed;
302
                                        counter_mouse_ih = 0;
222
                                    last_lb = keys->lb_pressed;
223
                                    counter_mouse_ih = 0;
303 224

  
304
                                    }
305
                                    break;
306 225
                                }
226
                                break;
227
                            #endif
228
                            #ifdef DIOGO
229
                            case COM1_IRQ:
230
                                printf("You've got mail.\n");
231
                                break;
232
                            #endif
307 233
                            }
308 234
                        }
235
                    }
309 236

  
310
                        break;
311
                    default:
312
                        break; /* no other notifications expected: do nothing */
313
                }
314
            } else { /* received standart message, not a notification */
315
                /* no standart message expected: do nothing */
237
                    break;
238
                default:
239
                    break; /* no other notifications expected: do nothing */
316 240
            }
241
        } else { /* received standart message, not a notification */
242
            /* no standart message expected: do nothing */
317 243
        }
244
    }
318 245

  
319
        while(list_size(shooter_list) > 0){
320
            gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
321
            gunner_dtor(p);
322
        }
246
    while(list_size(shooter_list) > 0){
247
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
248
        gunner_dtor(p);
249
    }
323 250

  
324
        while(list_size(bullet_list) > 0){
325
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
326
            bullet_dtor(p);
327
        }
328
        if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
329
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
251
    while(list_size(bullet_list) > 0){
252
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
253
        bullet_dtor(p);
254
    }
255
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
256
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
330 257

  
331
        timer_dtor(in_game_timer); in_game_timer = NULL;
258
    timer_dtor(in_game_timer); in_game_timer = NULL;
332 259

  
333
        basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
334
        basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
335
        sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
336
        basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
337
        basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
338
        map_dtor               (map1         ); map1          = NULL;
339
        font_dtor              (consolas     ); consolas      = NULL;
260
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
261
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
262
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
263
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
264
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
265
    map_dtor               (map1         ); map1          = NULL;
266
    font_dtor              (consolas     ); consolas      = NULL;
340 267

  
341
        // Unsubscribe interrupts
342
        if (unsubscribe_all()) {
343
            if (cleanup())
344
                printf("%s: failed to cleanup.\n", __func__);
345
            return 1;
346
        }
268
    // Unsubscribe interrupts
269
    if (unsubscribe_all()) {
270
        if (cleanup())
271
            printf("%s: failed to cleanup.\n", __func__);
272
        return 1;
273
    }
347 274

  
348 275

  
349
        if (cleanup()) {
350
            printf("%s: failed to cleanup.\n", __func__);
351
            return 1;
352
        }
353
    #endif
276
    if (cleanup()) {
277
        printf("%s: failed to cleanup.\n", __func__);
278
        return 1;
279
    }
354 280

  
355

  
356 281
    return 0;
357 282
}
proj/src/rtc.c
1
#include <lcom/lcf.h>
2

  
1 3
#include "rtc.h"
2 4

  
3
#include <lcom/lcf.h>
4

  
5 5
#include "utils.h"
6 6
#include "errors.h"
7 7

  
proj/src/test7.c
1 1
#include <lcom/lcf.h>
2 2

  
3 3
#include "errors.h"
4
#include "uart.h"
4
#include "nctp.h"
5 5

  
6 6
int ser_test_conf(unsigned short base_addr) {
7 7
	int ret = SUCCESS;
......
37 37
	if((ret = uart_disable_int_rx(base_addr))) return ret;
38 38
	if((ret = uart_disable_int_tx(base_addr))) return ret;
39 39
	if(tx == 0){
40
		uint8_t c;
41
		if((ret = uart_get_char_poll(base_addr, &c))) return ret;
42
		while((char)c != '.'){
43
			printf("%c", (char)c);
44
			if((ret = uart_get_char_poll(base_addr, &c))) return ret;
40
		uint8_t c = ' ';
41
		int r;
42
		while(c != '.'){
43
			r = nctp_get_char_poll(base_addr, &c);
44
			if(r == TIMEOUT_ERROR) continue;
45
			else if(r != SUCCESS ) return r;
46
			printf("%c", c);
45 47
		}
46
		printf("%c\n", (char)c);
47 48
	}else{
48 49
		for(int i = 0; i < stringc; ++i){
49 50
			for(int j = 0; strings[i][j] != 0; ++j)
50
				if((ret = uart_send_char_poll(base_addr, strings[i][j])))
51
				if((ret = nctp_send_char_poll(base_addr, strings[i][j])))
51 52
					return ret;
52 53
			if(i+1 != stringc)
53
				if((ret = uart_send_char_poll(base_addr, ' '))) return ret;
54
				if((ret = nctp_send_char_poll(base_addr, ' '))) return ret;
54 55
		}
55
		if((ret = uart_send_char_poll(base_addr, '.'))) return ret;
56
		if((ret = nctp_send_char_poll(base_addr, '.'))) return ret;
56 57
	}
57 58
	return SUCCESS;
58 59
}
proj/src/uart.c
61 61

  
62 62
/// LSR
63 63
#define UART_RECEIVER_READY_POS                 0
64
#define UART_OVERRUN_ERROR_POS                  1
65
#define UART_PARITY_ERROR_POS                   2
66
#define UART_FRAMING_ERROR_POS                  3
64 67
#define UART_TRANSMITTER_EMPTY_POS              5
65 68

  
66 69
#define UART_RECEIVER_READY                     (BIT(0))
70
#define UART_OVERRUN_ERROR                      (BIT(1))
71
#define UART_PARITY_ERROR                       (BIT(2))
72
#define UART_FRAMING_ERROR                      (BIT(3))
67 73
#define UART_TRANSMITTER_EMPTY                  (BIT(5))
68 74

  
69 75
#define UART_GET_RECEIVER_READY(n)              (((n)&UART_RECEIVER_READY           )>>UART_RECEIVER_READY_POS           )
76
#define UART_GET_OVERRUN_ERROR                  (((n)&UART_OVERRUN_ERROR            )>>UART_OVERRUN_ERROR_POS            )
77
#define UART_GET_PARITY_ERROR                   (((n)&UART_PARITY_ERROR             )>>UART_PARITY_ERROR_POS             )
78
#define UART_GET_FRAMING_ERROR                  (((n)&UART_FRAMING_ERROR            )>>UART_FRAMING_ERROR_POS            )
70 79
#define UART_GET_TRANSMITTER_EMPTY(n)           (((n)&UART_TRANSMITTER_EMPTY        )>>UART_TRANSMITTER_EMPTY_POS        )
71 80

  
72 81
static void uart_parse_config(uart_config *config){
......
241 250
    return uart_set_ier(base_addr, ier);
242 251
}
243 252

  
244
int uart_get_char_poll(int base_addr, uint8_t *p){
253
static int uart_has_communication_error(int base_addr){
245 254
    int ret;
246
    while(!uart_receiver_ready(base_addr)){}
247
    if((ret = uart_get_char(base_addr, p))) return ret;
255
    uint8_t lsr;
256
    if((ret = uart_get_lsr(base_addr, &lsr))) return 1;
257
    lsr &= (UART_OVERRUN_ERROR &
258
            UART_PARITY_ERROR  &
259
            UART_FRAMING_ERROR);
260
    return (lsr ? 1 : 0);
261
}
262

  
263
#include "nctp.h"
264

  
265
#define NCTP_START      0x80
266
#define NCTP_END        0xFF
267
#define NCTP_TRIES      3
268
#define NCTP_OK         0xFF
269
#define NCTP_NOK        0x00
270
#define NCTP_MAX_SIZE   1024 //in bytes
271

  
272
int nctp_send_char_poll(int base_addr, uint8_t  c){
273
    for(int i = 0; i < NCTP_TRIES; ++i)
274
        if(uart_transmitter_empty(base_addr))
275
            return uart_send_char(base_addr, c);
276
    return TIMEOUT_ERROR;
277
}
278
int nctp_get_char_poll (int base_addr, uint8_t *p){
279
    for(int i = 0; i < NCTP_TRIES; ++i)
280
        if(uart_receiver_ready(base_addr))
281
            return uart_get_char(base_addr, p);
282
    return TIMEOUT_ERROR;
283
}
284

  
285
int nctp_expect_ok(int base_addr){
286
    int ret;
287
    uint8_t ok;
288
    if((ret = nctp_get_char_poll(base_addr, &ok))) return ret;
289
    int cnt = 0;
290
    while(ok){
291
        cnt += ok&1;
292
        ok >>= 1;
293
    }
294
    if(cnt > 4) return SUCCESS;
295
    else        return NOK;
296
}
297

  
298
int nctp_send_char_try(int base_addr, uint8_t  c){
299
    int ret;
300
    for(size_t k = 0; k < NCTP_TRIES; ++k){
301
        if((ret = nctp_send_char_poll(base_addr, c))) return ret;
302
        if(nctp_expect_ok(base_addr) == SUCCESS) return SUCCESS;
303
    }
304
    return TRANS_FAILED;
305
}
306
int nctp_get_char_try (int base_addr, uint8_t *p){
307
    int ret;
308
    for(size_t k = 0; k < NCTP_TRIES; ++k){
309
        if((ret = nctp_get_char_poll (base_addr, p))) return ret;
310
        if(!uart_has_communication_error(base_addr))
311
            return nctp_send_char_try(base_addr, NCTP_OK ); //If it does not have any errors
312
    }
313
    if((ret = nctp_send_char_poll(base_addr, NCTP_NOK))) return ret;
314
    return TRANS_FAILED;
315
}
316

  
317
int nctp_send(int port, size_t num, uint8_t* ptr[], size_t sz[]){
318
    {
319
        int cnt = 0;
320
        for(size_t i = 0; i < num; ++i){
321
            cnt += sz[i];
322
            if(cnt > NCTP_MAX_SIZE) return TRANS_REFUSED;
323
        }
324
    }
325

  
326
    int base_addr;{
327
        switch(port){
328
            case 1: base_addr = COM1_ADDR; break;
329
            case 2: base_addr = COM2_ADDR; break;
330
            default: return INVALID_ARG;
331
        }
332
    }
333

  
334
    int ret;
335

  
336
    if((ret = uart_disable_int_rx(base_addr))) return ret;
337
    if((ret = uart_disable_int_tx(base_addr))) return ret;
338

  
339
    if((ret = nctp_send_char_try(base_addr, NCTP_START))) return ret;
340
    for(size_t i = 0; i < num; ++i){
341
        uint8_t *p = ptr[i]; size_t s = sz[i];
342
        for(size_t j = 0; j < s; ++j, ++p)
343
            if((ret = nctp_send_char_try(base_addr, *p)))
344
                return ret;
345
    }
346
    if((ret = nctp_send_char_try(base_addr, NCTP_END))) return ret;
347

  
248 348
    return SUCCESS;
249 349
}
250
int uart_send_char_poll(int base_addr, uint8_t  c){
350
int ntcp_get(int port, uint8_t *dest){
351
    int base_addr;{
352
        switch(port){
353
            case 1: base_addr = COM1_ADDR; break;
354
            case 2: base_addr = COM2_ADDR; break;
355
            default: return INVALID_ARG;
356
        }
357
    }
358

  
251 359
    int ret;
252
    while(!uart_transmitter_empty(base_addr)){}
253
    if((ret = uart_send_char(base_addr, c))) return ret;
360
    free(dest);
361
    dest = malloc(NCTP_MAX_SIZE*sizeof(uint8_t)); size_t i = 0;
362
    if(dest == NULL) return NULL_PTR;
363
    uint8_t c;
364
    if((ret = nctp_get_char_try (base_addr, &c     ))) return ret;
365
    while(true){
366
        if(i >= NCTP_MAX_SIZE) return TRANS_REFUSED;
367
        if((ret = nctp_get_char_try (base_addr, &c))) return ret;
368
        if(c == NCTP_END) break;
369
        else              dest[i] = c;
370
        ++i;
371
    }
254 372
    return SUCCESS;
255 373
}

Also available in: Unified diff