LAB 5 Tarjeta de Desarrollo Spartan-3 Laboratorio de Sistemas Digitales ELO212 Primer Semestre de 2010 Objetivos Generales Controlar dispositivos mediante una FPGA. Conocer la interfaz PS/2 para conexión de un teclado. Aprender el menejo de: pulsadores, interruptores, leds, display de 7 segmentos. Interfaz PS/2 Permite conectar periféricos teclado y mouse. Protocolo de comunicación serial bidireccional. Para teclados se garantiza el conjunto 2 de los scan codes. Responden a todos los comandos enviados, sin embargo no actúan en todos ellos. Socket y Pinout Pin 1: Data Pin 2: Reservado Pin 3: Ground Pin 4: Vcc (+5V) Pin 5: Clock Pin 6: Reservado Comunicación Bidireccional Estado IDLE: Estado INHIBIT: Data y Clock en alto Data en alto y Clock en bajo Estado REQUEST TO SEND: Data en bajo y Clock en alto PS/2 Frame (1) 1 bit de partida: siempre es 0. 8 bits de datos: LSB a MSB. 1 bit de paridad (impar) 1 bit de parada: siempre es 1. 1 bit de ack: sólo para comunicación host → teclado/mouse. PS/2 Frame (2) Scan Code (1) Hay varios tipos de scan codes, el más popular (por omisión) es el número 2. Cada tecla tiene asociado un scan code compuesto por: make code, que se emite al presionar una tecla breake code, que se emite al soltar la tecla Scan Code (2): Ejemplos Key Make Break ASCII A 1C F0,1C 41 B 32 F0,32 42 C 21 F0,21 43 D 23 F0,23 44 E 24 F0,24 45 F 2B F0,2B 46 G 34 F0,34 47 H 33 F0,33 48 I 43 F0,43 49 PS/2 Driver Verilog (1) kd ShiftRegSig1 ShiftRegSig2 kc 10 0 kbs 0 value xxxxxxxx 10 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 x x x x x x x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 x x x x x x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 0 x x x x x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 0 0 x x x x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 0 0 0 x x x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 0 0 0 0 0 x x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 0 0 0 0 0 x x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 1 0 0 0 0 0 x x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 1 1 0 0 0 0 0 x x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 1 1 1 0 0 0 0 0 x x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 1 1 1 1 0 0 0 0 0 x x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 1 1 1 1 1 1 0 0 0 0 0 x x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 1 1 1 1 1 1 0 0 0 0 0 x x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 x 10 10 0 kbs 0 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 0 10 10 0 kbs 1 value xxxxxxxx 1 PS/2 Driver Verilog (1) kd kc ShiftRegSig1 ShiftRegSig2 1 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 10 10 0 kbs 0 value 1C 1 PS/2 Driver Verilog (2a) module kbd(rst, clk, kd, kc, kbs, value); input rst, clk, kd, kc; // reset, clock, k-data, k-clock output reg kbs; // keyboard stroke (negedge) output reg [7:0] value; // scan code // -----------------------------------------------------------------// -- Signal Declarations // -----------------------------------------------------------------reg kdi, kci; reg dff1, dff2; reg [10:0] ShiftRegSig1; reg [10:1] ShiftRegSig2; reg kbs_tmp; PS/2 Driver Verilog (2b) // -----------------------------------------------------------------// -- Module Implementation // -----------------------------------------------------------------// --Flip Flops used to condition signals coming from PS2-always @ (posedge clk or posedge rst) begin if (rst == 1) begin dff1 <= 0; dff2 <= 0; kdi <= 0; kci <= 0; end else begin dff1 <= kd; kdi <= dff1; dff2 <= kc; kci <= dff2; end end PS/2 Driver Verilog (2c) // --Shift Registers used to clock in scan codes from PS2-always @(negedge kci or posedge rst) begin if (rst == 1) begin ShiftRegSig1 <= 11'b00000000000; ShiftRegSig2 <= 10'b0000000000; end else begin ShiftRegSig1[10:0] <= {kdi, ShiftRegSig1[10:1]}; ShiftRegSig2[10:1] <= {ShiftRegSig1[0], ShiftRegSig2[10:2]}; end end PS/2 Driver Verilog (2d) // --Wait for scan code always @(posedge rst or posedge kci) begin if (rst == 1) begin value <= 8'b00000000; kbs_tmp <= 0; end else if (ShiftRegSig2[9:2] == 8'b11110000) begin value <= value; kbs_tmp <= 1; end else if (ShiftRegSig2[8:1] == 8'b11110000) begin value <= ShiftRegSig1[8:1]; kbs_tmp <= 0; end end always @(negedge clk) kbs <= kbs_tmp; endmodule Ejemplo Uso Driver PS/2 module kbdUse(rst, clk, kd, kc, led); input rst, clk, kd, kc; output reg [7:0] led; wire [7:0] value; kbd kbd0(rst, clk, kd, kc, kbs, value); always @ (negedge rst or negedge kbs) if (rst == 0) led <= 0; else led <= led + 1; endmodule Display 7 segmentos 4 dígitos (7 segmentos). No dispone de conversores BCD / 7 segmentos. Ánodo común. 12 salidas para controlar el display. Se debe multiplexar en el tiempo para poder ver números de más de 1 dígito. Configuración del Display Conexiones Driver Verilog BCD/7Seg. module ssdec(val, pt, type, ssg); input [3:0] val; // binary value input pt, type; // point, display type (0: anode, 1: cathode) output [7:0] ssg; // segments assign ssg = ((type == 1) ? 8'h0 : 8'hff) ^ ( (val == 0) ? {pt, 7'b0111111} : (val == 1) ? {pt, 7'b0000110} : (val == 2) ? {pt, 7'b1011011} : (val == 3) ? {pt, 7'b1001111} : // …… (val == 11) ? {pt, 7'b1111100} : (val == 12) ? {pt, 7'b1011000} : (val == 13) ? {pt, 7'b1011110} : (val == 14) ? {pt, 7'b1111001} : {pt, 7'b1110001}); endmodule Driver Verilog Display 7Seg. (1) module display(clk, num, type, pts, sseg, an); input clk, type; input [3:0] pts; input [15:0] num; output [7:0] sseg; output reg [3:0] an; // fill in …. endmodule Contador BCD module bcdcounter(rst, clk, value); input rst, clk; output reg [15:0] value; // reset, clock // 4 bcd digits always @(negedge clk or posedge rst) begin if (rst == 1) value <= 0; else begin if (value[3:0] == 9) begin value[3:0] <= 0; if (value[7:4] == 9) begin value[7:4] <= 0; if (value[11:8] == 9) begin value[11:8] <= 0; if (value[15:12] == 9) value[15:12] <= 0; else value[15:12] <= value[15:12] + 1; end else value[11:8] <= value[11:8] + 1; end else value[7:4] <= value[7:4] + 1; end else value <= value + 1; end end endmodule Uso de Pulsadores, Interruptores y leds module (btn, swt, led); input [0:3] btn; input [0:7] swt; output [0:7] led; assign led[0:3] = btn; assign led[4:7] = swt[0:3]; endmodule Trabajo Previo (1) Diseño de un módulo para visualizar una cuenta en el display Completar diseño del módulo display. Usar display para mostrar tiempo MM.SS. Hacer simulación funcional y temporal. Módulo para uso de leds, interruptores y pulsadores. Leds reflejan el estado del interruptor. Pulsador 0: enciende todos los leds. Pulsador 1: apaga todos los leds. Pulsador 2: invierte estado de los leds. Hacer simulación funcional. Trabajo Previo (2) Diseño de un módulo que permita: Visualizar scan codes de teclado en display 7 segmentos, dígitos menos significativos. Visualizar el valor de teclas numéricas en los dígitos más significativos, o “FF” si no es número. En el Laboratorio Revisión actividades previas. Sintetizar y demostrar el funcionamiento del módulo reloj en formato MM.SS. Sintetizar el módulo para uso de pulsadores, interruptores y leds. Demostrar el funcionamiento del módulo que despliega scan codes del teclado PS/2. Diseño de una máquina digital. Máquina Digital (1) Op. Contenido del display y leds 0 Contador BCD ascendente X Hz. 1 Contador BCD descendente Y Hz. 2 Contador ascendente binario de X Hz. 3 Contador descendente binario de Y Hz. 4 Cuenta el número de teclas presionadas. Máquina Digital (2) Op. Contenido del display y leds 5 Número de repeticiones de una tecla. 6 2MSD ⇒ op. 0, 2LSD ⇒ op. 1. 7 Display off, se mantienen las cuentas. 8 Leds pestañean X Hz, se mantienen las cuentas. 9 Comportamiento dinámico para leds y puntos. Máquina Digital (2) Op. Contenido del display y leds 10 2LSD ⇒ último pulsador (0 - 3); mantiene cuentas. 11 Tiempo [ms] entre 2 eventos PS/2, máx 9999. 12 Tiempo transcurrido en MM.SS. Máquina Digital (3) Pulsador Descripción operación 0 Resetea todas las cuentas. 1 Display rotate right (eg. 1): 1234 ⇒ 4123 2 Display rotate left (eg. 1): 1234 ⇒ 2341 3 Blinking leds at Y Hz.