# drbild/cp2130 architecture

> Architecture diagram of the GitHub repository drbild/cp2130, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/drbild/cp2130
- Repository: https://github.com/drbild/cp2130

## Overview

##### Main Components Identification

a. Core API Layer
- Entry point for users
- High-level Pythonic interface

b. Feature Modules Layer
- SPI Communication
- GPIO Management
- Clock Configuration
- Event Counter
- ROM Operations
- USB Configuration

c. Low-Level Implementation Layer
- Chip Commands
- Register Operations
- USB Backend Implementations

**[User Application]**
- ↓
**[Core API Layer]**
- ↓
**[Feature Modules]**
- ↓
**[Low-Level Implementation]**
- ↓
**[Hardware Interface]**

##### Component Details

a. Core API Layer (cp2130.core):
- Connections to all feature modules
- Main CP2130 class as the primary interface

- SPI Module (spi.py)
- GPIO Module (gpio.py)
- Clock Module (clock.py)
- Event Counter Module (event_counter.py)
- Pin Configuration (pin_config.py)
- USB Configuration (usb_config.py)

c. Data Layer (data/):
- As a shared resource
- To both Core API and Feature Modules
- Enums and constants

d. Chip Layer:
- Command handling (chip/commands.py)
- Register management (chip/registers.py)
- Field definitions (chip/fields.py)

e. USB Backend Layer:
- Multiple implementations (PyUSB and libusb1)
- Use interface symbol for USBDevice interface
- To hardware layer

##### Important Relationships

a. Data Flow:
- User → Core API → Feature Modules → Chip Layer → USB Backend → Hardware

b. Dependencies:
- How data package is used across layers
- USB backend abstraction
- Chip command flow

##### Additional Elements

- Notes about critical features (e.g., cs_hold limitation)
- Version compatibility (Python 2.7 and 3.3+)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef feature fill:#2ecc71,stroke:#27ae60,color:white
    classDef data fill:#f1c40f,stroke:#f39c12,color:black
    classDef chip fill:#e67e22,stroke:#d35400,color:white
    classDef usb fill:#9b59b6,stroke:#8e44ad,color:white
    classDef hardware fill:#95a5a6,stroke:#7f8c8d,color:white

    %% User Application Layer
    UA[User Application]

    %% Core API Layer
    subgraph CoreAPI
        CP["CP2130 Core API"]:::core
    end
    click CP "https://github.com/drbild/cp2130/blob/master/cp2130/core.py"

    %% Feature Modules Layer
    subgraph FeatureModules
        SPI["SPI Communication"]:::feature
        GPIO["GPIO Management"]:::feature
        CLOCK["Clock Configuration"]:::feature
        EVENT["Event Counter"]:::feature
        PIN["Pin Configuration"]:::feature
        USBCONF["USB Configuration"]:::feature
    end
    click SPI "https://github.com/drbild/cp2130/blob/master/cp2130/spi.py"
    click GPIO "https://github.com/drbild/cp2130/blob/master/cp2130/gpio.py"
    click CLOCK "https://github.com/drbild/cp2130/blob/master/cp2130/clock.py"
    click EVENT "https://github.com/drbild/cp2130/blob/master/cp2130/event_counter.py"
    click PIN "https://github.com/drbild/cp2130/blob/master/cp2130/pin_config.py"
    click USBCONF "https://github.com/drbild/cp2130/blob/master/cp2130/usb_config.py"

    %% Data Layer
    subgraph DataLayer
        EVENTDATA["Event Counter Data"]:::data
        GPIODATA["GPIO Data"]:::data
        ROMDATA["ROM Data"]:::data
        SPIDATA["SPI Data"]:::data
        USBDATA["USB Data"]:::data
    end
    click EVENTDATA "https://github.com/drbild/cp2130/blob/master/cp2130/data/event_counter.py"
    click GPIODATA "https://github.com/drbild/cp2130/blob/master/cp2130/data/gpio.py"
    click ROMDATA "https://github.com/drbild/cp2130/blob/master/cp2130/data/rom.py"
    click SPIDATA "https://github.com/drbild/cp2130/blob/master/cp2130/data/spi.py"
    click USBDATA "https://github.com/drbild/cp2130/blob/master/cp2130/data/usb.py"

    %% Chip Layer
    subgraph ChipLayer
        CMD["Command Handler"]:::chip
        REG["Register Manager"]:::chip
        FIELD["Field Definitions"]:::chip
    end
    click CMD "https://github.com/drbild/cp2130/blob/master/cp2130/chip/commands.py"
    click REG "https://github.com/drbild/cp2130/blob/master/cp2130/chip/registers.py"
    click FIELD "https://github.com/drbild/cp2130/blob/master/cp2130/chip/fields.py"

    %% USB Backend Layer
    subgraph USBBackend
        USBI["USB Interface"]:::usb
        PYUSB["PyUSB Backend"]:::usb
        LIBUSB["LibUSB1 Backend"]:::usb
        HOTPLUG["Hotplug Support"]:::usb
    end
    click USBI "https://github.com/drbild/cp2130/blob/master/cp2130/usb/usb.py"
    click PYUSB "https://github.com/drbild/cp2130/blob/master/cp2130/usb/pyusb.py"
    click LIBUSB "https://github.com/drbild/cp2130/blob/master/cp2130/usb/libusb1/core.py"
    click HOTPLUG "https://github.com/drbild/cp2130/blob/master/cp2130/usb/libusb1/hotplug.py"

    %% Hardware Layer
    HW["CP2130 Hardware"]:::hardware

    %% Connections
    UA --> CP
    CP --> SPI & GPIO & CLOCK & EVENT & PIN & USBCONF
    SPI & GPIO & CLOCK & EVENT & PIN & USBCONF --> CMD
    CMD --> REG
    REG --> FIELD
    FIELD --> USBI
    USBI --> PYUSB & LIBUSB
    LIBUSB --> HOTPLUG
    PYUSB & LIBUSB --> HW

    %% Data Layer Connections
    EVENTDATA -.-> EVENT
    GPIODATA -.-> GPIO
    ROMDATA -.-> CP
    SPIDATA -.-> SPI
    USBDATA -.-> USBCONF

    %% Legend
    subgraph Legend
        L1["Core API"]:::core
        L2["Feature Modules"]:::feature
        L3["Data Layer"]:::data
        L4["Chip Layer"]:::chip
        L5["USB Backend"]:::usb
        L6["Hardware"]:::hardware
    end
```
