Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] USBIP support for MMU/32u4 #329

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
Checks: '*,-modernize-use-emplace,-hicpp-use-emplace,-readability*,-cppcoreguidelines-pro-type-union-access,-hicpp-braces*,-fuchsia*,-llvm-header-guard,-cppcoreguidelines-special-member-functions,-hicpp-special-member-functions'
Checks: '*,-modernize-use-emplace,-hicpp-use-emplace,-readability*,-cppcoreguidelines-pro-type-union-access,-hicpp-braces*,-fuchsia*,-llvm-header-guard,-cppcoreguidelines-special-member-functions,-hicpp-special-member-functions,-clang-diagnostic-unused-command-line-argument'
HeaderFilterRegex: 'parts|utility'
}
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ else()
endif()

if (ENABLE_TIDY)
set(CMAKE_CXX_CLANG_TIDY "/usr/bin/clang-tidy;-config=")
set(CMAKE_CXX_CLANG_TIDY "/usr/bin/clang-tidy-5.0;-config=")
else()
unset(CMAKE_CXX_CLANG_TIDY)
endif()
Expand Down Expand Up @@ -93,6 +93,7 @@ set(H_FILES_base
parts/components/TMC2130.h
parts/components/UART_Logger.h
parts/components/uart_pty.h
parts/components/usbip.h
parts/components/VoltageSrc.h
parts/components/w25x20cl.h
parts/I2CPeripheral.h
Expand Down Expand Up @@ -162,7 +163,15 @@ set(H_FILES_3rdParty

set(H_FILES ${H_FILES_base} ${H_FILES_3rdParty})

if (NOT APPLE)
set(NON_APPLE_SRC parts/components/usbip.c)
set_source_files_properties(parts/components/usbip.c PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
else()
set(NON_APPLE_SRC)
endif()

set(MK404_SOURCES_base
${NON_APPLE_SRC}
parts/Board.cpp
parts/I2CPeripheral.cpp
parts/boards/EinsyRambo.cpp
Expand Down Expand Up @@ -235,7 +244,6 @@ set(MK404_SOURCES_base
parts/KeyController.cpp
)


set(MK404_SOURCES_3rdparty
3rdParty/arcball/Camera.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion parts/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
#include <cstdint>
#include <cstdlib> // for exit, free
#include <cstring> // for memcpy, NULL
#include <ctime>
#include <fstream> // IWYU pragma: keep
#include <iomanip> // for operator<<, setw
#include <iostream>
#include <typeinfo> // for type_info
#include <time.h>
#include <unistd.h> // for usleep

namespace Boards {
Expand Down
2 changes: 1 addition & 1 deletion parts/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace Boards
virtual void OnAVRCycle(){};

// For boards that can handle extra chunks in the hex (language)
virtual void OnExtraHexChunk(gsl::span<uint8_t> /*chunk*/, uint32_t uiBase){};
virtual void OnExtraHexChunk(gsl::span<uint8_t> /*chunk*/, uint32_t /*uiBase*/){};

void OnKeyPress(const Key& key) override;

Expand Down
2 changes: 1 addition & 1 deletion parts/I2CPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include "I2CPeripheral.h"
#include "avr_twi.h" // for avr_twi_irq_msg, ::TWI_COND_ACK, ::TWI_COND_READ
#include "sim_avr.h" // for avr_t
#include "sim_irq.h" // for avr_raise_irq, avr_irq_register_notify, avr_irq_t
#include "sim_io.h" // for avr_io_getirq
#include "sim_irq.h" // for avr_raise_irq, avr_irq_register_notify, avr_irq_t
#include <cstdint> // for uint8_t, uint32_t, int32_t, uint16_t
#include <iostream> // for operator<<, cout, ostream

Expand Down
2 changes: 1 addition & 1 deletion parts/I2CPeripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#pragma once

#include "BasePeripheral.h"
#include <cstdint> // for uint8_t, uint32_t, int32_t, uint16_t
#include <sim_avr.h> // for avr_t
#include <sim_irq.h> // for avr_irq_t, avr_irq_register_notify
#include <cstdint> // for uint8_t, uint32_t, int32_t, uint16_t

class I2CPeripheral: public BasePeripheral
{
Expand Down
4 changes: 2 additions & 2 deletions parts/PrinterFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

#include "PrinterFactory.h"
#include "printers/IPCPrinter.h"
#include "printers/IPCPrinter_MMU2.h"
#include "printers/Prusa_MK1_13.h"
#include "printers/Prusa_MK25S_13.h"
#include "printers/Prusa_MK25_13.h"
Expand All @@ -31,8 +33,6 @@
#include "printers/Prusa_MK3SMMU2.h"
#include "printers/Prusa_MMU2.h"
#include "printers/Test_Printer.h"
#include "printers/IPCPrinter.h"
#include "printers/IPCPrinter_MMU2.h"
#include <algorithm> // for max
#include <iostream>

Expand Down
2 changes: 1 addition & 1 deletion parts/boards/EinsyRambo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include "Einsy_1_1a.h" // for Einsy_1_1a
#include "HD44780.h" // for HD44780
#include "PinNames.h" // for Pin, Pin::BTN_ENC, Pin::W25X20CL_PIN_CS
#include <iostream> // for fprintf, printf, stderr
#include <algorithm> // for copy
#include <iostream> // for fprintf, printf, stderr
#include <string>
#include <vector>

Expand Down
17 changes: 17 additions & 0 deletions parts/boards/MM_Control_01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
namespace Boards
{

MM_Control_01::~MM_Control_01()
{
#ifndef __APPLE__
pthread_cancel(m_usb_thread);
usbip_destroy(m_usb);
#endif
}

void MM_Control_01::SetupHardware()
{
DisableInterruptLevelPoll(5);
Expand Down Expand Up @@ -109,6 +117,15 @@ namespace Boards
m_lRed[1].ConnectFrom( m_shift.GetIRQ(HC595::BIT15), LED::LED_IN);

AddHardware(m_buttons,5);
#ifndef __APPLE__
m_usb = usbip_create(m_pAVR);
if (!m_usb)
{
std::cout << "Failed to create USBIP context\n";
exit(1);
}
pthread_create(&m_usb_thread, nullptr, usbip_main, m_usb);
#endif

}

Expand Down
12 changes: 11 additions & 1 deletion parts/boards/MM_Control_01.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include "LED.h" // for LED
#include "TMC2130.h" // for TMC2130
#include "uart_pty.h" // for uart_pty
#ifndef __APPLE__
extern "C" {
#include "usbip.h"
}
#endif
#include "wiring/MM_Control_01.h" // for MM_Control_01
#include <cstdint> // for uint32_t

Expand All @@ -37,7 +42,7 @@ namespace Boards
explicit MM_Control_01(uint32_t uiFreq = 16000000)
:Board(m_wiring,uiFreq){};

~MM_Control_01() override = default;
~MM_Control_01() override;

void Draw(float fY);

Expand All @@ -59,6 +64,11 @@ namespace Boards
m_lFINDA {0xFFCC00FF,'F'};
ADC_Buttons m_buttons {"MMUButtons"};

#ifndef __APPLE__
usbip_t* m_usb = nullptr;
pthread_t m_usb_thread = 0;
#endif

private:
const Wirings::MM_Control_01 m_wiring = Wirings::MM_Control_01();

Expand Down
2 changes: 1 addition & 1 deletion parts/boards/MiniRambo.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#include "SDCard.h"
#include "SerialLineMonitor.h"
#include "Thermistor.h"
#include "miniRAMBo_1_3a.h" // for Einsy_1_1a
#include "sim_irq.h" // for avr_irq_t
#include "uart_pty.h" // for uart_pty
#include "miniRAMBo_1_3a.h" // for Einsy_1_1a
#include <cstdint> // for uint32_t

namespace Boards
Expand Down
2 changes: 1 addition & 1 deletion parts/components/GLIndicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class GLIndicator
bool m_bVisible = false;
bool m_bBlackBG = false;
bool m_bInterp = false;
std::atomic_bool m_bDisabled {0};
std::atomic_bool m_bDisabled {false};
std::atomic_int16_t m_uiLerpVal = {0};

static constexpr Color3fv m_colColdTemp = {0, 1, 1};
Expand Down
2 changes: 1 addition & 1 deletion parts/components/HD44780GL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#include "hd44780_charROM.h" // for (anonymous), hd44780_ROM_AOO
#include "sim_avr_types.h" // for avr_regbit_t
#include "sim_regbit.h" // for avr_regbit_get, AVR_IO_REGBIT
#include <array> // for array<>::value_type, array
#include <GL/glew.h>
#include <array> // for array<>::value_type, array
#include <vector>

//#define TRACE(_w) _w
Expand Down
4 changes: 2 additions & 2 deletions parts/components/PINDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ void PINDA::CheckTrigger()
// Just calc the nearest MBL point and report it.
const float fMaxX = 255.;
const float fMaxY = 210.;
uint8_t iX = floor((std::min(fMaxX-1.f, std::max(0.f, m_fPos[0] - m_fOffset[0]))/fMaxX)*7);
uint8_t iY = floor((std::min(fMaxY-1.f, std::max(0.f, m_fPos[1] - m_fOffset[1]))/fMaxY)*7);
uint8_t iX = std::floor((std::min(fMaxX-1.f, std::max(0.f, m_fPos[0] - m_fOffset[0]))/fMaxX)*7);
uint8_t iY = std::floor((std::min(fMaxY-1.f, std::max(0.f, m_fPos[1] - m_fOffset[1]))/fMaxY)*7);

float fZTrig = gsl::at(m_mesh.points,iX+(7*iY));

Expand Down
4 changes: 4 additions & 0 deletions parts/components/TMC2130.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,13 @@ void TMC2130::OnStepIn(struct avr_irq_t * irq, uint32_t value)
TRACE(printf("cur pos: %f (%u)\n",m_fCurPos,m_iCurStep));
bStall |= m_bStall;
if (bStall)
{
SetDiag();
}
else
{
ClearDiag();
}
m_regs.defs.DRV_STATUS.stst = false;
// 2^20 comes from the datasheet.
RegisterTimer(m_fcnStandstill,1U<<20U,this);
Expand Down
2 changes: 1 addition & 1 deletion parts/components/TMC2130.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TMC2130: public SPIPeripheral, public Scriptable, public GLMotor

// Default constructor.
explicit TMC2130(char cAxis = ' ');
virtual ~TMC2130();
~TMC2130() override;

// Sets the configuration to the provided values. (inversion, positions, etc)
void SetConfig(TMC2130_cfg_t cfg);
Expand Down
91 changes: 91 additions & 0 deletions parts/components/usb_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* vim: set sts=4:sw=4:ts=4:noexpandtab
usb_types.h

Copyright 2017 Torbjorn Tyridal <ttyridal@gmail.com>

This file is part of simavr.

simavr is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

simavr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with simavr. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>

#define byte uint8_t
#define word uint16_t

struct usb_setup_pkt {
byte reqtype;
byte req;
word wValue;
word wIndex;
word wLength;
} __attribute__((__packed__));

#define USB_REQTYPE_DIR_DEV_TO_HOST 0x80
#define USB_REQTYPE_STD 0
#define USB_REQTYPE_DEVICE 0

#define USB_REQUEST_GET_DESCRIPTOR 0x06


// USB Descriptors
#define USB_DESCRIPTOR_DEVICE 0x01 // Device Descriptor.
#define USB_DESCRIPTOR_CONFIGURATION 0x02 // Configuration Descriptor.
#define USB_DESCRIPTOR_STRING 0x03 // String Descriptor.
#define USB_DESCRIPTOR_INTERFACE 0x04 // Interface Descriptor.
#define USB_DESCRIPTOR_ENDPOINT 0x05 // Endpoint Descriptor.
#define USB_DESCRIPTOR_DEVICE_QUALIFIER 0x06 // Device Qualifier.

struct usb_device_descriptor {
byte bLength; // Length of this descriptor.
byte bDescriptorType; // DEVICE descriptor type (USB_DESCRIPTOR_DEVICE).
word bcdUSB; // USB Spec Release Number (BCD).
byte bDeviceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bDeviceSubClass; // Subclass code (assigned by the USB-IF).
byte bDeviceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bMaxPacketSize0; // Maximum packet size for endpoint 0.
word idVendor; // Vendor ID (assigned by the USB-IF).
word idProduct; // Product ID (assigned by the manufacturer).
word bcdDevice; // Device release number (BCD).
byte iManufacturer; // Index of String Descriptor describing the manufacturer.
byte iProduct; // Index of String Descriptor describing the product.
byte iSerialNumber; // Index of String Descriptor with the device's serial number.
byte bNumConfigurations; // Number of possible configurations.
} __attribute__ ((__packed__));

struct usb_configuration_descriptor
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // CONFIGURATION descriptor type (USB_DESCRIPTOR_CONFIGURATION).
word wTotalLength; // Total length of all descriptors for this configuration.
byte bNumInterfaces; // Number of interfaces in this configuration.
byte bConfigurationValue; // Value of this configuration (1 based).
byte iConfiguration; // Index of String Descriptor describing the configuration.
byte bmAttributes; // Configuration characteristics.
byte bMaxPower; // Maximum power consumed by this configuration.
} __attribute__ ((__packed__));


struct usb_interface_descriptor
{
byte bLength; // Length of this descriptor.
byte bDescriptorType; // INTERFACE descriptor type (USB_DESCRIPTOR_INTERFACE).
byte bInterfaceNumber; // Number of this interface (0 based).
byte bAlternateSetting; // Value of this alternate interface setting.
byte bNumEndpoints; // Number of endpoints in this interface.
byte bInterfaceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
byte bInterfaceSubClass; // Subclass code (assigned by the USB-IF).
byte bInterfaceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte iInterface; // Index of String Descriptor describing the interface.
} __attribute__ ((__packed__));
Loading