Multi-host support
This commit is contained in:
committed by
Scott Lahteine
parent
dabb75034c
commit
f7efac57b7
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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_
|
||||
|
Reference in New Issue
Block a user