Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Emergency Parser support for STM32 #18095

Merged
merged 18 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Marlin/src/HAL/STM32/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)

#include "HAL.h"
#include "usb_serial.h"

#include "../../inc/MarlinConfig.h"
#include "../shared/Delay.h"
Expand Down Expand Up @@ -79,6 +80,8 @@ void HAL_init() {
#endif

SetSoftwareSerialTimerInterruptPriority();

TERN_(EMERGENCY_PARSER, USB_Hook_init());
}

void HAL_clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }
Expand Down
37 changes: 19 additions & 18 deletions Marlin/src/HAL/STM32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../shared/HAL_SPI.h"
#include "fastio.h"
#include "watchdog.h"
#include "MarlinSerial.h"

#include "../../inc/MarlinConfigPre.h"

Expand All @@ -48,17 +49,17 @@
#elif SERIAL_PORT == -1
#define MYSERIAL0 SerialUSB
#elif SERIAL_PORT == 1
#define MYSERIAL0 Serial1
#define MYSERIAL0 MSerial1
#elif SERIAL_PORT == 2
#define MYSERIAL0 Serial2
#define MYSERIAL0 MSerial2
#elif SERIAL_PORT == 3
#define MYSERIAL0 Serial3
#define MYSERIAL0 MSerial3
#elif SERIAL_PORT == 4
#define MYSERIAL0 Serial4
#define MYSERIAL0 MSerial4
#elif SERIAL_PORT == 5
#define MYSERIAL0 Serial5
#define MYSERIAL0 MSerial5
#elif SERIAL_PORT == 6
#define MYSERIAL0 Serial6
#define MYSERIAL0 MSerial6
#else
#error "SERIAL_PORT must be from -1 to 6. Please update your configuration."
#endif
Expand All @@ -72,17 +73,17 @@
#elif SERIAL_PORT_2 == -1
#define MYSERIAL1 SerialUSB
#elif SERIAL_PORT_2 == 1
#define MYSERIAL1 Serial1
#define MYSERIAL1 MSerial1
#elif SERIAL_PORT_2 == 2
#define MYSERIAL1 Serial2
#define MYSERIAL1 MSerial2
#elif SERIAL_PORT_2 == 3
#define MYSERIAL1 Serial3
#define MYSERIAL1 MSerial3
#elif SERIAL_PORT_2 == 4
#define MYSERIAL1 Serial4
#define MYSERIAL1 MSerial4
#elif SERIAL_PORT_2 == 5
#define MYSERIAL1 Serial5
#define MYSERIAL1 MSerial5
#elif SERIAL_PORT_2 == 6
#define MYSERIAL1 Serial6
#define MYSERIAL1 MSerial6
#else
#error "SERIAL_PORT_2 must be from -1 to 6. Please update your configuration."
#endif
Expand All @@ -100,17 +101,17 @@
#elif DGUS_SERIAL_PORT == -1
#define DGUS_SERIAL SerialUSB
#elif DGUS_SERIAL_PORT == 1
#define DGUS_SERIAL Serial1
#define DGUS_SERIAL MSerial1
#elif DGUS_SERIAL_PORT == 2
#define DGUS_SERIAL Serial2
#define DGUS_SERIAL MSerial2
#elif DGUS_SERIAL_PORT == 3
#define DGUS_SERIAL Serial3
#define DGUS_SERIAL MSerial3
#elif DGUS_SERIAL_PORT == 4
#define DGUS_SERIAL Serial4
#define DGUS_SERIAL MSerial4
#elif DGUS_SERIAL_PORT == 5
#define DGUS_SERIAL Serial5
#define DGUS_SERIAL MSerial5
#elif DGUS_SERIAL_PORT == 6
#define DGUS_SERIAL Serial6
#define DGUS_SERIAL MSerial6
#else
#error "DGUS_SERIAL_PORT must be from -1 to 6. Please update your configuration."
#endif
Expand Down
80 changes: 80 additions & 0 deletions Marlin/src/HAL/STM32/MarlinSerial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* 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/>.
*
*/

#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)

#include "../../inc/MarlinConfig.h"
#include "MarlinSerial.h"

#if ENABLED(EMERGENCY_PARSER)
#include "../../feature/e_parser.h"
#endif

#define DECLARE_SERIAL_PORT(ser_num) \
void _rx_complete_irq_ ## ser_num (serial_t * obj); \
MarlinSerial MSerial ## ser_num (USART ## ser_num, &_rx_complete_irq_ ## ser_num); \
void _rx_complete_irq_ ## ser_num (serial_t * obj) { MSerial ## ser_num ._rx_complete_irq(obj); }

#define DECLARE_SERIAL_PORT_EXP(ser_num) DECLARE_SERIAL_PORT(ser_num)

#if defined(SERIAL_PORT) && SERIAL_PORT >= 0
DECLARE_SERIAL_PORT_EXP(SERIAL_PORT)
#endif

#if defined(SERIAL_PORT_2) && SERIAL_PORT_2 >= 0
DECLARE_SERIAL_PORT_EXP(SERIAL_PORT_2)
#endif

#if defined(DGUS_SERIAL_PORT) && DGUS_SERIAL_PORT >= 0
DECLARE_SERIAL_PORT_EXP(DGUS_SERIAL_PORT)
#endif

void MarlinSerial::begin(unsigned long baud, uint8_t config) {
HardwareSerial::begin(baud, config);
// replace the IRQ callback with the one we have defined
#if ENABLED(EMERGENCY_PARSER)
_serial.rx_callback = _rx_callback;
#endif
}

// This function is Copyright (c) 2006 Nicholas Zambetti.
void MarlinSerial::_rx_complete_irq(serial_t *obj) {
// No Parity error, read byte and store it in the buffer if there is room
unsigned char c;

if (uart_getc(obj, &c) == 0) {

rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != obj->rx_tail) {
obj->rx_buff[obj->rx_head] = c;
obj->rx_head = i;
}

#if ENABLED(EMERGENCY_PARSER)
emergency_parser.update(emergency_state, c);
#endif
}
}

#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC
54 changes: 54 additions & 0 deletions Marlin/src/HAL/STM32/MarlinSerial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* 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 "../../inc/MarlinConfigPre.h"

#if ENABLED(EMERGENCY_PARSER)
#include "../../feature/e_parser.h"
#endif

typedef void (*usart_rx_callback_t)(serial_t * obj);

class MarlinSerial : public HardwareSerial {
public:
MarlinSerial(void* peripheral, usart_rx_callback_t rx_callback) :
HardwareSerial(peripheral), _rx_callback(rx_callback)
#if ENABLED(EMERGENCY_PARSER)
, emergency_state(EmergencyParser::State::EP_RESET)
#endif
{ }

void begin(unsigned long baud, uint8_t config);
inline void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }

void _rx_complete_irq(serial_t* obj);

protected:
usart_rx_callback_t _rx_callback;
#if ENABLED(EMERGENCY_PARSER)
EmergencyParser::State emergency_state;
#endif
};

extern MarlinSerial MSerial1;
extern MarlinSerial MSerial2;
extern MarlinSerial MSerial3;
extern MarlinSerial MSerial4;
extern MarlinSerial MSerial5;
4 changes: 0 additions & 4 deletions Marlin/src/HAL/STM32/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
// #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
//#endif

#if ENABLED(EMERGENCY_PARSER)
#error "EMERGENCY_PARSER is not yet implemented for STM32. Disable EMERGENCY_PARSER to continue."
#endif

#if ENABLED(FAST_PWM_FAN)
#error "FAST_PWM_FAN is not yet implemented for this platform."
#endif
Expand Down
55 changes: 55 additions & 0 deletions Marlin/src/HAL/STM32/usb_serial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* 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/>.
*
*/

#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)

#include "../../inc/MarlinConfigPre.h"

#if ENABLED(EMERGENCY_PARSER)

#include "usb_serial.h"
#include "../../feature/e_parser.h"

EmergencyParser::State emergency_state = EmergencyParser::State::EP_RESET;

int8_t (*USBD_CDC_Receive_original) (uint8_t *Buf, uint32_t *Len) = nullptr;

static int8_t USBD_CDC_Receive_hook(uint8_t *Buf, uint32_t *Len) {
for (uint32_t i = 0; i < *Len; i++)
emergency_parser.update(emergency_state, Buf[i]);
return USBD_CDC_Receive_original(Buf, Len);
}

typedef struct _USBD_CDC_Itf {
int8_t (* Init)(void);
int8_t (* DeInit)(void);
int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length);
int8_t (* Receive)(uint8_t *Buf, uint32_t *Len);
int8_t (* Transferred)(void);
} USBD_CDC_ItfTypeDef;

extern USBD_CDC_ItfTypeDef USBD_CDC_fops;

void USB_Hook_init() {
USBD_CDC_Receive_original = USBD_CDC_fops.Receive;
USBD_CDC_fops.Receive = USBD_CDC_Receive_hook;
}

#endif // EMERGENCY_PARSER
#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC
21 changes: 21 additions & 0 deletions Marlin/src/HAL/STM32/usb_serial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* 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

void USB_Hook_init();