Author: jsuijs
Date: Sun Nov 30 06:10:47 2008
New Revision: 600
Added:
trunk/unvalidated/include/peripheral/i2c/i2c_level1.jal
trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l1.jal
trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l1.jal
Modified:
trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal
trunk/unvalidated/include/peripheral/i2c/i2c_software.jal
trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l0.jal
trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l0.jal
Log:
long overdue level1 i2c routines (and fix in error handling of
i2c_software.jal).
Modified: trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal
==============================================================================
--- trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal (original)
+++ trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal Sun Nov 30
06:10:47 2008
@@ -86,10 +86,7 @@
function i2c_transmit_byte(byte in data) return bit is
sspbuf = data
- -- wait untill write is finished: _R_W =0
- -- we can also wait for SSPIF, so we don't need bank switching
- ; bank_1
- ; while _r_w == high loop end loop
+ -- wait untill write is finished
PIR1_SSPIF = false
; delay_10us(10)
while ! PIR1_SSPIF loop end loop
@@ -98,9 +95,9 @@
-- wich indicates that the slave has responded (or not)
-- if i2c device send an Aknowledge, then ready and OK
if SSPCON2_ACKSTAT == low then
- return true
+ return true -- okay
else
- return false
+ return false -- no response
end if
end function
--
-----------------------------------------------------------------------------
Added: trunk/unvalidated/include/peripheral/i2c/i2c_level1.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/include/peripheral/i2c/i2c_level1.jal Sun Nov 30
06:10:47 2008
@@ -0,0 +1,79 @@
+-- Title: i2c_hardware
+-- Author: Joep Suijs, Copyright (C) 2008 Joep Suijs
+-- Adapted-by:
+-- Compiler: >=2.4g
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: Level1 implementation of I2C (single) master.
+--
+-- TODO:
+--
+
+--
-----------------------------------------------------------------------------
+-- i2c_send_receive - send a string to an i2c slave and receive bytes back
+--
-----------------------------------------------------------------------------
+-- This funtion uses two global i2c buffers, one for transmit (tx) and one
for
+-- receive (rx). The address of the slave (higher 7 bits), number of bytes
to
+-- transmit and number of bytes to receive are parameters.
+-- returns true if all went well.
+--
-----------------------------------------------------------------------------
+function i2c_send_receive(byte in i2c_address, byte in i2c_tx_count, byte
in i2c_rx_count) return bit is
+ var byte i
+ var bit r = true -- okay
+
+ -- read first 3 bytes from an 24lc256 eeprom and output these to the
serial port
+ -- then, increment the last one (write one byte)
+ i2c_start()
+ r = i2c_transmit_byte(i2c_address & 0xFE) -- i2c address for write (for
memory address, within eeprom)
+ if (r == false) then return r end if
+
+ for i2c_tx_count using i loop
+ r = r & i2c_transmit_byte(i2c_tx_buffer[i])
+ if (r == false) then return r end if
+ end loop
+
+ if (i2c_rx_count > 0) then
+ i2c_restart()
+ r = r & i2c_transmit_byte(i2c_address | 0x01) -- i2c address for
read (of memory from eeprom)
+ if (r == false) then return r end if
+
+ if (i2c_rx_count > 1) then
+ for (i2c_rx_count-1) using i loop
+ i2c_rx_buffer[i] = i2c_receive_byte(true)
+ end loop
+ end if
+
+ i2c_rx_buffer[i2c_rx_count-1] = i2c_receive_byte(false)
+ end if
+
+ i2c_stop()
+
+ return true;
+
+end function
+
+
+
+--
-----------------------------------------------------------------------------
+-- i2c_receive_byteaddr - send a byte address and receive a string from an
i2c slave
+--
-----------------------------------------------------------------------------
+function i2c_receive_byteaddr(byte in i2c_address, byte in addr, byte in
i2c_rx_count) return bit is
+
+ i2c_tx_buffer[0] = addr
+ return i2c_send_receive(i2c_address, 1, i2c_rx_count)
+
+end function
+
+
+--
-----------------------------------------------------------------------------
+-- i2c_receive_wordaddr - send a word address and receive a string from an
i2c slave
+--
-----------------------------------------------------------------------------
+function i2c_receive_wordaddr(byte in i2c_address, word in addr, byte in
i2c_rx_count) return bit is
+
+ i2c_tx_buffer[0] = byte(addr >> 8)
+ i2c_tx_buffer[1] = byte(addr & 0xFF)
+ return i2c_send_receive(i2c_address, 2, i2c_rx_count)
+
+end function
Modified: trunk/unvalidated/include/peripheral/i2c/i2c_software.jal
==============================================================================
--- trunk/unvalidated/include/peripheral/i2c/i2c_software.jal (original)
+++ trunk/unvalidated/include/peripheral/i2c/i2c_software.jal Sun Nov 30
06:10:47 2008
@@ -79,6 +79,8 @@
-- ------------------------------------------------------------------------
function i2c_transmit_byte( byte in x ) return bit is
var bit b at x : 7
+ var bit r
+
for 8 loop
_i2c_bit_out( b )
x = x << 1
@@ -96,11 +98,13 @@
-- a check for the slave's acknowledge
-- (data should be low)
-- could be added here
+ r = ! i2c_sda
_i2c_wait()
i2c_scl_direction = low -- clock low
_i2c_wait()
;end procedure
+ return r -- true = okay, false = no ack received
end function
-- ------------------------------------------------------------------------
Modified: trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l0.jal
==============================================================================
--- trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l0.jal
(original)
+++ trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l0.jal Sun Nov
30 06:10:47 2008
@@ -54,11 +54,11 @@
-- then, increment the last one (write one byte)
i2c_start()
r = i2c_transmit_byte(0xA0) -- i2c address for write (for memory
address, within eeprom)
- r = r | i2c_transmit_byte(0x00) -- memory address high byte
- r = r | i2c_transmit_byte(0x00) -- memory address low byte
+ r = r & i2c_transmit_byte(0x00) -- memory address high byte
+ r = r & i2c_transmit_byte(0x00) -- memory address low byte
i2c_restart()
- r = r | i2c_transmit_byte(0xA1) -- i2c address for read (of memory from
eeprom)
+ r = r & i2c_transmit_byte(0xA1) -- i2c address for read (of memory from
eeprom)
i = i2c_receive_byte(true)
print_byte_hex(serial_hw_data, i);
@@ -77,9 +77,9 @@
-- write part (relies on var i, we just read!)
i2c_start()
r = i2c_transmit_byte(0xA0) -- i2c address for write (for memory
address, within eeprom)
- r = r | i2c_transmit_byte(0x00) -- memory address high byte
- r = r | i2c_transmit_byte(0x02) -- memory address low byte
- r = r | i2c_transmit_byte(i+1) -- data byte
+ r = r & i2c_transmit_byte(0x00) -- memory address high byte
+ r = r & i2c_transmit_byte(0x02) -- memory address low byte
+ r = r & i2c_transmit_byte(i+1) -- data byte
i2c_stop()
print_crlf(serial_hw_data)
Added: trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l1.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_hw_l1.jal Sun Nov
30 06:10:47 2008
@@ -0,0 +1,89 @@
+-- ------------------------------------------------------
+-- Title: Test program for i2c_hardware.jal
+--
+-- Author: Joep Suijs, Copyright (c) 2008..2008, all rights reserved.
+--
+-- Adapted-by:
+--
+-- Compiler: >=2.4g
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: Level 0 i2c test program
+-- interact with an i2c eeprom using the i2c primitives
+-- --
+-- This file defines a test for JALLIB testing, using a test-board
+-- defined by a BOARD file .
+--
+-- Sources:
+--
+-- Notes:
+-- setup:
+-- an 24lc256 i2c eeprom, connected to the test board i2c pins.
+-- a serial connection to a terminal
+--
+-- ------------------------------------------------------
+
+-- generic setup (see releated examples for more details)
+const usart_hw_serial = true -- true = RS232, false = SPI
+const serial_hw_baudrate = 115_200
+include serial_hardware
+serial_hw_init()
+include print
+include delay
+
+-- i2c setup
+const word _i2c_bus_speed = 1 ; * 100kHz
+const bit _i2c_level = true ; i2c levels (not SMB)
+
+include i2c_software
+;include i2c_hardware
+
+i2c_initialize()
+
+
+var byte i2c_tx_buffer[10]
+var byte i2c_rx_buffer[10]
+
+include i2c_level1
+
+-- we're alive
+const byte str1[] = "Test.\r\n"
+print_string(serial_hw_data, str1)
+
+
+var bit r
+var byte i
+forever loop
+
+ -- Send 2 bytes address to device 0xA0 and then read 3 bytes of data
+; i2c_tx_buffer[0] = 0 -- high byte address within i2c eeprom
+; i2c_tx_buffer[1] = 0 -- low byte address within i2c eeprom
+; r = i2c_send_receive(0xA0, 2, 3)
+
+ -- same as above
+ r = i2c_receive_wordaddr(0xA0, 0x0000, 3)
+
+ print_bit_truefalse(serial_hw_data, r)
+ serial_hw_data = " "
+
+ print_byte_hex(serial_hw_data, i2c_rx_buffer[0]);
+ serial_hw_data = " "
+ print_byte_hex(serial_hw_data, i2c_rx_buffer[1]);
+ serial_hw_data = " "
+ print_byte_hex(serial_hw_data, i2c_rx_buffer[2]);
+ serial_hw_data = " "
+
+ -- write part (increment 3rd byte at 0x0002)
+ i2c_tx_buffer[0] = 0 -- high byte address within i2c eeprom
+ i2c_tx_buffer[1] = 2 -- low byte address within i2c eeprom
+ i2c_tx_buffer[2] = i2c_rx_buffer[2] + 1 -- data
+ r = i2c_send_receive(0xA0, 3, 0)
+ print_bit_truefalse(serial_hw_data, r)
+ serial_hw_data = " "
+
+ print_crlf(serial_hw_data)
+
+ delay_100ms(10)
+end loop
Modified: trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l0.jal
==============================================================================
--- trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l0.jal
(original)
+++ trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l0.jal Sun Nov
30 06:10:47 2008
@@ -54,11 +54,11 @@
-- then, increment the last one (write one byte)
i2c_start()
r = i2c_transmit_byte(0xA0) -- i2c address for write (for memory
address, within eeprom)
- r = r | i2c_transmit_byte(0x00) -- memory address high byte
- r = r | i2c_transmit_byte(0x00) -- memory address low byte
+ r = r & i2c_transmit_byte(0x00) -- memory address high byte
+ r = r & i2c_transmit_byte(0x00) -- memory address low byte
i2c_restart()
- r = r | i2c_transmit_byte(0xA1) -- i2c address for read (of memory from
eeprom)
+ r = r & i2c_transmit_byte(0xA1) -- i2c address for read (of memory from
eeprom)
i = i2c_receive_byte(true)
print_byte_hex(serial_hw_data, i);
@@ -77,9 +77,9 @@
-- write part (relies on var i, we just read!)
i2c_start()
r = i2c_transmit_byte(0xA0) -- i2c address for write (for memory
address, within eeprom)
- r = r | i2c_transmit_byte(0x00) -- memory address high byte
- r = r | i2c_transmit_byte(0x02) -- memory address low byte
- r = r | i2c_transmit_byte(i+1) -- data byte
+ r = r & i2c_transmit_byte(0x00) -- memory address high byte
+ r = r & i2c_transmit_byte(0x02) -- memory address low byte
+ r = r & i2c_transmit_byte(i+1) -- data byte
i2c_stop()
print_crlf(serial_hw_data)
Added: trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l1.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/sample/test/peripheral/i2c/test_i2c_sw_l1.jal Sun Nov
30 06:10:47 2008
@@ -0,0 +1,89 @@
+-- ------------------------------------------------------
+-- Title: Test program for i2c_hardware.jal
+--
+-- Author: Joep Suijs, Copyright (c) 2008..2008, all rights reserved.
+--
+-- Adapted-by:
+--
+-- Compiler: >=2.4g
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: Level 0 i2c test program
+-- interact with an i2c eeprom using the i2c primitives
+-- --
+-- This file defines a test for JALLIB testing, using a test-board
+-- defined by a BOARD file .
+--
+-- Sources:
+--
+-- Notes:
+-- setup:
+-- an 24lc256 i2c eeprom, connected to the test board i2c pins.
+-- a serial connection to a terminal
+--
+-- ------------------------------------------------------
+
+-- generic setup (see releated examples for more details)
+const usart_hw_serial = true -- true = RS232, false = SPI
+const serial_hw_baudrate = 115_200
+include serial_hardware
+serial_hw_init()
+include print
+include delay
+
+-- i2c setup
+const word _i2c_bus_speed = 1 ; * 100kHz
+const bit _i2c_level = true ; i2c levels (not SMB)
+
+include i2c_software
+;include i2c_hardware
+
+i2c_initialize()
+
+
+var byte i2c_tx_buffer[10]
+var byte i2c_rx_buffer[10]
+
+include i2c_level1
+
+-- we're alive
+const byte str1[] = "Test.\r\n"
+print_string(serial_hw_data, str1)
+
+
+var bit r
+var byte i
+forever loop
+
+ -- Send 2 bytes address to device 0xA0 and then read 3 bytes of data
+ i2c_tx_buffer[0] = 0 -- high byte address within i2c eeprom
+ i2c_tx_buffer[1] = 0 -- low byte address within i2c eeprom
+ r = i2c_send_receive(0xA0, 2, 3)
+
+; -- same as above
+; r = i2c_receive_wordaddr(0xA0, 0x0000, 3)
+
+ print_bit_truefalse(serial_hw_data, r)
+ serial_hw_data = " "
+
+ print_byte_hex(serial_hw_data, i2c_rx_buffer[0]);
+ serial_hw_data = " "
+ print_byte_hex(serial_hw_data, i2c_rx_buffer[1]);
+ serial_hw_data = " "
+ print_byte_hex(serial_hw_data, i2c_rx_buffer[2]);
+ serial_hw_data = " "
+
+ -- write part (increment 3rd byte at 0x0002)
+ i2c_tx_buffer[0] = 0 -- high byte address within i2c eeprom
+ i2c_tx_buffer[1] = 2 -- low byte address within i2c eeprom
+ i2c_tx_buffer[2] = i2c_rx_buffer[2] + 1 -- data
+ r = i2c_send_receive(0xA0, 3, 0)
+ print_bit_truefalse(serial_hw_data, r)
+ serial_hw_data = " "
+
+ print_crlf(serial_hw_data)
+
+ delay_100ms(10)
+end loop
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---