Skip to content

Commit

Permalink
Fix STM32F1 emergency parser (MarlinFirmware#21011)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanmels authored and zillarob committed Feb 25, 2021
1 parent 3125743 commit 15ac23f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
29 changes: 28 additions & 1 deletion Marlin/src/HAL/STM32F1/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,32 @@

#if (defined(SERIAL_USB) && !defined(USE_USB_COMPOSITE))
USBSerial SerialUSB;
DefaultSerial MSerial(false, SerialUSB);
DefaultSerial MSerial(true, SerialUSB);

#if ENABLED(EMERGENCY_PARSER)
#include "../libmaple/usb/stm32f1/usb_reg_map.h"
#include "libmaple/usb_cdcacm.h"
// The original callback is not called (no way to retrieve address).
// That callback detects a special STM32 reset sequence: this functionality is not essential
// as M997 achieves the same.
void my_rx_callback(unsigned int, void*) {
// max length of 16 is enough to contain all emergency commands
uint8 buf[16];

//rx is usbSerialPart.endpoints[2]
uint16 len = usb_get_ep_rx_count(USB_CDCACM_RX_ENDP);
uint32 total = usb_cdcacm_data_available();

if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf)))
return;

// cannot get character by character due to bug in composite_cdcacm_peek_ex
len = usb_cdcacm_peek(buf, total);

for (uint32 i = 0; i < len; i++)
emergency_parser.update(MSerial.emergency_state, buf[i + total - len]);
}
#endif
#endif

uint16_t HAL_adc_result;
Expand Down Expand Up @@ -254,6 +279,8 @@ void HAL_init() {
#endif
#ifdef USE_USB_COMPOSITE
MSC_SD_init();
#elif BOTH(SERIAL_USB, EMERGENCY_PARSER)
usb_cdcacm_set_hooks(USB_CDCACM_HOOK_RX, my_rx_callback);
#endif
#if PIN_EXISTS(USB_CONNECT)
OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection
Expand Down
32 changes: 22 additions & 10 deletions Marlin/src/HAL/STM32F1/msc_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "msc_sd.h"
#include "SPI.h"
#include "usb_reg_map.h"

#define PRODUCT_ID 0x29

Expand All @@ -39,14 +40,27 @@ Serial0Type<USBCompositeSerial> MarlinCompositeSerial(true);
#endif

#if ENABLED(EMERGENCY_PARSER)
void (*real_rx_callback)(void);

void my_rx_callback(void) {
real_rx_callback();
int len = MarlinCompositeSerial.available();
while (len-- > 0) // >0 because available() may return a negative value
emergency_parser.update(MarlinCompositeSerial.emergency_state, MarlinCompositeSerial.peek());
}
// The original callback is not called (no way to retrieve address).
// That callback detects a special STM32 reset sequence: this functionality is not essential
// as M997 achieves the same.
void my_rx_callback(unsigned int, void*) {
// max length of 16 is enough to contain all emergency commands
uint8 buf[16];

//rx is usbSerialPart.endpoints[2]
uint16 len = usb_get_ep_rx_count(usbSerialPart.endpoints[2].address);
uint32 total = composite_cdcacm_data_available();

if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf)))
return;

// cannot get character by character due to bug in composite_cdcacm_peek_ex
len = composite_cdcacm_peek(buf, total);

for (uint32 i = 0; i < len; i++)
emergency_parser.update(MarlinCompositeSerial.emergency_state, buf[i+total-len]);
}
#endif

void MSC_SD_init() {
Expand All @@ -71,9 +85,7 @@ void MSC_SD_init() {
MarlinCompositeSerial.registerComponent();
USBComposite.begin();
#if ENABLED(EMERGENCY_PARSER)
//rx is usbSerialPart.endpoints[2]
real_rx_callback = usbSerialPart.endpoints[2].callback;
usbSerialPart.endpoints[2].callback = my_rx_callback;
composite_cdcacm_set_hooks(USBHID_CDCACM_HOOK_RX, my_rx_callback);
#endif
}

Expand Down

0 comments on commit 15ac23f

Please sign in to comment.