2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: mf2_server.c,v 1.1.2.5 2007-05-23 17:18:11 zer0 Exp $
22 /* Olivier MATZ, Droids-corp 2007 */
27 #include <aversive/wait.h>
29 #include <mf2_server.h>
30 #include <mf2_server_config.h>
33 #define data_Z() do { cbi(DDR(MF2_SERVER_DATA_PORT), MF2_SERVER_DATA_BIT); } while(0)
34 #define data_0() do { sbi(DDR(MF2_SERVER_DATA_PORT), MF2_SERVER_DATA_BIT); } while(0)
35 #define read_data() (bit_is_set(PIN(MF2_SERVER_DATA_PORT), MF2_SERVER_DATA_BIT))
36 #define data_is_Z() (!bit_is_set(DDR(MF2_SERVER_DATA_PORT), MF2_SERVER_DATA_BIT))
38 #define clk_Z() do { cbi(DDR(MF2_SERVER_CLK_PORT), MF2_SERVER_CLK_BIT); } while(0)
39 #define clk_0() do { sbi(DDR(MF2_SERVER_CLK_PORT), MF2_SERVER_CLK_BIT); } while(0)
40 #define read_clk() (bit_is_set(PIN(MF2_SERVER_CLK_PORT), MF2_SERVER_CLK_BIT))
41 #define clk_is_Z() (!bit_is_set(DDR(MF2_SERVER_CLK_PORT), MF2_SERVER_CLK_BIT))
43 #define MF2_SERVER_STATE_READY 0
44 #define MF2_SERVER_STATE_SEND 1
45 #define MF2_SERVER_STATE_RECV 2
47 static volatile uint8_t mf2_state=0;
49 typedef void (event)(char);
50 static event * tx_event = NULL;
51 static event * rx_event = NULL;
52 static volatile uint8_t mf2_step=0;
53 static volatile uint8_t mf2_parity_cpt=0;
54 static volatile char mf2_data_send=0;
55 static volatile char mf2_data_recv=0;
58 #define WAIT_KBD_CYCLE 600
59 #define WAIT_KBD_CYCLE4 WAIT_KBD_CYCLE/4
71 /* wait_4cyc(WAIT_KBD_CYCLE4); */
72 /* wait_4cyc(WAIT_KBD_CYCLE4); */
73 /* wait_4cyc(WAIT_KBD_CYCLE4); */
75 wait_4cyc(WAIT_KBD_CYCLE4);
78 for (i=0; i<8 ; i++) {
82 wait_4cyc(WAIT_KBD_CYCLE4);
86 wait_4cyc(WAIT_KBD_CYCLE4);
91 wait_4cyc(WAIT_KBD_CYCLE4);
93 wait_4cyc(WAIT_KBD_CYCLE4);
98 wait_4cyc(WAIT_KBD_CYCLE4);
100 wait_4cyc(WAIT_KBD_CYCLE4);
105 wait_4cyc(WAIT_KBD_CYCLE4);
107 wait_4cyc(WAIT_KBD_CYCLE4);
112 wait_4cyc(2*WAIT_KBD_CYCLE4);
116 static inline int8_t mf2_server_bus_free(void)
118 return read_clk() && read_data();
122 int8_t mf2_server_ready(void)
124 return (mf2_state==MF2_SERVER_STATE_READY && mf2_server_bus_free());
131 static inline void dump(void)
147 #define dump() do {} while(0)
150 void mf2_server_timer_cb(void)
152 /* static uint16_t i=0; */
155 /* if it is just polling */
156 if (mf2_state == MF2_SERVER_STATE_READY) {
160 /* the central server has something to say */
161 if (read_clk() && !read_data()) {
162 mf2_state = MF2_SERVER_STATE_RECV;
164 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb, timer1_get()+MF2_SERVER_CLK_HALF_PERIOD);
169 /* nothing to do if the central server has nothing to say */
172 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb, timer1_get()+MF2_SERVER_READ_POLL_PERIOD);
177 /* an operation is running */
180 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb, timer1_get()+MF2_SERVER_CLK_HALF_PERIOD);
182 /* XXX we should check if clk is 0 when clk_Z() */
183 if (mf2_state == MF2_SERVER_STATE_RECV) {
207 mf2_data_recv |= (1 << ( (mf2_step-3)/2 ) );
217 /* printf("%d\n", timer1_get() - t); */
245 /* c=check_rx_buf(rx_buf); */
246 rx_event((char)(mf2_data_recv));
247 /* rx_event((char)(0xED)); */
249 mf2_state = MF2_SERVER_STATE_READY;
251 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb, timer1_get()+MF2_SERVER_READ_POLL_PERIOD);
272 if(mf2_data_send & (1<<((mf2_step-3)/2))) {
291 /* /\* recv(); *\/ */
292 /* printf("Ceci est un test\n"); */
293 /* printf("%d\n", mf2_step); */
294 /* printf("%2.2x\n", mf2_data_send); */
297 mf2_state = MF2_SERVER_STATE_RECV;
304 if(!(mf2_parity_cpt%2))
327 mf2_state = MF2_SERVER_STATE_READY;
329 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb, timer1_get()+MF2_SERVER_READ_POLL_PERIOD);
338 int8_t mf2_server_send(char c)
344 if (!mf2_server_ready()) {
350 mf2_state = MF2_SERVER_STATE_SEND;
354 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb,
355 timer1_get()+MF2_SERVER_CLK_HALF_PERIOD);
362 void mf2_server_init(void)
364 cbi(MF2_SERVER_DATA_PORT, MF2_SERVER_DATA_BIT);
365 cbi(MF2_SERVER_CLK_PORT, MF2_SERVER_CLK_BIT);
367 timer1A_register_OC_intr_at_tics(mf2_server_timer_cb,
368 timer1_get()+MF2_SERVER_READ_POLL_PERIOD);
370 /* XXX choose timer */
375 /* This function is used to register another function which will be */
376 /* executed at each byte transmission. */
377 void mf2_server_register_tx_event(void (*f)(char))
385 /* This function is used to register another function which will be */
386 /* executed at each byte reception. */
387 void mf2_server_register_rx_event(void (*f)(char))