Beispiel #1
0
void pwmout_init(pwmout_t* obj, PinName pin) {
    // determine the channel
    PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
    MBED_ASSERT(pwm != (PWMName)NC);

    // power on
    CPGSTBCR3 &= ~(1<<0);

    obj->pwm = pwm;
    if (((uint32_t)PORT[obj->pwm] & 0x00000010) != 0) {
        obj->ch  = 2;
        PWMPWPR_2_BYTE_L = 0x00;
    } else {
        obj->ch  = 1;
        PWMPWPR_1_BYTE_L = 0x00;
    }

    // Wire pinout
    pinmap_pinout(pin, PinMap_PWM);

    // default to 491us: standard for servos, and fine for e.g. brightness control
    pwmout_write(obj, 0);
    if ((obj->ch == 2) && (init_period_ch2 == 0)) {
        pwmout_period_us(obj, 491);
        init_period_ch2 = 1;
    }
    if ((obj->ch == 1) && (init_period_ch1 == 0)) {
        pwmout_period_us(obj, 491);
        init_period_ch1 = 1;
    }
}
void pwmout_init(pwmout_t* obj, PinName pin) {
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);

    if (obj->pwm == (PWMName)NC) {
        error("PWM error: pinout mapping failed.");
    }

    // Enable TIM clock
    if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
    if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
    if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
    if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE();
    if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE();
    if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE();
    if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE();

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #3
0
void pwmout_init(pwmout_t* obj, PinName pin)
{
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);

    if (obj->pwm == (PWMName)NC) {
        error("PWM error: pinout mapping failed.");
    }

    // Enable TIM clock
#if defined(TIM1_BASE)
    if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
#endif
#if defined(TIM2_BASE)
    if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
#endif
    if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
    if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE();
    if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE();
    if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE();
    if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE();

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #4
0
void pwmout_init(pwmout_t* obj, PinName pin)
{
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
    MBED_ASSERT(obj->pwm != (PWMName)NC);

    // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
    uint32_t function = pinmap_function(pin, PinMap_PWM);
    MBED_ASSERT(function != (uint32_t)NC);
    obj->channel = STM_PIN_CHANNEL(function);
    obj->inverted = STM_PIN_INVERTED(function);

    // Enable TIM clock
#if defined(TIM1_BASE)
    if (obj->pwm == PWM_1) __HAL_RCC_TIM1_CLK_ENABLE();
#endif
#if defined(TIM2_BASE)
    if (obj->pwm == PWM_2) __HAL_RCC_TIM2_CLK_ENABLE();
#endif
#if defined(TIM3_BASE)
    if (obj->pwm == PWM_3) __HAL_RCC_TIM3_CLK_ENABLE();
#endif
#if defined(TIM4_BASE)
    if (obj->pwm == PWM_4) __HAL_RCC_TIM4_CLK_ENABLE();
#endif
#if defined(TIM5_BASE)
    if (obj->pwm == PWM_5) __HAL_RCC_TIM5_CLK_ENABLE();
#endif
#if defined(TIM8_BASE)
    if (obj->pwm == PWM_8) __HAL_RCC_TIM8_CLK_ENABLE();
#endif
#if defined(TIM9_BASE)
    if (obj->pwm == PWM_9) __HAL_RCC_TIM9_CLK_ENABLE();
#endif
#if defined(TIM10_BASE)
    if (obj->pwm == PWM_10) __HAL_RCC_TIM10_CLK_ENABLE();
#endif
#if defined(TIM11_BASE)
    if (obj->pwm == PWM_11) __HAL_RCC_TIM11_CLK_ENABLE();
#endif
#if defined(TIM12_BASE)
    if (obj->pwm == PWM_12) __HAL_RCC_TIM12_CLK_ENABLE();
#endif
#if defined(TIM13_BASE)
    if (obj->pwm == PWM_13) __HAL_RCC_TIM13_CLK_ENABLE();
#endif
#if defined(TIM14_BASE)
    if (obj->pwm == PWM_14) __HAL_RCC_TIM14_CLK_ENABLE();
#endif

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;
    obj->prescaler = 1;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #5
0
void pwmout_init(pwmout_t* obj, PinName pin) {  
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
  
    if (obj->pwm == (PWMName)NC) {
        error("PWM pinout mapping failed");
    }
    
    // Enable TIM clock
    if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    if (obj->pwm == PWM_4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    if (obj->pwm == PWM_5) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
    if (obj->pwm == PWM_9) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
    if (obj->pwm == PWM_10) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
    if (obj->pwm == PWM_11) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE);
    
    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);
    
    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;
    
    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #6
0
void pwmout_init(pwmout_t* obj, PinName pin)
{
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
    MBED_ASSERT(obj->pwm != (PWMName)NC);

    // Get the pin function and assign the used channel to the object
    uint32_t function = pinmap_function(pin, PinMap_PWM);
    MBED_ASSERT(function != (uint32_t)NC);
    obj->channel = STM_PIN_CHANNEL(function);
    obj->inverted = STM_PIN_INVERTED(function);

    // Enable TIM clock
    if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
#if defined(TIM3_BASE)
    if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();
#endif
    if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE();
#if defined(TIM22_BASE)
    if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE();
#endif

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #7
0
void pwmout_init(pwmout_t* obj, PinName pin) 
{
    uint32_t peripheral;
    u32 pwm_idx;
    u32 pin_sel;

    DBG_PWM_INFO("%s: Init PWM for pin(0x%x)\n", __FUNCTION__, pin);
    
    // Get the peripheral name from the pin and assign it to the object
    peripheral = pinmap_peripheral(pin, PinMap_PWM);

    if (unlikely(peripheral == NC)) {
        DBG_PWM_ERR("%s: Cannot find matched pwm for this pin(0x%x)\n", __FUNCTION__, pin);
        return;
    }

    pwm_idx = RTL_GET_PERI_IDX(peripheral);
    pin_sel = RTL_GET_PERI_SEL(peripheral);

    obj->pwm_idx = pwm_idx;
    obj->pin_sel = pin_sel;
    obj->period = 0;
    obj->pulse = 0;
    _memset((void *)&obj->pwm_hal_adp, 0, sizeof(HAL_PWM_ADAPTER));
    if (HAL_OK != HAL_Pwm_Init(&obj->pwm_hal_adp, pwm_idx, pin_sel)) {
        DBG_PWM_ERR("pwmout_init Err!\n");
        return;
    }
    pwmout_period_us(obj, 20000); // 20 ms per default
    HAL_Pwm_Enable(&obj->pwm_hal_adp);
}
Beispiel #8
0
void pwmout_init(pwmout_t* obj, PinName pin) {
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);

    if (obj->pwm == (PWMName)NC) {
        error("PWM pinout mapping failed");
    }

    // Enable TIM clock
    if (obj->pwm == TIM_3)  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    if (obj->pwm == TIM_14) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
    if (obj->pwm == TIM_15) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);
    if (obj->pwm == TIM_16) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
    if (obj->pwm == TIM_17) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);
    //pin_mode(pin, PullUp);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #9
0
void pwmout_init(pwmout_t* obj, PinName pin)
{
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
    MBED_ASSERT(obj->pwm != (PWMName)NC);

    // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object
    uint32_t function = pinmap_function(pin, PinMap_PWM);
    MBED_ASSERT(function != (uint32_t)NC);
    obj->channel = STM_PIN_CHANNEL(function);
    obj->inverted = STM_PIN_INVERTED(function);

    // Enable TIM clock
    if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
    if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
    if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE();

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;
    obj->prescaler = 1;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #10
0
uint8_t AmebaServo::attach(int pin, int min, int max)
{
    pinRemoveMode( pin );
    analogWrite( pin, 0 );
    pwmout_period_us( (pwmout_t *)gpio_pin_struct[pin], 20000);

    servoPin = pin;
    this->min  = min;
    this->max  = max;

    return 0;
}
Beispiel #11
0
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
    timer_mr tid = pwm_timer_map[obj->pwm];
    LPC_TMR_TypeDef *timer = Timers[tid.timer];
    uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1));
    
    timer->TCR = TCR_RESET;
    if (t_on > timer->MR3) {
        pwmout_period_us(obj, us);
    }
    uint32_t t_off = timer->MR3 - t_on;
    timer->MR[tid.mr] = t_off;
    timer->TCR = TCR_CNT_EN;
}
Beispiel #12
0
void pwmout_period_ms(pwmout_t *obj, int ms)
{
    // reught saturation < 0, quasi-max>
    if (ms > MAX_PWM_PERIOD_MS)
    {
        ms = MAX_PWM_PERIOD_MS;
    }
    else if (ms < 0)
    {
        ms = 0; // f. pwmout_period_us will set period to min. value
    }
    
    int us = ms * 1000;
    
    pwmout_period_us(obj, us);
}
Beispiel #13
0
void pwmout_period(pwmout_t *obj, float seconds)
{
    // raught saturation < 0, quasi-max>
    if (seconds > MAX_PWM_PERIOD_S)
    {
        seconds = MAX_PWM_PERIOD_S;
    }
    else if (seconds < 0)
    {
        seconds = 0; // f. pwmout_period_us will set period to min. value
    }
    
    int us = seconds * 1000000;
    
    pwmout_period_us(obj, us);
}
Beispiel #14
0
//int main_app(IN u16 argc, IN u8 *argv[])
void main(void)
{
    int i;
    
    for (i=0; i<4; i++) {
        pwmout_init(&pwm_led[i], pwm_led_pin[i]);
        pwmout_period_us(&pwm_led[i], PWM_PERIOD);
    }

    while (1) {
#if USE_FLOAT
        for (i=0; i<4; i++) {
            pwmout_write(&pwm_led[i], pwms[i]);

            pwms[i] += steps[i];
            if (pwms[i] >= 1.0) {
                steps[i] = -PWM_STEP;
                pwms[i] = 1.0;
            }

            if (pwms[i] <= 0.0) {
                steps[i] = PWM_STEP;
                pwms[i] = 0.0;
            }
        }
#else        
        for (i=0; i<4; i++) {
            pwmout_pulsewidth_us(&pwm_led[i], pwms[i]);

            pwms[i] += steps[i];
            if (pwms[i] >= PWM_PERIOD) {
                steps[i] = -PWM_STEP;
                pwms[i] = PWM_PERIOD;
            }

            if (pwms[i] <= 0) {
                steps[i] = PWM_STEP;
                pwms[i] = 0;
            }
        }
#endif        
//        wait_ms(20);
//        RtlMsleepOS(25);
		pwm_delay();
    }
}
Beispiel #15
0
void pwmout_init(pwmout_t* obj, PinName pin)
{
    // Get the peripheral name from the pin and assign it to the object
    obj->PWM_CHx = (PWM_CHn_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);

    if (obj->PWM_CHx == (PWM_CHn_TypeDef *)NC) {
        error("PWM error: pinout mapping failed.");
    }

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);
    
    GetSystemClock();

    obj->pin = pin;
    
    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #16
0
void pwmout_init(pwmout_t* obj, PinName pin) {
    // Get the peripheral name from the pin and assign it to the object
    obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
    MBED_ASSERT(obj->pwm != (PWMName)NC);

    // Enable TIM clock
    if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    if (obj->pwm == PWM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    if (obj->pwm == PWM_4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    // Configure GPIO
    pinmap_pinout(pin, PinMap_PWM);

    obj->pin = pin;
    obj->period = 0;
    obj->pulse = 0;

    pwmout_period_us(obj, 20000); // 20 ms per default
}
Beispiel #17
0
void pwmout_period(pwmout_t *obj, float seconds)
{
    pwmout_period_us(obj, seconds * 1000000.0f);
}
Beispiel #18
0
//******************************************************************************
void pwmout_period(pwmout_t* obj, float seconds)
{
    pwmout_period_us(obj, (int)(seconds * 1000000.0));
}
Beispiel #19
0
void pwmout_init(pwmout_t* obj, PinName pin) {
    // determine the channel
    PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
    MBED_ASSERT(pwm != (PWMName)NC);

    if (pwm >= MTU2_PWM_OFFSET) {
        /* PWM by MTU2 */
        int tmp_pwm;
        
        // power on
        CPGSTBCR3 &= ~(CPG_STBCR3_BIT_MSTP33);
        
        obj->pwm = pwm;
        tmp_pwm = (int)(obj->pwm - MTU2_PWM_OFFSET);
        if (((uint32_t)MTU2_PORT[tmp_pwm] & 0x00000040) == 0x00000040) {
            obj->ch  = 4;
            MTU2TOER |= 0x36;
        } else if (((uint32_t)MTU2_PORT[tmp_pwm] & 0x00000030) == 0x00000030) {
            obj->ch  = 3;
            MTU2TOER |= 0x09;
        } else if (((uint32_t)MTU2_PORT[tmp_pwm] & 0x00000020) == 0x00000020) {
            obj->ch  = 2;
        } else if (((uint32_t)MTU2_PORT[tmp_pwm] & 0x00000010) == 0x00000010) {
            obj->ch  = 1;
        } else {
            obj->ch  = 0;
        }
        // Wire pinout
        pinmap_pinout(pin, PinMap_PWM);

        int bitmask = 1 << (pin  & 0xf);

        *PMSR(PINGROUP(pin)) = (bitmask << 16) | 0;

        // default duty 0.0f
        pwmout_write(obj, 0);
        if (init_mtu2_period_ch[obj->ch] == 0) {
            // default period 1ms
            pwmout_period_us(obj, 1000);
            init_mtu2_period_ch[obj->ch] = 1;
        }
    } else {
        /* PWM */
        // power on
        CPGSTBCR3 &= ~(CPG_STBCR3_BIT_MSTP30);

        obj->pwm = pwm;
        if (((uint32_t)PORT[obj->pwm] & 0x00000010) == 0x00000010) {
            obj->ch  = 2;
            PWMPWPR_2_BYTE_L = 0x00;
        } else {
            obj->ch  = 1;
            PWMPWPR_1_BYTE_L = 0x00;
        }

        // Wire pinout
        pinmap_pinout(pin, PinMap_PWM);

        // default to 491us: standard for servos, and fine for e.g. brightness control
        pwmout_write(obj, 0);
        if ((obj->ch == 2) && (init_period_ch2 == 0)) {
            pwmout_period_us(obj, 491);
            init_period_ch2 = 1;
        }
        if ((obj->ch == 1) && (init_period_ch1 == 0)) {
            pwmout_period_us(obj, 491);
            init_period_ch1 = 1;
        }
    }
}
Beispiel #20
0
void pwmout_period_ms(pwmout_t *obj, int ms)
{
    pwmout_period_us(obj, ms * 1000);
}
Beispiel #21
0
//******************************************************************************
void pwmout_init(pwmout_t* obj, PinName pin)
{
    // Make sure the pin is free for GPIO use
    unsigned int port = (unsigned int)pin >> PORT_SHIFT;
    unsigned int port_pin = (unsigned int)pin & ~(0xFFFFFFFF << PORT_SHIFT);
    MBED_ASSERT(MXC_GPIO->free[port] & (0x1 << port_pin));

    int i = 0;
    PinMap pwm = PinMap_PWM[0];

    // Check if there is a pulse train already active on this port
    int pin_func = (MXC_GPIO->func_sel[port] & (0xF << (port_pin*4))) >> (port_pin*4);
    if((pin_func > 0) && (pin_func < 4)) {
        // Search through PinMap_PWM to find the active PT
        while(pwm.pin != (PinName)NC) {
            if((pwm.pin == pin) && (pwm.function == pin_func)) {
                break;
            }
            pwm = PinMap_PWM[++i];
        }

    } else {
       // Search through PinMap_PWM to find an available PT
        int i = 0;
        while(pwm.pin != (PinName)NC && (i > -1)) {
            pwm = PinMap_PWM[i++];
            if(pwm.pin == pin) {
                // Check each instance of PT
                while(1) {
                    // Check to see if this PT instance is already in use
                    if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length & 
                        MXC_F_PT_RATE_LENGTH_MODE)) {
                        i = -1;
                        break;
                    } 

                    // If all instances are in use, overwrite the last 
                    pwm = PinMap_PWM[++i];
                    if(pwm.pin != pin) {
                        pwm = PinMap_PWM[--i];
                        i = -1; 
                        break;
                    }

                }
            }
        } 
    }

    // Make sure we found an available PWM generator
    MBED_ASSERT(pwm.pin != (PinName)NC);

    // Disable all pwm output
    MXC_PTG->ctrl = 0;

    // Enable the clock
    MXC_CLKMAN->clk_ctrl_2_pt = MXC_E_CLKMAN_CLK_SCALE_ENABLED;

    // Set the drive mode to normal
    MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));

    // Set the obj pointer to the propper PWM instance
    obj->pwm = (mxc_pt_regs_t*)pwm.peripheral;

    // Initialize object period and pulse width
    obj->period = -1; 
    obj->pulse_width = -1;

    // Disable the output
    obj->pwm->train = 0x0;
    obj->pwm->rate_length = 0x0;

    // Configure the pin
    pin_mode(pin, (PinMode)PullNone);
    pin_function(pin, pwm.function);

    // default to 20ms: standard for servos, and fine for e.g. brightness control
    pwmout_period_us(obj, 20000);
    pwmout_write    (obj, 0);

    // Enable the global pwm
    MXC_PTG->ctrl = MXC_F_PT_CTRL_ENABLE_ALL;
}