]> rtime.felk.cvut.cz Git - mirosot.git/blob - testapp/mirosot_main.c
USE_EXR_LEVELS must be used globally for the whole tree.
[mirosot.git] / testapp / mirosot_main.c
1 /*
2 *  C Implementation: main_test
3 *
4 * Description:
5
6 *
7 *
8 * Author: Petr Kovacik <kovacp1@feld.cvut.cz>, (C) 2006
9 *
10 * Copyright:
11 *
12 */
13 #include <types.h>
14 #include <cpu_def.h>
15 #include <h8s2638h.h>
16 #include <periph/sci_rs232.h>
17 #include <system_def.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <pxmc.h>
22 #include <pxmc_h2638.h>
23
24 #include <bth_fce_out.h>
25 #include <bth_inface.h>
26
27 #include <cmd_proc.h>
28 #include "cmd_bth.h"
29 #include "cmd_pxmc.h"
30 #include <bth_h8s2638.h>
31
32 /* SCI channel where Bluetooth is connected */
33 #define SCI_CHAN_BTH 2
34
35 /*struktury prikazu cmd*/
36 cmd_des_t const cmd_des_help={0, 0,"HELP","prints help for commands",
37                         cmd_do_help,{(char*)&cmd_bth}};
38
39 cmd_des_t const *cmd_rs232_default[]={
40
41   &cmd_des_help,
42   (cmd_des_t*)1,
43   (cmd_des_t*)cmd_stm_default,
44   NULL
45 };
46 cmd_des_t const **cmd_bth=cmd_rs232_default;       /*cmd prikazy pro bth*/
47 cmd_des_t const **cmd_rs232=cmd_rs232_default;     /*cmd prikazy pro PC*/
48
49
50 /*struktury charakterizujici motor 0*/
51 pxmc_state_t mcsX0={
52   pxms_flg:0,
53   pxms_do_inp:0,
54   pxms_do_con:0,
55   pxms_do_out:0,
56   pxms_do_deb:0,
57   pxms_do_gen:0,
58   pxms_ap:0, pxms_as:0,
59   pxms_rp:155l*256,
60   pxms_rs:0, //pxms_subdiv:8,
61   pxms_md:8000l<<8, pxms_ms:5000, pxms_ma:10,
62   pxms_inp_info:(long)TPU_TCNT1, //TPU_TCNT1                    /*chanel TPU A,B*/
63   pxms_out_info:(long)PWM_PWBFR1A,                      /*chanel PWM A,B*/
64   pxms_ene:0, pxms_erc:0,
65   pxms_p:40, pxms_i:0, pxms_d:1, pxms_s1:0, pxms_s2:0,
66   pxms_me:0x1800, //6144
67   pxms_ptirc:40, // 2000 irc per rev, 200/4 steps /
68   pxms_ptper:1,
69   pxms_ptptr1:NULL,
70   pxms_ptptr2:NULL,
71   pxms_cfg:PXMS_CFG_MD2E_m|PXMS_CFG_HLS_m|
72       PXMS_CFG_HPS_m|PXMS_CFG_HDIR_m|0x1
73 };
74
75
76 /*struktury charakterizujici motor 1*/
77 pxmc_state_t mcsX1={
78   pxms_flg:0,
79   pxms_do_inp:0,
80   pxms_do_con:0,
81   pxms_do_out:0,
82   pxms_do_deb:0,
83   pxms_do_gen:0,
84   pxms_ap:0, pxms_as:0,
85   pxms_rp:155l*256,
86   pxms_rs:0, //pxms_subdiv:8,
87   pxms_md:8000l<<8, pxms_ms:5000, pxms_ma:10,
88   pxms_inp_info:(long)TPU_TCNT2,                        /*chanel TPU C,D*/
89   pxms_out_info:(long)PWM_PWBFR1C,                      /*chanel PWM C,D*/
90   pxms_ene:0, pxms_erc:0,
91   pxms_p:40, pxms_i:0, pxms_d:1, pxms_s1:0, pxms_s2:0,
92   pxms_me:0x1800, //6144
93   pxms_ptirc:40, // 2000 irc per rev, 200/4 steps /
94   pxms_ptper:1,
95   pxms_ptptr1:NULL,
96   pxms_ptptr2:NULL,
97   pxms_cfg:PXMS_CFG_MD2E_m|PXMS_CFG_HLS_m|      //FIXME: nastavit spravne priznaky pro dalsi motorove struktur
98       PXMS_CFG_HPS_m|PXMS_CFG_HDIR_m|0x1
99 };
100
101 pxmc_state_t *pxmc_main_arr[] = {&mcsX0,&mcsX1};
102
103 pxmc_state_list_t pxmc_main_list = {
104   pxml_arr:pxmc_main_arr,
105   pxml_cnt:sizeof(pxmc_main_arr)/sizeof(pxmc_main_arr[0])
106 };
107
108
109
110 //*******************************************************
111
112 void  unhandled_exception(void) __attribute__ ((interrupt_handler));
113
114 /*Interrupt routines*/
115 void  unhandled_exception(void)
116 {
117 };
118 //********************************************************
119
120
121 int cmd_rs232_processor_run(void)
122 {
123   int val;
124   cmd_io_t* cmd_io;
125
126   cmd_io=&cmd_io_rs232;
127   if(cmd_rs232_line_out(cmd_io))
128     return 1;
129
130   if(cmd_rs232_line_in(cmd_io)<=0)
131     return 0;
132
133   if(cmd_rs232){
134     val=proc_cmd_line(cmd_io, cmd_rs232, cmd_io->priv.ed_line.in->buf);
135   }else{
136     val=-CMDERR_BADCMD;
137   }
138
139   if(cmd_io->priv.ed_line.out->inbuf){
140     cmd_io_putc(cmd_io,'\r');
141     cmd_io_putc(cmd_io,'\n');
142
143   }else if(val<0){
144     char s[20];
145     cmd_io_write(&cmd_io_rs232,"ERROR ",6);
146     i2str(s,-val,0,0);
147     cmd_io_write(cmd_io,s,strlen(s));
148     cmd_io_putc(cmd_io,'\r');
149     cmd_io_putc(cmd_io,'\n');
150   }
151   return 1;
152 }
153
154
155
156 int main()
157 {
158   int zn_bth;
159
160   /********************************************************************************/
161   *DIO_PJDDR=0xff;      /*output gate*/
162   *DIO_PEDDR=0xff;      /*output gate*/
163   *DIO_PEDR=0x60;       /*0x0-LED - light all; 0x6 -ENA,ENB=1, LE33CD=0*/
164   *DIO_PJDR=0x00;       //switch on all the LEDs - TODO: Use generic macros fot this
165
166
167   /*priority preruseni - SCI > TPU*/
168   *SYS_SYSCR|=SYSCR_INTM1m;
169   *INT_IPRA=0x22; *INT_IPRB=0x22; *INT_IPRC=0x04;
170   *INT_IPRD=0x40; *INT_IPRE=0x44; *INT_IPRF=0x55;
171   *INT_IPRG=0x55; *INT_IPRH=0x55; *INT_IPRJ=0x06;
172   *INT_IPRK=0x67; *INT_IPRM=0x66;
173
174   /* disable and initialize interrupts...*/
175   cli();
176   excptvec_initfill(unhandled_exception, 0);
177
178   /*nastaveni seriovych linek - Bth, PC*/
179   sci_rs232_setmode(RS232_BAUD_RAW | 3, 0, 0, SCI_CHAN_BTH);    // HCI - hardcoded 115200
180   //sci_rs232_setmode(115200, 0, 0, 2); // HCI
181   sci_rs232_setmode(19200, 0, 0, sci_rs232_chan_default); //PC
182   sti();                        /* enable interrupts */
183
184   bth_init();  /* bluetooth initialization */
185
186   /*inicializace komunikacnich datovych I/O bufferu */
187   bth_inface_setup(0);
188
189   /*TPU channel 1 - inicialize (kontrola paketu + pocatecni zpozdeni)*/
190   bth_init_pkt_controll();
191
192   /*nastaveni zarizeni Bth do slave role*/
193   bth_parametr_slave();
194
195   /*kratka cas. pouza pro bth zarizeni - mazani zasilanych dat*/
196
197 //  bth_start_TPU_counter();
198   do {
199     zn_bth=sci_rs232_recch(SCI_CHAN_BTH);
200   } while(bth_get_timer()<11);
201
202   bth_start();
203
204   /* HW setup (TPU, PWM)*/
205   pxmc_set_pwm_tpu();
206
207   /*nastaveni DC motoru*/
208   pxmc_add_pservice_and_mode(4); /*Macro -  mod=4 tj. all motors are DC*/
209
210   /*nekonecna smycka obsluhujici bth, pc ...*/
211   do {
212     zn_bth=sci_rs232_recch(SCI_CHAN_BTH);
213     if(zn_bth!=-1)
214     {
215       bth_recieve_packet(zn_bth);
216     };
217
218     l2cap_send_data(0, 0);
219     bth_send_queue();           /* send built packets if there are any */
220     cmd_bth_processor_run();    /* run command processor on bluetooth */
221     cmd_rs232_processor_run();  /* run command processor on serial line */
222   } while(1);
223   return 0;
224 };
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 #if 0   //male pohybove demo - pohyb robota
245 //#include "timer.h"
246
247 //  init_timer3();
248
249 //   long int before = -1;
250 //   int a=1;
251   do{
252 /*    long int now = get_timer();
253
254   if (now != before)
255   switch (now % 10) {
256     case 0:
257   pxmc_spd(&mcsX0,a*400,0);
258   pxmc_spd(&mcsX1,-a*700,0);
259   break;
260     case 1:
261   pxmc_spd(&mcsX0,a*700,0);
262   pxmc_spd(&mcsX1,-a*400,0);
263   break;
264     case 2:
265   pxmc_spd(&mcsX0,a*2000,0);
266   pxmc_spd(&mcsX1,-a*1200,0);
267   break;
268     case 3:
269   pxmc_spd(&mcsX0,a*1200,0);
270   pxmc_spd(&mcsX1,-a*2000,0);
271   break;
272     case 4:
273   pxmc_spd(&mcsX0,a*1300,0);
274   pxmc_spd(&mcsX1,-a*1000,0);
275   break;
276     case 5:
277   pxmc_spd(&mcsX0,a*1300,0);
278   pxmc_spd(&mcsX1,-a*1000,0);
279   break;
280     case 6:
281   pxmc_spd(&mcsX0,a*100,0);
282   pxmc_spd(&mcsX1,-a*300,0);
283   break;
284     case 7:
285   pxmc_spd(&mcsX0,a*300,0);
286   pxmc_spd(&mcsX1,-a*100,0);
287   break;
288     case 8:
289   pxmc_spd(&mcsX0,a*2000,0);
290   pxmc_spd(&mcsX1,-a*100,0);
291   break;
292     case 9:
293   pxmc_spd(&mcsX0,a*4000,0);
294   pxmc_spd(&mcsX1,-a*4000,0);
295   if(a>0) a=-1;
296   else a=1;
297   break;
298           
299   }
300     */
301 #endif