My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- Title: Test program for i2c hardware slave, stateful implementation
-- Author: Sebastien Lelong, Copyright (c) 2008-2009, all rights reserved.
-- Adapted-by: Joep Suijs
-- Compiler: >=2.4m
-- Revision: $Revision$
--
-- 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,
-- based in i2c_hw_slave_isr.jal library. Basically, this i2c slave waits for
-- a byte, and when required, it sends byte + 1 (send "a", you'll get a "b")
-- --
-- This sample is aimed to be used with *_i2c_hw_master_echo.jal samples,
-- which is the master part.
--
--
--
-- This file has been generated by jallib.py from:
-- * board: board_16f88_js.jal
-- * test : test_i2c_hw_slave_echo.jal
--

;@jallib section chipdef
-- chip setup
include 16f88

;--
;-- This setup assumes a 20 MHz resonator or crystal
;-- is connected to pins OSC1 and OSC2.
pragma target OSC HS -- HS crystal or resonator
pragma target clock 20_000_000 -- oscillator frequency
pragma target WDT disabled -- no watchdog
pragma target LVP disabled -- no low-voltage programming
pragma target CCP1MUX pin_B3 -- ccp1 pin on B3

--
-- This setup uses the internal oscillator
;pragma target OSC INTOSC_NOCLKOUT -- HS crystal or resonator
;pragma target clock 8_000_000 -- oscillator frequency
;pragma target WDT disabled -- no watchdog
;pragma target LVP disabled -- no low-voltage programming
;pragma target CCP1MUX pin_B3 -- ccp1 pin on B3
;OSCCON_IRCF = 7 -- set prescaler to 1 (8 MHz)

;@jallib section led
-- LED IO definition
alias led is pin_b3
alias led_direction is pin_b3_direction
alias led2 is pin_b1
alias led2_direction is pin_b1_direction
led_direction = output

include i2c_hw_slave
-- 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)

-- will store what to send back to master
-- so if we get "a", we need to store "a" + 1
var byte data


-- Before including i2c_hw_slave_isr library, several callbacks
-- must be defined (callbacks are procedure which supposed to be defined
-- and be called on appriopriate time)

-- Since all those callbacks are called once per state (no other calls
-- in the program), it is suggested to use "pragma inline", to save
-- stack usage


-- 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
-- _trash is read from master, but it's a dummy data
-- usually (always ?) ignored
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
-- ultimate data processing... :)
data = rcv + 1
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
i2c_hw_slave_write_i2c(data)
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
-- This shouldn't occur in our i2c echo example
i2c_hw_slave_on_error()
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

-- blink a little to tell the world we're up
for 4 loop
led = on
_usec_delay(100000)
led = off
_usec_delay(100000)
end loop

-- just loop until interrupt is raised
forever loop
end loop

Change log

r3043 by jsuijs on Apr 22, 2012   Diff
regenerated samples with 'generated by
jallib.py' to detect outdated samples.
Go to: 
Project members, sign in to write a code review

Older revisions

r2811 by jsuijs on Oct 15, 2011   Diff
updated libs
r2760 by sebastien.lelong on Jul 31, 2011   Diff
added Revision: field + svn:keywords
Revision property on all existing
*.jal files. Also add to normalize EOL
chars to avoid mixed ones
r1826 by jsuijs on Mar 22, 2010   Diff
update compiler version note in
samples
All revisions of this file

File info

Size: 4665 bytes, 147 lines

File properties

svn:keywords
Revision
Powered by Google Project Hosting