Exemplo n.º 1
0
void task_pi_listen(void) {
  static int received = 0;
  //static char tx_test[] = {"The quick brown fox jumps over the lazy dog!\r\n"};
  static unsigned char rx_pi_cmd[265];
  static char PI_HDR[] = "$$$";
  static char cmd[]="PITAKEIMG";
  static char msg[100];
  static char tmp[51];
  static int i=0;
  static int j;
  static int msg_length; // used for message length to/from Pi
  static BOOL PI_ON = FALSE; // flag used if Pi is on or off
  static BOOL RTS_FROM_PI = FALSE; // when Pi needs to talk to PIC
  static BOOL CTS_FROM_PI = FALSE; // when Pi needs to talk to PIC
 
  //TAKEPICPI = FALSE;
  
  dprintf("Starting task_pi_listen...\r\n");
  
  while(1) {
    while (!csk_uart0_count()) { OS_Delay(50); }
    dprintf("In task_pi_listen...\tOERR=%d\r\n",U1STAbits.OERR);

     // check for UART0 RX buffer Overflow Error and reset bit
     if (U1STAbits.OERR==1) {
         U1STAbits.OERR=0;
         csk_uart2_puts("Reset OERR in main while!\r\n");
       }

    i = 0;
    msg_length = 0;
    while(csk_uart0_count()) {
      rx_pi_cmd[i] = csk_uart0_getchar();
      if (i==4) { 
  // take the 2 hex array elements and convert to int
        sprintf(tmp,"%c%c",rx_pi_cmd[i-1],rx_pi_cmd[i]);
        msg_length = (int)strtol(tmp,NULL,16);
        //dprintf("msg_length type: %s",typeof(msg_length));
       // dprintf("i= %d  msg_length= %d %c %c",i,msg_length,rx_pi_cmd[i-1],rx_pi_cmd[i]*1);
      }
      i++;
      // check for max msg_length (cases where byte 3 isn't valid
      // OR bounds checking w/ i & msg_length
   //   if ((msg_length > 20) || (i > (msg_length+4))) { break; }
    } // end: while(csk_uart0_count())
    
    // Message from Pi header: check for "$$$"
    if (rx_pi_cmd[0]=='$' && rx_pi_cmd[1]=='$' && rx_pi_cmd[2]=='$'){
      if (!(OSReadBinSem(BINSEM_PI_ISON)) && (rx_pi_cmd[5]=='P' && rx_pi_cmd[6]=='I' && rx_pi_cmd[7]=='P' && rx_pi_cmd[8]=='O' && rx_pi_cmd[9]=='W' && rx_pi_cmd[10]=='E' && rx_pi_cmd[11]=='R' && rx_pi_cmd[12]=='E' && rx_pi_cmd[13]=='D' && rx_pi_cmd[14]=='O' && rx_pi_cmd[15]=='N') ) {
      //if (!PI_ON && msg=="PIPOWEREDON"){
      //if (msg=="PIPOWEREDON"){
       // PI_ON = TRUE;
        OSSignalBinSem(BINSEM_PI_ISON);
        //csk_uart0_puts("$$$ROGERTHAT");
        //csk_uart0_puts("$$$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
        //csk_uart2_puts("in ROGERTHAT response\r\n");
        //sprintf(tmp, "\r\nin PIPOWERED response. PI_ON = %d\r\n", PI_ON);
       // sprintf(tmp, "$$$09ROGERTHAT\r\n");
        if (OSReadBinSem(BINSEM_TAKEPICPI)){
          csk_uart0_puts("$$$09ROGERTHAT");
        }
        //DEBUG...
//        dprintf("tmp: %s\t ",tmp);
//        dprintf("rx_pi_cmd before memset: %s\r\n",rx_pi_cmd);
      }
    } // end rx_pi_cmd header check
    //rx_pi_cmd[0] = '\0';
    memset(rx_pi_cmd, 0, sizeof(rx_pi_cmd)); // testing array clear
   // dprintf("rx_pi_cmd AFTER memset: %s\r\n",rx_pi_cmd);
    //RTS_FROM_PI = FALSE;

/*
    if (PI_ON) { // process commands for Pi...
      if (TAKEPICPI) {
        csk_uart0_puts("$$$09TAKEPICPI");
      } // end: if(TAKEPICPI)
    } // end: if(PI_ON)
*/
    
    OS_Delay(50);
  } // end while(1)
} // end task_pi_listen()
Exemplo n.º 2
0
/******************************************************************************
****                                                                       ****
**                                                                           **
init()

**                                                                           **
****                                                                       ****
******************************************************************************/
void init(void) {

  // Force WDT off for now ... having it on tends to confuse novice
  //   users.
  csk_wdt_off();
  

  // Keep interrupts off for now ...
  __disable_interrupt();

  
  // All CSK control signals are active LOW.
  #if   defined(__PIC24FJ256GA110__) // PSPM D

  // Minimal set of I/O ... only necessary control signals are configured as outputs.
  TRISA = 0xFFFF;
  TRISB = ~( BIT5); // TX3
  TRISC = ~( BIT1); // -OE_USB
  TRISD = ~( BIT8+BIT5+BIT2+BIT1); // HS3, HS4 & HS5, TX2
  TRISE = ~( BIT8+BIT4+BIT3+BIT2); // IO.30, -ON_SD, -ON_MHX & -OE_MHX
  TRISF = ~( BIT5+BIT3); // TX1 & TX0

  PORTA = 0x0000;
  PORTB = 0x0000+BIT5;              // TX3 initially HIGH
  PORTC = 0x0000+BIT1;              // -OE_USB is OFF
  PORTD = 0x0000+BIT8;              // TX2 initially HIGH
  PORTE = 0x0000+BIT4+BIT3+BIT2;   	// -ON_SD, -ON_MHX, -OE_MHX are OFF
  PORTF = 0x0000+BIT3+BIT5;			// TX0 & TX1 initially HIGH.
  PORTG = 0x0000;

  AD1PCFGL = 0xFFFF;

  #elif defined(__PIC24FJ256GB110__) // PSPM E Rev A

  // Minimal set of I/O ... only necessary control signals are configured as outputs.
  TRISA = 0xFFFF;
  TRISB = ~(                                                             BIT5                    ); // TX3
  TRISC = ~(                                                                            BIT1     ); // -OE_USB
  TRISD = ~(                                    BIT9+BIT8+BIT7+          BIT5                    ); // TX0, TX2, HS3, HS5
  TRISE = ~(                                         BIT8+               BIT4+BIT3+BIT2          ); // IO.30, -ON_SD, -ON_MHX & -OE_MHX
  TRISF = ~(                                                        BIT5                         ); // TX1
  TRISG = ~(BIT15                                                                                ); // HS4

  PORTA = 0x0000;
  PORTB = 0x0000+BIT5;           // TX3 initially HIGH
  PORTC = 0x0000+BIT1;           // -OE_USB is OFF
  PORTD = 0x0000+BIT9+BIT8;      // TX0, TX2 initially high
  PORTE = 0x0000+BIT4+BIT3+BIT2; // -ON_SD, -ON_MHX, -OE_MHX are OFF
  PORTF = 0x0000+BIT5;	         // TX1 initially high.
  PORTG = 0x0000;

  #elif defined(__PIC24FJ256GB210__) // PSPM E Rev B
  
  // Minimal set of I/O ... only necessary control signals are configured as outputs.
  TRISA = 0xFFFF;
  TRISB = ~(                                                             BIT5                    ); // TX3
  TRISC = ~(                                                                            BIT1     ); // -OE_USB
  TRISD = ~(                                    BIT9+BIT8+BIT7+          BIT5                    ); // TX0, TX2, HS3, HS5
  TRISE = ~(                                         BIT8+               BIT4+BIT3+BIT2          ); // IO.30, -ON_SD, -ON_MHX & -OE_MHX
  TRISF = ~(                                                        BIT5                         ); // TX1
  TRISG = ~(BIT15                                                                                ); // HS4

  PORTA = 0x0000;
  PORTB = 0x0000+BIT5;           // TX3 initially HIGH
  PORTC = 0x0000+BIT1;           // -OE_USB is OFF
  PORTD = 0x0000+BIT9+BIT8;      // TX0, TX2 initially high
  PORTE = 0x0000+BIT4+BIT3+BIT2; // -ON_SD, -ON_MHX, -OE_MHX are OFF
  PORTF = 0x0000+BIT5;	         // TX1 initially high.
  PORTG = 0x0000;

  ANSA = 0x0000;
  ANSB = 0x0000;
  ANSC = 0x0000;
  ANSD = 0x0000;
  ANSE = 0x0000;
  ANSF = 0x0000;
  ANSG = 0x0000;
  #else
  #error PIC24F device not supported by CubeSat Kit
  #endif 

  // High-level inits (works at any clock speed).
  csk_mhx_close();
  csk_mhx_pwr_off();
  csk_usb_close();
  csk_led_status_close();
  

  // Set up to run with primary oscillator.
  // See _CONFIG2 above. A configuration-word-centric setup of the
  //  oscillator(s) was chosen because of its relative simplicity.
  //  Note e.g. that PwrMgnt_OscSel() returns FALSE if clock switching
  //  (FCKSM) is disabled ...


  // Set up Timer2 to run at system tick rate                
  ConfigIntTimer2(T2_INT_ON & T2_INT_PRIOR_1);   // Timer is configured for 10 msec (100Hz), with interrupts
  OpenTimer2(T2_ON & T2_IDLE_CON & T2_GATE_OFF & T2_PS_1_1 & T2_32BIT_MODE_OFF & T2_SOURCE_INT,
             (MAIN_XTAL_FREQ/(2*100)));        // A prescalar is not required because 8E6/200 < 16 bits.


  #if   defined(__PIC24FJ256GA110__) // PSPM D
  // Configure I/O pins for UARTs via PIC24's PPS system.
  // RP inputs must be configured as inputs!
  // CSK UART0 is used as the terminal, via USB, IO.6(RP17) & IO.7(RP10)
  // CSK UART0 (PIC24 UART1) TX/RX = IO.6/IO.7
  iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP10);
  iPPSOutput(OUT_PIN_PPS_RP17,OUT_FN_PPS_U1TX);
  
  // CSK UART1 can talk to GPSRM 1 via IO.4(RP16) & IO.5(RP30)
  // CSK UART1 (PIC24 UART2) TX/RX = IO.4/IO.5
  iPPSInput(IN_FN_PPS_U2RX,IN_PIN_PPS_RP30);
  iPPSOutput(OUT_PIN_PPS_RP16,OUT_FN_PPS_U2TX);

  // CSK UART2 can talk to GPSRM 1 via IO.16(RP2) & IO.17(RP22)
  // CSK UART2 (PIC24 UART3) TX/RX = IO.16/IO.17
  iPPSInput(IN_FN_PPS_U3RX,IN_PIN_PPS_RP22);
  iPPSOutput(OUT_PIN_PPS_RP2,OUT_FN_PPS_U3TX);

  // CSK UART3 can talk to GPSRM 1 via IO.32(RP18) & IO.33(RP28)
  // CSK UART3 (PIC24 UART4) TX/RX = IO.4/IO.5
  iPPSInput(IN_FN_PPS_U4RX,IN_PIN_PPS_RP28);
  iPPSOutput(OUT_PIN_PPS_RP18,OUT_FN_PPS_U4TX);
  #elif defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB210__)
  iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP10); 	// RF4
  iPPSOutput(OUT_PIN_PPS_RP17,OUT_FN_PPS_U1TX);	// RF5
  iPPSInput(IN_FN_PPS_U2RX,IN_PIN_PPS_RP30);	// RF2
  iPPSOutput(OUT_PIN_PPS_RP4,OUT_FN_PPS_U2TX);	// RD9 
  iPPSInput(IN_FN_PPS_U3RX,IN_PIN_PPS_RP22);	// RD3
  iPPSOutput(OUT_PIN_PPS_RP2,OUT_FN_PPS_U3TX);	// RD8
  iPPSInput(IN_FN_PPS_U4RX,IN_PIN_PPS_RP28);	// RB4
  iPPSOutput(OUT_PIN_PPS_RP18,OUT_FN_PPS_U4TX);	// RB5
  #else
  #error PIC24F device not supported by CubeSat Kit
  #endif 


  // Init UARTs to 9600,N,8,1  
  // UARTs won't transmit until interrupts are enabled ...
  csk_uart0_open(CSK_UART_9600_N81);
  csk_uart1_open(CSK_UART_9600_N81);
  csk_uart2_open(CSK_UART_9600_N81);
  csk_uart3_open(CSK_UART_9600_N81);

  csk_usb_open();
  csk_uart0_puts(STR_CRLF STR_CRLF);
  csk_uart0_puts("Pumpkin " STR_CSK_TARGET " " STR_APP_NAME "." STR_CRLF);
  csk_uart0_puts(STR_VERSION "." STR_CRLF);
  csk_uart0_puts(STR_WARNING "." STR_CRLF);

  i2c1_open();
    
} /* init() */