OpenCores
URL https://opencores.org/ocsvn/ssbcc/ssbcc/trunk

Subversion Repositories ssbcc

[/] [ssbcc/] [trunk/] [example/] [i2c/] [lib_i2c.s] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sinclairrf
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;
3
; Copyright 2012, Sinclair R.F., Inc.
4
;
5
; Major I2C functions:
6
;   i2c_send_start      ( - )
7
;   i2c_send_byte       ( u - f )
8
;   i2c_read_byte       ( - u )
9
;   i2c_send_stop       ( - )
10
;
11
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12
 
13
; Compute how many iterations in the quarter-clock-cycle function are required.
14
;   ceil(100 MHz / 400 kHz / 4) ==> 63 clock cycles per I2C SCL quarter cycle
15
;   The function consumes:
16
;     3 clock cycles to call the function
17
;     3 clock cycles for the "outport(O_SCL)"
18
;     1 clock cycle to initialize the loop count
19
;     2 clock cycles to return
20
;     9 TOTAL
21
;   Add 2 to ensure rounding up when evaluting the integer fraction.
22
;   The loop is 3 clock cycles per iteration
23
.constant C_I2C_QUARTER_CYCLE ${(63-9+2)/3}
24
 
25
; ( - )
26
.function i2c_send_start
27
 
28
  .call(i2c_quarter_cycle,1)
29
  .call(i2c_quarter_cycle,0)
30
.return
31
 
32
; Set a start without the preceding stop.
33
; ( - )
34
.function i2c_send_restart
35
  .call(i2c_quarter_cycle,0)
36
  .call(i2c_quarter_cycle,1)
37
 
38
  .call(i2c_quarter_cycle,0)
39
.return
40
 
41
; Send the byte and indicate false if the acknowledge bit was received.
42
; ( u - f )
43
.function i2c_send_byte
44
  ; send the byte, msb first
45
  ; ( u - )
46
  ${8-1} :outer
47
    ; send the next bit
48
    swap <<msb swap
49
    .call(i2c_clock_cycle,over) drop
50
  .jumpc(outer,1-) drop drop
51
  ; get the acknowledge bit at the middle of the high portion of SCL
52
  ; ( - f )
53
  .call(i2c_clock_cycle,1)
54
.return
55
 
56
; Read the next byte from the device.
57
; ( - u )
58
.function i2c_read_byte
59
 
60
    swap <<0 .call(i2c_clock_cycle,1) or swap
61
  .jumpc(loop,1-) drop
62
  ; send the acknowledgment bit
63
  .call(i2c_clock_cycle,0)
64
.return(drop)
65
 
66
; Send a stop by bringing SDA high while SCL is high.
67
; ( - )
68
.function i2c_send_stop
69
 
70
  .call(i2c_quarter_cycle,1)
71
  1 .outport(O_SDA) .call(i2c_quarter_cycle,1)
72
.return
73
 
74
; Send the clock as a "0110" pattern and sample SDA in the middle of the high
75
; portion.
76
; ( u_sda_out - u_sda_in )
77
.function i2c_clock_cycle
78
  .outport(O_SDA)
79
  .call(i2c_quarter_cycle,0)
80
  .call(i2c_quarter_cycle,1)
81
  .inport(I_SDA)
82
  .call(i2c_quarter_cycle,1)
83
  .call(i2c_quarter_cycle,0)
84
.return
85
 
86
; Output the I2C SCL value and then wait for a quarter of the I2C clock cycle.
87
; ( u_scl - )
88
.function i2c_quarter_cycle
89
  .outport(O_SCL)
90
  ${C_I2C_QUARTER_CYCLE-1} :loop .jumpc(loop,1-)
91
.return(drop)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.