2017-06-17 23:19:42 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
#ifndef _HAL_SERIAL_H_
|
|
|
|
#define _HAL_SERIAL_H_
|
2017-06-17 23:19:42 +02:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <debug_frmwrk.h>
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
/**
|
2017-06-17 23:19:42 +02:00
|
|
|
* Generic RingBuffer
|
|
|
|
* T type of the buffer array
|
|
|
|
* S size of the buffer (must be power of 2)
|
|
|
|
*
|
|
|
|
* todo: optimise
|
|
|
|
*/
|
|
|
|
template <typename T, uint32_t S> class RingBuffer {
|
|
|
|
public:
|
2017-11-05 15:49:38 +01:00
|
|
|
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; }
|
2017-06-17 23:19:42 +02:00
|
|
|
bool peek(T *value) volatile {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (value == 0 || available() == 0)
|
2017-06-17 23:19:42 +02:00
|
|
|
return false;
|
|
|
|
*value = buffer[buffer_mask & index_read];
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-05 15:49:38 +01:00
|
|
|
int read() volatile {
|
|
|
|
if ((buffer_mask & index_read) == (buffer_mask & index_write)) return -1;
|
2017-10-06 00:19:43 +02:00
|
|
|
T val = buffer[buffer_mask & index_read];
|
|
|
|
++index_read;
|
|
|
|
return val;
|
2017-06-17 23:19:42 +02:00
|
|
|
}
|
2017-11-05 15:49:38 +01:00
|
|
|
bool write(T value) volatile {
|
2017-06-17 23:19:42 +02:00
|
|
|
uint32_t next_head = buffer_mask & (index_write + 1);
|
2017-11-05 15:49:38 +01:00
|
|
|
if (next_head != index_read) {
|
2017-06-17 23:19:42 +02:00
|
|
|
buffer[buffer_mask & index_write] = value;
|
|
|
|
index_write = next_head;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HalSerial {
|
|
|
|
public:
|
2017-11-05 15:49:38 +01:00
|
|
|
HalSerial() { host_connected = false; }
|
2017-06-17 23:19:42 +02:00
|
|
|
|
|
|
|
void begin(int32_t baud) {
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
int peek() {
|
|
|
|
uint8_t value;
|
|
|
|
return receive_buffer.peek(&value) ? value : -1;
|
2017-06-17 23:19:42 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
int read() { return receive_buffer.read(); }
|
2017-06-17 23:19:42 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
size_t write(char c) { return host_connected ? transmit_buffer.write((uint8_t)c) : 0; }
|
|
|
|
|
|
|
|
operator bool() { return host_connected; }
|
2017-06-17 23:19:42 +02:00
|
|
|
|
|
|
|
uint16_t available() {
|
|
|
|
return (uint16_t)receive_buffer.available();
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
void flush() { receive_buffer.clear(); }
|
2017-10-04 22:40:54 +02:00
|
|
|
|
|
|
|
uint8_t availableForWrite(void){
|
|
|
|
return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void flushTX(void){
|
2017-11-05 15:49:38 +01:00
|
|
|
if (host_connected)
|
|
|
|
while (transmit_buffer.available()) { /* nada */ }
|
2017-06-17 23:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void printf(const char *format, ...) {
|
|
|
|
static char buffer[256];
|
|
|
|
va_list vArgs;
|
|
|
|
va_start(vArgs, format);
|
|
|
|
int length = vsnprintf((char *) buffer, 256, (char const *) format, vArgs);
|
|
|
|
va_end(vArgs);
|
|
|
|
if (length > 0 && length < 256) {
|
|
|
|
if (host_connected) {
|
|
|
|
for (int i = 0; i < length;) {
|
|
|
|
if (transmit_buffer.write(buffer[i])) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:15:33 +01:00
|
|
|
#define DEC 10
|
|
|
|
#define HEX 16
|
|
|
|
#define OCT 8
|
|
|
|
#define BIN 2
|
|
|
|
|
|
|
|
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++) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (!(i % 4) && i) write(' ');
|
|
|
|
if (!(i % 16) && i) write(' ');
|
|
|
|
if (value & mask) write('1');
|
|
|
|
else write('0');
|
2018-01-10 05:15:33 +01:00
|
|
|
value <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
void print(const char value[]) { printf("%s" , value); }
|
2018-01-10 05:15:33 +01:00
|
|
|
void print(char value, int nbase = BYTE) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (nbase == BIN) print_bin(value, 8);
|
2018-01-10 05:15:33 +01:00
|
|
|
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) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (nbase == BIN) print_bin(value, 8);
|
2018-01-10 05:15:33 +01:00
|
|
|
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) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (nbase == BIN) print_bin(value, 16);
|
2018-01-10 05:15:33 +01:00
|
|
|
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) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (nbase == BIN) print_bin(value, 16);
|
2018-01-10 05:15:33 +01:00
|
|
|
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) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (nbase == BIN) print_bin(value, 32);
|
2018-01-10 05:15:33 +01:00
|
|
|
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) {
|
2017-11-05 15:49:38 +01:00
|
|
|
if (nbase == BIN) print_bin(value, 32);
|
2018-01-10 05:15:33 +01:00
|
|
|
else if (nbase == OCT) printf("%11o", value);
|
|
|
|
else if (nbase == HEX) printf("%8X", value);
|
|
|
|
else printf("%lu" , value);
|
2017-06-17 23:19:42 +02:00
|
|
|
}
|
2017-11-05 15:49:38 +01:00
|
|
|
void print(float value, int round = 6) { printf("%f" , value); }
|
|
|
|
void print(double value, int round = 6) { printf("%f" , value); }
|
2017-06-17 23:19:42 +02:00
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
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'); }
|
2018-01-10 05:15:33 +01:00
|
|
|
|
2017-06-17 23:19:42 +02:00
|
|
|
volatile RingBuffer<uint8_t, 128> receive_buffer;
|
|
|
|
volatile RingBuffer<uint8_t, 128> transmit_buffer;
|
|
|
|
volatile bool host_connected;
|
|
|
|
};
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
#endif // _HAL_SERIAL_H_
|