Merge pull request #8148 from tcm0116/2.0.x-multi_host

[2.0.x] Add multi-host support
This commit is contained in:
Scott Lahteine
2018-01-10 02:38:59 -06:00
committed by GitHub
82 changed files with 1562 additions and 748 deletions

View File

@ -33,8 +33,7 @@
// Includes
// --------------------------------------------------------------------------
#include "../HAL.h"
#include "../../core/macros.h"
#include "../../inc/MarlinConfig.h"
// --------------------------------------------------------------------------
// Externals

View File

@ -47,6 +47,12 @@
#include "watchdog_AVR.h"
#include "math_AVR.h"
#ifdef USBCON
#include "HardwareSerial.h"
#else
#include "MarlinSerial.h"
#endif
// --------------------------------------------------------------------------
// Defines
// --------------------------------------------------------------------------
@ -79,6 +85,18 @@ typedef int8_t pin_t;
//extern uint8_t MCUSR;
#define NUM_SERIAL 1
#ifdef USBCON
#if ENABLED(BLUETOOTH)
#define MYSERIAL0 bluetoothSerial
#else
#define MYSERIAL0 Serial
#endif
#else
#define MYSERIAL0 customizedSerial
#endif
// --------------------------------------------------------------------------
// Public functions
// --------------------------------------------------------------------------

View File

@ -483,9 +483,9 @@ inline void report_pin_state_extended(pin_t pin, bool ignore, bool extended = fa
for (uint8_t y = 0; y < 28; y++) { // always print pin name
temp_char = pgm_read_byte(name_mem_pointer + y);
if (temp_char != 0)
MYSERIAL.write(temp_char);
SERIAL_CHAR(temp_char);
else {
for (uint8_t i = 0; i < 28 - y; i++) MYSERIAL.write(' ');
for (uint8_t i = 0; i < 28 - y; i++) SERIAL_CHAR(' ');
break;
}
}

View File

@ -66,9 +66,9 @@ void PRINT_ARRAY_NAME(uint8_t x) {
for (uint8_t y = 0; y < MAX_NAME_LENGTH; y++) {
char temp_char = pgm_read_byte(name_mem_pointer + y);
if (temp_char != 0)
MYSERIAL.write(temp_char);
SERIAL_CHAR(temp_char);
else {
for (uint8_t i = 0; i < MAX_NAME_LENGTH - y; i++) MYSERIAL.write(' ');
for (uint8_t i = 0; i < MAX_NAME_LENGTH - y; i++) SERIAL_CHAR(' ');
break;
}
}

View File

@ -121,7 +121,7 @@ static uint8_t curGroup = 0xFF; // Current FLASH group
char buffer[80];
sprintf(buffer, "Page: %d (0x%04x)\n", page, page);
MYSERIAL.print(buffer);
SERIAL_PROTOCOL(buffer);
char* p = &buffer[0];
for (int i = 0; i< PageSize; ++i) {
@ -133,7 +133,7 @@ static uint8_t curGroup = 0xFF; // Current FLASH group
if ((i & 15) == 15) {
*p++ = '\n';
*p = 0;
MYSERIAL.print(buffer);
SERIAL_PROTOCOL(buffer);
p = &buffer[0];
}
}
@ -182,7 +182,7 @@ static bool ee_PageWrite(uint16_t page,const void* data) {
SERIAL_ECHOLNPAIR("EEPROM PageWrite ",page);
SERIAL_ECHOLNPAIR(" in FLASH address ",(uint32_t)addrflash);
SERIAL_ECHOLNPAIR(" base address ",(uint32_t)getFlashStorage(0));
MYSERIAL.flush();
SERIAL_FLUSH();
#endif
// Get the page relative to the start of the EFC controller, and the EFC controller to use
@ -313,7 +313,7 @@ static bool ee_PageErase(uint16_t page) {
SERIAL_ECHOLNPAIR("EEPROM PageErase ",page);
SERIAL_ECHOLNPAIR(" in FLASH address ",(uint32_t)addrflash);
SERIAL_ECHOLNPAIR(" base address ",(uint32_t)getFlashStorage(0));
MYSERIAL.flush();
SERIAL_FLUSH();
#endif
// Get the page relative to the start of the EFC controller, and the EFC controller to use
@ -943,7 +943,7 @@ static void ee_Init() {
#ifdef EE_EMU_DEBUG
SERIAL_ECHO_START();
SERIAL_ECHOLNPAIR("EEPROM Current Group: ",curGroup);
MYSERIAL.flush();
SERIAL_FLUSH();
#endif
// Now, validate that all the other group pages are empty
@ -957,7 +957,7 @@ static void ee_Init() {
SERIAL_ECHO_START();
SERIAL_ECHOPAIR("EEPROM Page ",page);
SERIAL_ECHOLNPAIR(" not clean on group ",grp);
MYSERIAL.flush();
SERIAL_FLUSH();
#endif
ee_PageErase(grp * PagesPerGroup + page);
}
@ -978,7 +978,7 @@ static void ee_Init() {
#ifdef EE_EMU_DEBUG
SERIAL_ECHO_START();
SERIAL_ECHOLNPAIR("EEPROM Active page: ",curPage);
MYSERIAL.flush();
SERIAL_FLUSH();
#endif
// Make sure the pages following the first clean one are also clean
@ -988,7 +988,7 @@ static void ee_Init() {
SERIAL_ECHO_START();
SERIAL_ECHOPAIR("EEPROM Page ",page);
SERIAL_ECHOLNPAIR(" not clean on active group ",curGroup);
MYSERIAL.flush();
SERIAL_FLUSH();
ee_Dump(curGroup * PagesPerGroup + page, getFlashStorage(curGroup * PagesPerGroup + page));
#endif
ee_PageErase(curGroup * PagesPerGroup + page);

View File

@ -40,8 +40,15 @@
//
// Defines
//
#if SERIAL_PORT >= -1 && SERIAL_PORT <= 4
#define MYSERIAL customizedSerial
#define NUM_SERIAL 1
//#undef SERIAL_PORT
//#define SERIAL_PORT -1
#if SERIAL_PORT == -1
#define MYSERIAL0 SerialUSB
#else
#define MYSERIAL0 customizedSerial
#endif
// We need the previous define before the include, or compilation bombs...
@ -147,6 +154,10 @@ uint16_t HAL_getAdcFreerun(uint8_t chan, bool wait_for_conversion = false);
void HAL_enable_AdcFreerun(void);
//void HAL_disable_AdcFreerun(uint8_t chan);
/**
* Pin Map
*/
#define GET_PIN_MAP_PIN(index) index
#define GET_PIN_MAP_INDEX(pin) pin
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

View File

@ -29,7 +29,7 @@ extern "C" {
HalSerial usb_serial;
//u8glib required fucntions
// U8glib required functions
extern "C" void u8g_xMicroDelay(uint16_t val) {
delayMicroseconds(val);
}
@ -85,7 +85,7 @@ void HAL_adc_enable_channel(int ch) {
pin_t pin = analogInputToDigitalPin(ch);
if (pin == -1) {
MYSERIAL.printf("%sINVALID ANALOG PORT:%d\n", errormagic, ch);
SERIAL_PRINTF("%sINVALID ANALOG PORT:%d\n", errormagic, ch);
kill(MSG_KILLED);
}
@ -97,15 +97,15 @@ void HAL_adc_enable_channel(int ch) {
pin_port == 1 ? 3 : 10;
switch (pin_sel_register) {
case 1 :
case 1:
LPC_PINCON->PINSEL1 &= ~(0x3 << pinsel_start_bit);
LPC_PINCON->PINSEL1 |= (0x1 << pinsel_start_bit);
break;
case 3 :
case 3:
LPC_PINCON->PINSEL3 &= ~(0x3 << pinsel_start_bit);
LPC_PINCON->PINSEL3 |= (0x3 << pinsel_start_bit);
break;
case 0 :
case 0:
LPC_PINCON->PINSEL0 &= ~(0x3 << pinsel_start_bit);
LPC_PINCON->PINSEL0 |= (0x2 << pinsel_start_bit);
break;
@ -115,7 +115,7 @@ void HAL_adc_enable_channel(int ch) {
uint8_t active_adc = 0;
void HAL_adc_start_conversion(const uint8_t ch) {
if (analogInputToDigitalPin(ch) == -1) {
MYSERIAL.printf("HAL: HAL_adc_start_conversion: invalid channel %d\n", ch);
SERIAL_PRINTF("HAL: HAL_adc_start_conversion: invalid channel %d\n", ch);
return;
}

View File

@ -69,19 +69,44 @@ extern "C" volatile uint32_t _millis;
#define ST7920_DELAY_2 DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP
#define ST7920_DELAY_3 DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP;DELAY_5_NOP
//Serial override
extern HalSerial usb_serial;
#if !WITHIN(SERIAL_PORT, -1, 3)
#error "SERIAL_PORT must be from -1 to 3"
#endif
#if SERIAL_PORT == -1
#define MYSERIAL usb_serial
#define MYSERIAL0 usb_serial
#elif SERIAL_PORT == 0
#define MYSERIAL Serial
#define MYSERIAL0 Serial
#elif SERIAL_PORT == 1
#define MYSERIAL Serial1
#define MYSERIAL0 Serial1
#elif SERIAL_PORT == 2
#define MYSERIAL Serial2
#define MYSERIAL0 Serial2
#elif SERIAL_PORT == 3
#define MYSERIAL Serial3
#define MYSERIAL0 Serial3
#endif
#ifdef SERIAL_PORT_2
#if !WITHIN(SERIAL_PORT_2, -1, 3)
#error "SERIAL_PORT_2 must be from -1 to 3"
#elif SERIAL_PORT_2 == SERIAL_PORT
#error "SERIAL_PORT_2 must be different than SERIAL_PORT"
#endif
#define NUM_SERIAL 2
#if SERIAL_PORT_2 == -1
#define MYSERIAL1 usb_serial
#elif SERIAL_PORT_2 == 0
#define MYSERIAL1 Serial
#elif SERIAL_PORT_2 == 1
#define MYSERIAL1 Serial1
#elif SERIAL_PORT_2 == 2
#define MYSERIAL1 Serial2
#elif SERIAL_PORT_2 == 3
#define MYSERIAL1 Serial3
#endif
#else
#define NUM_SERIAL 1
#endif
#define CRITICAL_SECTION_START uint32_t primask = __get_PRIMASK(); __disable_irq();

View File

@ -139,7 +139,7 @@ void analogWrite(pin_t pin, int pwm_value) { // 1 - 254: pwm_value, 0: LOW, 255
if (LPC1768_PWM_attach_pin(pin, 1, LPC_PWM1->MR0, 0xFF))
LPC1768_PWM_write(pin, map(value, 0, 255, 1, LPC_PWM1->MR0)); // map 1-254 onto PWM range
else { // out of PWM channels
if (!out_of_PWM_slots) MYSERIAL.printf(".\nWARNING - OUT OF PWM CHANNELS\n.\n"); //only warn once
if (!out_of_PWM_slots) SERIAL_ECHOPGM(".\nWARNING - OUT OF PWM CHANNELS\n.\n"); //only warn once
out_of_PWM_slots = true;
digitalWrite(pin, value); // treat as a digital pin if out of channels
}

View File

@ -88,14 +88,13 @@ int main(void) {
#endif
}
// Only initialize the debug framework if using the USB emulated serial port
if ((HalSerial*) &MYSERIAL == &usb_serial)
debug_frmwrk_init();
MYSERIAL.begin(BAUDRATE);
MYSERIAL.printf("\n\nLPC1768 (%dMhz) UART0 Initialised\n", SystemCoreClock / 1000000);
#if TX_BUFFER_SIZE > 0
MYSERIAL.flushTX();
#if NUM_SERIAL > 0
MYSERIAL0.begin(BAUDRATE);
#if NUM_SERIAL > 1
MYSERIAL1.begin(BAUDRATE);
#endif
SERIAL_PRINTF("\n\nLPC1768 (%dMhz) UART0 Initialised\n", SystemCoreClock / 1000000);
SERIAL_FLUSHTX();
#endif
HAL_timer_init();

View File

@ -20,8 +20,8 @@
*
*/
#ifndef HAL_SERIAL_H_
#define HAL_SERIAL_H_
#ifndef _HAL_SERIAL_H_
#define _HAL_SERIAL_H_
#include <stdarg.h>
#include <stdio.h>
@ -30,7 +30,7 @@ extern "C" {
#include <debug_frmwrk.h>
}
/*
/**
* Generic RingBuffer
* T type of the buffer array
* S size of the buffer (must be power of 2)
@ -39,46 +39,33 @@ extern "C" {
*/
template <typename T, uint32_t S> class RingBuffer {
public:
RingBuffer() {
index_read = 0;
index_write = 0;
}
RingBuffer() { index_read = index_write = 0; }
uint32_t available() volatile { return buffer_mask & (index_write - index_read); }
uint32_t free() volatile { return buffer_size - available(); }
bool empty() volatile { return (buffer_mask & index_read) == (buffer_mask & index_write); }
bool full() volatile { return index_read == buffer_mask & (index_write + 1); }
void clear() volatile { index_read = index_write = 0; }
bool peek(T *value) volatile {
if(value == 0 || available() == 0)
if (value == 0 || available() == 0)
return false;
*value = buffer[buffer_mask & index_read];
return true;
}
uint32_t available() volatile {
return buffer_mask & (index_write - index_read);
}
uint32_t free() volatile {
return buffer_size - available();
}
T read() volatile {
if((buffer_mask & index_read) == (buffer_mask & index_write)) return T(-1);
int read() volatile {
if ((buffer_mask & index_read) == (buffer_mask & index_write)) return -1;
T val = buffer[buffer_mask & index_read];
++index_read;
return val;
}
bool write( T value) volatile {
bool write(T value) volatile {
uint32_t next_head = buffer_mask & (index_write + 1);
if(next_head != index_read) {
if (next_head != index_read) {
buffer[buffer_mask & index_write] = value;
index_write = next_head;
return true;
}
return false;
}
bool empty() volatile {
return (buffer_mask & index_read) == (buffer_mask & index_write);
}
bool full() volatile {
return index_read == buffer_mask & (index_write + 1);
}
void clear() volatile {
index_read = index_write = 0;
}
private:
static const uint32_t buffer_size = S;
@ -90,42 +77,35 @@ private:
class HalSerial {
public:
HalSerial() {
host_connected = false;
}
HalSerial() { host_connected = false; }
void begin(int32_t baud) {
}
char read() {
return (char)receive_buffer.read();
int peek() {
uint8_t value;
return receive_buffer.peek(&value) ? value : -1;
}
void write(char c) {
_DBC(c); //Duplicate output over uart0
if(host_connected) transmit_buffer.write((uint8_t)c);
}
int read() { return receive_buffer.read(); }
operator bool() {
return host_connected;
}
size_t write(char c) { return host_connected ? transmit_buffer.write((uint8_t)c) : 0; }
operator bool() { return host_connected; }
uint16_t available() {
return (uint16_t)receive_buffer.available();
}
void flush() {
receive_buffer.clear();
}
void flush() { receive_buffer.clear(); }
uint8_t availableForWrite(void){
return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free();
}
void flushTX(void){
if(host_connected) {
while(transmit_buffer.available());
}
if (host_connected)
while (transmit_buffer.available()) { /* nada */ }
}
void printf(const char *format, ...) {
@ -135,7 +115,6 @@ public:
int length = vsnprintf((char *) buffer, 256, (char const *) format, vArgs);
va_end(vArgs);
if (length > 0 && length < 256) {
_DBG(buffer); //Duplicate output over uart0
if (host_connected) {
for (int i = 0; i < length;) {
if (transmit_buffer.write(buffer[i])) {
@ -152,108 +131,72 @@ public:
#define BIN 2
#define BYTE 0
void print_bin(uint32_t value, uint8_t num_digits) {
uint32_t mask = 1 << (num_digits -1);
for (uint8_t i = 0; i < num_digits; i++) {
if (!(i % 4) && i) printf(" ");
if (!(i % 16) && i) printf(" ");
if (value & mask) printf("1");
else printf("0");
if (!(i % 4) && i) write(' ');
if (!(i % 16) && i) write(' ');
if (value & mask) write('1');
else write('0');
value <<= 1;
}
}
void print(const char value[]) {
printf("%s" , value);
}
void print(const char value[]) { printf("%s" , value); }
void print(char value, int nbase = BYTE) {
if (nbase == BIN) print_bin(value,8);
if (nbase == BIN) print_bin(value, 8);
else if (nbase == OCT) printf("%3o", value);
else if (nbase == HEX) printf("%2X", value);
else if (nbase == DEC ) printf("%d", value);
else printf("%c" , value);
}
void print(unsigned char value, int nbase = BYTE) {
if (nbase == BIN) print_bin(value,8);
if (nbase == BIN) print_bin(value, 8);
else if (nbase == OCT) printf("%3o", value);
else if (nbase == HEX) printf("%2X", value);
else printf("%u" , value);
}
void print(int value, int nbase = BYTE) {
if (nbase == BIN) print_bin(value,16);
if (nbase == BIN) print_bin(value, 16);
else if (nbase == OCT) printf("%6o", value);
else if (nbase == HEX) printf("%4X", value);
else printf("%d", value);
}
void print(unsigned int value, int nbase = BYTE) {
if (nbase == BIN) print_bin(value,16);
if (nbase == BIN) print_bin(value, 16);
else if (nbase == OCT) printf("%6o", value);
else if (nbase == HEX) printf("%4X", value);
else printf("%u" , value);
}
void print(long value, int nbase = BYTE) {
if (nbase == BIN) print_bin(value,32);
if (nbase == BIN) print_bin(value, 32);
else if (nbase == OCT) printf("%11o", value);
else if (nbase == HEX) printf("%8X", value);
else printf("%ld" , value);
}
void print(unsigned long value, int nbase = BYTE) {
if (nbase == BIN) print_bin(value,32);
if (nbase == BIN) print_bin(value, 32);
else if (nbase == OCT) printf("%11o", value);
else if (nbase == HEX) printf("%8X", value);
else printf("%lu" , value);
}
void print(float value, int round = 6) {
printf("%f" , value);
}
void print(double value, int round = 6) {
printf("%f" , value );
}
void print(float value, int round = 6) { printf("%f" , value); }
void print(double value, int round = 6) { printf("%f" , value); }
void println(const char value[]) {
printf("%s\n" , value);
}
void println(char value, int nbase = BYTE) {
print(value, nbase);
println();
}
void println(unsigned char value, int nbase = BYTE) {
print(value, nbase);
println();
}
void println(int value, int nbase = BYTE) {
print(value, nbase);
println();
}
void println(unsigned int value, int nbase = BYTE) {
print(value, nbase);
println();
}
void println(long value, int nbase = BYTE) {
print(value, nbase);
println();
}
void println(unsigned long value, int nbase = BYTE) {
print(value, nbase);
println();
}
void println(float value, int round = 6) {
printf("%f\n" , value );
}
void println(double value, int round = 6) {
printf("%f\n" , value );
}
void println(void) {
print('\n');
}
void println(const char value[]) { printf("%s\n" , value); }
void println(char value, int nbase = BYTE) { print(value, nbase); println(); }
void println(unsigned char value, int nbase = BYTE) { print(value, nbase); println(); }
void println(int value, int nbase = BYTE) { print(value, nbase); println(); }
void println(unsigned int value, int nbase = BYTE) { print(value, nbase); println(); }
void println(long value, int nbase = BYTE) { print(value, nbase); println(); }
void println(unsigned long value, int nbase = BYTE) { print(value, nbase); println(); }
void println(float value, int round = 6) { printf("%f\n" , value); }
void println(double value, int round = 6) { printf("%f\n" , value); }
void println(void) { print('\n'); }
volatile RingBuffer<uint8_t, 128> receive_buffer;
volatile RingBuffer<uint8_t, 128> transmit_buffer;
volatile bool host_connected;
};
#endif /* MARLIN_SRC_HAL_HAL_SERIAL_H_ */
#endif // _HAL_SERIAL_H_

View File

@ -65,16 +65,41 @@
// Defines
// --------------------------------------------------------------------------
#if !WITHIN(SERIAL_PORT, -1, 3)
#error "SERIAL_PORT must be from -1 to 3"
#endif
#if SERIAL_PORT == -1
#define MYSERIAL SerialUSB
#define MYSERIAL0 SerialUSB
#elif SERIAL_PORT == 0
#define MYSERIAL Serial
#define MYSERIAL0 Serial
#elif SERIAL_PORT == 1
#define MYSERIAL Serial1
#define MYSERIAL0 Serial1
#elif SERIAL_PORT == 2
#define MYSERIAL Serial2
#define MYSERIAL0 Serial2
#elif SERIAL_PORT == 3
#define MYSERIAL Serial3
#define MYSERIAL0 Serial3
#endif
#ifdef SERIAL_PORT_2
#if !WITHIN(SERIAL_PORT_2, -1, 3)
#error "SERIAL_PORT_2 must be from -1 to 3"
#elif SERIAL_PORT_2 == SERIAL_PORT
#error "SERIAL_PORT_2 must be different than SERIAL_PORT"
#endif
#define NUM_SERIAL 2
#if SERIAL_PORT_2 == -1
#define MYSERIAL1 SerialUSB
#elif SERIAL_PORT_2 == 0
#define MYSERIAL1 Serial
#elif SERIAL_PORT_2 == 1
#define MYSERIAL1 Serial1
#elif SERIAL_PORT_2 == 2
#define MYSERIAL1 Serial2
#elif SERIAL_PORT_2 == 3
#define MYSERIAL1 Serial3
#endif
#else
#define NUM_SERIAL 1
#endif
/**

View File

@ -57,16 +57,18 @@
#define IS_TEENSY35 defined(__MK64FX512__)
#define IS_TEENSY36 defined(__MK66FX1M0__)
#define NUM_SERIAL 1
#if SERIAL_PORT == -1
#define MYSERIAL SerialUSB
#define MYSERIAL0 SerialUSB
#elif SERIAL_PORT == 0
#define MYSERIAL Serial
#define MYSERIAL0 Serial
#elif SERIAL_PORT == 1
#define MYSERIAL Serial1
#define MYSERIAL0 Serial1
#elif SERIAL_PORT == 2
#define MYSERIAL Serial2
#define MYSERIAL0 Serial2
#elif SERIAL_PORT == 3
#define MYSERIAL Serial3
#define MYSERIAL0 Serial3
#endif
#define HAL_SERVO_LIB libServo