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

Dshot #3

Open
wants to merge 2 commits into
base: main
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
11 changes: 7 additions & 4 deletions Core/Inc/dshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct {
uint32_t channel;

uint16_t buffer[MAX_BUFF_SIZE+2];
uint16_t speed;
} Motor_HandleTypeDef;

/**
Expand All @@ -41,29 +42,31 @@ QuadMotor_HandleTypeDef *quad_motors;
* @brief Initialisation function for the timing and packet
* buffer needed for the dshot protocol
*/
void DSHOT_init(QuadMotor_HandleTypeDef *motors);
void DSHOT_Init(QuadMotor_HandleTypeDef *motors);

/**
* @brief Arming sequence for the BHEli_S ESC
* @note You must continuously send datat to the ESCs after
* this function has finished else the motors will
* not be armed.
*/
void DSHOT_arm();
void DSHOT_Arm();

/**
* @brief Create a packet of buffer data that prescribes todo
* the dshot 300 protocol
* @param the throttle value to be sent to the ESC
*/
void DSHOT_create_packet(uint16_t val, uint16_t *buf);
void DSHOT_Create_Packet(uint16_t val, uint16_t *buf);

/**
* @brief Read from DMA and create the PWM signal
* @param motor Motor type def to determine which PWM channel to send data to
* @param val Throttle value to be sent to the ESC/motor
* @note val is clamped at THROTTLE_MIN and THROTTLE_MAX
*/
void DSHOT_command_motor(Motor_HandleTypeDef *motor, uint16_t val);
void DSHOT_Command_Motor(Motor_HandleTypeDef *motor);

void DSHOT_Command_All_Motors();

#endif
3 changes: 3 additions & 0 deletions Core/Inc/stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void SysTick_Handler(void);
void DMA1_Stream0_IRQHandler(void);
void DMA1_Stream2_IRQHandler(void);
void DMA1_Stream3_IRQHandler(void);
void DMA1_Stream5_IRQHandler(void);
void DMA1_Stream6_IRQHandler(void);
void USART2_IRQHandler(void);
void DMA1_Stream7_IRQHandler(void);
/* USER CODE BEGIN EFP */

Expand Down
40 changes: 27 additions & 13 deletions Core/Src/dshot.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "dshot.h"

//
void DSHOT_init(QuadMotor_HandleTypeDef *motors)
void DSHOT_Init(QuadMotor_HandleTypeDef *motors)
{
quad_motors = motors;

Expand All @@ -25,17 +25,21 @@ void DSHOT_init(QuadMotor_HandleTypeDef *motors)
motors->motors[3].buffer[17] = 0x00;
}

void DSHOT_arm()
void DSHOT_Arm()
{
// Creating packet info with throttle field value of 0x00
DSHOT_create_packet(0, (uint16_t *)quad_motors->motors[0].buffer);
DSHOT_create_packet(0, (uint16_t *)quad_motors->motors[1].buffer);
DSHOT_create_packet(0, (uint16_t *)quad_motors->motors[2].buffer);
DSHOT_create_packet(0, (uint16_t *)quad_motors->motors[3].buffer);
DSHOT_Create_Packet(0, (uint16_t *)quad_motors->motors[0].buffer);
DSHOT_Create_Packet(0, (uint16_t *)quad_motors->motors[1].buffer);
DSHOT_Create_Packet(0, (uint16_t *)quad_motors->motors[2].buffer);
DSHOT_Create_Packet(0, (uint16_t *)quad_motors->motors[3].buffer);

// We will send 0x00 to each ESC for about 3 seconds
uint32_t millis = HAL_GetTick();

// HAL_TIM_PWM_Start_DMA(quad_motors->motors[0].tim, quad_motors->motors[0].channel, (uint32_t *)quad_motors->motors[0].buffer, 18);
// HAL_TIM_PWM_Start_DMA(quad_motors->motors[1].tim, quad_motors->motors[1].channel, (uint32_t *)quad_motors->motors[1].buffer, 18);
// HAL_TIM_PWM_Start_DMA(quad_motors->motors[2].tim, quad_motors->motors[2].channel, (uint32_t *)quad_motors->motors[2].buffer, 18);
// HAL_TIM_PWM_Start_DMA(quad_motors->motors[3].tim, quad_motors->motors[3].channel, (uint32_t *)quad_motors->motors[3].buffer, 18);
while ((HAL_GetTick() - millis) < ARM_TIME)
{
// Start the DMA transmission to the PWM periphal
Expand All @@ -48,7 +52,7 @@ void DSHOT_arm()
}
}

void DSHOT_create_packet(uint16_t val, uint16_t *buf)
void DSHOT_Create_Packet(uint16_t val, uint16_t *buf)
{
// Disable telemetry bit
val = (val << 1) & 0xfffe;
Expand All @@ -71,18 +75,28 @@ void DSHOT_create_packet(uint16_t val, uint16_t *buf)
}
}

void DSHOT_command_motor(Motor_HandleTypeDef *motor, uint16_t val)
void DSHOT_Command_Motor(Motor_HandleTypeDef *motor)
{
// Clamp the motor speed
if (val < MIN_THROTTLE)
val = MIN_THROTTLE;
if (motor->speed < MIN_THROTTLE)
motor->speed = MIN_THROTTLE;

if (val > MAX_THROTTLE)
val = MAX_THROTTLE;
if (motor->speed > MAX_THROTTLE)
motor->speed = MAX_THROTTLE;

// Create the packet and begin the tnansmission
DSHOT_create_packet(val, (uint16_t *)motor->buffer);
DSHOT_Create_Packet(motor->speed, (uint16_t *)motor->buffer);
HAL_TIM_PWM_Start_DMA(motor->tim, motor->channel, (uint32_t *)motor->buffer, 17);
// HAL_TIM_PWM_Stop_DMA(motor->tim, motor->channel);
}

void DSHOT_Command_All_Motors()
{
for (int i = 0; i < 4; i++)
{
DSHOT_Command_Motor(&quad_motors->motors[i]);
HAL_Delay(1);
}
}


31 changes: 21 additions & 10 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ DMA_HandleTypeDef hdma_tim4_ch1;
DMA_HandleTypeDef hdma_tim4_ch2;

UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_tx;
DMA_HandleTypeDef hdma_usart2_rx;

/* USER CODE BEGIN PV */

Expand Down Expand Up @@ -124,8 +126,8 @@ int main(void)
/* USER CODE BEGIN 2 */

HAL_Delay(100);
DSHOT_init(&quadmotors);
DSHOT_arm();
DSHOT_Init(&quadmotors);
DSHOT_Arm();
uint16_t speed = MIN_THROTTLE;
/* USER CODE END 2 */

Expand All @@ -141,12 +143,13 @@ int main(void)
for (int j = 0; j < 4; j++)
{
if (i == j)
DSHOT_command_motor(&(quadmotors.motors[j].tim), speed);
quad_motors->motors[j].speed = speed;
else
DSHOT_command_motor(&(quadmotors.motors[j].tim), MIN_THROTTLE);
quad_motors->motors[j].speed = MIN_THROTTLE;
DSHOT_Command_All_Motors();
}
speed += 5;
HAL_Delay(5);
// HAL_Delay(5);
}
speed = MAX_THROTTLE/2;

Expand All @@ -155,17 +158,19 @@ int main(void)
for (int j = 0; j < 4; j++)
{
if (i == j)
DSHOT_command_motor(&(quadmotors.motors[j].tim), speed);
// DSHOT_command_motor(&(quadmotors.motors[j]), speed);
quad_motors->motors[j].speed = speed;
else
DSHOT_command_motor(&(quadmotors.motors[j].tim), MIN_THROTTLE);
quad_motors->motors[j].speed = MIN_THROTTLE;
DSHOT_Command_All_Motors();
}
speed -= 5;
HAL_Delay(5);
// HAL_Delay(5);
}
speed = MIN_THROTTLE;
}
HAL_Delay(5);

// HAL_Delay(5);
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
Expand Down Expand Up @@ -475,6 +480,12 @@ static void MX_DMA_Init(void)
/* DMA1_Stream3_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
/* DMA1_Stream5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
/* DMA1_Stream6_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);
/* DMA1_Stream7_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream7_IRQn);
Expand Down
58 changes: 54 additions & 4 deletions Core/Src/stm32f4xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ extern DMA_HandleTypeDef hdma_tim4_ch1;

extern DMA_HandleTypeDef hdma_tim4_ch2;

extern DMA_HandleTypeDef hdma_usart2_tx;

extern DMA_HandleTypeDef hdma_usart2_rx;

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

Expand Down Expand Up @@ -191,7 +195,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
hdma_tim3_ch4_up.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_tim3_ch4_up.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tim3_ch4_up.Init.Mode = DMA_NORMAL;
hdma_tim3_ch4_up.Init.Priority = DMA_PRIORITY_LOW;
hdma_tim3_ch4_up.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tim3_ch4_up.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_tim3_ch4_up) != HAL_OK)
{
Expand All @@ -212,7 +216,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
hdma_tim3_ch3.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_tim3_ch3.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tim3_ch3.Init.Mode = DMA_NORMAL;
hdma_tim3_ch3.Init.Priority = DMA_PRIORITY_LOW;
hdma_tim3_ch3.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tim3_ch3.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_tim3_ch3) != HAL_OK)
{
Expand Down Expand Up @@ -243,7 +247,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
hdma_tim4_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_tim4_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tim4_ch1.Init.Mode = DMA_NORMAL;
hdma_tim4_ch1.Init.Priority = DMA_PRIORITY_LOW;
hdma_tim4_ch1.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tim4_ch1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_tim4_ch1) != HAL_OK)
{
Expand All @@ -261,7 +265,7 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
hdma_tim4_ch2.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_tim4_ch2.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tim4_ch2.Init.Mode = DMA_NORMAL;
hdma_tim4_ch2.Init.Priority = DMA_PRIORITY_LOW;
hdma_tim4_ch2.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tim4_ch2.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_tim4_ch2) != HAL_OK)
{
Expand Down Expand Up @@ -407,6 +411,46 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USART2 DMA Init */
/* USART2_TX Init */
hdma_usart2_tx.Instance = DMA1_Stream6;
hdma_usart2_tx.Init.Channel = DMA_CHANNEL_4;
hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart2_tx.Init.Mode = DMA_NORMAL;
hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)
{
Error_Handler();
}

__HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx);

/* USART2_RX Init */
hdma_usart2_rx.Instance = DMA1_Stream5;
hdma_usart2_rx.Init.Channel = DMA_CHANNEL_4;
hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart2_rx.Init.PeriphInc = DMA_PINC_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_NORMAL;
hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
{
Error_Handler();
}

__HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);

/* USART2 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
/* USER CODE BEGIN USART2_MspInit 1 */

/* USER CODE END USART2_MspInit 1 */
Expand Down Expand Up @@ -436,6 +480,12 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
*/
HAL_GPIO_DeInit(GPIOA, USART_TX_Pin|USART_RX_Pin);

/* USART2 DMA DeInit */
HAL_DMA_DeInit(huart->hdmatx);
HAL_DMA_DeInit(huart->hdmarx);

/* USART2 interrupt DeInit */
HAL_NVIC_DisableIRQ(USART2_IRQn);
/* USER CODE BEGIN USART2_MspDeInit 1 */

/* USER CODE END USART2_MspDeInit 1 */
Expand Down
45 changes: 45 additions & 0 deletions Core/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ extern DMA_HandleTypeDef hdma_tim3_ch4_up;
extern DMA_HandleTypeDef hdma_tim3_ch3;
extern DMA_HandleTypeDef hdma_tim4_ch1;
extern DMA_HandleTypeDef hdma_tim4_ch2;
extern DMA_HandleTypeDef hdma_usart2_tx;
extern DMA_HandleTypeDef hdma_usart2_rx;
extern UART_HandleTypeDef huart2;
/* USER CODE BEGIN EV */

/* USER CODE END EV */
Expand Down Expand Up @@ -243,6 +246,48 @@ void DMA1_Stream3_IRQHandler(void)
/* USER CODE END DMA1_Stream3_IRQn 1 */
}

/**
* @brief This function handles DMA1 stream5 global interrupt.
*/
void DMA1_Stream5_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Stream5_IRQn 0 */

/* USER CODE END DMA1_Stream5_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart2_rx);
/* USER CODE BEGIN DMA1_Stream5_IRQn 1 */

/* USER CODE END DMA1_Stream5_IRQn 1 */
}

/**
* @brief This function handles DMA1 stream6 global interrupt.
*/
void DMA1_Stream6_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Stream6_IRQn 0 */

/* USER CODE END DMA1_Stream6_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart2_tx);
/* USER CODE BEGIN DMA1_Stream6_IRQn 1 */

/* USER CODE END DMA1_Stream6_IRQn 1 */
}

/**
* @brief This function handles USART2 global interrupt.
*/
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */

/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */

/* USER CODE END USART2_IRQn 1 */
}

/**
* @brief This function handles DMA1 stream7 global interrupt.
*/
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.18.0-B7] date: [Wed Feb 01 07:33:00 EST 2023]
# File automatically-generated by tool: [projectgenerator] version: [3.18.0-B7] date: [Sat Feb 25 13:31:02 EST 2023]
##########################################################################################################################

# ------------------------------------------------
Expand Down
Loading