code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include "../../../inc/MarlinConfig.h" #include "Clock.h" std::chrono::nanoseconds Clock::startup = std::chrono::high_resolution_clock::now().time_since_epoch(); uint32_t Clock::frequency = F_CPU; double Clock::time_multiplier = 1.0; #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Clock.cpp
C++
agpl-3.0
1,146
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <chrono> #include <thread> class Clock { public: static uint64_t ticks(uint32_t frequency = Clock::frequency) { return (Clock::nanos() - Clock::startup.count()) / (1000000000ULL / frequency); } static uint64_t nanosToTicks(uint64_t ns, uint32_t frequency = Clock::frequency) { return ns / (1000000000ULL / frequency); } // Time acceleration compensated static uint64_t ticksToNanos(uint64_t tick, uint32_t frequency = Clock::frequency) { return (tick * (1000000000ULL / frequency)) / Clock::time_multiplier; } static void setFrequency(uint32_t freq) { Clock::frequency = freq; } // Time Acceleration compensated static uint64_t nanos() { auto now = std::chrono::high_resolution_clock::now().time_since_epoch(); return (now.count() - Clock::startup.count()) * Clock::time_multiplier; } static uint64_t micros() { return Clock::nanos() / 1000; } static uint64_t millis() { return Clock::micros() / 1000; } static double seconds() { return Clock::nanos() / 1000000000.0; } static void delayCycles(uint64_t cycles) { std::this_thread::sleep_for(std::chrono::nanoseconds( (1000000000L / frequency) * cycles) / Clock::time_multiplier ); } static void delayMicros(uint64_t micros) { std::this_thread::sleep_for(std::chrono::microseconds( micros ) / Clock::time_multiplier); } static void delayMillis(uint64_t millis) { std::this_thread::sleep_for(std::chrono::milliseconds( millis ) / Clock::time_multiplier); } static void delaySeconds(double secs) { std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(secs * 1000) / Clock::time_multiplier); } // Will reduce timer resolution increasing likelihood of overflows static void setTimeMultiplier(double tm) { Clock::time_multiplier = tm; } private: static std::chrono::nanoseconds startup; static uint32_t frequency; static double time_multiplier; };
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Clock.h
C++
agpl-3.0
2,828
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include "Gpio.h" pin_data Gpio::pin_map[Gpio::pin_count+1] = {}; IOLogger* Gpio::logger = nullptr; #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Gpio.cpp
C++
agpl-3.0
1,012
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "Clock.h" #include "../../../inc/MarlinConfigPre.h" #include <stdint.h> typedef int16_t pin_type; struct GpioEvent { enum Type { NOP, FALL, RISE, SET_VALUE, SETM, SETD }; uint64_t timestamp; pin_type pin_id; GpioEvent::Type event; GpioEvent(uint64_t timestamp, pin_type pin_id, GpioEvent::Type event) { this->timestamp = timestamp; this->pin_id = pin_id; this->event = event; } }; class IOLogger { public: virtual ~IOLogger(){}; virtual void log(GpioEvent ev) = 0; }; class Peripheral { public: virtual ~Peripheral(){}; virtual void interrupt(GpioEvent ev) = 0; virtual void update() = 0; }; struct pin_data { uint8_t dir; uint8_t mode; uint16_t value; Peripheral* cb; }; class Gpio { public: static const pin_type pin_count = 255; static pin_data pin_map[pin_count+1]; static bool valid_pin(pin_type pin) { return pin >= 0 && pin <= pin_count; } static void set(pin_type pin) { set(pin, 1); } static void set(pin_type pin, uint16_t value) { if (!valid_pin(pin)) return; GpioEvent::Type evt_type = value > 1 ? GpioEvent::SET_VALUE : value > pin_map[pin].value ? GpioEvent::RISE : value < pin_map[pin].value ? GpioEvent::FALL : GpioEvent::NOP; pin_map[pin].value = value; GpioEvent evt(Clock::nanos(), pin, evt_type); if (pin_map[pin].cb) { pin_map[pin].cb->interrupt(evt); } if (Gpio::logger) Gpio::logger->log(evt); } static uint16_t get(pin_type pin) { if (!valid_pin(pin)) return 0; return pin_map[pin].value; } static void clear(pin_type pin) { set(pin, 0); } static void setMode(pin_type pin, uint8_t value) { if (!valid_pin(pin)) return; pin_map[pin].mode = value; GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETM); if (pin_map[pin].cb) pin_map[pin].cb->interrupt(evt); if (Gpio::logger) Gpio::logger->log(evt); } static uint8_t getMode(pin_type pin) { if (!valid_pin(pin)) return 0; return pin_map[pin].mode; } static void setDir(pin_type pin, uint8_t value) { if (!valid_pin(pin)) return; pin_map[pin].dir = value; GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETD); if (pin_map[pin].cb) pin_map[pin].cb->interrupt(evt); if (Gpio::logger) Gpio::logger->log(evt); } static uint8_t getDir(pin_type pin) { if (!valid_pin(pin)) return 0; return pin_map[pin].dir; } static void attachPeripheral(pin_type pin, Peripheral* per) { if (!valid_pin(pin)) return; pin_map[pin].cb = per; } static void attachLogger(IOLogger* logger) { Gpio::logger = logger; } private: static IOLogger* logger; };
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Gpio.h
C++
agpl-3.0
3,552
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include "Clock.h" #include <stdio.h> #include "../../../inc/MarlinConfig.h" #include "Heater.h" Heater::Heater(pin_t heater, pin_t adc) { heater_state = 0; room_temp_raw = 150; last = Clock::micros(); heater_pin = heater; adc_pin = adc; heat = 0.0; } Heater::~Heater() { } void Heater::update() { // crude pwm read and cruder heat simulation auto now = Clock::micros(); double delta = (now - last); if (delta > 1000 ) { heater_state = pwmcap.update(0xFFFF * Gpio::pin_map[heater_pin].value); last = now; heat += (heater_state - heat) * (delta / 1000000000.0); NOLESS(heat, room_temp_raw); Gpio::pin_map[analogInputToDigitalPin(adc_pin)].value = 0xFFFF - (uint16_t)heat; } } void Heater::interrupt(GpioEvent ev) { // unused } #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Heater.cpp
C++
agpl-3.0
1,687
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "Gpio.h" struct LowpassFilter { uint64_t data_delay = 0; uint16_t update(uint16_t value) { data_delay += value - (data_delay >> 6); return uint16_t(data_delay >> 6); } }; class Heater: public Peripheral { public: Heater(pin_t heater, pin_t adc); virtual ~Heater(); void interrupt(GpioEvent ev); void update(); pin_t heater_pin, adc_pin; uint16_t room_temp_raw; uint16_t heater_state; LowpassFilter pwmcap; double heat; uint64_t last; };
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Heater.h
C++
agpl-3.0
1,359
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include "IOLoggerCSV.h" IOLoggerCSV::IOLoggerCSV(std::string filename) { file.open(filename); } IOLoggerCSV::~IOLoggerCSV() { file.close(); } void IOLoggerCSV::log(GpioEvent ev) { std::lock_guard<std::mutex> lock(vector_lock); events.push_back(ev); //minimal impact to signal handler } void IOLoggerCSV::flush() { { std::lock_guard<std::mutex> lock(vector_lock); while (!events.empty()) { file << events.front().timestamp << ", "<< events.front().pin_id << ", " << events.front().event << std::endl; events.pop_front(); } } file.flush(); } #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/IOLoggerCSV.cpp
C++
agpl-3.0
1,490
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <mutex> #include <list> #include <fstream> #include "Gpio.h" class IOLoggerCSV: public IOLogger { public: IOLoggerCSV(std::string filename); virtual ~IOLoggerCSV(); void flush(); void log(GpioEvent ev); private: std::ofstream file; std::list<GpioEvent> events; std::mutex vector_lock; };
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/IOLoggerCSV.h
C++
agpl-3.0
1,189
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include <random> #include <stdio.h> #include "Clock.h" #include "LinearAxis.h" LinearAxis::LinearAxis(pin_type enable, pin_type dir, pin_type step, pin_type end_min, pin_type end_max) { enable_pin = enable; dir_pin = dir; step_pin = step; min_pin = end_min; max_pin = end_max; min_position = 50; max_position = (200*80) + min_position; position = rand() % ((max_position - 40) - min_position) + (min_position + 20); last_update = Clock::nanos(); Gpio::attachPeripheral(step_pin, this); } LinearAxis::~LinearAxis() { } void LinearAxis::update() { } void LinearAxis::interrupt(GpioEvent ev) { if (ev.pin_id == step_pin && !Gpio::pin_map[enable_pin].value) { if (ev.event == GpioEvent::RISE) { last_update = ev.timestamp; position += -1 + 2 * Gpio::pin_map[dir_pin].value; Gpio::pin_map[min_pin].value = (position < min_position); //Gpio::pin_map[max_pin].value = (position > max_position); //if (position < min_position) printf("axis(%d) endstop : pos: %d, mm: %f, min: %d\n", step_pin, position, position / 80.0, Gpio::pin_map[min_pin].value); } } } #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/LinearAxis.cpp
C++
agpl-3.0
2,032
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <chrono> #include "Gpio.h" class LinearAxis: public Peripheral { public: LinearAxis(pin_type enable, pin_type dir, pin_type step, pin_type end_min, pin_type end_max); virtual ~LinearAxis(); void update(); void interrupt(GpioEvent ev); pin_type enable_pin; pin_type dir_pin; pin_type step_pin; pin_type min_pin; pin_type max_pin; int32_t position; int32_t min_position; int32_t max_position; uint64_t last_update; };
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/LinearAxis.h
C++
agpl-3.0
1,331
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include "Timer.h" #include <stdio.h> Timer::Timer() { active = false; compare = 0; frequency = 0; overruns = 0; timerid = 0; cbfn = nullptr; period = 0; start_time = 0; avg_error = 0; } Timer::~Timer() { if (timerid != 0) { timer_delete(timerid); timerid = 0; } } void Timer::init(uint32_t sig_id, uint32_t sim_freq, callback_fn* fn) { struct sigaction sa; struct sigevent sev; frequency = sim_freq; cbfn = fn; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = Timer::handler; sigemptyset(&sa.sa_mask); if (sigaction(SIGRTMIN, &sa, nullptr) == -1) { return; // todo: handle error } sigemptyset(&mask); sigaddset(&mask, SIGRTMIN); disable(); sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGRTMIN; sev.sigev_value.sival_ptr = (void*)this; if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { return; // todo: handle error } } void Timer::start(uint32_t frequency) { setCompare(this->frequency / frequency); //printf("timer(%ld) started\n", getID()); } void Timer::enable() { if (sigprocmask(SIG_UNBLOCK, &mask, nullptr) == -1) { return; // todo: handle error } active = true; //printf("timer(%ld) enabled\n", getID()); } void Timer::disable() { if (sigprocmask(SIG_SETMASK, &mask, nullptr) == -1) { return; // todo: handle error } active = false; } void Timer::setCompare(uint32_t compare) { uint32_t nsec_offset = 0; if (active) { nsec_offset = Clock::nanos() - this->start_time; // calculate how long the timer would have been running for nsec_offset = nsec_offset < 1000 ? nsec_offset : 0; // constrain, this shouldn't be needed but apparently Marlin enables interrupts on the stepper timer before initialising it, todo: investigate ?bug? } this->compare = compare; uint64_t ns = Clock::ticksToNanos(compare, frequency) - nsec_offset; struct itimerspec its; its.it_value.tv_sec = ns / 1000000000; its.it_value.tv_nsec = ns % 1000000000; its.it_interval.tv_sec = its.it_value.tv_sec; its.it_interval.tv_nsec = its.it_value.tv_nsec; if (timer_settime(timerid, 0, &its, nullptr) == -1) { printf("timer(%ld) failed, compare: %d(%ld)\n", getID(), compare, its.it_value.tv_nsec); return; // todo: handle error } //printf("timer(%ld) started, compare: %d(%d)\n", getID(), compare, its.it_value.tv_nsec); this->period = its.it_value.tv_nsec; this->start_time = Clock::nanos(); } uint32_t Timer::getCount() { return Clock::nanosToTicks(Clock::nanos() - this->start_time, frequency); } #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Timer.cpp
C++
agpl-3.0
3,454
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <stdint.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <time.h> #include <stdio.h> #include "Clock.h" class Timer { public: Timer(); virtual ~Timer(); typedef void (callback_fn)(); void init(uint32_t sig_id, uint32_t sim_freq, callback_fn* fn); void start(uint32_t frequency); void enable(); bool enabled() {return active;} void disable(); void setCompare(uint32_t compare); uint32_t getCount(); uint32_t getCompare() {return compare;} uint32_t getOverruns() {return overruns;} uint32_t getAvgError() {return avg_error;} intptr_t getID() { return (*(intptr_t*)timerid); } static void handler(int sig, siginfo_t *si, void *uc) { Timer* _this = (Timer*)si->si_value.sival_ptr; _this->avg_error += (Clock::nanos() - _this->start_time) - _this->period; //high_resolution_clock is also limited in precision, but best we have _this->avg_error /= 2; //very crude precision analysis (actually within +-500ns usually) _this->start_time = Clock::nanos(); // wrap _this->cbfn(); _this->overruns += timer_getoverrun(_this->timerid); // even at 50Khz this doesn't stay zero, again demonstrating the limitations // using a realtime linux kernel would help somewhat } private: bool active; uint32_t compare; uint32_t frequency; uint32_t overruns; timer_t timerid; sigset_t mask; callback_fn* cbfn; uint64_t period; uint64_t avg_error; uint64_t start_time; };
2301_81045437/Marlin
Marlin/src/HAL/LINUX/hardware/Timer.h
C++
agpl-3.0
2,400
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/LINUX/inc/Conditionals_LCD.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/LINUX/inc/Conditionals_adv.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/LINUX/inc/Conditionals_post.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/LINUX/inc/Conditionals_type.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Test X86_64-specific configuration values for errors at compile-time. */ // Emulating RAMPS #if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11) #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector" #endif #if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY #error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for HAL/LINUX." #endif #if HAS_SPI_TFT || HAS_FSMC_TFT #error "Sorry! TFT displays are not available for HAL/LINUX." #endif #if HAS_TMC_SW_SERIAL #error "TMC220x Software Serial is not supported for HAL/LINUX." #endif #if ENABLED(POSTMORTEM_DEBUGGING) #error "POSTMORTEM_DEBUGGING is not yet supported for HAL/LINUX." #endif
2301_81045437/Marlin
Marlin/src/HAL/LINUX/inc/SanityCheck.h
C
agpl-3.0
1,682
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <stddef.h> #include <stdint.h> #include <math.h> #include <cstring> #include <pinmapping.h> #define HIGH 0x01 #define LOW 0x00 #define INPUT 0x00 #define OUTPUT 0x01 #define INPUT_PULLUP 0x02 #define INPUT_PULLDOWN 0x03 #define LSBFIRST 0 #define MSBFIRST 1 #define CHANGE 0x02 #define FALLING 0x03 #define RISING 0x04 typedef uint8_t byte; #define PROGMEM #define PSTR(v) (v) #define PGM_P const char * // Used for libraries, preprocessor, and constants #define abs(x) ((x)>0?(x):-(x)) #ifndef isnan #define isnan std::isnan #endif #ifndef isinf #define isinf std::isinf #endif #define sq(v) ((v) * (v)) #define constrain(value, arg_min, arg_max) ((value) < (arg_min) ? (arg_min) :((value) > (arg_max) ? (arg_max) : (value))) // Interrupts void cli(); // Disable void sei(); // Enable void attachInterrupt(uint32_t pin, void (*callback)(), uint32_t mode); void detachInterrupt(uint32_t pin); extern "C" { void GpioEnableInt(uint32_t port, uint32_t pin, uint32_t mode); void GpioDisableInt(uint32_t port, uint32_t pin); } // Time functions extern "C" void delay(const int ms); void _delay_ms(const int ms); void delayMicroseconds(unsigned long); uint32_t millis(); //IO functions void pinMode(const pin_t, const uint8_t); void digitalWrite(pin_t, uint8_t); bool digitalRead(pin_t); void analogWrite(pin_t, int); uint16_t analogRead(pin_t); int32_t random(int32_t); int32_t random(int32_t, int32_t); void randomSeed(uint32_t); char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s); int map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max);
2301_81045437/Marlin
Marlin/src/HAL/LINUX/include/Arduino.h
C++
agpl-3.0
2,570
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include <pinmapping.h> #include "../../../gcode/parser.h" int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) { return parser.intval(code, dval); } #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/include/pinmapping.cpp
C++
agpl-3.0
1,074
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfigPre.h" #include <stdint.h> #include "../hardware/Gpio.h" typedef pin_type pin_t; #define P_NC -1 constexpr uint16_t NUM_DIGITAL_PINS = Gpio::pin_count; constexpr uint8_t NUM_ANALOG_INPUTS = 16; #define HAL_SENSITIVE_PINS constexpr uint8_t analog_offset = NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS; // Get the digital pin for an analog index constexpr pin_t analogInputToDigitalPin(const int8_t p) { return (WITHIN(p, 0, NUM_ANALOG_INPUTS) ? analog_offset + p : P_NC); } // Get the analog index for a digital pin constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) { return (WITHIN(p, analog_offset, NUM_DIGITAL_PINS) ? p - analog_offset : P_NC); } // Return the index of a pin number constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) { return pin; } // Test whether the pin is valid constexpr bool VALID_PIN(const pin_t p) { return WITHIN(p, 0, NUM_DIGITAL_PINS); } // Test whether the pin is PWM constexpr bool PWM_PIN(const pin_t p) { return false; } // Test whether the pin is interruptible constexpr bool INTERRUPT_PIN(const pin_t p) { return false; } // Get the pin number at the given index constexpr pin_t GET_PIN_MAP_PIN(const int16_t ind) { return ind; } // Parse a G-code word into a pin index int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
2301_81045437/Marlin
Marlin/src/HAL/LINUX/include/pinmapping.h
C++
agpl-3.0
2,199
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfigPre.h" #if ENABLED(EMERGENCY_PARSER) #include "../../../feature/e_parser.h" #endif #include "../../../core/serial_hook.h" #include <stdarg.h> #include <stdio.h> /** * Generic RingBuffer * T type of the buffer array * S size of the buffer (must be power of 2) */ template <typename T, uint32_t S> class RingBuffer { public: RingBuffer() { index_read = index_write = 0; } uint32_t available() volatile { return index_write - index_read; } uint32_t free() volatile { return buffer_size - available(); } bool empty() volatile { return index_read == index_write; } bool full() volatile { return available() == buffer_size; } void clear() volatile { index_read = index_write = 0; } bool peek(T *value) volatile { if (value == 0 || available() == 0) return false; *value = buffer[mask(index_read)]; return true; } int read() volatile { if (empty()) return -1; return buffer[mask(index_read++)]; } bool write(T value) volatile { if (full()) return false; buffer[mask(index_write++)] = value; return true; } private: uint32_t mask(uint32_t val) volatile { return buffer_mask & val; } static const uint32_t buffer_size = S; static const uint32_t buffer_mask = buffer_size - 1; volatile T buffer[buffer_size]; volatile uint32_t index_write; volatile uint32_t index_read; }; struct HalSerial { HalSerial() { host_connected = true; } void begin(int32_t) {} void end() {} int peek() { uint8_t value; return receive_buffer.peek(&value) ? value : -1; } int read() { return receive_buffer.read(); } size_t write(char c) { if (!host_connected) return 0; while (!transmit_buffer.free()); return transmit_buffer.write(c); } bool connected() { return host_connected; } uint16_t available() { return (uint16_t)receive_buffer.available(); } void flush() { receive_buffer.clear(); } uint8_t availableForWrite() { return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free(); } void flushTX() { if (host_connected) while (transmit_buffer.available()) { /* nada */ } } volatile RingBuffer<uint8_t, 128> receive_buffer; volatile RingBuffer<uint8_t, 128> transmit_buffer; volatile bool host_connected; }; typedef Serial1Class<HalSerial> MSerialT;
2301_81045437/Marlin
Marlin/src/HAL/LINUX/include/serial.h
C++
agpl-3.0
3,265
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #ifndef UNIT_TEST //#define GPIO_LOGGING // Full GPIO and Positional Logging #include "../../inc/MarlinConfig.h" #include "../shared/Delay.h" #include "hardware/IOLoggerCSV.h" #include "hardware/Heater.h" #include "hardware/LinearAxis.h" #include <stdio.h> #include <stdarg.h> #include <thread> #include <iostream> #include <fstream> extern void setup(); extern void loop(); // simple stdout / stdin implementation for fake serial port void write_serial_thread() { for (;;) { for (std::size_t i = usb_serial.transmit_buffer.available(); i > 0; i--) { fputc(usb_serial.transmit_buffer.read(), stdout); } std::this_thread::yield(); } } void read_serial_thread() { char buffer[255] = {}; for (;;) { std::size_t len = _MIN(usb_serial.receive_buffer.free(), 254U); if (fgets(buffer, len, stdin)) for (std::size_t i = 0; i < strlen(buffer); i++) usb_serial.receive_buffer.write(buffer[i]); std::this_thread::yield(); } } void simulation_loop() { Heater hotend(HEATER_0_PIN, TEMP_0_PIN); Heater bed(HEATER_BED_PIN, TEMP_BED_PIN); LinearAxis x_axis(X_ENABLE_PIN, X_DIR_PIN, X_STEP_PIN, X_MIN_PIN, X_MAX_PIN); LinearAxis y_axis(Y_ENABLE_PIN, Y_DIR_PIN, Y_STEP_PIN, Y_MIN_PIN, Y_MAX_PIN); LinearAxis z_axis(Z_ENABLE_PIN, Z_DIR_PIN, Z_STEP_PIN, Z_MIN_PIN, Z_MAX_PIN); LinearAxis extruder0(E0_ENABLE_PIN, E0_DIR_PIN, E0_STEP_PIN, P_NC, P_NC); #ifdef GPIO_LOGGING IOLoggerCSV logger("all_gpio_log.csv"); Gpio::attachLogger(&logger); std::ofstream position_log; position_log.open("axis_position_log.csv"); int32_t x,y,z; #endif for (;;) { hotend.update(); bed.update(); x_axis.update(); y_axis.update(); z_axis.update(); extruder0.update(); #ifdef GPIO_LOGGING if (x_axis.position != x || y_axis.position != y || z_axis.position != z) { uint64_t update = _MAX(x_axis.last_update, y_axis.last_update, z_axis.last_update); position_log << update << ", " << x_axis.position << ", " << y_axis.position << ", " << z_axis.position << std::endl; position_log.flush(); x = x_axis.position; y = y_axis.position; z = z_axis.position; } // flush the logger logger.flush(); #endif std::this_thread::yield(); } } int main() { std::thread write_serial (write_serial_thread); std::thread read_serial (read_serial_thread); #ifdef MYSERIAL1 MYSERIAL1.begin(BAUDRATE); SERIAL_ECHOLNPGM("x86_64 Initialized"); SERIAL_FLUSHTX(); #endif Clock::setFrequency(F_CPU); Clock::setTimeMultiplier(1.0); // some testing at 10x HAL_timer_init(); std::thread simulation (simulation_loop); DELAY_US(10000); setup(); for (;;) { loop(); std::this_thread::yield(); } simulation.join(); write_serial.join(); read_serial.join(); } #endif // UNIT_TEST #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/main.cpp
C++
agpl-3.0
3,786
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Support routines for X86_64 */ /** * Translation of routines & variables used by pinsDebug.h */ #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS #define IS_ANALOG(P) (DIGITAL_PIN_TO_ANALOG_PIN(P) >= 0 ? 1 : 0) #define digitalRead_mod(p) digitalRead(p) #define GET_ARRAY_PIN(p) pin_array[p].pin #define PRINT_ARRAY_NAME(x) do{ sprintf_P(buffer, PSTR("%-" STRINGIFY(MAX_NAME_LENGTH) "s"), pin_array[x].name); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN(p) do{ sprintf_P(buffer, PSTR("%3d "), p); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN_ANALOG(p) do{ sprintf_P(buffer, PSTR(" (A%2d) "), DIGITAL_PIN_TO_ANALOG_PIN(pin)); SERIAL_ECHO(buffer); }while(0) #define MULTI_NAME_PAD 16 // space needed to be pretty if not first name assigned to a pin // active ADC function/mode/code values for PINSEL registers constexpr int8_t ADC_pin_mode(pin_t pin) { return -1; } int8_t get_pin_mode(const pin_t pin) { return VALID_PIN(pin) ? 0 : -1; } bool GET_PINMODE(const pin_t pin) { const int8_t pin_mode = get_pin_mode(pin); if (pin_mode == -1 || pin_mode == ADC_pin_mode(pin)) // Invalid pin or active analog pin return false; return (Gpio::getMode(pin) != 0); // Input/output state } bool GET_ARRAY_IS_DIGITAL(const pin_t pin) { return (!IS_ANALOG(pin) || get_pin_mode(pin) != ADC_pin_mode(pin)); } void pwm_details(const pin_t pin) {} bool pwm_status(const pin_t) { return false; } void print_port(const pin_t) {}
2301_81045437/Marlin
Marlin/src/HAL/LINUX/pinsDebug.h
C++
agpl-3.0
2,328
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 * Copyright (c) 2009 Michael Margolis. All right reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /** * Based on "servo.h - Interrupt driven Servo library for Arduino using 16 bit timers - * Version 2 Copyright (c) 2009 Michael Margolis. All right reserved. * * The only modification was to update/delete macros to match the LPC176x. */ #include <stdint.h> // Macros //values in microseconds #define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo #define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached #define REFRESH_INTERVAL 20000 // minimum time to refresh servos in microseconds #define MAX_SERVOS 4 #define INVALID_SERVO 255 // flag indicating an invalid servo index // Types typedef struct { uint8_t nbr : 8 ; // a pin number from 0 to 254 (255 signals invalid pin) uint8_t isActive : 1 ; // true if this channel is enabled, pin not pulsed if false } ServoPin_t; typedef struct { ServoPin_t Pin; unsigned int pulse_width; // pulse width in microseconds } ServoInfo_t; // Global variables extern uint8_t ServoCount; extern ServoInfo_t servo_info[MAX_SERVOS];
2301_81045437/Marlin
Marlin/src/HAL/LINUX/servo_private.h
C
agpl-3.0
2,969
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if ALL(HAS_MARLINUI_U8GLIB, HAS_MEDIA) && (LCD_PINS_D4 == SD_SCK_PIN || LCD_PINS_EN == SD_MOSI_PIN || DOGLCD_SCK == SD_SCK_PIN || DOGLCD_MOSI == SD_MOSI_PIN) #define SOFTWARE_SPI // If the SD card and LCD adapter share the same SPI pins, then software SPI is currently // needed due to the speed and mode required for communicating with each device being different. // This requirement can be removed if the SPI access to these devices is updated to use // spiBeginTransaction. #endif // Onboard SD //#define SD_SCK_PIN P0_07 //#define SD_MISO_PIN P0_08 //#define SD_MOSI_PIN P0_09 //#define SD_SS_PIN P0_06 // External SD #ifndef SD_SCK_PIN #define SD_SCK_PIN 50 #endif #ifndef SD_MISO_PIN #define SD_MISO_PIN 51 #endif #ifndef SD_MOSI_PIN #define SD_MOSI_PIN 52 #endif #ifndef SD_SS_PIN #define SD_SS_PIN 53 #endif #ifndef SDSS #define SDSS SD_SS_PIN #endif
2301_81045437/Marlin
Marlin/src/HAL/LINUX/spi_pins.h
C
agpl-3.0
1,872
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_LINUX__ #include "hardware/Timer.h" #include "../../inc/MarlinConfig.h" /** * Use POSIX signals to attempt to emulate Interrupts * This has many limitations and is not fit for the purpose */ HAL_STEP_TIMER_ISR(); HAL_TEMP_TIMER_ISR(); Timer timers[2]; void HAL_timer_init() { timers[0].init(0, STEPPER_TIMER_RATE, TIMER0_IRQHandler); timers[1].init(1, TEMP_TIMER_RATE, TIMER1_IRQHandler); } void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { timers[timer_num].start(frequency); } void HAL_timer_enable_interrupt(const uint8_t timer_num) { timers[timer_num].enable(); } void HAL_timer_disable_interrupt(const uint8_t timer_num) { timers[timer_num].disable(); } bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { return timers[timer_num].enabled(); } void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) { timers[timer_num].setCompare(compare); } hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) { return timers[timer_num].getCompare(); } hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { return timers[timer_num].getCount(); } #endif // __PLAT_LINUX__
2301_81045437/Marlin
Marlin/src/HAL/LINUX/timers.cpp
C++
agpl-3.0
2,039
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL timers for Linux X86_64 */ #include <stdint.h> // ------------------------ // Defines // ------------------------ #define FORCE_INLINE __attribute__((always_inline)) inline typedef uint32_t hal_timer_t; #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF #define HAL_TIMER_RATE ((SystemCoreClock) / 4) // frequency of timers peripherals #ifndef MF_TIMER_STEP #define MF_TIMER_STEP 0 // Timer Index for Stepper #endif #ifndef MF_TIMER_PULSE #define MF_TIMER_PULSE MF_TIMER_STEP #endif #ifndef MF_TIMER_TEMP #define MF_TIMER_TEMP 1 // Timer Index for Temperature #endif #define TEMP_TIMER_RATE 1000000 #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US) #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP) #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP) #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP) #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP) #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP) #ifndef HAL_STEP_TIMER_ISR #define HAL_STEP_TIMER_ISR() extern "C" void TIMER0_IRQHandler() #endif #ifndef HAL_TEMP_TIMER_ISR #define HAL_TEMP_TIMER_ISR() extern "C" void TIMER1_IRQHandler() #endif // PWM timer #define HAL_PWM_TIMER #define HAL_PWM_TIMER_ISR() extern "C" void TIMER3_IRQHandler() #define HAL_PWM_TIMER_IRQn void HAL_timer_init(); void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare); hal_timer_t HAL_timer_get_compare(const uint8_t timer_num); hal_timer_t HAL_timer_get_count(const uint8_t timer_num); FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) { const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks; if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp); } void HAL_timer_enable_interrupt(const uint8_t timer_num); void HAL_timer_disable_interrupt(const uint8_t timer_num); bool HAL_timer_interrupt_enabled(const uint8_t timer_num); #define HAL_timer_isr_prologue(T) NOOP #define HAL_timer_isr_epilogue(T) NOOP
2301_81045437/Marlin
Marlin/src/HAL/LINUX/timers.h
C
agpl-3.0
3,667
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Linux LCD-specific defines */
2301_81045437/Marlin
Marlin/src/HAL/LINUX/u8g/LCD_defines.h
C
agpl-3.0
914
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" #include "../shared/Delay.h" #include "../../core/millis_t.h" #include <usb/usb.h> #include <usb/usbcfg.h> #include <usb/usbhw.h> #include <usb/usbcore.h> #include <usb/cdc.h> #include <usb/cdcuser.h> #include <usb/mscuser.h> #include <CDCSerial.h> #include <usb/mscuser.h> DefaultSerial1 USBSerial(false, UsbSerial); uint32_t MarlinHAL::adc_result = 0; pin_t MarlinHAL::adc_pin = 0; // U8glib required functions extern "C" { void u8g_xMicroDelay(uint16_t val) { DELAY_US(val); } void u8g_MicroDelay() { u8g_xMicroDelay(1); } void u8g_10MicroDelay() { u8g_xMicroDelay(10); } void u8g_Delay(uint16_t val) { delay(val); } } // return free heap space int freeMemory() { char stack_end; void *heap_start = malloc(sizeof(uint32_t)); if (heap_start == 0) return 0; uint32_t result = (uint32_t)&stack_end - (uint32_t)heap_start; free(heap_start); return result; } extern "C" { #include <debug_frmwrk.h> int isLPC1769(); void disk_timerproc(); } extern uint32_t MSC_SD_Init(uint8_t pdrv); void SysTick_Callback() { disk_timerproc(); } TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial()); void MarlinHAL::init() { // Init LEDs #if PIN_EXISTS(LED) SET_DIR_OUTPUT(LED_PIN); WRITE_PIN_CLR(LED_PIN); #if PIN_EXISTS(LED2) SET_DIR_OUTPUT(LED2_PIN); WRITE_PIN_CLR(LED2_PIN); #if PIN_EXISTS(LED3) SET_DIR_OUTPUT(LED3_PIN); WRITE_PIN_CLR(LED3_PIN); #if PIN_EXISTS(LED4) SET_DIR_OUTPUT(LED4_PIN); WRITE_PIN_CLR(LED4_PIN); #endif #endif #endif // Flash status LED 3 times to indicate Marlin has started booting for (uint8_t i = 0; i < 6; ++i) { TOGGLE(LED_PIN); delay(100); } #endif // Init Servo Pins #define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW) #if HAS_SERVO_0 INIT_SERVO(0); #endif #if HAS_SERVO_1 INIT_SERVO(1); #endif #if HAS_SERVO_2 INIT_SERVO(2); #endif #if HAS_SERVO_3 INIT_SERVO(3); #endif #if HAS_SERVO_4 INIT_SERVO(4); #endif #if HAS_SERVO_5 INIT_SERVO(5); #endif //debug_frmwrk_init(); //_DBG("\n\nDebug running\n"); // Initialize the SD card chip select pins as soon as possible #if PIN_EXISTS(SD_SS) OUT_WRITE(SD_SS_PIN, HIGH); #endif #if PIN_EXISTS(ONBOARD_SD_CS) && ONBOARD_SD_CS_PIN != SD_SS_PIN OUT_WRITE(ONBOARD_SD_CS_PIN, HIGH); #endif #ifdef LPC1768_ENABLE_CLKOUT_12M /** * CLKOUTCFG register * bit 8 (CLKOUT_EN) = enables CLKOUT signal. Disabled for now to prevent glitch when enabling GPIO. * bits 7:4 (CLKOUTDIV) = set to 0 for divider setting of /1 * bits 3:0 (CLKOUTSEL) = set to 1 to select main crystal oscillator as CLKOUT source */ LPC_SC->CLKOUTCFG = (0<<8)|(0<<4)|(1<<0); // set P1.27 pin to function 01 (CLKOUT) PINSEL_CFG_Type PinCfg; PinCfg.Portnum = 1; PinCfg.Pinnum = 27; PinCfg.Funcnum = 1; // function 01 (CLKOUT) PinCfg.OpenDrain = 0; // not open drain PinCfg.Pinmode = 2; // no pull-up/pull-down PINSEL_ConfigPin(&PinCfg); // now set CLKOUT_EN bit SBI(LPC_SC->CLKOUTCFG, 8); #endif USB_Init(); // USB Initialization USB_Connect(false); // USB clear connection delay(1000); // Give OS time to notice USB_Connect(true); TERN_(HAS_SD_HOST_DRIVE, MSC_SD_Init(0)); // Enable USB SD card access const millis_t usb_timeout = millis() + 2000; while (!USB_Configuration && PENDING(millis(), usb_timeout)) { delay(50); idletask(); #if PIN_EXISTS(LED) TOGGLE(LED_PIN); // Flash quickly during USB initialization #endif } HAL_timer_init(); TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler } #include "../../sd/cardreader.h" // HAL idle task void MarlinHAL::idletask() { #if HAS_SHARED_MEDIA // If Marlin is using the SD card we need to lock it to prevent access from // a PC via USB. // Other HALs use IS_SD_PRINTING() and IS_SD_FILE_OPEN() to check for access but // this will not reliably detect delete operations. To be safe we will lock // the disk if Marlin has it mounted. Unfortunately there is currently no way // to unmount the disk from the LCD menu. // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN()) if (card.isMounted()) MSC_Aquire_Lock(); else MSC_Release_Lock(); #endif // Perform USB stack housekeeping MSC_RunDeferredCommands(); } void MarlinHAL::reboot() { NVIC_SystemReset(); } uint8_t MarlinHAL::get_reset_source() { #if ENABLED(USE_WATCHDOG) if (watchdog_timed_out()) return RST_WATCHDOG; #endif return RST_POWER_ON; } void MarlinHAL::clear_reset_source() { watchdog_clear_timeout_flag(); } void flashFirmware(const int16_t) { delay(500); // Give OS time to disconnect USB_Connect(false); // USB clear connection delay(1000); // Give OS time to notice hal.reboot(); } #if ENABLED(USE_WATCHDOG) #include <lpc17xx_wdt.h> #define WDT_TIMEOUT_US TERN(WATCHDOG_DURATION_8S, 8000000, 4000000) // 4 or 8 second timeout void MarlinHAL::watchdog_init() { #if ENABLED(WATCHDOG_RESET_MANUAL) // We enable the watchdog timer, but only for the interrupt. // Configure WDT to only trigger an interrupt // Disable WDT interrupt (just in case, to avoid triggering it!) NVIC_DisableIRQ(WDT_IRQn); // We NEED memory barriers to ensure Interrupts are actually disabled! // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the ) __DSB(); __ISB(); // Configure WDT to only trigger an interrupt // Initialize WDT with the given parameters WDT_Init(WDT_CLKSRC_IRC, WDT_MODE_INT_ONLY); // Configure and enable WDT interrupt. NVIC_ClearPendingIRQ(WDT_IRQn); NVIC_SetPriority(WDT_IRQn, 0); // Use highest priority, so we detect all kinds of lockups NVIC_EnableIRQ(WDT_IRQn); #else WDT_Init(WDT_CLKSRC_IRC, WDT_MODE_RESET); #endif WDT_Start(WDT_TIMEOUT_US); } void MarlinHAL::watchdog_refresh() { WDT_Feed(); #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED) TOGGLE(LED_PIN); // heartbeat indicator #endif } // Timeout state bool MarlinHAL::watchdog_timed_out() { return TEST(WDT_ReadTimeOutFlag(), 0); } void MarlinHAL::watchdog_clear_timeout_flag() { WDT_ClrTimeOutFlag(); } #endif // USE_WATCHDOG #include "../../../gcode/parser.h" // For M42/M43, scan command line for pin code // return index into pin map array if found and the pin is valid. // return dval if not found or not a valid pin. int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) { const uint16_t val = (uint16_t)parser.intval(code, -1), port = val / 100, pin = val % 100; const int16_t ind = (port < ((NUM_DIGITAL_PINS) >> 5) && pin < 32) ? ((port << 5) | pin) : -2; return ind > -1 ? ind : dval; } #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/HAL.cpp
C++
agpl-3.0
7,951
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL_LPC1768/HAL.h * Hardware Abstraction Layer for NXP LPC1768 */ #define CPU_32_BIT #include <stdint.h> #include <stdarg.h> #include <algorithm> extern "C" volatile uint32_t _millis; #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" #include "MarlinSerial.h" #include <adc.h> #include <pinmapping.h> #include <CDCSerial.h> // ------------------------ // Serial ports // ------------------------ typedef ForwardSerial1Class< decltype(UsbSerial) > DefaultSerial1; extern DefaultSerial1 USBSerial; #define _MSERIAL(X) MSerial##X #define MSERIAL(X) _MSERIAL(X) #if SERIAL_PORT == -1 #define MYSERIAL1 USBSerial #elif WITHIN(SERIAL_PORT, 0, 3) #define MYSERIAL1 MSERIAL(SERIAL_PORT) #else #error "SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB." #endif #ifdef SERIAL_PORT_2 #if SERIAL_PORT_2 == -1 #define MYSERIAL2 USBSerial #elif WITHIN(SERIAL_PORT_2, 0, 3) #define MYSERIAL2 MSERIAL(SERIAL_PORT_2) #else #error "SERIAL_PORT_2 must be from 0 to 3. You can also use -1 if the board supports Native USB." #endif #endif #ifdef SERIAL_PORT_3 #if SERIAL_PORT_3 == -1 #define MYSERIAL3 USBSerial #elif WITHIN(SERIAL_PORT_3, 0, 3) #define MYSERIAL3 MSERIAL(SERIAL_PORT_3) #else #error "SERIAL_PORT_3 must be from 0 to 3. You can also use -1 if the board supports Native USB." #endif #endif #ifdef MMU2_SERIAL_PORT #if MMU2_SERIAL_PORT == -1 #define MMU2_SERIAL USBSerial #elif WITHIN(MMU2_SERIAL_PORT, 0, 3) #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT) #else #error "MMU2_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB." #endif #endif #ifdef LCD_SERIAL_PORT #if LCD_SERIAL_PORT == -1 #define LCD_SERIAL USBSerial #elif WITHIN(LCD_SERIAL_PORT, 0, 3) #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT) #else #error "LCD_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB." #endif #if HAS_DGUS_LCD #define LCD_SERIAL_TX_BUFFER_FREE() LCD_SERIAL.available() #endif #endif // // Interrupts // #define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq() #define CRITICAL_SECTION_END() if (irqon) __enable_irq() // // ADC // #define ADC_MEDIAN_FILTER_SIZE (23) // Higher values increase step delay (phase shift), // (ADC_MEDIAN_FILTER_SIZE + 1) / 2 sample step delay (12 samples @ 500Hz: 24ms phase shift) // Memory usage per ADC channel (bytes): (6 * ADC_MEDIAN_FILTER_SIZE) + 16 // 8 * ((6 * 23) + 16 ) = 1232 Bytes for 8 channels #define ADC_LOWPASS_K_VALUE (2) // Higher values increase rise time // Rise time sample delays for 100% signal convergence on full range step // (1 : 13, 2 : 32, 3 : 67, 4 : 139, 5 : 281, 6 : 565, 7 : 1135, 8 : 2273) // K = 6, 565 samples, 500Hz sample rate, 1.13s convergence on full range step // Memory usage per ADC channel (bytes): 4 (32 Bytes for 8 channels) #define HAL_ADC_VREF_MV 3300 // ADC voltage reference #define HAL_ADC_RESOLUTION 12 // 15 bit maximum, raw temperature is stored as int16_t #define HAL_ADC_FILTERED // Disable oversampling done in Marlin as ADC values already filtered in HAL // // Pin Mapping for M42, M43, M226 // // Test whether the pin is valid constexpr bool VALID_PIN(const pin_t pin) { return LPC176x::pin_is_valid(pin); } // Get the analog index for a digital pin constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t pin) { return (LPC176x::pin_is_valid(pin) && LPC176x::pin_has_adc(pin)) ? pin : -1; } // Return the index of a pin number constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) { return LPC176x::pin_index(pin); } // Get the pin number at the given index constexpr pin_t GET_PIN_MAP_PIN(const int16_t index) { return LPC176x::pin_index(index); } // Parse a G-code word into a pin index int16_t PARSED_PIN_INDEX(const char code, const int16_t dval); // P0.6 thru P0.9 are for the onboard SD card #define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09, // ------------------------ // Defines // ------------------------ #ifndef PLATFORM_M997_SUPPORT #define PLATFORM_M997_SUPPORT #endif void flashFirmware(const int16_t); #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment // Default graphical display delays #define CPU_ST7920_DELAY_1 600 #define CPU_ST7920_DELAY_2 750 #define CPU_ST7920_DELAY_3 750 // ------------------------ // Free Memory Accessor // ------------------------ #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif int freeMemory(); #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} static void init(); // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return !__get_PRIMASK(); } static void isr_on() { __enable_irq(); } static void isr_off() { __disable_irq(); } static void delay_ms(const int ms) { _delay_ms(ms); } // Watchdog static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); static bool watchdog_timed_out() IF_DISABLED(USE_WATCHDOG, { return false; }); static void watchdog_clear_timeout_flag() IF_DISABLED(USE_WATCHDOG, {}); // Tasks, called from idle() static void idletask(); // Reset static uint8_t get_reset_source(); static void clear_reset_source(); // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>; // Called by Temperature::init once at startup static void adc_init() {} // Called by Temperature::init for each sensor at startup static void adc_enable(const pin_t pin) { FilteredADC::enable_channel(pin); } // Begin ADC sampling on the given pin. Called from Temperature::isr! static uint32_t adc_result; static pin_t adc_pin; static void adc_start(const pin_t pin) { adc_pin = pin; } // Is the ADC ready for reading? static bool adc_ready() { return LPC176x::adc_hardware.done(LPC176x::pin_get_adc_channel(adc_pin)); } // The current value of the ADC register static uint16_t adc_value() { adc_result = FilteredADC::read(adc_pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits return uint16_t(adc_result); } /** * Set the PWM duty cycle for the pin to the given value. * Optionally invert the duty cycle [default = false] * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255] */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false); /** * Set the frequency of the timer corresponding to the provided pin * All Hardware PWM pins will run at the same frequency and * All Software PWM pins will run at the same frequency */ static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired); };
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/HAL.h
C++
agpl-3.0
8,447
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Software SPI functions originally from Arduino Sd2Card Library * Copyright (c) 2009 by William Greiman */ /** * For TARGET_LPC1768 */ /** * Hardware SPI and Software SPI implementations are included in this file. * The hardware SPI runs faster and has higher throughput but is not compatible * with some LCD interfaces/adapters. * * Control of the slave select pin(s) is handled by the calling routines. * * Some of the LCD interfaces/adapters result in the LCD SPI and the SD card * SPI sharing pins. The SCK, MOSI & MISO pins can NOT be set/cleared with * WRITE nor digitalWrite when the hardware SPI module within the LPC17xx is * active. If any of these pins are shared then the software SPI must be used. * * A more sophisticated hardware SPI can be found at the following link. * This implementation has not been fully debugged. * https://github.com/MarlinFirmware/Marlin/tree/071c7a78f27078fd4aee9a3ef365fcf5e143531e */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" #include <SPI.h> // Hardware SPI and SPIClass #include <lpc17xx_pinsel.h> #include <lpc17xx_clkpwr.h> #include "../shared/HAL_SPI.h" // ------------------------ // Public functions // ------------------------ #if ENABLED(SOFTWARE_SPI) // Software SPI #include <SoftwareSPI.h> static uint8_t SPI_speed = SPI_FULL_SPEED; static uint8_t spiTransfer(uint8_t b) { return swSpiTransfer(b, SPI_speed, SD_SCK_PIN, SD_MISO_PIN, SD_MOSI_PIN); } void spiBegin() { swSpiBegin(SD_SCK_PIN, SD_MISO_PIN, SD_MOSI_PIN); } void spiInit(uint8_t spiRate) { SPI_speed = swSpiInit(spiRate, SD_SCK_PIN, SD_MOSI_PIN); } uint8_t spiRec() { return spiTransfer(0xFF); } void spiRead(uint8_t*buf, uint16_t nbyte) { for (int i = 0; i < nbyte; i++) buf[i] = spiTransfer(0xFF); } void spiSend(uint8_t b) { (void)spiTransfer(b); } void spiSend(const uint8_t *buf, size_t nbyte) { for (uint16_t i = 0; i < nbyte; i++) (void)spiTransfer(buf[i]); } void spiSendBlock(uint8_t token, const uint8_t *buf) { (void)spiTransfer(token); for (uint16_t i = 0; i < 512; i++) (void)spiTransfer(buf[i]); } #else #ifdef SD_SPI_SPEED #define INIT_SPI_SPEED SD_SPI_SPEED #else #define INIT_SPI_SPEED SPI_FULL_SPEED #endif void spiBegin() { spiInit(INIT_SPI_SPEED); } // Set up SCK, MOSI & MISO pins for SSP0 void spiInit(uint8_t spiRate) { #if SD_MISO_PIN == BOARD_SPI1_MISO_PIN SPI.setModule(1); #elif SD_MISO_PIN == BOARD_SPI2_MISO_PIN SPI.setModule(2); #endif SPI.setDataSize(DATA_SIZE_8BIT); SPI.setDataMode(SPI_MODE0); SPI.setClock(SPISettings::spiRate2Clock(spiRate)); SPI.begin(); } static uint8_t doio(uint8_t b) { return SPI.transfer(b & 0x00FF) & 0x00FF; } void spiSend(uint8_t b) { doio(b); } void spiSend(const uint8_t *buf, size_t nbyte) { for (uint16_t i = 0; i < nbyte; i++) doio(buf[i]); } void spiSend(uint32_t chan, byte b) {} void spiSend(uint32_t chan, const uint8_t *buf, size_t nbyte) {} // Read single byte from SPI uint8_t spiRec() { return doio(0xFF); } uint8_t spiRec(uint32_t chan) { return 0; } // Read from SPI into buffer void spiRead(uint8_t *buf, uint16_t nbyte) { for (uint16_t i = 0; i < nbyte; i++) buf[i] = doio(0xFF); } uint8_t spiTransfer(uint8_t b) { return doio(b); } // Write from buffer to SPI void spiSendBlock(uint8_t token, const uint8_t *buf) { (void)spiTransfer(token); for (uint16_t i = 0; i < 512; i++) (void)spiTransfer(buf[i]); } // Begin SPI transaction, set clock, bit order, data mode void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { // TODO: Implement this method } #endif // SOFTWARE_SPI /** * @brief Wait until TXE (tx empty) flag is set and BSY (busy) flag unset. */ static inline void waitSpiTxEnd(LPC_SSP_TypeDef *spi_d) { while (SSP_GetStatus(spi_d, SSP_STAT_TXFIFO_EMPTY) == RESET) { /* nada */ } // wait until TXE=1 while (SSP_GetStatus(spi_d, SSP_STAT_BUSY) == SET) { /* nada */ } // wait until BSY=0 } // Retain the pin init state of the SPI, to avoid init more than once, // even if more instances of SPIClass exist static bool spiInitialised[BOARD_NR_SPI] = { false }; SPIClass::SPIClass(uint8_t device) { // Init things specific to each SPI device // clock divider setup is a bit of hack, and needs to be improved at a later date. #if BOARD_NR_SPI >= 1 _settings[0].spi_d = LPC_SSP0; _settings[0].dataMode = SPI_MODE0; _settings[0].dataSize = DATA_SIZE_8BIT; _settings[0].clock = SPI_CLOCK_MAX; //_settings[0].clockDivider = determine_baud_rate(_settings[0].spi_d, _settings[0].clock); #endif #if BOARD_NR_SPI >= 2 _settings[1].spi_d = LPC_SSP1; _settings[1].dataMode = SPI_MODE0; _settings[1].dataSize = DATA_SIZE_8BIT; _settings[1].clock = SPI_CLOCK_MAX; //_settings[1].clockDivider = determine_baud_rate(_settings[1].spi_d, _settings[1].clock); #endif setModule(device); // Init the GPDMA controller // TODO: call once in the constructor? or each time? GPDMA_Init(); } SPIClass::SPIClass(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel) { #if BOARD_NR_SPI >= 1 if (mosi == BOARD_SPI1_MOSI_PIN) SPIClass(1); #endif #if BOARD_NR_SPI >= 2 if (mosi == BOARD_SPI2_MOSI_PIN) SPIClass(2); #endif } void SPIClass::begin() { // Init the SPI pins in the first begin call if ((_currentSetting->spi_d == LPC_SSP0 && spiInitialised[0] == false) || (_currentSetting->spi_d == LPC_SSP1 && spiInitialised[1] == false)) { pin_t sck, miso, mosi; if (_currentSetting->spi_d == LPC_SSP0) { sck = BOARD_SPI1_SCK_PIN; miso = BOARD_SPI1_MISO_PIN; mosi = BOARD_SPI1_MOSI_PIN; spiInitialised[0] = true; } else if (_currentSetting->spi_d == LPC_SSP1) { sck = BOARD_SPI2_SCK_PIN; miso = BOARD_SPI2_MISO_PIN; mosi = BOARD_SPI2_MOSI_PIN; spiInitialised[1] = true; } PINSEL_CFG_Type PinCfg; // data structure to hold init values PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = LPC176x::pin_bit(sck); PinCfg.Portnum = LPC176x::pin_port(sck); PINSEL_ConfigPin(&PinCfg); SET_OUTPUT(sck); PinCfg.Pinnum = LPC176x::pin_bit(miso); PinCfg.Portnum = LPC176x::pin_port(miso); PINSEL_ConfigPin(&PinCfg); SET_INPUT(miso); PinCfg.Pinnum = LPC176x::pin_bit(mosi); PinCfg.Portnum = LPC176x::pin_port(mosi); PINSEL_ConfigPin(&PinCfg); SET_OUTPUT(mosi); } updateSettings(); SSP_Cmd(_currentSetting->spi_d, ENABLE); // start SSP running } void SPIClass::beginTransaction(const SPISettings &cfg) { setBitOrder(cfg.bitOrder); setDataMode(cfg.dataMode); setDataSize(cfg.dataSize); //setClockDivider(determine_baud_rate(_currentSetting->spi_d, settings.clock)); begin(); } uint8_t SPIClass::transfer(const uint16_t b) { // Send and receive a single byte SSP_ReceiveData(_currentSetting->spi_d); // read any previous data SSP_SendData(_currentSetting->spi_d, b); waitSpiTxEnd(_currentSetting->spi_d); // wait for it to finish return SSP_ReceiveData(_currentSetting->spi_d); } uint16_t SPIClass::transfer16(const uint16_t data) { return (transfer((data >> 8) & 0xFF) << 8) | (transfer(data & 0xFF) & 0xFF); } void SPIClass::end() { // Neither is needed for Marlin //SSP_Cmd(_currentSetting->spi_d, DISABLE); //SSP_DeInit(_currentSetting->spi_d); } void SPIClass::send(uint8_t data) { SSP_SendData(_currentSetting->spi_d, data); } void SPIClass::dmaSend(void *buf, uint16_t length, bool minc) { //TODO: LPC dma can only write 0xFFF bytes at once. GPDMA_Channel_CFG_Type GPDMACfg; /* Configure GPDMA channel 0 -------------------------------------------------------------*/ /* DMA Channel 0 */ GPDMACfg.ChannelNum = 0; // Source memory GPDMACfg.SrcMemAddr = (uint32_t)buf; // Destination memory - Not used GPDMACfg.DstMemAddr = 0; // Transfer size GPDMACfg.TransferSize = length; // Transfer width GPDMACfg.TransferWidth = (_currentSetting->dataSize == DATA_SIZE_16BIT) ? GPDMA_WIDTH_HALFWORD : GPDMA_WIDTH_BYTE; // Transfer type GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_M2P; // Source connection - unused GPDMACfg.SrcConn = 0; // Destination connection GPDMACfg.DstConn = (_currentSetting->spi_d == LPC_SSP0) ? GPDMA_CONN_SSP0_Tx : GPDMA_CONN_SSP1_Tx; GPDMACfg.DMALLI = 0; // Enable dma on SPI SSP_DMACmd(_currentSetting->spi_d, SSP_DMA_TX, ENABLE); // Only increase memory if minc is true GPDMACfg.MemoryIncrease = (minc ? GPDMA_DMACCxControl_SI : 0); // Setup channel with given parameter GPDMA_Setup(&GPDMACfg); // Enable DMA GPDMA_ChannelCmd(0, ENABLE); /** * Observed behaviour on normal data transfer completion (SKR 1.3 board / LPC1768 MCU) * GPDMA_STAT_INTTC flag is SET * GPDMA_STAT_INTERR flag is NOT SET * GPDMA_STAT_RAWINTTC flag is NOT SET * GPDMA_STAT_RAWINTERR flag is SET */ // Wait for data transfer while (!GPDMA_IntGetStatus(GPDMA_STAT_INTTC, 0) && !GPDMA_IntGetStatus(GPDMA_STAT_INTERR, 0)) {} // Clear err and int GPDMA_ClearIntPending (GPDMA_STATCLR_INTTC, 0); GPDMA_ClearIntPending (GPDMA_STATCLR_INTERR, 0); // Disable DMA GPDMA_ChannelCmd(0, DISABLE); waitSpiTxEnd(_currentSetting->spi_d); SSP_DMACmd(_currentSetting->spi_d, SSP_DMA_TX, DISABLE); } void SPIClass::dmaSendAsync(void *buf, uint16_t length, bool minc) { //TODO: LPC dma can only write 0xFFF bytes at once. GPDMA_Channel_CFG_Type GPDMACfg; /* Configure GPDMA channel 0 -------------------------------------------------------------*/ /* DMA Channel 0 */ GPDMACfg.ChannelNum = 0; // Source memory GPDMACfg.SrcMemAddr = (uint32_t)buf; // Destination memory - Not used GPDMACfg.DstMemAddr = 0; // Transfer size GPDMACfg.TransferSize = length; // Transfer width GPDMACfg.TransferWidth = (_currentSetting->dataSize == DATA_SIZE_16BIT) ? GPDMA_WIDTH_HALFWORD : GPDMA_WIDTH_BYTE; // Transfer type GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_M2P; // Source connection - unused GPDMACfg.SrcConn = 0; // Destination connection GPDMACfg.DstConn = (_currentSetting->spi_d == LPC_SSP0) ? GPDMA_CONN_SSP0_Tx : GPDMA_CONN_SSP1_Tx; GPDMACfg.DMALLI = 0; // Enable dma on SPI SSP_DMACmd(_currentSetting->spi_d, SSP_DMA_TX, ENABLE); // Only increase memory if minc is true GPDMACfg.MemoryIncrease = (minc ? GPDMA_DMACCxControl_SI : 0); // Setup channel with given parameter GPDMA_Setup(&GPDMACfg); // Enable DMA GPDMA_ChannelCmd(0, ENABLE); } uint16_t SPIClass::read() { return SSP_ReceiveData(_currentSetting->spi_d); } void SPIClass::read(uint8_t *buf, uint32_t len) { for (uint16_t i = 0; i < len; i++) buf[i] = transfer(0xFF); } void SPIClass::setClock(uint32_t clock) { _currentSetting->clock = clock; } void SPIClass::setModule(uint8_t device) { _currentSetting = &_settings[device - 1]; } // SPI channels are called 1, 2, and 3 but the array is zero-indexed void SPIClass::setBitOrder(uint8_t bitOrder) { _currentSetting->bitOrder = bitOrder; } void SPIClass::setDataMode(uint8_t dataMode) { _currentSetting->dataMode = dataMode; } void SPIClass::setDataSize(uint32_t dataSize) { _currentSetting->dataSize = dataSize; } /** * Set up/tear down */ void SPIClass::updateSettings() { //SSP_DeInit(_currentSetting->spi_d); //todo: need force de init?! // Divide PCLK by 2 for SSP0 //CLKPWR_SetPCLKDiv(_currentSetting->spi_d == LPC_SSP0 ? CLKPWR_PCLKSEL_SSP0 : CLKPWR_PCLKSEL_SSP1, CLKPWR_PCLKSEL_CCLK_DIV_2); SSP_CFG_Type HW_SPI_init; // data structure to hold init values SSP_ConfigStructInit(&HW_SPI_init); // set values for SPI mode HW_SPI_init.ClockRate = _currentSetting->clock; HW_SPI_init.Databit = _currentSetting->dataSize; /** * SPI Mode CPOL CPHA Shift SCK-edge Capture SCK-edge * 0 0 0 Falling Rising * 1 0 1 Rising Falling * 2 1 0 Rising Falling * 3 1 1 Falling Rising */ switch (_currentSetting->dataMode) { case SPI_MODE0: HW_SPI_init.CPHA = SSP_CPHA_FIRST; HW_SPI_init.CPOL = SSP_CPOL_HI; break; case SPI_MODE1: HW_SPI_init.CPHA = SSP_CPHA_SECOND; HW_SPI_init.CPOL = SSP_CPOL_HI; break; case SPI_MODE2: HW_SPI_init.CPHA = SSP_CPHA_FIRST; HW_SPI_init.CPOL = SSP_CPOL_LO; break; case SPI_MODE3: HW_SPI_init.CPHA = SSP_CPHA_SECOND; HW_SPI_init.CPOL = SSP_CPOL_LO; break; default: break; } // TODO: handle bitOrder SSP_Init(_currentSetting->spi_d, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers } #if SD_MISO_PIN == BOARD_SPI1_MISO_PIN SPIClass SPI(1); #elif SD_MISO_PIN == BOARD_SPI2_MISO_PIN SPIClass SPI(2); #endif #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/HAL_SPI.cpp
C++
agpl-3.0
13,809
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <SPI.h> /** * Marlin currently requires 3 SPI classes: * * SPIClass: * This class is normally provided by frameworks and has a semi-default interface. * This is needed because some libraries reference it globally. * * SPISettings: * Container for SPI configs for SPIClass. As above, libraries may reference it globally. * * These two classes are often provided by frameworks so we cannot extend them to add * useful methods for Marlin. * * MarlinSPI: * Provides the default SPIClass interface plus some Marlin goodies such as a simplified * interface for SPI DMA transfer. * */ using MarlinSPI = SPIClass;
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/MarlinSPI.h
C
agpl-3.0
1,515
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "MarlinSerial.h" #include "../../inc/MarlinConfig.h" #if USING_HW_SERIAL0 MarlinSerial _MSerial0(LPC_UART0); MSerialT MSerial0(true, _MSerial0); extern "C" void UART0_IRQHandler() { _MSerial0.IRQHandler(); } #endif #if USING_HW_SERIAL1 MarlinSerial _MSerial1((LPC_UART_TypeDef *) LPC_UART1); MSerialT MSerial1(true, _MSerial1); extern "C" void UART1_IRQHandler() { _MSerial1.IRQHandler(); } #endif #if USING_HW_SERIAL2 MarlinSerial _MSerial2(LPC_UART2); MSerialT MSerial2(true, _MSerial2); extern "C" void UART2_IRQHandler() { _MSerial2.IRQHandler(); } #endif #if USING_HW_SERIAL3 MarlinSerial _MSerial3(LPC_UART3); MSerialT MSerial3(true, _MSerial3); extern "C" void UART3_IRQHandler() { _MSerial3.IRQHandler(); } #endif #if ENABLED(EMERGENCY_PARSER) bool MarlinSerial::recv_callback(const char c) { // Need to figure out which serial port we are and react in consequence (Marlin does not have CONTAINER_OF macro) if (false) {} #if USING_HW_SERIAL0 else if (this == &_MSerial0) emergency_parser.update(MSerial0.emergency_state, c); #endif #if USING_HW_SERIAL1 else if (this == &_MSerial1) emergency_parser.update(MSerial1.emergency_state, c); #endif #if USING_HW_SERIAL2 else if (this == &_MSerial2) emergency_parser.update(MSerial2.emergency_state, c); #endif #if USING_HW_SERIAL3 else if (this == &_MSerial3) emergency_parser.update(MSerial3.emergency_state, c); #endif return true; } #endif #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/MarlinSerial.cpp
C++
agpl-3.0
2,416
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <HardwareSerial.h> #include <WString.h> #include "../../inc/MarlinConfigPre.h" #if ENABLED(EMERGENCY_PARSER) #include "../../feature/e_parser.h" #endif #include "../../core/serial_hook.h" class MarlinSerial : public HardwareSerial<RX_BUFFER_SIZE, TX_BUFFER_SIZE> { public: MarlinSerial(LPC_UART_TypeDef *UARTx) : HardwareSerial<RX_BUFFER_SIZE, TX_BUFFER_SIZE>(UARTx) { } void end() {} uint8_t availableForWrite(void) { /* flushTX(); */ return TX_BUFFER_SIZE; } #if ENABLED(EMERGENCY_PARSER) bool recv_callback(const char c) override; #endif }; // On LPC176x framework, HardwareSerial does not implement the same interface as Arduino's Serial, so overloads // of 'available' and 'read' method are not used in this multiple inheritance scenario. // Instead, use a ForwardSerial here that adapts the interface. typedef ForwardSerial1Class<MarlinSerial> MSerialT; extern MSerialT MSerial0; extern MSerialT MSerial1; extern MSerialT MSerial2; extern MSerialT MSerial3; // Consequently, we can't use a RuntimeSerial either. The workaround would be to use // a RuntimeSerial<ForwardSerial<MarlinSerial>> type here. Ignore for now until it's actually required. #if ENABLED(SERIAL_RUNTIME_HOOK) #error "SERIAL_RUNTIME_HOOK is not yet supported for LPC176x." #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/MarlinSerial.h
C++
agpl-3.0
2,166
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" #include "HAL.h" #if ENABLED(POSTMORTEM_DEBUGGING) #include "../shared/MinSerial.h" #include <debug_frmwrk.h> static void TX(char c) { _DBC(c); } void install_min_serial() { HAL_min_serial_out = &TX; } #if DISABLED(DYNAMIC_VECTORTABLE) extern "C" { __attribute__((naked)) void JumpHandler_ASM() { __asm__ __volatile__ ( "b CommonHandler_ASM\n" ); } void __attribute__((naked, alias("JumpHandler_ASM"))) HardFault_Handler(); void __attribute__((naked, alias("JumpHandler_ASM"))) BusFault_Handler(); void __attribute__((naked, alias("JumpHandler_ASM"))) UsageFault_Handler(); void __attribute__((naked, alias("JumpHandler_ASM"))) MemManage_Handler(); void __attribute__((naked, alias("JumpHandler_ASM"))) NMI_Handler(); } #endif #endif // POSTMORTEM_DEBUGGING #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/MinSerial.cpp
C++
agpl-3.0
1,761
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 * Copyright (c) 2009 Michael Margolis. All right reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /** * Based on "servo.h - Interrupt driven Servo library for Arduino using 16 bit timers - * Version 2 Copyright (c) 2009 Michael Margolis. All right reserved. * * The only modification was to update/delete macros to match the LPC176x. */ #include <Servo.h> class libServo: public Servo { public: void move(const int value) { constexpr uint16_t servo_delay[] = SERVO_DELAY; static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long."); if (attach(servo_info[servoIndex].Pin.nbr) >= 0) { // try to reattach write(value); safe_delay(servo_delay[servoIndex]); // delay to allow servo to reach position TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach()); } } }; class libServo; typedef libServo hal_servo_t;
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/Servo.h
C++
agpl-3.0
2,572
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 /** * Emulate EEPROM storage using Flash Memory * * Use a single 32K flash sector to store EEPROM data. To reduce the * number of erase operations a simple "leveling" scheme is used that * maintains a number of EEPROM "slots" within the larger flash sector. * Each slot is used in turn and the entire sector is only erased when all * slots have been used. * * A simple RAM image is used to hold the EEPROM data during I/O operations * and this is flushed to the next available slot when an update is complete. * If RAM usage becomes an issue we could store this image in one of the two * 16Kb I/O buffers (intended to hold DMA USB and Ethernet data, but currently * unused). */ #include "../../inc/MarlinConfig.h" #if ENABLED(FLASH_EEPROM_EMULATION) #include "../shared/eeprom_api.h" extern "C" { #include <lpc17xx_iap.h> } #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE 0x1000 // 4KB #endif #define SECTOR_START(sector) ((sector < 16) ? (sector << 12) : ((sector - 14) << 15)) #define EEPROM_SECTOR 29 #define SECTOR_SIZE 32768 #define EEPROM_SLOTS ((SECTOR_SIZE)/(MARLIN_EEPROM_SIZE)) #define EEPROM_ERASE 0xFF #define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * (MARLIN_EEPROM_SIZE)) static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0}; static bool eeprom_dirty = false; static int current_slot = 0; size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { uint32_t first_nblank_loc, first_nblank_val; IAP_STATUS_CODE status; // discover which slot we are currently using. __disable_irq(); status = BlankCheckSector(EEPROM_SECTOR, EEPROM_SECTOR, &first_nblank_loc, &first_nblank_val); __enable_irq(); if (status == CMD_SUCCESS) { // sector is blank so nothing stored yet for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE; current_slot = EEPROM_SLOTS; } else { // current slot is the first non blank one current_slot = first_nblank_loc / (MARLIN_EEPROM_SIZE); uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot); // load current settings for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i]; } eeprom_dirty = false; return true; } bool PersistentStore::access_finish() { if (eeprom_dirty) { IAP_STATUS_CODE status; if (--current_slot < 0) { // all slots have been used, erase everything and start again __disable_irq(); status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR); __enable_irq(); current_slot = EEPROM_SLOTS - 1; } __disable_irq(); status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096); __enable_irq(); if (status != CMD_SUCCESS) return false; eeprom_dirty = false; } return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { const int p = REAL_EEPROM_ADDR(pos); for (size_t i = 0; i < size; i++) ram_eeprom[p + i] = value[i]; eeprom_dirty = true; crc16(crc, value, size); pos += size; return false; // return true for any error } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { const int p = REAL_EEPROM_ADDR(pos); const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos]; if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[p + i]; crc16(crc, buff, size); pos += size; return false; // return true for any error } #endif // FLASH_EEPROM_EMULATION #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/eeprom_flash.cpp
C++
agpl-3.0
4,537
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Implementation of EEPROM settings in SD Card */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" #if ENABLED(SDCARD_EEPROM_EMULATION) //#define DEBUG_SD_EEPROM_EMULATION #include "../shared/eeprom_api.h" #include <chanfs/diskio.h> #include <chanfs/ff.h> extern uint32_t MSC_Aquire_Lock(); extern uint32_t MSC_Release_Lock(); FATFS fat_fs; FIL eeprom_file; bool eeprom_file_open = false; #define EEPROM_FILENAME "eeprom.dat" #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE size_t(0x1000) // 4KiB of Emulated EEPROM #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { const char eeprom_erase_value = 0xFF; MSC_Aquire_Lock(); if (f_mount(&fat_fs, "", 1)) { MSC_Release_Lock(); return false; } FRESULT res = f_open(&eeprom_file, EEPROM_FILENAME, FA_OPEN_ALWAYS | FA_WRITE | FA_READ); if (res) MSC_Release_Lock(); if (res == FR_OK) { UINT bytes_written; FSIZE_t file_size = f_size(&eeprom_file); f_lseek(&eeprom_file, file_size); while (file_size < capacity() && res == FR_OK) { res = f_write(&eeprom_file, &eeprom_erase_value, 1, &bytes_written); file_size++; } } if (res == FR_OK) { f_lseek(&eeprom_file, 0); f_sync(&eeprom_file); eeprom_file_open = true; } return res == FR_OK; } bool PersistentStore::access_finish() { f_close(&eeprom_file); f_unmount(""); MSC_Release_Lock(); eeprom_file_open = false; return true; } // This extra chit-chat goes away soon, but is helpful for now // to see errors that are happening in read_data / write_data static void debug_rw(const bool write, int &pos, const uint8_t *value, const size_t size, const FRESULT s, const size_t total=0) { #if ENABLED(DEBUG_SD_EEPROM_EMULATION) FSTR_P const rw_str = write ? F("write") : F("read"); SERIAL_ECHOLN(C(' '), rw_str, F("_data("), pos, C(','), *value, C(','), size, F(", ...)")); if (total) SERIAL_ECHOLN(F(" f_"), rw_str, F("()="), s, F("\n size="), size, F("\n bytes_"), write ? F("written=") : F("read="), total); else SERIAL_ECHOLNPGM(" f_lseek()=", s); #endif } // File function return codes for type FRESULT. This goes away soon, but // is helpful right now to see any errors in read_data and write_data. // // typedef enum { // FR_OK = 0, /* (0) Succeeded */ // FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ // FR_INT_ERR, /* (2) Assertion failed */ // FR_NOT_READY, /* (3) The physical drive cannot work */ // FR_NO_FILE, /* (4) Could not find the file */ // FR_NO_PATH, /* (5) Could not find the path */ // FR_INVALID_NAME, /* (6) The path name format is invalid */ // FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ // FR_EXIST, /* (8) Access denied due to prohibited access */ // FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ // FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ // FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ // FR_NOT_ENABLED, /* (12) The volume has no work area */ // FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ // FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ // FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ // FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ // FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ // FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ // FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ // } FRESULT; bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { if (!eeprom_file_open) return true; FRESULT s; UINT bytes_written = 0; s = f_lseek(&eeprom_file, pos); if (s) { debug_rw(true, pos, value, size, s); return s; } s = f_write(&eeprom_file, (void*)value, size, &bytes_written); if (s) { debug_rw(true, pos, value, size, s, bytes_written); return s; } crc16(crc, value, size); pos += size; return bytes_written != size; // return true for any error } bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing/*=true*/) { if (!eeprom_file_open) return true; UINT bytes_read = 0; FRESULT s; s = f_lseek(&eeprom_file, pos); if (s) { debug_rw(false, pos, value, size, s); return true; } if (writing) { s = f_read(&eeprom_file, (void*)value, size, &bytes_read); crc16(crc, value, size); } else { uint8_t temp[size]; s = f_read(&eeprom_file, (void*)temp, size, &bytes_read); crc16(crc, temp, size); } if (s) { debug_rw(false, pos, value, size, s, bytes_read); return true; } pos += size; return bytes_read != size; // return true for any error } #endif // SDCARD_EEPROM_EMULATION #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/eeprom_sdcard.cpp
C++
agpl-3.0
6,091
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" #if USE_WIRED_EEPROM /** * PersistentStore for Arduino-style EEPROM interface * with implementations supplied by the framework. */ #include "../shared/eeprom_if.h" #include "../shared/eeprom_api.h" #ifndef MARLIN_EEPROM_SIZE #define MARLIN_EEPROM_SIZE 0x8000 // 32K #endif size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; } bool PersistentStore::access_start() { eeprom_init(); return true; } bool PersistentStore::access_finish() { return true; } bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { uint16_t written = 0; while (size--) { uint8_t v = *value; uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos); if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed! eeprom_write_byte(p, v); if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes if (eeprom_read_byte(p) != v) { SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); return true; } } crc16(crc, &v, 1); pos++; value++; } return false; } bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) { do { // Read from external EEPROM const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos)); if (writing) *value = c; crc16(crc, &c, 1); pos++; value++; } while (--size); return false; } #endif // USE_WIRED_EEPROM #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/eeprom_wired.cpp
C++
agpl-3.0
2,505
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Endstop Interrupts * * Without endstop interrupts the endstop pins must be polled continually in * the temperature-ISR via endstops.update(), most of the time finding no change. * With this feature endstops.update() is called only when we know that at * least one endstop has changed state, saving valuable CPU cycles. * * This feature only works when all used endstop pins can generate an 'external interrupt'. * * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'. * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino) */ #include "../../module/endstops.h" // One ISR for all EXT-Interrupts void endstop_ISR() { endstops.update(); } void setup_endstop_interrupts() { #define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE) #define LPC1768_PIN_INTERRUPT_M(pin) ((pin >> 0x5 & 0x7) == 0 || (pin >> 0x5 & 0x7) == 2) #if USE_X_MAX #if !LPC1768_PIN_INTERRUPT_M(X_MAX_PIN) #error "X_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(X_MAX_PIN); #endif #if USE_X_MIN #if !LPC1768_PIN_INTERRUPT_M(X_MIN_PIN) #error "X_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(X_MIN_PIN); #endif #if USE_Y_MAX #if !LPC1768_PIN_INTERRUPT_M(Y_MAX_PIN) #error "Y_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Y_MAX_PIN); #endif #if USE_Y_MIN #if !LPC1768_PIN_INTERRUPT_M(Y_MIN_PIN) #error "Y_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Y_MIN_PIN); #endif #if USE_Z_MAX #if !LPC1768_PIN_INTERRUPT_M(Z_MAX_PIN) #error "Z_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z_MAX_PIN); #endif #if USE_Z_MIN #if !LPC1768_PIN_INTERRUPT_M(Z_MIN_PIN) #error "Z_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z_MIN_PIN); #endif #if USE_X2_MAX #if !LPC1768_PIN_INTERRUPT_M(X2_MAX_PIN) #error "X2_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(X2_MAX_PIN); #endif #if USE_X2_MIN #if !LPC1768_PIN_INTERRUPT_M(X2_MIN_PIN) #error "X2_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(X2_MIN_PIN); #endif #if USE_Y2_MAX #if !LPC1768_PIN_INTERRUPT_M(Y2_MAX_PIN) #error "Y2_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Y2_MAX_PIN); #endif #if USE_Y2_MIN #if !LPC1768_PIN_INTERRUPT_M(Y2_MIN_PIN) #error "Y2_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Y2_MIN_PIN); #endif #if USE_Z2_MAX #if !LPC1768_PIN_INTERRUPT_M(Z2_MAX_PIN) #error "Z2_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z2_MAX_PIN); #endif #if USE_Z2_MIN #if !LPC1768_PIN_INTERRUPT_M(Z2_MIN_PIN) #error "Z2_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z2_MIN_PIN); #endif #if USE_Z3_MAX #if !LPC1768_PIN_INTERRUPT_M(Z3_MAX_PIN) #error "Z3_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z3_MAX_PIN); #endif #if USE_Z3_MIN #if !LPC1768_PIN_INTERRUPT_M(Z3_MIN_PIN) #error "Z3_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z3_MIN_PIN); #endif #if USE_Z4_MAX #if !LPC1768_PIN_INTERRUPT_M(Z4_MAX_PIN) #error "Z4_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z4_MAX_PIN); #endif #if USE_Z4_MIN #if !LPC1768_PIN_INTERRUPT_M(Z4_MIN_PIN) #error "Z4_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z4_MIN_PIN); #endif #if USE_Z_MIN_PROBE #if !LPC1768_PIN_INTERRUPT_M(Z_MIN_PROBE_PIN) #error "Z_MIN_PROBE_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(Z_MIN_PROBE_PIN); #endif #if USE_I_MAX #if !LPC1768_PIN_INTERRUPT_M(I_MAX_PIN) #error "I_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(I_MAX_PIN); #elif USE_I_MIN #if !LPC1768_PIN_INTERRUPT_M(I_MIN_PIN) #error "I_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(I_MIN_PIN); #endif #if USE_J_MAX #if !LPC1768_PIN_INTERRUPT_M(J_MAX_PIN) #error "J_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(J_MAX_PIN); #elif USE_J_MIN #if !LPC1768_PIN_INTERRUPT_M(J_MIN_PIN) #error "J_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(J_MIN_PIN); #endif #if USE_K_MAX #if !LPC1768_PIN_INTERRUPT_M(K_MAX_PIN) #error "K_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(K_MAX_PIN); #elif USE_K_MIN #if !LPC1768_PIN_INTERRUPT_M(K_MIN_PIN) #error "K_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(K_MIN_PIN); #endif #if USE_U_MAX #if !LPC1768_PIN_INTERRUPT_M(U_MAX_PIN) #error "U_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(U_MAX_PIN); #elif USE_U_MIN #if !LPC1768_PIN_INTERRUPT_M(U_MIN_PIN) #error "U_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(U_MIN_PIN); #endif #if USE_V_MAX #if !LPC1768_PIN_INTERRUPT_M(V_MAX_PIN) #error "V_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(V_MAX_PIN); #elif USE_V_MIN #if !LPC1768_PIN_INTERRUPT_M(V_MIN_PIN) #error "V_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(V_MIN_PIN); #endif #if USE_W_MAX #if !LPC1768_PIN_INTERRUPT_M(W_MAX_PIN) #error "W_MAX_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(W_MAX_PIN); #elif USE_W_MIN #if !LPC1768_PIN_INTERRUPT_M(W_MIN_PIN) #error "W_MIN_PIN is not INTERRUPT-capable. Disable ENDSTOP_INTERRUPTS_FEATURE to continue." #endif _ATTACH(W_MIN_PIN); #endif }
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/endstop_interrupts.h
C
agpl-3.0
7,717
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" #include <pwm.h> void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) { if (!LPC176x::pin_is_valid(pin)) return; if (LPC176x::pwm_attach_pin(pin)) LPC176x::pwm_write_ratio(pin, invert ? 1.0f - (float)v / v_size : (float)v / v_size); // map 1-254 onto PWM range } void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) { LPC176x::pwm_set_frequency(pin, f_desired); } #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/fast_pwm.cpp
C++
agpl-3.0
1,419
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Fast I/O Routines for LPC1768/9 * Use direct port manipulation to save scads of processor time. * Contributed by Triffid_Hunter and modified by Kliment, thinkyhead, Bob-the-Kuhn, et.al. */ /** * Description: Fast IO functions LPC1768 * * For TARGET LPC1768 */ #include "../shared/Marduino.h" #define PWM_PIN(P) true // all pins are PWM capable #define LPC_PIN(pin) LPC176x::gpio_pin(pin) #define LPC_GPIO(port) LPC176x::gpio_port(port) #define SET_DIR_INPUT(IO) LPC176x::gpio_set_input(IO) #define SET_DIR_OUTPUT(IO) LPC176x::gpio_set_output(IO) #define SET_MODE(IO, mode) pinMode(IO, mode) #define WRITE_PIN_SET(IO) LPC176x::gpio_set(IO) #define WRITE_PIN_CLR(IO) LPC176x::gpio_clear(IO) #define READ_PIN(IO) LPC176x::gpio_get(IO) #define WRITE_PIN(IO,V) LPC176x::gpio_set(IO, V) /** * Magic I/O routines * * Now you can simply SET_OUTPUT(STEP); WRITE(STEP, HIGH); WRITE(STEP, LOW); * * Why double up on these macros? see https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html */ /// Read a pin #define _READ(IO) READ_PIN(IO) /// Write to a pin #define _WRITE(IO,V) WRITE_PIN(IO,V) /// toggle a pin #define _TOGGLE(IO) _WRITE(IO, !READ(IO)) /// set pin as input #define _SET_INPUT(IO) SET_DIR_INPUT(IO) /// set pin as output #define _SET_OUTPUT(IO) SET_DIR_OUTPUT(IO) /// set pin as input with pullup mode #define _PULLUP(IO,V) pinMode(IO, (V) ? INPUT_PULLUP : INPUT) /// set pin as input with pulldown mode #define _PULLDOWN(IO,V) pinMode(IO, (V) ? INPUT_PULLDOWN : INPUT) /// check if pin is an input #define _IS_INPUT(IO) (!LPC176x::gpio_get_dir(IO)) /// check if pin is an output #define _IS_OUTPUT(IO) (LPC176x::gpio_get_dir(IO)) /// Read a pin wrapper #define READ(IO) _READ(IO) /// Write to a pin wrapper #define WRITE(IO,V) _WRITE(IO,V) /// toggle a pin wrapper #define TOGGLE(IO) _TOGGLE(IO) /// set pin as input wrapper #define SET_INPUT(IO) _SET_INPUT(IO) /// set pin as input with pullup wrapper #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0) /// set pin as input with pulldown wrapper #define SET_INPUT_PULLDOWN(IO) do{ _SET_INPUT(IO); _PULLDOWN(IO, HIGH); }while(0) /// set pin as output wrapper - reads the pin and sets the output to that value #define SET_OUTPUT(IO) do{ _WRITE(IO, _READ(IO)); _SET_OUTPUT(IO); }while(0) // set pin as PWM #define SET_PWM SET_OUTPUT /// check if pin is an input wrapper #define IS_INPUT(IO) _IS_INPUT(IO) /// check if pin is an output wrapper #define IS_OUTPUT(IO) _IS_OUTPUT(IO) // Shorthand #define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V)
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/fastio.h
C
agpl-3.0
3,820
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #ifndef SERIAL_PORT #define SERIAL_PORT 0 #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/inc/Conditionals_LCD.h
C
agpl-3.0
927
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if DISABLED(NO_SD_HOST_DRIVE) #define HAS_SD_HOST_DRIVE 1 #endif #ifndef RX_BUFFER_SIZE #define RX_BUFFER_SIZE 128 #endif #ifndef TX_BUFFER_SIZE #define TX_BUFFER_SIZE 32 #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/inc/Conditionals_adv.h
C
agpl-3.0
1,062
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if USE_FALLBACK_EEPROM #define FLASH_EEPROM_EMULATION #elif ANY(I2C_EEPROM, SPI_EEPROM) #define USE_SHARED_EEPROM 1 #endif // LPC1768 boards seem to lose steps when saving to EEPROM during print (issue #20785) // TODO: Which other boards are incompatible? #if defined(MCU_LPC1768) && ENABLED(FLASH_EEPROM_EMULATION) && PRINTCOUNTER_SAVE_INTERVAL > 0 #define PRINTCOUNTER_SYNC #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/inc/Conditionals_post.h
C
agpl-3.0
1,267
/** * Marlin 3D Printer Firmware * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/inc/Conditionals_type.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if PIO_PLATFORM_VERSION < 1001 #error "nxplpc-arduino-lpc176x package is out of date, Please update the PlatformIO platforms, frameworks and libraries. You may need to remove the platform and let it reinstall automatically." #endif #if PIO_FRAMEWORK_VERSION < 2006 #error "framework-arduino-lpc176x package is out of date, Please update the PlatformIO platforms, frameworks and libraries." #endif /** * Detect an old pins file by checking for old ADC pins values. */ #define _OLD_TEMP_PIN(P) PIN_EXISTS(P) && _CAT(P,_PIN) <= 7 && !WITHIN(_CAT(P,_PIN), TERN(LPC1768_IS_SKRV1_3, 0, 2), 3) // Include P0_00 and P0_01 for SKR V1.3 board #if _OLD_TEMP_PIN(TEMP_BED) #error "TEMP_BED_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_0) #error "TEMP_0_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_1) #error "TEMP_1_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_2) #error "TEMP_2_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_3) #error "TEMP_3_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_4) #error "TEMP_4_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_5) #error "TEMP_5_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_6) #error "TEMP_6_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #elif _OLD_TEMP_PIN(TEMP_7) #error "TEMP_7_PIN must be defined using the Pn_nn or Pn_nn_An format. (See the included pins files)." #endif #undef _OLD_TEMP_PIN /** * Because PWM hardware channels all share the same frequency, along with the * fallback software channels, FAST_PWM_FAN is incompatible with Servos. */ static_assert(!(NUM_SERVOS && ENABLED(FAST_PWM_FAN)), "BLTOUCH and Servos are incompatible with FAST_PWM_FAN on LPC176x boards."); #if SPINDLE_LASER_FREQUENCY static_assert(!NUM_SERVOS, "BLTOUCH and Servos are incompatible with SPINDLE_LASER_FREQUENCY on LPC176x boards."); #endif /** * Test LPC176x-specific configuration values for errors at compile-time. */ //#if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11) // #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector" //#endif #if MB(RAMPS_14_RE_ARM_EFB, RAMPS_14_RE_ARM_EEB, RAMPS_14_RE_ARM_EFF, RAMPS_14_RE_ARM_EEF, RAMPS_14_RE_ARM_SF) #if IS_RRD_FG_SC && HAS_DRIVER(TMC2130) && DISABLED(TMC_USE_SW_SPI) #error "Re-ARM with REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER and TMC2130 requires TMC_USE_SW_SPI." #endif #endif #if HAS_FSMC_TFT #error "Sorry! FSMC TFT displays are not current available for HAL/LPC1768." #endif static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported on LPC176x."); /** * Flag any serial port conflicts * * Port | TX | RX | * --- | --- | --- | * Serial | P0_02 | P0_03 | * Serial1 | P0_15 | P0_16 | * Serial2 | P0_10 | P0_11 | * Serial3 | P0_00 | P0_01 | */ #define ANY_TX(N,V...) DO(IS_TX##N,||,V) #define ANY_RX(N,V...) DO(IS_RX##N,||,V) #if USING_HW_SERIAL0 #define IS_TX0(P) (P == P0_02) #define IS_RX0(P) (P == P0_03) #if IS_TX0(TMC_SPI_MISO) || IS_RX0(TMC_SPI_MOSI) #error "Serial port pins (0) conflict with Trinamic SPI pins!" #elif HAS_PRUSA_MMU1 && (IS_TX0(E_MUX1_PIN) || IS_RX0(E_MUX0_PIN)) #error "Serial port pins (0) conflict with Multi-Material-Unit multiplexer pins!" #elif (AXIS_HAS_SPI(X) && IS_TX0(X_CS_PIN)) || (AXIS_HAS_SPI(Y) && IS_RX0(Y_CS_PIN)) #error "Serial port pins (0) conflict with X/Y axis SPI pins!" #endif #undef IS_TX0 #undef IS_RX0 #endif #if USING_HW_SERIAL1 #define IS_TX1(P) (P == P0_15) #define IS_RX1(P) (P == P0_16) #define _IS_TX1_1 IS_TX1 #define _IS_RX1_1 IS_RX1 #if IS_TX1(TMC_SPI_SCK) #error "Serial port pins (1) conflict with other pins!" #elif HAS_ROTARY_ENCODER #if IS_TX1(BTN_EN2) || IS_RX1(BTN_EN1) #error "Serial port pins (1) conflict with Encoder Buttons!" #elif ANY_TX(1, SD_SCK_PIN, LCD_PINS_D4, DOGLCD_SCK, LCD_RESET_PIN, LCD_PINS_RS, SHIFT_CLK_PIN) \ || ANY_RX(1, LCD_SDSS, LCD_PINS_RS, SD_MISO_PIN, DOGLCD_A0, SD_SS_PIN, LCD_SDSS, DOGLCD_CS, LCD_RESET_PIN, LCD_BACKLIGHT_PIN) #error "Serial port pins (1) conflict with LCD pins!" #endif #endif #undef IS_TX1 #undef IS_RX1 #undef _IS_TX1_1 #undef _IS_RX1_1 #endif #if USING_HW_SERIAL2 #define IS_TX2(P) (P == P0_10) #define IS_RX2(P) (P == P0_11) #define _IS_TX2_1 IS_TX2 #define _IS_RX2_1 IS_RX2 #if IS_TX2(X2_ENABLE_PIN) || ANY_RX(2, X2_DIR_PIN, X2_STEP_PIN) || (AXIS_HAS_SPI(X2) && IS_TX2(X2_CS_PIN)) #error "Serial port pins (2) conflict with X2 pins!" #elif IS_TX2(Y2_ENABLE_PIN) || ANY_RX(2, Y2_DIR_PIN, Y2_STEP_PIN) || (AXIS_HAS_SPI(Y2) && IS_TX2(Y2_CS_PIN)) #error "Serial port pins (2) conflict with Y2 pins!" #elif IS_TX2(Z2_ENABLE_PIN) || ANY_RX(2, Z2_DIR_PIN, Z2_STEP_PIN) || (AXIS_HAS_SPI(Z2) && IS_TX2(Z2_CS_PIN)) #error "Serial port pins (2) conflict with Z2 pins!" #elif IS_TX2(Z3_ENABLE_PIN) || ANY_RX(2, Z3_DIR_PIN, Z3_STEP_PIN) || (AXIS_HAS_SPI(Z3) && IS_TX2(Z3_CS_PIN)) #error "Serial port pins (2) conflict with Z3 pins!" #elif IS_TX2(Z4_ENABLE_PIN) || ANY_RX(2, Z4_DIR_PIN, Z4_STEP_PIN) || (AXIS_HAS_SPI(Z4) && IS_TX2(Z4_CS_PIN)) #error "Serial port pins (2) conflict with Z4 pins!" #elif ANY_RX(2, X_DIR_PIN, Y_DIR_PIN) #error "Serial port pins (2) conflict with other pins!" #elif Y_HOME_TO_MIN && IS_TX2(Y_STOP_PIN) #error "Serial port pins (2) conflict with Y endstop pin!" #elif USE_Z_MIN_PROBE && IS_TX2(Z_MIN_PROBE_PIN) #error "Serial port pins (2) conflict with probe pin!" #elif ANY_TX(2, X_ENABLE_PIN, Y_ENABLE_PIN) || ANY_RX(2, X_DIR_PIN, Y_DIR_PIN) #error "Serial port pins (2) conflict with X/Y stepper pins!" #elif HAS_MULTI_EXTRUDER && (IS_TX2(E1_ENABLE_PIN) || (AXIS_HAS_SPI(E1) && IS_TX2(E1_CS_PIN))) #error "Serial port pins (2) conflict with E1 stepper pins!" #elif EXTRUDERS && ANY_RX(2, E0_DIR_PIN, E0_STEP_PIN) #error "Serial port pins (2) conflict with E stepper pins!" #endif #undef IS_TX2 #undef IS_RX2 #undef _IS_TX2_1 #undef _IS_RX2_1 #endif #if USING_HW_SERIAL3 #define PIN_IS_TX3(P) (PIN_EXISTS(P) && P##_PIN == P0_00) #define PIN_IS_RX3(P) (P##_PIN == P0_01) #if PIN_IS_TX3(X_MIN) || PIN_IS_RX3(X_MAX) #error "Serial port pins (3) conflict with X endstop pins!" #elif PIN_IS_TX3(Y_SERIAL_TX) || PIN_IS_TX3(Y_SERIAL_RX) || PIN_IS_RX3(X_SERIAL_TX) || PIN_IS_RX3(X_SERIAL_RX) #error "Serial port pins (3) conflict with X/Y axis UART pins!" #elif PIN_IS_TX3(X2_DIR) || PIN_IS_RX3(X2_STEP) #error "Serial port pins (3) conflict with X2 pins!" #elif PIN_IS_TX3(Y2_DIR) || PIN_IS_RX3(Y2_STEP) #error "Serial port pins (3) conflict with Y2 pins!" #elif PIN_IS_TX3(Z2_DIR) || PIN_IS_RX3(Z2_STEP) #error "Serial port pins (3) conflict with Z2 pins!" #elif PIN_IS_TX3(Z3_DIR) || PIN_IS_RX3(Z3_STEP) #error "Serial port pins (3) conflict with Z3 pins!" #elif PIN_IS_TX3(Z4_DIR) || PIN_IS_RX3(Z4_STEP) #error "Serial port pins (3) conflict with Z4 pins!" #elif HAS_MULTI_EXTRUDER && (PIN_IS_TX3(E1_DIR) || PIN_IS_RX3(E1_STEP)) #error "Serial port pins (3) conflict with E1 pins!" #endif #undef PIN_IS_TX3 #undef PIN_IS_RX3 #endif #undef ANY_TX #undef ANY_RX // // Flag any i2c pin conflicts // #if ANY(HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM) #define USEDI2CDEV_M 1 // <Arduino>/Wire.cpp #if USEDI2CDEV_M == 0 // P0_27 [D57] (AUX-1) .......... P0_28 [D58] (AUX-1) #define PIN_IS_SDA0(P) (P##_PIN == P0_27) #define IS_SCL0(P) (P == P0_28) #if HAS_MEDIA && PIN_IS_SDA0(SD_DETECT) #error "SDA0 overlaps with SD_DETECT_PIN!" #elif PIN_IS_SDA0(E0_AUTO_FAN) #error "SDA0 overlaps with E0_AUTO_FAN_PIN!" #elif PIN_IS_SDA0(BEEPER) #error "SDA0 overlaps with BEEPER_PIN!" #elif IS_SCL0(BTN_ENC) #error "SCL0 overlaps with Encoder Button!" #elif IS_SCL0(SD_SS_PIN) #error "SCL0 overlaps with SD_SS_PIN!" #elif IS_SCL0(LCD_SDSS) #error "SCL0 overlaps with LCD_SDSS!" #endif #undef PIN_IS_SDA0 #undef IS_SCL0 #elif USEDI2CDEV_M == 1 // P0_00 [D20] (SCA) ............ P0_01 [D21] (SCL) #define PIN_IS_SDA1(P) (PIN_EXISTS(P) && P##_PIN == P0_00) #define PIN_IS_SCL1(P) (P##_PIN == P0_01) #if PIN_IS_SDA1(X_MIN) || PIN_IS_SCL1(X_MAX) #error "One or more i2c (1) pins overlaps with X endstop pins! Disable i2c peripherals." #elif PIN_IS_SDA1(X2_DIR) || PIN_IS_SCL1(X2_STEP) #error "One or more i2c (1) pins overlaps with X2 pins! Disable i2c peripherals." #elif PIN_IS_SDA1(Y2_DIR) || PIN_IS_SCL1(Y2_STEP) #error "One or more i2c (1) pins overlaps with Y2 pins! Disable i2c peripherals." #elif PIN_IS_SDA1(Z2_DIR) || PIN_IS_SCL1(Z2_STEP) #error "One or more i2c (1) pins overlaps with Z2 pins! Disable i2c peripherals." #elif PIN_IS_SDA1(Z3_DIR) || PIN_IS_SCL1(Z3_STEP) #error "One or more i2c (1) pins overlaps with Z3 pins! Disable i2c peripherals." #elif PIN_IS_SDA1(Z4_DIR) || PIN_IS_SCL1(Z4_STEP) #error "One or more i2c (1) pins overlaps with Z4 pins! Disable i2c peripherals." #elif HAS_MULTI_EXTRUDER && (PIN_IS_SDA1(E1_DIR) || PIN_IS_SCL1(E1_STEP)) #error "One or more i2c (1) pins overlaps with E1 pins! Disable i2c peripherals." #endif #undef PIN_IS_SDA1 #undef PIN_IS_SCL1 #elif USEDI2CDEV_M == 2 // P0_10 [D38] (X_ENABLE_PIN) ... P0_11 [D55] (X_DIR_PIN) #define PIN_IS_SDA2(P) (P##_PIN == P0_10) #define PIN_IS_SCL2(P) (P##_PIN == P0_11) #if PIN_IS_SDA2(Y_STOP) #error "i2c SDA2 overlaps with Y endstop pin!" #elif USE_Z_MIN_PROBE && PIN_IS_SDA2(Z_MIN_PROBE) #error "i2c SDA2 overlaps with Z probe pin!" #elif PIN_IS_SDA2(X_ENABLE) || PIN_IS_SDA2(Y_ENABLE) #error "i2c SDA2 overlaps with X/Y ENABLE pin!" #elif AXIS_HAS_SPI(X) && PIN_IS_SDA2(X_CS) #error "i2c SDA2 overlaps with X CS pin!" #elif PIN_IS_SDA2(X2_ENABLE) #error "i2c SDA2 overlaps with X2 enable pin! Disable i2c peripherals." #elif PIN_IS_SDA2(Y2_ENABLE) #error "i2c SDA2 overlaps with Y2 enable pin! Disable i2c peripherals." #elif PIN_IS_SDA2(Z2_ENABLE) #error "i2c SDA2 overlaps with Z2 enable pin! Disable i2c peripherals." #elif PIN_IS_SDA2(Z3_ENABLE) #error "i2c SDA2 overlaps with Z3 enable pin! Disable i2c peripherals." #elif PIN_IS_SDA2(Z4_ENABLE) #error "i2c SDA2 overlaps with Z4 enable pin! Disable i2c peripherals." #elif HAS_MULTI_EXTRUDER && PIN_IS_SDA2(E1_ENABLE) #error "i2c SDA2 overlaps with E1 enable pin! Disable i2c peripherals." #elif HAS_MULTI_EXTRUDER && AXIS_HAS_SPI(E1) && PIN_IS_SDA2(E1_CS) #error "i2c SDA2 overlaps with E1 CS pin! Disable i2c peripherals." #elif EXTRUDERS && (PIN_IS_SDA2(E0_STEP) || PIN_IS_SDA2(E0_DIR)) #error "i2c SCL2 overlaps with E0 STEP/DIR pin! Disable i2c peripherals." #elif PIN_IS_SDA2(X_DIR) || PIN_IS_SDA2(Y_DIR) #error "One or more i2c pins overlaps with X/Y DIR pin! Disable i2c peripherals." #endif #undef PIN_IS_SDA2 #undef PIN_IS_SCL2 #endif #undef USEDI2CDEV_M #endif #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED) #error "SERIAL_STATS_MAX_RX_QUEUED is not supported on LPC176x." #elif ENABLED(SERIAL_STATS_DROPPED_RX) #error "SERIAL_STATS_DROPPED_RX is not supported on LPX176x." #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/inc/SanityCheck.h
C
agpl-3.0
12,782
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../shared/HAL_SPI.h" #include <stdint.h> #include <lpc17xx_ssp.h> #include <lpc17xx_gpdma.h> //#define MSBFIRST 1 #define SPI_MODE0 0 #define SPI_MODE1 1 #define SPI_MODE2 2 #define SPI_MODE3 3 #define DATA_SIZE_8BIT SSP_DATABIT_8 #define DATA_SIZE_16BIT SSP_DATABIT_16 #define SPI_CLOCK_MAX_TFT 30000000UL #define SPI_CLOCK_DIV2 8333333 //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED #define SPI_CLOCK_DIV4 4166667 //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED #define SPI_CLOCK_DIV8 2083333 //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED #define SPI_CLOCK_DIV16 1000000 //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED #define SPI_CLOCK_DIV32 500000 //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5 #define SPI_CLOCK_DIV64 250000 //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6 #define SPI_CLOCK_DIV128 125000 //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h #define SPI_CLOCK_MAX SPI_CLOCK_DIV2 #define BOARD_NR_SPI 2 //#define BOARD_SPI1_NSS_PIN PA4 ?! #define BOARD_SPI1_SCK_PIN P0_15 #define BOARD_SPI1_MISO_PIN P0_17 #define BOARD_SPI1_MOSI_PIN P0_18 //#define BOARD_SPI2_NSS_PIN PB12 ?! #define BOARD_SPI2_SCK_PIN P0_07 #define BOARD_SPI2_MISO_PIN P0_08 #define BOARD_SPI2_MOSI_PIN P0_09 class SPISettings { public: SPISettings(uint32_t spiRate, int inBitOrder, int inDataMode) { init_AlwaysInline(spiRate2Clock(spiRate), inBitOrder, inDataMode, DATA_SIZE_8BIT); } SPISettings(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) { if (__builtin_constant_p(inClock)) init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize); else init_MightInline(inClock, inBitOrder, inDataMode, inDataSize); } SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT); } //uint32_t spiRate() const { return spi_speed; } static uint32_t spiRate2Clock(uint32_t spiRate) { uint32_t Marlin_speed[7]; // CPSR is always 2 Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED Marlin_speed[1] = 4166667; //(SCR: 5) desired: 4,000,000 actual: 4,166,667 +4.2% SPI_HALF_SPEED Marlin_speed[2] = 2083333; //(SCR: 11) desired: 2,000,000 actual: 2,083,333 +4.2% SPI_QUARTER_SPEED Marlin_speed[3] = 1000000; //(SCR: 24) desired: 1,000,000 actual: 1,000,000 SPI_EIGHTH_SPEED Marlin_speed[4] = 500000; //(SCR: 49) desired: 500,000 actual: 500,000 SPI_SPEED_5 Marlin_speed[5] = 250000; //(SCR: 99) desired: 250,000 actual: 250,000 SPI_SPEED_6 Marlin_speed[6] = 125000; //(SCR:199) desired: 125,000 actual: 125,000 Default from HAL.h return Marlin_speed[spiRate > 6 ? 6 : spiRate]; } private: void init_MightInline(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) { init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize); } void init_AlwaysInline(uint32_t inClock, uint8_t inBitOrder, uint8_t inDataMode, uint32_t inDataSize) __attribute__((__always_inline__)) { clock = inClock; bitOrder = inBitOrder; dataMode = inDataMode; dataSize = inDataSize; } //uint32_t spi_speed; uint32_t clock; uint32_t dataSize; //uint32_t clockDivider; uint8_t bitOrder; uint8_t dataMode; LPC_SSP_TypeDef *spi_d; friend class SPIClass; }; /** * @brief Wirish SPI interface. * * This is the same interface is available across HAL * * This implementation uses software slave management, so the caller * is responsible for controlling the slave select line. */ class SPIClass { public: /** * @param spiPortNumber Number of the SPI port to manage. */ SPIClass(uint8_t spiPortNumber); /** * Init using pins */ SPIClass(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel = (pin_t)-1); /** * Select and configure the current selected SPI device to use */ void begin(); /** * Disable the current SPI device */ void end(); void beginTransaction(const SPISettings&); void endTransaction() {} // Transfer using 1 "Data Size" uint8_t transfer(uint16_t data); // Transfer 2 bytes in 8 bit mode uint16_t transfer16(uint16_t data); void send(uint8_t data); uint16_t read(); void read(uint8_t *buf, uint32_t len); void dmaSend(void *buf, uint16_t length, bool minc); void dmaSendAsync(void *buf, uint16_t length, bool minc); /** * @brief Sets the number of the SPI peripheral to be used by * this HardwareSPI instance. * * @param spi_num Number of the SPI port. 1-2 in low density devices * or 1-3 in high density devices. */ void setModule(uint8_t device); void setClock(uint32_t clock); void setBitOrder(uint8_t bitOrder); void setDataMode(uint8_t dataMode); void setDataSize(uint32_t ds); inline uint32_t getDataSize() { return _currentSetting->dataSize; } private: SPISettings _settings[BOARD_NR_SPI]; SPISettings *_currentSetting; void updateSettings(); }; extern SPIClass SPI;
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/include/SPI.h
C++
agpl-3.0
6,135
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * digipot_mcp4451_I2C_routines.c * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfigPre.h" #if ENABLED(DIGIPOT_MCP4451) && MB(MKS_SBASE) #ifdef __cplusplus extern "C" { #endif #include "digipot_mcp4451_I2C_routines.h" uint8_t digipot_mcp4451_start(uint8_t sla) { // send slave address and write bit // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't // happen which means only the value of the slave address was send. Keep looping until // the slave address and write bit are actually sent. do { _I2C_Stop(I2CDEV_M); // output stop state on I2C bus _I2C_Start(I2CDEV_M); // output start state on I2C bus while ((I2C_status != I2C_I2STAT_M_TX_START) && (I2C_status != I2C_I2STAT_M_TX_RESTART) && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address LPC_I2C1->I2DAT = (sla << 1) & I2C_I2DAT_BITMASK; // transmit slave address & write bit LPC_I2C1->I2CONSET = I2C_I2CONSET_AA; LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC; while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK) && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)) { /* wait for slaw to finish */ } } while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK)); return 1; } uint8_t digipot_mcp4451_send_byte(uint8_t data) { LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data LPC_I2C1->I2CONSET = I2C_I2CONSET_AA; LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC; while (I2C_status != I2C_I2STAT_M_TX_DAT_ACK && I2C_status != I2C_I2STAT_M_TX_DAT_NACK); // wait for xmit to finish return 1; } #ifdef __cplusplus } #endif #endif // DIGIPOT_MCP4451 && MKS_SBASE #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/include/digipot_mcp4451_I2C_routines.c
C
agpl-3.0
2,951
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * digipot_mcp4451_I2C_routines.h * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html */ #ifdef __cplusplus extern "C" { #endif #include <lpc17xx_i2c.h> #include <lpc17xx_pinsel.h> #include <lpc17xx_libcfg_default.h> #include "i2c_util.h" uint8_t digipot_mcp4451_start(uint8_t sla); uint8_t digipot_mcp4451_send_byte(uint8_t data); #ifdef __cplusplus } #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/include/digipot_mcp4451_I2C_routines.h
C
agpl-3.0
1,312
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * HAL_LPC1768/include/i2c_util.c */ #ifdef TARGET_LPC1768 #include "i2c_util.h" #define U8G_I2C_OPT_FAST 16 // from u8g.h #ifdef __cplusplus extern "C" { #endif void configure_i2c(const uint8_t clock_option) { /** * Init I2C pin connect */ PINSEL_CFG_Type PinCfg; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; #if I2C_MASTER_ID == 0 PinCfg.Funcnum = 1; PinCfg.Pinnum = 27; // SDA0 / D57 AUX-1 ... SCL0 / D58 AUX-1 #elif I2C_MASTER_ID == 1 PinCfg.Funcnum = 3; PinCfg.Pinnum = 0; // SDA1 / D20 SCA ... SCL1 / D21 SCL #elif I2C_MASTER_ID == 2 PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; // SDA2 / D38 X_ENABLE_PIN ... SCL2 / D55 X_DIR_PIN #endif PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum += 1; PINSEL_ConfigPin(&PinCfg); // Initialize I2C peripheral I2C_Init(I2CDEV_M, (clock_option & U8G_I2C_OPT_FAST) ? 400000: 100000); // LCD data rates // Enable Master I2C operation I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); } ////////////////////////////////////////////////////////////////////////////////////// // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them. uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) { // Reset STA, STO, SI I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC; // Enter to Master Transmitter mode I2Cx->I2CONSET = I2C_I2CONSET_STA; // Wait for complete while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI)); I2Cx->I2CONCLR = I2C_I2CONCLR_STAC; return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK); } void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) { /* Make sure start bit is not active */ if (I2Cx->I2CONSET & I2C_I2CONSET_STA) I2Cx->I2CONCLR = I2C_I2CONCLR_STAC; I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA; I2Cx->I2CONCLR = I2C_I2CONCLR_SIC; } #ifdef __cplusplus } #endif #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/include/i2c_util.c
C
agpl-3.0
2,836
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL_LPC1768/include/i2c_util.h */ #include "../../../inc/MarlinConfigPre.h" #ifndef I2C_MASTER_ID #define I2C_MASTER_ID 1 #endif #if I2C_MASTER_ID == 0 #define I2CDEV_M LPC_I2C0 #elif I2C_MASTER_ID == 1 #define I2CDEV_M LPC_I2C1 #elif I2C_MASTER_ID == 2 #define I2CDEV_M LPC_I2C2 #else #error "Master I2C device not defined!" #endif #include <lpc17xx_i2c.h> #include <lpc17xx_pinsel.h> #include <lpc17xx_libcfg_default.h> #ifdef __cplusplus extern "C" { #endif void configure_i2c(const uint8_t clock_option); uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx); void _I2C_Stop(LPC_I2C_TypeDef *I2Cx); #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK) #ifdef __cplusplus } #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/include/i2c_util.h
C
agpl-3.0
1,589
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Support routines for LPC1768 */ /** * Translation of routines & variables used by pinsDebug.h */ #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS #define IS_ANALOG(P) (DIGITAL_PIN_TO_ANALOG_PIN(P) >= 0 ? 1 : 0) #define digitalRead_mod(p) extDigitalRead(p) #define GET_ARRAY_PIN(p) pin_array[p].pin #define PRINT_ARRAY_NAME(x) do{ sprintf_P(buffer, PSTR("%-" STRINGIFY(MAX_NAME_LENGTH) "s"), pin_array[x].name); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN(p) do{ sprintf_P(buffer, PSTR("P%d_%02d"), LPC176x::pin_port(p), LPC176x::pin_bit(p)); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN_ANALOG(p) do{ sprintf_P(buffer, PSTR("_A%d "), LPC176x::pin_get_adc_channel(pin)); SERIAL_ECHO(buffer); }while(0) #define MULTI_NAME_PAD 17 // space needed to be pretty if not first name assigned to a pin // pins that will cause hang/reset/disconnect in M43 Toggle and Watch utilities #ifndef M43_NEVER_TOUCH #define M43_NEVER_TOUCH(Q) ((Q) == P0_29 || (Q) == P0_30 || (Q) == P2_09) // USB pins #endif bool GET_PINMODE(const pin_t pin) { if (!LPC176x::pin_is_valid(pin) || LPC176x::pin_adc_enabled(pin)) // Invalid pin or active analog pin return false; return LPC176x::gpio_direction(pin); } #define GET_ARRAY_IS_DIGITAL(x) ((bool) pin_array[x].is_digital) void print_port(const pin_t) {} void pwm_details(const pin_t) {} bool pwm_status(const pin_t) { return false; }
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/pinsDebug.h
C
agpl-3.0
2,255
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if ALL(HAS_MARLINUI_U8GLIB, HAS_MEDIA) && (LCD_PINS_D4 == SD_SCK_PIN || LCD_PINS_EN == SD_MOSI_PIN || DOGLCD_SCK == SD_SCK_PIN || DOGLCD_MOSI == SD_MOSI_PIN) #define SOFTWARE_SPI // If the SD card and LCD adapter share the same SPI pins, then software SPI is currently // needed due to the speed and mode required for communicating with each device being different. // This requirement can be removed if the SPI access to these devices is updated to use // spiBeginTransaction. #endif /** onboard SD card */ //#define SD_SCK_PIN P0_07 //#define SD_MISO_PIN P0_08 //#define SD_MOSI_PIN P0_09 //#define SD_SS_PIN P0_06 /** external */ #ifndef SD_SCK_PIN #define SD_SCK_PIN P0_15 #endif #ifndef SD_MISO_PIN #define SD_MISO_PIN P0_17 #endif #ifndef SD_MOSI_PIN #define SD_MOSI_PIN P0_18 #endif #ifndef SD_SS_PIN #define SD_SS_PIN P1_23 #endif #if !defined(SDSS) || SDSS == P_NC // gets defaulted in pins.h #undef SDSS #define SDSS SD_SS_PIN #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/spi_pins.h
C
agpl-3.0
1,972
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfig.h" #if HAS_SPI_TFT #include "tft_spi.h" SPIClass TFT_SPI::SPIx(TFT_SPI_DEVICE); void TFT_SPI::init() { #if PIN_EXISTS(TFT_RESET) OUT_WRITE(TFT_RESET_PIN, HIGH); delay(100); #endif #if PIN_EXISTS(TFT_BACKLIGHT) OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH); #endif OUT_WRITE(TFT_DC_PIN, HIGH); OUT_WRITE(TFT_CS_PIN, HIGH); SPIx.setModule(TFT_SPI_DEVICE); SPIx.setClock(SPI_CLOCK_MAX_TFT); SPIx.setBitOrder(MSBFIRST); SPIx.setDataMode(SPI_MODE0); } void TFT_SPI::dataTransferBegin(uint16_t dataSize) { SPIx.setDataSize(dataSize); SPIx.begin(); WRITE(TFT_CS_PIN, LOW); } #ifdef TFT_DEFAULT_DRIVER #include "../../../lcd/tft_io/tft_ids.h" #endif uint32_t TFT_SPI::getID() { uint32_t id; id = readID(LCD_READ_ID); if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) id = readID(LCD_READ_ID4); #ifdef TFT_DEFAULT_DRIVER if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) id = TFT_DEFAULT_DRIVER; #endif return id; } uint32_t TFT_SPI::readID(const uint16_t inReg) { uint32_t data = 0; #if PIN_EXISTS(TFT_MISO) uint8_t d = 0; SPIx.setDataSize(DATASIZE_8BIT); SPIx.setClock(SPI_CLOCK_DIV64); SPIx.begin(); WRITE(TFT_CS_PIN, LOW); writeReg(inReg); for (uint8_t i = 0; i < 4; ++i) { SPIx.read((uint8_t*)&d, 1); data = (data << 8) | d; } dataTransferEnd(); SPIx.setClock(SPI_CLOCK_MAX_TFT); #endif return data >> 7; } bool TFT_SPI::isBusy() { #define __IS_DMA_CONFIGURED(__HANDLE__) ((__HANDLE__)->DMACCSrcAddr != 0) // DMA Channel 0 is hardcoded in dmaSendAsync() and dmaSend() if (!__IS_DMA_CONFIGURED(LPC_GPDMACH0)) return false; if (GPDMA_IntGetStatus(GPDMA_STAT_INTERR, 0)) { // You should not be here - DMA transfer error flag is set // Abort DMA transfer and release SPI } else { // Check if DMA transfer completed flag is set if (!GPDMA_IntGetStatus(GPDMA_STAT_INTTC, 0)) return true; // Check if SPI TX butter is empty and SPI is idle if ((SSP_GetStatus(LPC_SSPx, SSP_STAT_TXFIFO_EMPTY) == RESET) || (SSP_GetStatus(LPC_SSPx, SSP_STAT_BUSY) == SET)) return true; } abort(); return false; } void TFT_SPI::abort() { // DMA Channel 0 is hardcoded in dmaSendAsync() and dmaSend() // Disable DMA GPDMA_ChannelCmd(0, DISABLE); // Clear ERR and TC GPDMA_ClearIntPending(GPDMA_STATCLR_INTTC, 0); GPDMA_ClearIntPending(GPDMA_STATCLR_INTERR, 0); // Disable DMA on SPI SSP_DMACmd(LPC_SSPx, SSP_DMA_TX, DISABLE); // Deconfigure DMA Channel 0 LPC_GPDMACH0->DMACCControl = 0U; LPC_GPDMACH0->DMACCConfig = 0U; LPC_GPDMACH0->DMACCSrcAddr = 0U; LPC_GPDMACH0->DMACCDestAddr = 0U; dataTransferEnd(); } void TFT_SPI::transmit(uint16_t data) { SPIx.transfer(data); } void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { dataTransferBegin(DATASIZE_16BIT); SPIx.dmaSend(data, count, memoryIncrease); abort(); } void TFT_SPI::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { dataTransferBegin(DATASIZE_16BIT); SPIx.dmaSendAsync(data, count, memoryIncrease); TERN_(TFT_SHARED_IO, while (isBusy())); } #endif // HAS_SPI_TFT #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/tft/tft_spi.cpp
C++
agpl-3.0
4,132
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfig.h" #include <SPI.h> #include <lpc17xx_ssp.h> // #include <lpc17xx_gpdma.h> #define IS_SPI(N) (BOARD_NR_SPI >= N && (TFT_SCK_PIN == BOARD_SPI##N##_SCK_PIN) && (TFT_MOSI_PIN == BOARD_SPI##N##_MOSI_PIN) && (TFT_MISO_PIN == BOARD_SPI##N##_MISO_PIN)) #if IS_SPI(1) #define TFT_SPI_DEVICE 1 #define LPC_SSPx LPC_SSP0 #elif IS_SPI(2) #define TFT_SPI_DEVICE 2 #define LPC_SSPx LPC_SSP1 #else #error "Invalid TFT SPI configuration." #endif #undef IS_SPI #ifndef LCD_READ_ID #define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341) #endif #ifndef LCD_READ_ID4 #define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341) #endif #define DATASIZE_8BIT SSP_DATABIT_8 #define DATASIZE_16BIT SSP_DATABIT_16 #define TFT_IO_DRIVER TFT_SPI #define DMA_MAX_WORDS 0xFFF #define DMA_MINC_ENABLE 1 #define DMA_MINC_DISABLE 0 class TFT_SPI { private: static uint32_t readID(const uint16_t inReg); static void transmit(uint16_t data); static void transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count); static void transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count); public: static SPIClass SPIx; static void init(); static uint32_t getID(); static bool isBusy(); static void abort(); static void dataTransferBegin(uint16_t dataWidth=DATASIZE_16BIT); static void dataTransferEnd() { WRITE(TFT_CS_PIN, HIGH); SSP_Cmd(LPC_SSPx, DISABLE); }; static void dataTransferAbort(); static void writeData(uint16_t data) { transmit(data); } static void writeReg(const uint16_t inReg) { WRITE(TFT_DC_PIN, LOW); transmit(inReg); WRITE(TFT_DC_PIN, HIGH); } static void writeSequence_DMA(uint16_t *data, uint16_t count) { transmitDMA(DMA_MINC_ENABLE, data, count); } static void writeMultiple_DMA(uint16_t color, uint16_t count) { static uint16_t data; data = color; transmitDMA(DMA_MINC_DISABLE, &data, count); } static void writeSequence(uint16_t *data, uint16_t count) { transmit(DMA_MINC_ENABLE, data, count); } static void writeMultiple(uint16_t color, uint32_t count) { while (count > 0) { transmit(DMA_MINC_DISABLE, &color, count > DMA_MAX_WORDS ? DMA_MAX_WORDS : count); count = count > DMA_MAX_WORDS ? count - DMA_MAX_WORDS : 0; } } };
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/tft/tft_spi.h
C++
agpl-3.0
3,212
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfig.h" #if HAS_TFT_XPT2046 || HAS_RES_TOUCH_BUTTONS #include "xpt2046.h" #include <SPI.h> uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } #if ENABLED(TOUCH_BUTTONS_HW_SPI) #include <SPI.h> SPIClass XPT2046::SPIx(TOUCH_BUTTONS_HW_SPI_DEVICE); static void touch_spi_init(uint8_t spiRate) { XPT2046::SPIx.setModule(TOUCH_BUTTONS_HW_SPI_DEVICE); XPT2046::SPIx.setClock(SPI_CLOCK_DIV128); XPT2046::SPIx.setBitOrder(MSBFIRST); XPT2046::SPIx.setDataMode(SPI_MODE0); XPT2046::SPIx.setDataSize(DATA_SIZE_8BIT); } #endif void XPT2046::init() { #if DISABLED(TOUCH_BUTTONS_HW_SPI) SET_INPUT(TOUCH_MISO_PIN); SET_OUTPUT(TOUCH_MOSI_PIN); SET_OUTPUT(TOUCH_SCK_PIN); #endif OUT_WRITE(TOUCH_CS_PIN, HIGH); #if PIN_EXISTS(TOUCH_INT) // Optional Pendrive interrupt pin SET_INPUT(TOUCH_INT_PIN); #endif TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6)); // Read once to enable pendrive status pin getRawData(XPT2046_X); } bool XPT2046::isTouched() { return isBusy() ? false : ( #if PIN_EXISTS(TOUCH_INT) READ(TOUCH_INT_PIN) != HIGH #else getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD #endif ); } bool XPT2046::getRawPoint(int16_t * const x, int16_t * const y) { if (isBusy() || !isTouched()) return false; *x = getRawData(XPT2046_X); *y = getRawData(XPT2046_Y); return isTouched(); } uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) { uint16_t data[3]; dataTransferBegin(); TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.begin()); for (uint16_t i = 0; i < 3 ; i++) { IO(coordinate); data[i] = (IO() << 4) | (IO() >> 4); } TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.end()); dataTransferEnd(); uint16_t delta01 = delta(data[0], data[1]), delta02 = delta(data[0], data[2]), delta12 = delta(data[1], data[2]); if (delta01 > delta02 || delta01 > delta12) data[delta02 > delta12 ? 0 : 1] = data[2]; return (data[0] + data[1]) >> 1; } uint16_t XPT2046::IO(uint16_t data) { return TERN(TOUCH_BUTTONS_HW_SPI, hardwareIO, softwareIO)(data); } extern uint8_t spiTransfer(uint8_t b); #if ENABLED(TOUCH_BUTTONS_HW_SPI) uint16_t XPT2046::hardwareIO(uint16_t data) { return SPIx.transfer(data & 0xFF); } #endif uint16_t XPT2046::softwareIO(uint16_t data) { uint16_t result = 0; for (uint8_t j = 0x80; j; j >>= 1) { WRITE(TOUCH_SCK_PIN, LOW); WRITE(TOUCH_MOSI_PIN, data & j ? HIGH : LOW); if (READ(TOUCH_MISO_PIN)) result |= j; WRITE(TOUCH_SCK_PIN, HIGH); } WRITE(TOUCH_SCK_PIN, LOW); return result; } #endif // HAS_TFT_XPT2046 || HAS_RES_TOUCH_BUTTONS #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/tft/xpt2046.cpp
C++
agpl-3.0
3,597
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfig.h" #if ENABLED(TOUCH_BUTTONS_HW_SPI) #include <SPI.h> #endif #ifndef TOUCH_MISO_PIN #define TOUCH_MISO_PIN SD_MISO_PIN #endif #ifndef TOUCH_MOSI_PIN #define TOUCH_MOSI_PIN SD_MOSI_PIN #endif #ifndef TOUCH_SCK_PIN #define TOUCH_SCK_PIN SD_SCK_PIN #endif #ifndef TOUCH_CS_PIN #define TOUCH_CS_PIN SD_SS_PIN #endif #ifndef TOUCH_INT_PIN #define TOUCH_INT_PIN -1 #endif #define XPT2046_DFR_MODE 0x00 #define XPT2046_SER_MODE 0x04 #define XPT2046_CONTROL 0x80 enum XPTCoordinate : uint8_t { XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, }; #ifndef XPT2046_Z1_THRESHOLD #define XPT2046_Z1_THRESHOLD 10 #endif class XPT2046 { private: static bool isBusy() { return false; } static uint16_t getRawData(const XPTCoordinate coordinate); static bool isTouched(); static void dataTransferBegin() { WRITE(TOUCH_CS_PIN, LOW); }; static void dataTransferEnd() { WRITE(TOUCH_CS_PIN, HIGH); }; #if ENABLED(TOUCH_BUTTONS_HW_SPI) static uint16_t hardwareIO(uint16_t data); #endif static uint16_t softwareIO(uint16_t data); static uint16_t IO(uint16_t data = 0); public: #if ENABLED(TOUCH_BUTTONS_HW_SPI) static SPIClass SPIx; #endif static void init(); static bool getRawPoint(int16_t * const x, int16_t * const y); };
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/tft/xpt2046.h
C++
agpl-3.0
2,382
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Description: * * Timers for LPC1768 */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfig.h" void HAL_timer_init() { SBI(LPC_SC->PCONP, SBIT_TIMER0); // Power ON Timer 0 LPC_TIM0->PR = (HAL_TIMER_RATE) / (STEPPER_TIMER_RATE) - 1; // Use prescaler to set frequency if needed SBI(LPC_SC->PCONP, SBIT_TIMER1); // Power ON Timer 1 LPC_TIM1->PR = (HAL_TIMER_RATE) / 1000000 - 1; } void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { switch (timer_num) { case MF_TIMER_STEP: LPC_TIM0->MCR = _BV(SBIT_MR0I) | _BV(SBIT_MR0R); // Match on MR0, reset on MR0, interrupts when NVIC enables them LPC_TIM0->MR0 = uint32_t(STEPPER_TIMER_RATE) / frequency; // Match value (period) to set frequency LPC_TIM0->TCR = _BV(SBIT_CNTEN); // Counter Enable NVIC_SetPriority(TIMER0_IRQn, NVIC_EncodePriority(0, 1, 0)); NVIC_EnableIRQ(TIMER0_IRQn); break; case MF_TIMER_TEMP: LPC_TIM1->MCR = _BV(SBIT_MR0I) | _BV(SBIT_MR0R); // Match on MR0, reset on MR0, interrupts when NVIC enables them LPC_TIM1->MR0 = uint32_t(TEMP_TIMER_RATE) / frequency; LPC_TIM1->TCR = _BV(SBIT_CNTEN); // Counter Enable NVIC_SetPriority(TIMER1_IRQn, NVIC_EncodePriority(0, 2, 0)); NVIC_EnableIRQ(TIMER1_IRQn); break; default: break; } } #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/timers.cpp
C++
agpl-3.0
2,219
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL For LPC1768 */ #include <stdint.h> #include "../../core/macros.h" #define SBIT_TIMER0 1 #define SBIT_TIMER1 2 #define SBIT_CNTEN 0 #define SBIT_MR0I 0 // Timer 0 Interrupt when TC matches MR0 #define SBIT_MR0R 1 // Timer 0 Reset TC on Match #define SBIT_MR0S 2 // Timer 0 Stop TC and PC on Match #define SBIT_MR1I 3 #define SBIT_MR1R 4 #define SBIT_MR1S 5 #define SBIT_MR2I 6 #define SBIT_MR2R 7 #define SBIT_MR2S 8 #define SBIT_MR3I 9 #define SBIT_MR3R 10 #define SBIT_MR3S 11 // ------------------------ // Defines // ------------------------ #define _HAL_TIMER(T) _CAT(LPC_TIM, T) #define _HAL_TIMER_IRQ(T) TIMER##T##_IRQn #define __HAL_TIMER_ISR(T) extern "C" void TIMER##T##_IRQHandler() #define _HAL_TIMER_ISR(T) __HAL_TIMER_ISR(T) typedef uint32_t hal_timer_t; #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF #define HAL_TIMER_RATE ((F_CPU) / 4) // frequency of timers peripherals #ifndef MF_TIMER_STEP #define MF_TIMER_STEP 0 // Timer Index for Stepper #endif #ifndef MF_TIMER_PULSE #define MF_TIMER_PULSE MF_TIMER_STEP #endif #ifndef MF_TIMER_TEMP #define MF_TIMER_TEMP 1 // Timer Index for Temperature #endif #ifndef MF_TIMER_PWM #define MF_TIMER_PWM 3 // Timer Index for PWM #endif #define TEMP_TIMER_RATE 1000000 #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US) #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP) #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP) #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP) #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP) #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP) #ifndef HAL_STEP_TIMER_ISR #define HAL_STEP_TIMER_ISR() _HAL_TIMER_ISR(MF_TIMER_STEP) #endif #ifndef HAL_TEMP_TIMER_ISR #define HAL_TEMP_TIMER_ISR() _HAL_TIMER_ISR(MF_TIMER_TEMP) #endif // Timer references by index #define STEP_TIMER_PTR _HAL_TIMER(MF_TIMER_STEP) #define TEMP_TIMER_PTR _HAL_TIMER(MF_TIMER_TEMP) // ------------------------ // Public functions // ------------------------ void HAL_timer_init(); void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) { switch (timer_num) { case MF_TIMER_STEP: STEP_TIMER_PTR->MR0 = compare; break; // Stepper Timer Match Register 0 case MF_TIMER_TEMP: TEMP_TIMER_PTR->MR0 = compare; break; // Temp Timer Match Register 0 } } FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return STEP_TIMER_PTR->MR0; // Stepper Timer Match Register 0 case MF_TIMER_TEMP: return TEMP_TIMER_PTR->MR0; // Temp Timer Match Register 0 } return 0; } FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return STEP_TIMER_PTR->TC; // Stepper Timer Count case MF_TIMER_TEMP: return TEMP_TIMER_PTR->TC; // Temp Timer Count } return 0; } FORCE_INLINE static void HAL_timer_enable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: NVIC_EnableIRQ(TIMER0_IRQn); break; // Enable interrupt handler case MF_TIMER_TEMP: NVIC_EnableIRQ(TIMER1_IRQn); break; // Enable interrupt handler } } FORCE_INLINE static void HAL_timer_disable_interrupt(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: NVIC_DisableIRQ(TIMER0_IRQn); break; // Disable interrupt handler case MF_TIMER_TEMP: NVIC_DisableIRQ(TIMER1_IRQn); break; // Disable interrupt handler } // We NEED memory barriers to ensure Interrupts are actually disabled! // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the ) __DSB(); __ISB(); } // This function is missing from CMSIS FORCE_INLINE static bool NVIC_GetEnableIRQ(IRQn_Type IRQn) { return TEST(NVIC->ISER[uint32_t(IRQn) >> 5], uint32_t(IRQn) & 0x1F); } FORCE_INLINE static bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: return NVIC_GetEnableIRQ(TIMER0_IRQn); // Check if interrupt is enabled or not case MF_TIMER_TEMP: return NVIC_GetEnableIRQ(TIMER1_IRQn); // Check if interrupt is enabled or not } return false; } FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) { switch (timer_num) { case MF_TIMER_STEP: SBI(STEP_TIMER_PTR->IR, SBIT_CNTEN); break; case MF_TIMER_TEMP: SBI(TEMP_TIMER_PTR->IR, SBIT_CNTEN); break; } } #define HAL_timer_isr_epilogue(T) NOOP
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/timers.h
C
agpl-3.0
6,087
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ // adapted from I2C/master/master.c example // https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html #ifdef TARGET_LPC1768 #include "../include/i2c_util.h" #include "../../../core/millis_t.h" extern int millis(); #ifdef __cplusplus extern "C" { #endif ////////////////////////////////////////////////////////////////////////////////////// #define I2CDEV_S_ADDR 0x78 // From SSD1306 (actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write) // Send slave address and write bit uint8_t u8g_i2c_start(const uint8_t sla) { // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't // happen which means only the value of the slave address was send. Keep looping until // the slave address and write bit are actually sent. do{ _I2C_Stop(I2CDEV_M); // output stop state on I2C bus _I2C_Start(I2CDEV_M); // output start state on I2C bus while ((I2C_status != I2C_I2STAT_M_TX_START) && (I2C_status != I2C_I2STAT_M_TX_RESTART) && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address LPC_I2C1->I2DAT = I2CDEV_S_ADDR & I2C_I2DAT_BITMASK; // transmit slave address & write bit LPC_I2C1->I2CONSET = I2C_I2CONSET_AA; LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC; while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK) && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for slaw to finish }while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK)); return 1; } void u8g_i2c_init(const uint8_t clock_option) { configure_i2c(clock_option); u8g_i2c_start(0); // Send slave address and write bit } uint8_t u8g_i2c_send_byte(uint8_t data) { #define I2C_TIMEOUT 3 LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data LPC_I2C1->I2CONSET = I2C_I2CONSET_AA; LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC; const millis_t timeout = millis() + I2C_TIMEOUT; while ((I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK) && PENDING(millis(), timeout)); // wait for xmit to finish // had hangs with SH1106 so added time out - have seen temporary screen corruption when this happens return 1; } void u8g_i2c_stop() { } #ifdef __cplusplus } #endif #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/LCD_I2C_routines.cpp
C++
agpl-3.0
3,434
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once void u8g_i2c_init(const uint8_t clock_options); //uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos); uint8_t u8g_i2c_start(uint8_t sla); uint8_t u8g_i2c_send_byte(uint8_t data); void u8g_i2c_stop();
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/LCD_I2C_routines.h
C
agpl-3.0
1,073
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * LPC1768 LCD-specific defines */ #ifndef U8G_HAL_LINKS #define U8G_COM_SSD_I2C_HAL u8g_com_arduino_ssd_i2c_fn // See U8glib-HAL #else uint8_t u8g_com_HAL_LPC1768_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); uint8_t u8g_com_HAL_LPC1768_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); uint8_t u8g_com_HAL_LPC1768_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); uint8_t u8g_com_HAL_LPC1768_ssd_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); #define U8G_COM_HAL_SW_SPI_FN u8g_com_HAL_LPC1768_sw_spi_fn #define U8G_COM_HAL_HW_SPI_FN u8g_com_HAL_LPC1768_hw_spi_fn #define U8G_COM_ST7920_HAL_SW_SPI u8g_com_HAL_LPC1768_ST7920_sw_spi_fn #define U8G_COM_ST7920_HAL_HW_SPI u8g_com_HAL_LPC1768_ST7920_hw_spi_fn #define U8G_COM_SSD_I2C_HAL u8g_com_HAL_LPC1768_ssd_hw_i2c_fn #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/LCD_defines.h
C
agpl-3.0
1,899
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * LCD delay routines - used by all the drivers. * * These are based on the LPC1768 routines. * * Couldn't just call exact copies because the overhead * results in a one microsecond delay taking about 4µS. */ #ifdef __cplusplus extern "C" { #endif void U8g_delay(int msec); void u8g_MicroDelay(); void u8g_10MicroDelay(); #ifdef __cplusplus } #endif
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/LCD_delay.h
C
agpl-3.0
1,244
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Low level pin manipulation routines - used by all the drivers. * * These are based on the LPC1768 pinMode, digitalRead & digitalWrite routines. * * Couldn't just call exact copies because the overhead killed the LCD update speed * With an intermediate level the softspi was running in the 10-20kHz range which * resulted in using about about 25% of the CPU's time. */ #ifdef TARGET_LPC1768 #include <LPC17xx.h> #include <lpc17xx_pinsel.h> #include "../../../core/macros.h" //#include <pinmapping.h> #define LPC_PORT_OFFSET (0x0020) #define LPC_PIN(pin) (1UL << pin) #define LPC_GPIO(port) ((volatile LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + LPC_PORT_OFFSET * port)) #define INPUT 0 #define OUTPUT 1 #define INPUT_PULLUP 2 uint8_t LPC1768_PIN_PORT(const uint8_t pin); uint8_t LPC1768_PIN_PIN(const uint8_t pin); #ifdef __cplusplus extern "C" { #endif // I/O functions // As defined by Arduino INPUT(0x0), OUTPUT(0x1), INPUT_PULLUP(0x2) void pinMode_LCD(uint8_t pin, uint8_t mode) { #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111)) #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111)) PINSEL_CFG_Type config = { LPC1768_PIN_PORT(pin), LPC1768_PIN_PIN(pin), PINSEL_FUNC_0, PINSEL_PINMODE_TRISTATE, PINSEL_PINMODE_NORMAL }; switch (mode) { case INPUT: LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin)); PINSEL_ConfigPin(&config); break; case OUTPUT: LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR |= LPC_PIN(LPC1768_PIN_PIN(pin)); PINSEL_ConfigPin(&config); break; case INPUT_PULLUP: LPC_GPIO(LPC1768_PIN_PORT(pin))->FIODIR &= ~LPC_PIN(LPC1768_PIN_PIN(pin)); config.Pinmode = PINSEL_PINMODE_PULLUP; PINSEL_ConfigPin(&config); break; default: break; } } void u8g_SetPinOutput(uint8_t internal_pin_number) { pinMode_LCD(internal_pin_number, 1); // OUTPUT } void u8g_SetPinInput(uint8_t internal_pin_number) { pinMode_LCD(internal_pin_number, 0); // INPUT } void u8g_SetPinLevel(uint8_t pin, uint8_t pin_status) { #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111)) #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111)) if (pin_status) LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET = LPC_PIN(LPC1768_PIN_PIN(pin)); else LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR = LPC_PIN(LPC1768_PIN_PIN(pin)); } uint8_t u8g_GetPinLevel(uint8_t pin) { #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111)) #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111)) return (uint32_t)LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOPIN & LPC_PIN(LPC1768_PIN_PIN(pin)) ? 1 : 0; } #ifdef __cplusplus } #endif #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/LCD_pin_routines.c
C
agpl-3.0
3,690
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Low level pin manipulation routines - used by all the drivers. * * These are based on the LPC1768 pinMode, digitalRead & digitalWrite routines. * * Couldn't just call exact copies because the overhead killed the LCD update speed * With an intermediate level the softspi was running in the 10-20kHz range which * resulted in using about about 25% of the CPU's time. */ void u8g_SetPinOutput(uint8_t internal_pin_number); void u8g_SetPinInput(uint8_t internal_pin_number); void u8g_SetPinLevel(uint8_t pin, uint8_t pin_status); uint8_t u8g_GetPinLevel(uint8_t pin);
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/LCD_pin_routines.h
C
agpl-3.0
1,457
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_msp430_hw_spi.c * * Universal 8bit Graphics Library * * Copyright (c) 2011, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfigPre.h" #if HAS_MARLINUI_U8GLIB #include <U8glib-HAL.h> #include "../../shared/HAL_SPI.h" #ifndef LCD_SPI_SPEED #ifdef SD_SPI_SPEED #define LCD_SPI_SPEED SD_SPI_SPEED // Assume SPI speed shared with SD #else #define LCD_SPI_SPEED SPI_FULL_SPEED // Use full speed if SD speed is not supplied #endif #endif uint8_t u8g_com_HAL_LPC1768_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: u8g_SetPILevel(u8g, U8G_PI_CS, 1); u8g_SetPILevel(u8g, U8G_PI_A0, 1); u8g_SetPILevel(u8g, U8G_PI_RESET, 1); u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_SetPIOutput(u8g, U8G_PI_A0); u8g_SetPIOutput(u8g, U8G_PI_RESET); u8g_Delay(5); spiBegin(); spiInit(LCD_SPI_SPEED); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_SetPILevel(u8g, U8G_PI_A0, arg_val); break; case U8G_COM_MSG_CHIP_SELECT: u8g_SetPILevel(u8g, U8G_PI_CS, (arg_val ? 0 : 1)); break; case U8G_COM_MSG_RESET: u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_WRITE_BYTE: spiSend((uint8_t)arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { spiSend(*ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { spiSend(*ptr++); arg_val--; } } break; } return 1; } #endif // HAS_MARLINUI_U8GLIB #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/u8g_com_HAL_LPC1768_hw_spi.cpp
C++
agpl-3.0
4,118
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_arduino_ssd_i2c.c * * COM interface for Arduino (AND ATmega) and the SSDxxxx chip (SOLOMON) variant * I2C protocol * * Universal 8bit Graphics Library * * Copyright (c) 2011, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** * Special pin usage: * U8G_PI_I2C_OPTION additional options * U8G_PI_A0_STATE used to store the last value of the command/data register selection * U8G_PI_SET_A0 1: Signal request to update I2C device with new A0_STATE, 0: Do nothing, A0_STATE matches I2C device * U8G_PI_SCL clock line (NOT USED) * U8G_PI_SDA data line (NOT USED) * * U8G_PI_RESET reset line (currently disabled, see below) * * Protocol: * SLA, Cmd/Data Selection, Arguments * The command/data register is selected by a special instruction byte, which is sent after SLA * * The continue bit is always 0 so that a (re)start is equired for the change from cmd to/data mode */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfigPre.h" #if HAS_MARLINUI_U8GLIB #include <U8glib-HAL.h> #define I2C_SLA (0x3C*2) //#define I2C_CMD_MODE 0x080 #define I2C_CMD_MODE 0x000 #define I2C_DATA_MODE 0x040 uint8_t u8g_com_ssd_I2C_start_sequence(u8g_t *u8g) { /* are we requested to set the a0 state? */ if (u8g->pin_list[U8G_PI_SET_A0] == 0) return 1; /* setup bus, might be a repeated start */ if (u8g_i2c_start(I2C_SLA) == 0) return 0; if (u8g->pin_list[U8G_PI_A0_STATE] == 0) { if (u8g_i2c_send_byte(I2C_CMD_MODE) == 0) return 0; } else if (u8g_i2c_send_byte(I2C_DATA_MODE) == 0) return 0; u8g->pin_list[U8G_PI_SET_A0] = 0; return 1; } uint8_t u8g_com_HAL_LPC1768_ssd_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: //u8g_com_arduino_digital_write(u8g, U8G_PI_SCL, HIGH); //u8g_com_arduino_digital_write(u8g, U8G_PI_SDA, HIGH); //u8g->pin_list[U8G_PI_A0_STATE] = 0; /* initial RS state: unknown mode */ u8g_i2c_init(u8g->pin_list[U8G_PI_I2C_OPTION]); u8g_com_ssd_I2C_start_sequence(u8g); break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: /* Currently disabled, but it could be enable. Previous restrictions have been removed */ /* u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val); */ break; case U8G_COM_MSG_CHIP_SELECT: u8g->pin_list[U8G_PI_A0_STATE] = 0; u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again, also forces start condition */ if (arg_val == 0 ) { /* disable chip, send stop condition */ u8g_i2c_stop(); } else { /* enable, do nothing: any byte writing will trigger the i2c start */ } break; case U8G_COM_MSG_WRITE_BYTE: //u8g->pin_list[U8G_PI_SET_A0] = 1; if (u8g_com_ssd_I2C_start_sequence(u8g) == 0) { u8g_i2c_stop(); return 0; } if (u8g_i2c_send_byte(arg_val) == 0) { u8g_i2c_stop(); return 0; } // u8g_i2c_stop(); break; case U8G_COM_MSG_WRITE_SEQ: { //u8g->pin_list[U8G_PI_SET_A0] = 1; if (u8g_com_ssd_I2C_start_sequence(u8g) == 0) { u8g_i2c_stop(); return 0; } uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { if (u8g_i2c_send_byte(*ptr++) == 0) { u8g_i2c_stop(); return 0; } arg_val--; } } // u8g_i2c_stop(); break; case U8G_COM_MSG_WRITE_SEQ_P: { //u8g->pin_list[U8G_PI_SET_A0] = 1; if (u8g_com_ssd_I2C_start_sequence(u8g) == 0) { u8g_i2c_stop(); return 0; } uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { if (u8g_i2c_send_byte(u8g_pgm_read(ptr)) == 0) return 0; ptr++; arg_val--; } } // u8g_i2c_stop(); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g->pin_list[U8G_PI_A0_STATE] = arg_val; u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again */ break; } // switch return 1; } #endif // HAS_MARLINUI_U8GLIB #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp
C++
agpl-3.0
6,447
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_LPC1768_st7920_hw_spi.c * * Universal 8bit Graphics Library * * Copyright (c) 2011, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfigPre.h" #if HAS_MARLINUI_U8GLIB #include <U8glib-HAL.h> #include "../../shared/HAL_SPI.h" #include "../../shared/Delay.h" void spiBegin(); void spiInit(uint8_t spiRate); void spiSend(uint8_t b); void spiSend(const uint8_t *buf, size_t n); static uint8_t rs_last_state = 255; static void u8g_com_LPC1768_st7920_write_byte_hw_spi(uint8_t rs, uint8_t val) { if (rs != rs_last_state) { // Time to send a command/data byte rs_last_state = rs; spiSend(rs ? 0x0FA : 0x0F8); // Send data or command DELAY_US(40); // Give the controller some time: 20 is bad, 30 is OK, 40 is safe } spiSend(val & 0xF0); spiSend(val << 4); } uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: u8g_SetPILevel(u8g, U8G_PI_CS, 0); u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_Delay(5); spiBegin(); spiInit(SPI_EIGHTH_SPEED); // ST7920 max speed is about 1.1 MHz u8g->pin_list[U8G_PI_A0_STATE] = 0; // initial RS state: command mode break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_ADDRESS: // Define cmd (arg_val = 0) or data mode (arg_val = 1) u8g->pin_list[U8G_PI_A0_STATE] = arg_val; break; case U8G_COM_MSG_CHIP_SELECT: u8g_SetPILevel(u8g, U8G_PI_CS, arg_val); // Note: the ST7920 has an active high chip-select break; case U8G_COM_MSG_WRITE_BYTE: u8g_com_LPC1768_st7920_write_byte_hw_spi(u8g->pin_list[U8G_PI_A0_STATE], arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { u8g_com_LPC1768_st7920_write_byte_hw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { u8g_com_LPC1768_st7920_write_byte_hw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++); arg_val--; } } break; } return 1; } #endif // HAS_MARLINUI_U8GLIB #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp
C++
agpl-3.0
4,660
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_st7920_hw_spi.c * * Universal 8bit Graphics Library * * Copyright (c) 2011, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfigPre.h" #if IS_U8GLIB_ST7920 #include <U8glib-HAL.h> #include <SoftwareSPI.h> #include "../../shared/Delay.h" #include "../../shared/HAL_SPI.h" #ifndef LCD_SPI_SPEED #define LCD_SPI_SPEED SPI_EIGHTH_SPEED // About 1 MHz #endif static pin_t SCK_pin_ST7920_HAL, MOSI_pin_ST7920_HAL_HAL; static uint8_t SPI_speed = 0; static void u8g_com_LPC1768_st7920_write_byte_sw_spi(uint8_t rs, uint8_t val) { static uint8_t rs_last_state = 255; if (rs != rs_last_state) { // Transfer Data (FA) or Command (F8) swSpiTransfer(rs ? 0x0FA : 0x0F8, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); rs_last_state = rs; DELAY_US(40); // Give the controller time to process the data: 20 is bad, 30 is OK, 40 is safe } swSpiTransfer(val & 0x0F0, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); swSpiTransfer(val << 4, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); } uint8_t u8g_com_HAL_LPC1768_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: SCK_pin_ST7920_HAL = u8g->pin_list[U8G_PI_SCK]; MOSI_pin_ST7920_HAL_HAL = u8g->pin_list[U8G_PI_MOSI]; u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_SetPIOutput(u8g, U8G_PI_SCK); u8g_SetPIOutput(u8g, U8G_PI_MOSI); u8g_Delay(5); SPI_speed = swSpiInit(LCD_SPI_SPEED, SCK_pin_ST7920_HAL, MOSI_pin_ST7920_HAL_HAL); u8g_SetPILevel(u8g, U8G_PI_CS, 0); u8g_SetPILevel(u8g, U8G_PI_SCK, 0); u8g_SetPILevel(u8g, U8G_PI_MOSI, 0); u8g->pin_list[U8G_PI_A0_STATE] = 0; /* initial RS state: command mode */ break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g->pin_list[U8G_PI_A0_STATE] = arg_val; break; case U8G_COM_MSG_CHIP_SELECT: if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_CS]) u8g_SetPILevel(u8g, U8G_PI_CS, arg_val); //note: the st7920 has an active high chip select break; case U8G_COM_MSG_WRITE_BYTE: u8g_com_LPC1768_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { u8g_com_LPC1768_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { u8g_com_LPC1768_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++); arg_val--; } } break; } return 1; } #endif // IS_U8GLIB_ST7920 #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_sw_spi.cpp
C++
agpl-3.0
5,283
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_std_sw_spi.c * * Universal 8bit Graphics Library * * Copyright (c) 2015, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef TARGET_LPC1768 #include "../../../inc/MarlinConfigPre.h" #if HAS_MARLINUI_U8GLIB && !IS_U8GLIB_ST7920 #include <SoftwareSPI.h> #include "../../shared/HAL_SPI.h" #ifndef LCD_SPI_SPEED #define LCD_SPI_SPEED SPI_QUARTER_SPEED // About 2 MHz #endif #include <Arduino.h> #include <algorithm> #include <LPC17xx.h> #include <gpio.h> #include <U8glib-HAL.h> uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) { for (uint8_t i = 0; i < 8; ++i) { if (spi_speed == 0) { LPC176x::gpio_set(mosi_pin, !!(b & 0x80)); LPC176x::gpio_set(sck_pin, HIGH); b <<= 1; if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1; LPC176x::gpio_set(sck_pin, LOW); } else { const uint8_t state = (b & 0x80) ? HIGH : LOW; for (uint8_t j = 0; j < spi_speed; ++j) LPC176x::gpio_set(mosi_pin, state); for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) LPC176x::gpio_set(sck_pin, HIGH); b <<= 1; if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1; for (uint8_t j = 0; j < spi_speed; ++j) LPC176x::gpio_set(sck_pin, LOW); } } return b; } uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) { for (uint8_t i = 0; i < 8; ++i) { const uint8_t state = (b & 0x80) ? HIGH : LOW; if (spi_speed == 0) { LPC176x::gpio_set(sck_pin, LOW); LPC176x::gpio_set(mosi_pin, state); LPC176x::gpio_set(mosi_pin, state); // need some setup time LPC176x::gpio_set(sck_pin, HIGH); } else { for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) LPC176x::gpio_set(sck_pin, LOW); for (uint8_t j = 0; j < spi_speed; ++j) LPC176x::gpio_set(mosi_pin, state); for (uint8_t j = 0; j < spi_speed; ++j) LPC176x::gpio_set(sck_pin, HIGH); } b <<= 1; if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1; } return b; } static uint8_t SPI_speed = 0; static void u8g_sw_spi_shift_out(uint8_t dataPin, uint8_t clockPin, uint8_t val) { #if ANY(FYSETC_MINI_12864, MKS_MINI_12864) swSpiTransfer_mode_3(val, SPI_speed, clockPin, -1, dataPin); #else swSpiTransfer_mode_0(val, SPI_speed, clockPin, -1, dataPin); #endif } uint8_t u8g_com_HAL_LPC1768_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: u8g_SetPIOutput(u8g, U8G_PI_SCK); u8g_SetPIOutput(u8g, U8G_PI_MOSI); u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_SetPIOutput(u8g, U8G_PI_A0); if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPIOutput(u8g, U8G_PI_RESET); SPI_speed = swSpiInit(LCD_SPI_SPEED, u8g->pin_list[U8G_PI_SCK], u8g->pin_list[U8G_PI_MOSI]); u8g_SetPILevel(u8g, U8G_PI_SCK, 0); u8g_SetPILevel(u8g, U8G_PI_MOSI, 0); break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_CHIP_SELECT: #if ANY(FYSETC_MINI_12864, MKS_MINI_12864) // LCD SPI is running mode 3 while SD card is running mode 0 if (arg_val) { // SCK idle state needs to be set to the proper idle state before // the next chip select goes active u8g_SetPILevel(u8g, U8G_PI_SCK, 1); // Set SCK to mode 3 idle state before CS goes active u8g_SetPILevel(u8g, U8G_PI_CS, LOW); } else { u8g_SetPILevel(u8g, U8G_PI_CS, HIGH); u8g_SetPILevel(u8g, U8G_PI_SCK, 0); // Set SCK to mode 0 idle state after CS goes inactive } #else u8g_SetPILevel(u8g, U8G_PI_CS, !arg_val); #endif break; case U8G_COM_MSG_WRITE_BYTE: u8g_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { u8g_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], *ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { u8g_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], u8g_pgm_read(ptr)); ptr++; arg_val--; } } break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_SetPILevel(u8g, U8G_PI_A0, arg_val); break; } return 1; } #endif // HAS_MARLINUI_U8GLIB && !IS_U8GLIB_ST7920 #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/u8g/u8g_com_HAL_LPC1768_sw_spi.cpp
C++
agpl-3.0
7,211
# # upload_extra_script.py # set the output_port # if target_filename is found then that drive is used # else if target_drive is found then that drive is used # from __future__ import print_function import pioutil if pioutil.is_pio_build(): target_filename = "FIRMWARE.CUR" target_drive = "REARM" import platform current_OS = platform.system() Import("env") def print_error(e): print('\nUnable to find destination disk (%s)\n' \ 'Please select it in platformio.ini using the upload_port keyword ' \ '(https://docs.platformio.org/en/latest/projectconf/section_env_upload.html) ' \ 'or copy the firmware (.pio/build/%s/firmware.bin) manually to the appropriate disk\n' \ %(e, env.get('PIOENV'))) def before_upload(source, target, env): try: from pathlib import Path # # Find a disk for upload # upload_disk = 'Disk not found' target_file_found = False target_drive_found = False if current_OS == 'Windows': # # platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:' # Windows - doesn't care about the disk's name, only cares about the drive letter import subprocess,string from ctypes import windll from pathlib import PureWindowsPath # getting list of drives # https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python drives = [] bitmask = windll.kernel32.GetLogicalDrives() for letter in string.ascii_uppercase: if bitmask & 1: drives.append(letter) bitmask >>= 1 for drive in drives: final_drive_name = drive + ':' # print ('disc check: {}'.format(final_drive_name)) try: volume_info = str(subprocess.check_output('cmd /C dir ' + final_drive_name, stderr=subprocess.STDOUT)) except Exception as e: print ('error:{}'.format(e)) continue else: if target_drive in volume_info and not target_file_found: # set upload if not found target file yet target_drive_found = True upload_disk = PureWindowsPath(final_drive_name) if target_filename in volume_info: if not target_file_found: upload_disk = PureWindowsPath(final_drive_name) target_file_found = True elif current_OS == 'Linux': # # platformio.ini will accept this for a Linux upload port designation: 'upload_port = /media/media_name/drive' # import getpass user = getpass.getuser() mpath = Path('/media', user) drives = [ x for x in mpath.iterdir() if x.is_dir() ] if target_drive in drives: # If target drive is found, use it. target_drive_found = True upload_disk = mpath / target_drive else: for drive in drives: try: fpath = mpath / drive filenames = [ x.name for x in fpath.iterdir() if x.is_file() ] except: continue else: if target_filename in filenames: upload_disk = mpath / drive target_file_found = True break # # set upload_port to drive if found # if target_file_found or target_drive_found: env.Replace( UPLOAD_FLAGS="-P$UPLOAD_PORT" ) elif current_OS == 'Darwin': # MAC # # platformio.ini will accept this for a OSX upload port designation: 'upload_port = /media/media_name/drive' # dpath = Path('/Volumes') # human readable names drives = [ x for x in dpath.iterdir() if x.is_dir() ] if target_drive in drives and not target_file_found: # set upload if not found target file yet target_drive_found = True upload_disk = dpath / target_drive for drive in drives: try: fpath = dpath / drive # will get an error if the drive is protected filenames = [ x.name for x in fpath.iterdir() if x.is_file() ] except: continue else: if target_filename in filenames: upload_disk = dpath / drive target_file_found = True break # # Set upload_port to drive if found # if target_file_found or target_drive_found: env.Replace(UPLOAD_PORT=str(upload_disk)) print('\nUpload disk: ', upload_disk, '\n') else: print_error('Autodetect Error') except Exception as e: print_error(str(e)) env.AddPreAction("upload", before_upload)
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/upload_extra_script.py
Python
agpl-3.0
5,760
/** * Marlin 3D Printer Firmware * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef TARGET_LPC1768 #include "../../inc/MarlinConfigPre.h" #if ENABLED(EMERGENCY_PARSER) #include "../../feature/e_parser.h" EmergencyParser::State emergency_state; bool CDC_RecvCallback(const char c) { emergency_parser.update(emergency_state, c); return true; } #endif // EMERGENCY_PARSER #endif // TARGET_LPC1768
2301_81045437/Marlin
Marlin/src/HAL/LPC1768/usb_serial.cpp
C++
agpl-3.0
1,189
/** * Marlin 3D Printer Firmware * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifdef __PLAT_NATIVE_SIM__ #ifndef HAS_LIBBSD #include "HAL.h" /** * Copy string src to buffer dst of size dsize. At most dsize-1 * chars will be copied. Always NUL terminates (unless dsize == 0). * Returns strlen(src); if retval >= dsize, truncation occurred. */ size_t MarlinHAL::_strlcpy(char *dst, const char *src, size_t dsize) { const char *osrc = src; size_t nleft = dsize; // Copy as many bytes as will fit. if (nleft != 0) while (--nleft != 0) if ((*dst++ = *src++) == '\0') break; // Not enough room in dst, add NUL and traverse rest of src. if (nleft == 0) { if (dsize != 0) *dst = '\0'; // NUL-terminate dst while (*src++) { /* nada */ } } return (src - osrc - 1); // count does not include NUL } #endif // HAS_LIBBSD #endif // __PLAT_NATIVE_SIM__
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/HAL.cpp
C++
agpl-3.0
2,508
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <stdint.h> #include <stdarg.h> #undef min #undef max #include <algorithm> #include "pinmapping.h" void _printf (const char *format, ...); void _putc(uint8_t c); uint8_t _getc(); //arduino: Print.h #define DEC 10 #define HEX 16 #define OCT 8 #define BIN 2 //arduino: binary.h (weird defines) #define B01 1 #define B10 2 #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" #include "serial.h" // ------------------------ // Defines // ------------------------ #define CPU_32_BIT #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp #define F_CPU 100000000 #define SystemCoreClock F_CPU #define CPU_ST7920_DELAY_1 600 #define CPU_ST7920_DELAY_2 750 #define CPU_ST7920_DELAY_3 750 // ------------------------ // Serial ports // ------------------------ extern MSerialT serial_stream_0; extern MSerialT serial_stream_1; extern MSerialT serial_stream_2; extern MSerialT serial_stream_3; #define _MSERIAL(X) serial_stream_##X #define MSERIAL(X) _MSERIAL(X) #if WITHIN(SERIAL_PORT, 0, 3) #define MYSERIAL1 MSERIAL(SERIAL_PORT) #else #error "SERIAL_PORT must be from 0 to 3. Please update your configuration." #endif #ifdef SERIAL_PORT_2 #if WITHIN(SERIAL_PORT_2, 0, 3) #define MYSERIAL2 MSERIAL(SERIAL_PORT_2) #else #error "SERIAL_PORT_2 must be from 0 to 3. Please update your configuration." #endif #endif #ifdef MMU2_SERIAL_PORT #if WITHIN(MMU2_SERIAL_PORT, 0, 3) #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT) #else #error "MMU2_SERIAL_PORT must be from 0 to 3. Please update your configuration." #endif #endif #ifdef LCD_SERIAL_PORT #if WITHIN(LCD_SERIAL_PORT, 0, 3) #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT) #else #error "LCD_SERIAL_PORT must be from 0 to 3. Please update your configuration." #endif #endif // ------------------------ // Interrupts // ------------------------ #define CRITICAL_SECTION_START() #define CRITICAL_SECTION_END() // ------------------------ // ADC // ------------------------ #define HAL_ADC_VREF_MV 5000 #define HAL_ADC_RESOLUTION 10 /* ---------------- Delay in cycles */ #define DELAY_CYCLES(x) Kernel::delayCycles(x) #define SYSTEM_YIELD() Kernel::yield() // Maple Compatibility typedef void (*systickCallback_t)(void); void systick_attach_callback(systickCallback_t cb); extern volatile uint32_t systick_uptime_millis; // Marlin uses strstr in constexpr context, this is not supported, workaround by defining constexpr versions of the required functions. #define strstr(a, b) strstr_constexpr((a), (b)) constexpr inline std::size_t strlen_constexpr(const char* str) { // https://github.com/gcc-mirror/gcc/blob/5c7634a0e5f202935aa6c11b6ea953b8bf80a00a/libstdc%2B%2B-v3/include/bits/char_traits.h#L329 if (str != nullptr) { std::size_t i = 0; while (str[i] != '\0') ++i; return i; } return 0; } constexpr inline int strncmp_constexpr(const char* lhs, const char* rhs, std::size_t count) { // https://github.com/gcc-mirror/gcc/blob/13b9cbfc32fe3ac4c81c4dd9c42d141c8fb95db4/libstdc%2B%2B-v3/include/bits/char_traits.h#L655 if (lhs == nullptr || rhs == nullptr) return rhs != nullptr ? -1 : 1; for (std::size_t i = 0; i < count; ++i) if (lhs[i] != rhs[i]) return lhs[i] < rhs[i] ? -1 : 1; else if (lhs[i] == '\0') return 0; return 0; } constexpr inline const char* strstr_constexpr(const char* str, const char* target) { // https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) { std::size_t len = strlen_constexpr(target); do { char sc = {}; do { if ((sc = *str++) == '\0') return nullptr; } while (sc != c); } while (strncmp_constexpr(str, target, len) != 0); --str; } return str; } constexpr inline char* strstr_constexpr(char* str, const char* target) { // https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) { std::size_t len = strlen_constexpr(target); do { char sc = {}; do { if ((sc = *str++) == '\0') return nullptr; } while (sc != c); } while (strncmp_constexpr(str, target, len) != 0); --str; } return str; } // ------------------------ // Free Memory Accessor // ------------------------ #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif int freeMemory(); #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} // Watchdog static void watchdog_init(); static void watchdog_refresh(); static void init() {} // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return true; } static void isr_on() {} static void isr_off() {} static void delay_ms(const int ms) { _delay_ms(ms); } // Tasks, called from idle() static void idletask(); // Reset static constexpr uint8_t reset_reason = RST_POWER_ON; static uint8_t get_reset_source() { return reset_reason; } static void clear_reset_source() {} // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // static uint8_t active_ch; // Called by Temperature::init once at startup static void adc_init(); // Called by Temperature::init for each sensor at startup static void adc_enable(const uint8_t ch); // Begin ADC sampling on the given channel. Called from Temperature::isr! static void adc_start(const uint8_t ch); // Is the ADC ready for reading? static bool adc_ready(); // The current value of the ADC register static uint16_t adc_value(); /** * Set the PWM duty cycle for the pin to the given value. * No option to invert the duty cycle [default = false] * No option to change the scale of the provided value to enable finer PWM duty control [default = 255] */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); } static void set_pwm_frequency(const pin_t, int) {} #ifndef HAS_LIBBSD /** * Redirect missing strlcpy here */ static size_t _strlcpy(char *dst, const char *src, size_t dsize); #define strlcpy hal._strlcpy #endif };
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/HAL.h
C++
agpl-3.0
7,473
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include <SPI.h> using MarlinSPI = SPIClass;
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/MarlinSPI.h
C
agpl-3.0
922
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Fast I/O Routines for X86_64 */ #include "../shared/Marduino.h" #include <pinmapping.h> #define SET_DIR_INPUT(IO) Gpio::setDir(IO, 1) #define SET_DIR_OUTPUT(IO) Gpio::setDir(IO, 0) #define SET_MODE(IO, mode) Gpio::setMode(IO, mode) #define WRITE_PIN_SET(IO) Gpio::set(IO) #define WRITE_PIN_CLR(IO) Gpio::clear(IO) #define READ_PIN(IO) Gpio::get(IO) #define WRITE_PIN(IO,V) Gpio::set(IO, V) /** * Magic I/O routines * * Now you can simply SET_OUTPUT(STEP); WRITE(STEP, HIGH); WRITE(STEP, LOW); * * Why double up on these macros? see https://gcc.gnu.org/onlinedocs/cpp/Stringification.html */ /// Read a pin #define _READ(IO) READ_PIN(IO) /// Write to a pin #define _WRITE(IO,V) WRITE_PIN(IO,V) /// toggle a pin #define _TOGGLE(IO) _WRITE(IO, !READ(IO)) /// set pin as input #define _SET_INPUT(IO) SET_DIR_INPUT(IO) /// set pin as output #define _SET_OUTPUT(IO) SET_DIR_OUTPUT(IO) /// set pin as input with pullup mode #define _PULLUP(IO,V) pinMode(IO, (V) ? INPUT_PULLUP : INPUT) /// set pin as input with pulldown mode #define _PULLDOWN(IO,V) pinMode(IO, (V) ? INPUT_PULLDOWN : INPUT) // hg42: all pins can be input or output (I hope) // hg42: undefined pins create compile error (IO, is no pin) // hg42: currently not used, but was used by pinsDebug /// check if pin is an input #define _IS_INPUT(IO) (IO >= 0) /// check if pin is an output #define _IS_OUTPUT(IO) (IO >= 0) /// Read a pin wrapper #define READ(IO) _READ(IO) /// Write to a pin wrapper #define WRITE(IO,V) _WRITE(IO,V) /// toggle a pin wrapper #define TOGGLE(IO) _TOGGLE(IO) /// set pin as input wrapper #define SET_INPUT(IO) _SET_INPUT(IO) /// set pin as input with pullup wrapper #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0) /// set pin as input with pulldown wrapper #define SET_INPUT_PULLDOWN(IO) do{ _SET_INPUT(IO); _PULLDOWN(IO, HIGH); }while(0) /// set pin as output wrapper - reads the pin and sets the output to that value #define SET_OUTPUT(IO) do{ _WRITE(IO, _READ(IO)); _SET_OUTPUT(IO); }while(0) // set pin as PWM #define SET_PWM(IO) SET_OUTPUT(IO) /// check if pin is an input wrapper #define IS_INPUT(IO) _IS_INPUT(IO) /// check if pin is an output wrapper #define IS_OUTPUT(IO) _IS_OUTPUT(IO) // Shorthand #define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V)
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/fastio.h
C
agpl-3.0
3,510
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/inc/Conditionals_LCD.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once // Add strcmp_P if missing #ifndef strcmp_P #define strcmp_P(a, b) strcmp((a), (b)) #endif #ifndef strcat_P #define strcat_P(dest, src) strcat((dest), (src)) #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/inc/Conditionals_adv.h
C
agpl-3.0
1,046
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/inc/Conditionals_post.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/inc/Conditionals_type.h
C
agpl-3.0
875
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Test X86_64-specific configuration values for errors at compile-time. */ // Emulating RAMPS #if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11) #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector" #endif #if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY #error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for HAL/LINUX." #endif #if HAS_TMC_SW_SERIAL #error "TMC220x Software Serial is not supported on LINUX." #endif #if ENABLED(POSTMORTEM_DEBUGGING) #error "POSTMORTEM_DEBUGGING is not yet supported on LINUX." #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/inc/SanityCheck.h
C
agpl-3.0
1,568
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #ifdef __PLAT_NATIVE_SIM__ #include "../../inc/MarlinConfig.h" #include "pinsDebug.h" int8_t ADC_pin_mode(pin_t pin) { return -1; } int8_t get_pin_mode(const pin_t pin) { return VALID_PIN(pin) ? 0 : -1; } bool GET_PINMODE(const pin_t pin) { const int8_t pin_mode = get_pin_mode(pin); if (pin_mode == -1 || pin_mode == ADC_pin_mode(pin)) // Invalid pin or active analog pin return false; return (Gpio::getMode(pin) != 0); // Input/output state } bool GET_ARRAY_IS_DIGITAL(const pin_t pin) { return !IS_ANALOG(pin) || get_pin_mode(pin) != ADC_pin_mode(pin); } void print_port(const pin_t) {} void pwm_details(const pin_t) {} bool pwm_status(const pin_t) { return false; } #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/pinsDebug.cpp
C++
agpl-3.0
1,560
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Support routines for X86_64 */ #pragma once /** * Translation of routines & variables used by pinsDebug.h */ #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS #define IS_ANALOG(P) (DIGITAL_PIN_TO_ANALOG_PIN(P) >= 0 ? 1 : 0) #define digitalRead_mod(p) digitalRead(p) #define GET_ARRAY_PIN(p) pin_array[p].pin #define PRINT_ARRAY_NAME(x) do{ sprintf_P(buffer, PSTR("%-" STRINGIFY(MAX_NAME_LENGTH) "s"), pin_array[x].name); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN(p) do{ sprintf_P(buffer, PSTR("%3d "), p); SERIAL_ECHO(buffer); }while(0) #define PRINT_PIN_ANALOG(p) do{ sprintf_P(buffer, PSTR(" (A%2d) "), DIGITAL_PIN_TO_ANALOG_PIN(pin)); SERIAL_ECHO(buffer); }while(0) #define MULTI_NAME_PAD 16 // space needed to be pretty if not first name assigned to a pin // Active ADC function/mode/code values for PINSEL registers int8_t ADC_pin_mode(pin_t pin); int8_t get_pin_mode(const pin_t pin); bool GET_PINMODE(const pin_t pin); bool GET_ARRAY_IS_DIGITAL(const pin_t pin); void print_port(const pin_t); void pwm_details(const pin_t); bool pwm_status(const pin_t);
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/pinsDebug.h
C
agpl-3.0
1,939
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 * Copyright (c) 2009 Michael Margolis. All right reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /** * Based on "servo.h - Interrupt driven Servo library for Arduino using 16 bit timers - * Version 2 Copyright (c) 2009 Michael Margolis. All right reserved. * * The only modification was to update/delete macros to match the LPC176x. * */ #include <stdint.h> // Macros //values in microseconds #define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo #define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached #define REFRESH_INTERVAL 20000 // minimum time to refresh servos in microseconds #define MAX_SERVOS 4 #define INVALID_SERVO 255 // flag indicating an invalid servo index // Types typedef struct { uint8_t nbr : 8 ; // a pin number from 0 to 254 (255 signals invalid pin) uint8_t isActive : 1 ; // true if this channel is enabled, pin not pulsed if false } ServoPin_t; typedef struct { ServoPin_t Pin; unsigned int pulse_width; // pulse width in microseconds } ServoInfo_t; // Global variables extern uint8_t ServoCount; extern ServoInfo_t servo_info[MAX_SERVOS];
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/servo_private.h
C
agpl-3.0
2,972
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #if ALL(HAS_MARLINUI_U8GLIB, HAS_MEDIA) && (LCD_PINS_D4 == SD_SCK_PIN || LCD_PINS_EN == SD_MOSI_PIN || DOGLCD_SCK == SD_SCK_PIN || DOGLCD_MOSI == SD_MOSI_PIN) #define SOFTWARE_SPI // If the SD card and LCD adapter share the same SPI pins, then software SPI is currently // needed due to the speed and mode required for communicating with each device being different. // This requirement can be removed if the SPI access to these devices is updated to use // spiBeginTransaction. #endif // Onboard SD //#define SD_SCK_PIN P0_07 //#define SD_MISO_PIN P0_08 //#define SD_MOSI_PIN P0_09 //#define SD_SS_PIN P0_06 // External SD #ifndef SD_SCK_PIN #define SD_SCK_PIN 50 #endif #ifndef SD_MISO_PIN #define SD_MISO_PIN 51 #endif #ifndef SD_MOSI_PIN #define SD_MOSI_PIN 52 #endif #ifndef SD_SS_PIN #define SD_SS_PIN 53 #endif #ifndef SDSS #define SDSS SD_SS_PIN #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/spi_pins.h
C
agpl-3.0
1,888
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfig.h" #ifndef LCD_READ_ID #define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341) #endif #ifndef LCD_READ_ID4 #define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341) #endif #define DATASIZE_8BIT 8 #define DATASIZE_16BIT 16 #define TFT_IO_DRIVER TFT_SPI #define DMA_MAX_WORDS 0xFFFF #define DMA_MINC_ENABLE 1 #define DMA_MINC_DISABLE 0 class TFT_SPI { private: static uint32_t readID(const uint16_t inReg); static void transmit(uint16_t data); static void transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count); public: // static SPIClass SPIx; static void init(); static uint32_t getID(); static bool isBusy(); static void abort(); static void dataTransferBegin(uint16_t dataWidth=DATASIZE_16BIT); static void dataTransferEnd(); static void dataTransferAbort(); static void writeData(uint16_t data); static void writeReg(const uint16_t inReg); static void writeSequence_DMA(uint16_t *data, uint16_t count) { writeSequence(data, count); } static void writeMultiple_DMA(uint16_t color, uint16_t count) { writeMultiple(color, count); } static void writeSequence(uint16_t *data, uint16_t count); static void writeMultiple(uint16_t color, uint32_t count); };
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/tft/tft_spi.h
C++
agpl-3.0
2,200
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #include "../../../inc/MarlinConfig.h" #if ENABLED(TOUCH_BUTTONS_HW_SPI) #include <SPI.h> #endif #ifndef TOUCH_MISO_PIN #define TOUCH_MISO_PIN SD_MISO_PIN #endif #ifndef TOUCH_MOSI_PIN #define TOUCH_MOSI_PIN SD_MOSI_PIN #endif #ifndef TOUCH_SCK_PIN #define TOUCH_SCK_PIN SD_SCK_PIN #endif #ifndef TOUCH_CS_PIN #define TOUCH_CS_PIN SD_SS_PIN #endif #ifndef TOUCH_INT_PIN #define TOUCH_INT_PIN -1 #endif #define XPT2046_DFR_MODE 0x00 #define XPT2046_SER_MODE 0x04 #define XPT2046_CONTROL 0x80 enum XPTCoordinate : uint8_t { XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, }; #ifndef XPT2046_Z1_THRESHOLD #define XPT2046_Z1_THRESHOLD 10 #endif class XPT2046 { private: static bool isBusy() { return false; } static uint16_t getRawData(const XPTCoordinate coordinate); static bool isTouched(); static void dataTransferBegin(); static void dataTransferEnd(); #if ENABLED(TOUCH_BUTTONS_HW_SPI) static uint16_t hardwareIO(uint16_t data); #endif static uint16_t softwareIO(uint16_t data); static uint16_t IO(uint16_t data = 0); public: #if ENABLED(TOUCH_BUTTONS_HW_SPI) static SPIClass SPIx; #endif static void init(); static bool getRawPoint(int16_t * const x, int16_t * const y); };
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/tft/xpt2046.h
C++
agpl-3.0
2,321
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * HAL timers for Linux X86_64 */ #include <stdint.h> // ------------------------ // Defines // ------------------------ #define FORCE_INLINE __attribute__((always_inline)) inline typedef uint64_t hal_timer_t; #define HAL_TIMER_TYPE_MAX 0xFFFFFFFFFFFFFFFF #define HAL_TIMER_RATE ((SystemCoreClock) / 4) // frequency of timers peripherals #ifndef MF_TIMER_STEP #define MF_TIMER_STEP 0 // Timer Index for Stepper #endif #ifndef MF_TIMER_PULSE #define MF_TIMER_PULSE MF_TIMER_STEP #endif #ifndef MF_TIMER_TEMP #define MF_TIMER_TEMP 1 // Timer Index for Temperature #endif #ifndef MF_TIMER_SYSTICK #define MF_TIMER_SYSTICK 2 // Timer Index for Systick #endif #define SYSTICK_TIMER_FREQUENCY 1000 #define TEMP_TIMER_RATE 1000000 #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US) #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_STEP) #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_STEP) #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(MF_TIMER_STEP) #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(MF_TIMER_TEMP) #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(MF_TIMER_TEMP) #ifndef HAL_STEP_TIMER_ISR #define HAL_STEP_TIMER_ISR() extern "C" void TIMER0_IRQHandler() #endif #ifndef HAL_TEMP_TIMER_ISR #define HAL_TEMP_TIMER_ISR() extern "C" void TIMER1_IRQHandler() #endif void HAL_timer_init(); void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare); hal_timer_t HAL_timer_get_compare(const uint8_t timer_num); hal_timer_t HAL_timer_get_count(const uint8_t timer_num); void HAL_timer_enable_interrupt(const uint8_t timer_num); void HAL_timer_disable_interrupt(const uint8_t timer_num); bool HAL_timer_interrupt_enabled(const uint8_t timer_num); #define HAL_timer_isr_prologue(T) NOOP #define HAL_timer_isr_epilogue(T) NOOP
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/timers.h
C
agpl-3.0
3,404
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ // adapted from I2C/master/master.c example // https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html #ifdef __PLAT_NATIVE_SIM__ #include <cstdint> #ifdef __cplusplus extern "C" { #endif uint8_t u8g_i2c_start(const uint8_t sla) { return 1; } void u8g_i2c_init(const uint8_t clock_option) { } uint8_t u8g_i2c_send_byte(uint8_t data) { return 1; } void u8g_i2c_stop() { } #ifdef __cplusplus } #endif #endif // __PLAT_NATIVE_SIM__
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/LCD_I2C_routines.cpp
C++
agpl-3.0
1,355
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once #ifdef __cplusplus extern "C" { #endif void u8g_i2c_init(const uint8_t clock_options); //uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos); uint8_t u8g_i2c_start(uint8_t sla); uint8_t u8g_i2c_send_byte(uint8_t data); void u8g_i2c_stop(); #ifdef __cplusplus } #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/LCD_I2C_routines.h
C
agpl-3.0
1,146
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Native/Simulator LCD-specific defines */ void usleep(uint64_t microsec); uint8_t u8g_com_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); uint8_t u8g_com_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); #define U8G_COM_HAL_SW_SPI_FN u8g_com_sw_spi_fn #define U8G_COM_ST7920_HAL_SW_SPI u8g_com_ST7920_sw_spi_fn
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/LCD_defines.h
C
agpl-3.0
1,250
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * LCD delay routines - used by all the drivers. * * These are based on the LPC1768 routines. * * Couldn't just call exact copies because the overhead * results in a one microsecond delay taking about 4µS. */ #ifdef __cplusplus extern "C" { #endif void U8g_delay(int msec); void u8g_MicroDelay(); void u8g_10MicroDelay(); #ifdef __cplusplus } #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/LCD_delay.h
C
agpl-3.0
1,244
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Low level pin manipulation routines - used by all the drivers. * * These are based on the LPC1768 pinMode, digitalRead & digitalWrite routines. * * Couldn't just call exact copies because the overhead killed the LCD update speed * With an intermediate level the softspi was running in the 10-20kHz range which * resulted in using about about 25% of the CPU's time. */ #ifdef __PLAT_NATIVE_SIM__ #include "../fastio.h" #include "LCD_pin_routines.h" #ifdef __cplusplus extern "C" { #endif void u8g_SetPinOutput(uint8_t internal_pin_number) { SET_DIR_OUTPUT(internal_pin_number); } void u8g_SetPinInput(uint8_t internal_pin_number) { SET_DIR_INPUT(internal_pin_number); } void u8g_SetPinLevel(uint8_t pin, uint8_t pin_status) { WRITE_PIN(pin, pin_status); } uint8_t u8g_GetPinLevel(uint8_t pin) { return READ_PIN(pin); } #ifdef __cplusplus } #endif #endif // __PLAT_NATIVE_SIM__
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/LCD_pin_routines.cpp
C++
agpl-3.0
1,763
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * Low level pin manipulation routines - used by all the drivers. * * These are based on the LPC1768 pinMode, digitalRead & digitalWrite routines. * * Couldn't just call exact copies because the overhead killed the LCD update speed * With an intermediate level the softspi was running in the 10-20kHz range which * resulted in using about about 25% of the CPU's time. */ #ifdef __cplusplus extern "C" { #endif void u8g_SetPinOutput(uint8_t internal_pin_number); void u8g_SetPinInput(uint8_t internal_pin_number); void u8g_SetPinLevel(uint8_t pin, uint8_t pin_status); uint8_t u8g_GetPinLevel(uint8_t pin); #ifdef __cplusplus } #endif
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/LCD_pin_routines.h
C
agpl-3.0
1,530
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_st7920_hw_spi.c * * Universal 8bit Graphics Library * * Copyright (c) 2011, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef __PLAT_NATIVE_SIM__ #include "../../../inc/MarlinConfig.h" #if IS_U8GLIB_ST7920 #include <U8glib-HAL.h> #include "../../shared/Delay.h" #undef SPI_SPEED #define SPI_SPEED 6 #define SPI_DELAY_CYCLES (1 + SPI_SPEED * 10) static pin_t SCK_pin_ST7920_HAL, MOSI_pin_ST7920_HAL_HAL; static uint8_t SPI_speed = 0; static uint8_t swSpiTransfer(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin) { for (uint8_t i = 0; i < 8; i++) { WRITE_PIN(mosi_pin, !!(b & 0x80)); DELAY_CYCLES(SPI_SPEED); WRITE_PIN(sck_pin, HIGH); DELAY_CYCLES(SPI_SPEED); b <<= 1; if (miso_pin >= 0 && READ_PIN(miso_pin)) b |= 1; WRITE_PIN(sck_pin, LOW); DELAY_CYCLES(SPI_SPEED); } return b; } static uint8_t swSpiInit(const uint8_t spiRate, const pin_t sck_pin, const pin_t mosi_pin) { WRITE_PIN(mosi_pin, HIGH); WRITE_PIN(sck_pin, LOW); return spiRate; } static void u8g_com_st7920_write_byte_sw_spi(uint8_t rs, uint8_t val) { static uint8_t rs_last_state = 255; if (rs != rs_last_state) { // Transfer Data (FA) or Command (F8) swSpiTransfer(rs ? 0x0FA : 0x0F8, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); rs_last_state = rs; DELAY_US(40); // Give the controller time to process the data: 20 is bad, 30 is OK, 40 is safe } swSpiTransfer(val & 0x0F0, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); swSpiTransfer(val << 4, SPI_speed, SCK_pin_ST7920_HAL, -1, MOSI_pin_ST7920_HAL_HAL); } #ifdef __cplusplus extern "C" { #endif uint8_t u8g_com_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: SCK_pin_ST7920_HAL = u8g->pin_list[U8G_PI_SCK]; MOSI_pin_ST7920_HAL_HAL = u8g->pin_list[U8G_PI_MOSI]; u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_SetPIOutput(u8g, U8G_PI_SCK); u8g_SetPIOutput(u8g, U8G_PI_MOSI); u8g_Delay(5); SPI_speed = swSpiInit(SPI_SPEED, SCK_pin_ST7920_HAL, MOSI_pin_ST7920_HAL_HAL); u8g_SetPILevel(u8g, U8G_PI_CS, 0); u8g_SetPILevel(u8g, U8G_PI_SCK, 0); u8g_SetPILevel(u8g, U8G_PI_MOSI, 0); u8g->pin_list[U8G_PI_A0_STATE] = 0; /* initial RS state: command mode */ break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g->pin_list[U8G_PI_A0_STATE] = arg_val; break; case U8G_COM_MSG_CHIP_SELECT: if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_CS]) u8g_SetPILevel(u8g, U8G_PI_CS, arg_val); //note: the st7920 has an active high chip select break; case U8G_COM_MSG_WRITE_BYTE: u8g_com_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { u8g_com_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { u8g_com_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++); arg_val--; } } break; } return 1; } #ifdef __cplusplus } #endif #endif // IS_U8GLIB_ST7920 #endif // __PLAT_NATIVE_SIM__
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/u8g_com_st7920_sw_spi.cpp
C++
agpl-3.0
5,860
/** * Marlin 3D Printer Firmware * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * Based on u8g_com_std_sw_spi.c * * Universal 8bit Graphics Library * * Copyright (c) 2015, olikraus@gmail.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef __PLAT_NATIVE_SIM__ #include "../../../inc/MarlinConfig.h" #if HAS_MARLINUI_U8GLIB && !IS_U8GLIB_ST7920 #undef SPI_SPEED #define SPI_SPEED 2 // About 2 MHz #include <Arduino.h> #include <U8glib-HAL.h> #ifdef __cplusplus extern "C" { #endif uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) { for (uint8_t i = 0; i < 8; ++i) { if (spi_speed == 0) { WRITE_PIN(mosi_pin, !!(b & 0x80)); WRITE_PIN(sck_pin, HIGH); b <<= 1; if (miso_pin >= 0 && READ_PIN(miso_pin)) b |= 1; WRITE_PIN(sck_pin, LOW); } else { const uint8_t state = (b & 0x80) ? HIGH : LOW; for (uint8_t j = 0; j < spi_speed; ++j) WRITE_PIN(mosi_pin, state); for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) WRITE_PIN(sck_pin, HIGH); b <<= 1; if (miso_pin >= 0 && READ_PIN(miso_pin)) b |= 1; for (uint8_t j = 0; j < spi_speed; ++j) WRITE_PIN(sck_pin, LOW); } } return b; } uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) { for (uint8_t i = 0; i < 8; ++i) { const uint8_t state = (b & 0x80) ? HIGH : LOW; if (spi_speed == 0) { WRITE_PIN(sck_pin, LOW); WRITE_PIN(mosi_pin, state); WRITE_PIN(mosi_pin, state); // need some setup time WRITE_PIN(sck_pin, HIGH); } else { for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) WRITE_PIN(sck_pin, LOW); for (uint8_t j = 0; j < spi_speed; ++j) WRITE_PIN(mosi_pin, state); for (uint8_t j = 0; j < spi_speed; ++j) WRITE_PIN(sck_pin, HIGH); } b <<= 1; if (miso_pin >= 0 && READ_PIN(miso_pin)) b |= 1; } return b; } static uint8_t SPI_speed = 0; static uint8_t swSpiInit(const uint8_t spi_speed, const uint8_t clk_pin, const uint8_t mosi_pin) { return spi_speed; } static void u8g_sw_spi_shift_out(uint8_t dataPin, uint8_t clockPin, uint8_t val) { #if ANY(FYSETC_MINI_12864, MKS_MINI_12864) swSpiTransfer_mode_3(val, SPI_speed, clockPin, -1, dataPin); #else swSpiTransfer_mode_0(val, SPI_speed, clockPin, -1, dataPin); #endif } uint8_t u8g_com_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch (msg) { case U8G_COM_MSG_INIT: u8g_SetPIOutput(u8g, U8G_PI_SCK); u8g_SetPIOutput(u8g, U8G_PI_MOSI); u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_SetPIOutput(u8g, U8G_PI_A0); if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPIOutput(u8g, U8G_PI_RESET); SPI_speed = swSpiInit(SPI_SPEED, u8g->pin_list[U8G_PI_SCK], u8g->pin_list[U8G_PI_MOSI]); u8g_SetPILevel(u8g, U8G_PI_SCK, 0); u8g_SetPILevel(u8g, U8G_PI_MOSI, 0); break; case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_RESET: if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_CHIP_SELECT: #if ANY(FYSETC_MINI_12864, MKS_MINI_12864) // LCD SPI is running mode 3 while SD card is running mode 0 if (arg_val) { // SCK idle state needs to be set to the proper idle state before // the next chip select goes active u8g_SetPILevel(u8g, U8G_PI_SCK, 1); // Set SCK to mode 3 idle state before CS goes active u8g_SetPILevel(u8g, U8G_PI_CS, LOW); } else { u8g_SetPILevel(u8g, U8G_PI_CS, HIGH); u8g_SetPILevel(u8g, U8G_PI_SCK, 0); // Set SCK to mode 0 idle state after CS goes inactive } #else u8g_SetPILevel(u8g, U8G_PI_CS, !arg_val); #endif break; case U8G_COM_MSG_WRITE_BYTE: u8g_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { u8g_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], *ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t *)arg_ptr; while (arg_val > 0) { u8g_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], u8g_pgm_read(ptr)); ptr++; arg_val--; } } break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_SetPILevel(u8g, U8G_PI_A0, arg_val); break; } return 1; } #ifdef __cplusplus } #endif #elif NONE(TFT_COLOR_UI, TFT_CLASSIC_UI, TFT_LVGL_UI, HAS_MARLINUI_HD44780) && HAS_MARLINUI_U8GLIB #include <U8glib-HAL.h> uint8_t u8g_com_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {return 0;} #endif // HAS_MARLINUI_U8GLIB && !IS_U8GLIB_ST7920 #endif // __PLAT_NATIVE_SIM__
2301_81045437/Marlin
Marlin/src/HAL/NATIVE_SIM/u8g/u8g_com_sw_spi.cpp
C++
agpl-3.0
7,337
/** * Marlin 3D Printer Firmware * Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * SAMD21 HAL developed by Bart Meijer (brupje) * Based on SAMD51 HAL by Giuliano Zaro (AKA GMagician) */ #ifdef __SAMD21__ #include "../../inc/MarlinConfig.h" #include <wiring_private.h> #if USING_HW_SERIALUSB DefaultSerial1 MSerialUSB(false, SerialUSB); #endif #if USING_HW_SERIAL0 DefaultSerial2 MSerial1(false, Serial1); #endif #if USING_HW_SERIAL1 DefaultSerial3 MSerial2(false, Serial2); #endif #define WDT_CONFIG_PER_7_Val 0x9u #define WDT_CONFIG_PER_Pos 0 #define WDT_CONFIG_PER_7 (WDT_CONFIG_PER_7_Val << WDT_CONFIG_PER_Pos) #if ENABLED(USE_WATCHDOG) #define WDT_TIMEOUT_REG TERN(WATCHDOG_DURATION_8S, WDT_CONFIG_PER_CYC8192, WDT_CONFIG_PER_CYC4096) // 4 or 8 second timeout void MarlinHAL::watchdog_init() { // Set up the generic clock (GCLK2) used to clock the watchdog timer at 1.024kHz GCLK->GENDIV.reg = GCLK_GENDIV_DIV(4) | // Divide the 32.768kHz clock source by divisor 32, where 2^(4 + 1): 32.768kHz/32=1.024kHz GCLK_GENDIV_ID(2); // Select Generic Clock (GCLK) 2 while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization REG_GCLK_GENCTRL = GCLK_GENCTRL_DIVSEL | // Set to divide by 2^(GCLK_GENDIV_DIV(4) + 1) GCLK_GENCTRL_IDC | // Set the duty cycle to 50/50 HIGH/LOW GCLK_GENCTRL_GENEN | // Enable GCLK2 GCLK_GENCTRL_SRC_OSCULP32K | // Set the clock source to the ultra low power oscillator (OSCULP32K) GCLK_GENCTRL_ID(2); // Select GCLK2 while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization // Feed GCLK2 to WDT (Watchdog Timer) REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN | // Enable GCLK2 to the WDT GCLK_CLKCTRL_GEN_GCLK2 | // Select GCLK2 GCLK_CLKCTRL_ID_WDT; // Feed the GCLK2 to the WDT while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization WDT->CONFIG.bit.PER = WDT_CONFIG_PER_7; // Set the WDT reset timeout to 4 seconds while (WDT->STATUS.bit.SYNCBUSY); // Wait for synchronization REG_WDT_CTRL = WDT_CTRL_ENABLE; // Enable the WDT in normal mode while (WDT->STATUS.bit.SYNCBUSY); // Wait for synchronization } // Reset watchdog. MUST be called at least every 4 seconds after the // first watchdog_init or SAMD will go into emergency procedures. void MarlinHAL::watchdog_refresh() { WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY; while (WDT->STATUS.bit.SYNCBUSY); } #endif // ------------------------ // Types // ------------------------ // ------------------------ // Private Variables // ------------------------ // ------------------------ // Private functions // ------------------------ void MarlinHAL::dma_init() {} // ------------------------ // Public functions // ------------------------ // HAL initialization task void MarlinHAL::init() { TERN_(DMA_IS_REQUIRED, dma_init()); #if HAS_MEDIA #if HAS_SD_DETECT && SD_CONNECTION_IS(ONBOARD) SET_INPUT_PULLUP(SD_DETECT_PIN); #endif OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up #endif } #pragma push_macro("WDT") #undef WDT // Required to be able to use '.bit.WDT'. Compiler wrongly replace struct field with WDT define uint8_t MarlinHAL::get_reset_source() { return 0; } #pragma pop_macro("WDT") void MarlinHAL::reboot() { NVIC_SystemReset(); } extern "C" { void * _sbrk(int incr); extern unsigned int __bss_end__; // end of bss section } // Return free memory between end of heap (or end bss) and whatever is current int freeMemory() { int free_memory, heap_end = (int)_sbrk(0); return (int)&free_memory - (heap_end ?: (int)&__bss_end__); } // ------------------------ // ADC // ------------------------ uint16_t MarlinHAL::adc_result; void MarlinHAL::adc_init() { /* thanks to https://www.eevblog.com/forum/microcontrollers/samd21g18-adc-with-resrdy-interrupts-only-reads-once-or-twice/ */ ADC->CTRLA.bit.ENABLE = false; while(ADC->STATUS.bit.SYNCBUSY); // load chip corrections uint32_t bias = (*((uint32_t *) ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos; uint32_t linearity = (*((uint32_t *) ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; linearity |= ((*((uint32_t *) ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos) << 5; /* Wait for bus synchronization. */ while (ADC->STATUS.bit.SYNCBUSY) {}; ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(linearity); /* Wait for bus synchronization. */ while (ADC->STATUS.bit.SYNCBUSY) {}; ADC->CTRLA.bit.SWRST = true; while(ADC->STATUS.bit.SYNCBUSY); ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INTVCC1; ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_32| ADC_AVGCTRL_ADJRES(4);; ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV128 | ADC_CTRLB_RESSEL_16BIT | ADC_CTRLB_FREERUN; while(ADC->STATUS.bit.SYNCBUSY); ADC->SAMPCTRL.bit.SAMPLEN = 0x00; while(ADC->STATUS.bit.SYNCBUSY); ADC->INPUTCTRL.reg = ADC_INPUTCTRL_INPUTSCAN(HAL_ADC_AIN_LEN) // scan (INPUTSCAN + NUM_EXTUDERS - 1) pins | ADC_INPUTCTRL_GAIN_DIV2 |ADC_INPUTCTRL_MUXNEG_GND| HAL_ADC_AIN_START ; /* set to first AIN */ while(ADC->STATUS.bit.SYNCBUSY); ADC->INTENSET.reg |= ADC_INTENSET_RESRDY; // enable Result Ready ADC interrupts while (ADC->STATUS.bit.SYNCBUSY); NVIC_EnableIRQ(ADC_IRQn); // enable ADC interrupts NVIC_SetPriority(ADC_IRQn, 3); ADC->CTRLA.bit.ENABLE = true; } volatile uint32_t adc_results[HAL_ADC_AIN_NUM_SENSORS]; void ADC_Handler() { while(ADC->STATUS.bit.SYNCBUSY == 1); int pos = ADC->INPUTCTRL.bit.INPUTOFFSET; adc_results[pos] = ADC->RESULT.reg; /* Read the value. */ ADC->INTFLAG.reg = ADC_INTENSET_RESRDY; /* Clear the data ready flag. */ } void MarlinHAL::adc_start(const pin_t pin) { /* due to the way INPUTOFFSET works, the last sensor is the first position in the array and we want the ADC_handler interrupt to be as simple possible, so we do the calculation here. */ unsigned int pos = PIN_TO_INPUTCTRL(pin) - HAL_ADC_AIN_START + 1; if (pos == HAL_ADC_AIN_NUM_SENSORS) pos = 0; adc_result = adc_results[pos]; // 16-bit resolution //adc_result = 0xFFFF; } #endif // __SAMD21__
2301_81045437/Marlin
Marlin/src/HAL/SAMD21/HAL.cpp
C++
agpl-3.0
7,372
/** * Marlin 3D Printer Firmware * Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ #pragma once /** * SAMD21 HAL developed by Bart Meijer (brupje) * Based on SAMD51 HAL by Giuliano Zaro (AKA GMagician) */ #define CPU_32_BIT #include "../shared/Marduino.h" #include "../shared/math_32bit.h" #include "../shared/HAL_SPI.h" #include "fastio.h" // ------------------------ // Serial ports // ------------------------ #include "../../core/serial_hook.h" typedef ForwardSerial1Class< decltype(SerialUSB) > DefaultSerial1; extern DefaultSerial1 MSerialUSB; // Serial ports typedef ForwardSerial1Class< decltype(Serial1) > DefaultSerial2; typedef ForwardSerial1Class< decltype(Serial2) > DefaultSerial3; extern DefaultSerial2 MSerial0; extern DefaultSerial3 MSerial1; #define __MSERIAL(X) MSerial##X #define _MSERIAL(X) __MSERIAL(X) #define MSERIAL(X) _MSERIAL(INCREMENT(X)) #if WITHIN(SERIAL_PORT, 0, 1) #define MYSERIAL1 MSERIAL(SERIAL_PORT) #elif SERIAL_PORT == -1 #define MYSERIAL1 MSerialUSB #else #error "SERIAL_PORT must be -1 (Native USB only)." #endif #ifdef SERIAL_PORT_2 #if WITHIN(SERIAL_PORT_2, 0, 1) #define MYSERIAL2 MSERIAL(SERIAL_PORT) #elif SERIAL_PORT_2 == -1 #define MYSERIAL2 MSerialUSB #else #error "SERIAL_PORT_2 must be -1 (Native USB only)." #endif #endif #ifdef MMU2_SERIAL_PORT #if WITHIN(MMU2_SERIAL_PORT, 0, 1) #define MMU2_SERIAL MSERIAL(SERIAL_PORT) #elif MMU2_SERIAL_PORT == -1 #define MMU2_SERIAL MSerialUSB #else #error "MMU2_SERIAL_PORT must be -1 (Native USB only)." #endif #endif #ifdef LCD_SERIAL_PORT #if WITHIN(LCD_SERIAL_PORT, 0, 1) #define LCD_SERIAL MSERIAL(SERIAL_PORT) #elif LCD_SERIAL_PORT == -1 #define LCD_SERIAL MSerialUSB #else #error "LCD_SERIAL_PORT must be -1 (Native USB only)." #endif #endif typedef int8_t pin_t; #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp class Servo; typedef Servo hal_servo_t; // // Interrupts // #define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq() #define CRITICAL_SECTION_END() if (irqon) __enable_irq() #define cli() __disable_irq() // Disable interrupts #define sei() __enable_irq() // Enable interrupts // // ADC // #define HAL_ADC_FILTERED 1 // Disable Marlin's oversampling. The HAL filters ADC values. #define HAL_ADC_VREF_MV 3300 #define HAL_ADC_RESOLUTION 12 #define HAL_ADC_AIN_START ADC_INPUTCTRL_MUXPOS_PIN3 #define HAL_ADC_AIN_NUM_SENSORS 3 #define HAL_ADC_AIN_LEN HAL_ADC_AIN_NUM_SENSORS-1 // // Pin Mapping for M42, M43, M226 // #define GET_PIN_MAP_PIN(index) index #define GET_PIN_MAP_INDEX(pin) pin #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval) // // Tone // void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0); void noTone(const pin_t _pin); // ------------------------ // Class Utilities // ------------------------ #pragma GCC diagnostic push #if GCC_VERSION <= 50000 #pragma GCC diagnostic ignored "-Wunused-function" #endif #ifdef __cplusplus extern "C" { #endif char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s); extern "C" int freeMemory(); #ifdef __cplusplus } #endif #pragma GCC diagnostic pop // ------------------------ // MarlinHAL Class // ------------------------ class MarlinHAL { public: // Earliest possible init, before setup() MarlinHAL() {} // Watchdog static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); static void init(); // Called early in setup() static void init_board() {} // Called less early in setup() static void reboot(); // Restart the firmware from 0x0 // Interrupts static bool isr_state() { return !__get_PRIMASK(); } static void isr_on() { sei(); } static void isr_off() { cli(); } static void delay_ms(const int ms) { delay(ms); } // Tasks, called from idle() static void idletask() {} // Reset static uint8_t get_reset_source(); static void clear_reset_source() {} // Free SRAM static int freeMemory() { return ::freeMemory(); } // // ADC Methods // static uint16_t adc_result; // Called by Temperature::init once at startup static void adc_init(); // Called by Temperature::init for each sensor at startup static void adc_enable(const uint8_t ch) {} // Begin ADC sampling on the given pin. Called from Temperature::isr! static void adc_start(const pin_t pin); // Is the ADC ready for reading? static bool adc_ready() { return true; } // The current value of the ADC register static uint16_t adc_value() { return adc_result; } /** * Set the PWM duty cycle for the pin to the given value. * No option to invert the duty cycle [default = false] * No option to change the scale of the provided value to enable finer PWM duty control [default = 255] */ static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); } private: static void dma_init(); };
2301_81045437/Marlin
Marlin/src/HAL/SAMD21/HAL.h
C++
agpl-3.0
5,867
/** * Marlin 3D Printer Firmware * Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * * Based on Sprinter and grbl. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * * This program 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. * * This program 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 this program. If not, see <https://www.gnu.org/licenses/>. * */ /** * SAMD21 HAL developed by Bart Meijer (brupje) * Based on SAMD51 HAL by Giuliano Zaro (AKA GMagician) */ /** * Hardware and software SPI implementations are included in this file. * * Control of the slave select pin(s) is handled by the calling routines and * SAMD21 let hardware SPI handling to remove SS from its logic. */ #ifdef __SAMD21__ // -------------------------------------------------------------------------- // Includes // -------------------------------------------------------------------------- #include "../../inc/MarlinConfig.h" #include <SPI.h> // -------------------------------------------------------------------------- // Public functions // -------------------------------------------------------------------------- #if ANY(SOFTWARE_SPI, FORCE_SOFT_SPI) // ------------------------ // Software SPI // ------------------------ #error "Software SPI not supported for SAMD21. Use Hardware SPI." #else // !SOFTWARE_SPI static SPISettings spiConfig; // ------------------------ // Hardware SPI // ------------------------ void spiBegin() { spiInit(SPI_HALF_SPEED); } void spiInit(uint8_t spiRate) { // Use Marlin datarates uint32_t clock; switch (spiRate) { case SPI_FULL_SPEED: clock = 8000000; break; case SPI_HALF_SPEED: clock = 4000000; break; case SPI_QUARTER_SPEED: clock = 2000000; break; case SPI_EIGHTH_SPEED: clock = 1000000; break; case SPI_SIXTEENTH_SPEED: clock = 500000; break; case SPI_SPEED_5: clock = 250000; break; case SPI_SPEED_6: clock = 125000; break; default: clock = 4000000; break; // Default from the SPI library } spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0); SPI.begin(); } /** * @brief Receives a single byte from the SPI port. * * @return Byte received * * @details */ uint8_t spiRec() { SPI.beginTransaction(spiConfig); uint8_t returnByte = SPI.transfer(0xFF); SPI.endTransaction(); return returnByte; } /** * @brief Receives a number of bytes from the SPI port to a buffer * * @param buf Pointer to starting address of buffer to write to. * @param nbyte Number of bytes to receive. * @return Nothing */ void spiRead(uint8_t *buf, uint16_t nbyte) { if (nbyte == 0) return; memset(buf, 0xFF, nbyte); SPI.beginTransaction(spiConfig); SPI.transfer(buf, nbyte); SPI.endTransaction(); } /** * @brief Sends a single byte on SPI port * * @param b Byte to send * * @details */ void spiSend(uint8_t b) { SPI.beginTransaction(spiConfig); SPI.transfer(b); SPI.endTransaction(); } /** * @brief Write token and then write from 512 byte buffer to SPI (for SD card) * * @param buf Pointer with buffer start address * @return Nothing * * @details Uses DMA */ void spiSendBlock(uint8_t token, const uint8_t *buf) { SPI.beginTransaction(spiConfig); SPI.transfer(token); SPI.transfer((uint8_t*)buf, 512); SPI.endTransaction(); } void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) { spiConfig = SPISettings(spiClock, (BitOrder)bitOrder, dataMode); SPI.beginTransaction(spiConfig); } #endif // !SOFTWARE_SPI #endif // __SAMD21__
2301_81045437/Marlin
Marlin/src/HAL/SAMD21/HAL_SPI.cpp
C++
agpl-3.0
4,226