Author: jsuijs
Date: Sun Sep 28 02:21:24 2008
New Revision: 332

Added:
    trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal
Modified:
    trunk/unvalidated/sample/test/peripheral/i2c/i2c_test_l0.jal

Log:
And i2c_hardware is running too.

Added: trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/include/peripheral/i2c/i2c_hardware.jal   Sun Sep 28  
02:21:24 2008
@@ -0,0 +1,142 @@
+-- Title: i2c_hardware
+-- Author: Stef Mientki, Copyright (C) 2005 Stef Mientki
+-- Adapted-by: Joep Suijs.
+-- 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: Hardware implementation of I2C (single) master.
+--
+-- TODO:
+--
+
+-- --------------------------------------------------------------------
+-- some constants needed for the baudrate generator
+-- --------------------------------------------------------------------
+const _i2c_100kHz = (target_clock / (4 *  100_000)) - 1
+const _i2c_400kHz = (target_clock / (4 *  400_000)) - 1
+const _i2c_1MHz   = (target_clock / (4 * 1000_000)) - 1
+-- --------------------------------------------------------------------
+
+
+-- --------------------------------------------------------------------
+-- if the device has changed, change the speed if necessary
+-- --------------------------------------------------------------------
+procedure i2c_initialize() is
+
+   -- One time initialize ---------------------------------------------
+   -- i2c requires to set TRIS bits to input
+   -- so to be sure ...
+   i2c_scl_direction = input
+   i2c_sda_direction = input
+
+   sspcon  = 0b0010_1000   -- set up 16f87x as master device
+   sspcon2 = 0b0010_0000   -- sets default acknowledge bit value
+   -- -----------------------------------------------------------------
+
+   -- set correct speed
+   if    _i2c_bus_speed == 10 then sspadd = _i2c_1MHz
+   elsif _i2c_bus_speed == 4  then sspadd = _i2c_400kHz
+   else                            sspadd = _i2c_100kHz
+   end if
+
+   -- set correct level
+   -- SMP-bit7 = 0, slew rate enabled, better for EMC
+   if _i2c_level then
+      sspstat = 0b_0000_0000   ;i2c bus levels, CKE=0
+   else
+      sspstat = 0b_0100_0000   ;SMbus levels, CKE=1
+   end if
+end procedure
+--  
-----------------------------------------------------------------------------
+
+
+--  
-----------------------------------------------------------------------------
+-- i2c_start - Sends Start bit and waits untill finished
+--  
-----------------------------------------------------------------------------
+procedure i2c_start() is
+   SSPCON2_SEN = high
+   while SSPCON2_SEN == high loop end loop
+end procedure
+--  
-----------------------------------------------------------------------------
+
+--  
-----------------------------------------------------------------------------
+-- i2c_restart - Sends Restart bit and waits untill finished
+--  
-----------------------------------------------------------------------------
+procedure i2c_restart() is
+   SSPCON2_RSEN = high
+   while SSPCON2_RSEN == high loop end loop
+end procedure
+--  
-----------------------------------------------------------------------------
+
+--  
-----------------------------------------------------------------------------
+-- i2c_stop - Sends Stop bit and waits until finished
+--  
-----------------------------------------------------------------------------
+procedure i2c_stop() is
+   SSPCON2_PEN = high
+   while SSPCON2_PEN == high loop end loop
+end procedure
+--  
-----------------------------------------------------------------------------
+
+--  
-----------------------------------------------------------------------------
+-- i2c_transmit_byte -
+--  
-----------------------------------------------------------------------------
+--  
-----------------------------------------------------------------------------
+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
+   PIR1_SSPIF = false
+   ; delay_10us(10)
+   while ! PIR1_SSPIF loop end loop
+
+   -- get Acknowledge Status Bit _ACKSTAT
+   -- 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
+   else
+      return false
+   end if
+end function
+--  
-----------------------------------------------------------------------------
+
+--  
-----------------------------------------------------------------------------
+-- i2c_receive_byte -
+--  
-----------------------------------------------------------------------------
+-- start receiving of a byte and waits till finished
+-- if param ACK is true, the byte is acknowledged and next bytes can be  
received.
+-- if param ACK is false, the byte is nacked and a stop *should be* sent.
+-- note: this behavior is inverted from the line level and orignal  
library, but
+--       consistent with the i2c_software library.
+--  
-----------------------------------------------------------------------------
+function i2c_receive_byte(bit in ACK ) return byte is
+   var byte data
+   -- start receive cyclus and wait till full byte received
+   SSPCON2_RCEN = high
+   ; delay_10us(10)
+   while SSPSTAT_BF == low loop  end loop
+
+   -- send Acknowledge (=low) if STOP=true=high
+   -- and wait till finishd
+   SSPCON2_ACKDT = ! ACK
+   SSPCON2_ACKEN = high
+   while SSPCON2_ACKEN == high loop end loop
+
+   -- get read data
+   data = sspbuf
+   ;serial_hw_write(data)
+
+   ;  if ACK == i2c_NACK then _i2c_stopbit end if
+   return data
+end function
+--  
-----------------------------------------------------------------------------
+
+
+
+
+

Modified: trunk/unvalidated/sample/test/peripheral/i2c/i2c_test_l0.jal
==============================================================================
--- trunk/unvalidated/sample/test/peripheral/i2c/i2c_test_l0.jal        
(original)
+++ trunk/unvalidated/sample/test/peripheral/i2c/i2c_test_l0.jal        Sun Sep 
28  
02:21:24 2008
@@ -19,9 +19,10 @@
  include delay_any_mc

  -- i2c setup
-const word i2c_bus_speed = 4 ; * 100kHz
-include i2c_software
-; include i2c_hardware
+const word _i2c_bus_speed = 1 ; * 100kHz
+const bit _i2c_level = true   ; i2c levels (not SMB)
+;include i2c_software
+include i2c_hardware
  i2c_initialize()

  -- we're alive
@@ -56,13 +57,13 @@

     i2c_stop()

+   -- 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
     i2c_stop()
-

     print_crlf(serial_hw_data)


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to