You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "pkarashchenko (via GitHub)" <gi...@apache.org> on 2023/08/11 23:49:25 UTC
[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #10172: battery:add goldfish battery driver
pkarashchenko commented on code in PR #10172:
URL: https://github.com/apache/nuttx/pull/10172#discussion_r1291891372
##########
drivers/power/battery/goldfish_battery.c:
##########
@@ -0,0 +1,330 @@
+/****************************************************************************
+ * drivers/power/battery/goldfish_battery.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/battery_gauge.h>
+#include <nuttx/power/battery_ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GOLDFISH_BATTERY_READ(data, addr) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr))
+#define GOLDFISH_BATTERY_WRITE(data, addr, x) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr) = (x))
+#define GOLDFISH_GAUGE "/dev/charge/goldfish_battery"
+
+/****************************************************************************
+ * Private type
+ ****************************************************************************/
+
+enum
+{
+ /* Status register */
+
+ BATTERY_INT_STATUS = 0x00,
+
+ /* Set this to enable IRQ */
+
+ BATTERY_INT_ENABLE = 0x04,
+ BATTERY_AC_ONLINE = 0x08,
+ BATTERY_STATUS = 0x0c,
+ BATTERY_HEALTH = 0x10,
+ BATTERY_PRESENT = 0x14,
+ BATTERY_CAPACITY = 0x18,
+ BATTERY_VOLTAGE = 0x1c,
+ BATTERY_TEMP = 0x20,
+ BATTERY_CHARGE_COUNTER = 0x24,
+ BATTERY_VOLTAGE_MAX = 0x28,
+ BATTERY_CURRENT_MAX = 0x2c,
+ BATTERY_CURRENT_NOW = 0x30,
+ BATTERY_CURRENT_AVG = 0x34,
+ BATTERY_CHARGE_FULL_UAH = 0x38,
+ BATTERY_CYCLE_COUNT = 0x40,
+ BATTERY_STATUS_CHANGED = 1u << 0,
+ AC_STATUS_CHANGED = 1u << 1,
+ BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
+};
+
+enum
+{
+ POWER_SUPPLY_STATUS_UNKNOWN,
+ POWER_SUPPLY_STATUS_CHARGING,
+ POWER_SUPPLY_STATUS_DISCHARGING,
+ POWER_SUPPLY_STATUS_NOT_CHARGING,
+ POWER_SUPPLY_STATUS_FULL,
+};
+
+struct goldfish_battery_data_s
+{
+ FAR void *reg_base;
+ int irq;
+ struct battery_gauge_dev_s battery;
Review Comment:
remove 2 leading spaces
##########
drivers/power/battery/goldfish_battery.c:
##########
@@ -0,0 +1,330 @@
+/****************************************************************************
+ * drivers/power/battery/goldfish_battery.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/battery_gauge.h>
+#include <nuttx/power/battery_ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GOLDFISH_BATTERY_READ(data, addr) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr))
+#define GOLDFISH_BATTERY_WRITE(data, addr, x) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr) = (x))
+#define GOLDFISH_GAUGE "/dev/charge/goldfish_battery"
+
+/****************************************************************************
+ * Private type
+ ****************************************************************************/
+
+enum
+{
+ /* Status register */
+
+ BATTERY_INT_STATUS = 0x00,
+
+ /* Set this to enable IRQ */
+
+ BATTERY_INT_ENABLE = 0x04,
+ BATTERY_AC_ONLINE = 0x08,
+ BATTERY_STATUS = 0x0c,
+ BATTERY_HEALTH = 0x10,
+ BATTERY_PRESENT = 0x14,
+ BATTERY_CAPACITY = 0x18,
+ BATTERY_VOLTAGE = 0x1c,
+ BATTERY_TEMP = 0x20,
+ BATTERY_CHARGE_COUNTER = 0x24,
+ BATTERY_VOLTAGE_MAX = 0x28,
+ BATTERY_CURRENT_MAX = 0x2c,
+ BATTERY_CURRENT_NOW = 0x30,
+ BATTERY_CURRENT_AVG = 0x34,
+ BATTERY_CHARGE_FULL_UAH = 0x38,
+ BATTERY_CYCLE_COUNT = 0x40,
+ BATTERY_STATUS_CHANGED = 1u << 0,
+ AC_STATUS_CHANGED = 1u << 1,
+ BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
+};
+
+enum
+{
+ POWER_SUPPLY_STATUS_UNKNOWN,
+ POWER_SUPPLY_STATUS_CHARGING,
+ POWER_SUPPLY_STATUS_DISCHARGING,
+ POWER_SUPPLY_STATUS_NOT_CHARGING,
+ POWER_SUPPLY_STATUS_FULL,
+};
+
+struct goldfish_battery_data_s
+{
+ FAR void *reg_base;
+ int irq;
+ struct battery_gauge_dev_s battery;
+ struct work_s work;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int goldfish_charger_online(FAR struct battery_gauge_dev_s *dev,
+ FAR bool *status);
+static int goldfish_battery_state(FAR struct battery_gauge_dev_s *dev,
+ FAR int *status);
+static int goldfish_battery_voltage(FAR struct battery_gauge_dev_s *dev,
+ FAR b16_t *value);
+static int goldfish_battery_capacity(FAR struct battery_gauge_dev_s *dev,
+ FAR b16_t *value);
+static int goldfish_battery_current(FAR struct battery_gauge_dev_s *dev,
+ FAR b16_t *value);
+static int goldfish_battery_temp(FAR struct battery_gauge_dev_s *dev,
+ FAR b8_t *value);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct battery_gauge_operations_s g_goldfish_gauge_ops =
+{
+ .online = goldfish_charger_online,
+ .state = goldfish_battery_state,
+ .voltage = goldfish_battery_voltage,
+ .current = goldfish_battery_current,
+ .capacity = goldfish_battery_capacity,
+ .temp = goldfish_battery_temp,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int goldfish_charger_online(FAR struct battery_gauge_dev_s *dev,
+ FAR bool *status)
+{
+ FAR struct goldfish_battery_data_s *priv =
+ container_of(dev, struct goldfish_battery_data_s, battery);
+
+ *status = GOLDFISH_BATTERY_READ(priv, BATTERY_AC_ONLINE) & 0x1;
+ return OK;
+}
+
+static int goldfish_battery_state(FAR struct battery_gauge_dev_s *dev,
+ FAR int *status)
+{
+ FAR struct goldfish_battery_data_s *priv =
+ container_of(dev, struct goldfish_battery_data_s, battery);
+ uint32_t regval;
+
+ regval = GOLDFISH_BATTERY_READ(priv, BATTERY_STATUS);
+
+ switch (regval)
+ {
+ case POWER_SUPPLY_STATUS_UNKNOWN:
+ *status = BATTERY_UNKNOWN;
+ break;
Review Comment:
remove 2 leading spaces
##########
drivers/power/battery/goldfish_battery.c:
##########
@@ -0,0 +1,330 @@
+/****************************************************************************
+ * drivers/power/battery/goldfish_battery.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/battery_gauge.h>
+#include <nuttx/power/battery_ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GOLDFISH_BATTERY_READ(data, addr) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr))
+#define GOLDFISH_BATTERY_WRITE(data, addr, x) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr) = (x))
+#define GOLDFISH_GAUGE "/dev/charge/goldfish_battery"
+
+/****************************************************************************
+ * Private type
+ ****************************************************************************/
+
+enum
+{
+ /* Status register */
+
+ BATTERY_INT_STATUS = 0x00,
+
+ /* Set this to enable IRQ */
+
+ BATTERY_INT_ENABLE = 0x04,
+ BATTERY_AC_ONLINE = 0x08,
+ BATTERY_STATUS = 0x0c,
+ BATTERY_HEALTH = 0x10,
+ BATTERY_PRESENT = 0x14,
+ BATTERY_CAPACITY = 0x18,
+ BATTERY_VOLTAGE = 0x1c,
+ BATTERY_TEMP = 0x20,
+ BATTERY_CHARGE_COUNTER = 0x24,
+ BATTERY_VOLTAGE_MAX = 0x28,
+ BATTERY_CURRENT_MAX = 0x2c,
+ BATTERY_CURRENT_NOW = 0x30,
+ BATTERY_CURRENT_AVG = 0x34,
+ BATTERY_CHARGE_FULL_UAH = 0x38,
+ BATTERY_CYCLE_COUNT = 0x40,
+ BATTERY_STATUS_CHANGED = 1u << 0,
+ AC_STATUS_CHANGED = 1u << 1,
+ BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
+};
+
+enum
+{
+ POWER_SUPPLY_STATUS_UNKNOWN,
+ POWER_SUPPLY_STATUS_CHARGING,
+ POWER_SUPPLY_STATUS_DISCHARGING,
+ POWER_SUPPLY_STATUS_NOT_CHARGING,
Review Comment:
remove 2 leading spaces
##########
drivers/power/battery/goldfish_battery.c:
##########
@@ -0,0 +1,330 @@
+/****************************************************************************
+ * drivers/power/battery/goldfish_battery.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/battery_gauge.h>
+#include <nuttx/power/battery_ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GOLDFISH_BATTERY_READ(data, addr) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr))
+#define GOLDFISH_BATTERY_WRITE(data, addr, x) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr) = (x))
+#define GOLDFISH_GAUGE "/dev/charge/goldfish_battery"
+
+/****************************************************************************
+ * Private type
+ ****************************************************************************/
+
+enum
+{
+ /* Status register */
+
+ BATTERY_INT_STATUS = 0x00,
Review Comment:
remove 2 leading spaces
##########
drivers/power/battery/goldfish_battery.c:
##########
@@ -0,0 +1,330 @@
+/****************************************************************************
+ * drivers/power/battery/goldfish_battery.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/battery_gauge.h>
+#include <nuttx/power/battery_ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GOLDFISH_BATTERY_READ(data, addr) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr))
+#define GOLDFISH_BATTERY_WRITE(data, addr, x) \
+ (*(FAR volatile uint32_t *)(data->reg_base + addr) = (x))
+#define GOLDFISH_GAUGE "/dev/charge/goldfish_battery"
+
+/****************************************************************************
+ * Private type
+ ****************************************************************************/
+
+enum
+{
+ /* Status register */
+
+ BATTERY_INT_STATUS = 0x00,
+
+ /* Set this to enable IRQ */
+
+ BATTERY_INT_ENABLE = 0x04,
+ BATTERY_AC_ONLINE = 0x08,
+ BATTERY_STATUS = 0x0c,
+ BATTERY_HEALTH = 0x10,
+ BATTERY_PRESENT = 0x14,
+ BATTERY_CAPACITY = 0x18,
+ BATTERY_VOLTAGE = 0x1c,
+ BATTERY_TEMP = 0x20,
+ BATTERY_CHARGE_COUNTER = 0x24,
+ BATTERY_VOLTAGE_MAX = 0x28,
+ BATTERY_CURRENT_MAX = 0x2c,
+ BATTERY_CURRENT_NOW = 0x30,
+ BATTERY_CURRENT_AVG = 0x34,
+ BATTERY_CHARGE_FULL_UAH = 0x38,
+ BATTERY_CYCLE_COUNT = 0x40,
+ BATTERY_STATUS_CHANGED = 1u << 0,
+ AC_STATUS_CHANGED = 1u << 1,
+ BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
+};
+
+enum
+{
+ POWER_SUPPLY_STATUS_UNKNOWN,
+ POWER_SUPPLY_STATUS_CHARGING,
+ POWER_SUPPLY_STATUS_DISCHARGING,
+ POWER_SUPPLY_STATUS_NOT_CHARGING,
+ POWER_SUPPLY_STATUS_FULL,
+};
+
+struct goldfish_battery_data_s
+{
+ FAR void *reg_base;
+ int irq;
+ struct battery_gauge_dev_s battery;
+ struct work_s work;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int goldfish_charger_online(FAR struct battery_gauge_dev_s *dev,
+ FAR bool *status);
+static int goldfish_battery_state(FAR struct battery_gauge_dev_s *dev,
+ FAR int *status);
+static int goldfish_battery_voltage(FAR struct battery_gauge_dev_s *dev,
+ FAR b16_t *value);
+static int goldfish_battery_capacity(FAR struct battery_gauge_dev_s *dev,
+ FAR b16_t *value);
+static int goldfish_battery_current(FAR struct battery_gauge_dev_s *dev,
+ FAR b16_t *value);
+static int goldfish_battery_temp(FAR struct battery_gauge_dev_s *dev,
+ FAR b8_t *value);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct battery_gauge_operations_s g_goldfish_gauge_ops =
+{
+ .online = goldfish_charger_online,
+ .state = goldfish_battery_state,
+ .voltage = goldfish_battery_voltage,
+ .current = goldfish_battery_current,
+ .capacity = goldfish_battery_capacity,
+ .temp = goldfish_battery_temp,
Review Comment:
Need to make init C89 compatible
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org