Пример #1
0
static void init(void) {
	usart1_init(115200, buf_in, ARR_LEN(buf_in), buf_out, ARR_LEN(buf_out));
	sysclock_init();
	can_init();

	sei();
	puts_P(PSTR("Init complete\n\n"));
}
Пример #2
0
static void init(void) {
	sysclock_init();
	_delay_ms(100);
	button_init();
	can_init();

	sei();
	puts_P(PSTR("Init complete\n\n"));
}
Пример #3
0
void initialize_hardware(){
	 sysclock_init();
	 LPC_MRT->Channel[0].CTRL=(1u<<1);  //multi-rate timer canal 0 en mode 'one-shot interrupt'
	 assign_pins(); // assignation des périphériques aux broches
	 //LPC_SYSCON->IRQLATENCY=40;
	 NVIC_SetPriority(SCT_IRQn,0); // plus haute priorité d'interruption
	 NVIC_SetPriority(MRT_IRQn,3); // plus basse priorité
	 PWM_init();
     SPI0_init();
}//f()
Пример #4
0
void setup(void)
{
   unsigned char i;

   watchdog_disable();
   
   XBR0 = 0x01;                 // Enable RX/TX
   XBR1 = 0x40;                 // Enable crossbar
   P0MDOUT = 0x90;              // P0.4: TX, P0.7: RS485 enable Push/Pull
   P0SKIP  = 0x0C;              // Skip P0.2&3 for Xtal
   P0MDIN  = 0xF3;              // P0.2&3 as analog input for Xtal
   P2MDOUT = 0x02;              // P2.1 for external watchdog

   OSCXCN = 0;
   OSCICN |= 0x03;              // Configure internal oscillator for
                                // its maximum frequency

   CLKMUL = 0x00;               // Select internal oscillator as
                                // input to clock multiplier

   CLKMUL |= 0x80;              // Enable clock multiplier
   for (i=0 ; i<100 ; i++);     // Delay for >5us
   CLKMUL |= 0xC0;              // Initialize the clock multiplier

   while(!(CLKMUL & 0x20));     // Wait for multiplier to lock

   CLKSEL |= USB_4X_CLOCK;      // Select USB clock
   CLKSEL |= SYS_INT_OSC;       // Select system clock

   VDM0CN = 0x80;               // VDD monitor enable
   while ((VDM0CN & 0x40)==0);  // wait for VDD monitor to stabilize

   /* start system clock */
   sysclock_init();

   /* init memory */
   RS485_ENABLE = 0;

   /* initialize UART0 */
   uart_init(0, BD_115200);
   PS0 = 1;                     // serial interrupt high priority

   /* Blink LEDs */
   led_blink(0, 3, 150);
   led_blink(1, 3, 150);

   /* invert first LED */
   led_mode(0, 1);

   /* dummy to prevent linker error */
   delay_us(1);
}
Пример #5
0
static void init(void) {
	usart1_init(115200, buf_in, ARR_LEN(buf_in), buf_out, ARR_LEN(buf_out));
	sysclock_init();
	adc_init(1, AVCC, 4);
	can_init();

	vnh2sp30_init();
	vnh2sp30_active_break_to_Vcc();

	SET_PIN_MODE(NEUT_PORT, NEUT_PIN, INPUT_PULLUP);
	SET_PIN_MODE(IGN_PORT, IGN_PIN, OUTPUT);
	IGNITION_UNCUT();

	can_subscribe(PADDLE_STATUS);
	can_subscribe(NEUTRAL_ENABLED);
	can_subscribe(GEAR_STOP_BUTTON);
	can_subscribe(RESET_GEAR_ESTIMATE);

	sei();
	puts_P(PSTR("Init complete\n\n"));
}
Пример #6
0
int main(void)

{
    extern struct keypad buttons[MAX_BUTTONS];
    extern volatile uint8_t SBstore[MAX_BUTTONS];
    extern uint8_t total_buttons;
    uint8_t pov[4] = {0};


  /* Configure the system clock, Initializes the Flash interface and the Systick */
  sysclock_init();

  /* Initialize all configured peripherals */
  gpio_init();
//  adc_init();
  MX_USB_DEVICE_Init();


  while (1)
  {
	  	 fill_buffer_4_axises();
	  	 CheckRotaries();

	  	 for (uint8_t i=0;i<MAX_POVS;i++) {
	  		pov[i]=0;
	  	 }

		 for (uint8_t i=0;i<total_buttons;i++) {
				if (SBstore[i] == pov1up_button){
					if (buttons[i].pressed)
						pov[0] |= 1<<3;
				}
				if (SBstore[i] == pov1right_button){
					if (buttons[i].pressed)
						pov[0] |= 1<<2;
				}
				if (SBstore[i] == pov1down_button){
					if (buttons[i].pressed)
						pov[0] |= 1<<1;
				}
				if (SBstore[i] == pov1left_button){
					if (buttons[i].pressed)
						pov[0] |= 1;
				}
				if (SBstore[i] == pov2up_button){
					if (buttons[i].pressed)
						pov[1] |= 1<<3;
				}
				if (SBstore[i] == pov2right_button){
					if (buttons[i].pressed)
						pov[1] |= 1<<2;
				}
				if (SBstore[i] == pov2down_button){
					if (buttons[i].pressed)
						pov[1] |= 1<<1;
				}
				if (SBstore[i] == pov2left_button){
					if (buttons[i].pressed)
						pov[1] |= 1;
				}
				if (SBstore[i] == pov3up_button){
					if (buttons[i].pressed)
						pov[2] |= 1<<3;
				}
				if (SBstore[i] == pov3right_button){
					if (buttons[i].pressed)
						pov[2] |= 1<<2;
				}
				if (SBstore[i] == pov3down_button){
					if (buttons[i].pressed)
						pov[2] |= 1<<1;
				}
				if (SBstore[i] == pov3left_button){
					if (buttons[i].pressed)
						pov[2] |= 1;
				}
				if (SBstore[i] == pov4up_button){
					if (buttons[i].pressed)
						pov[3] |= 1<<3;
				}
				if (SBstore[i] == pov4right_button){
					if (buttons[i].pressed)
						pov[3] |= 1<<2;
				}
				if (SBstore[i] == pov4down_button){
					if (buttons[i].pressed)
						pov[3] |= 1<<1;
				}
				if (SBstore[i] == pov4left_button){
					if (buttons[i].pressed)
						pov[3] |= 1;
				}
			 if (SBstore[i] == joystick_button) {
				 if (buttons[i].pressed){
					 //USBSendBuffer[i/8+1] |= ButtonsCodes[i%8];
					 gamepad_report.buttons |= 1ULL<<i;
				 } else {
					 //USBSendBuffer[i/8+1] &= ~ButtonsCodes[i%8];
					 gamepad_report.buttons &= ~(1ULL<<i);
				 }
			 }
			if (SBstore[i] == mouseleft_button){
				if (buttons[i].pressed){
					mouse_report.left_button = 1;
				} else {
					mouse_report.left_button = 0;
				}
			}
			if (SBstore[i] == mousemiddle_button){
				if (buttons[i].pressed){
					mouse_report.middle_button = 1;
				} else {
					mouse_report.middle_button = 0;
				}
			}
			if (SBstore[i] == mouseright_button){
				if (buttons[i].pressed){
					mouse_report.right_button = 1;
				} else {
					mouse_report.right_button = 0;
				}
			}
			if (SBstore[i] == leftcontrol_button){
				if (buttons[i].pressed){
					keyboard_report.left_ctrl = 1;
				} else {
					keyboard_report.left_ctrl = 0;
				}
			}
			if (SBstore[i] == leftshift_button){
				if (buttons[i].pressed){
					keyboard_report.left_shft = 1;
				} else {
					keyboard_report.left_shft = 0;
				}
			}
			if (SBstore[i] == leftalt_button){
				if (buttons[i].pressed){
					keyboard_report.left_alt = 1;
				} else {
					keyboard_report.left_alt = 0;
				}
			}
			if (SBstore[i] == leftgui_button){
				if (buttons[i].pressed){
					keyboard_report.left_gui = 1;
				} else {
					keyboard_report.left_gui = 0;
				}
			}
			if (SBstore[i] == rightcontrol_button){
				if (buttons[i].pressed){
					keyboard_report.right_ctrl = 1;
				} else {
					keyboard_report.right_ctrl = 0;
				}
			}
			if (SBstore[i] == rightshift_button){
				if (buttons[i].pressed){
					keyboard_report.right_shft = 1;
				} else {
					keyboard_report.right_shft = 0;
				}
			}
			if (SBstore[i] == rightalt_button){
				if (buttons[i].pressed){
					keyboard_report.right_alt = 1;
				} else {
					keyboard_report.right_alt = 0;
				}
			}
			if (SBstore[i] == rightgui_button){
				if (buttons[i].pressed){
					keyboard_report.right_gui = 1;
				} else {
					keyboard_report.right_gui = 0;
				}
			}
			if 		(((SBstore[i] >= a_button) && (SBstore[i] <= space_button)) ||
					((SBstore[i] >= f1_button) && (SBstore[i] <= f12_button)) ||
					((SBstore[i] >= insert_button) && (SBstore[i] <= uparrow_button))) {
				if (buttons[i].pressed){
					keyboard_report.key1 = SBstore[i];
				} else {
					keyboard_report.key1 = 0;
				}
			}
			if (SBstore[i] == volumemute_button) {
				if (buttons[i].pressed){
					multimedia_report.volume_mute = 1;
				} else {
					multimedia_report.volume_mute = 0;
				}
			}
			if (SBstore[i] == volumeup_button) {
				if (buttons[i].pressed){
					multimedia_report.volume_up = 1;
				} else {
					multimedia_report.volume_up = 0;
				}
			}
			if (SBstore[i] == volumedown_button) {
				if (buttons[i].pressed){
					multimedia_report.volume_down = 1;
				} else {
					multimedia_report.volume_down = 0;
				}
			}
		 }

		 for (uint8_t i=0;i<MAX_POVS;i++) {
		 	  			 switch (pov[i]) {
		 	  			 	 case (8)	: gamepad_report.pov[i] = 0; break;
		 	  			 	 case (12)	: gamepad_report.pov[i] = 1; break;
		 	  			 	 case (4)	: gamepad_report.pov[i] = 2; break;
		 	  			 	 case (6)	: gamepad_report.pov[i] = 3; break;
		 	  			 	 case (2)	: gamepad_report.pov[i] = 4; break;
		 	  			 	 case (3)	: gamepad_report.pov[i] = 5; break;
		 	  			 	 case (1)	: gamepad_report.pov[i] = 6; break;
		 	  			 	 case (9)	: gamepad_report.pov[i] = 7; break;
		 	  			 	 default	: gamepad_report.pov[i] = 8; break;
		 	  			 }
		 	  		 }

/*
	  for (uint8_t i=0;i<Number_Rotaries + config.total_single_encoders;i++){

				  diff = millis - RotaryStore[i].time_pressed;

				  if (diff > config.rotary_press_time) {
					  USBSendBuffer[(i/4)+encoders_offset] &= ~ButtonsCodes[(i%4)*2];
					  USBSendBuffer[(i/4)+encoders_offset] &= ~ButtonsCodes[(i%4)*2+1];
					  RotaryStore[i].pressed = 0;
					  RotaryStore[i].previous_state = 0;
				  } else {
					  if (!RotaryStore[i].previous_state){
						  if (RotaryStore[i].pressed == DIR_CW) {
							  USBSendBuffer[(i/4)+encoders_offset] |= ButtonsCodes[(i%4)*2];
						  }
						  if (RotaryStore[i].pressed == DIR_CCW) {
							  USBSendBuffer[(i/4)+encoders_offset] |= ButtonsCodes[(i%4)*2+1];
						  }
						  RotaryStore[i].previous_state = RotaryStore[i].pressed;
					  }
				  }
	  }

				  if (!RotaryStore[i].pressed) {
					  USBSendBuffer[(i/4)+encoders_offset] &= ~ButtonsCodes[(i%4)*2];
					  USBSendBuffer[(i/4)+encoders_offset] &= ~ButtonsCodes[(i%4)*2+1];
				  }


				  if (RotaryStore[i].pressed == DIR_CW) {
					  if ( diff > config.rotary_press_time){
						  USBSendBuffer[(i/4)+encoders_offset] &= ~ButtonsCodes[(i%4)*2];
						  RotaryStore[i].pressed = 0;
					  }
				  		 else
				  			 USBSendBuffer[(i/4)+encoders_offset] |= ButtonsCodes[(i%4)*2];
				  	 }

				  if (RotaryStore[i].pressed == DIR_CCW) {
					  if (diff > config.rotary_press_time){
						  USBSendBuffer[(i/4)+encoders_offset] &= ~ButtonsCodes[(i%4)*2+1];
						  RotaryStore[i].pressed = 0;
					  }
				  		  else
				  			  USBSendBuffer[(i/4)+encoders_offset] |= ButtonsCodes[(i%4)*2+1];
				  		}
			  }
*/

	  //We should send report only if some action exist
			/*  for (uint8_t i=1;i<21;i++) {
				  chk |= USBSendBuffer[i];
			  }
			  for (uint8_t i=21;i<25;i++) {
				  if (USBSendBuffer[i] != 8) chk |= USBSendBuffer[i];
			  }
			  if (chk) {
				 USBD_CUSTOM_HID_SendReport(hUsbDevice_0, USBSendBuffer, USEDPINS+1);
				 packet_counter=0;
			  } else {
				  packet_counter++;
				  if (packet_counter < 6) USBD_CUSTOM_HID_SendReport(hUsbDevice_0, USBSendBuffer, USEDPINS+1);
			  }*/
  }
}
Пример #7
0
void setup(void)
{
   unsigned char adr, flags, d;
   unsigned short i;
   unsigned char *p;

   _flkey = 0;
   
   /* first disable watchdog */
   watchdog_disable();

   /* avoid any blocking of RS485 bus */
   RS485_ENABLE = RS485_ENABLE_OFF;

   /* Port and oscillator configuration */

#if defined(CPU_C8051F120)

   SFRPAGE   = CONFIG_PAGE;
  
   XBR0 = 0x04;                 // Enable XBar, UART0 & UART1
   XBR1 = 0x00;
   XBR2 = 0x44;

  #ifdef CLK_25MHZ
   /* Select internal quartz oscillator */
   SFRPAGE   = LEGACY_PAGE;
   FLSCL     = 0x00;            // set flash read time for <25 MHz

   SFRPAGE   = CONFIG_PAGE;
   OSCICN    = 0x83;            // divide by 1
   CLKSEL    = 0x00;            // select internal oscillator
  #else          // 98 MHz
   /* Select internal quartz oscillator */
   SFRPAGE   = LEGACY_PAGE;
   FLSCL     = 0xB0;            // set flash read time for 100 MHz

   SFRPAGE   = CONFIG_PAGE;
   OSCICN    = 0x83;            // divide by 1
   CLKSEL    = 0x00;            // select internal oscillator

   PLL0CN    |= 0x01;
   PLL0DIV   = 0x01;
   PLL0FLT   = 0x01;
   PLL0MUL   = 0x04;
   for (i = 0 ; i < 15; i++);   // Wait 5us for initialization
   PLL0CN    |= 0x02;
   for (i = 0 ; i<50000 && ((PLL0CN & 0x10) == 0) ; i++);

   CLKSEL    = 0x02;            // select PLL as sysclk src
  #endif

#elif defined(CPU_C8051F020)

   XBR0 = 0x04;                 // Enable UART0 & UART1
   XBR1 = 0x00;
   XBR2 = 0x44;

   /* Select external quartz oscillator */
   OSCXCN = 0x67;               // Crystal mode, Power Factor 22E6
   OSCICN = 0x08;               // CLKSL=1 (external)

#elif defined(CPU_C8051F310) || defined(CPU_C8051F320)

   XBR0 = 0x01;                 // Enable RX/TX
   XBR1 = 0x40;                 // Enable crossbar

   /* Select internal quartz oscillator */
   OSCICN = 0x83;               // IOSCEN=1, SYSCLK=24.5 MHz
   CLKSEL = 0x00;               // derive SYSCLK from internal source

#else

   XBR0 = 0x04;                 // Enable RX/TX
   XBR1 = 0x00;
   XBR2 = 0x40;                 // Enable crossbar

   PRT0CF = 0x01;               // P0.0: TX = Push Pull
   PRT1CF = 0x00;               // P1
   PRT2CF = 0x00;               // P2  Open drain for 5V LCD
   PRT3CF = 0x20;               // P3.5: RS485 enable = Push Pull

   /* Select external quartz oscillator */
   OSCXCN = 0x67;               // Crystal mode, Power Factor 22E6
   OSCICN = 0x08;               // CLKSL=1 (external)

#endif
        
#ifdef CFG_HAVE_LCD
   lcd_setup();
#endif

#ifdef CFG_HAVE_EMIF
   /* initialize external memory interface */
   d = emif_init();

   /* do memory test on cold start */
   SFRPAGE = LEGACY_PAGE;
   if (d > 0 && (RSTSRC & 0x02) > 0)
      emif_test(d);
#endif

   /* start system clock */
   sysclock_init();

   /* enable watchdog with default timeout */
   watchdog_enable(0);

   /* enable missing clock detector */
   RSTSRC |= 0x04;

   /* default LED mode */
   for (i=0 ; i<N_LED ; i++)
      led_mode(i, 1);
   
   /* initialize all memory */
   CSR = 0;
   addressed = 0;
   flash_param = 0;
   flash_program = 0;
   flash_allowed = 0;
   wrong_cpu = 0;
   _flkey = 0;

#ifdef CFG_HAVE_RTC
   rtc_set = 0;
#endif

   i_in = i_out = n_out = 0;
   _cur_sub_addr = 0;
   for (i=0 ; i<sizeof(in_buf) ; i++)
      in_buf[i] = 0;
   for (i=0 ; i<sizeof(out_buf) ; i++)
      out_buf[i] = 0;

   /* check if we got reset by watchdog */
#if defined(CPU_C8051F120)
   SFRPAGE   = LEGACY_PAGE;
#endif
   WD_RESET = ((RSTSRC & 0x02) == 0 && (RSTSRC & 0x08) > 0);

   /* initialize UART(s) */
   uart_init(0, BD_115200);

#ifdef CFG_UART1_MSCB
   uart_init(1, BD_115200);
#endif

#ifdef CFG_DYN_VARIABLES
   setup_variables();
#endif

   /* count variables */
   for (n_variables = _var_size = 0;; n_variables++) {
      _var_size += variables[n_variables].width;
      if (variables[n_variables].width == 0)
         break;
   }

   /* check if variables are in xdata and xdata is present */
   if (n_variables > 0) {
      p = variables[0].ud;
      d = *p;
      *p = 0x55;
      if (*p != 0x55)
         wrong_cpu = 1;
      *p = 0xAA;
      if (*p != 0xAA)
         wrong_cpu = 1;
      *p = d;
   }

   /* retrieve EEPROM data */
#ifdef CPU_C8051F120
   SFRPAGE = LEGACY_PAGE;
#endif
   if ((RSTSRC & 0x02) > 0)
      flags = eeprom_retrieve(1); // vars on cold start
   else
      flags = eeprom_retrieve(0);

   if ((flags & (1 << 0)) == 0) {
      configured_addr = 0;
   
      /* set initial values */
      sys_info.node_addr = 0xFFFF;
      sys_info.group_addr = 0xFFFF;
      memset(sys_info.node_name, 0, sizeof(sys_info.node_name));
      strncpy(sys_info.node_name, node_name, sizeof(sys_info.node_name));
   } else
      configured_addr = 1;

   /* store SVN revision */
   sys_info.svn_revision = (svn_rev_main[6]-'0')*1000+
                           (svn_rev_main[7]-'0')*100+
                           (svn_rev_main[8]-'0')*10+
                           (svn_rev_main[9]-'0');

   if ((flags & (1 << 1)) == 0) {

      /* init variables */
      for (i = 0; variables[i].width; i++)
         if (!(variables[i].flags & MSCBF_DATALESS)) {
            /* do it for each sub-address */
            for (adr = 0 ; adr < _n_sub_addr ; adr++) {
               memset((char*)variables[i].ud + _var_size*adr, 0, variables[i].width);
            }
         }

      /* call user initialization routine with initialization */
      user_init(1);

      /* write current variables to flash later in main routine */
      configured_vars = 0;
   } else {
      /* call user initialization routine without initialization */
      user_init(0);
      configured_vars = 1;
   }

   /* Blink LEDs */
   for (i=0 ; i<N_LED ; i++)
      led_blink(i, 3, 150);

}
Пример #8
0
void setup(void)
{
   int i;                       // software timer

   WDTCN = 0xDE;                // Disable watchdog timer
   WDTCN = 0xAD;

   SFRPAGE = CONFIG_PAGE;       // set SFR page

   XBR0 = 0x04;                 // Enable UART0 & UART1
   XBR1 = 0x00;
   XBR2 = 0x44;

   // all pins used by the external memory interface are in push-pull mode
   P0MDOUT = 0xFF;
   P1MDOUT = 0xFF;
   P2MDOUT = 0xFF;
   P3MDOUT = 0xFF;
   P0 = 0xC0;                   // /WR, /RD, are high, RESET is low
   P1 = 0x00;
   P2 = 0x00;                   // P1, P2 contain the address lines
   P3 = 0xFF;                   // P3 contains the data lines

   OSCICN = 0x83;               // set internal oscillator to run
   // at its maximum frequency

   CLKSEL = 0x00;               // Select the internal osc. as
   // the SYSCLK source

   //Turn on the PLL and increase the system clock by a factor of M/N = 2
   SFRPAGE = CONFIG_PAGE;

   PLL0CN = 0x00;               // Set internal osc. as PLL source
   SFRPAGE = LEGACY_PAGE;
   FLSCL = 0x10;                // Set FLASH read time for 50MHz clk
   // or less
   SFRPAGE = CONFIG_PAGE;
   PLL0CN |= 0x01;              // Enable Power to PLL
   PLL0DIV = 0x01;              // Set Pre-divide value to N (N = 1)
   PLL0FLT = 0x01;              // Set the PLL filter register for
   // a reference clock from 19 - 30 MHz
   // and an output clock from 45 - 80 MHz
   PLL0MUL = 0x02;              // Multiply SYSCLK by M (M = 2)

   for (i = 0; i < 256; i++);   // Wait at least 5us
   PLL0CN |= 0x02;              // Enable the PLL
   while (!(PLL0CN & 0x10));    // Wait until PLL frequency is locked
   CLKSEL = 0x02;               // Select PLL as SYSCLK source

   SFRPAGE = LEGACY_PAGE;

   EMI0CF = 0xD7;               // Split-Mode, non-multiplexed
   // on P0-P3 (use 0xF7 for P4 - P7)

   EMI0TC = 0xB7;               // This value may be modified
   // according to SYSCLK to meet the
   // timing requirements for the CS8900A
   // For example, EMI0TC should be >= 0xB7
   // for a 100 MHz SYSCLK.


   SFRPAGE = ADC0_PAGE;
   AMX0CF = 0x00;               // select single ended analog inputs
   ADC0CF = 0xE0;               // 16 system clocks, gain 1
   ADC0CN = 0x80;               // enable ADC 

   REF0CN = 0x03;               // enable internal reference
   DAC0CN = 0x80;               // enable DAC0
   DAC1CN = 0x80;               // enable DAC1

   /* init memory */
   RS485_ENABLE = 0;

   /* start system clock */
   sysclock_init();
   CKCON = 0x00;                // assumed by tcp_timer()

   /* Blink LEDs */
   led_blink(0, 5, 150);
   led_blink(1, 5, 150);

   /* invert first LED */
   led_mode(0, 1);
}