Skip to content

Commit

Permalink
#7 Add example for STM32F4
Browse files Browse the repository at this point in the history
  • Loading branch information
rrusch committed May 14, 2024
1 parent 6c71f34 commit 1178bf6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
57 changes: 57 additions & 0 deletions examples/stm32f4_usb_serial/Application/SerialGcode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SPDX-FileCopyrightText: 2024 Roland Rusch, easy-smart solution GmbH <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-only
*/

#include "SerialGcode.hpp"
#include "AbstractCommand.hpp"
#include "Stm32GcodeRunner.hpp"

void SerialGcode::loop() {
Stm32Common::buf_size_signed_t posR = rxBuffer.findPos('\r');
Stm32Common::buf_size_signed_t posN = rxBuffer.findPos('\n');
const Stm32Common::buf_size_signed_t eolPos = std::max(posR, posN);
if (eolPos > 0) {
Stm32GcodeRunner::AbstractCommand *cmd{};
const size_t cmdLen = std::min(posR < 0 ? SIZE_MAX : posR, posN < 0 ? SIZE_MAX : posN);
auto parserRet = Stm32GcodeRunner::parser->parseString(
cmd, reinterpret_cast<const char *>(rxBuffer.getReadPointer()), cmdLen);
rxBuffer.remove(eolPos + 1);

if (parserRet == Stm32GcodeRunner::Parser::parserReturn::OK) {
Debugger_log(DBG, "Found command: %s", cmd->getName());
Stm32GcodeRunner::CommandContext *cmdCtx{};
Stm32GcodeRunner::worker->createCommandContext(cmdCtx);
if (cmdCtx == nullptr) {
txBuffer.println("ERROR: Command buffer full");
return;
}
cmdCtx->setCommand(cmd);

cmdCtx->registerOnWriteFunction([cmdCtx, this]() {
// Debugger_log(DBG, "onWriteFn()");
if (cmdCtx->outputLength() > 0) {
const auto result = cmdCtx->outputRead(
reinterpret_cast<char *>(this->txBuffer.getWritePointer()),
this->txBuffer.getRemainingSpace());
this->txBuffer.setWrittenBytes(result);
}
});

cmdCtx->registerOnCmdEndFunction([cmdCtx]() {
// Debugger_log(DBG, "onCmdEndFn()");
Stm32GcodeRunner::worker->deleteCommandContext(cmdCtx);
});

Stm32GcodeRunner::worker->enqueueCommandContext(cmdCtx);
} else if (parserRet == Stm32GcodeRunner::Parser::parserReturn::UNKNOWN_COMMAND) {
txBuffer.println("ERROR: UNKNOWN COMMAND");
} else if (parserRet == Stm32GcodeRunner::Parser::parserReturn::GARBAGE_STRING) {
txBuffer.println("ERROR: UNKNOWN COMMAND");
} else {
// txBuffer.println("ERROR: ONLY WHITESPACE");
}
}

Stm32Serial::loop();
}
18 changes: 18 additions & 0 deletions examples/stm32f4_usb_serial/Application/SerialGcode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2024 Roland Rusch, easy-smart solution GmbH <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-only
*/

#ifndef STM32F4_USB_SERIAL_SERIALGCODE_HPP
#define STM32F4_USB_SERIAL_SERIALGCODE_HPP

#include "Stm32Serial.hpp"

class SerialGcode : public Stm32Serial::Stm32Serial {
public:
explicit SerialGcode(::Stm32Serial::AbstractDriver *driver) : Stm32Serial(driver) {}

void loop() override;
};

#endif //STM32F4_USB_SERIAL_SERIALGCODE_HPP
3 changes: 2 additions & 1 deletion examples/stm32f4_usb_serial/Application/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include "usart.h"
#include "Driver/Stm32EmptyDriver.hpp"
#include "Driver/Stm32HalUartItDriver.hpp"
#include "SerialGcode.hpp"

uint32_t dummyCpp;
Stm32ItmLogger::Stm32ItmLogger logger;
Stm32Serial::Stm32HalUartItDriver uart1Driver(&huart1, "uart1Driver");
//Stm32Serial::Stm32EmptyDriver uart1Driver("uart1Driver");
Stm32Serial::Stm32Serial Serial1(&uart1Driver);
SerialGcode Serial1(&uart1Driver);
4 changes: 2 additions & 2 deletions examples/stm32f4_usb_serial/Application/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#include "globals.h"
#include <cstdint>
#include "Stm32ItmLogger.hpp"
#include "Stm32Serial.hpp"
#include "SerialGcode.hpp"

#ifdef __cplusplus
extern "C" {
#endif

extern uint32_t dummyCpp;
extern Stm32ItmLogger::Stm32ItmLogger logger;
extern Stm32Serial::Stm32Serial Serial1;
extern SerialGcode Serial1;

#ifdef __cplusplus
}
Expand Down
31 changes: 18 additions & 13 deletions examples/stm32f4_usb_serial/Application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "main.hpp"
#include "globals.hpp"
#include "RunEvery.hpp"


/**
Expand All @@ -31,21 +32,25 @@ void setup() {
* @see mainLoopThread() in AZURE_RTOS/App/app_azure_rtos.c
*/
void loop() {
dummyCpp++;
dummyCandCpp++;
Serial1.loop();

static Stm32Common::RunEvery re1(3000);
re1.loop([](){
Serial1.print("counter = ");
Serial1.print(dummyCpp);
Serial1.println();
Serial1.flush();
});

Serial1.print("counter = ");
Serial1.print(dummyCpp);
Serial1.println();
Serial1.flush();


HAL_GPIO_WritePin(LED1_GRN_GPIO_Port, LED1_GRN_Pin, dummyCpp & 1 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(LED2_ORG_GPIO_Port, LED2_ORG_Pin, dummyCpp & 2 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(LED3_RED_GPIO_Port, LED3_RED_Pin, dummyCpp & 4 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(LED4_BLU_GPIO_Port, LED4_BLU_Pin, dummyCpp & 8 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_Delay(500);
static Stm32Common::RunEvery re2(300);
re2.loop([]() {
HAL_GPIO_WritePin(LED1_GRN_GPIO_Port, LED1_GRN_Pin, dummyCpp & 1 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(LED2_ORG_GPIO_Port, LED2_ORG_Pin, dummyCpp & 2 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(LED3_RED_GPIO_Port, LED3_RED_Pin, dummyCpp & 4 ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(LED4_BLU_GPIO_Port, LED4_BLU_Pin, dummyCpp & 8 ? GPIO_PIN_RESET : GPIO_PIN_SET);
dummyCpp++;
dummyCandCpp++;
});
}


Expand Down
2 changes: 1 addition & 1 deletion examples/stm32f4_usb_serial/Lib/Stm32Serial

0 comments on commit 1178bf6

Please sign in to comment.