]> iEval git - clump.git/blobdiff - master.v
Implement a tiny part of i2c
[clump.git] / master.v
index b54abe9ad0e9ef94025b22001e66658e9082ff39..f76bfb1a9fcd11920b74597c1a02190d8c588ed7 100644 (file)
--- a/master.v
+++ b/master.v
@@ -1,4 +1,6 @@
 `include "master_rom.v"
+`include "i2c.v"
+`include "uart.v"
 
 `ifdef SIM
  `define UART_DIVIDE 1
@@ -7,14 +9,17 @@
  // s/192/3/ for 19200 baud uart
 `endif
 
-module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, output reg ready_out = 1, input ready_in);
-   wire clk;
-   wire clk_tmp;
+module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, output reg ready_out = 1, input ready_in, output scl, output sda);
+//   wire clk;
+//   wire clk_tmp;
 
    //pll pll (.clock_in(CLKin), .clock_out(clk));
 
    reg [20:0] counter = 0;
 
+`ifdef SIM
+   wire clk = CLKin;
+`else
    reg                   clk = 0;
 
    always @ (posedge CLKin) begin
@@ -25,12 +30,41 @@ module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, outp
          else
                counter <= counter + 1;
    end
+`endif
 
    reg [3:0] program_counter = 0;
    wire [31:0] rom_output;
 
    master_rom master_rom (.clk(clk), .addr(program_counter), .data(rom_output));
 
+   reg [7:0]   i2c_tx_byte;
+   reg                    i2c_transmit = 0;
+   wire           i2c_is_transmitting;
+
+   i2c_write i2c (.clk(clk), .scl(scl), .sda(sda), .tx_byte(i2c_tx_byte), .transmit(i2c_transmit), .is_transmitting(i2c_is_transmitting));
+
+   reg [3:0]   i2c_init_step = 0;
+
+   always @ (posedge clk) begin
+         if(i2c_is_transmitting || i2c_transmit)
+               i2c_transmit <= 0;
+         else begin
+                if(i2c_init_step == 0) begin
+                       i2c_tx_byte <= 8'h21; // turn on oscillator
+                       i2c_transmit <= 1;
+                       i2c_init_step <= 1;
+                end else if(i2c_init_step == 1) begin
+                       i2c_tx_byte <= 8'h87; // display on, blink 0.5Hz
+                       i2c_transmit <= 1;
+                       i2c_init_step <= 2;
+                end else if(i2c_init_step == 2) begin
+                       i2c_tx_byte <= 8'hEF; // max brightness
+                       i2c_transmit <= 1;
+                       i2c_init_step <= 3;
+                end
+         end
+   end
+
 
 `define STATE_SEND 0
 `define STATE_WAIT_PROPAGATE 1
@@ -56,7 +90,7 @@ module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, outp
    reg [7:0]   saved_news [3:0];
 
    assign led[4] = state != `STATE_WASTE_TIME;
-   assign led[3:0] = 0;
+   assign led[3:0] = i2c_init_step;
 
    always @(posedge clk) begin
          case(state)
@@ -96,7 +130,7 @@ module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, outp
                           - receive the instruction back
                           - receive the news
                           - propagate the news
-                          - go back to `STATE_SEND
+                          - go to `STATE_WASTE_TIME
                        */
                   if(uart_ptr == 8) begin
                          state <= `STATE_PROPAGATE_NEWS;
This page took 0.027304 seconds and 4 git commands to generate.