From beaba463f719a22f86d26167f71465520cacd393 Mon Sep 17 00:00:00 2001 From: Sanrio Alvares Date: Mon, 20 Mar 2017 09:00:29 -0700 Subject: [PATCH] peripheralman.c: Added PWM Signed-off-by: Sanrio Alvares Signed-off-by: Brendan Le Foll --- include/mraa_adv_func.h | 1 + include/mraa_internal_types.h | 21 +++++++- src/peripheralman/peripheralman.c | 89 +++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) diff --git a/include/mraa_adv_func.h b/include/mraa_adv_func.h index d0aa603..1cf8da3 100644 --- a/include/mraa_adv_func.h +++ b/include/mraa_adv_func.h @@ -89,6 +89,7 @@ typedef struct { mraa_result_t (*pwm_init_pre) (int pin); mraa_result_t (*pwm_init_post) (mraa_pwm_context pwm); mraa_result_t (*pwm_period_replace) (mraa_pwm_context dev, int period); + mraa_result_t (*pwm_duty_cycle_replace) (mraa_pwm_context dev, int duty_cycle); float (*pwm_read_replace) (mraa_pwm_context dev); mraa_result_t (*pwm_write_replace) (mraa_pwm_context dev, float duty); mraa_result_t (*pwm_write_pre) (mraa_pwm_context dev, float percentage); diff --git a/include/mraa_internal_types.h b/include/mraa_internal_types.h index 1d403a1..9e0839d 100644 --- a/include/mraa_internal_types.h +++ b/include/mraa_internal_types.h @@ -45,6 +45,7 @@ #define MAX_I2C_BUS_COUNT 12 #define MAX_SPI_BUS_COUNT 12 #define MAX_UART_COUNT 6 +#define MAX_PWM_COUNT 6 // general status failures for internal functions @@ -187,6 +188,9 @@ struct _pwm { mraa_boolean_t owner; /**< Owner of pwm context*/ mraa_adv_func_t* advance_func; /**< override function table */ /*@}*/ +#ifdef PERIPHERALMAN + APwmDevice *bpwm; +#endif }; /** @@ -369,6 +373,16 @@ typedef struct { /*@}*/ } mraa_uart_dev_t; +/** + * A Structure representing a pwm device. + */ +typedef struct { + /*@{*/ + unsigned int index; /**< ID as exposed in the system */ + char* device_path; /**< To store "/dev/pwm" for example */ + /*@}*/ +} mraa_pwm_dev_t; + /** * A Structure representing a platform/board. */ @@ -386,10 +400,13 @@ typedef struct _board_t { unsigned int def_spi_bus; /**< Position in array of defult spi bus */ unsigned int adc_raw; /**< ADC raw bit value */ unsigned int adc_supported; /**< ADC supported bit value */ - unsigned int def_uart_dev; /**< Position in array of defult uart */ - int uart_dev_count; /**< Usable spi Count */ + unsigned int def_uart_dev; /**< Position in array of default uart */ + unsigned int def_pwm_dev; /**< Position in array of default pwm */ + int uart_dev_count; /**< Usable uart Count */ mraa_uart_dev_t uart_dev[MAX_UART_COUNT]; /**< Array of UARTs */ mraa_boolean_t no_bus_mux; /**< i2c/spi/adc/pwm/uart bus muxing setup not required */ + int pwm_dev_count; /**< Usable pwm Count */ + mraa_pwm_dev_t pwm_dev[MAX_PWM_COUNT]; /**< Array of PWMs */ int pwm_default_period; /**< The default PWM period is US */ int pwm_max_period; /**< Maximum period in us */ int pwm_min_period; /**< Minimum period in us */ diff --git a/src/peripheralman/peripheralman.c b/src/peripheralman/peripheralman.c index 3475b67..cb45f36 100644 --- a/src/peripheralman/peripheralman.c +++ b/src/peripheralman/peripheralman.c @@ -38,6 +38,77 @@ char **spi_busses = NULL; int spi_busses_count = 0; char **uart_devices = NULL; int uart_busses_count = 0; +char **pwm_devices = NULL; +int pwm_dev_count = 0; + +static mraa_pwm_context +mraa_pman_pwm_init_replace(int pin) +{ + mraa_pwm_context dev; + if (APeripheralManagerClient_openPwm(client, pwm_devices[pin], &dev->bpwm) != 0) { + APwmDevice_delete(dev->bpwm); + return NULL; + } + + return dev; +} + +static mraa_result_t +mraa_pman_pwm_period_replace(mraa_pwm_context dev, int period) +{ + if (!dev) { + syslog(LOG_ERR, "pwm: stop: context is NULL"); + return 0; + } + + if (APwm_setFrequencyHz(dev->bpwm, period) != 0) { + return 0; + } + + return MRAA_SUCCESS; +} + +static mraa_result_t +mraa_pman_pwm_duty_cycle_replace(mraa_pwm_context dev, int duty_cycle) +{ + if (!dev) { + syslog(LOG_ERR, "pwm: stop: context is NULL"); + return 0; + } + + if (APwm_setDutyCycle(dev->bpwm, duty_cycle) != 0) { + return 0; + } + + return MRAA_SUCCESS; +} + +static mraa_result_t +mraa_pman_pwm_enable_replace(mraa_pwm_context dev, int enable) +{ + if (!dev) { + syslog(LOG_ERR, "pwm: stop: context is NULL"); + return 0; + } + + if (APwm_setEnabled(dev->bpwm, enable) != 0) { + return 0; + } + + return MRAA_SUCCESS; +} + +static float +mraa_pman_pwm_read_replace(mraa_pwm_context dev) +{ + return -MRAA_ERROR_FEATURE_NOT_SUPPORTED; +} + +static mraa_result_t +mraa_pman_pwm_write_replace(mraa_pwm_context dev, fload duty) +{ + return -MRAA_ERROR_FEATURE_NOT_SUPPORTED; +} static mraa_result_t mraa_pman_uart_init_raw_replace(mraa_uart_context dev, const char* path) @@ -676,6 +747,7 @@ mraa_peripheralman_plat_init() i2c_busses = APeripheralManagerClient_listI2cBuses(client, &i2c_busses_count); spi_busses = APeripheralManagerClient_listSpiBuses(client, &spi_busses_count); uart_devices = APeripheralManagerClient_listUartDevices(client, &uart_busses_count); + pwm_devices = APeripheralManagerClient_listPwmDevices(client, &pwm_dev_count); b->platform_name = "peripheralmanager"; // query this from peripheral manager? @@ -690,9 +762,14 @@ mraa_peripheralman_plat_init() b->i2c_bus_count = i2c_busses_count; b->spi_bus_count = spi_busses_count; b->uart_dev_count = uart_busses_count; + b->pwm_dev_count = pwm_dev_count; + b->pwm_default_period = 5000; + b->pwm_max_period = 218453; + b->pwm_min_period = 1; b->def_i2c_bus = 0; b->def_spi_bus = 0; b->def_uart_dev = 0; + b->def_pwm_dev = 0; b->pins = (mraa_pininfo_t*) calloc(b->phy_pin_count, sizeof(mraa_pininfo_t)); if (b->pins == NULL) { @@ -727,6 +804,11 @@ mraa_peripheralman_plat_init() b->spi_bus[i].cs = -1; } + //Updating PWM structure + for (i = 0; i < pwm_dev_count; i++) { + b->pwm_dev[i].index = i; + } + b->adv_func = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t)); if (b->adv_func == NULL) { free(b->pins); @@ -780,6 +862,12 @@ mraa_peripheralman_plat_init() b->adv_func->uart_write_replace = &mraa_pman_uart_write_replace; b->adv_func->uart_read_replace = &mraa_pman_uart_read_replace; + b->adv_func->pwm_init_replace = &mraa_pman_pwm_init_replace; + b->adv_func->pwm_period_replace = &mraa_pman_pwm_period_replace; + b->adv_func->pwm_duty_cycle_replace = &mraa_pman_pwm_duty_cycle_replace; + b->adv_func->pwm_enable_replace = &mraa_pman_pwm_enable_replace; + b->adv_func->pwm_read_replace = &mraa_pman_pwm_read_replace; + b->adv_func->pwm_write_replace = &mraa_pman_pwm_write_replace; return b; } @@ -809,6 +897,7 @@ free_resources(char ***resources, int count) void pman_mraa_deinit() { + free_resources(&pwm_devices, pwm_dev_count); free_resources(&uart_devices, uart_busses_count); free_resources(&spi_busses, spi_busses_count); free_resources(&i2c_busses, i2c_busses_count);