acassis commented on code in PR #8482: URL: https://github.com/apache/nuttx/pull/8482#discussion_r1103252114
########## drivers/usbmisc/fusb302.c: ########## @@ -0,0 +1,1952 @@ +/**************************************************************************** + * drivers/usbmisc/fusb302.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 <nuttx/config.h> + +#include <assert.h> +#include <errno.h> +#include <poll.h> +#include <debug.h> + +#include <nuttx/compiler.h> +#include <nuttx/fs/fs.h> +#include <nuttx/kmalloc.h> +#include <nuttx/kthread.h> +#include <nuttx/mutex.h> +#include <nuttx/wqueue.h> +#include <nuttx/i2c/i2c_master.h> + +#include <nuttx/usb/fusb302.h> +#include <sys/ioctl.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef FUSB302_I2C_FREQUENCY +# define FUSB302_I2C_FREQUENCY 1000000 +#endif + +#ifdef CONFIG_DEBUG_USB_ERROR +# define fusb302_err(x, ...) _err(x, ##__VA_ARGS__) +#else +# define fusb302_err(x, ...) uerr(x, ##__VA_ARGS__) +#endif + +#if defined(CONFIG_DEBUG_USB_INFO) || defined (CONFIG_DEBUG_FUSB302_REG) +# define fusb302_info(x, ...) _info(x, ##__VA_ARGS__) +#else +# define fusb302_info(x, ...) uinfo(x, ##__VA_ARGS__) +#endif + +#define FUSB302_I2C_RETRIES 10 + +/* Switches0 - 0x02 */ + +#define SWITCHES0_PULLDOWN_CC1 (1 << 0) +#define SWITCHES0_PULLDOWN_CC2 (1 << 1) +#define SWITCHES0_PULLDOWN_SHIFT (0) +#define SWITCHES0_PULLDOWN_MASK (3 << SWITCHES0_PULLDOWN_SHIFT) +#define SWITCHES0_MEASURE_CC1 (1 << 2) +#define SWITCHES0_MEASURE_CC2 (1 << 3) +#define SWITCHES0_MEASURE_SHIFT (3) +#define SWITCHES0_MEASURE_MASK (3 << SWITCHES0_MEASURE_SHIFT) +#define SWITCHES0_VCONN_CC1 (1 << 4) +#define SWITCHES0_VCONN_CC2 (1 << 5) +#define SWITCHES0_VCONN_SHIFT (4) +#define SWITCHES0_VCONN_MASK (3 << SWITCHES0_VCONN_SHIFT) +#define SWITCHES0_PULLUP_CC1 (1 << 6) +#define SWITCHES0_PULLUP_CC2 (1 << 7) +#define SWITCHES0_PULLUP_SHIFT (6) +#define SWITCHES0_PULLUP_MASK (3 << SWITCHES0_PULLUP_SHIFT) + +/* Switches1 */ + +#define SWITCHES1_TXCC1_MASK (1 << 0) +#define SWITCHES1_TXCC2_MASK (1 << 1) +#define SWITCHES1_AUTO_CRC_MASK (1 << 2) +#define SWITCHES1_DATAROLE_MASK (1 << 3) +#define SWITCHES1_SPECREV_SHIFT (1 << 5) /* Bits 6:5 Specrole */ +#define SWITCHES1_SPECREV_MASK (0x03 << 6) +#define SWITCHES1_SPECREV(n) ((uint8_t)(n) << SWITCHES1_SPECREV_SHIFT) +#define SWITCHES1_POWERROLE_MASK (1 << 6) + +/* Measure - 0x04 */ + +#define MEASURE_MDAC_SHIFT (0) /* Bits 5:0 MDAC */ +#define MEASURE_MDAC_MASK (0b111111 << MEASURE_MDAC_SHIFT) +#define MEASURE_MDAC(n) ((uint8_t)(n)) << MEASURE_MDAC_SHIFT +#define MEASURE_VBUS_BY_MDAC (1 << 6) +#define SET_MDAC(n) ((uint8_t)(n)) << MEASURE_MDAC_SHIFT) + +/* Slice - 0x05 */ + +#define MEASURE_SDAC_SHIFT (0) /* Bits 5:0 SDAC */ +#define MEASURE_SDAC_MASK (0b111111 < MEASURE_SDAC_SHIFT) +#define MEASURE_SDAC(n) ((uint8_t)(n)) << MEASURE_SDAC_SHIFT) +#define MEASURE_SDAC_HYS_SHIFT (6) /* Bits 7:6 SDAC hysteris */ +#define MEASURE_SDAC_HYS_MASK (0b11 << MEASURE_SDAC_HYS_SHIFT) +#define SDAC_HYS_VAL(n) ((uint8)t)(n)) << MEASURE_SDAC_HYS_SHIFT) + +/* Control0 - 0x06 */ + +#define CONTROL0_TX_START_MASK (1 << 0) +#define CONTROL0_AUTO_PRE_SHIFT (1) +#define CONTROL0_HOST_CUR_SHIFT (2) /* Bits 3:2 Host Current mode */ +#define CONTROL0_HOST_CUR_MASK (3 << CONTROL0_HOST_CUR_SHIFT) +#define HOST_CURRENT_NONE (0) /* no current */ +#define HOST_CURRENT_80UA (1) /* default USB power */ +#define HOST_CURRENT_180UA (2) /* medium power, 1.5A */ +#define HOST_CURRENT_330UA (3) /* high power, 3A */ +#define HOST_CURRENT(n) ((uint8_t)(n) << CONTROL0_HOST_CUR_SHIFT) +#define CONTROL0_INT_MASK (1 << 5) +#define CONTROL0_TX_FLUSH (1 << 6) + +/* Control1 - 0x07 */ + +#define CONTROL1_ENSOP1_MASK (1 << 0) +#define CONTROL1_ENSOP2_MASK (1 << 1) +#define CONTROL1_RXFLUSH_MASK (1 << 2) +#define CONTROL1_BIST_MODE2_MASK (1 << 4) +#define CONTROL1_ENSOP1DB_MASK (1 << 5) +#define CONTROL1_ENSOP2DB_MASK (1 << 6) + +/* Control2 - 0x08 */ + +#define CONTROL2_TOGGLE (1 << 0) +#define CONTROL2_MODE_SHIFT (1) /* Bits 2:1 */ +#define CONTROL2_MODE_MASK (0b11 << CONTROL2_MODE_SHIFT) +#define SRC_POLLING (0b11) +#define SNK_POLLING (0b10) +#define DRP_POLLING (0b01) +#define SET_POLL_MODE(n) ((uint8_t)(n) << CONTROL2_MODE_SHIFT) +#define CONTROL2_WAKE_EN (1 << 3) +#define CONTROL2_TOG_RD_ONLY (1 << 5) +#define CONTROL2_TOG_SAVE_PWR_SHIFT (6) /* Bits 7:6 */ +#define CONTROL2_TOG_SAVE_PWR_MASK (0b11 << CONTROL2_TOG_SAVE_PWR_SHIFT +#define WAIT_NONE (0b00) +#define WAIT_40MS (0b01) +#define WAIT_80MS (0b10) +#define WAIT_160MS (0b11) +#define SET_WAIT_MODE(n) (((uint8_t)(n) << CONTROL2_TOG_SAVE_PWR_SHIFT) + +/* Control3 - 0x09 */ + +#define CONTROL3_AUTO_RETRY (0) +#define CONTROL3_N_RETRIES_SHIFT (1) /* Bits 2:1 */ +#define CONTROL3_N_RETRIES_MASK (0b11 << CONTROL3_N_RETRIES_SHIFT) +#define NO_RETRIES (0b00) +#define ONE_RETRIES (0b01) +#define TWO_RETRIES (0b10) +#define THREE_RETRIES (0b11) +#define SET_NUM_RETRIES(n) ((uint8_t(n) << CONTROL3_N_RETRIES_SHIFT) +#define CONTROL3_AUTO_SOFTRESET (1 << 3) +#define CONTROL3_AUTO_HARDRESET (1 << 4) +#define CONTROL3_SEND_HARDRESET (1 << 6) + +/* Mask - 0x0A */ + +#define MASK_BC_LVL (1 << 0) +#define MASK_COLLISION (1 << 1) +#define MASK_WAKE (1 << 2) +#define MASK_ALERT (1 << 3) +#define MASK_CRC_CHK (1 << 4) +#define MASK_COMP_CHNG (1 << 5) +#define MASK_ACTIVITY (1 << 6) +#define MASK_VBUS_OK (1 << 7) +#define MASK_ALL (0xff) +#define MASK_FOR_DISCONNECT (MASK_VBUS_OK | MASK_COMP_CHNG) + +/* Power 0x0B */ + +#define POWER_PWR_SHIFT (0) /* Bits 3:0 */ +#define POWER_PWR_MASK (15 << POWER_PWR_SHIFT) +#define POWER_MODE_ALL (15) +#define POWER_MODE_TOGGLING (7) +#define POWER_MODE_BANDGAP_AND_WAKE (1) +#define POWER_MODE_RX_AND_IREF (2) +#define POWER_MODE_MEASURE_BLOCK (4) +#define POWER_MODE_MEASURE_INT_OSC (8) +#define POWER_MODE(n) ((uint8_t)(n) << POWER_PWR_SHIFT) + +/* Reset 0x0C */ + +#define RESET_SW_RESET (1 << 0) +#define RESET_PD_RESET (1 << 1) + +/* OCPreg = 0x0D */ + +#define OCPREG_CUR_SHIFT (0) /* Bits 2:0 */ +#define OCPREG_CUR_MASK (0b111 << OCPREG_CUR_SHIFT) +#define X1_MAX_RANGE_DIV8 (0b000) +#define X2_MAX_RANGE_DIV8 (0b001) +#define X3_MAX_RANGE_DIV8 (0b010) +#define X4_MAX_RANGE_DIV8 (0b011) +#define X5_MAX_RANGE_DIV8 (0b100) +#define X6_MAX_RANGE_DIV8 (0b101) +#define X7_MAX_RANGE_D (0b110) +#define MAX_RANGE (0b111) +#define SET_OCP_RANGE(n) ((uint8_t)(n) << OCPREG_CUR_SHIFT) +#define OCPREF_OCP_RANGE (1 << 3) + +/* MaskA - 0x0E */ + +#define MASKA_HARDRST (1 << 0) +#define MASKA_SOFTRST (1 << 1) +#define MASKA_TXSENT (1 << 2) +#define MASKA_HARDSENT (1 << 3) +#define MASKA_RETRYFAIL (1 << 4) +#define MASKA_SOFTFAIL (1 << 5) +#define MASKA_TOGDONE (1 << 6) +#define MASKA_OCP_TEMP (1 << 7) + +/* MaskB - 0x0F */ + +#define MASKB_GCRCSENT (1 << 0) + +/* Control4 - 0x10 */ +#define CONTROL4_TOG_USRC_EXIT (1 << 0) Review Comment: You missed this one -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
