Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
feat: debug using usart
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorBel committed Apr 4, 2021
1 parent 4d19f3c commit 7aa5d6b
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions src/bldc-sensored/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "misc.h"
#include <math.h>

#define _DEBUG

#include "definitions.h"
// #include "motorcontrolsettings.h"

Expand Down Expand Up @@ -103,6 +105,7 @@ void led_init()
//GPIO_SetBits(GPIOC, GPIO_Pin_13); // Set C13 to High level ("1")
GPIO_ResetBits(GPIOC, GPIO_Pin_13); // Set C13 to Low level ("0")
}

void tim1_init()
{
// PB13-15 will be normal gpio
Expand Down Expand Up @@ -144,11 +147,81 @@ void tim1_init()
TIM1->DIER = b6;
}

void usart3_init(void)
{
/* Enable USART3 and GPIOB clock */
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* NVIC Configuration */
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* Configure the GPIOs */
GPIO_InitTypeDef GPIO_InitStructure;

/* Configure USART3 Tx (PB.10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configure USART3 Rx (PB.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configure the USART3 */
USART_InitTypeDef USART_InitStructure;

/* USART3 configuration ------------------------------------------------------*/
/* USART3 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART3, &USART_InitStructure);

/* Enable USART3 */
USART_Cmd(USART3, ENABLE);

/* Enable the USART3 Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}

void USARTSend(const unsigned char *pucBuffer)
{
while (*pucBuffer)
{
USART_SendData(USART3, *pucBuffer++);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{
}
}
}

void HallSensorsGetPosition(void)
{
// uint8_t res6_7 = (uint8_t)((GPIO_ReadInputData(GPIOB) & (GPIO_Pin_6 | GPIO_Pin_7)) >> 5);
// uint8_t res4 = (uint8_t)((GPIO_ReadInputData(GPIOB) & GPIO_Pin_4) >> 4);
// uint8_t hallpos = res6_7 | res4;
uint8_t hallpos = (uint8_t)((GPIO_ReadInputData(GPIOB) & (GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9)) >> 7);
switch (hallpos)
{
Expand All @@ -175,6 +248,12 @@ void HallSensorsGetPosition(void)
phase = 0;
break;
}

#ifdef _DEBUG
char buffer[80] = {'\0'};
sprintf(buffer, "\r\nRaw hall: %d\r\nPhase: %d\r\n", hallpos, phase);
USARTSend(buffer);
#endif
}

void HallSensorsInit(void)
Expand Down Expand Up @@ -356,6 +435,10 @@ int main(void)
// tim1 setup
tim1_init();

#ifdef _DEBUG
usart3_init();
#endif

delay(5000000); //let power supply settle
delay(5000000); //let power supply settle
delay(5000000); //let power supply settle
Expand Down

0 comments on commit 7aa5d6b

Please sign in to comment.