[myhdl-list] Hello World
Brought to you by:
jandecaluwe
From: Günther S. <gue...@gm...> - 2015-04-25 18:12:36
|
Hello, i am quite new to Myhdl and playing around with the de0-nano Board. I was trying to access the ADXL345 over the SPI 3 wire bus. (just trying to read the DEVID) The small code example is working in simulation mode as far as i can judge. But on the real board i get 0x0 for the DEVID which should be 0xE5 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> import os import random from random import randrange from copy import copy ## python compile.py de0nano; cp altera/ise/stroby_de0nano/de0nano.qsf /home/tux/altera_proj/DE0_NANO_default ## python convert.py ; cp top.vhd pck_myhdl_081.vhd /home/tux/altera_proj/DE0_NANO_default ## python convert.py ; cp top.v tb_top.v /home/tux/altera_proj/DE0_NANO_default ## cp top.v top.v_orig; cp tb_top.v tb_top.v_orig; python convert.py ; cp top.v tb_top.v /home/tux/altera_proj/DE0_NANO_default; meld top.v top.v_orig; meld tb_top.v tb_top.v_orig; ## python gw.py; gtkwave test_dff.vcd ## pip install --upgrade git+https://github.com/jandecaluwe/myhdl from myhdl import * def clk_gen(CLOCK_50, tick, tick2): #CLK_FREQ = 48e6 # clock frequency #LED_RATE = 0.001 # strobe change rate CLK_FREQ = 10 # simulation only !!!! LED_RATE = 1 # simulation mode CNT_MAX = int(CLK_FREQ * LED_RATE) diff_rate = 0.45 CNT_DIFF = int(CLK_FREQ * diff_rate) clk_cnt = Signal(intbv(0, min=0, max=CNT_MAX+2)) @always(CLOCK_50.posedge) def logic(): clk_cnt.next = clk_cnt + 1 if clk_cnt == CNT_MAX-CNT_MAX/2: tick.next = 1 if clk_cnt == CNT_MAX-CNT_MAX/2+CNT_DIFF: tick2.next = 1 if clk_cnt == CNT_MAX: clk_cnt.next = 0 tick.next = 0 tick2.next = 0 return logic def readData(tick, tick2, G_SENSOR_CS_N,I2C_SDAT,LED , I2C_SCLK, count, START, PAUSE): w_adr_1 = Signal(intbv(0x8c)[8:0])#31-->31 w_data1 = Signal(intbv(0x02)[8:0])#40-->40 w_adr_2 = Signal(intbv(0x01)[8:0])#00-->80 r_data2 = Signal(intbv(0)[8:0]) # should be DEVID (0xE5) io = I2C_SDAT.driver() @always(tick.negedge) def readData_gen(): if count > 37+PAUSE: count.next = count -1 elif (count >=29+PAUSE) and (count <= 37+PAUSE) : count.next = count - 1 if (count >= 29+PAUSE) and (count <= 36+PAUSE): io.next = w_adr_1[36+PAUSE - count] elif (count >= 21+PAUSE) and (count < 29+PAUSE): count.next= count - 1 io.next = w_data1[28+PAUSE - count] elif (count >= 20) and (count < 21+PAUSE): count.next= count - 1 elif (count >= 11) and (count < 20): count.next = count - 1 if (count > 11) and (count <= 18): io.next = w_adr_2[18 - count] elif (count > 0) and (count < 11): count.next = count - 1 io.next = None else: count.next = START @always(tick2.posedge) def c_select(): if count == (45 + PAUSE): G_SENSOR_CS_N.next = 0 if count == (43 + PAUSE): G_SENSOR_CS_N.next = 1 if count == (36 + PAUSE): G_SENSOR_CS_N.next = 0 if count == (20 + PAUSE): G_SENSOR_CS_N.next = 1 if count == 18 : G_SENSOR_CS_N.next = 0 if count == 2 : G_SENSOR_CS_N.next = 1 if (count > 2) and (count < 11): if (I2C_SDAT == True): r_data2.next[10-count] = 1 else: r_data2.next[10-count] = 0 collector = Signal(intbv(0)[8:0]) @always_comb def check_sclk2(): if I2C_SDAT == 1: collector.next[0] = 1 else: collector.next[0] = 0 if I2C_SCLK == 1: collector.next[1] = 1 else: collector.next[1] = 0 if G_SENSOR_CS_N == 1: collector.next[2] = 0 else: collector.next[2] = 1 ################################# if count == 36+PAUSE: collector.next[7] = 1 else: collector.next[7] = 0 if count == 28+PAUSE: collector.next[6] = 1 else: collector.next[6] = 0 if count == 18: collector.next[5] = 1 else: collector.next[5] = 0 if count <=9 and count >=2: collector.next[4] = 1 else: collector.next[4] = 0 @always_comb def collector_comb(): LED.next = r_data2 #LED.next = collector return instances() def drive_spi(tick, tick2, G_SENSOR_CS_N,G_SENSOR_INT,I2C_SCLK,I2C_SDAT, LED, count, START, PAUSE): read_Adr_inst = readData( tick, tick2, G_SENSOR_CS_N,I2C_SDAT,LED , I2C_SCLK,count, START, PAUSE) @always_comb def al_c1(): I2C_SCLK.next = tick return instances() def top( CLOCK_50, LED,G_SENSOR_CS_N,G_SENSOR_INT,I2C_SCLK,I2C_SDAT): tick = Signal(False) tick2 = Signal(False) START = 40 PAUSE = 4 count = Signal(intbv(START, min=-1, max=10000)) clk_gen_inst = clk_gen(CLOCK_50, tick, tick2) drive_spi_inst = drive_spi(tick, tick2, G_SENSOR_CS_N,G_SENSOR_INT,I2C_SCLK,I2C_SDAT, LED, count, START, PAUSE) return instances() def test_dff(): G_SENSOR_CS_N = Signal(bool(1)) G_SENSOR_INT = Signal(bool(0)) I2C_SCLK = Signal(bool(1)) I2C_SDAT = TristateSignal(False) LED = Signal(intbv(0)[8:]) clk = Signal(bool(0)) dff_inst = top( clk, LED,G_SENSOR_CS_N,G_SENSOR_INT,I2C_SCLK,I2C_SDAT) @always(delay(1)) def clkgen(): clk.next = not clk return dff_inst, clkgen def simulate(timesteps): tb = traceSignals(test_dff) sim = Simulation(tb) sim.run(timesteps) simulate(4000) ######################################################## main.py: from myhdl import * from gw import top def convert(): G_SENSOR_CS_N = Signal(bool(1)) G_SENSOR_INT = Signal(bool(0)) I2C_SCLK = Signal(bool(1)) I2C_SDAT = TristateSignal(False) CLOCK_50 = Signal(bool(0)) LED = Signal(intbv(0)[8:]) ## toVerilog(top,clock,reset,LED) ## toVHDL(top,CLOCK_50, LED,G_SENSOR_CS_N,G_SENSOR_INT,I2C_SCLK,I2C_SDAT ) toVerilog(top,CLOCK_50, LED,G_SENSOR_CS_N,G_SENSOR_INT,I2C_SCLK,I2C_SDAT ) if __name__ == '__main__': convert() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I could not see any errors whan i look on the gtkwave signal. Mybe somebody has the same eval board and is somewhat familiar with the SPI. My guess is, that i am doing something wrong with the tristate signal I2C_SDAT ! Could this be the reason that a still have 0x0 instead of 0xE5 when i read this port? I would very much appreciate if someone could help me out because i really like myhdl a lot even i have not seen al lot of this tooling. Thank you very much. -- Mit freundlichen Grüßen Günther Stangassinger |