Author: jsuijs
Date: Sat Jan 24 08:11:18 2009
New Revision: 746
Added:
trunk/include/peripheral/i2c/i2c_hw_slave_msg.jal
trunk/sample/test/peripheral/i2c/test_i2c_hw_slave_msg.jal
Log:
code complete (ready for test) version of i2c msg interface
Added: trunk/include/peripheral/i2c/i2c_hw_slave_msg.jal
==============================================================================
--- (empty file)
+++ trunk/include/peripheral/i2c/i2c_hw_slave_msg.jal Sat Jan 24 08:11:18
2009
@@ -0,0 +1,127 @@
+-- Title: i2c hardware slave Message Interface
+-- Author: Sebastien Lelong, Joep Suijs, Copyright (c) 2008-2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4i
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: this library provides an message interface to i2c hardware
slave.
+-- It relies on the jalv2 implementation of Microchip Application Note
AN734.
+--
+-- Sources:
+-- - AN734:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011798
+--
+
+
+-- map sspcon1 to sspcon
+var volatile byte SSPCON1 is SSPCON
+var volatile bit SSPCON1_WCOL at SSPCON1 : 7
+var volatile bit SSPCON1_SSPOV at SSPCON1 : 6
+var volatile bit SSPCON1_SSPEN at SSPCON1 : 5
+var volatile bit SSPCON1_CKP at SSPCON1 : 4
+var volatile bit*4 SSPCON1_SSPM at SSPCON1 : 0
+
+include i2c_hw_slave
+
+-- vars
+var byte i2c_index -- i2c index (pointer)
+var byte i2c_datapresent -- datapresent flag
+var byte i2c_status -- status (of last user command)
+var byte i2c_buffer[I2C_BUFFER_SIZE]
+
+
+procedure i2c_call_process_message() is
+ pragma inline
+
+ -- let user process buffer
+ if (i2c_datapresent == 1) then
+ var byte temp = i2c_index;
+ i2c_index = 0;
+ i2c_process_message(temp)
+ i2c_index = 0
+ i2c_datapresent = 0
+ end if
+
+end procedure
+
+-- this callback is used when something wrong happened
+-- during communication between master and us
+procedure i2c_hw_slave_on_error() is
+ pragma inline
+ -- Just tell user user something's got wrong
+ forever loop
+ led = on
+ _usec_delay(200000)
+ led = off
+ _usec_delay(200000)
+ end loop
+end procedure
+
+
+-- this callback is used when master wants to talk to us
+-- and our i2c address has been recognized
+procedure i2c_hw_slave_on_state_1(byte in _trash) is
+ pragma inline
+ i2c_index = 0 -- set pointer to buffer start
+end procedure
+
+
+-- This callback is used when master sends a data byte
+procedure i2c_hw_slave_on_state_2(byte in rcv) is
+ pragma inline
+
+ i2c_datapresent = 1 -- Indicate we received data
+ -- prepare pointers
+ i2c_buffer[i2c_index] = rcv
+
+ if (i2c_index < I2C_BUFFER_SIZE) then
+ i2c_index = i2c_index + 1 -- point to next position
+ end if
+end procedure
+
+
+-- this callback is used when master wants to read something
+-- from us. It should use i2c_hw_slave_write() to send something
+procedure i2c_hw_slave_on_state_3() is
+ pragma inline
+
+ -- let user process buffer
+ i2c_call_process_message()
+
+ -- send first byte to master
+ i2c_index = 0 -- set pointer to buffer start
+ i2c_hw_slave_write_i2c(i2c_buffer[0]) -- send data
+ i2c_index = 1 -- point to next position
+
+end procedure
+
+
+-- this callback is used when master, after having read something,
+-- still wants to read and get data from us.
+procedure i2c_hw_slave_on_state_4() is
+ pragma inline
+
+ -- let user process buffer
+ i2c_call_process_message()
+
+ -- This shouldn't occur in our i2c echo example
+ i2c_hw_slave_write_i2c(i2c_buffer[i2c_index]) -- send data
+ i2c_index = i2c_index + 1 -- point to next position
+
+
+end procedure
+
+
+-- this callback is used when master does not want to talk
+-- with us anymore... This is an appropriate place to reset
+-- data for instance
+procedure i2c_hw_slave_on_state_5() is
+ pragma inline
+ -- data = 0
+end procedure
+
+
+-- callbacks are defined, now include ISR
+include i2c_hw_slave_isr
+
Added: trunk/sample/test/peripheral/i2c/test_i2c_hw_slave_msg.jal
==============================================================================
--- (empty file)
+++ trunk/sample/test/peripheral/i2c/test_i2c_hw_slave_msg.jal Sat Jan 24
08:11:18 2009
@@ -0,0 +1,76 @@
+-- Title: Test program for i2c hardware slave, message interface
implementation
+-- Author: Joep Suijs, Sebastien Lelong, Copyright (c) 2008-2009, all
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4i
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: this sample shows how to implements an i2c hardware slave,
+-- using the message interface lib. Basically, this i2c slave waits for
+-- a full message to arrive and then calls a user routine to process the
message and
+-- (optional) prepare a response.
+--
+
+
+;@jallib use chipdef
+
+;@jallib use led
+led_direction = output
+
+
+procedure i2c_process_message(byte in byte_count);
+
+const byte I2C_BUFFER_SIZE = 40
+include i2c_hw_slave_msg
+
+
+-- this will be the slave address. It looks like:
+-- 0b_0101_1100
+-- => 0b_0101_110 : 7-bits address
+-- => 0 : 8th bit is to specify read or write operation.
+-- Value can be anything, it does not matter while init
+const byte SLAVE_ADDRESS = 0x5C
+i2c_hw_slave_init(SLAVE_ADDRESS,false) -- no START/STOP interrupts
+
+
+
+
+procedure i2c_process_message(byte in byte_count) is
+ -- ---------------------
+ -- User code starts here
+ -- ---------------------
+ -- This code is called every time the PIC received a serie of bytes.
+ -- It takes care of execution of the user code exactly once.
+ if (i2c_buffer[0] == 0x80) then
+
+ if (byte_count == 1) then
+
+ -- cmd 0x80 - request version and status
+ i2c_buffer[0] = 0x3C -- application code
+ i2c_buffer[1] = 0x01 -- version
+ i2c_buffer[2] = i2c_status -- status
+
+ i2c_status = 0 -- succes
+ else
+ i2c_status = 1 -- invalid write length
+ end if
+
+ elsif (i2c_buffer[0] == 0x90) then
+ if (byte_count == 2) then
+ -- cmd 00x90 - set servo & request sensor data
+--// I2cBeep = i2c_buffer[1]
+ elsif (byte_count == 1) then
+--// i2c_buffer[0] = I2cOdoCounter1
+--// i2c_buffer[1] = I2cOdoCounter2
+ else
+ i2c_status = 1 -- invalid write length
+ end if
+
+ else
+ i2c_status = 2 -- unknown command
+ end if
+
+end procedure
+
+
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---