Statistics
| Revision:

root / trunk / tp_info6 / bsp / periph.c @ 2

History | View | Annotate | Download (4.07 KB)

1
#ifdef M32C87
2
        #include "sfr32c87.h"
3
#endif
4

    
5
#ifdef M32C83
6
        #include "sfr32c83.h"
7
#endif
8

    
9

    
10
#include <itron.h> 
11
#include <kernel.h> 
12
#include "kernel_id.h" 
13
#include "periph.h"
14
#include "uart0.h"
15
#include "dma0.h";
16

    
17

    
18

    
19
Tperiph periph[26];
20

    
21
#define eot 0x0D
22

    
23
void periph_init (void)
24
{
25
int i;
26
        uart0_init();
27
    dma0_init();
28
        for (i=0;i<26;i++)
29
                periph[i].maj=periph[i].val=periph[i].ev=0;
30
                
31
}
32

    
33
void periph_nom(char * nom){
34
char c;
35
int i=0;
36
        c=*nom;
37
        do{
38
                periph_write('I',c);
39
                dly_tsk(0);
40
                i++;
41
                if (i>=8) break;
42
        }while (c=*(++nom));
43
        periph_write('I',0);
44
        dly_tsk(0);
45
}
46

    
47
int periph_read (unsigned char a,int * val)
48
{
49
int i;
50
/*        addr &= ~0x20;
51
        addr = addr -'A';
52
        if (addr>26)
53
                return 0;
54
*/        
55
        if (!periph[ADDR(a)].maj){
56
                periph_inter(a);
57
        //        dly_tsk(10);
58
        }
59
        if (periph[ADDR(a)].maj) {
60
                periph[ADDR(a)].maj=0;
61
                *val = periph[ADDR(a)].val;
62
                return 1;
63
        }
64
        *val = periph[ADDR(a)].val;
65
        return 0;
66
}
67

    
68
void periph_write(char addr,int valeur)
69
{
70
CanFrame comm; 
71
        comm.data.id=addr; comm.data.rtr=':'; comm.data.val=valeur;
72
        snd_dtq (CanTx, (VP_INT) comm.msg);
73
}
74

    
75
void iperiph_write(char addr,int valeur)
76
{
77
CanFrame comm; 
78
        comm.data.id=addr; comm.data.rtr=':'; comm.data.val=valeur;
79
        ipsnd_dtq (CanTx, (VP_INT) comm.msg);
80
}
81

    
82
void periph_inter(char addr)
83
{
84
CanFrame comm;
85
        comm.data.id=addr; comm.data.rtr='?';
86
        snd_dtq (CanTx,(VP_INT) comm.msg);
87
}
88

    
89
void iperiph_inter(char addr)
90
{
91
CanFrame comm;
92
        comm.data.id=addr; comm.data.rtr='?';
93
        ipsnd_dtq (CanTx,(VP_INT) comm.msg);
94
}
95

    
96
void periph_even(char addr)
97
{
98
CanFrame comm;
99
        comm.data.id=addr; comm.data.rtr='!';
100
        snd_dtq (CanTx,(VP_INT) comm.msg);        
101
}
102

    
103
void iperiph_even(char addr)
104
{
105
CanFrame comm;
106
        comm.data.id=addr; comm.data.rtr='!';
107
        ipsnd_dtq (CanTx,(VP_INT) comm.msg);        
108
}
109

    
110
static int hex_2_int (char * str)
111
{
112
        int i;
113
        long val;
114
        val=0;
115
        for (i=0;i<4;i++)
116
        {
117
                val=(val*16);
118
                val+=(str[i]>='A')?(int) str[i]-'A'+10:(int)str[i]-'0';
119
        }
120
        if (val>0x7FFF) val=val-0x10000;
121
        return val;
122
}
123

    
124
static void int_2_hex(unsigned int val, char * str)
125
{
126
        int i;
127
        char c;
128
        
129
        for (i=3;i>=0;i--)
130
        {
131
                c = val & 0x0F;
132
                c+='0';
133
                if (c>'9') c+='A'-'9'-1;
134
                str[i]=c;
135
                val=val>>4;
136
        }
137
}
138

    
139
void periph_rx(VP_INT foo)
140
{
141
char c;
142
unsigned int valeur;
143
CanFrame msg;
144

    
145
        while (1)
146
        {
147
                if (rcv_dtq (CanRx,(VP_INT *) &msg)==E_OK)
148
                {
149
                        c=msg.data.id;
150
                        if (c=='!'){
151
                                set_flg(event,msg.data.val);
152
                                continue;
153
                        }
154
                        if (c<'A' || c>'z')
155
                                continue;        
156
                        c=ADDR(msg.data.id);
157
                
158
                        if (msg.data.rtr=='!'){
159
                                periph[c].val=1;
160
                                periph[c].maj++;
161
                                if (periph[c].ev)
162
                                        set_flg(ev_periph,periph[c].ev);
163
                                continue;
164
                        }
165

    
166
                        if (msg.data.rtr==':'){
167
                                periph[c].val=msg.data.val;
168
                                periph[c].maj++;
169
                                if (periph[c].ev)
170
                                        set_flg(ev_periph,periph[c].ev);                                
171
                                continue;
172
                        }
173
                }
174
        }
175
}
176

    
177
void periph_tx(VP_INT foo)                                     // test emission trame formatee
178
{
179
CanFrame rec;
180
char buf_em[8];
181
UINT flag;
182
char valid;
183

    
184
        while(1)
185
        {
186
                  if (rcv_dtq (CanTx,(VP_INT *) &rec.msg)==E_OK)
187
        {
188
                        //buf_em[0]=eot; //0x0A??
189
                        buf_em[0]=rec.data.id;
190
                        valid=0;
191
                        if (rec.data.rtr==':' || rec.data.rtr==0){
192
                                buf_em[1]=':';
193
                                int_2_hex (rec.data.val,&buf_em[2]);
194
                                buf_em[6]=eot; 
195
                                buf_em[7]=0;
196
                                valid=1;
197
                        }
198
                        if (rec.data.rtr=='?' || rec.data.rtr==1){
199
                                buf_em[1]='?';
200
                                buf_em[2]=eot;
201
                                buf_em[3]=0;
202
                                //buf_em[3]=buf_em[4]=buf_em[5]=buf_em[6]='-';
203
                                //buf_em[4]=0;
204
                                valid=1;
205
                        }        
206
                        if (!valid)
207
                                continue;        
208
                                                
209
                        uart0_itx(buf_em);                                                
210
/*                        uart0_str_dma(buf_em);   // declenche transfert
211
                        // Attention le debugger peu prendre la main n'importe quand et bloquer
212
                        // la detection de fin d'envoi de message
213
                        // Le timeout permet de debloquer la situation 
214

215
                        wai_flg(ev_bus_fin_tr,(FLGPTN) 0x0001,TWF_ANDW,&flag);
216

217
                        //twai_flg(ev_bus_fin_tr,(FLGPTN) 0x0001,TWF_ANDW,&flag,5);
218
                        //clr_flg( ID_ev_bus_fin_tr,(FLGPTN) 0); 
219
*/                        
220
                        wai_flg(ev_bus_fin_tr,(FLGPTN) 0x0001,TWF_ANDW,&flag);
221
        }                                            
222
        }
223
}
224

    
225