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

Major UART communication improvement #151

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
build/*
!build/hover.hex
.pio/
.pioenvs/
.vscode/
31 changes: 28 additions & 3 deletions Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#define DELAY_IN_MAIN_LOOP 5 // in ms. default 5. it is independent of all the timing critical stuff. do not touch if you do not know what you are doing.

#define TIMEOUT 5 // number of wrong / missing input commands before emergency off
#define START_FRAME 0xAAAA // serial command start-of-frame magic word
#define SERIAL_START_FRAME 0xABCD // [-] Start frame definition for serial commands
#define SERIAL_TIMEOUT 160 // [-] Serial timeout duration for the received data. 160 ~= 0.8 sec. Calculation: 0.8 sec / 0.005 sec
#define SERIAL_BUFFER_SIZE 64 // [bytes] Size of Serial Rx buffer. Make sure it is always larger than the structure size

// ############################### GENERAL ###############################

Expand Down Expand Up @@ -47,15 +49,23 @@
// ############################### SERIAL DEBUG ###############################

#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck or lcd) is used!
#define DEBUG_BAUD 115200 // UART baud rate
#if defined(DEBUG_SERIAL_USART2)
#define USART2_BAUD 115200 // UART baud rate
#elif defined(DEBUG_SERIAL_USART3)
#define USART3_BAUD 115200 // UART baud rate
#endif
//#define DEBUG_SERIAL_SERVOTERM // Software for plotting graphs: https://github.com/STMBL/Servoterm-app
#define DEBUG_SERIAL_ASCII // "1:345 2:1337 3:0 4:0 5:0 6:0 7:0 8:0\r\n"

// ############################### INPUT ###############################

// ###### CONTROL VIA UART (serial) ######
//#define CONTROL_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
#define CONTROL_BAUD 19200 // control via usart from eg an Arduino or raspberry
#if defined(CONTROL_SERIAL_USART2)
#define USART2_BAUD 19200 // UART baud rate
#elif defined(CONTROL_SERIAL_USART3)
#define USART3_BAUD 19200 // UART baud rate
#endif
// for Arduino, use void loop(void){ Serial.write((uint8_t *) &steer, sizeof(steer)); Serial.write((uint8_t *) &speed, sizeof(speed));delay(20); }

// ###### CONTROL VIA RC REMOTE ######
Expand Down Expand Up @@ -155,13 +165,28 @@ else {\
#error DEBUG_I2C_LCD and DEBUG_SERIAL_USART3 not allowed. it is on the same cable.
#endif

#if defined(CONTROL_SERIAL_USART2) && defined(CONTROL_SERIAL_USART3)
#error CONTROL_SERIAL_USART2 and CONTROL_SERIAL_USART3 not allowed, choose one.
#endif

#if defined(DEBUG_SERIAL_USART2) && defined(DEBUG_SERIAL_USART3)
#error DEBUG_SERIAL_USART2 and DEBUG_SERIAL_USART3 not allowed, choose one.
#endif

#ifdef CONTROL_SERIAL_USART2
#if defined CONTROL_DEFINED
#error select exactly 1 input method in config.h!
#endif
#define CONTROL_DEFINED
#endif

#ifdef CONTROL_SERIAL_USART3
#if defined CONTROL_DEFINED
#error select exactly 1 input method in config.h!
#endif
#define CONTROL_DEFINED
#endif

#ifdef CONTROL_PPM
#if defined CONTROL_DEFINED
#error select exactly 1 input method in config.h!
Expand Down
20 changes: 20 additions & 0 deletions Inc/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#pragma once
#include "stm32f1xx_hal.h"
#include "config.h"

#define LEFT_HALL_U_PIN GPIO_PIN_5
#define LEFT_HALL_V_PIN GPIO_PIN_6
Expand Down Expand Up @@ -141,10 +142,12 @@
#define SIGN(a) (((a) < 0.0) ? (-1.0) : (((a) > 0.0) ? (1.0) : (0.0)))
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0), 1.0)
#define IN_RANGE(x, low, up) (((x) >= (low)) && ((x) <= (up)))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN3(a, b, c) MIN(a, MIN(b, c))
#define MAX3(a, b, c) MAX(a, MAX(b, c))
#define ARRAY_LEN(x) (uint32_t)(sizeof(x) / sizeof(*(x)))

typedef struct {
uint16_t rr1;
Expand All @@ -158,3 +161,20 @@ typedef struct {
uint16_t temp;
uint16_t l_rx2;
} adc_buf_t;

typedef struct{
uint16_t start;
int16_t steer;
int16_t speed;
uint16_t checksum;
} SerialCommand;

void usart2_rx_check(void);
void usart3_rx_check(void);
#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)
void usart_process_debug(uint8_t *userCommand, uint32_t len);
#endif
#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3)
void usart_process_command(SerialCommand *command_in, SerialCommand *command_out, uint8_t usart_idx);
#endif
void UART_DisableRxErrors(UART_HandleTypeDef *huart);
3 changes: 2 additions & 1 deletion Inc/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ void MX_GPIO_Init(void);
void MX_TIM_Init(void);
void MX_ADC1_Init(void);
void MX_ADC2_Init(void);
void UART_Init(void);
void UART2_Init(void);
void UART3_Init(void);
6 changes: 6 additions & 0 deletions Inc/stm32f1xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel1_IRQHandler(void);
void DMA1_Channel2_IRQHandler(void);
void DMA1_Channel3_IRQHandler(void);
void DMA1_Channel6_IRQHandler(void);
void DMA1_Channel7_IRQHandler(void);
void DMA2_Channel4_5_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions Src/bldc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const uint8_t hall_to_pos[8] = {
0,
};

inline void blockPWM(int pwm, int pos, int *u, int *v, int *w) {
void blockPWM(int pwm, int pos, int *u, int *v, int *w) {
switch(pos) {
case 0:
*u = 0;
Expand Down Expand Up @@ -75,7 +75,7 @@ inline void blockPWM(int pwm, int pos, int *u, int *v, int *w) {
}
}

inline void blockPhaseCurrent(int pos, int u, int v, int *q) {
void blockPhaseCurrent(int pos, int u, int v, int *q) {
switch(pos) {
case 0:
*q = u - v;
Expand Down
59 changes: 36 additions & 23 deletions Src/comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
#include "stdio.h"
#include "string.h"

UART_HandleTypeDef huart2;

#ifdef DEBUG_SERIAL_USART3
#define UART_DMA_CHANNEL DMA1_Channel2
#endif

#ifdef DEBUG_SERIAL_USART2
#define UART_DMA_CHANNEL DMA1_Channel7
#endif

extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

volatile uint8_t uart_buf[100];
volatile int16_t ch_buf[8];
Expand All @@ -37,28 +29,49 @@ void consoleScope() {
uart_buf[8] = CLAMP(ch_buf[7]+127, 0, 255);
uart_buf[9] = '\n';

if(UART_DMA_CHANNEL->CNDTR == 0) {
UART_DMA_CHANNEL->CCR &= ~DMA_CCR_EN;
UART_DMA_CHANNEL->CNDTR = 10;
UART_DMA_CHANNEL->CMAR = (uint32_t)uart_buf;
UART_DMA_CHANNEL->CCR |= DMA_CCR_EN;
#ifdef DEBUG_SERIAL_USART2
if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) {
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength);
}
#endif
#ifdef DEBUG_SERIAL_USART3
if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) {
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength);
}
#endif
#endif

#if defined DEBUG_SERIAL_ASCII && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3)
memset(uart_buf, 0, sizeof(uart_buf));
sprintf(uart_buf, "1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n", ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]);
int strLength;
strLength = sprintf((char *)(uintptr_t)uart_buf,
"1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n",
ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]);

if(UART_DMA_CHANNEL->CNDTR == 0) {
UART_DMA_CHANNEL->CCR &= ~DMA_CCR_EN;
UART_DMA_CHANNEL->CNDTR = strlen(uart_buf);
UART_DMA_CHANNEL->CMAR = (uint32_t)uart_buf;
UART_DMA_CHANNEL->CCR |= DMA_CCR_EN;
#ifdef DEBUG_SERIAL_USART2
if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) {
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength);
}
#endif
#ifdef DEBUG_SERIAL_USART3
if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) {
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength);
}
#endif
#endif
}

void consoleLog(char *message)
{
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)message, strlen(message));
#if defined DEBUG_SERIAL_ASCII && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3)
#ifdef DEBUG_SERIAL_USART2
if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) {
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)message, strlen((char *)(uintptr_t)message));
}
#endif
#ifdef DEBUG_SERIAL_USART3
if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) {
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)message, strlen((char *)(uintptr_t)message));
}
#endif
#endif
}
Loading