Exemple #1
0
void motorRightOff(void){
	chSysLock();
	pwmEnableChannelI(motor_pwm, rp1, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	pwmEnableChannelI(motor_pwm, rp2, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	chBSemSignalI(&motor_right_sem);
	chSysUnlock();
}
Exemple #2
0
CCM_FUNC static THD_FUNCTION(ThreadBDU, arg)
{
  event_listener_t el1;
  eventmask_t flags;
  (void)arg;
  chRegSetThreadName("BDU");

  chEvtRegisterMask(chnGetEventSource(&SDU2), &el1, ALL_EVENTS);

  while(USBD1.state != USB_READY) chThdSleepMilliseconds(10);
  while(SDU2.state != SDU_READY) chThdSleepMilliseconds(10);

  while (TRUE)
  {
    chEvtWaitAnyTimeout(ALL_EVENTS, TIME_IMMEDIATE);
    flags = chEvtGetAndClearFlags(&el1);

    pwmEnableChannel(&PWMD_LED2, CHN_LED2, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED2, 8000));

    if (flags & CHN_INPUT_AVAILABLE)
    {
      pwmEnableChannel(&PWMD_LED2, CHN_LED2, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED2, 1000));
      readCommand((BaseChannel *)&SDU2);
    }
    else
      chThdSleepMilliseconds(2);
  }
  return;
}
Exemple #3
0
void convStart( void )
{
    /*
    pwmStart( &PWMD2, &pwmCfg );
    palSetPadMode( GPIOA, 0, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    palSetPadMode( GPIOA, 1, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    //palSetPadMode( GPIOA, 2, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH( &PWMD2, 9500 ) );
    pwmEnableChannel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH( &PWMD2, 3030 ) );
    //pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH( &PWMD2, 3030 ) );
    */

	// Start PWM peripherial.
    pwmStart( &CONV_PWM, &pwmCfg );
	// Init PWM pins.
    palSetPadMode( CONV_PORT, CONV_BUCK_PIN,  PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    palSetPadMode( CONV_PORT, CONV_BOOST_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    // Set zero active period.
    pwmEnableChannel(&CONV_PWM, PWM_BOOST_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, 0000 ) );
    pwmEnableChannel(&CONV_PWM, PWM_BUCK_CHAN,  PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, 0000 ) );

    // Init ADC.
    palSetGroupMode(CONV_ADC_PORT, PAL_PORT_BIT( CONV_BUCK_FB_PIN ) |
    	                           PAL_PORT_BIT( CONV_BOOST_FB_PIN ) |
    	                           PAL_PORT_BIT( CONV_INPUT_FB_PIN ),
                                   0, PAL_MODE_INPUT_ANALOG);
    adcStart( &ADCD1, NULL );

    adcStartConversion( &ADCD1, &adcGroup, adcSamples, ADC_BUF_DEPTH );
}
Exemple #4
0
void motor_refresh(PWMDriver *pwmp) {
	(void)pwmp;
	static uint8_t led_state = 0;
	pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, motors[0]));
	pwmEnableChannel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, motors[1]));
	pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, motors[2]));
	pwmEnableChannel(&PWMD2, 3, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, motors[3]));
}
Exemple #5
0
void motor_right_off(void *p) {
	(void) p;
	chSysLockFromIsr();
	pwmEnableChannelI(motor_pwm, rp1, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	pwmEnableChannelI(motor_pwm, rp2, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	chBSemSignalI(&motor_right_sem);
	chSysUnlockFromIsr();
}
Exemple #6
0
void motor_init(void) {
	pwmStart(&PWMD2, &pwmcfg);
	
	pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 500)); // 1mS Pulse
	pwmEnableChannel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 500)); // 1mS Pulse
	pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 500)); // 1mS Pulse
	pwmEnableChannel(&PWMD2, 3, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 500)); // 1mS Pulse
}
Exemple #7
0
void motor_right_on(Direction dir){
	chBSemWait(&motor_right_sem);
	chSysLock();
	if(dir == D_FORWARD){
		pwmEnableChannelI(motor_pwm, rp1, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
		pwmEnableChannelI(motor_pwm, rp2, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,motor_speed));
	}else{
		pwmEnableChannelI(motor_pwm, rp1, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,motor_speed));
		pwmEnableChannelI(motor_pwm, rp2, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	}
	chSysUnlock();
}
void PWMPlatform::set(std::uint8_t ch, float dc) {
  if (ch < 4) {
    pwmcnt_t width = PWM_PERCENTAGE_TO_WIDTH(&PWMD1, dc * 10000.0f);
    pwmEnableChannel(&PWMD1, ch, width);
  }
  else if (ch < 8) {
    pwmcnt_t width = PWM_PERCENTAGE_TO_WIDTH(&PWMD4, dc * 10000.0f);
    pwmEnableChannel(&PWMD4, ch-4, width);
  }
  else if (ch < 12) {
    pwmcnt_t width = PWM_PERCENTAGE_TO_WIDTH(&PWMD3, dc * 10000.0f);
    pwmEnableChannel(&PWMD3, ch-8, width);
  }
}
Exemple #9
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();
  /*
   * Initializes the PWM driver 1.
   * GPIOC1 is the PWM output.
   */

  palSetPadMode(IOPORT3, 1, PAL_MODE_ALTERNATIVE_4);
  pwmStart(&PWMD1, &pwmcfg);
  pwmEnablePeriodicNotification(&PWMD1);

  /*
   * Starts the PWM channel 0 using 25% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  pwmEnableChannel(&PWMD1, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  pwmEnableChannelNotification(&PWMD1, 1); // MUST be before EnableChannel...
  chThdSleepMilliseconds(2500);

  /*
   * Changes PWM period to 500µs the duty cycle becomes 50%
   * implicitly.
   */
  pwmChangePeriod(&PWMD1, 18000);
  chThdSleepMilliseconds(2500);
  //~ pwmEnablePeriodicNotification(&PWMD1);
  /*
   * Disables channel 0 and stops the drivers.
   */
  //~ pwmDisableChannel(&PWMD1, 0);
  //~ pwmStop(&PWMD1);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
    chThdSleepMilliseconds(5);
  }
  return 0;
}
Exemple #10
0
/* This function will start the PWM generator.
 */
extern void bldcInit(void) {
  bldc.scheme = &pwmScheme;
  bldc.state = 0;          //Default to first state
  bldc.nextState = 0;
  bldc.directionFwd = TRUE;
  bldc.stateChangeInterval = MS2RTT(20);
  bldc.prevStateChange = halGetCounterValue();
  bldc.nextStateChange = bldc.prevStateChange + bldc.stateChangeInterval;
  bldc.pwmOutT0 = 0;
  bldc.pwmOutT1 = 0;
  bldc.stateCount = sizeof(pwmScheme)/3;
  bldc.dutyCycle = 1800;

  palWriteGroup (PWM_OUT_PORT, PWM_OUT_PORT_MASK, PWM_OUT_OFFSET,  PWM_OFF);
  palSetGroupMode (PWM_OUT_PORT, PWM_OUT_PORT_MASK, PWM_OUT_OFFSET, PAL_MODE_OUTPUT_PUSHPULL);
  palSetPadMode (GPIOA, GPIOA_PIN1, PAL_MODE_INPUT_ANALOG);

  pwmStart(&PWMD1, &pwmcfg);

  // ADC trigger channel. This will trigger the ADC read at 95% of the cycle,
  // when all the PWM outputs are set to 0
  pwmEnableChannel (&PWMD1, PWM_ADCTRIG_CH, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, PWM_MAX_DUTY_CYCLE));

  // Start the ADC
  adcStart(&ADCD1, NULL);

}
Exemple #11
0
void MotorsSetSpeed(unsigned motor, pwmcnt_t speed){
	if (motor >= NUM_MOTORS) return;
	if (speed > MAX_MOTOR_SPEED) speed = MAX_MOTOR_SPEED;

	motor_speeds[motor] = speed;
	pwmEnableChannel(&PWMD8, motor, PWM_PERCENTAGE_TO_WIDTH(&PWMD8, speed));
}
Exemple #12
0
// This function should probably be replaced by a Mailbox and a thread.
extern void bldcSetDutyCycle(uint32_t dutyCycle) {
  if (dutyCycle > PWM_MAX_DUTY_CYCLE) {
    dutyCycle = PWM_MAX_DUTY_CYCLE;
  }

  bldc.dutyCycle = dutyCycle;

  pwmEnableChannel (&PWMD1, PWM_PULSE0_CH, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, dutyCycle));
}
Exemple #13
0
static THD_FUNCTION(BreatheThread, arg) {
  (void)arg;
  chRegSetThreadName("breatheThread");

  while(true) {
    pwmEnableChannel(&PWM_DRIVER, active_led, PWM_PERCENTAGE_TO_WIDTH(&PWM_DRIVER,breathing_table[table_pos]));
    table_pos++;
    if(table_pos == TABLE_SIZE) {
      table_pos = 0;
      active_led = (active_led+1) % 2;
    }
    chThdSleepMilliseconds(BREATHE_STEP);
  }
}
Exemple #14
0
static void contAdcReadyCb( ADCDriver * adcp, adcsample_t * buffer, size_t n )
{
    (void)adcp;
    //(void)buffer;
    (void)n;
    // Buck
    if ( buffer[ BUCK_IND ] < buckSp )
    {
    	buckPwm += buckGain;
    	buckPwm = ( buckPwm <= 10000 ) ? buckPwm : 10000;
        pwmEnableChannelI(&CONV_PWM, PWM_BUCK_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, buckPwm ) );
    }
    else if ( buffer[ BUCK_IND ] > buckSp )
    {
    	buckPwm = ( buckPwm >= buckGain ) ? buckPwm : buckGain;
    	buckPwm -= buckGain;
    	pwmEnableChannelI(&CONV_PWM, PWM_BUCK_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, buckPwm ) );
    }
    // Boost
    if ( buffer[ INPUT_IND ] >= inputSp )
    {
        if ( buffer[ BOOST_IND ] < boostSp )
        {
        	boostPwm += boostGain;
        	boostPwm = ( boostPwm <= 9000 ) ? boostPwm : 9000;
        	pwmEnableChannelI(&CONV_PWM, PWM_BOOST_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, boostPwm ) );
        }
        else if ( buffer[ BOOST_IND ] > boostSp )
        {
        	boostPwm = ( boostPwm >= boostGain ) ? boostPwm : boostGain;
        	boostPwm -= boostGain;
        	pwmEnableChannelI(&CONV_PWM, PWM_BOOST_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, boostPwm ) );
        }
    }
    else
    	pwmEnableChannelI(&CONV_PWM, PWM_BOOST_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, 0 ) );
};
Exemple #15
0
static THD_FUNCTION(BreatheThread, arg) {
  (void)arg;
  chRegSetThreadName("breatheThread");

  while(true) {
    switch(active_led) {
      case 0: /* red LED */
        pwmEnableChannel(&PWM_DRIVER, 2, PWM_PERCENTAGE_TO_WIDTH(&PWM_DRIVER,breathing_table[table_pos]));
        break;
      case 1: /* green LED */    
        pwmEnableChannel(&PWM_DRIVER, 4, PWM_PERCENTAGE_TO_WIDTH(&PWM_DRIVER,breathing_table[table_pos]));
        break;
      case 2: /* blue LED */    
        pwmEnableChannel(&PWM_DRIVER, 5, PWM_PERCENTAGE_TO_WIDTH(&PWM_DRIVER,breathing_table[table_pos]));
        break;
    }
    table_pos++;
    if(table_pos == TABLE_SIZE) {
      table_pos = 0;
      active_led = (active_led+1) % 3;
    }
    chThdSleepMilliseconds(BREATHE_STEP);
  }
}
Exemple #16
0
//initialize motors
int motorInit(ioportid_t gpio, int p11, int p12, int p21, int p22, PWMDriver *pwm, int lpa, int lpb, int rpa, int rpb){
	//vars
	lp1 = lpa;
	lp2 = lpb;
	rp1 = rpa;
	rp2 = rpb;
	motor_pwm = pwm;
	//configure
	palSetPadMode(gpio, p11, PAL_MODE_ALTERNATE(2));
	palSetPadMode(gpio, p12, PAL_MODE_ALTERNATE(2));
	palSetPadMode(gpio, p21, PAL_MODE_ALTERNATE(2));
	palSetPadMode(gpio, p22, PAL_MODE_ALTERNATE(2));
	
	chBSemInit(&motor_left_sem, FALSE);
	chBSemInit(&motor_right_sem, FALSE);
	
	//disable motors
	pwmStart(motor_pwm, &motor_pwmcfg);
	pwmEnableChannel(motor_pwm, lp1, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	pwmEnableChannel(motor_pwm, lp2, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	pwmEnableChannel(motor_pwm, rp1, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	pwmEnableChannel(motor_pwm, rp2, PWM_PERCENTAGE_TO_WIDTH(motor_pwm,0));
	return TRUE;
}
/**
 * set the light level from external thread
 * @param level
 */
void CLightHandler::SetLightLevel(uint8_t level){
	/* deactivate light alarm */
	bLightAlarm = false;

	/* wait for access */
	semLightLevelSet.wait();

	u16LightLevel = PWM_PERCENTAGE_TO_WIDTH(&PWMD5, level);

	pwmEnableChannel(&PWMD5,0,u16LightLevel);

	/* release access */
	semLightLevelSet.signal();

}
Exemple #18
0
void blik(arg_t)
{

	static int16_t ja = 0;
	static int16_t inc = 10;
	//palTogglePad(TEST_LED_PORT, TEST_LED_PIN);

	if (ja > 800)
		inc = -10;
	if (ja < 30)
		inc = 10;

	ja = ja + inc;

	pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, ja));
}
Exemple #19
0
/*
 * GPIO_TRIGGER interrupt callback. Start transmit burst, disable additional
 * transmit interrupts for TRANSMIT_LOCKOUT.
 */
static void extTransmit(EXTDriver *extp, expchannel_t channel) {

  (void)extp;
  (void)channel;

  //Start transmitting
  pwmEnableChannel(&PWMD2, 3, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
  palClearPad(GPIOA, GPIO_RX);

 //Disable Transmit interrupt and start timer to reenable
  extChannelDisableI(&EXTD1, 0);              //Disable transmit interrupt
  gptStartOneShotI(&GPTD1, TRANSMIT_TIME);    //Start timer for transmit
  gptStartOneShotI(&GPTD3, TRANSMIT_LOCKOUT); //Start timer for transmit unlock


  chSysUnlockFromIsr();
}
Exemple #20
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Initializes the PWM driver 1 and ICU driver 1.
   * GPIOD10 is the PWM output.
   * GPIOA0 is the ICU input.
   * The two pins have to be externally connected together.
   */
  icuStart(&ICUD1, &icucfg);
  icuEnable(&ICUD1);

  /* Sets A0 alternative function.*/
  SIU.PCR[0].R = 0b0100010100000100;

  pwmStart(&PWMD1, &pwmcfg);
  /* Sets D10 alternative function.*/
  SIU.PCR[58].R = 0b0100010100000100;

  chThdSleepMilliseconds(2000);

  /*
   * Starts the PWM channel 0 using 75% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 50% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 25% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes PWM period and the PWM channel 0 to 50% duty cycle.
   */
  pwmChangePeriod(&PWMD1, 25000);
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Disables channel 0 and stops the drivers.
   */
  pwmDisableChannel(&PWMD1, 0);
  pwmStop(&PWMD1);
  icuDisable(&ICUD1);
  icuStop(&ICUD1);
  palClearPad(PORT_D, PD_LED3);
  palClearPad(PORT_D, PD_LED4);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
  return 0;
}
void PWMPlatform::set(std::uint8_t ch, float dc) {
  pwmcnt_t width = PWM_PERCENTAGE_TO_WIDTH(&PWMD1, dc * 10000.0f);
  pwmEnableChannel(&PWMD1, ch, width);
}
Exemple #22
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Starting PWM driver 1 and enabling the notifications.
   * GPIOA8 is programmed as PWM output (channel 1 of TIM1).
   */
  pwmStart(&PWMD1, &pwmcfg);
  pwmEnablePeriodicNotification(&PWMD1);
  palSetPadMode(GPIOA, GPIOA_ARD_D5, PAL_MODE_ALTERNATE(1));

  /*
   * Starting ICU driver 2.
   * GPIOA15 is programmed as ICU input (channel 1 of TIM2).
   */
  icuStart(&ICUD2, &icucfg);
  palSetPadMode(GPIOA, GPIOA_ARD_D9, PAL_MODE_ALTERNATE(1));

  /*
   * GPIOI1 is programmed as output (board LED).
   */
  palClearPad(GPIOI, GPIOI_ARD_D13);
  palSetPadMode(GPIOI, GPIOI_ARD_D13, PAL_MODE_OUTPUT_PUSHPULL);
  chThdSleepMilliseconds(1000);

  /*
   * Starting ICU capture and enabling the notifications.
   */
  icuStartCapture(&ICUD2);
  icuEnableNotifications(&ICUD2);

  /*
   * Normal main() thread activity, various PWM patterns are generated
   * cyclically, if the ICU input is connected to the PWM output the
   * board LED mirrors the PWM output.
   */
  while (true) {
    /*
     * Starts the PWM channel 0 using 75% duty cycle.
     */
    pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
    pwmEnableChannelNotification(&PWMD1, 0);
    chThdSleepMilliseconds(5000);

    /*
     * Changes the PWM channel 0 to 50% duty cycle.
     */
    pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
    chThdSleepMilliseconds(5000);

    /*
     * Changes the PWM channel 0 to 25% duty cycle.
     */
    pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
    chThdSleepMilliseconds(5000);

    /*
     * Changes PWM period to half second the duty cycle becomes 50%
     * implicitly.
     */
    pwmChangePeriod(&PWMD1, 5000);
    chThdSleepMilliseconds(5000);

    /*
     * Disables channel 0.
     */
    pwmDisableChannel(&PWMD1, 0);
  }
}
Exemple #23
0
static THD_FUNCTION(ThreadRecord, arg)
{
    (void)arg;
    chRegSetThreadName("Recording");
    uint16_t duty = 0;
    uint8_t result = 0;
    uint8_t count = 0;
    bool debounce = false;
    bool indicator = false;

    /* Load settings from EE first */
    if (readSettingsFromEE() != 0)
    {
        pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 10000));
        writeSettingsToEE();
        chThdSleepMilliseconds(3000);
    }

    if (readTablesFromEE() != 0)
    {
        pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 0));
        chThdSleepMilliseconds(1000);
        pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 10000));
        chThdSleepMilliseconds(3000);
    }

    while (true)
    {
        if (palReadPad(PORT_BUTTON1, PAD_BUTTON1) == PAL_LOW)
        {
            if (!debounce)
            {
                pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 10000));
                count++;
                indicator = true;
            }
            else {
                count = 0;
                indicator = false;
            }
        }
        else
        {
            count = 0;
            debounce = false;
            indicator = false;
        }

        if (count >= 2 && !debounce)
        {
            /* Toggle Record mode */
            settings.functions ^= FUNC_RECORD;
            writeSettingsToEE();
            readSettingsFromEE();
            debounce = true;

            pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 0));
            chThdSleepMilliseconds(150);
            pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 10000));
            chThdSleepMilliseconds(150);
            pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 0));
            chThdSleepMilliseconds(150);
            pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 10000));
            chThdSleepMilliseconds(150);
            pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, 0));
        }

        if (settings.functions & FUNC_RECORD)
        {
            /* Record tables */
            result = writeTablesToEE();
            if (result == 0)
            {
                duty = duty == 0 ? 10000 : 0;
            }
            else
            {
                duty = duty == 0 ? 1000 : 0;
            }
        }
        else
        {
            duty = 0;
        }

        if (!indicator)
            pwmEnableChannel(&PWMD_LED1, CHN_LED1, PWM_PERCENTAGE_TO_WIDTH(&PWMD_LED1, duty));

        chThdSleepMilliseconds(500);
    }
   return;
}
Exemple #24
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * LED initially off.
   */
  palSetPad(IOPORT3, GPIOC_LED);

  /*
   * Initializes the PWM driver 1 and ICU driver 4.
   */
  pwmStart(&PWMD1, &pwmcfg);
  palSetPadMode(IOPORT1, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  icuStart(&ICUD4, &icucfg);
  icuEnable(&ICUD4);
  chThdSleepMilliseconds(2000);

  /*
   * Starts the PWM channel 0 using 75% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 50% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 25% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes PWM period to half second the duty cycle becomes 50%
   * implicitly.
   */
  pwmChangePeriod(&PWMD1, 5000);
  chThdSleepMilliseconds(5000);

  /*
   * Disables channel 0 and stops the drivers.
   */
  pwmDisableChannel(&PWMD1, 0);
  pwmStop(&PWMD1);
  icuDisable(&ICUD4);
  icuStop(&ICUD4);
  palSetPad(IOPORT3, GPIOC_LED);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
  return 0;
}
Exemple #25
0
extern void bldcStart(void) {
  // The PWM output generator channel.
  pwmEnableChannel (&PWMD1, PWM_PULSE0_CH, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, bldc.dutyCycle));
}
Exemple #26
0
int main(void)
{

	halInit();
	chSysInit();

	//kruciální řádky - odpojit jtag; nechat jenom swd - sou na nem piny pro SPI1
	//premapovat SPI1 na PB3;4;5
	RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
	AFIO->MAPR |= AFIO_MAPR_SPI1_REMAP;
	AFIO->MAPR |= 0b010 << 24;

#if 1
	spiStart(&SPID1, &config);
	palSetPadMode(config.ssport, config.sspad, PAL_MODE_OUTPUT_PUSHPULL);
	palSetPadMode(SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_MODE);
	palSetPadMode(SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_MODE);
	palSetPadMode(SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_MODE);

#else
	palSetPadMode(config.ssport, config.sspad, PAL_MODE_OUTPUT_PUSHPULL);
	palSetPadMode(SPI_SCK_PORT, SPI_SCK_PIN, PAL_MODE_OUTPUT_PUSHPULL);
	palSetPadMode(SPI_MISO_PORT, SPI_MISO_PIN, PAL_MODE_OUTPUT_PUSHPULL);
	palSetPadMode(SPI_MOSI_PORT, SPI_MOSI_PIN, PAL_MODE_OUTPUT_PUSHPULL);

	palClearPad(config.ssport, config.sspad);
	palClearPad(SPI_SCK_PORT, SPI_SCK_PIN);
	palClearPad(SPI_MISO_PORT, SPI_MISO_PIN);
	palClearPad(SPI_MOSI_PORT, SPI_MOSI_PIN);

	palSetPad(config.ssport, config.sspad);
	palClearPad(config.ssport, config.sspad);
	palClearPad(SPI_SCK_PORT, SPI_SCK_PIN);
	palClearPad(SPI_MISO_PORT, SPI_MISO_PIN);
	palClearPad(SPI_MOSI_PORT, SPI_MOSI_PIN);
	palSetPad(SPI_SCK_PORT, SPI_SCK_PIN);
	palClearPad(config.ssport, config.sspad);
	palClearPad(SPI_SCK_PORT, SPI_SCK_PIN);
	palClearPad(SPI_MISO_PORT, SPI_MISO_PIN);
	palClearPad(SPI_MOSI_PORT, SPI_MOSI_PIN);
	palSetPad(SPI_MISO_PORT, SPI_MISO_PIN);
	palClearPad(config.ssport, config.sspad);
	palClearPad(SPI_SCK_PORT, SPI_SCK_PIN);
	palClearPad(SPI_MISO_PORT, SPI_MISO_PIN);
	palClearPad(SPI_MOSI_PORT, SPI_MOSI_PIN);
	palSetPad(SPI_MOSI_PORT, SPI_MOSI_PIN);
	palClearPad(config.ssport, config.sspad);
	palClearPad(SPI_SCK_PORT, SPI_SCK_PIN);
	palClearPad(SPI_MISO_PORT, SPI_MISO_PIN);
	palClearPad(SPI_MOSI_PORT, SPI_MOSI_PIN);

	//
#endif

	palSetPadMode(TEST_LED_PORT, TEST_LED_PIN, PAL_MODE_INPUT);
	palSetPadMode(TEST_LED_PORT2, TEST_LED_PIN2, PAL_MODE_OUTPUT_PUSHPULL);

	palClearPad(TEST_LED_PORT, TEST_LED_PIN);
	palClearPad(TEST_LED_PORT2, TEST_LED_PIN2);

	s1.Register();

	palSetPadMode(GPIOA, 2, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
//	palSetPad(GPIOA,2);
	//DBGMCU->CR |= DBGMCU_CR_DBG_TIM2_STOP;
	pwmStart(&PWMD2, &pwmcfg);
	pwmEnableChannel(&PWMD2,2,PWM_PERCENTAGE_TO_WIDTH(&PWMD2,5000));

	//setup watchdog
	DBGMCU->CR |= DBGMCU_CR_DBG_IWDG_STOP;
	IWDG->KR = 0x5555;
	IWDG->PR = 6;
	IWDG->RLR = 0xFFF;
	IWDG->KR = 0xCCCC;

#if 1
	ser.Init();

	rf.begin();
	rf.enableAckPayload();
	rf.enableDynamicPayloads();
	for (int i = 0; i < 5; i++)
		rf.openReadingPipe(i, pipe + i);
	rf.startListening();

#endif
	while (TRUE)
	{
		Scheduler::Play();
		ser.Loop();
		sysTime = chTimeNow();
	}

	return 1;
}
Exemple #27
0
/*
 * Application entry point.
 */
int main(void) {

  /* Initialization of all the imported components in the order specified in
	 the application wizard. The function is generated automatically.*/
  componentsInit();

  palClearPad(PORT11, P11_LED4);

  /*
   * Initializes the PWM driver 8 and ICU driver 1.
   * PIN80 is the PWM output.
   * PIN63 is the ICU input.
   * The two pins have to be externally connected together.
   */

  /* Sets PIN63 alternative function.*/
  SIU.PCR[179].R = 0b0000011000001100;

  /* Sets PIN65 alternative function.*/
  SIU.PCR[181].R = 0b0000010100001100;

  icuStart(&ICUD2, &icucfg);
  icuEnable(&ICUD2);
  pwmStart(&PWMD1, &pwmcfg);

  chThdSleepMilliseconds(2000);

  /*
   * Starts the PWM channel 0 using 75% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 50% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 25% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes PWM period and the PWM channel 0 to 50% duty cycle.
   */
  pwmChangePeriod(&PWMD1, 25000);
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Disables PWM channel 0 and stops the drivers.
   */
  pwmDisableChannel(&PWMD1, 0);
  pwmStop(&PWMD1);

  /*
   * Disables and stops the ICU drivers.
   */

  icuDisable(&ICUD2);
  icuStop(&ICUD2);

  palClearPad(PORT11, P11_LED3);
  palClearPad(PORT11, P11_LED4);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
  return 0;
}
Exemple #28
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Initializes the PWM driver 2 and ICU driver 3.
   * GPIOA15 is the PWM output.
   * GPIOC6 is the ICU input.
   * The two pins have to be externally connected together.
   */
  pwmStart(&PWMD2, &pwmcfg);
  palSetPadMode(GPIOA, 15, PAL_MODE_ALTERNATE(1));
  icuStart(&ICUD3, &icucfg);
  palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2));
  icuEnable(&ICUD3);
  chThdSleepMilliseconds(2000);

  /*
   * Starts the PWM channel 0 using 75% duty cycle.
   */
  pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 7500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 50% duty cycle.
   */
  pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 25% duty cycle.
   */
  pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 2500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes PWM period to half second the duty cycle becomes 50%
   * implicitly.
   */
  pwmChangePeriod(&PWMD2, 5000);
  chThdSleepMilliseconds(5000);

  /*
   * Disables channel 0 and stops the drivers.
   */
  pwmDisableChannel(&PWMD2, 0);
  pwmStop(&PWMD2);
  icuDisable(&ICUD3);
  icuStop(&ICUD3);
  palClearPad(GPIOE, GPIOE_LED4_BLUE);
  palClearPad(GPIOE, GPIOE_LED9_BLUE);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
  return 0;
}
Exemple #29
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Initializes the PWM driver 1 and ICU driver 2.
   * GPIOE9 is the PWM output.
   * GPIOA0 is the ICU input.
   * The two pins have to be externally connected together.
   */
  pwmStart(&PWMD1, &pwmcfg);
  pwmEnablePeriodicNotification(&PWMD1);
  icuStart(&ICUD2, &icucfg);
  icuStartCapture(&ICUD2);
  icuEnableNotifications(&ICUD2);
  chThdSleepMilliseconds(2000);

  /*
   * Starts the PWM channel 0 using 75% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
  pwmEnableChannelNotification(&PWMD1, 0);
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 50% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  chThdSleepMilliseconds(5000);

  /*
   * Changes the PWM channel 0 to 25% duty cycle.
   */
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  chThdSleepMilliseconds(5000);

  /*
   * Changes PWM period to half second the duty cycle becomes 50%
   * implicitly.
   */
  pwmChangePeriod(&PWMD1, 5000);
  chThdSleepMilliseconds(5000);

  /*
   * Disables channel 0 and stops the drivers.
   */
  pwmDisableChannel(&PWMD1, 0);
  pwmStop(&PWMD1);
  icuStopCapture(&ICUD2);
  icuStop(&ICUD2);
  palClearPad(GPIOB, GPIOB_LED1);
  palClearPad(GPIOB, GPIOB_LED2);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (true) {
    chThdSleepMilliseconds(500);
  }
  return 0;
}