Skip to content

Commit

Permalink
EMERGENCY_PARSER support (MarlinFirmware#3)
Browse files Browse the repository at this point in the history
* provide hooks for usart rx and tx

* implement and enable emergency parser feature
  • Loading branch information
shadow578 authored Feb 17, 2023
1 parent 545456e commit 3a9cc22
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@
* Currently handles M108, M112, M410, M876
* NOTE: Not yet implemented for all platforms.
*/
//#define EMERGENCY_PARSER
#define EMERGENCY_PARSER

/**
* Realtime Reporting (requires EMERGENCY_PARSER)
Expand Down
41 changes: 39 additions & 2 deletions Marlin/lib/h32_core/framework/cores/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,46 @@ extern "C"
rb_reset(dev->wb);
}

/**
* USART transmit hook
*
* @param ch the data byte to be transmitted
* @param usart the usart channel. ([1,2,3,4]; 1 = DWIN, 2 = PRINT)
*/
__weak extern void usart_tx_irq_hook(uint8_t ch, uint8_t usart);

/**
* USART receive hook
*
* @param ch the data byte that was received
* @param usart the usart channel. ([1,2,3,4]; 1 = DWIN, 2 = PRINT)
*/
__weak extern void usart_rx_irq_hook(uint8_t ch, uint8_t usart);

/**
* map usart device registers to usart channel number
*/
static inline uint8_t usart_dev_to_channel(M4_USART_TypeDef *dev_regs)
{
if (dev_regs == M4_USART1)
return 1;
if (dev_regs == M4_USART2)
return 2;
if (dev_regs == M4_USART3)
return 3;
if (dev_regs == M4_USART4)
return 4;

return -1;
}

static inline void usart_tx_irq(ring_buffer *wb, M4_USART_TypeDef *regs)
{
if (!rb_is_empty(wb))
{
USART_SendData(regs, rb_remove(wb));
uint8_t ch = rb_remove(wb);
usart_tx_irq_hook(ch, usart_dev_to_channel(regs));
USART_SendData(regs, ch);
}
else
{
Expand All @@ -182,7 +217,9 @@ extern "C"

static inline void usart_rx_irq(ring_buffer *rb, M4_USART_TypeDef *regs)
{
rb_push_insert(rb, (uint8)USART_RecData(regs));
uint8_t ch = (uint8)USART_RecData(regs);
usart_rx_irq_hook(ch, usart_dev_to_channel(regs));
rb_push_insert(rb, ch);
}

#ifdef __cplusplus
Expand Down
18 changes: 18 additions & 0 deletions Marlin/src/HAL/HC32F46x/HAL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "HAL.h"

//
// Emergency Parser
//

#if ENABLED(EMERGENCY_PARSER)
void usart_rx_irq_hook(uint8_t ch, uint8_t usart)
{
// only handle receive on USART channel 2 (PRINT/HOST serial)
if (usart != 2)
return;

// submit character to emergency parser
if (MYSERIAL1.emergency_parser_enabled())
emergency_parser.update(MYSERIAL1.emergency_state, ch);
}
#endif
8 changes: 8 additions & 0 deletions Marlin/src/HAL/HC32F46x/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
#error "SERIAL_PORT_2 must be different than SERIAL_PORT"
#endif

//
// Emergency Parser
//

#if ENABLED(EMERGENCY_PARSER)
void usart_rx_irq_hook(uint8_t ch, uint8_t usart);
#endif

//
// Misc. Defines
//
Expand Down
12 changes: 3 additions & 9 deletions Marlin/src/HAL/HC32F46x/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
* Test HC32F46x-specific configuration values for errors at compile-time.
*/

#if ENABLED(EMERGENCY_PARSER)
#error "EMERGENCY_PARSER is not yet implemented for HC32F46x. 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 Expand Up @@ -56,9 +52,7 @@
#error "NEOPIXEL_LED (Adafruit NeoPixel) is not supported for HC32F46x. Comment out this line to proceed at your own risk!"
#endif

// Emergency Parser needs at least one serial with HardwareSerial or USBComposite.
// The USBSerial maple don't allow any hook to implement EMERGENCY_PARSER.
// And copy all USBSerial code to marlin space to support EMERGENCY_PARSER, when we have another options, don't worth it.
#if ENABLED(EMERGENCY_PARSER) && !defined(USE_USB_COMPOSITE) && ((SERIAL_PORT == -1 && !defined(SERIAL_PORT_2)) || (SERIAL_PORT_2 == -1 && !defined(SERIAL_PORT)))
#error "EMERGENCY_PARSER is only supported by HardwareSerial or USBComposite in HC32F46x."
// Emergency Parser needs at least one serial with HardwareSerial.
#if ENABLED(EMERGENCY_PARSER) && ((SERIAL_PORT == -1 && !defined(SERIAL_PORT_2)) || (SERIAL_PORT_2 == -1 && !defined(SERIAL_PORT)))
#error "EMERGENCY_PARSER is only supported by HardwareSerial on HC32F46x."
#endif

0 comments on commit 3a9cc22

Please sign in to comment.