Project

General

Profile

Revision 145

transfering files

View differences:

proj/Makefile
1 1
PROG=proj
2 2

  
3
SRCS= proj.c utils.c
3
SRCS= proj.c
4 4

  
5 5
CPPFLAGS += -pedantic -D __LCOM_OPTIMIZED_
6 6

  
proj/errors.h
1 1
#ifndef ERRORS_H_INCLUDED
2 2
#define ERRORS_H_INCLUDED
3 3

  
4
/** @brief Error Codes */
4 5
enum errors {
5 6
    SUCCESS = 0,        /* @brief Sucessful */
6 7
    NULL_PTR,           /* @brief Null Pointer Error */
proj/kbc.c
1
#include <lcom/lcf.h>
2

  
3
#include "kbc.h"
4

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

  
9
int (kbc_read_cmd)(uint8_t *cmd){
10
    int ret = 0;
11
    if((ret = kbc_issue_cmd(READ_KBC_CMD))) return ret;
12
    if((ret = kbc_read_byte(cmd))) return ret;
13
    return SUCCESS;
14
}
15

  
16
int (kbc_change_cmd)(uint8_t cmd){
17
    int ret = 0;
18
    if((ret = kbc_issue_cmd(WRITE_KBC_CMD))) return ret;
19
    if((ret = kbc_issue_arg(cmd))) return ret;
20
    return SUCCESS;
21
}
22

  
23
int (kbc_restore_kbd)(){
24
    int ret = 0;
25
    uint8_t cmd = 0;
26
    if((ret = kbc_read_cmd(&cmd))) return ret;
27
    cmd = (cmd | INT_KBD) & (~DIS_KBD);
28
    if((ret = kbc_change_cmd(cmd))) return ret;
29
    return SUCCESS;
30
}
31

  
32
int (kbc_issue_cmd)(uint8_t cmd){
33
    int ret = 0;
34
    uint8_t stat;
35
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
36
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
37
        if((stat&IN_BUF_FULL) == 0){
38
            if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR;
39
            return SUCCESS;
40
        }
41
        tickdelay(micros_to_ticks(DELAY));
42
    }
43
    return TIMEOUT_ERROR;
44
}
45

  
46
int (kbc_issue_arg)(uint8_t arg){
47
    int ret = 0;
48
    uint8_t stat;
49
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
50
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
51
        if((stat&IN_BUF_FULL) == 0){
52
            if(sys_outb(KBC_CMD_ARG, arg)) return WRITE_ERROR;
53
            return SUCCESS;
54
        }
55
        tickdelay(micros_to_ticks(DELAY));
56
    }
57
    return TIMEOUT_ERROR;
58
}
59

  
60
int (kbc_read_byte)(uint8_t *byte){
61
    int ret = 0;
62
    uint8_t stat;
63
    for(int i = 0; i < KBC_NUM_TRIES; ++i){
64
        if((ret = util_sys_inb(STATUS_REG, &stat))) return ret;
65
        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
66
            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR;
67
            if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret;
68
            else return SUCCESS;
69
        }
70
        tickdelay(micros_to_ticks(DELAY));
71
    }
72
    printf("Timing out\n");
73
    return TIMEOUT_ERROR;
74
}
0 75

  
proj/kbc.h
1
/**
2
 * This file concerns everything related to the KBC (KeyBoard Controller, which
3
 * actually also manages the mouse)
4
 */
5

  
6
#ifndef KBC_H_INCLUDED
7
#define KBC_H_INCLUDED
8

  
9
/**
10
 * @brief High-level function that reads the command byte of the KBC
11
 * @param cmd Pointer to variable where command byte read from KBC will be stored
12
 * @return 0 if operation was successful, 1 otherwise
13
 */
14
int (kbc_read_cmd)(uint8_t *cmd);
15

  
16
/**
17
 * @brief High-level function that changes the command byte of the KBC
18
 * @param cmd New value for command byte of KBC
19
 * @return 0 if operation was successful, 1 otherwise
20
 */
21
int (kbc_change_cmd)(uint8_t cmd);
22

  
23
/**
24
 * @brief High-level function that restores KBC to normal state
25
 * High-level function that restores KBC to normal state, because lcf_start
26
 * changes the command byte of KBC. If this function is not used, there is a
27
 * chance that the keyboard and keyboard interrupts remain disabled.
28
 * @return 0 if operation was successful, 1 otherwise
29
 */
30
int (kbc_restore_keyboard)();
31

  
32
/**
33
 * @brief Low-level function to issue a command to keyboard
34
 * @param cmd command to be issued
35
 * @return 0 if operation was successful, 1 otherwise
36
 */
37
int (kbc_issue_cmd)(uint8_t cmd);
38

  
39
/**
40
 * @brief Low-level function to issue an argument of a command
41
 * @param cmd argument to be issued
42
 * @return 0 if operation was successful, 1 otherwise
43
 */
44
int (kbc_issue_arg)(uint8_t arg);
45

  
46
/**
47
 * @brief Low-level function for reading byte from keyboard
48
 * Low-level function for reading byte from keyboard. Waits until output buffer
49
 * is full
50
 * @param value Pointer to variable where byte read from keyboard will be stored
51
 * @return 0 if operation was successful, 1 otherwise
52
 */
53
int (kbc_read_byte)(uint8_t *byte);
54

  
55
#endif //KBC_H_INCLUDED
0 56

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

  
3
#include "keyboard.h"
4

  
5
#include "kbc.h"
6
#include "kbc_macros.h"
7
#include "utils.h"
8
#include "errors.h"
9

  
10
int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
11
    if (interrupt_id == NULL) return NULL_PTR;
12
    *interrupt_id = interrupt_bit;
13
    if(sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
14
    return SUCCESS;
15
}
16

  
17
int done = 1;
18
int sz = 1;
19
int got_error_keyboard = SUCCESS;
20

  
21
void (kbc_ih)(void) {
22
    if(done) sz = 1;
23
    else     sz++;
24
    uint8_t status = 0;
25
    got_error_keyboard = SUCCESS;
26
    if ((got_error_keyboard = util_sys_inb(STATUS_REG, &status))) return;
27
    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
28
        got_error_keyboard = 1;
29
        return;
30
    }
31
    if ((status & OUT_BUF_FUL) == 0 || (status & AUX_MOUSE) != 0) {
32
        got_error_keyboard = READ_ERROR;
33
        return;
34
    }
35
    uint8_t byte = 0;
36
    if ((got_error_keyboard = util_sys_inb(OUTPUT_BUF, &byte))) return;
37

  
38
    scancode[sz-1] = byte;
39
    done = !(TWO_BYTE_CODE == byte);
40
}
41

  
42
int (keyboard_poll)(uint8_t bytes[], uint8_t *size){
43
    int ret = 0;
44
    if(bytes == NULL || size == NULL) return NULL_PTR;
45
    uint8_t c;
46
    if((ret = kbc_read_byte(&c))) return ret;
47
    if(c == TWO_BYTE_CODE){
48
        if((ret = kbc_read_byte(&bytes[1]))) return ret;
49
        bytes[0] = c;
50
        *size = 2;
51
    }else{
52
        bytes[1] = 0;
53
        bytes[0] = c;
54
        *size = 1;
55
    }
56
    return SUCCESS;
57
}
0 58

  
proj/keyboard.h
1
/**
2
 * This file concerns everything related to the keyboard
3
 */
4

  
5
#ifndef KEYBOARD_H_INCLUDED
6
#define KEYBOARD_H_INCLUDED
7

  
8
int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
9

  
10
uint8_t scancode[2];
11
int done;
12
int sz;
13
int got_error_keyboard;
14

  
15
void (kbc_ih)(void);
16

  
17
int (keyboard_poll)(uint8_t bytes[], uint8_t *size);
18

  
19
#endif //KEYBOARD_H_INCLUDED
0 20

  
proj/utils.c
17 17
    return SUCCESS;
18 18
}
19 19

  
20
#ifdef LAB3
21
    uint32_t sys_inb_counter = 0;
22
#endif
23

  
24 20
int (util_sys_inb)(int port, uint8_t *value) {
25 21
    if(value == NULL) return NULL_PTR;
26 22
    uint32_t n = 0;
......
29 25
    return SUCCESS;
30 26
}
31 27

  
32
int16_t abs16(int16_t x) {
33
    return (x >= 0) ? (int16_t)(x) : (int16_t)(-x);
28
int (unsubscribe_interrupt)(int *interrupt_id) {
29
    if (interrupt_id == NULL) return NULL_PTR;
30
    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
31
    return SUCCESS;
34 32
}
35 33

  
36 34
int16_t min(int16_t a, int16_t b){ return (b < a ? b : a); }
proj/utils.h
26 26
int (util_sys_inb)(int port, uint8_t *value);
27 27

  
28 28
/**
29
 * @brief Unsubcribes Interrupts
30
 * @param interrupt_id Interrupt ID, value via arguments on subscription of the interrupt_id
31
 * @see subscribe_kbc_interrupt, subscribe_timer_interrupt
32
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
33
 */
34
int (unsubscribe_interrupt)(int *interrupt_id);
35

  
36
/**
29 37
 * @brief Gets the minimum value out of two values.
30 38
 * @param a     First value
31 39
 * @param b     Second value

Also available in: Unified diff