]> rtime.felk.cvut.cz Git - mirosot.git/blobdiff - testapp/mirosot_main.c
An unsucesfull attempt to clean bluetooth library and make it working.
[mirosot.git] / testapp / mirosot_main.c
index 735132c1b6f01ab27271e3b584730fd00f1eaf11..25e96150d5bc7787f71fa2a970f113cd7a7dcb1e 100644 (file)
@@ -7,7 +7,7 @@
 *
 * Author: Petr Kovacik <kovacp1@feld.cvut.cz>, (C) 2006
 *
-* Copyright: 
+* Copyright:
 *
 */
 #define _USE_EXR_LEVELS 1
 #include "cmd_pxmc.h"
 #include <bth_h8s2638.h>
 
+/* SCI channel where Bluetooth is connected */
+#define SCI_CHAN_BTH 2
 
 /*struktury prikazu cmd*/
 cmd_des_t const cmd_des_help={0, 0,"HELP","prints help for commands",
                         cmd_do_help,{(char*)&cmd_bth}};
-                        
+
 cmd_des_t const *cmd_rs232_default[]={
 
   &cmd_des_help,
@@ -58,7 +60,7 @@ pxmc_state_t mcsX0={
   pxms_rp:155l*256,
   pxms_rs:0, //pxms_subdiv:8,
   pxms_md:8000l<<8, pxms_ms:5000, pxms_ma:10,
-  pxms_inp_info:(long)TPU_TCNT1,//TPU_TCNT1                    /*chanel TPU A,B*/
+  pxms_inp_info:(long)TPU_TCNT1, //TPU_TCNT1                   /*chanel TPU A,B*/
   pxms_out_info:(long)PWM_PWBFR1A,                     /*chanel PWM A,B*/
   pxms_ene:0, pxms_erc:0,
   pxms_p:40, pxms_i:0, pxms_d:1, pxms_s1:0, pxms_s2:0,
@@ -99,11 +101,9 @@ pxmc_state_t mcsX1={
 
 pxmc_state_t *pxmc_main_arr[] = {&mcsX0,&mcsX1};
 
-#define SUMMOTORS  (sizeof(pxmc_main_arr)/sizeof(pxmc_main_arr[0]))
-
 pxmc_state_list_t pxmc_main_list = {
   pxml_arr:pxmc_main_arr,
-  pxml_cnt:SUMMOTORS
+  pxml_cnt:sizeof(pxmc_main_arr)/sizeof(pxmc_main_arr[0])
 };
 
 
@@ -111,18 +111,6 @@ pxmc_state_list_t pxmc_main_list = {
 //*******************************************************
 
 void  unhandled_exception(void) __attribute__ ((interrupt_handler));
-/**
- * init - shaddow registers, outputs..
- *
- * Initializes P1 and P3 shaddow registers,
- * sets PJ.1, PJ.2, PJ.3 LED as outputs,
- * initialises interrupt vector.
- */
-void init()
-{
-  /* initialises interrupt vector */
-  excptvec_initfill(unhandled_exception, 0);
-}
 
 /*Interrupt routines*/
 void  unhandled_exception(void)
@@ -135,11 +123,11 @@ int cmd_rs232_processor_run(void)
 {
   int val;
   cmd_io_t* cmd_io;
-   
+
   cmd_io=&cmd_io_rs232;
   if(cmd_rs232_line_out(cmd_io))
     return 1;
-       
+
   if(cmd_rs232_line_in(cmd_io)<=0)
     return 0;
 
@@ -149,47 +137,13 @@ int cmd_rs232_processor_run(void)
     val=-CMDERR_BADCMD;
   }
 
-  if(cmd_io->priv.ed_line.out->inbuf){
-    cmd_io_putc(cmd_io,'\r');
-    cmd_io_putc(cmd_io,'\n');
-    
-  }else if(val<0){
-    char s[20];
-    cmd_io_write(&cmd_io_rs232,"ERROR ",6);
-    i2str(s,-val,0,0);
-    cmd_io_write(cmd_io,s,strlen(s));
-    cmd_io_putc(cmd_io,'\r');
-    cmd_io_putc(cmd_io,'\n');
-  }
-  return 1;
-}
-
-
-int cmd_bth_processor_run(void)
-{
-  int val;
-  cmd_io_t* cmd_io;
-
-  cmd_io=&cmd_io_bth;
-  if(cmd_bth_line_out(cmd_io))
-    return 1;
-
-  if(cmd_bth_line_in(cmd_io)<=0)
-    return 0;
-
-  if(cmd_bth){
-    val=proc_cmd_line(cmd_io, cmd_bth, cmd_io->priv.ed_line.in->buf);
-  }else{
-    val=-CMDERR_BADCMD;
-  }
-
   if(cmd_io->priv.ed_line.out->inbuf){
     cmd_io_putc(cmd_io,'\r');
     cmd_io_putc(cmd_io,'\n');
 
   }else if(val<0){
     char s[20];
-    cmd_io_write(&cmd_io_bth,"ERROR ",6);
+    cmd_io_write(&cmd_io_rs232,"ERROR ",6);
     i2str(s,-val,0,0);
     cmd_io_write(cmd_io,s,strlen(s));
     cmd_io_putc(cmd_io,'\r');
@@ -208,33 +162,32 @@ int main()
   *DIO_PJDDR=0xff;     /*output gate*/
   *DIO_PEDDR=0xff;     /*output gate*/
   *DIO_PEDR=0x60;      /*0x0-LED - light all; 0x6 -ENA,ENB=1, LE33CD=0*/
-  *DIO_PJDR=0x00;      //rozsviceni vsech diod na */
-  
+  *DIO_PJDR=0x00;      //switch on all the LEDs - TODO: Use generic macros fot this
+
 
   /*priority preruseni - SCI > TPU*/
   *SYS_SYSCR|=SYSCR_INTM1m;
-  *INT_IPRA=0x22; *INT_IPRB=0x22; *INT_IPRC=0x04; 
+  *INT_IPRA=0x22; *INT_IPRB=0x22; *INT_IPRC=0x04;
   *INT_IPRD=0x40; *INT_IPRE=0x44; *INT_IPRF=0x55;
   *INT_IPRG=0x55; *INT_IPRH=0x55; *INT_IPRJ=0x06;
   *INT_IPRK=0x67; *INT_IPRM=0x66;
 
-  /*povoleni vsech preruseni atd...*/
+  /* disable and initialize interrupts...*/
   cli();
-  init();
+  excptvec_initfill(unhandled_exception, 0);
 
   /*nastaveni seriovych linek - Bth, PC*/
-  sci_rs232_setmode(RS232_BAUD_RAW | 3, 0, 0, 2);      // HCI - hardcoded 115200
+  sci_rs232_setmode(RS232_BAUD_RAW | 3, 0, 0, SCI_CHAN_BTH);   // HCI - hardcoded 115200
   //sci_rs232_setmode(115200, 0, 0, 2);        // HCI
   sci_rs232_setmode(19200, 0, 0, sci_rs232_chan_default); //PC
-  sti();
+  sti();                        /* enable interrupts */
 
-  /*inicializace bluetooth*/
-  bth_init();
+  bth_init();  /* bluetooth initialization */
 
   /*inicializace komunikacnich datovych I/O bufferu */
   bth_inface_setup(0);
 
-  /*TPU kanal 1 - inicialize (kontrola paketu + pocatecni zpozdeni)*/
+  /*TPU channel 1 - inicialize (kontrola paketu + pocatecni zpozdeni)*/
   bth_init_pkt_controll();
 
   /*nastaveni zarizeni Bth do slave role*/
@@ -243,32 +196,31 @@ int main()
   /*kratka cas. pouza pro bth zarizeni - mazani zasilanych dat*/
 
 //  bth_start_TPU_counter();
-  do
-  {
-    zn_bth=sci_rs232_recch(2);
-  }while(bth_get_timer()<11);
+  do {
+    zn_bth=sci_rs232_recch(SCI_CHAN_BTH);
+  } while(bth_get_timer()<11);
+
   bth_start();
 
-  /*nastaveni HW (TPU, PWM)*/
-  pxmc_set_pwm_tpu(SUMMOTORS);
+  /* HW setup (TPU, PWM)*/
+  pxmc_set_pwm_tpu();
 
   /*nastaveni DC motoru*/
   pxmc_add_pservice_and_mode(4); /*Macro -  mod=4 tj. all motors are DC*/
 
   /*nekonecna smycka obsluhujici bth, pc ...*/
-  do{
-    zn_bth=sci_rs232_recch(2);
+  do {
+    zn_bth=sci_rs232_recch(SCI_CHAN_BTH);
     if(zn_bth!=-1)
     {
       bth_recieve_packet(zn_bth);
     };
 
     l2cap_send_data(0, 0);
-    bth_send_queue();           /*odesli sestavene pakety, pokod jsou*/
-    cmd_bth_processor_run();    /*sber + odesilani cmd prikazu bth*/
-    cmd_rs232_processor_run();  /*sber + odesilani cmd prikazu PC*/
-    
-  }while(1);
+    bth_send_queue();           /* send built packets if there are any */
+    cmd_bth_processor_run();    /* run command processor on bluetooth */
+    cmd_rs232_processor_run();  /* run command processor on serial line */
+  } while(1);
   return 0;
 };