Skip to content

Commit

Permalink
serial & storage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
robotsrulz committed Jun 19, 2017
1 parent bd156e3 commit 10ba634
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 48 deletions.
9 changes: 6 additions & 3 deletions Inc/SerialIo.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* SerialIo.hpp
* SerialIo.h
*
* Created: 09/11/2014 09:20:46
* Author: David
*/
*/


#ifndef SERIALIO_H_
#define SERIALIO_H_

#ifdef __cplusplus
namespace SerialIo
{
void Init(uint32_t baudRate);
Expand All @@ -18,5 +19,7 @@ namespace SerialIo
void SendInt(int i);
void CheckInput();
}
#else
#endif

#endif /* SERIALIO_H_ */
#endif /* SERIALIO_H_ */
2 changes: 1 addition & 1 deletion Src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ namespace FileManager
if (n > 0 && n <= 10)
{
numVolumes = n + 3;
firstOnScrVol = n + 1;
firstOnScrVol = n;
}
}

Expand Down
65 changes: 27 additions & 38 deletions Src/SerialIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
#include "PanelDue.h"

extern UART_HandleTypeDef huart2;
extern uint8_t comm1RxString[];

extern "C" void Error_Handler(void);

// Receive data processing
const size_t rxBufsize = 2048;
static volatile char rxBuffer[rxBufsize];
static volatile size_t nextIn = 0;
static size_t nextOut = 0;
static bool inError = false;

namespace SerialIo
{
static unsigned int lineNumber = 0;
Expand All @@ -29,6 +37,18 @@ namespace SerialIo
const char* array trCedilla = "C\xC7" "c\xE7";

// Initialize the serial I/O subsystem, or re-initialize it with a new baud rate
uint16_t numChars = 0;
uint8_t checksum = 0;

// Send a character to the 3D printer.
// A typical command string is only about 12 characters long, which at 115200 baud takes just over 1ms to send.
// So there is no particular reason to use interrupts, and by so doing so we avoid having to handle buffer full situations.
void RawSendChar(char c)
{
HAL_UART_Transmit(&huart2, (uint8_t *)&c, 1, 1000);
// while(HAL_UART_Transmit(&huart2, &c, 1, 1000) != HAL_OK) { }
}

void Init(uint32_t baudRate)
{
/* USART2 init function */
Expand All @@ -46,18 +66,11 @@ namespace SerialIo
{
Error_Handler();
}
}

uint16_t numChars = 0;
uint8_t checksum = 0;
RawSendChar('\n');

// Send a character to the 3D printer.
// A typical command string is only about 12 characters long, which at 115200 baud takes just over 1ms to send.
// So there is no particular reason to use interrupts, and by so doing so we avoid having to handle buffer full situations.
void RawSendChar(char c)
{
HAL_UART_Transmit(&huart2, (uint8_t *)&c, 1, 1000);
// while(HAL_UART_Transmit(&huart2, &c, 1, 1000) != HAL_OK) { }
__HAL_UART_FLUSH_DRREGISTER(&huart2);
HAL_UART_Receive_DMA(&huart2, comm1RxString, 0x100);
}

void SendCharAndChecksum(char c)
Expand Down Expand Up @@ -148,13 +161,6 @@ namespace SerialIo
SendChar((char)((char)i + '0'));
}

// Receive data processing
const size_t rxBufsize = 2048;
static volatile char rxBuffer[rxBufsize];
static volatile size_t nextIn = 0;
static size_t nextOut = 0;
static bool inError = false;

// Enumeration to represent the json parsing state.
// We don't allow nested objects or nested arrays, so we don't need a state stack.
// An additional variable elementCount is 0 if we are not in an array, else the number of elements we have found (including the current one)
Expand Down Expand Up @@ -648,10 +654,13 @@ namespace SerialIo
}
}
}
}

extern "C" {

// Called by the ISR to store a received character.
// If the buffer is full, we wait for the next end-of-line.
void receiveChar(char c)
void huart2ReceiveChar(char c)
{
if (c == '\n')
{
Expand All @@ -672,26 +681,6 @@ namespace SerialIo
}
}

// Called by the ISR to signify an error. We wait for the next end of line.
void receiveError()
{
inError = true;
}
}

extern "C" {

extern uint8_t comm1RxBuffer;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART2)
{
__HAL_UART_FLUSH_DRREGISTER(&huart2); // Clear the buffer to prevent overrun
SerialIo::receiveChar(comm1RxBuffer);
}
}

};

// End
18 changes: 14 additions & 4 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ static osThreadId serviceTaskHandle; // low latency tasks
// QueueHandle_t xUIEventQueue;
// QueueHandle_t xPCommEventQueue;

#define MAXCOMM1SIZE 0xffu // Biggest string the user will type
uint8_t comm1RxBuffer = '\000'; // where we store that one character that just came in
// static volatile uint8_t comm1RxString[MAXCOMM1SIZE + 1]; // where we build our string from characters coming in
// static volatile int comm1RxIndex = 0; // index for going though comm1RxString
#define MAXCOMM1SIZE 0xffu // Biggest string the user will type
uint8_t comm1RxBuffer = '\000'; // where we store that one character that just came in
uint8_t comm1RxString[MAXCOMM1SIZE + 1]; // where we build our string from characters coming in
static uint32_t comm1RxStringPtr = 0;

void huart2ReceiveChar(char c);

/* USER CODE END PV */

Expand Down Expand Up @@ -522,6 +524,14 @@ void StartServiceTask(void const * argument) {
break;
}
}

if (huart2.hdmarx && huart2.hdmarx->Instance) {
while (comm1RxStringPtr != ((0x100 - huart2.hdmarx->Instance->CNDTR) & 0xffu)) {

huart2ReceiveChar(comm1RxString[comm1RxStringPtr++]);
comm1RxStringPtr &= 0xffu;
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Src/stm32f1xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
hdma_usart2_rx.Instance = DMA1_Channel6;
hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart2_rx.Init.MemInc = DMA_MINC_DISABLE;
hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;
Expand All @@ -280,7 +280,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
Error_Handler();
}

__HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);
__HAL_LINKDMA(huart, hdmarx, hdma_usart2_rx);

/* Peripheral interrupt init */
HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
Expand Down

0 comments on commit 10ba634

Please sign in to comment.