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

263 klipper take 2 #282

Merged
merged 6 commits into from
Nov 11, 2020
Merged
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 MK404.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ int main(int argc, char *argv[])
// Longer term it'd be neat to have a synonym handler in TCLAP....
bool bArgHacks = argNoHacks.isSet() || argKlipper.isSet() || argMarlin.isSet();
bool bArgSkew = argSkew.isSet() || argKlipper.isSet();

Config::Get().SetSkewCorrect(bArgSkew);
// Make new image.
if (argImgSize.isSet())
{
Expand Down
69 changes: 64 additions & 5 deletions parts/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,23 +404,76 @@ namespace Boards {

void* Board::RunAVR()
{
// std::vector<uint64_t> vdC, vsim;
// std::vector<double> vwall;
// vdC.reserve(10000);
// vwall.reserve(10000);
// vsim.reserve(10000);
// Idle lambda that stops the virtual AVR calling usleep.
auto fcnSleep = [](avr_t*, avr_cycle_count_t) { return; };
avr_regbit_t MCUSR = m_pAVR->reset_flags.porf;
MCUSR.mask =0xFF;
MCUSR.bit = 0;
std::cout << "Starting " << m_wiring.GetMCUName() << " execution...\n";
struct timespec tp {0,0}, tStart {0,0};
clock_gettime(CLOCK_MONOTONIC, &tStart);
volatile uint64_t idle = 1000000;
while (idle>0)
{
asm(""); // NOLINT - is what it is so that it doesn't get optimized out.
idle--;
}
clock_gettime(CLOCK_MONOTONIC, &tp);
uint64_t idlens = tp.tv_nsec - tStart.tv_nsec;
auto fnsPerIdle = static_cast<float>(idlens)/1e6f;
std::cout << "10M idle cycles is " << std::to_string(idlens) << " ns (" << std::to_string(fnsPerIdle) << " ns per tick)\n";
if (m_bCorrectSkew)
{
m_pAVR->sleep = fcnSleep;
}
int state = cpu_Running;
auto tNext = m_pAVR->cycle;
uint64_t uiIdle = 10000, uiLost = 0;
while ((state != cpu_Done) && (state != cpu_Crashed) && !m_bQuit){
// Check the timing every 10k cycles, ~10 ms
if (m_bCorrectSkew && m_pAVR->cycle%500==0)
if (m_bCorrectSkew && m_pAVR->cycle>tNext)
{
auto tWall = avr_get_time_stamp(m_pAVR);
auto tSim = avr_cycles_to_nsec(m_pAVR, m_pAVR->cycle);
auto tSim = avr_cycles_to_nsec(m_pAVR, m_pAVR->cycle) + uiLost;
if (tWall<tSim)
{
auto tDiff = tSim - tWall;
if (tDiff>200000) usleep(tDiff/1000);
//std::cout << "Sim is ahead by" << std::to_string(tSim - tWall) << "ns!\n";
auto tDiff = gsl::narrow<int64_t>(tSim - static_cast<uint64_t>(tWall));
if (tDiff>100000)
{
uint64_t volatile idle = (static_cast<float>(tDiff)/fnsPerIdle);
while (idle>0)
{
asm(""); // NOLINT - is what it is so that it doesn't get optimized out.
idle--;
}
// auto tSleep = gsl::narrow<int64_t>(avr_get_time_stamp(m_pAVR) - tWall);
// if (tSleep > tDiff+150000) std::cout << "Slept too long! Asked: " << std::to_string(tDiff/1000) << " got " << std::to_string(tSleep/1000) << "us!\n";
}
}
if (tSim<tWall)
{
auto tDiff = gsl::narrow<int64_t>(static_cast<uint64_t>(tWall)-tSim);
if (tDiff>1000000) // 1 ms
{
std::cout << "Lost " << std::to_string(tDiff/1000) << " us!\n";
if (tDiff>5000000) // If we lose more than 5ms, don't try to catch up.
{
uiLost += tDiff;
}
}
}
// if (vsim.size()<10000)
// {
// vwall.push_back(tWall);
// vsim.push_back(tSim);
// vdC.push_back(m_pAVR->cycle);
// }
tNext = m_pAVR->cycle + uiIdle;
}
if (m_bIsPrimary) // Only one board should be scripting.
{
Expand Down Expand Up @@ -457,6 +510,12 @@ namespace Boards {
}
std::cout << m_wiring.GetMCUName() << "finished (" << state << ").\n";
avr_terminate(m_pAVR);
// std::cout << "cycles, wall, sim\n";
// for (size_t i=0; i<vsim.size(); i++)
// {
// std::cout << std::to_string(vdC.at(i)) << ',' << std::to_string(vwall.at(i)) << ',' << std::to_string(vsim.at(i)) << '\n';
// }

return nullptr;
};

Expand Down
12 changes: 4 additions & 8 deletions parts/components/HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ void HD44780::ResetCursor()
void HD44780::ClearScreen()
{
{
std::lock_guard<std::mutex> lock(m_lock);
for (auto &c : m_vRam)
{
c = ' ';
Expand Down Expand Up @@ -102,7 +101,7 @@ Scriptable::LineStatus HD44780::ProcessAction(unsigned int iAction, const std::v
{
return IssueLineError(std::string("ADDR") + std::to_string(iAddr) + " is out of range [0,63]");
}
if (m_cgRam[iAddr] == stoi(vArgs.at(0)))
if (gsl::at(m_cgRam,iAddr) == stoi(vArgs.at(0)))
{
return LineStatus::Finished;
}
Expand Down Expand Up @@ -218,16 +217,14 @@ uint32_t HD44780::OnDataReady()
uint32_t delay = 37; // uS
if (m_bInCGRAM)
{
std::lock_guard<std::mutex> lock(m_lock);
m_cgRam[m_uiCGCursor] = m_uiDataPins;
gsl::at(m_cgRam,m_uiCGCursor) = m_uiDataPins;
TRACE(printf("hd44780_write_data %02x to CGRAM %02x\n",m_uiDataPins,m_uiCGCursor));
IncrementCGRAMCursor();
}
else
{
{
std::lock_guard<std::mutex> lock(m_lock);
m_vRam[m_uiCursor] = m_uiDataPins;
m_vRam.at(m_uiCursor) = m_uiDataPins;
}

for (unsigned int i=0; i<m_uiHeight; i++) // Flag line change for search performance.
Expand Down Expand Up @@ -396,8 +393,7 @@ uint32_t HD44780::ProcessRead()
if (m_uiPinState & (1U << RS)) { // read data
delay = 37;
{
std::lock_guard<std::mutex> lock(m_lock);
m_uiReadPins = m_vRam[m_uiCursor];
m_uiReadPins = gsl::at(m_vRam,m_uiCursor);
}
IncrementCursor();
} else { // read 'command' ie status register
Expand Down
9 changes: 2 additions & 7 deletions parts/components/HD44780.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#include "sim_irq.h" // for avr_irq_t
#include <atomic>
#include <cstdint> // for uint8_t, uint16_t, uint32_t
#include <mutex>
#include <string> // for string
#include <vector> // for vector

Expand Down Expand Up @@ -100,10 +99,8 @@ class HD44780:public BasePeripheral, public Scriptable
// The GL draw accesses these:
std::atomic_uint8_t m_uiHeight = {4}; // width and height of the LCD
std::atomic_uint8_t m_uiWidth = {20};
uint8_t _m_vRam[104] {' '};
uint8_t _m_cgRam[64] {' '};
gsl::span<uint8_t> m_vRam {_m_vRam};
gsl::span<uint8_t> m_cgRam {_m_cgRam};
std::array<std::atomic_uint8_t,104> m_vRam = {};
std::array<std::atomic_uint8_t,64> m_cgRam = {};

LineStatus ProcessAction(unsigned int iAction, const std::vector<std::string> &args) override;

Expand Down Expand Up @@ -149,8 +146,6 @@ class HD44780:public BasePeripheral, public Scriptable

std::vector<uint8_t> m_lineOffsets = {0, 0x40, 0, 0x40};

std::mutex m_lock; // Needed for GL thread access to v/cgRAM

private:
enum Actions
{
Expand Down
12 changes: 8 additions & 4 deletions parts/components/HD44780GL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#else
# include <GL/gl.h> // for glVertex3f, glBegin, glEnd, glMaterialfv
#endif
#include <mutex>
#include <vector>

//#define TRACE(_w) _w
Expand Down Expand Up @@ -146,6 +145,8 @@ void HD44780GL::GenerateCharQuads()

void HD44780GL::GLPutChar(unsigned char c, uint32_t character, uint32_t text, uint32_t shadow, bool bMaterial)
{
uint8_t _cgChar[8];
gsl::span<uint8_t> pChar {_cgChar};
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorHelper(character,bMaterial);
Expand All @@ -158,7 +159,11 @@ void HD44780GL::GLPutChar(unsigned char c, uint32_t character, uint32_t text, ui
uint8_t iCols=8;
if (c<16)
{
uiData = m_cgRam.begin() + ((c & 7U) <<3U);
for (int i=0; i<iCols; i++)
{
pChar[i] = m_cgRam.at(((c & 7U) <<3U)+i);
}
uiData = pChar.begin(); // m_cgRam.begin() + ((c & 7U) <<3U);
}
else
{
Expand Down Expand Up @@ -244,8 +249,7 @@ void HD44780GL::Draw(
for (int v = 0 ; v < m_uiHeight; v++) {
glPushMatrix();
for (int i = 0; i < m_uiWidth; i++) {
std::lock_guard<std::mutex> lock(m_lock);
GLPutChar(m_vRam[m_lineOffsets.at(v) + i], character, text, shadow, bMaterial);
GLPutChar(gsl::at(m_vRam,m_lineOffsets.at(v) + i), character, text, shadow, bMaterial);
glTranslatef(6, 0, 0);
}
glPopMatrix();
Expand Down
5 changes: 5 additions & 0 deletions parts/components/uart_pty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@


#include "uart_pty.h"
#include "Config.h"
#include "avr_uart.h" // for AVR_IOCTL_UART_GETIRQ, ::AVR_...
#include "gsl-lite.hpp"
#include "sim_io.h" // for avr_io_getirq, avr_ioctl
Expand Down Expand Up @@ -331,6 +332,10 @@ void uart_pty::Connect(char uart)
uint32_t f = 0;
avr_ioctl(m_pAVR, AVR_IOCTL_UART_GET_FLAGS(uart), &f); //NOLINT - complaint in external macro
f &= ~AVR_UART_FLAG_STDIO;
if (Config::Get().GetSkewCorrect())
{
f&= ~(AVR_UART_FLAG_POLL_SLEEP);
}
avr_ioctl(m_pAVR, AVR_IOCTL_UART_SET_FLAGS(uart), &f); //NOLINT - complaint in external macro

avr_irq_t * src = avr_io_getirq(m_pAVR, AVR_IOCTL_UART_GETIRQ(uart), UART_IRQ_OUTPUT); //NOLINT - complaint in external macro
Expand Down
5 changes: 5 additions & 0 deletions utility/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ class Config
inline void SetColourE(bool bVal){ m_bColorExtrusion = bVal;}
inline bool GetColourE(){ return m_bColorExtrusion;}

// Should extrusion be coloured by width?.
inline void SetSkewCorrect(bool bVal){ m_bSkew = bVal;}
inline bool GetSkewCorrect(){ return m_bSkew;}

private:
unsigned int m_iExtrusion = false;
bool m_bColorExtrusion = false;
bool m_bSkew = false;

};