Preliminary cleanup of #include structure (#9763)
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "../pinmapping.h"
|
||||
#include <pinmapping.h>
|
||||
|
||||
#define HIGH 0x01
|
||||
#define LOW 0x00
|
||||
|
46
Marlin/src/HAL/HAL_LPC1768/include/SPI.h
Normal file
46
Marlin/src/HAL/HAL_LPC1768/include/SPI.h
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../../HAL_SPI.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MSBFIRST 1
|
||||
#define SPI_MODE3 0
|
||||
|
||||
class SPISettings {
|
||||
public:
|
||||
SPISettings(int a, int b, int c) {};
|
||||
};
|
||||
|
||||
class SPIClass {
|
||||
public:
|
||||
void begin();
|
||||
void beginTransaction(SPISettings foo) {};
|
||||
void endTransaction() {};
|
||||
uint8_t transfer(uint8_t data);
|
||||
uint16_t transfer16(uint16_t data);
|
||||
};
|
||||
|
||||
extern SPIClass SPI;
|
219
Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp
Normal file
219
Marlin/src/HAL/HAL_LPC1768/include/Wire.cpp
Normal file
@ -0,0 +1,219 @@
|
||||
/*
|
||||
TwoWire.cpp - TWI/I2C library for Wiring & Arduino
|
||||
Copyright (c) 2006 Nicholas Zambetti. 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
|
||||
*/
|
||||
|
||||
#ifdef TARGET_LPC1768
|
||||
|
||||
extern "C" {
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <lpc17xx_i2c.h>
|
||||
#include <lpc17xx_pinsel.h>
|
||||
#include <lpc17xx_libcfg_default.h>
|
||||
}
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#define USEDI2CDEV_M 1
|
||||
|
||||
#if (USEDI2CDEV_M == 0)
|
||||
#define I2CDEV_M LPC_I2C0
|
||||
#elif (USEDI2CDEV_M == 1)
|
||||
#define I2CDEV_M LPC_I2C1
|
||||
#elif (USEDI2CDEV_M == 2)
|
||||
#define I2CDEV_M LPC_I2C2
|
||||
#else
|
||||
#error "Master I2C device not defined!"
|
||||
#endif
|
||||
|
||||
// Initialize Class Variables //////////////////////////////////////////////////
|
||||
|
||||
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
|
||||
uint8_t TwoWire::rxBufferIndex = 0;
|
||||
uint8_t TwoWire::rxBufferLength = 0;
|
||||
|
||||
uint8_t TwoWire::txAddress = 0;
|
||||
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
|
||||
uint8_t TwoWire::txBufferIndex = 0;
|
||||
uint8_t TwoWire::txBufferLength = 0;
|
||||
|
||||
uint8_t TwoWire::transmitting = 0;
|
||||
|
||||
// Constructors ////////////////////////////////////////////////////////////////
|
||||
|
||||
TwoWire::TwoWire() {
|
||||
}
|
||||
|
||||
// Public Methods //////////////////////////////////////////////////////////////
|
||||
|
||||
void TwoWire::begin(void) {
|
||||
rxBufferIndex = 0;
|
||||
rxBufferLength = 0;
|
||||
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
|
||||
/*
|
||||
* Init I2C pin connect
|
||||
*/
|
||||
PINSEL_CFG_Type PinCfg;
|
||||
PinCfg.OpenDrain = 0;
|
||||
PinCfg.Pinmode = 0;
|
||||
|
||||
#if USEDI2CDEV_M == 0
|
||||
PinCfg.Funcnum = 1;
|
||||
PinCfg.Pinnum = 27;
|
||||
PinCfg.Portnum = 0;
|
||||
PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1
|
||||
PinCfg.Pinnum = 28;
|
||||
PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1
|
||||
#endif
|
||||
|
||||
#if USEDI2CDEV_M == 1
|
||||
PinCfg.Funcnum = 3;
|
||||
PinCfg.Pinnum = 0;
|
||||
PinCfg.Portnum = 0;
|
||||
PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA
|
||||
PinCfg.Pinnum = 1;
|
||||
PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL
|
||||
#endif
|
||||
|
||||
#if USEDI2CDEV_M == 2
|
||||
PinCfg.Funcnum = 2;
|
||||
PinCfg.Pinnum = 10;
|
||||
PinCfg.Portnum = 0;
|
||||
PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN
|
||||
PinCfg.Pinnum = 11;
|
||||
PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN
|
||||
#endif
|
||||
|
||||
// Initialize I2C peripheral
|
||||
I2C_Init(I2CDEV_M, 100000);
|
||||
|
||||
// Enable Master I2C operation
|
||||
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
|
||||
}
|
||||
|
||||
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
|
||||
// clamp to buffer length
|
||||
if (quantity > BUFFER_LENGTH)
|
||||
quantity = BUFFER_LENGTH;
|
||||
|
||||
// perform blocking read into buffer
|
||||
I2C_M_SETUP_Type transferMCfg;
|
||||
transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
|
||||
transferMCfg.tx_data = NULL;
|
||||
transferMCfg.tx_length = 0;
|
||||
transferMCfg.rx_data = rxBuffer;
|
||||
transferMCfg.rx_length = quantity;
|
||||
transferMCfg.retransmissions_max = 3;
|
||||
I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
|
||||
|
||||
// set rx buffer iterator vars
|
||||
rxBufferIndex = 0;
|
||||
rxBufferLength = transferMCfg.rx_count;
|
||||
|
||||
return transferMCfg.rx_count;
|
||||
}
|
||||
|
||||
uint8_t TwoWire::requestFrom(int address, int quantity) {
|
||||
return requestFrom((uint8_t)address, (uint8_t)quantity);
|
||||
}
|
||||
|
||||
void TwoWire::beginTransmission(uint8_t address) {
|
||||
// indicate that we are transmitting
|
||||
transmitting = 1;
|
||||
// set address of targeted slave
|
||||
txAddress = address;
|
||||
// reset tx buffer iterator vars
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
}
|
||||
|
||||
void TwoWire::beginTransmission(int address) {
|
||||
beginTransmission((uint8_t)address);
|
||||
}
|
||||
|
||||
uint8_t TwoWire::endTransmission(void) {
|
||||
// transmit buffer (blocking)
|
||||
I2C_M_SETUP_Type transferMCfg;
|
||||
transferMCfg.sl_addr7bit = txAddress >> 1; // not sure about the right shift
|
||||
transferMCfg.tx_data = txBuffer;
|
||||
transferMCfg.tx_length = txBufferLength;
|
||||
transferMCfg.rx_data = NULL;
|
||||
transferMCfg.rx_length = 0;
|
||||
transferMCfg.retransmissions_max = 3;
|
||||
Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
|
||||
|
||||
// reset tx buffer iterator vars
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
|
||||
// indicate that we are done transmitting
|
||||
transmitting = 0;
|
||||
|
||||
return status == SUCCESS ? 0 : 4;
|
||||
}
|
||||
|
||||
// must be called after beginTransmission(address)
|
||||
size_t TwoWire::write(uint8_t data) {
|
||||
if (transmitting) {
|
||||
// don't bother if buffer is full
|
||||
if (txBufferLength >= BUFFER_LENGTH) return 0;
|
||||
|
||||
// put byte in tx buffer
|
||||
txBuffer[txBufferIndex++] = data;
|
||||
|
||||
// update amount in buffer
|
||||
txBufferLength = txBufferIndex;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// must be called after beginTransmission(address)
|
||||
size_t TwoWire::write(const uint8_t *data, size_t quantity) {
|
||||
size_t sent = 0;
|
||||
if (transmitting)
|
||||
for (sent = 0; sent < quantity; ++sent)
|
||||
if (!write(data[sent])) break;
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
// Must be called after requestFrom(address, numBytes)
|
||||
int TwoWire::available(void) {
|
||||
return rxBufferLength - rxBufferIndex;
|
||||
}
|
||||
|
||||
// Must be called after requestFrom(address, numBytes)
|
||||
int TwoWire::read(void) {
|
||||
return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1;
|
||||
}
|
||||
|
||||
// Must be called after requestFrom(address, numBytes)
|
||||
int TwoWire::peek(void) {
|
||||
return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex] : -1;
|
||||
}
|
||||
|
||||
// Preinstantiate Objects //////////////////////////////////////////////////////
|
||||
|
||||
TwoWire Wire = TwoWire();
|
||||
|
||||
#endif // TARGET_LPC1768
|
@ -1,162 +0,0 @@
|
||||
/**
|
||||
* Marlin 3D Printer Firmware
|
||||
* Copyright (C) 2016, 2017 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// 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
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <lpc17xx_i2c.h>
|
||||
#include <lpc17xx_pinsel.h>
|
||||
#include <lpc17xx_libcfg_default.h>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// 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.
|
||||
|
||||
static 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);
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#define USEDI2CDEV_M 1 // use I2C1 controller
|
||||
|
||||
#if (USEDI2CDEV_M == 0)
|
||||
#define I2CDEV_M LPC_I2C0
|
||||
#elif (USEDI2CDEV_M == 1)
|
||||
#define I2CDEV_M LPC_I2C1
|
||||
#elif (USEDI2CDEV_M == 2)
|
||||
#define I2CDEV_M LPC_I2C2
|
||||
#else
|
||||
#error "Master I2C device not defined!"
|
||||
#endif
|
||||
|
||||
|
||||
PINSEL_CFG_Type PinCfg;
|
||||
I2C_M_SETUP_Type transferMCfg;
|
||||
|
||||
#define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
void digipot_mcp4451_init(void) {
|
||||
|
||||
/**
|
||||
* Init I2C pin connect
|
||||
*/
|
||||
PinCfg.OpenDrain = 0;
|
||||
PinCfg.Pinmode = 0;
|
||||
#if ((USEDI2CDEV_M == 0))
|
||||
PinCfg.Funcnum = 1;
|
||||
PinCfg.Pinnum = 27;
|
||||
PinCfg.Portnum = 0;
|
||||
PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1
|
||||
PinCfg.Pinnum = 28;
|
||||
PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1
|
||||
#endif
|
||||
#if ((USEDI2CDEV_M == 1))
|
||||
PinCfg.Funcnum = 3;
|
||||
PinCfg.Pinnum = 0;
|
||||
PinCfg.Portnum = 0;
|
||||
PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA
|
||||
PinCfg.Pinnum = 1;
|
||||
PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL
|
||||
#endif
|
||||
#if ((USEDI2CDEV_M == 2))
|
||||
PinCfg.Funcnum = 2;
|
||||
PinCfg.Pinnum = 10;
|
||||
PinCfg.Portnum = 0;
|
||||
PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN
|
||||
PinCfg.Pinnum = 11;
|
||||
PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN
|
||||
#endif
|
||||
// Initialize I2C peripheral
|
||||
I2C_Init(I2CDEV_M, 400000); // hardwired to 400KHz bit rate, 100KHz is the other option
|
||||
|
||||
/* Enable Master I2C operation */
|
||||
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
|
||||
}
|
||||
|
||||
|
||||
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 // TARGET_LPC1768
|
74
Marlin/src/HAL/HAL_LPC1768/include/pinmapping.cpp
Normal file
74
Marlin/src/HAL/HAL_LPC1768/include/pinmapping.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
/**
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef TARGET_LPC1768
|
||||
|
||||
#include <pinmapping.h>
|
||||
|
||||
#include "../../../gcode/parser.h"
|
||||
|
||||
// Get the digital pin for an analog index
|
||||
pin_t analogInputToDigitalPin(const int8_t p) {
|
||||
return (WITHIN(p, 0, NUM_ANALOG_INPUTS) ? adc_pin_table[p] : P_NC);
|
||||
}
|
||||
|
||||
// Return the index of a pin number
|
||||
// The pin number given here is in the form ppp:nnnnn
|
||||
int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
|
||||
const uint16_t index = (LPC1768_PIN_PORT(pin) << 5) | LPC1768_PIN_PIN(pin);
|
||||
return (index < NUM_DIGITAL_PINS && pin_map[index] != P_NC) ? index : -1;
|
||||
}
|
||||
|
||||
// Test whether the pin is valid
|
||||
bool VALID_PIN(const pin_t p) {
|
||||
const int16_t ind = GET_PIN_MAP_INDEX(p);
|
||||
return ind >= 0 && pin_map[ind] >= 0;
|
||||
}
|
||||
|
||||
// Get the analog index for a digital pin
|
||||
int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) {
|
||||
return (VALID_PIN(p) ? LPC1768_PIN_ADC(p) : -1);
|
||||
}
|
||||
|
||||
// Test whether the pin is PWM
|
||||
bool PWM_PIN(const pin_t p) {
|
||||
return VALID_PIN(p) && LPC1768_PIN_PWM(p);
|
||||
}
|
||||
|
||||
// Test whether the pin is interruptable
|
||||
bool INTERRUPT_PIN(const pin_t p) {
|
||||
return VALID_PIN(p) && LPC1768_PIN_INTERRUPT(p);
|
||||
}
|
||||
|
||||
// Get the pin number at the given index
|
||||
pin_t GET_PIN_MAP_PIN(const int16_t ind) {
|
||||
return WITHIN(ind, 0, NUM_DIGITAL_PINS - 1) ? pin_map[ind] : P_NC;
|
||||
}
|
||||
|
||||
int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
|
||||
const uint16_t val = (uint16_t)parser.intval(code), port = val / 100, pin = val % 100;
|
||||
const int16_t ind = (port < (NUM_DIGITAL_PINS >> 5) && (pin < 32))
|
||||
? GET_PIN_MAP_INDEX(port << 5 | pin) : -2;
|
||||
return ind > -2 ? ind : dval;
|
||||
}
|
||||
|
||||
#endif // TARGET_LPC1768
|
293
Marlin/src/HAL/HAL_LPC1768/include/pinmapping.h
Normal file
293
Marlin/src/HAL/HAL_LPC1768/include/pinmapping.h
Normal file
@ -0,0 +1,293 @@
|
||||
/**
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _PINMAPPING_H_
|
||||
#define _PINMAPPING_H_
|
||||
|
||||
#include "../../../inc/MarlinConfigPre.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int16_t pin_t;
|
||||
|
||||
#define PORT_0 000
|
||||
#define PORT_1 001
|
||||
#define PORT_2 010
|
||||
#define PORT_3 011
|
||||
#define PORT_4 100
|
||||
|
||||
#define PORT_(p) PORT_##p
|
||||
#define PORT(p) PORT_(p)
|
||||
|
||||
#define PIN_0 00000
|
||||
#define PIN_1 00001
|
||||
#define PIN_2 00010
|
||||
#define PIN_3 00011
|
||||
#define PIN_4 00100
|
||||
#define PIN_5 00101
|
||||
#define PIN_6 00110
|
||||
#define PIN_7 00111
|
||||
#define PIN_8 01000
|
||||
#define PIN_9 01001
|
||||
#define PIN_10 01010
|
||||
#define PIN_11 01011
|
||||
#define PIN_12 01100
|
||||
#define PIN_13 01101
|
||||
#define PIN_14 01110
|
||||
#define PIN_15 01111
|
||||
#define PIN_16 10000
|
||||
#define PIN_17 10001
|
||||
#define PIN_18 10010
|
||||
#define PIN_19 10011
|
||||
#define PIN_20 10100
|
||||
#define PIN_21 10101
|
||||
#define PIN_22 10110
|
||||
#define PIN_23 10111
|
||||
#define PIN_24 11000
|
||||
#define PIN_25 11001
|
||||
#define PIN_26 11010
|
||||
#define PIN_27 11011
|
||||
#define PIN_28 11100
|
||||
#define PIN_29 11101
|
||||
#define PIN_30 11110
|
||||
#define PIN_31 11111
|
||||
|
||||
#define PIN_(p) PIN_##p
|
||||
#define PIN(p) PIN_(p)
|
||||
|
||||
#define ADC_NONE 0000
|
||||
#define ADC_CHAN_0 0001
|
||||
#define ADC_CHAN_1 0010
|
||||
#define ADC_CHAN_2 0011
|
||||
#define ADC_CHAN_3 0100
|
||||
#define ADC_CHAN_4 0101
|
||||
#define ADC_CHAN_5 0110
|
||||
#define ADC_CHAN_6 0111
|
||||
#define ADC_CHAN_7 1000
|
||||
|
||||
#define ADC_CHAN_(c) ADC_CHAN_##c
|
||||
#define ADC_CHAN(p) ADC_CHAN_(p)
|
||||
|
||||
#define BOOL_0 0
|
||||
#define BOOL_1 1
|
||||
#define BOOL_(b) BOOL_##b
|
||||
|
||||
#define INTERRUPT(b) BOOL_(b)
|
||||
#define PWM(b) BOOL_(b)
|
||||
|
||||
// Combine elements into pin bits: 0b00AAAAWIPPPNNNNN
|
||||
#define LPC1768_PIN_(port, pin, int, pwm, adc) 0b00##adc##pwm##int##port##pin
|
||||
#define LPC1768_PIN(port, pin, int, pwm, adc) LPC1768_PIN_(port, pin, int, pwm, adc)
|
||||
|
||||
constexpr uint8_t LPC1768_PIN_PORT(const pin_t pin) { return ((uint8_t)((pin >> 5) & 0b111)); }
|
||||
constexpr uint8_t LPC1768_PIN_PIN(const pin_t pin) { return ((uint8_t)(pin & 0b11111)); }
|
||||
constexpr bool LPC1768_PIN_INTERRUPT(const pin_t pin) { return (((pin >> 8) & 0b1) != 0); }
|
||||
constexpr bool LPC1768_PIN_PWM(const pin_t pin) { return (((pin >> 9) & 0b1) != 0); }
|
||||
constexpr int8_t LPC1768_PIN_ADC(const pin_t pin) { return (int8_t)((pin >> 10) & 0b1111) - 1; }
|
||||
|
||||
// ******************
|
||||
// Runtime pinmapping
|
||||
// ******************
|
||||
#define P_NC -1
|
||||
|
||||
#if SERIAL_PORT != 3 && SERIAL_PORT_2 != 3
|
||||
#define P0_00 LPC1768_PIN(PORT(0), PIN( 0), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_01 LPC1768_PIN(PORT(0), PIN( 1), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#endif
|
||||
#if SERIAL_PORT != 0 && SERIAL_PORT_2 != 0
|
||||
#define P0_02 LPC1768_PIN(PORT(0), PIN( 2), INTERRUPT(1), PWM(0), ADC_CHAN(7))
|
||||
#define P0_03 LPC1768_PIN(PORT(0), PIN( 3), INTERRUPT(1), PWM(0), ADC_CHAN(6))
|
||||
#endif
|
||||
#define P0_04 LPC1768_PIN(PORT(0), PIN( 4), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_05 LPC1768_PIN(PORT(0), PIN( 5), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_06 LPC1768_PIN(PORT(0), PIN( 6), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_07 LPC1768_PIN(PORT(0), PIN( 7), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_08 LPC1768_PIN(PORT(0), PIN( 8), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_09 LPC1768_PIN(PORT(0), PIN( 9), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#if SERIAL_PORT != 2 && SERIAL_PORT_2 != 2
|
||||
#define P0_10 LPC1768_PIN(PORT(0), PIN(10), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_11 LPC1768_PIN(PORT(0), PIN(11), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#endif
|
||||
#if SERIAL_PORT != 1 && SERIAL_PORT_2 != 1
|
||||
#define P0_15 LPC1768_PIN(PORT(0), PIN(15), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_16 LPC1768_PIN(PORT(0), PIN(16), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#endif
|
||||
#define P0_17 LPC1768_PIN(PORT(0), PIN(17), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_18 LPC1768_PIN(PORT(0), PIN(18), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_19 LPC1768_PIN(PORT(0), PIN(19), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_20 LPC1768_PIN(PORT(0), PIN(20), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_21 LPC1768_PIN(PORT(0), PIN(21), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_22 LPC1768_PIN(PORT(0), PIN(22), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_23 LPC1768_PIN(PORT(0), PIN(23), INTERRUPT(1), PWM(0), ADC_CHAN(0))
|
||||
#define P0_24 LPC1768_PIN(PORT(0), PIN(24), INTERRUPT(1), PWM(0), ADC_CHAN(1))
|
||||
#define P0_25 LPC1768_PIN(PORT(0), PIN(25), INTERRUPT(1), PWM(0), ADC_CHAN(2))
|
||||
#define P0_26 LPC1768_PIN(PORT(0), PIN(26), INTERRUPT(1), PWM(0), ADC_CHAN(3))
|
||||
#define P0_27 LPC1768_PIN(PORT(0), PIN(27), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_28 LPC1768_PIN(PORT(0), PIN(28), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#if SERIAL_PORT != -1 && SERIAL_PORT_2 != -1
|
||||
#define P0_29 LPC1768_PIN(PORT(0), PIN(29), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P0_30 LPC1768_PIN(PORT(0), PIN(30), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#endif
|
||||
#define P1_00 LPC1768_PIN(PORT(1), PIN( 0), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_01 LPC1768_PIN(PORT(1), PIN( 1), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_04 LPC1768_PIN(PORT(1), PIN( 4), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_08 LPC1768_PIN(PORT(1), PIN( 8), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_09 LPC1768_PIN(PORT(1), PIN( 9), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_10 LPC1768_PIN(PORT(1), PIN(10), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_14 LPC1768_PIN(PORT(1), PIN(14), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_15 LPC1768_PIN(PORT(1), PIN(15), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_16 LPC1768_PIN(PORT(1), PIN(16), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_17 LPC1768_PIN(PORT(1), PIN(17), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_18 LPC1768_PIN(PORT(1), PIN(18), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P1_19 LPC1768_PIN(PORT(1), PIN(19), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_20 LPC1768_PIN(PORT(1), PIN(20), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P1_21 LPC1768_PIN(PORT(1), PIN(21), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P1_22 LPC1768_PIN(PORT(1), PIN(22), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_23 LPC1768_PIN(PORT(1), PIN(23), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P1_24 LPC1768_PIN(PORT(1), PIN(24), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P1_25 LPC1768_PIN(PORT(1), PIN(25), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_26 LPC1768_PIN(PORT(1), PIN(26), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P1_27 LPC1768_PIN(PORT(1), PIN(27), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_28 LPC1768_PIN(PORT(1), PIN(28), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_29 LPC1768_PIN(PORT(1), PIN(29), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P1_30 LPC1768_PIN(PORT(1), PIN(30), INTERRUPT(0), PWM(0), ADC_CHAN(4))
|
||||
#define P1_31 LPC1768_PIN(PORT(1), PIN(31), INTERRUPT(0), PWM(0), ADC_CHAN(5))
|
||||
#define P2_00 LPC1768_PIN(PORT(2), PIN( 0), INTERRUPT(1), PWM(1), ADC_NONE)
|
||||
#define P2_01 LPC1768_PIN(PORT(2), PIN( 1), INTERRUPT(1), PWM(1), ADC_NONE)
|
||||
#define P2_02 LPC1768_PIN(PORT(2), PIN( 2), INTERRUPT(1), PWM(1), ADC_NONE)
|
||||
#define P2_03 LPC1768_PIN(PORT(2), PIN( 3), INTERRUPT(1), PWM(1), ADC_NONE)
|
||||
#define P2_04 LPC1768_PIN(PORT(2), PIN( 4), INTERRUPT(1), PWM(1), ADC_NONE)
|
||||
#define P2_05 LPC1768_PIN(PORT(2), PIN( 5), INTERRUPT(1), PWM(1), ADC_NONE)
|
||||
#define P2_06 LPC1768_PIN(PORT(2), PIN( 6), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_07 LPC1768_PIN(PORT(2), PIN( 7), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_08 LPC1768_PIN(PORT(2), PIN( 8), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_09 LPC1768_PIN(PORT(2), PIN( 9), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_10 LPC1768_PIN(PORT(2), PIN(10), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_11 LPC1768_PIN(PORT(2), PIN(11), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_12 LPC1768_PIN(PORT(2), PIN(12), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P2_13 LPC1768_PIN(PORT(2), PIN(13), INTERRUPT(1), PWM(0), ADC_NONE)
|
||||
#define P3_25 LPC1768_PIN(PORT(3), PIN(25), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P3_26 LPC1768_PIN(PORT(3), PIN(26), INTERRUPT(0), PWM(1), ADC_NONE)
|
||||
#define P4_28 LPC1768_PIN(PORT(4), PIN(28), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
#define P4_29 LPC1768_PIN(PORT(4), PIN(29), INTERRUPT(0), PWM(0), ADC_NONE)
|
||||
|
||||
// Pin index for M43 and M226
|
||||
constexpr pin_t pin_map[] = {
|
||||
#if SERIAL_PORT != 3 && SERIAL_PORT_2 != 3
|
||||
P0_00, P0_01,
|
||||
#else
|
||||
P_NC, P_NC,
|
||||
#endif
|
||||
#if SERIAL_PORT != 0 && SERIAL_PORT_2 != 0
|
||||
P0_02, P0_03,
|
||||
#else
|
||||
P_NC, P_NC,
|
||||
#endif
|
||||
P0_04, P0_05, P0_06, P0_07,
|
||||
P0_08, P0_09,
|
||||
#if SERIAL_PORT != 2 && SERIAL_PORT_2 != 2
|
||||
P0_10, P0_11,
|
||||
#else
|
||||
P_NC, P_NC,
|
||||
#endif
|
||||
P_NC, P_NC, P_NC,
|
||||
#if SERIAL_PORT != 1 && SERIAL_PORT_2 != 1
|
||||
P0_15,
|
||||
P0_16,
|
||||
#else
|
||||
P_NC,
|
||||
P_NC,
|
||||
#endif
|
||||
P0_17, P0_18, P0_19, P0_20, P0_21, P0_22, P0_23,
|
||||
P0_24, P0_25, P0_26, P0_27, P0_28,
|
||||
#if SERIAL_PORT != -1 && SERIAL_PORT_2 != -1
|
||||
P0_29, P0_30,
|
||||
#else
|
||||
P_NC, P_NC,
|
||||
#endif
|
||||
P_NC,
|
||||
|
||||
P1_00, P1_01, P_NC, P_NC, P1_04, P_NC, P_NC, P_NC,
|
||||
P1_08, P1_09, P1_10, P_NC, P_NC, P_NC, P1_14, P1_15,
|
||||
P1_16, P1_17, P1_18, P1_19, P1_20, P1_21, P1_22, P1_23,
|
||||
P1_24, P1_25, P1_26, P1_27, P1_28, P1_29, P1_30, P1_31,
|
||||
|
||||
P2_00, P2_01, P2_02, P2_03, P2_04, P2_05, P2_06, P2_07,
|
||||
P2_08, P2_09, P2_10, P2_11, P2_12, P2_13, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P3_25, P3_26, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC, P_NC,
|
||||
P_NC, P_NC, P_NC, P_NC, P4_28, P4_29, P_NC, P_NC
|
||||
};
|
||||
|
||||
constexpr uint8_t NUM_DIGITAL_PINS = COUNT(pin_map);
|
||||
|
||||
constexpr pin_t adc_pin_table[] = {
|
||||
P0_23, P0_24, P0_25, P0_26, P1_30, P1_31,
|
||||
#if SERIAL_PORT != 0 && SERIAL_PORT_2 != 0
|
||||
P0_03, P0_02
|
||||
#endif
|
||||
};
|
||||
|
||||
#if SERIAL_PORT != 0 && SERIAL_PORT_2 != 0
|
||||
#define NUM_ANALOG_INPUTS 8
|
||||
#else
|
||||
#define NUM_ANALOG_INPUTS 6
|
||||
#endif
|
||||
|
||||
// P0.6 thru P0.9 are for the onboard SD card
|
||||
#define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09
|
||||
|
||||
// Get the digital pin for an analog index
|
||||
pin_t analogInputToDigitalPin(const int8_t p);
|
||||
|
||||
// Return the index of a pin number
|
||||
// The pin number given here is in the form ppp:nnnnn
|
||||
int16_t GET_PIN_MAP_INDEX(const pin_t pin);
|
||||
|
||||
// Test whether the pin is valid
|
||||
bool VALID_PIN(const pin_t p);
|
||||
|
||||
// Get the analog index for a digital pin
|
||||
int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p);
|
||||
|
||||
// Test whether the pin is PWM
|
||||
bool PWM_PIN(const pin_t p);
|
||||
|
||||
// Test whether the pin is interruptable
|
||||
bool INTERRUPT_PIN(const pin_t p);
|
||||
|
||||
// Get the pin number at the given index
|
||||
pin_t GET_PIN_MAP_PIN(const int16_t ind);
|
||||
|
||||
// Parse a G-code word into a pin index
|
||||
int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
|
||||
|
||||
#endif // _PINMAPPING_H_
|
201
Marlin/src/HAL/HAL_LPC1768/include/serial.h
Normal file
201
Marlin/src/HAL/HAL_LPC1768/include/serial.h
Normal file
@ -0,0 +1,201 @@
|
||||
/**
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _HAL_SERIAL_H_
|
||||
#define _HAL_SERIAL_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern "C" {
|
||||
#include <debug_frmwrk.h>
|
||||
}
|
||||
|
||||
/**
|
||||
* 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:
|
||||
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)
|
||||
return false;
|
||||
*value = buffer[buffer_mask & index_read];
|
||||
return true;
|
||||
}
|
||||
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 {
|
||||
uint32_t next_head = buffer_mask & (index_write + 1);
|
||||
if (next_head != index_read) {
|
||||
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:
|
||||
HalSerial() { host_connected = false; }
|
||||
|
||||
void begin(int32_t baud) {
|
||||
}
|
||||
|
||||
int peek() {
|
||||
uint8_t value;
|
||||
return receive_buffer.peek(&value) ? value : -1;
|
||||
}
|
||||
|
||||
int read() { return receive_buffer.read(); }
|
||||
|
||||
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(); }
|
||||
|
||||
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()) { /* nada */ }
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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++) {
|
||||
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(char value, int nbase = 0) {
|
||||
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 = 0) {
|
||||
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 = 0) {
|
||||
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 = 0) {
|
||||
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 = 0) {
|
||||
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 = 0) {
|
||||
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 println(const char value[]) { printf("%s\n" , value); }
|
||||
void println(char value, int nbase = 0) { print(value, nbase); println(); }
|
||||
void println(unsigned char value, int nbase = 0) { print(value, nbase); println(); }
|
||||
void println(int value, int nbase = 0) { print(value, nbase); println(); }
|
||||
void println(unsigned int value, int nbase = 0) { print(value, nbase); println(); }
|
||||
void println(long value, int nbase = 0) { print(value, nbase); println(); }
|
||||
void println(unsigned long value, int nbase = 0) { 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 // _HAL_SERIAL_H_
|
Reference in New Issue
Block a user