int timer_set_square(unsigned long timer, unsigned long freq) { char buf[12]; sprintf(buf, "TIMER_SEL", timer); unsigned long div = (TIMER_FREQ / freq); unsigned long LSB = div; unsigned long MSB = (div >> 8); unsigned char conf, port; if (freq < 19){ printf("Error: Frequency must be greater than 18 Hz.\n"); printf("OVERFLOW -> (Clock signal frequency)/freq must have a maximum of 16 bits.\n"); return EXIT_FAILURE; } else if (TIMER_FREQ < freq){ printf("Error: Frequency must be less than 1193182 Hz (clock signal frequency).\n"); return EXIT_FAILURE; } int i = timer_get_conf(timer, &conf); if (i != 0) return EXIT_FAILURE; port = (TIMER_0 + timer); unsigned char counting_mode; if (conf == 0) counting_mode = TIMER_BIN; else if (conf == 1) counting_mode = TIMER_BCD; if (sys_outb(TIMER_CTRL, (int) buf | TIMER_LSB_MSB | TIMER_SQR_WAVE | counting_mode ) != OK || sys_outb(port, LSB) != OK || sys_outb(port, MSB) != OK) return EXIT_FAILURE; else return EXIT_SUCCESS; }
int timerSetSquare(unsigned long timer, unsigned long freq) { int port; unsigned short freqToSend; unsigned char LSB, MSB; unsigned long byte = 0; freqToSend = TIMER_FREQ / freq; LSB = freqToSend & 0xFF; MSB = freqToSend >> 8; if (timer > 2) return -1; switch (timer) { case 0: port = TIMER_0; byte = TIMER_SEL0; break; case 1: port = TIMER_1; byte = TIMER_SEL1; break; case 2: port = TIMER_2; byte = TIMER_SEL2; break; } sys_outb(TIMER_CTRL, TIMER_LSB_MSB | TIMER_SQR_WAVE | TIMER_BIN); sys_outb(port, LSB); sys_outb(port, MSB); return 0; }
int initialize_fifos(void) { receive_fifo = new_char_queue_t(receive_fifo); transmit_fifo = new_char_queue_t(transmit_fifo); unsigned long fcr = 0; if(sys_inb(COM1+FIFO_CTRL_REG, &fcr) != OK) { printf("Error reading FIFO_CTRL_REG\n"); return 1; } fcr |= FIFO_INIT; if(sys_outb(COM1+FIFO_CTRL_REG, fcr) != OK) { printf("Error writing to FIFO_CTRL_REG\n"); return 1; } if(sys_inb(COM2+FIFO_CTRL_REG, &fcr) != OK) { printf("Error reading FIFO_CTRL_REG\n"); return 1; } fcr |= FIFO_INIT; if(sys_outb(COM2+FIFO_CTRL_REG, fcr) != OK) { printf("Error writing to FIFO_CTRL_REG\n"); return 1; } return 0; }
int get_divisor_latch(unsigned long com_address,unsigned long* dl) { unsigned long lsb = 0; unsigned long msb = 0; unsigned long lcr = 0; if(sys_inb(com_address + LINE_CTRL_REG, &lcr) != OK) { printf("Error reading LINE_CTRL_REG\n"); return 1; } unsigned long newlcr = lcr | DLAB; if(sys_outb(com_address + LINE_CTRL_REG, newlcr) != OK) { printf("Error writing LINE_CTRL_REG\n"); return 1; } if(sys_inb(com_address + DIVISOR_LATCH_LSB, &lsb) != OK) { printf("Error reading DIVISOR_LATCH_LSB\n"); return 1; } if(sys_inb(com_address + DIVISOR_LATCH_MSB, &msb) != OK) { printf("Error reading DIVISOR_LATCH_MSB\n"); return 1; } if(sys_outb(com_address + LINE_CTRL_REG, lcr) != OK) { printf("Error writing LINE_CTRL_REG\n"); return 1; } *dl = (msb << 8) + lsb; return 0; }
int timer_set_square(unsigned long timer, unsigned long freq) { if(freq<1||freq>TIMER_FREQ){ printf("ERROR FREQ"); return 1; } unsigned long resultado_freq,control_word; resultado_freq = TIMER_FREQ / freq; control_word = TIMER_LSB_MSB|TIMER_SQR_WAVE|TIMER_BIN; unsigned short timer_end=TIMER_0+timer; unsigned char freq_lsb = resultado_freq; unsigned char freq_msb = resultado_freq >> 8; switch(timer){ case 0: control_word |= TIMER_SEL0; break; case 1: control_word |= TIMER_SEL1; break; case 2: control_word |= TIMER_SEL2; break; default:break; } if(sys_outb(TIMER_CTRL,control_word)!=OK) printf("Erro Control word"); if(sys_outb(timer_end, freq_lsb)!=OK) printf("Erro timer LSB"); if(sys_outb(timer_end, freq_msb)!=OK) printf("Erro timer MSB"); return 0; }
int set_divisor_latch(unsigned long com_address,unsigned long dl) { unsigned long lsb = dl & 0xFF; unsigned long msb = dl >> 8; unsigned long lcr = 0; if(sys_inb(com_address + LINE_CTRL_REG, &lcr) != OK) { printf("Error reading LINE_CTRL_REG\n"); return 1; } unsigned long newlcr = lcr | DLAB; if(sys_outb(com_address + LINE_CTRL_REG, newlcr) != OK) { printf("Error writing LINE_CTRL_REG\n"); return 1; } if(sys_outb(com_address + DIVISOR_LATCH_LSB, lsb) != OK) { printf("Error writing DIVISOR_LATCH_LSB\n"); return 1; } if(sys_outb(com_address + DIVISOR_LATCH_MSB, msb) != OK) { printf("Error writing DIVISOR_LATCH_MSB\n"); return 1; } if(sys_outb(com_address + LINE_CTRL_REG, lcr) != OK) { printf("Error writing LINE_CTRL_REG\n"); return 1; } return 0; }
int timer_set_square(unsigned long timer, unsigned long freq) { if(timer==0){ sys_outb(TIMER_CTRL, 0x37); sys_outb(TIMER_0 , freq & 0xff); sys_outb(TIMER_0 , freq>>8); }
int timer_set_square(unsigned long timer, unsigned long freq) { unsigned long tempByte; unsigned char st; if (timer == 0) { timer_get_conf(timer, &st); st = 0x0F & st; //st fica com os ultimos 4 bits tempByte = (TIMER_SEL0 | TIMER_LSB_MSB | st); // seleciona o TIMER_0 e ativa LSB followed by MSB sys_outb(TIMER_CTRL, tempByte); sys_outb(TIMER_0, TIMER_FREQ/freq); //manda LSB sys_outb(TIMER_0, (TIMER_FREQ/freq >> 8)); //manda MSB return 0; }
int test_packet(unsigned short cnt) { int conta = 0, ind = 0, r, ipc_status; unsigned long irq_set, data; char cmd; message msg; if ((irq_set = mouse_subscribe()) == -1) { printf("Unable to subscribe mouse!\n"); return 1; } if (sys_outb(STAT_REG, ENABLE_MOUSE) != OK) //rato enable printf("Error\n"); if (sys_outb(STAT_REG, W_TO_MOUSE) != OK) //MOUSE printf("Error-MC\n"); if (sys_outb(OUT_BUF, ENABLE_SEND) != OK) //Ativar o envio printf("Error-SEND\n"); while (cnt > conta) { if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: if (msg.NOTIFY_ARG & irq_set) { get_packet(); mouse_print(); } break; default: break; } } else { } } printf("Terminou!\n"); if (sys_outb(STAT_REG, W_TO_MOUSE) != OK) printf("ERROR-MC"); if (sys_outb(OUT_BUF, DISABLE_STREAM) != OK) printf("ERROR-DISABLE_STREAM"); if (sys_inb(OUT_BUF, &data) != OK) printf("OUT_BUF not full!\n"); if (data != ACK) printf("Not ACK!\n"); if (mouse_unsubscribe() == -1) printf("falhou unsubscribe mouse!\n"); return 0; }
int mouse_write(unsigned char cmd) { unsigned long stat; int i = 0; for (i = 0; i < KBC_IO_MAX_TRIES; i++) { sys_outb(CMD_REG, WRITE_BYTE); tickdelay(micros_to_ticks(DELAY_US)); sys_outb(IN_BUF, cmd); /* no args command */ tickdelay(micros_to_ticks(DELAY_US)); sys_inb(OUT_BUF, &stat); /* assuming it returns OK */ if (stat == ACK) { return 0; } } }
/* ** Name: void outb(unsigned short int port, unsigned long value); ** Function: Writes a byte to specified i/o port. */ PUBLIC void outb(unsigned short port, unsigned long value) { int rc; if ((rc = sys_outb(port, value)) != OK) warning("outb", rc); return; }
//write a value to kbc without resending int write_to_kbc_no_resend(port_t port,unsigned long value, unsigned char read_response) { int attempts = 0; unsigned long stat; unsigned long response=0; while(1) { if(attempts >= MAX_ATTEMPTS){ printf("write_to_kbc_no_resend() failed, max attempts reached: %d\n", attempts); return -1; } if(sys_inb(STAT_REG, &stat)!= OK){ printf("write_to_kbc_no_resend() failed, failure reading status\n"); return -1; } if( (stat & IN_BUF_STATUS) == 0 ) { if(sys_outb(port, value) != OK){ printf("write_to_kbc_no_resend() failed, failure writing value 0x%x, at port 0x%x\n", value, port); return -1; } if(read_response) read_kbd_value(&response); break; } attempts++; WAIT_MS(WAIT_TIME); } return response; }
int writeToKBC(unsigned long reg, unsigned long cmd) { unsigned long stat, r, data, i = 0; while(i < 3) // retry 3 times on time-out { r = sys_inb(STAT_REG, &stat); if (r != OK) { printf("sys_inb failed with: %d", r); return -1; } if ((stat & IBF) == 0) { r = sys_outb(reg, cmd); if (r != OK) { printf("sys_inb failed with: %d", r); return -1; } else return 0; } // gives the KBC or the keyboard enough-time to respond tickdelay(micros_to_ticks(DELAY_US)); i++; } printf("writeToKBC failed: unable to write data\n"); return -1; }
int write_kbc(unsigned long port, unsigned char byte) { LOG unsigned long stat, counter = 0; while (counter < TIMEOUT_COUNTER) { if (sys_inb(STAT_REG, &stat) != 0) { printf("write_kbc: sys_inb failed.\n"); return -1; } if(!(stat & IBF)) { if (sys_outb(port, byte) != 0) { printf("write_kbc: sys_outb failed.\n"); return -1; } return 0; } if (tickdelay(micros_to_ticks(DELAY_US)) != 0) { printf("write_kbc: tickdelay failed.\n"); return -1; } counter++; } printf("write_kbc: time out.\n"); return -1; }
static void my_outb(u16_t port, u8_t value) { int s; if ((s = sys_outb(port, value)) != OK) printf("RTL8169: warning, sys_outb failed: %d\n", s); }
int test_date() { int ipc_status; message msg; unsigned int i=0; unsigned rtc_hook_id; rtc_subscribe_int(&rtc_hook_id); while(1) { if (driver_receive(ANY, &msg, &ipc_status) != 0) { if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & (1 << RTC_HOOK)) { /* subscribed interrupt */ i++; } break; default: break; } } } } rtc_unsubscribe_int(rtc_hook_id); sys_outb(RTC_ADDR_REG, RTC_CTRL_REG_B); /// }
void load_config(unsigned long registers[]){ disable(); sys_outb(RTC_ADDR_REG, 10); sys_inb(RTC_DATA_REG,®isters[0]); sys_outb(RTC_ADDR_REG, 11); sys_inb(RTC_DATA_REG,®isters[1]); sys_outb(RTC_ADDR_REG, 12); sys_inb(RTC_DATA_REG,®isters[2]); sys_outb(RTC_ADDR_REG, 13); sys_inb(RTC_DATA_REG,®isters[3]); enable(); }
/*===========================================================================* * do_initialize * *===========================================================================*/ static void do_initialize() { /* Set global variables and initialize the printer. */ if(sys_outb(port_base + 2, INIT_PRINTER) != OK) { printf("printer: sys_outb of %x failed\n", port_base+2); panic("do_initialize: sys_outb init failed"); } micro_delay(1000000/20); /* easily satisfies Centronics minimum */ if(sys_outb(port_base + 2, PR_SELECT) != OK) { printf("printer: sys_outb of %x failed\n", port_base+2); panic("do_initialize: sys_outb select failed"); } irq_hook_id = 0; if(sys_irqsetpolicy(PRINTER_IRQ, 0, &irq_hook_id) != OK || sys_irqenable(&irq_hook_id) != OK) { panic("do_initialize: irq enabling failed"); } }
int ser_set(unsigned short base_addr, unsigned long bits, unsigned long stop, long parity, unsigned long rate) { if(base_addr != 1 && base_addr != 2) { printf("Incorrect com: d%\n", base_addr); return 1; } if(bits < 5 || bits > 8) { printf("Incorrect number of bits: %d\n", bits); return 1; } if(rate != STD_RATE_VALUE_1 && rate != STD_RATE_VALUE_2 && rate != STD_RATE_VALUE_3 && rate !=STD_RATE_VALUE_4 && rate !=STD_RATE_VALUE_5 && rate != STD_RATE_VALUE_6 && rate != STD_RATE_VALUE_7 && rate!= STD_RATE_VALUE_8 ) { printf("Incorrect rate: %d\n", rate); return 1; } if(stop != 1 && stop != 2) { printf("Incorrect number of stop bits: %d\n", parity); return 1; } if(parity < 0 || parity > 7) { printf ("Incorrect parity\n"); return 1; } unsigned long com_address; if(base_addr == 1) com_address = COM1; else com_address = COM2; unsigned long writable_word_length = bits - 5; unsigned long writable_stop_bits = ((stop-1) << 2); unsigned long writable_parity = (parity << 3); unsigned long out = writable_parity|writable_stop_bits|writable_word_length; unsigned long lcr = 0; if(sys_inb(com_address + LINE_CTRL_REG, &lcr) != OK) { printf("Error reading LINE_CTRL_REG\n"); return 1; } out |= (lcr & (DLAB|SET_BREAK_ENABLE)); if(sys_outb(com_address + LINE_CTRL_REG, out) != OK) { printf("Error writing LINE_CTRL_REG\n"); return 1; } unsigned long divisor = UART_FREQ / rate; if(set_divisor_latch(com_address, divisor)) { printf("Error setting divisor_latch\n"); return 1; } return 0; }
int timer_set_square(unsigned long timer, unsigned long freq) { unsigned long lsb; unsigned char msb; unsigned long fr; unsigned char st; //verifica se o resultado da frequencia é menor que o valor maximo possivel de representar em 2 bytes. if(TIMER_FREQ/freq >= 0xFFFF) { printf("Frequencia nao suportavel pelo timer (overflow).\n"); return 1; } fr = TIMER_FREQ / freq; if (timer == 0 || timer == 1 || timer == 2) { timer_get_conf(timer, &st); st=st<<4; st=st>>4; st=(TIMER_0 + timer | TIMER_LSB_MSB | st); //controlador criado mantendo o counting mode e operation if (sys_outb(TIMER_CTRL, st)) { printf("Error by calling sys_outb for confs\n"); return 1; } lsb = (char)fr; //isola o primeiro byte da frequencia fr = fr>>8; msb = (char)fr; //isola o segundo byte da frequencia if (sys_outb(TIMER_0 + timer, lsb)) //envia o LSB para o timer { printf("Error by calling sys_outb for lsb.\n"); return 1; } else if (sys_outb(TIMER_0 + timer, msb)) //envia o MSB para o timer { printf("Error by calling sys_outb for msb.\n"); return 1; } else { return 0; } } else
int KBD_toggle_led(int x) { static int led_status = 0; unsigned long status; if (x == 0){ if (led_status == 0 || led_status == 2 || led_status == 4 || led_status == 6) led_status += 1; else led_status -= 1; } else if (x == 1) { if (led_status == 0 || led_status == 4 || led_status == 5 || led_status == 1) led_status += 2; else led_status -= 2; } else if (x == 2) { if (led_status == 0 || led_status == 1 || led_status == 2 || led_status == 3) led_status += 4; else led_status -= 4; } while (status != ACK){ sys_outb(IN_BUF, LEDS_COMM); sys_inb(OUT_BUF, &status); //rec_cmd(); } if(sys_outb(IN_BUF, led_status) != OK) return 1; sys_inb(OUT_BUF, &status); //send_cmd(LED); return 0; }
int timer_get_conf(unsigned long timer, unsigned char *st) { unsigned char temp; //Initialize ReadBack Command temp = TIMER_RB_CMD | TIMER_RB_SEL(timer) |TIMER_RB_COUNT_; // Read Back Command sys_outb(TIMER_CTRL,temp); //execute previous command unsigned char timer_sel=TIMER_0+timer; unsigned long temp_st; // Auxiliar long variable, to be filled sys_inb(timer_sel,&temp_st); // There is no need for a cast this way *st = temp_st; // The original variable st is filled as supposed and the unimportant bits are truncated while passing a long to a char return 0; }
int sendCMDtoKBD(unsigned long command) { int n = 0; unsigned long stat; for(;n<MAX_TRIES; n++) { if( sys_inb(BUF_STAT, &stat) == OK && (stat & ERR_IN_FULL) == 0 && sys_outb(BUF_IN, command) == OK) { return 0; } tickdelay(micros_to_ticks(DELAY));} return -1; }
int timer_set_square(unsigned long timer, unsigned long freq) { // configura o read-back command unsigned char read_back = TIMER_SQR_WAVE | TIMER_BIN | (TIMER_0 + timer) | TIMER_LSB_MSB; // calcula a frequencia a ser utilizada unsigned long new_freq = TIMER_FREQ / freq; // tem que se passar 1 byte de cada vez, o lsb, seguido do msb unsigned char lsb = new_freq & 0xFF; unsigned char msb = new_freq >> 8; if (new_freq <= TIMER_FREQ && new_freq >= TIMER_MIN_FREQ) { // informa-se o controlar e de seguida passa-se o lsb e o msb sys_outb(TIMER_CTRL, read_back); sys_outb(TIMER_0 + timer, lsb); sys_outb(TIMER_0 + timer, msb); return 0; } else { printf("ERROR: freq out of range \n"); return 1; } }
int timer_disable_speaker() { unsigned long speaker; if (sys_inb(SPEAKER_CTRL, &speaker) == OK) { if (sys_outb(SPEAKER_CTRL, speaker & (char)(11111 << 3)) == OK) //// { return 0; } } return 1; }
int timer_enable_speaker() { unsigned long speaker; if (sys_inb(SPEAKER_CTRL, &speaker) == OK) { if (sys_outb(SPEAKER_CTRL, speaker | (long unsigned int)3) == OK) { return 0; } } return 1; }
int timer_set_square(unsigned long timer, unsigned long freq) { unsigned long finalfreq; finalfreq = TIMER_FREQ/freq; switch(timer){ case 0: printf("\tTimer 0 selected \n"); if(sys_outb(TIMER_CTRL,(TIMER_SEL0|TIMER_LSB_MSB|TIMER_SQR_WAVE|TIMER_BIN)) != OK) {printf("\t timer_set_square: ERROR setting timer 0 \n");return 1;} sys_outb(TIMER_0,((finalfreq) & 0xFF)); /* LSB */ sys_outb(TIMER_0,((finalfreq) >> 8)); /* MSB */ break; case 1: printf("\tTimer 1 selected \n"); if(sys_outb(TIMER_CTRL,(TIMER_SEL1|TIMER_LSB_MSB|TIMER_SQR_WAVE|TIMER_BIN)) != OK) {printf("\t timer_set_square: ERROR setting timer 1 \n");return 1;} sys_outb(TIMER_1,((finalfreq) & 0xFF)); /* LSB */ sys_outb(TIMER_1,((finalfreq) >> 8)); /* MSB */ break; case 2: printf("\tTimer 2 selected \n"); if(sys_outb(TIMER_CTRL,(TIMER_SEL2|TIMER_LSB_MSB|TIMER_SQR_WAVE|TIMER_BIN)) != OK) {printf("\t timer_set_square: ERROR setting timer 2 \n");return 1;} sys_outb(TIMER_2,((finalfreq) & 0xFF)); /* LSB */ sys_outb(TIMER_2,((finalfreq) >> 8)); /* MSB */ break; default: return 1; } return 0; }
int test_confi() { int i; for(i = 0; i < 14; i++) { sys_outb(RTC_ADDR_REG, i); sys_inb(RTC_DATA_REG, &data[i]); i++; } printf("A: 0x%x \n B: 0x%x \n C: 0x%x \n D: 0x%x \n", data[10], data[11], data[12], data[13]); }
int transmit_m(void * data, size_t size) { push_m(transmit_fifo, data, size); while(!is_empty_q(transmit_fifo) && transmitter_is_ready(transmit_port)){ if(sys_outb(transmit_port+TRANSMITTER_HOLDING_REG, front(transmit_fifo)) != OK) { printf("Error writing TRANSMITTER_HOLDING_REG\n"); return 1; } pop(transmit_fifo); } return 0; }
int transmit_b(unsigned char byte) { push(transmit_fifo, byte); while(!is_empty_q(transmit_fifo) && transmitter_is_ready(transmit_port)){ if(sys_outb(transmit_port+TRANSMITTER_HOLDING_REG, front(transmit_fifo)) != OK) { printf("Error writing TRANSMITTER_HOLDING_REG\n"); return 1; } pop(transmit_fifo); } return 0; }