Exemplo n.º 1
0
int main(void)
{

	SystemInit();

	STM32F4_Discovery_LEDInit(LED3); //Orange
	STM32F4_Discovery_LEDInit(LED4); //Green
	STM32F4_Discovery_LEDInit(LED5); //Red
	STM32F4_Discovery_LEDInit(LED6); //Blue

	STM32F4_Discovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

	USBD_Init(&USB_OTG_dev,USB_OTG_FS_CORE_ID,&USR_desc,&USBD_CDC_cb,&USR_cb);

	while (1){

		if(usb_cdc_kbhit()){
			char c, buffer_out[15];
			c = usb_cdc_getc();
			switch(c){
				case '3':
					STM32F4_Discovery_LEDToggle(LED3);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED3_PIN));
					usb_cdc_printf(buffer_out);
					break;
				case '4':
					STM32F4_Discovery_LEDToggle(LED4);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED4_PIN));
					usb_cdc_printf(buffer_out);
					break;
				case '5':
					STM32F4_Discovery_LEDToggle(LED5);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED5_PIN));
					usb_cdc_printf(buffer_out);
					break;
				case '6':
					STM32F4_Discovery_LEDToggle(LED6);
					sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED6_PIN));
					usb_cdc_printf(buffer_out);
					break;
			}
		}

		button_sts = STM32F4_Discovery_PBGetState(BUTTON_USER);

		if(button_sts){
			STM32F4_Discovery_LEDOff(LED3);
			STM32F4_Discovery_LEDOff(LED5);
			STM32F4_Discovery_LEDOff(LED3);
			STM32F4_Discovery_LEDOff(LED5);
		}
	}
}
Exemplo n.º 2
0
void main(void) {
   char c;

   LED_OFF(LED1);
   LED_OFF(LED2);
   LED_OFF(LED3);

   printf("\r\n\nCCS CDC (Virtual RS232) Example\r\n");

  #ifdef __PCH__
   printf("PCH: v");
   printf(__PCH__);
  #else
   printf("PCM: v");
   printf(__PCM__);
  #endif
   printf("\r\n");

   usb_init();

  #if !(__USB_PIC_PERIF__)
   printf("USBN: 0x%X", usbn_get_version());
   printf("\r\n\n");
  #endif


   while (TRUE) {
      usb_task();
      usb_debug_task();

      if (kbhit()) {
         c=getc();
         if (c=='\n') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         if (c=='\r') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         else {usb_cdc_putc(c);}
      }
      if (usb_cdc_kbhit()) {
         c=usb_cdc_getc();
         if (c=='\n') {putc('\r'); putc('\n');}
         if (c=='\r') {putc('\r'); putc('\n');}
         else {putc(c);}
      }
   }
}
Exemplo n.º 3
0
int waitFor(char targetByte) {
   int tollerance=100;  // how many bytes to ignore before giving up
   
   while (tollerance > 0) {
      while (!usb_cdc_kbhit()) {
          delay_ms(1);

//         usb_task();   // not really neccessary if usb_init() is used instead of usb_init_cs()
//         delay_ms(1);
      }
      
      if (usb_cdc_getc() == targetByte) {
         return(1);
      }
      
      tollerance--;
   }
   return(0);

}
Exemplo n.º 4
0
void housekeep() {
    usb_task(); 	// service periodic usb functions
    while (usb_cdc_kbhit() && (nqueue(&rxque0) < HIBUF)) {
	enqueue(&rxque0, usb_cdc_getc());
    }
}
Exemplo n.º 5
0
void downloadBootloader()
{
   int Buffer[SerBufferSize];    // serial input buffer   

   int1 notDone = 1;

   unsigned int recLen;   // HEX file record length
   unsigned int16 writeAddr;  // HEX file write address
   unsigned char recType;  // HEX file record type

   unsigned char i=0,j;   // general counters
///   unsigned int16 UserBootVectorAddr; // holds the address of our new boot vector
   unsigned int positionInMemoryBlock;
   unsigned int16 memoryBlockAddress;
   unsigned int bufferIndex;
   unsigned int writeLen;
   
   int16 BankAddress;
   int1 skipWriting=0;
   
   int relocateCount=0;
   #define REC_SIZE  0x10
   int relocateRecordLen[6];
   int relocateOriginalAddress[6];

//   int flashReadBuffer[getenv("FLASH_ERASE_SIZE")];  // buffer for the relocated first mem block
   int flashReadBuffer[REC_SIZE];  // buffer for the relocated first mem block

      usb_init();
      while(!usb_cdc_connected());

      while (notDone) {


         //////////////////////////////////////////
         /// Wait for ':'

         do {
            while (!usb_cdc_kbhit());
         } while (usb_cdc_getc() != ':');



         /////////////////////////////////////////
         //  Record length

         recLen = read8();

         /////////////////////////////////////////
         //  Write Address

         writeAddr  = ((int16)read8() << 8) + read8();

         /////////////////////////////////////////
         //  Rec Type

         usb_cdc_getc();  // ignore the first digit, it is always '0'
         recType = usb_cdc_getc();

         if (recType == '1') { // End of file record
            notDone = 0;

         } else if (recType == '4') {  // bank select record

            BankAddress = (read8()<<8) + read8();
            if (BankAddress != 0)
               skipWriting = 1;
            else
               skipWriting = 0;
            
         } else if ((recType == '0') && !(skipWriting)) { // data record
            

            /// get the data
            for (i=0; i < recLen ; i++) {
                Buffer[i] = read8();
                
            }
 
/*            // if data is in the EEPROM area -> do nothing
            if ((writeAddr >= 0x2100) && (writeAddr <= 0x21FF)) {
               // we don't support EEPROM records yet
            }
*/


            // if writing to the first memory block -> we need to temporarily 
            // relocate it elsewhere. Since this area contains the boot and 
            // interrupt vectors -> it should be written as the very last memory 
            // block.
            
            if (writeAddr & (0xFFFF ^ (getenv("FLASH_ERASE_SIZE")-1)) == 0) {
               relocateOriginalAddress[relocateCount] = writeAddr; // remmber the orginal location
               relocateRecordLen[relocateCount] = recLen; // remember each record's len

               writeAddr = RESERVED_MEMORY_START + (relocateCount * REC_SIZE);
               relocateCount++;
            } 
            
            positionInMemoryBlock = writeAddr & (getenv("FLASH_ERASE_SIZE")-1);
            memoryBlockAddress = writeAddr & (0xFFFF ^ (getenv("FLASH_ERASE_SIZE")-1));
            writeFlash(memoryBlockAddress, positionInMemoryBlock, recLen, Buffer);

         }
         
         if (notDone)
            // Tells the PC to send the next line
            printf(active_comm_putc, "%c",READY_FOR_NEXT);

      }
 
       // Tells the PC that we are finished
      printf(active_comm_putc,"%c", FINISH_FLAG);
 
      delay_ms(100);
      
      /////////////////////////////////////////////////////////////////////////
      //
      //  Now we move the relocated first block to its original place 
      //

      // read the entire flash block to memory

      disable_interrupts(GLOBAL);

      for (i=0;i<relocateCount;i++) {

         read_program_memory(RESERVED_MEMORY_START + (i*REC_SIZE), flashReadBuffer, REC_SIZE);

         writeAddr = relocateOriginalAddress[i];
         writeFlash(0x0, writeAddr, relocateRecordLen[i], flashReadBuffer );
      }

      output_low(USER_LED);
      delay_ms(100);

      reset_cpu();


}
Exemplo n.º 6
0
Arquivo: main.c Projeto: sepherro/mi
int main(void)
{

	SystemInit();

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);	//

	STM32F4_Discovery_LEDInit(LED3); //Orange
	STM32F4_Discovery_LEDInit(LED4); //Green
	STM32F4_Discovery_LEDInit(LED5); //Red
	STM32F4_Discovery_LEDInit(LED6); //Blue

	STM32F4_Discovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

	USBD_Init(&USB_OTG_dev,USB_OTG_FS_CORE_ID,&USR_desc,&USBD_CDC_cb,&USR_cb);

	led_driver_init();
	motor_driver_init();

	SysTick_Config(2100000);										//10 times a second: 168 MHz / 8 = 21 MHz
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

	//while(1)
	//{
	//led_driver_write_data(50, 10);

	//}
	while (1)
	{

		if( usb_cdc_kbhit() )
		{
			character = usb_cdc_getc();
			usb_rx_buffer[usb_rx_ptr] = character;
			usb_rx_ptr++;

			if( usb_rx_ptr == 10 )															//upon receiving 10 bytes
			{
				if( ( usb_rx_buffer[0] == 0xAA ) && ( usb_rx_buffer[9] == 0xCC ) )			//check head and tail for correctness
				{
					usb_rx_ptr = 0;															//reset data pointer if correct
					flag_new_command_received = 1;											//set the flag if packet correct
				}
				else
				{
					usb_rx_ptr = 0;															// if packet incorrect, reset data pointer
				}
			}
		}

		if(flag_new_command_received)
		{



			flag_new_command_received = 0;
		}

		if(flag_send_sensor_data)
		{


			motor_set_speed(0x55, 0x55);

			flag_send_sensor_data = 0;
		}

		// here's how to send data through USB VCP
		/*
		sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED6_PIN));
		usb_cdc_printf(buffer_out);
		*/
	}
}
Exemplo n.º 7
0
void main()
{
   int1 flag = 0;
   
   porta = 0;//all ports are zero
   portb = 0;
   portc = 0;

   setup_adc_ports(no_analogs|vss_vdd); //digital functions selected
   setup_adc(adc_off); //internal rc oscillator disabled for adc
   setup_wdt(wdt_off); //watch dog timer disabled
   setup_timer_0(rtcc_off); //all timers disabled
   setup_timer_1(t1_disabled);
   setup_timer_2(t2_disabled,0,1);
   setup_timer_3(t3_disabled|t3_div_by_1);
   setup_comparator(nc_nc_nc_nc); //comparators disabled
   setup_vref(false); //no reference voltage in ra2
   setup_ccp1(ccp_off); //disable ccp1
   setup_ccp2(ccp_off); //disable ccp2
   enable_interrupts(int_rda); //uart rx interruption enabled
   enable_interrupts(global); //global interruptions enabled
   usb_cdc_init();
   usb_init(); //initialize hardware usb and wait for PC conection
 
   set_tris_a(0b00111111);
   set_tris_b(0b11111011);//rb2 output mclr dspic
   
   port_b_pullups(false);
   set_tris_c(0b10111111);
   
   stateDspic = running;
   counterReset = 0;  
   delay_ms(500);//wait for supply stabilization

   while(true)
   {
      usb_task();
      manage_conection();
 
      if (usb_cdc_kbhit())
      {
         data_rx_usb=usb_cdc_getc();//read buffer and save in data_rx
         printf("%c",data_rx_usb);//send through uart
         
         if (data_rx_usb == rstKeyword[0])
         {
            if (counterReset == 0)
               counterReset++;
         }
         else if (data_rx_usb == rstKeyword[1])
         {
            if (counterReset == 1)
               counterReset++;
            else
               counterReset = 0;
         }
         else if (data_rx_usb == rstKeyword[2])
         {
            if (counterReset == 2)
               counterReset++;
            else
               counterReset = 0;
         }
         else if (data_rx_usb == rstKeyword[3])
         {
            if (counterReset == 3)
               counterReset++;
            else
               counterReset = 0;
         }
         else if (data_rx_usb == rstKeyword[4] && counterReset == 4)//here, all requirements were met
         {
            counterReset = 0;
            flag = 0; //reset flag
            for(i = 0; i < 10000; i++) //wait for the next byte
            {
               if (usb_cdc_kbhit()) //if a new byte is received
               {
                  data_rx_usb = usb_cdc_getc();//read buffer and save in data_rx
                  printf("%c",data_rx_usb);//send through uart 
                  flag = 0;
                  break;
               }
               flag = 1;                            
            }
            if (flag == 1) //apply reset when no characters were received
            {
               stateDspic = stop;
               delay_ms(50);
               stateDspic = running;    
            }
         }
         else
            counterReset = 0;
         
      }
   }
}
Exemplo n.º 8
0
void load_program(void)
{
  int1  do_ACKLOD, done=FALSE;
  int8  checksum, line_type;
  int16 l_addr,h_addr=0;
  int8 to;
  int32 addr;
  int8  dataidx, i, count;
  int8  data[32];
  int  buffidx;
  char buffer[BUFFER_LEN_LOD];
   
  while (!done)  // Loop until the entire program is downloaded
  {
    usb_task();

    if(!usb_cdc_kbhit())
      continue;
         
    buffidx = 0;  // Read into the buffer until 0x0D ('\r') is received or the buffer is full
    to = 250;   //250 milliseconds
    do 
    {
      if(!usb_cdc_kbhit())
      {
	delay_ms(1);
	to--;
	if(!to)
	  break;
      }
      else
	to = 250;
      i = usb_cdc_getc();
      buffer[buffidx++] = i;
    }while((i != 0x0D) && (i != 0x0A) && (buffidx <= BUFFER_LEN_LOD));
            
    if(!to)
      continue;

    usb_cdc_putc(XOFF);  // Suspend sender

    do_ACKLOD = TRUE;

    // Only process data blocks that start with ':'
    if(buffer[0] == ':')
    {
      count = atoi_b16 (&buffer[1]);  // Get the number of bytes from the buffer

      // Get the lower 16 bits of address
      l_addr = make16(atoi_b16(&buffer[3]),atoi_b16(&buffer[5]));

      line_type = atoi_b16 (&buffer[7]);

      addr = make32(h_addr,l_addr);

      // If the line type is 1, then data is done being sent
      if(line_type == 1) 
      {
	done = TRUE;
      }
      else if((addr >= (int32)APPLICATION_START) && (addr < ((int32)0x300000)))
      {
	checksum = 0;  // Sum the bytes to find the check sum value
	for(i=1; i<(buffidx-3); i+=2)
	  checksum += atoi_b16 (&buffer[i]);
	checksum = 0xFF - checksum + 1;

	if(checksum != atoi_b16 (&buffer[buffidx-3]))
	  do_ACKLOD = FALSE;
	else   
	{
	  if(line_type == 0) {
	    // Loops through all of the data and stores it in data
	    // The last 2 bytes are the check sum, hence buffidx-3
	    for(i = 9,dataidx=0; i < buffidx-3; i += 2)
	      data[dataidx++]=atoi_b16(&buffer[i]);
                    
	    rom_w(addr, data, count);
	  }
	  else if(line_type == 4)
	    h_addr = make16(atoi_b16(&buffer[9]), atoi_b16(&buffer[11]));
	}
      }
    }

    if(do_ACKLOD)
      usb_cdc_putc (ACKLOD);

    usb_cdc_putc(XON);
  }

  usb_cdc_putc(ACKLOD);
  usb_cdc_putc(XON);
  delay_ms(2000);   //give time for packet to flush
  reset_cpu();
}