Revision 284
changed more stuff
proj/libs/peripherals/include/uart.h | ||
---|---|---|
1 |
#ifndef UART_H_INCLUDED |
|
2 |
#define UART_H_INCLUDED |
|
3 |
|
|
4 |
#define COM1_ADDR 0x3F8 |
|
5 |
#define COM2_ADDR 0x2F8 |
|
6 |
#define COM1_IRQ 4 |
|
7 |
#define COM2_IRQ 3 |
|
8 |
#define COM1_VECTOR 0x0C |
|
9 |
#define COM2_VECTOR 0x0B |
|
10 |
|
|
11 |
typedef enum { |
|
12 |
uart_parity_none = 0x0, |
|
13 |
uart_parity_odd = 0x1, |
|
14 |
uart_parity_even = 0x3, |
|
15 |
uart_parity_par1 = 0x5, |
|
16 |
uart_parity_par0 = 0x7 |
|
17 |
} uart_parity; |
|
18 |
|
|
19 |
typedef struct { |
|
20 |
int base_addr ; |
|
21 |
uint8_t lcr ; |
|
22 |
uint8_t dll ; |
|
23 |
uint8_t dlm ; |
|
24 |
uint8_t bits_per_char ; |
|
25 |
uint8_t stop_bits ; |
|
26 |
uart_parity parity ; |
|
27 |
uint8_t break_control :1; |
|
28 |
uint8_t dlab :1; |
|
29 |
uint16_t divisor_latch ; |
|
30 |
uint8_t ier ; |
|
31 |
uint8_t received_data_int :1; |
|
32 |
uint8_t transmitter_empty_int :1; |
|
33 |
uint8_t receiver_line_stat_int:1; |
|
34 |
uint8_t modem_stat_int :1; |
|
35 |
} uart_config; |
|
36 |
|
|
37 |
int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
|
38 |
|
|
39 |
int uart_get_config(int base_addr, uart_config *config); |
|
40 |
void uart_print_config(uart_config config); |
|
41 |
|
|
42 |
int uart_set_bits_per_character(int base_addr, uint8_t bits_per_char); |
|
43 |
int uart_set_stop_bits (int base_addr, uint8_t stop ); |
|
44 |
int uart_set_parity (int base_addr, uart_parity par ); |
|
45 |
int uart_set_bit_rate (int base_addr, float bit_rate ); |
|
46 |
|
|
47 |
int uart_enable_int_rx (int base_addr); |
|
48 |
int uart_disable_int_rx(int base_addr); |
|
49 |
int uart_enable_int_tx (int base_addr); |
|
50 |
int uart_disable_int_tx(int base_addr); |
|
51 |
|
|
52 |
/// NCTP - Non-critical transmission protocol |
|
53 |
int nctp_init(void); |
|
54 |
int nctp_free(void); |
|
55 |
|
|
56 |
int nctp_send(size_t num, uint8_t* ptr[], size_t sz[]); |
|
57 |
|
|
58 |
int nctp_ih_err; |
|
59 |
void nctp_ih(void); |
|
60 |
|
|
61 |
/// HLTP - High-level transmission protocol |
|
62 |
int hltp_send_string(const char *p); |
|
63 |
|
|
64 |
#endif //UART_H_INCLUDED |
|
65 | 0 |
proj/libs/peripherals/include/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 |
/* KBC IRQ Line */ |
|
10 |
|
|
11 |
#define KBC_IRQ 1 /* @brief KBC Controller IRQ Line */ |
|
12 |
#define MOUSE_IRQ 12 /* @brief Mouse IRQ Line */ |
|
13 |
|
|
14 |
/* Delay for KBC */ |
|
15 |
#define DELAY 20000 /* @brief KBC Response Delay */ |
|
16 |
#define KBC_NUM_TRIES 20 /* @brief Number of tries to issue command before timeout */ |
|
17 |
|
|
18 |
/* I/O Ports Addresses */ |
|
19 |
|
|
20 |
#define KBC_CMD 0x64 /* @brief Address to send commands to KBC */ |
|
21 |
#define KBC_CMD_ARG 0x60 /* @brief Address to write KBC Command Arguments */ |
|
22 |
#define STATUS_REG 0x64 /* @brief KBC Status Register address */ |
|
23 |
|
|
24 |
#define OUTPUT_BUF 0x60 /* @brief Address of Output Buffer of KBC */ |
|
25 |
|
|
26 |
/* KBC Commands */ |
|
27 |
#define READ_KBC_CMD 0x20 /* @brief Read KBC Command Byte */ |
|
28 |
#define WRITE_KBC_CMD 0x60 /* @brief Write KBC Command Byte */ |
|
29 |
#define KBC_SELF_TEST 0xAA /* @brief KBC Diagnostic Tests */ |
|
30 |
#define KBC_INT_TEST 0xAB /* @brief Tests Keyboard Clock and Data lines */ |
|
31 |
#define KBC_INT_DISABLE 0xAD /* @brief Disable KBC Interface */ |
|
32 |
#define KBC_INT_ENABLE 0xAE /* @brief Enable KBC Interface */ |
|
33 |
#define MOUSE_DISABLE 0xA7 /* @brief Disable Mouse */ |
|
34 |
#define MOUSE_ENABLE 0xA8 /* @brief Enable Mouse */ |
|
35 |
#define MOUSE_INT_TEST 0xA9 /* @brief Tests Mouse data line */ |
|
36 |
#define MOUSE_WRITE_B 0xD4 /* @brief Write a byte directly to the mouse */ |
|
37 |
|
|
38 |
/* Status Byte Masking */ |
|
39 |
|
|
40 |
#define OUT_BUF_FUL BIT(0) /* @brief Output Buffer State */ |
|
41 |
#define IN_BUF_FULL BIT(1) /* @brief Input Buffer State */ |
|
42 |
#define SYS_FLAG BIT(2) /* @brief System Flag */ |
|
43 |
#define DATA_CMD_WRITE BIT(3) /* @brief Identifier of type of byte in input buffer */ |
|
44 |
#define INH_FLAG BIT(4) /* @brief Keyboard inihibited */ |
|
45 |
#define AUX_MOUSE BIT(5) /* @brief Mouse Data */ |
|
46 |
#define TIME_OUT_REC BIT(6) /* @brief Time Out Error - Invalid Data */ |
|
47 |
#define PARITY_ERROR BIT(7) /* @brief Parity Error - Invalid Data */ |
|
48 |
|
|
49 |
/* Scancode Constants */ |
|
50 |
|
|
51 |
#define ESC_BREAK_CODE 0x81 /* @brief ESC Break Code */ |
|
52 |
#define TWO_BYTE_CODE 0xE0 /* @brief First byte of a two byte Scancode */ |
|
53 |
#define BREAK_CODE_BIT BIT(7) /* @brief Bit to distinguish between Make code and Break code */ |
|
54 |
|
|
55 |
/* Command byte masks */ |
|
56 |
#define INT_KBD BIT(0) /* @brief Enable Keyboard Interrupts */ |
|
57 |
#define INT_MOU BIT(1) /* @brief Enable Mouse Interrupts */ |
|
58 |
#define DIS_KBD BIT(4) /* @brief Disable Keyboard */ |
|
59 |
#define DIS_MOU BIT(5) /* @brief Disable Mouse */ |
|
60 |
|
|
61 |
/** |
|
62 |
* @brief High-level function that reads the command byte of the KBC |
|
63 |
* @param cmd Pointer to variable where command byte read from KBC will be stored |
|
64 |
* @return 0 if operation was successful, 1 otherwise |
|
65 |
*/ |
|
66 |
int (kbc_read_cmd)(uint8_t *cmd); |
|
67 |
|
|
68 |
/** |
|
69 |
* @brief High-level function that changes the command byte of the KBC |
|
70 |
* @param cmd New value for command byte of KBC |
|
71 |
* @return 0 if operation was successful, 1 otherwise |
|
72 |
*/ |
|
73 |
int (kbc_change_cmd)(uint8_t cmd); |
|
74 |
|
|
75 |
/** |
|
76 |
* @brief High-level function that restores KBC to normal state |
|
77 |
* High-level function that restores KBC to normal state, because lcf_start |
|
78 |
* changes the command byte of KBC. If this function is not used, there is a |
|
79 |
* chance that the keyboard and keyboard interrupts remain disabled. |
|
80 |
* @return 0 if operation was successful, 1 otherwise |
|
81 |
*/ |
|
82 |
int (kbc_restore_keyboard)(); |
|
83 |
|
|
84 |
/** |
|
85 |
* @brief Low-level function to issue a command to keyboard |
|
86 |
* @param cmd command to be issued |
|
87 |
* @return 0 if operation was successful, 1 otherwise |
|
88 |
*/ |
|
89 |
int (kbc_issue_cmd)(uint8_t cmd); |
|
90 |
|
|
91 |
/** |
|
92 |
* @brief Low-level function to issue an argument of a command |
|
93 |
* @param cmd argument to be issued |
|
94 |
* @return 0 if operation was successful, 1 otherwise |
|
95 |
*/ |
|
96 |
int (kbc_issue_arg)(uint8_t arg); |
|
97 |
|
|
98 |
/** |
|
99 |
* @brief Low-level function for reading byte from keyboard |
|
100 |
* Low-level function for reading byte from keyboard. Waits until output buffer |
|
101 |
* is full |
|
102 |
* @param value Pointer to variable where byte read from keyboard will be stored |
|
103 |
* @return 0 if operation was successful, 1 otherwise |
|
104 |
*/ |
|
105 |
int (kbc_read_byte)(uint8_t *byte); |
|
106 |
|
|
107 |
#endif //KBC_H_INCLUDED |
|
108 | 0 |
proj/libs/peripherals/include/timer.h | ||
---|---|---|
1 |
/** |
|
2 |
* This file concerns everything related to the timer |
|
3 |
*/ |
|
4 |
|
|
5 |
#ifndef TIMER_H_INCLUDED |
|
6 |
#define TIMER_H_INCLUDED |
|
7 |
|
|
8 |
#define TIMER0_IRQ 0 /**< @brief Timer 0 IRQ line */ |
|
9 |
|
|
10 |
int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
|
11 |
|
|
12 |
uint32_t no_interrupts; |
|
13 |
|
|
14 |
#endif //TIMER_H_INCLUDED |
|
15 | 0 |
proj/libs/peripherals/include/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 |
#include "kbc.h" |
|
9 |
|
|
10 |
/** |
|
11 |
* @brief Subscribes Keyboard Interrupts and disables Minix Default IH |
|
12 |
* @param interrupt_bit Bit of Interrupt Vector that will be set when Keyboard Interrupt is pending |
|
13 |
* @param interrupt_id Keyboard Interrupt ID to specify the Keyboard Interrupt in other calls |
|
14 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
15 |
* @see {_ERRORS_H_::errors} |
|
16 |
*/ |
|
17 |
int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
|
18 |
|
|
19 |
uint8_t scancode[2]; |
|
20 |
int done; |
|
21 |
int sz; |
|
22 |
int got_error_keyboard; |
|
23 |
|
|
24 |
void (kbc_ih)(void); |
|
25 |
|
|
26 |
int (keyboard_poll)(uint8_t bytes[], uint8_t *size); |
|
27 |
|
|
28 |
#endif //KEYBOARD_H_INCLUDED |
|
29 | 0 |
proj/libs/peripherals/include/graph.h | ||
---|---|---|
1 |
#ifndef GRAPH_H_INCLUDED |
|
2 |
#define GRAPH_H_INCLUDED |
|
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 |
|
|
13 |
// Colors in RBG (8 bit) |
|
14 |
#define GRAPH_BLACK 0x000000 |
|
15 |
#define GRAPH_WHITE 0xFFFFFF |
|
16 |
|
|
17 |
// Alpha |
|
18 |
#define ALPHA_THRESHOLD 0x7F |
|
19 |
|
|
20 |
/// MACROS |
|
21 |
#define GET_ALP(n) (0xFF & ((n) >> 24)) |
|
22 |
#define GET_RED(n) (0xFF & ((n) >> 16)) |
|
23 |
#define GET_GRE(n) (0xFF & ((n) >> 8)) |
|
24 |
#define GET_BLU(n) (0xFF & (n )) |
|
25 |
#define GET_COLOR(n) (0xFFFFFF & (n)) |
|
26 |
#define SET_ALP(n) (((n)&0xFF) << 24) |
|
27 |
#define SET_RED(n) (((n)&0xFF) << 16) |
|
28 |
#define SET_GRE(n) (((n)&0xFF) << 8) |
|
29 |
#define SET_BLU(n) (((n)&0xFF) ) |
|
30 |
#define SET_RGB(r,g,b) (SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
31 |
|
|
32 |
/// PUBLIC GET |
|
33 |
uint16_t (graph_get_XRes) (void); |
|
34 |
uint16_t (graph_get_YRes) (void); |
|
35 |
uint16_t (graph_get_bytes_pixel) (void); |
|
36 |
|
|
37 |
/// INIT |
|
38 |
int (graph_init)(uint16_t mode); |
|
39 |
|
|
40 |
/// CLEANUP |
|
41 |
int (graph_cleanup)(void); |
|
42 |
|
|
43 |
/// PIXEL DRAWING |
|
44 |
int (graph_set_pixel) (uint16_t x, uint16_t y, uint32_t color); |
|
45 |
void (graph_set_pixel_pos) (unsigned pos , uint32_t color); |
|
46 |
|
|
47 |
/// SCREEN |
|
48 |
int (graph_clear_screen)(void); |
|
49 |
|
|
50 |
/// DRAW |
|
51 |
int (graph_draw)(void); |
|
52 |
|
|
53 |
#endif /* end of include guard: GRAPH_H_INCLUDED */ |
|
54 | 0 |
proj/libs/peripherals/include/mouse.h | ||
---|---|---|
1 |
#ifndef MOUSE_H_INCLUDED |
|
2 |
#define MOUSE_H_INCLUDED |
|
3 |
|
|
4 |
/** |
|
5 |
* @brief Subscribes Mouse Interrupts and disables Minix Default IH |
|
6 |
* @param interrupt_bit Bit of Interrupt Vector that will be set when Mouse Interrupt is pending |
|
7 |
* @param interrupt_id Mouse Interrupt ID to specify the Mouse Interrupt in other calls |
|
8 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
9 |
* @see {_ERRORS_H_::errors} |
|
10 |
*/ |
|
11 |
int (subscribe_mouse_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
|
12 |
|
|
13 |
//These have to do with mouse_ih |
|
14 |
int got_error_mouse_ih; |
|
15 |
uint8_t packet_mouse_ih[3]; |
|
16 |
int counter_mouse_ih; |
|
17 |
|
|
18 |
/** |
|
19 |
* @brief Parse 3 bytes and returns it as a parsed, struct packet. |
|
20 |
* @param packet_bytes array of bytes to parse |
|
21 |
* @param pp Pointer to packet to store the information. |
|
22 |
*/ |
|
23 |
void (mouse_parse_packet)(const uint8_t *packet_bytes, struct packet *pp); |
|
24 |
|
|
25 |
/** |
|
26 |
* @brief Polls mouse for data. Blocks execution until a valid mouse packet is obtained. |
|
27 |
* @param pp pointer to packet struct in which the result will be stored |
|
28 |
* @param period time (in milliseconds) the poller should wait between pollings of bytes |
|
29 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
30 |
*/ |
|
31 |
int mouse_poll(struct packet *pp, uint16_t period); |
|
32 |
|
|
33 |
/** |
|
34 |
* @brief Sets data report mode for mouse |
|
35 |
* @param on zero to disable data report, any other value to enable data report |
|
36 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
37 |
*/ |
|
38 |
int (mouse_set_data_report)(int on); |
|
39 |
|
|
40 |
/** |
|
41 |
* @brief Reads data byte from mouse |
|
42 |
* <summary> |
|
43 |
* Polls the mouse till data is available for reading |
|
44 |
* </summary> |
|
45 |
* @param data Pointer to variable where byte read from mouse will be stored |
|
46 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
47 |
* @see {_ERRORS_H_::errors} |
|
48 |
*/ |
|
49 |
int (mouse_read_data)(uint8_t *data, uint16_t period); |
|
50 |
|
|
51 |
/** |
|
52 |
* @brief Issues command to mouse |
|
53 |
* <summary> |
|
54 |
* Issues command to mouse, returns error after two consecutive errors reported by the acknowledgment byte |
|
55 |
* </summary> |
|
56 |
* @param cmd Command to be issued |
|
57 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
58 |
* @see {_ERRORS_H_::errors} |
|
59 |
*/ |
|
60 |
int (mouse_issue_cmd)(uint32_t cmd); |
|
61 |
|
|
62 |
/** |
|
63 |
* @brief Reads byte from mouse |
|
64 |
* <summary> |
|
65 |
* Reads byte from mouse, giving error if exceeds number of tries to read |
|
66 |
* </summary> |
|
67 |
* @param byte Pointer to variable where byte read from mouse will be stored |
|
68 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
69 |
* @see {_ERRORS_H_::errors} |
|
70 |
*/ |
|
71 |
int (mouse_read_byte)(uint8_t *byte); |
|
72 |
|
|
73 |
/** |
|
74 |
* @brief Polls OUT_BUF for byte coming from mouse. |
|
75 |
* @param byte pointer to byte read from OUT_BUF |
|
76 |
* @param period time (in milliseconds) the poller should wait between pollings of bytes |
|
77 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
78 |
*/ |
|
79 |
int (mouse_poll_byte)(uint8_t *byte, uint16_t period); |
|
80 |
|
|
81 |
/** |
|
82 |
* @brief Converts 9-bit number to 16-bit with sign extension |
|
83 |
* @param sign_bit Sign bit identifiying the signal of the number |
|
84 |
* @param byte Least significant byte that will be extended |
|
85 |
* @return Extended 9-bit number |
|
86 |
*/ |
|
87 |
int16_t (sign_extend_byte)(uint8_t sign_bit, uint8_t byte); |
|
88 |
|
|
89 |
#endif //MOUSE_H_INCLUDED |
|
90 | 0 |
proj/libs/peripherals/include/rtc.h | ||
---|---|---|
1 |
#ifndef RTC_H_INCLUDED |
|
2 |
#define RTC_H_INCLUDED |
|
3 |
|
|
4 |
#include <stdint.h> |
|
5 |
|
|
6 |
/** |
|
7 |
* @brief Subscribes RTC Interrupts |
|
8 |
* @param interrupt_bit Bit of Interrupt Vector that will be set when RTC Interrupt is pending |
|
9 |
* @param interrupt_id RTC Interrupt ID to specify the RTC Interrupt in other calls |
|
10 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
11 |
* @see {_ERRORS_H_::errors} |
|
12 |
*/ |
|
13 |
int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
|
14 |
|
|
15 |
int (rtc_read_register)(uint32_t reg, uint8_t *data); |
|
16 |
|
|
17 |
int (rtc_write_register)(uint32_t reg, uint8_t data); |
|
18 |
|
|
19 |
/** |
|
20 |
* @brief Checks if there's an update in progress. |
|
21 |
* @return The value of the flag UIP, or ERROR_CODE if error occurs. |
|
22 |
*/ |
|
23 |
int (rtc_check_update)(void); |
|
24 |
|
|
25 |
/** |
|
26 |
* @brief Enables/Disables updates of time/date registers. |
|
27 |
* @param on zero to disable, any other value to enable |
|
28 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
29 |
*/ |
|
30 |
int (rtc_set_updates)(int on); |
|
31 |
|
|
32 |
/** |
|
33 |
* @brief Reads time from RTC. |
|
34 |
* @param time Pointer to array of 3 bytes to store the information about time (hours, minutes, seconds) |
|
35 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
36 |
*/ |
|
37 |
int (rtc_read_time)(uint8_t *time); |
|
38 |
|
|
39 |
/** |
|
40 |
* @brief Reads date from RTC. |
|
41 |
* @param date Pointer to array of 4 bytes to store the information about date (year, month, day, weekday) |
|
42 |
* @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK |
|
43 |
*/ |
|
44 |
int (rtc_read_date)(uint8_t *date); |
|
45 |
|
|
46 |
#endif /* end of include guard: RTC_H_INCLUDED */ |
|
47 | 0 |
proj/libs/peripherals/src/uart.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "uart.h" |
|
4 |
|
|
5 |
#include "queue.h" |
|
6 |
#include "errors.h" |
|
7 |
|
|
8 |
#define UART_BITRATE 115200 |
|
9 |
#define UART_WAIT 20 //microseconds |
|
10 |
|
|
11 |
#define UART_RBR 0 |
|
12 |
#define UART_THR 0 |
|
13 |
#define UART_IER 1 |
|
14 |
#define UART_IIR 2 |
|
15 |
#define UART_FCR 2 |
|
16 |
#define UART_LCR 3 |
|
17 |
#define UART_MCR 4 |
|
18 |
#define UART_LSR 5 |
|
19 |
#define UART_MSR 6 |
|
20 |
#define UART_SR 7 |
|
21 |
|
|
22 |
#define UART_DLL 0 |
|
23 |
#define UART_DLM 1 |
|
24 |
|
|
25 |
/// LCR |
|
26 |
#define UART_BITS_PER_CHAR_POS 0 |
|
27 |
#define UART_STOP_BITS_POS 2 |
|
28 |
#define UART_PARITY_POS 3 |
|
29 |
#define UART_BREAK_CONTROL_POS 6 |
|
30 |
#define UART_DLAB_POS 7 |
|
31 |
|
|
32 |
#define UART_BITS_PER_CHAR (BIT(0) | BIT(1)) |
|
33 |
#define UART_STOP_BITS (BIT(2)) |
|
34 |
#define UART_PARITY (BIT(3) | BIT(4) | BIT(5)) |
|
35 |
#define UART_BREAK_CONTROL (BIT(6)) |
|
36 |
#define UART_DLAB (BIT(7)) |
|
37 |
|
|
38 |
#define UART_GET_BITS_PER_CHAR(n) (((n)&UART_BITS_PER_CHAR) + 5) |
|
39 |
#define UART_GET_STOP_BITS(n) (((n)&UART_STOP_BITS)? 2 : 1) |
|
40 |
#define UART_GET_PARITY(n) (((n)&UART_PARITY )>>UART_PARITY_POS ) |
|
41 |
#define UART_GET_BREAK_CONTROL(n) (((n)&UART_BREAK_CONTROL)>>UART_BREAK_CONTROL_POS) |
|
42 |
#define UART_GET_DLAB(n) (((n)&UART_DLAB )>>UART_DLAB_POS ) |
|
43 |
#define UART_GET_DIV_LATCH(m,l) ((m)<<8 | (l)) |
|
44 |
#define UART_GET_DLL(n) ((n)&0xFF) |
|
45 |
#define UART_GET_DLM(n) (((n)>>8)&0xFF) |
|
46 |
|
|
47 |
/// IER |
|
48 |
#define UART_INT_EN_RX_POS 0 |
|
49 |
#define UART_INT_EN_TX_POS 1 |
|
50 |
#define UART_INT_EN_RECEIVER_LINE_STAT_POS 2 |
|
51 |
#define UART_INT_EN_MODEM_STAT_POS 3 |
|
52 |
|
|
53 |
#define UART_INT_EN_RX (BIT(0)) |
|
54 |
#define UART_INT_EN_TX (BIT(1)) |
|
55 |
#define UART_INT_EN_RECEIVER_LINE_STAT (BIT(2)) |
|
56 |
#define UART_INT_EN_MODEM_STAT (BIT(3)) |
|
57 |
|
|
58 |
#define UART_GET_INT_EN_RX(n) (((n)&UART_INT_EN_RX )>>UART_INT_EN_RX_POS ) |
|
59 |
#define UART_GET_INT_EN_TX(n) (((n)&UART_INT_EN_TX )>>UART_INT_EN_TX_POS ) |
|
60 |
#define UART_GET_INT_EN_RECEIVER_LINE_STAT(n) (((n)&UART_INT_EN_RECEIVER_LINE_STAT)>>UART_INT_EN_RECEIVER_LINE_STAT_POS) |
|
61 |
#define UART_GET_INT_EN_MODEM_STAT(n) (((n)&UART_INT_EN_MODEM_STAT )>>UART_INT_EN_MODEM_STAT_POS ) |
|
62 |
|
|
63 |
/// LSR |
|
64 |
#define UART_RECEIVER_READY_POS 0 |
|
65 |
#define UART_OVERRUN_ERROR_POS 1 |
|
66 |
#define UART_PARITY_ERROR_POS 2 |
|
67 |
#define UART_FRAMING_ERROR_POS 3 |
|
68 |
#define UART_TRANSMITTER_EMPTY_POS 5 |
|
69 |
|
|
70 |
#define UART_RECEIVER_READY (BIT(0)) |
|
71 |
#define UART_OVERRUN_ERROR (BIT(1)) |
|
72 |
#define UART_PARITY_ERROR (BIT(2)) |
|
73 |
#define UART_FRAMING_ERROR (BIT(3)) |
|
74 |
#define UART_TRANSMITTER_EMPTY (BIT(5)) |
|
75 |
|
|
76 |
#define UART_GET_RECEIVER_READY(n) (((n)&UART_RECEIVER_READY )>>UART_RECEIVER_READY_POS ) |
|
77 |
#define UART_GET_OVERRUN_ERROR (((n)&UART_OVERRUN_ERROR )>>UART_OVERRUN_ERROR_POS ) |
|
78 |
#define UART_GET_PARITY_ERROR (((n)&UART_PARITY_ERROR )>>UART_PARITY_ERROR_POS ) |
|
79 |
#define UART_GET_FRAMING_ERROR (((n)&UART_FRAMING_ERROR )>>UART_FRAMING_ERROR_POS ) |
|
80 |
#define UART_GET_TRANSMITTER_EMPTY(n) (((n)&UART_TRANSMITTER_EMPTY )>>UART_TRANSMITTER_EMPTY_POS ) |
|
81 |
|
|
82 |
/// IIR |
|
83 |
#define UART_INT_PEND_POS 1 |
|
84 |
|
|
85 |
#define UART_INT_PEND (BIT(3)|BIT(2)|BIT(1)) |
|
86 |
|
|
87 |
#define UART_GET_IF_INT_PEND(n) (!((n)&1)) |
|
88 |
typedef enum { |
|
89 |
uart_int_receiver_line_stat = ( BIT(1) | BIT(0)), |
|
90 |
uart_int_rx = ( BIT(1) ), |
|
91 |
uart_int_char_timeout_fifo = (BIT(2) | BIT(1) ), |
|
92 |
uart_int_tx = ( BIT(0)), |
|
93 |
uart_int_modem_stat = (0) |
|
94 |
} uart_int_code; |
|
95 |
#define UART_GET_INT_PEND(n) ((uart_int_code)(((n)&UART_INT_PEND)>>UART_INT_PEND_POS)) |
|
96 |
|
|
97 |
int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
|
98 |
if (interrupt_id == NULL) return 1; |
|
99 |
*interrupt_id = interrupt_bit; |
|
100 |
return (sys_irqsetpolicy(COM1_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)); |
|
101 |
} |
|
102 |
|
|
103 |
static void uart_parse_config(uart_config *config){ |
|
104 |
/// LCR |
|
105 |
config->bits_per_char = UART_GET_BITS_PER_CHAR (config->lcr); |
|
106 |
config->stop_bits = UART_GET_STOP_BITS (config->lcr); |
|
107 |
config->parity = UART_GET_PARITY (config->lcr); if((config->parity & BIT(0)) == 0) config->parity = uart_parity_none; |
|
108 |
config->break_control = UART_GET_BREAK_CONTROL (config->lcr); |
|
109 |
config->dlab = UART_GET_DLAB (config->lcr); |
|
110 |
/// IER |
|
111 |
config->received_data_int = UART_GET_INT_EN_RX (config->ier); |
|
112 |
config->transmitter_empty_int = UART_GET_INT_EN_TX (config->ier); |
|
113 |
config->receiver_line_stat_int = UART_GET_INT_EN_RECEIVER_LINE_STAT(config->ier); |
|
114 |
config->modem_stat_int = UART_GET_INT_EN_MODEM_STAT (config->ier); |
|
115 |
/// DIV LATCH |
|
116 |
config->divisor_latch = UART_GET_DIV_LATCH(config->dlm, config->dll); |
|
117 |
} |
|
118 |
|
|
119 |
static int uart_get_lcr(int base_addr, uint8_t *p){ |
|
120 |
return util_sys_inb(base_addr+UART_LCR, p); |
|
121 |
} |
|
122 |
static int uart_set_lcr(int base_addr, uint8_t config){ |
|
123 |
if(sys_outb(base_addr+UART_LCR, config)) return WRITE_ERROR; |
|
124 |
return SUCCESS; |
|
125 |
} |
|
126 |
static int uart_get_lsr(int base_addr, uint8_t *p){ |
|
127 |
return util_sys_inb(base_addr+UART_LSR, p); |
|
128 |
} |
|
129 |
static int uart_get_iir(int base_addr, uint8_t *p){ |
|
130 |
return util_sys_inb(base_addr+UART_IIR, p); |
|
131 |
} |
|
132 |
|
|
133 |
static int uart_enable_divisor_latch(int base_addr){ |
|
134 |
int ret = SUCCESS; |
|
135 |
uint8_t conf; if((ret = uart_get_lcr(base_addr, &conf))) return ret; |
|
136 |
return uart_set_lcr(base_addr, conf | UART_DLAB); |
|
137 |
} |
|
138 |
static int uart_disable_divisor_latch(int base_addr){ |
|
139 |
int ret = SUCCESS; |
|
140 |
uint8_t conf; if((ret = uart_get_lcr(base_addr, &conf))) return ret; |
|
141 |
return uart_set_lcr(base_addr, conf & (~UART_DLAB)); |
|
142 |
} |
|
143 |
|
|
144 |
static int uart_get_ier(int base_addr, uint8_t *p){ |
|
145 |
int ret; |
|
146 |
if((ret = uart_disable_divisor_latch(base_addr))) return ret; |
|
147 |
return util_sys_inb(base_addr+UART_IER, p); |
|
148 |
} |
|
149 |
static int uart_set_ier(int base_addr, uint8_t n){ |
|
150 |
int ret; |
|
151 |
if((ret = uart_disable_divisor_latch(base_addr))) return ret; |
|
152 |
if(sys_outb(base_addr+UART_IER, n)) return WRITE_ERROR; |
|
153 |
return SUCCESS; |
|
154 |
} |
|
155 |
|
|
156 |
int uart_get_config(int base_addr, uart_config *config){ |
|
157 |
int ret = SUCCESS; |
|
158 |
|
|
159 |
config->base_addr = base_addr; |
|
160 |
|
|
161 |
if((ret = uart_get_lcr(base_addr, &config->lcr))) return ret; |
|
162 |
|
|
163 |
if((ret = uart_get_ier(base_addr, &config->ier))) return ret; |
|
164 |
|
|
165 |
if((ret = uart_enable_divisor_latch (base_addr))) return ret; |
|
166 |
if((ret = util_sys_inb(base_addr+UART_DLL, &config->dll ))) return ret; |
|
167 |
if((ret = util_sys_inb(base_addr+UART_DLM, &config->dlm ))) return ret; |
|
168 |
if((ret = uart_disable_divisor_latch(base_addr))) return ret; |
|
169 |
|
|
170 |
uart_parse_config(config); |
|
171 |
return ret; |
|
172 |
} |
|
173 |
void uart_print_config(uart_config config){ |
|
174 |
|
|
175 |
printf("%s configuration:\n", (config.base_addr == COM1_ADDR ? "COM1" : "COM2")); |
|
176 |
printf("\tLCR = 0x%X: %d bits per char\t %d stop bits\t", config.lcr, config.bits_per_char, config.stop_bits); |
|
177 |
if((config.parity&BIT(0)) == 0) printf("NO parity\n"); |
|
178 |
else switch(config.parity){ |
|
179 |
case uart_parity_odd : printf("ODD parity\n" ); break; |
|
180 |
case uart_parity_even: printf("EVEN parity\n" ); break; |
|
181 |
case uart_parity_par1: printf("parity bit is 1\n"); break; |
|
182 |
case uart_parity_par0: printf("parity bit is 0\n"); break; |
|
183 |
default : printf("invalid\n" ); break; |
|
184 |
} |
|
185 |
printf("\tDLM = 0x%02X DLL=0x%02X: bitrate = %d bps\n", config.dlm, config.dll, UART_BITRATE/config.divisor_latch); |
|
186 |
printf("\tIER = 0x%02X: Rx interrupts: %s\tTx interrupts: %s\n", config.ier, |
|
187 |
(config.received_data_int ? "ENABLED":"DISABLED"), |
|
188 |
(config.transmitter_empty_int ? "ENABLED":"DISABLED")); |
|
189 |
} |
|
190 |
|
|
191 |
int uart_set_bits_per_character(int base_addr, uint8_t bits_per_char){ |
|
192 |
if(bits_per_char < 5 || bits_per_char > 8) return INVALID_ARG; |
|
193 |
int ret = SUCCESS; |
|
194 |
bits_per_char = (bits_per_char-5)&0x3; |
|
195 |
uint8_t conf; if((ret = uart_get_lcr(base_addr, &conf))) return ret; |
|
196 |
conf = (conf & (~UART_BITS_PER_CHAR)) | bits_per_char; |
|
197 |
return uart_set_lcr(base_addr, conf); |
|
198 |
} |
|
199 |
int uart_set_stop_bits(int base_addr, uint8_t stop){ |
|
200 |
if(stop != 1 && stop != 2) return INVALID_ARG; |
|
201 |
int ret = SUCCESS; |
|
202 |
stop -= 1; |
|
203 |
stop = (stop&1)<<2; |
|
204 |
uint8_t conf; if((ret = uart_get_lcr(base_addr, &conf))) return ret; |
|
205 |
conf = (conf & (~UART_STOP_BITS)) | stop; |
|
206 |
return uart_set_lcr(base_addr, conf); |
|
207 |
} |
|
208 |
int uart_set_parity(int base_addr, uart_parity par){ |
|
209 |
int ret = SUCCESS; |
|
210 |
uint8_t parity = par << 3; |
|
211 |
uint8_t conf; if((ret = uart_get_lcr(base_addr, &conf))) return ret; |
|
212 |
conf = (conf & (~UART_PARITY)) | parity; |
|
213 |
return uart_set_lcr(base_addr, conf); |
|
214 |
} |
|
215 |
int uart_set_bit_rate(int base_addr, float bit_rate){ |
|
216 |
int ret = SUCCESS; |
|
217 |
uint16_t latch = UART_BITRATE/bit_rate; |
|
218 |
uint8_t dll = UART_GET_DLL(latch); |
|
219 |
uint8_t dlm = UART_GET_DLM(latch); |
|
220 |
if((ret = uart_enable_divisor_latch(base_addr))) return ret; |
|
221 |
if(sys_outb(base_addr+UART_DLL, dll)) return WRITE_ERROR; |
|
222 |
if(sys_outb(base_addr+UART_DLM, dlm)) return WRITE_ERROR; |
|
223 |
if((ret = uart_disable_divisor_latch(base_addr))) return ret; |
|
224 |
return SUCCESS; |
|
225 |
} |
|
226 |
|
|
227 |
static int uart_get_char(int base_addr, uint8_t *p){ |
|
228 |
int ret; |
|
229 |
if((ret = uart_disable_divisor_latch(base_addr))) return ret; |
|
230 |
return util_sys_inb(base_addr+UART_RBR, p); |
|
231 |
} |
|
232 |
static int uart_send_char(int base_addr, uint8_t c){ |
|
233 |
int ret; |
|
234 |
if((ret = uart_disable_divisor_latch(base_addr))) return ret; |
|
235 |
if(sys_outb(base_addr+UART_THR, c)) return WRITE_ERROR; |
|
236 |
return SUCCESS; |
|
237 |
} |
|
238 |
static int uart_receiver_ready(int base_addr){ |
|
239 |
uint8_t lsr; |
|
240 |
if(uart_get_lsr(base_addr, &lsr)) return false; |
|
241 |
return UART_GET_RECEIVER_READY(lsr); |
|
242 |
} |
|
243 |
static int uart_transmitter_empty(int base_addr){ |
|
244 |
uint8_t lsr; |
|
245 |
if(uart_get_lsr(base_addr, &lsr)) return false; |
|
246 |
return UART_GET_TRANSMITTER_EMPTY(lsr); |
|
247 |
} |
|
248 |
|
|
249 |
int uart_enable_int_rx(int base_addr){ |
|
250 |
int ret; |
|
251 |
uint8_t ier; |
|
252 |
if((ret = uart_get_ier(base_addr, &ier))) return ret; |
|
253 |
ier |= UART_INT_EN_RX; |
|
254 |
return uart_set_ier(base_addr, ier); |
|
255 |
} |
|
256 |
int uart_disable_int_rx(int base_addr){ |
|
257 |
int ret; |
|
258 |
uint8_t ier; |
|
259 |
if((ret = uart_get_ier(base_addr, &ier))) return ret; |
|
260 |
ier &= ~UART_INT_EN_RX; |
|
261 |
return uart_set_ier(base_addr, ier); |
|
262 |
} |
|
263 |
int uart_enable_int_tx(int base_addr){ |
|
264 |
int ret; |
|
265 |
uint8_t ier; |
|
266 |
if((ret = uart_get_ier(base_addr, &ier))) return ret; |
|
267 |
ier |= UART_INT_EN_TX; |
|
268 |
return uart_set_ier(base_addr, ier); |
|
269 |
} |
|
270 |
int uart_disable_int_tx(int base_addr){ |
|
271 |
int ret; |
|
272 |
uint8_t ier; |
|
273 |
if((ret = uart_get_ier(base_addr, &ier))) return ret; |
|
274 |
ier &= ~UART_INT_EN_TX; |
|
275 |
return uart_set_ier(base_addr, ier); |
|
276 |
} |
|
277 |
|
|
278 |
/// NCTP |
|
279 |
|
|
280 |
#define NCTP_START 0x80 |
|
281 |
#define NCTP_END 0xFF |
|
282 |
#define NCTP_OK 0xFF |
|
283 |
#define NCTP_NOK 0x00 |
|
284 |
|
|
285 |
queue_t *out = NULL; |
|
286 |
queue_t *in = NULL; |
|
287 |
|
|
288 |
int nctp_init(void){ |
|
289 |
out = queue_ctor(); if(out == NULL) return NULL_PTR; |
|
290 |
in = queue_ctor(); if(in == NULL) return NULL_PTR; |
|
291 |
return SUCCESS; |
|
292 |
} |
|
293 |
int nctp_free(void){ |
|
294 |
while(!queue_empty(out)){ |
|
295 |
free(queue_top(out)); |
|
296 |
queue_pop(out); |
|
297 |
} |
|
298 |
while(!queue_empty(in)){ |
|
299 |
free(queue_top(in)); |
|
300 |
queue_pop(in); |
|
301 |
} |
|
302 |
return SUCCESS; |
|
303 |
} |
|
304 |
|
|
305 |
int nctp_send(size_t num, uint8_t* ptr[], size_t sz[]){ |
|
306 |
{ |
|
307 |
size_t cnt = 0; |
|
308 |
for(size_t i = 0; i < num; ++i){ |
|
309 |
cnt += sz[i]; |
|
310 |
if(cnt > queue_max_size) return TRANS_REFUSED; |
|
311 |
} |
|
312 |
} |
|
313 |
int ret; |
|
314 |
uint8_t *tmp; |
|
315 |
tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_START; queue_push(out, tmp); |
|
316 |
for(size_t i = 0; i < num; ++i){ |
|
317 |
uint8_t *p = ptr[i]; size_t s = sz[i]; |
|
318 |
for(size_t j = 0; j < s; ++j, ++p){ |
|
319 |
tmp = malloc(sizeof(uint8_t)); *tmp = *p; queue_push(out, tmp); |
|
320 |
} |
|
321 |
} |
|
322 |
tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_END; queue_push(out, tmp); |
|
323 |
if(uart_transmitter_empty(COM1_ADDR)){ |
|
324 |
if((ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out)))) return ret; |
|
325 |
queue_pop(out); |
|
326 |
} |
|
327 |
return SUCCESS; |
|
328 |
} |
|
329 |
|
|
330 |
static int nctp_transmit(void){ |
|
331 |
if(!queue_empty(out)){ |
|
332 |
int ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out)); |
|
333 |
queue_pop(out); |
|
334 |
return ret; |
|
335 |
}else return SUCCESS; |
|
336 |
} |
|
337 |
|
|
338 |
static void process(){ |
|
339 |
free(queue_top(in)); queue_pop(in); |
|
340 |
while(*(uint8_t*)queue_top(in) != NCTP_END){ |
|
341 |
printf("%c", *(uint8_t*)queue_top(in)); |
|
342 |
free(queue_top(in)); queue_pop(in); |
|
343 |
} |
|
344 |
printf("\n"); |
|
345 |
free(queue_top(in)); queue_pop(in); |
|
346 |
} |
|
347 |
static int nctp_receive(void){ |
|
348 |
int ret; |
|
349 |
uint8_t c; |
|
350 |
int num_ends = 0; |
|
351 |
while(uart_receiver_ready(COM1_ADDR)){ |
|
352 |
if((ret = uart_get_char(COM1_ADDR, &c))) return ret; |
|
353 |
uint8_t *tmp = malloc(sizeof(uint8_t)); *tmp = c; |
|
354 |
queue_push(in, tmp); |
|
355 |
if(c == NCTP_END) ++num_ends; |
|
356 |
} |
|
357 |
while(num_ends-- > 0) process(); |
|
358 |
return SUCCESS; |
|
359 |
} |
|
360 |
|
|
361 |
int nctp_ih_err = SUCCESS; |
|
362 |
void nctp_ih(void){ |
|
363 |
uint8_t iir; |
|
364 |
if((nctp_ih_err = uart_get_iir(COM1_ADDR, &iir))) return; |
|
365 |
if(UART_GET_IF_INT_PEND(iir)){ |
|
366 |
switch(UART_GET_INT_PEND(iir)){ |
|
367 |
case uart_int_rx: nctp_receive (); break; |
|
368 |
case uart_int_tx: nctp_transmit(); break; |
|
369 |
default: break; |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
|
|
374 |
/// HLTP |
|
375 |
int hltp_send_string(const char *p){ |
|
376 |
uint8_t* ptr[1]; ptr[0] = (uint8_t*)p; |
|
377 |
size_t sz[1]; sz[0] = strlen(p)+1; |
|
378 |
return nctp_send(1, ptr, sz); |
|
379 |
} |
|
380 | 0 |
proj/libs/peripherals/src/kbc.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "kbc.h" |
|
4 |
|
|
5 |
#include "utils.h" |
|
6 |
#include "errors.h" |
|
7 |
|
|
8 |
int (kbc_read_cmd)(uint8_t *cmd){ |
|
9 |
int ret = 0; |
|
10 |
if((ret = kbc_issue_cmd(READ_KBC_CMD))) return ret; |
|
11 |
if((ret = kbc_read_byte(cmd))) return ret; |
|
12 |
return SUCCESS; |
|
13 |
} |
|
14 |
|
|
15 |
int (kbc_change_cmd)(uint8_t cmd){ |
|
16 |
int ret = 0; |
|
17 |
if((ret = kbc_issue_cmd(WRITE_KBC_CMD))) return ret; |
|
18 |
if((ret = kbc_issue_arg(cmd))) return ret; |
|
19 |
return SUCCESS; |
|
20 |
} |
|
21 |
|
|
22 |
int (kbc_restore_kbd)(){ |
|
23 |
int ret = 0; |
|
24 |
uint8_t cmd = 0; |
|
25 |
if((ret = kbc_read_cmd(&cmd))) return ret; |
|
26 |
cmd = (cmd | INT_KBD) & (~DIS_KBD); |
|
27 |
if((ret = kbc_change_cmd(cmd))) return ret; |
|
28 |
return SUCCESS; |
|
29 |
} |
|
30 |
|
|
31 |
int (kbc_issue_cmd)(uint8_t cmd){ |
|
32 |
int ret = 0; |
|
33 |
uint8_t stat; |
|
34 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
|
35 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
|
36 |
if((stat&IN_BUF_FULL) == 0){ |
|
37 |
if(sys_outb(KBC_CMD, cmd)) return WRITE_ERROR; |
|
38 |
return SUCCESS; |
|
39 |
} |
|
40 |
tickdelay(micros_to_ticks(DELAY)); |
|
41 |
} |
|
42 |
return TIMEOUT_ERROR; |
|
43 |
} |
|
44 |
|
|
45 |
int (kbc_issue_arg)(uint8_t arg){ |
|
46 |
int ret = 0; |
|
47 |
uint8_t stat; |
|
48 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
|
49 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
|
50 |
if((stat&IN_BUF_FULL) == 0){ |
|
51 |
if(sys_outb(KBC_CMD_ARG, arg)) return WRITE_ERROR; |
|
52 |
return SUCCESS; |
|
53 |
} |
|
54 |
tickdelay(micros_to_ticks(DELAY)); |
|
55 |
} |
|
56 |
return TIMEOUT_ERROR; |
|
57 |
} |
|
58 |
|
|
59 |
int (kbc_read_byte)(uint8_t *byte){ |
|
60 |
int ret = 0; |
|
61 |
uint8_t stat; |
|
62 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
|
63 |
if((ret = util_sys_inb(STATUS_REG, &stat))) return ret; |
|
64 |
if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){ |
|
65 |
if(stat & (PARITY_ERROR | TIME_OUT_REC)) return OTHER_ERROR; |
|
66 |
if((ret = util_sys_inb(OUTPUT_BUF, byte))) return ret; |
|
67 |
else return SUCCESS; |
|
68 |
} |
|
69 |
tickdelay(micros_to_ticks(DELAY)); |
|
70 |
} |
|
71 |
printf("Timing out\n"); |
|
72 |
return TIMEOUT_ERROR; |
|
73 |
} |
|
74 | 0 |
proj/libs/peripherals/src/keyboard.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "keyboard.h" |
|
4 |
|
|
5 |
#include "kbc.h" |
|
6 |
#include "utils.h" |
|
7 |
#include "errors.h" |
|
8 |
#include "proj_func.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 |
if (done) update_key_presses(); |
|
42 |
|
|
43 |
} |
|
44 |
|
|
45 |
int (keyboard_poll)(uint8_t bytes[], uint8_t *size){ |
|
46 |
int ret = 0; |
|
47 |
if(bytes == NULL || size == NULL) return NULL_PTR; |
|
48 |
uint8_t c; |
|
49 |
if((ret = kbc_read_byte(&c))) return ret; |
|
50 |
if(c == TWO_BYTE_CODE){ |
|
51 |
if((ret = kbc_read_byte(&bytes[1]))) return ret; |
|
52 |
bytes[0] = c; |
|
53 |
*size = 2; |
|
54 |
}else{ |
|
55 |
bytes[1] = 0; |
|
56 |
bytes[0] = c; |
|
57 |
*size = 1; |
|
58 |
} |
|
59 |
return SUCCESS; |
|
60 |
} |
|
61 | 0 |
proj/libs/peripherals/src/timer.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "timer.h" |
|
4 |
#include "graph.h" |
|
5 |
#include "sprite.h" |
|
6 |
|
|
7 |
#define TIMER_FREQ 1193182 /**< @brief clock frequency for timer in PC and AT */ |
|
8 |
#define TIMER_MIN_FREQ (TIMER_FREQ/UINT16_MAX) + ((TIMER_FREQ % UINT16_MAX) ? 1 : 0) /**< @brief mininum frequency for timer */ |
|
9 |
|
|
10 |
/* I/O port addresses */ |
|
11 |
|
|
12 |
#define TIMER_0 0x40 /**< @brief Timer 0 count register */ |
|
13 |
#define TIMER_1 0x41 /**< @brief Timer 1 count register */ |
|
14 |
#define TIMER_2 0x42 /**< @brief Timer 2 count register */ |
|
15 |
#define TIMER_CTRL 0x43 /**< @brief Control register */ |
|
16 |
|
|
17 |
#define SPEAKER_CTRL 0x61 /**< @brief Register for speaker control */ |
|
18 |
|
|
19 |
/* Timer control */ |
|
20 |
|
|
21 |
/* Timer selection: bits 7 and 6 */ |
|
22 |
|
|
23 |
#define TIMER_SEL0 0x00 /**< @brief Control Word for Timer 0 */ |
|
24 |
#define TIMER_SEL1 BIT(6) /**< @brief Control Word for Timer 1 */ |
|
25 |
#define TIMER_SEL2 BIT(7) /**< @brief Control Word for Timer 2 */ |
|
26 |
#define TIMER_RB_CMD (BIT(7) | BIT(6)) /**< @brief Read Back Command */ |
|
27 |
|
|
28 |
/* Register selection: bits 5 and 4 */ |
|
29 |
|
|
30 |
#define TIMER_LSB BIT(4) /**< @brief Initialize Counter LSB only */ |
|
31 |
#define TIMER_MSB BIT(5) /**< @brief Initialize Counter MSB only */ |
|
32 |
#define TIMER_LSB_MSB (TIMER_LSB | TIMER_MSB) /**< @brief Initialize LSB first and MSB afterwards */ |
|
33 |
#define TIMER_INMODE_MASK 0x30 /**< @brief Mask for Timer Counter Mode */ |
|
34 |
#define TIMER_INMODE_POS 4 /**< @brief Bit position of Timer Counter Mode */ |
|
35 |
|
|
36 |
/* Operating mode: bits 3, 2 and 1 */ |
|
37 |
|
|
38 |
#define TIMER_SQR_WAVE (BIT(2) | BIT(1)) /**< @brief Mode 3: square wave generator */ |
|
39 |
#define TIMER_RATE_GEN BIT(2) /**< @brief Mode 2: rate generator */ |
|
40 |
#define TIMER_MODE_MASK 0x0E /**< @brief Mask for mode */ |
|
41 |
#define TIMER_MODE_POS 1 /**< @brief Position of smallest bit from mode */ |
|
42 |
#define TIMER_MODE_2ALT 0x6 /**< @brief Alternative notation for mode 2 */ |
|
43 |
#define TIMER_MODE_3ALT 0x7 /**< @brief Alternative notation for mode 3 */ |
|
44 |
#define TIMER_MODE_RED2 0x03 /**< @brief Reduce 3-bit mode to 2-bit mode */ |
|
45 |
|
|
46 |
/* Counting mode: bit 0 */ |
|
47 |
|
|
48 |
#define TIMER_BCD 0x01 /**< @brief Count in BCD */ |
|
49 |
#define TIMER_BIN 0x00 /**< @brief Count in binary */ |
|
50 |
|
|
51 |
/* READ-BACK COMMAND FORMAT */ |
|
52 |
|
|
53 |
#define TIMER_RB_COUNT_ BIT(5) /**< @brief Read counter value on read-back (0 to activate) */ |
|
54 |
#define TIMER_RB_STATUS_ BIT(4) /**< @brief Read status value on read-back (0 to activate) */ |
|
55 |
#define TIMER_RB_SEL(n) BIT((n) + 1) /**< @brief Select timer to read information from */ |
|
56 |
|
|
57 |
int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
|
58 |
if (interrupt_id == NULL) return 1; |
|
59 |
*interrupt_id = interrupt_bit; |
|
60 |
return (sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, interrupt_id)); |
|
61 |
} |
|
62 |
|
|
63 |
uint32_t no_interrupts = 0; |
|
64 |
void (timer_int_handler)() { |
|
65 |
no_interrupts++; |
|
66 |
} |
|
67 | 0 |
proj/libs/peripherals/src/graph.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "graph.h" |
|
4 |
|
|
5 |
#include "errors.h" |
|
6 |
#include <stdio.h> |
|
7 |
|
|
8 |
#define VC_BIOS_SERV 0x10 /** @brief Interrupt service of video card */ |
|
9 |
#define VBE_CALL 0x4F /** @brief VBE call function specifier */ |
|
10 |
|
|
11 |
#define MBYTE_BASE 0x0 /** @brief Base address (zero address) */ |
|
12 |
#define MBYTE_SIZE 0xFFFFF /** @brief Size of a mebibyte */ |
|
13 |
|
|
14 |
// Graphics Functions |
|
15 |
#define VBE_CTRL_INFO 0x00 /** @brief Get VBE Controller Information */ |
|
16 |
#define VBE_MD_INFO 0x01 /** @brief Get VBE Mode Information */ |
|
17 |
#define SET_VBE_MD 0x02 /** @brief Set VBE Mode */ |
|
18 |
|
|
19 |
// Error codes (AH) |
|
20 |
#define AH_SUCCESS 0x00 /** @brief Success code on BIOS call */ |
|
21 |
#define AH_FUNC_CALL_FAIL 0x01 /** @brief Function call failed */ |
|
22 |
#define AH_FUNC_NOT_SUPP 0x02 /** @brief Function call is not supported in current HW configuration */ |
|
23 |
#define AH_FUNC_INVALID 0x03 /** @brief Invalid function in current video mode */ |
|
24 |
|
|
25 |
/// MACROS |
|
26 |
#define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
|
27 |
|
|
28 |
/// STRUCT |
|
29 |
typedef struct __attribute__((packed)) { |
|
30 |
|
|
31 |
char VbeSignature[4] ; |
|
32 |
uint16_t VbeVersion ; |
|
33 |
uint32_t OemStringPtr ; |
|
34 |
uint8_t Capabilities[4] ; |
|
35 |
uint32_t VideoModePtr ; |
|
36 |
uint16_t TotalMemory ; |
|
37 |
|
|
38 |
uint16_t OemSoftwareRev ; |
|
39 |
uint32_t OemVendorNamePtr ; |
|
40 |
uint32_t OemProductNamePtr ; |
|
41 |
uint32_t OemProductRevPtr ; |
|
42 |
char Reserved[222] ; |
|
43 |
|
|
44 |
char OemData[256] ; |
|
45 |
} VbeInfoBlock; |
|
46 |
|
|
47 |
static vbe_mode_info_t vbe_mem_info; |
|
48 |
|
|
49 |
/// PRIVATE GET |
|
50 |
static uint16_t (graph_get_bits_pixel) (void){ return vbe_mem_info.BitsPerPixel; } |
|
51 |
static phys_bytes (graph_get_phys_addr) (void){ return vbe_mem_info.PhysBasePtr; } |
|
52 |
static unsigned (graph_get_vram_size) (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); } |
|
53 |
//static uint16_t (graph_get_RedMaskSize) (void){ return vbe_mem_info.RedMaskSize ; } |
|
54 |
//static uint16_t (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; } |
|
55 |
//static uint16_t (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; } |
|
56 |
|
|
57 |
/// PUBLIC GET |
|
58 |
uint16_t (graph_get_XRes) (void){ return vbe_mem_info.XResolution; } |
|
59 |
uint16_t (graph_get_YRes) (void){ return vbe_mem_info.YResolution; } |
|
60 |
uint16_t (graph_get_bytes_pixel) (void){ return (graph_get_bits_pixel() + 7) >> 3; } |
|
61 |
|
|
62 |
/// |
|
63 |
static int (get_permission)(unsigned int base_addr, unsigned int size) { |
|
64 |
struct minix_mem_range mmr; |
|
65 |
mmr.mr_base = base_addr; |
|
66 |
mmr.mr_limit = base_addr + size; |
|
67 |
return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr); |
|
68 |
} |
|
69 |
|
|
70 |
//static int (get_permissions_first_mbyte)(void) { |
|
71 |
// return get_permission(MBYTE_BASE, MBYTE_SIZE); |
|
72 |
//} |
|
73 |
|
|
74 |
/// MEMORY |
|
75 |
static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */ |
|
76 |
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */ |
|
77 |
static mmap_t mem_map; |
|
78 |
static int (graph_free_memory)(void) { |
|
79 |
int r = SUCCESS; |
|
80 |
free(video_buf); video_buf = NULL; |
|
81 |
r = !lm_free(&mem_map); |
|
82 |
return r; |
|
83 |
} |
|
84 |
static int (graph_map_vram)(void) { |
|
85 |
int r; |
|
86 |
const unsigned vram_base = graph_get_phys_addr(); |
|
87 |
const unsigned vram_size = graph_get_vram_size(); |
|
88 |
if ((r = get_permission(vram_base, vram_size))) { |
|
89 |
if (graph_free_memory()) { |
|
90 |
printf("%s: lm_free failed\n", __func__); |
|
91 |
} |
|
92 |
panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r); |
|
93 |
} |
|
94 |
|
|
95 |
video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size); |
|
96 |
|
|
97 |
if (video_mem == MAP_FAILED) { |
|
98 |
if (graph_free_memory()) { |
|
99 |
printf("%s: lm_free failed\n", __func__); |
|
100 |
} |
|
101 |
panic("%s: couldn't map video memory.", __func__); |
|
102 |
} |
|
103 |
|
|
104 |
video_buf = malloc(vram_size); |
|
105 |
|
|
106 |
return SUCCESS; |
|
107 |
} |
|
108 |
|
|
109 |
/// INFO GET |
|
110 |
static int (vbe_get_mode_information)(uint16_t mode) { |
|
111 |
memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values |
|
112 |
|
|
113 |
struct reg86 reg_86; |
|
114 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct |
|
115 |
|
|
116 |
vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map); |
|
117 |
|
|
118 |
reg_86.intno = VC_BIOS_SERV; |
|
119 |
reg_86.ah = VBE_CALL; |
|
120 |
reg_86.al = VBE_MD_INFO; |
|
121 |
reg_86.cx = mode; |
|
122 |
reg_86.es = PB2BASE(mem_map.phys); |
|
123 |
reg_86.di = PB2OFF(mem_map.phys); |
|
124 |
// BIOS CALL |
|
125 |
if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) { |
|
126 |
printf("%s: sys_int86 failed\n", __func__); |
|
127 |
if (graph_free_memory()) { |
|
128 |
printf("%s: lm_free failed\n", __func__); |
|
129 |
} |
|
130 |
return BIOS_CALL_ERROR; |
|
131 |
} |
|
132 |
|
|
133 |
memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size); |
|
134 |
return SUCCESS; |
|
135 |
} |
|
136 |
/* |
|
137 |
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) { |
|
138 |
memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values |
|
139 |
|
|
140 |
mmap_t controller_map; |
|
141 |
|
|
142 |
struct reg86 reg_86; |
|
143 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct |
|
144 |
|
|
145 |
VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map); |
|
146 |
|
|
147 |
uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys; |
|
148 |
|
|
149 |
virtual_addr->VbeSignature[0] = 'V'; |
|
150 |
virtual_addr->VbeSignature[1] = 'B'; |
|
151 |
virtual_addr->VbeSignature[2] = 'E'; |
|
152 |
virtual_addr->VbeSignature[3] = '2'; |
|
153 |
|
|
154 |
|
|
155 |
reg_86.intno = VC_BIOS_SERV; |
|
156 |
reg_86.ah = VBE_CALL; |
|
157 |
reg_86.al = VBE_CTRL_INFO; |
|
158 |
reg_86.es = PB2BASE(controller_map.phys); |
|
159 |
reg_86.di = PB2OFF(controller_map.phys); |
|
160 |
// BIOS CALL |
|
161 |
if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) { |
|
162 |
printf("%s: sys_int86 failed\n", __func__); |
|
163 |
if (!lm_free(&controller_map)) { |
|
164 |
printf("%s: lm_free failed\n", __func__); |
|
165 |
} |
|
166 |
return BIOS_CALL_ERROR; |
|
167 |
} |
|
168 |
|
|
169 |
info_p->VBESignature[0] = virtual_addr->VbeSignature[0]; |
|
170 |
info_p->VBESignature[1] = virtual_addr->VbeSignature[1]; |
|
171 |
info_p->VBESignature[2] = virtual_addr->VbeSignature[2]; |
|
172 |
info_p->VBESignature[3] = virtual_addr->VbeSignature[3]; |
|
173 |
|
|
174 |
uint8_t lsb, msb; |
|
175 |
util_get_LSB(virtual_addr->VbeVersion, &lsb); |
|
176 |
util_get_MSB(virtual_addr->VbeVersion, &msb); |
|
177 |
info_p->VBEVersion[0] = lsb; |
|
178 |
info_p->VBEVersion[1] = msb; |
|
179 |
|
|
180 |
info_p->TotalMemory = (virtual_addr->TotalMemory << 6); |
|
181 |
|
|
182 |
// Convert Far Far Pointer to Virtual Address |
|
183 |
|
|
184 |
uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr); |
|
185 |
uint32_t virtual_ptr = phys_ptr + virtual_base; |
|
186 |
info_p->OEMString = (char*)(virtual_ptr); |
|
187 |
|
|
188 |
phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr); |
|
189 |
virtual_ptr = phys_ptr + virtual_base; |
|
190 |
info_p->VideoModeList = (uint16_t*)(virtual_ptr); |
|
191 |
|
|
192 |
phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr); |
|
193 |
virtual_ptr = phys_ptr + virtual_base; |
|
194 |
info_p->OEMVendorNamePtr = (char*)(virtual_ptr); |
|
195 |
|
|
196 |
phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr); |
|
197 |
virtual_ptr = phys_ptr + virtual_base; |
|
198 |
info_p->OEMProductNamePtr = (char*)(virtual_ptr); |
|
199 |
|
|
200 |
phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr); |
|
201 |
virtual_ptr = phys_ptr + virtual_base; |
|
202 |
info_p->OEMProductRevPtr = (char*)(virtual_ptr); |
|
203 |
|
|
204 |
if (!lm_free(&controller_map)) { |
|
205 |
printf("%s: lm_free failed\n", __func__); |
|
206 |
return LCF_ERROR; |
|
207 |
} |
|
208 |
|
|
209 |
return SUCCESS; |
|
210 |
} |
|
211 |
*/ |
|
212 |
|
|
213 |
/// INIT |
|
214 |
/** |
|
215 |
* @brief |
|
216 |
* @param mode |
|
217 |
* @return |
|
218 |
*/ |
|
219 |
static int (graph_set_mode)(uint16_t mode) { |
|
220 |
struct reg86 reg_86; |
|
221 |
|
|
222 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct |
|
223 |
|
|
224 |
// Set Reg86 |
|
225 |
reg_86.intno = VC_BIOS_SERV; |
|
226 |
reg_86.ah = VBE_CALL; |
|
227 |
reg_86.al = SET_VBE_MD; |
|
228 |
reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD; |
|
229 |
|
|
230 |
// BIOS CALL |
|
231 |
if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) { |
|
232 |
printf("%s: sys_int86 failed\n", __func__); |
|
233 |
return BIOS_CALL_ERROR; |
|
234 |
} |
|
235 |
|
|
236 |
return SUCCESS; |
|
237 |
} |
|
238 |
int (graph_init)(uint16_t mode){ |
|
239 |
if (vbe_get_mode_information(mode)) { |
|
240 |
printf("%s: failed to get information for mode %x.\n", __func__, mode); |
|
241 |
return 1; |
|
242 |
} |
|
243 |
|
|
244 |
graph_map_vram(); // if function fails it aborts program |
|
245 |
|
|
246 |
if (graph_set_mode(mode)) { |
|
247 |
printf("%s: failed to set graphic mode %x.\n", __func__, mode); |
|
248 |
return 1; |
|
249 |
}; |
|
250 |
return SUCCESS; |
|
251 |
} |
|
252 |
|
|
253 |
/// CLEANUP |
|
254 |
int (graph_cleanup)(void){ |
|
255 |
int r = SUCCESS; |
|
256 |
if ((r = vg_exit())) |
|
257 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__); |
|
258 |
if ((r = graph_free_memory())) |
|
259 |
printf("%s: lm_free failed\n", __func__); |
|
260 |
return r; |
|
261 |
} |
|
262 |
|
|
263 |
/// PIXEL DRAWING |
|
264 |
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) { |
|
265 |
//pixels are certain to be inside can reduce lag |
|
266 |
/*if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) { |
|
267 |
//printf("%s: invalid pixel.\n", __func__); |
|
268 |
return OUT_OF_RANGE; |
|
269 |
}*/ |
|
270 |
unsigned int pos = (x + y * vbe_mem_info.XResolution) * 3/*graph_get_bytes_pixel()*/; |
|
271 |
memcpy(video_buf + pos, &color, 3/*graph_get_bytes_pixel()*/); |
|
272 |
return SUCCESS; |
|
273 |
} |
|
274 |
void (graph_set_pixel_pos)(unsigned pos, uint32_t color){ |
|
275 |
memcpy(video_buf + pos, &color, graph_get_bytes_pixel()); |
|
276 |
} |
|
277 |
int (graph_clear_screen)(void){ memset(video_buf, 0, graph_get_vram_size()); return SUCCESS; } |
|
278 |
int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; } |
|
279 |
|
|
280 |
///SPRITE |
|
281 |
#include "sprite.h" |
|
282 |
|
|
283 |
#include "utils.h" |
|
284 |
#include "fast_math.h" |
|
285 |
#include <math.h> |
|
286 |
|
|
287 |
struct basic_sprite{ |
|
288 |
uint8_t *map; |
|
289 |
uint16_t w, h; |
|
290 |
int16_t u0, v0; |
|
291 |
}; |
|
292 |
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){ |
|
293 |
basic_sprite_t *ret = malloc(sizeof(basic_sprite_t)); |
|
294 |
if(ret == NULL) return NULL; |
|
295 |
enum xpm_image_type type = XPM_8_8_8_8; |
|
296 |
xpm_image_t img; |
|
297 |
ret->map = xpm_load((xpm_map_t)xpm, type, &img); |
|
298 |
if(ret->map == NULL){ |
|
299 |
basic_sprite_dtor(ret); |
|
300 |
return NULL; |
|
301 |
} |
|
302 |
ret->w = img.width; |
|
303 |
ret->h = img.height; |
|
304 |
ret->u0 = u0; |
|
305 |
ret->v0 = v0; |
|
306 |
return ret; |
|
307 |
} |
|
308 |
void (basic_sprite_dtor)(basic_sprite_t *p){ |
|
309 |
if(p == NULL) return; |
|
310 |
free(p->map); |
|
311 |
free(p); |
|
312 |
} |
|
313 |
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; } |
|
314 |
uint16_t (basic_sprite_get_w) (const basic_sprite_t *p){ return p->w ; } |
|
315 |
uint16_t (basic_sprite_get_h) (const basic_sprite_t *p){ return p->h ; } |
|
316 |
int16_t (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; } |
|
317 |
int16_t (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; } |
|
318 |
|
|
319 |
/* |
|
320 |
struct basic_sprite_alpha{ |
|
321 |
uint8_t *map; |
|
322 |
uint16_t w, h; |
|
323 |
int16_t u0, v0; |
|
324 |
}; |
|
325 |
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){ |
|
326 |
basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t)); |
|
327 |
if(ret == NULL) return NULL; |
|
328 |
enum xpm_image_type type = XPM_8_8_8_8; |
|
329 |
xpm_image_t img; |
|
330 |
ret->map = NULL; |
|
331 |
uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img); |
|
332 |
if(m == NULL){ |
|
333 |
basic_sprite_alpha_dtor(ret); |
|
334 |
return NULL; |
|
335 |
} |
|
336 |
ret->map = m; |
|
337 |
if(ret->map == NULL){ |
|
338 |
basic_sprite_alpha_dtor(ret); |
|
339 |
return NULL; |
|
340 |
} |
|
341 |
ret->w = img.width; |
|
342 |
ret->h = img.height; |
|
343 |
ret->u0 = u0; |
|
344 |
ret->v0 = v0; |
|
345 |
return ret; |
|
346 |
} |
|
347 |
void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){ |
|
348 |
if(p == NULL) return; |
|
349 |
free(p->map); |
|
350 |
free(p); |
|
351 |
} |
|
352 |
const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; } |
|
353 |
uint16_t (basic_sprite_alpha_get_w) (const basic_sprite_alpha_t *p){ return p->w ; } |
|
354 |
uint16_t (basic_sprite_alpha_get_h) (const basic_sprite_alpha_t *p){ return p->h ; } |
|
355 |
int16_t (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; } |
|
356 |
int16_t (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; } |
|
357 |
*/ |
|
358 |
|
|
359 |
struct sprite{ |
|
360 |
const basic_sprite_t *bsp; |
|
361 |
int16_t x, y; //position in screen |
|
362 |
double theta, s, c; |
|
363 |
double scale; |
|
364 |
}; |
|
365 |
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){ |
|
366 |
sprite_t *ret = malloc(sizeof(sprite_t)); |
|
367 |
if(ret == NULL) return NULL; |
|
368 |
ret->bsp = bsp; |
|
369 |
ret->x = 0; |
|
370 |
ret->y = 0; |
|
371 |
sprite_set_angle(ret, 0.0); |
|
372 |
ret->scale = 1.0; |
|
373 |
return ret; |
|
374 |
} |
|
375 |
void (sprite_dtor)(sprite_t *p){ |
|
376 |
if(p == NULL) return; |
|
377 |
free(p); |
|
378 |
} |
|
379 |
void (sprite_set_pos) (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; } |
|
380 |
void (sprite_set_angle) (sprite_t *p, double angle ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); } |
|
381 |
void (sprite_set_scale) (sprite_t *p, double scale ){ p->scale = scale; } |
|
382 |
int16_t (sprite_get_x)(const sprite_t *p){ return p->x; } |
|
383 |
int16_t (sprite_get_y)(const sprite_t *p){ return p->y; } |
|
384 |
double (sprite_get_angle)(const sprite_t *p){ return p->theta; } |
|
385 |
uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); } |
Also available in: Unified diff