コード例 #1
0
/*
 * TODO: Find a solution to PWM with MBED. There is presumably a timer issue
 * with PWM/MBED RTOS. Currently PWM does not work correctly.
 */
extern "C" AJ_Status AJS_TargetIO_PinPWM(void* pinCtx, double dutyCycle, uint32_t freq)
{
    //TODO: Incorporate frequency
    AJ_Status status = AJ_OK;;
    GPIO* gpio = (GPIO*)pinCtx;
    uint8_t dc = (uint8_t)(dutyCycle * 255);

    if ((dc == 0) || (dc == 255)) {
        AJS_TargetIO_PinSet(pinCtx, dutyCycle);
        return AJ_OK;
    }
    if (!gpio->pwm.dutyCycle) {
        status = AllocPWM(gpio);
    }
    if (status == AJ_OK) {
        //AJ_Printf("Duty float = %f\n", dcfloat);
        gpio->pwm.dutyCycle = dc;
        AJS_SetHWTimer(freq);
        //gpio->pwm.object->write(dutyCycle);
    }
    return AJ_OK;
}
コード例 #2
0
ファイル: ajs_io.c プロジェクト: avernon/asl_distribution
static int NativeLevelSetter(duk_context* ctx)
{
    AJS_TargetIO_PinSet(PinCtxPtr(ctx), duk_require_int(ctx, 0));
    return 0;
}
コード例 #3
0
ファイル: io_gpio.c プロジェクト: avernon/asl_distribution
AJ_Status AJS_TargetIO_PinPWM(void* pinCtx, double dutyCycle, uint32_t freq)
{
    AJ_Status status;
    GPIO* gpio = (GPIO*)pinCtx;
    int8_t pwmId = pinInfo[gpio->pinId].pwmId;
    uint32_t period;
    uint32_t dutyNS;
    const char* dev;
    char val[16];

    AJ_InfoPrintf(("AJS_TargetIO_PinPWM(%d, %f, %d)\n", gpio->pinId, dutyCycle, freq));

    if (pwmId < 0) {
        return AJ_ERR_INVALID;
    }
    dev = pinInfo[gpio->pinId].gpioDev;
    /*
     * Handle limit cases
     */
    if (dutyCycle == 0.0) {
        AJS_TargetIO_PinSet(pinCtx, 0);
        return AJ_OK;
    }
    if (dutyCycle == 1.0) {
        AJS_TargetIO_PinSet(pinCtx, 1);
        return AJ_OK;
    }
    /*
     * Check if we need to enable PWM
     */
    if (gpio->pwmPeriod == 0) {
        status = ExportIfNeeded(dev, pwmId, pwm_root);
        if (status != AJ_OK) {
            return status;
        }
        status = SetDeviceProp(dev, pwm_root, "enable", "1");
        if (status != AJ_OK) {
            return status;
        }
    }
    if (freq == 0) {
        /*
         * If we don't already have a value for period read the default from the device
         */
        if (gpio->pwmPeriod) {
            period = gpio->pwmPeriod;
        } else {
            char buf[16];
            size_t sz = sizeof(buf) - 1;
            status = GetDeviceProp(dev, pwm_root, "period", buf, &sz);
            if (status != AJ_OK) {
                return status;
            }
            buf[sz] = 0;
            period = strtoul(buf, NULL, 10);
            gpio->pwmPeriod = period;
        }
    } else {
        /*
         * Convert frequency to period time in nanoseconds
         */
        period = 1000000000 / freq;
        /*
         * Check if the period needs to be updated
         */
        if (period != gpio->pwmPeriod) {
            snprintf(val, sizeof(val), "%u", period);
            status = SetDeviceProp(dev, pwm_root, "period", val);
            if (status != AJ_OK) {
                return status;
            }
            gpio->pwmPeriod = period;
        }
    }
    dutyNS = (uint32_t)((double)period * dutyCycle);
    AJ_InfoPrintf(("period = %u duty_time = %u\n", period, dutyNS));
    snprintf(val, sizeof(val), "%u", dutyNS);
    return SetDeviceProp(dev, pwm_root, "duty_cycle", val);
}
コード例 #4
0
ファイル: io_gpio.c プロジェクト: avernon/asl_distribution
void AJS_TargetIO_PinToggle(void* pinCtx)
{
    GPIO* gpio = (GPIO*)pinCtx;
    AJS_TargetIO_PinSet(pinCtx, !gpio->value);
}