FIX&ENH: rotary driver works!
ADD: three pins for current strength values FIX: ADC1->ADC2 ENH: prepare for UART FIX&ADD: add USART print, add extra_script to print floats to COM port #2 ADD: some func #2 Rebuild project structure ADD: .clang-format file ENG: use flag ADD: USART1 FIX: .ioc FIX: .ioc ADD: debug script to angle value ADD: gitlab-ci ADD: gitlab CI ADD: gitlab-ci FIX: gitlab-ci ADD&FIX: all sensors work! (or not?)
This commit is contained in:
parent
d1b0ed0858
commit
8157b41ea9
23 changed files with 673 additions and 339 deletions
20
.clang-format
Normal file
20
.clang-format
Normal file
|
@ -0,0 +1,20 @@
|
|||
---
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 2
|
||||
|
||||
# If function declarations have brace on the new line like this:
|
||||
# void function()
|
||||
# {
|
||||
# ...
|
||||
# }
|
||||
BreakBeforeBraces: Allman
|
||||
|
||||
# If function declarations have brace on the same line like this:
|
||||
# void function() {
|
||||
# ...
|
||||
# }
|
||||
# BreakBeforeBraces: Attach
|
||||
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
BinPackParameters: true
|
||||
ColumnLimit: 0 # Disable line column limit
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
.pio
|
||||
.vscode
|
||||
.idea
|
18
.gitlab-ci.yml
Normal file
18
.gitlab-ci.yml
Normal file
|
@ -0,0 +1,18 @@
|
|||
image: python:3.9
|
||||
|
||||
stages:
|
||||
- build
|
||||
|
||||
before_script:
|
||||
- pip install -U platformio
|
||||
- pio update
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script:
|
||||
- pio run
|
||||
artifacts:
|
||||
paths:
|
||||
- .pio/build/robotroller/firmware.elf
|
||||
- .pio/build/robotroller/firmware.bin
|
||||
expire_in: 1 week
|
12
Inc/adc.h
12
Inc/adc.h
|
@ -32,14 +32,20 @@ extern "C" {
|
|||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
extern ADC_HandleTypeDef hadc2;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
#define Vref 3.3f
|
||||
#define ADCResolution 4096.0f
|
||||
#define ShuntResistorValue 1.f
|
||||
#define INA_gain 10.f // This gain
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_ADC1_Init(void);
|
||||
void MX_ADC2_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
uint32_t ReadADCValue(ADC_HandleTypeDef *hadc, uint32_t channel);
|
||||
float GetCurrentFromADC(uint32_t adcValue);
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -39,7 +39,7 @@ extern "C" {
|
|||
void MX_GPIO_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
void AS5045_CS_Init(void);
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -29,8 +29,6 @@ extern "C" {
|
|||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#include "stm32f4xx_ll_adc.h"
|
||||
#include "stm32f4xx_ll_spi.h"
|
||||
#include "stm32f4xx_ll_tim.h"
|
||||
#include "stm32f4xx_ll_usart.h"
|
||||
#include "stm32f4xx_ll_rcc.h"
|
||||
|
@ -71,6 +69,12 @@ void Error_Handler(void);
|
|||
/* USER CODE END EFP */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
#define SENSE3_Pin LL_GPIO_PIN_5
|
||||
#define SENSE3_GPIO_Port GPIOC
|
||||
#define SENSE2_Pin LL_GPIO_PIN_0
|
||||
#define SENSE2_GPIO_Port GPIOB
|
||||
#define SENSE1_Pin LL_GPIO_PIN_1
|
||||
#define SENSE1_GPIO_Port GPIOB
|
||||
#define AS5045_CS_Pin LL_GPIO_PIN_15
|
||||
#define AS5045_CS_GPIO_Port GPIOB
|
||||
#define EN_W_Pin LL_GPIO_PIN_6
|
||||
|
|
|
@ -32,14 +32,19 @@ extern "C" {
|
|||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
extern SPI_HandleTypeDef hspi2;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
#define AS5040_DIAG_OC_FAULT 0x0001 // Offset Compensation Finished
|
||||
#define AS5040_DIAG_CO_FAULT 0x0002 // Cordic Overflow
|
||||
#define AS5040_DIAG_LIN_FAULT 0x0004 // Linearity Alarm
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_SPI2_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
float NormalizeToDegrees(uint16_t rawAngle);
|
||||
uint16_t AS5045_ReadAngle(SPI_HandleTypeDef *hspi, uint8_t *status);
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
*/
|
||||
#define HAL_MODULE_ENABLED
|
||||
|
||||
/* #define HAL_ADC_MODULE_ENABLED */
|
||||
#define HAL_ADC_MODULE_ENABLED
|
||||
/* #define HAL_CRYP_MODULE_ENABLED */
|
||||
#define HAL_CAN_MODULE_ENABLED
|
||||
/* #define HAL_CRC_MODULE_ENABLED */
|
||||
|
@ -62,7 +62,7 @@
|
|||
/* #define HAL_SAI_MODULE_ENABLED */
|
||||
/* #define HAL_SD_MODULE_ENABLED */
|
||||
/* #define HAL_MMC_MODULE_ENABLED */
|
||||
/* #define HAL_SPI_MODULE_ENABLED */
|
||||
#define HAL_SPI_MODULE_ENABLED
|
||||
#define HAL_TIM_MODULE_ENABLED
|
||||
/* #define HAL_UART_MODULE_ENABLED */
|
||||
/* #define HAL_USART_MODULE_ENABLED */
|
||||
|
|
|
@ -60,8 +60,9 @@ void TIM1_BRK_TIM9_IRQHandler(void);
|
|||
void TIM1_CC_IRQHandler(void);
|
||||
void TIM2_IRQHandler(void);
|
||||
void TIM3_IRQHandler(void);
|
||||
void SPI2_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
extern volatile FlagStatus flag_10kHz;
|
||||
/* USER CODE END EFP */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
36
Inc/tim.h
36
Inc/tim.h
|
@ -1,21 +1,21 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file tim.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the tim.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file tim.h
|
||||
* @brief This file contains all the function prototypes for
|
||||
* the tim.c file
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __TIM_H__
|
||||
|
@ -33,7 +33,7 @@ extern "C" {
|
|||
/* USER CODE END Includes */
|
||||
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern volatile bool tim3_semaphore;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
@ -43,7 +43,7 @@ void MX_TIM2_Init(void);
|
|||
void MX_TIM3_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
extern volatile FlagStatus flag_10kHz;
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -40,7 +40,7 @@ void MX_USART1_UART_Init(void);
|
|||
void MX_USART2_UART_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
void USART1_PutString(char *str);
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
196
Src/adc.c
196
Src/adc.c
|
@ -21,95 +21,149 @@
|
|||
#include "adc.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
uint32_t ReadADCValue(ADC_HandleTypeDef *hadc, uint32_t channel)
|
||||
{
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
sConfig.Channel = channel;
|
||||
sConfig.Rank = 1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
||||
|
||||
HAL_ADC_ConfigChannel(hadc, &sConfig);
|
||||
HAL_ADC_Start(hadc); // Start conversion
|
||||
|
||||
if (HAL_ADC_PollForConversion(hadc, HAL_MAX_DELAY) == HAL_OK) // Poll for conversion
|
||||
{
|
||||
return HAL_ADC_GetValue(hadc); // Get the converted value
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0; // If there was an error, return 0
|
||||
}
|
||||
}
|
||||
|
||||
float GetCurrentFromADC(uint32_t adcValue)
|
||||
{
|
||||
float voltage = (adcValue / ADCResolution) * Vref;
|
||||
float current = voltage / (ShuntResistorValue * INA_gain);
|
||||
// сократи значение до 0.000
|
||||
current = (int)(current * 1000) / 1000.0f;
|
||||
return current;
|
||||
}
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* ADC1 init function */
|
||||
void MX_ADC1_Init(void)
|
||||
ADC_HandleTypeDef hadc2;
|
||||
|
||||
/* ADC2 init function */
|
||||
void MX_ADC2_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN ADC1_Init 0 */
|
||||
/* USER CODE BEGIN ADC2_Init 0 */
|
||||
|
||||
/* USER CODE END ADC1_Init 0 */
|
||||
/* USER CODE END ADC2_Init 0 */
|
||||
|
||||
LL_ADC_InitTypeDef ADC_InitStruct = {0};
|
||||
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
|
||||
LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
|
||||
LL_ADC_INJ_InitTypeDef ADC_INJ_InitStruct = {0};
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/* USER CODE BEGIN ADC2_Init 1 */
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
|
||||
/* USER CODE END ADC2_Init 1 */
|
||||
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
||||
/**ADC1 GPIO Configuration
|
||||
PA4 ------> ADC1_IN4
|
||||
PA5 ------> ADC1_IN5
|
||||
PA6 ------> ADC1_IN6
|
||||
PB0 ------> ADC1_IN8
|
||||
PB1 ------> ADC1_IN9
|
||||
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_4|LL_GPIO_PIN_5|LL_GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
hadc2.Instance = ADC2;
|
||||
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
hadc2.Init.ScanConvMode = DISABLE;
|
||||
hadc2.Init.ContinuousConvMode = DISABLE;
|
||||
hadc2.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
hadc2.Init.NbrOfConversion = 1;
|
||||
hadc2.Init.DMAContinuousRequests = DISABLE;
|
||||
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
if (HAL_ADC_Init(&hadc2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN ADC1_Init 1 */
|
||||
|
||||
/* USER CODE END ADC1_Init 1 */
|
||||
|
||||
/** Common config
|
||||
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
|
||||
*/
|
||||
ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
|
||||
ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
|
||||
ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
|
||||
LL_ADC_Init(ADC1, &ADC_InitStruct);
|
||||
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
|
||||
ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
|
||||
ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
|
||||
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
|
||||
ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
|
||||
LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
|
||||
LL_ADC_REG_SetFlagEndOfConversion(ADC1, LL_ADC_REG_FLAG_EOC_SEQUENCE_CONV);
|
||||
ADC_CommonInitStruct.CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||
ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
|
||||
LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
|
||||
sConfig.Channel = ADC_CHANNEL_15;
|
||||
sConfig.Rank = 1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
||||
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN ADC2_Init 2 */
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_4);
|
||||
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_3CYCLES);
|
||||
/* USER CODE END ADC2_Init 2 */
|
||||
|
||||
/** Configure Injected Channel
|
||||
*/
|
||||
ADC_INJ_InitStruct.TriggerSource = LL_ADC_INJ_TRIG_EXT_TIM1_TRGO;
|
||||
ADC_INJ_InitStruct.SequencerLength = LL_ADC_INJ_SEQ_SCAN_ENABLE_2RANKS;
|
||||
ADC_INJ_InitStruct.SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
|
||||
ADC_INJ_InitStruct.TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
|
||||
LL_ADC_INJ_Init(ADC1, &ADC_INJ_InitStruct);
|
||||
LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_1, LL_ADC_CHANNEL_8);
|
||||
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_8, LL_ADC_SAMPLINGTIME_3CYCLES);
|
||||
LL_ADC_INJ_SetOffset(ADC1, LL_ADC_INJ_RANK_1, 0);
|
||||
LL_ADC_INJ_StartConversionExtTrig(ADC1, LL_ADC_INJ_TRIG_EXT_RISING);
|
||||
}
|
||||
|
||||
/** Configure Injected Channel
|
||||
*/
|
||||
LL_ADC_INJ_Init(ADC1, &ADC_INJ_InitStruct);
|
||||
LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_2, LL_ADC_CHANNEL_9);
|
||||
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_9, LL_ADC_SAMPLINGTIME_3CYCLES);
|
||||
LL_ADC_INJ_SetOffset(ADC1, LL_ADC_INJ_RANK_2, 0);
|
||||
LL_ADC_INJ_StartConversionExtTrig(ADC1, LL_ADC_INJ_TRIG_EXT_RISING);
|
||||
/* USER CODE BEGIN ADC1_Init 2 */
|
||||
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
||||
{
|
||||
|
||||
/* USER CODE END ADC1_Init 2 */
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(adcHandle->Instance==ADC2)
|
||||
{
|
||||
/* USER CODE BEGIN ADC2_MspInit 0 */
|
||||
|
||||
/* USER CODE END ADC2_MspInit 0 */
|
||||
/* ADC2 clock enable */
|
||||
__HAL_RCC_ADC2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**ADC2 GPIO Configuration
|
||||
PC5 ------> ADC2_IN15
|
||||
PB0 ------> ADC2_IN8
|
||||
PB1 ------> ADC2_IN9
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN ADC2_MspInit 1 */
|
||||
|
||||
/* USER CODE END ADC2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
||||
{
|
||||
|
||||
if(adcHandle->Instance==ADC2)
|
||||
{
|
||||
/* USER CODE BEGIN ADC2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END ADC2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_ADC2_CLK_DISABLE();
|
||||
|
||||
/**ADC2 GPIO Configuration
|
||||
PC5 ------> ADC2_IN15
|
||||
PB0 ------> ADC2_IN8
|
||||
PB1 ------> ADC2_IN9
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_5);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
|
||||
|
||||
/* ADC2 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(ADC_IRQn);
|
||||
/* USER CODE BEGIN ADC2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END ADC2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
|
15
Src/gpio.c
15
Src/gpio.c
|
@ -106,5 +106,20 @@ void MX_GPIO_Init(void)
|
|||
}
|
||||
|
||||
/* USER CODE BEGIN 2 */
|
||||
void AS5045_CS_Init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
// Enable the GPIO clocks for CS pin
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
// Configure the GPIO pin for the CS as output
|
||||
GPIO_InitStruct.Pin = AS5045_CS_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(AS5045_CS_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
// Set the CS pin high initially
|
||||
HAL_GPIO_WritePin(AS5045_CS_GPIO_Port, AS5045_CS_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
/* USER CODE END 2 */
|
||||
|
|
162
Src/main.c
162
Src/main.c
|
@ -1,29 +1,30 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "adc.h"
|
||||
#include "can.h"
|
||||
#include "gpio.h"
|
||||
#include "spi.h"
|
||||
#include "tim.h"
|
||||
#include "usart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
@ -63,9 +64,9 @@ static void MX_NVIC_Init(void);
|
|||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
@ -78,6 +79,7 @@ int main(void)
|
|||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
|
@ -89,7 +91,6 @@ int main(void)
|
|||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_ADC1_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_USART2_UART_Init();
|
||||
|
@ -98,49 +99,97 @@ int main(void)
|
|||
MX_CAN1_Init();
|
||||
MX_CAN2_Init();
|
||||
MX_TIM3_Init();
|
||||
MX_ADC2_Init();
|
||||
|
||||
/* Initialize interrupts */
|
||||
MX_NVIC_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
|
||||
{
|
||||
/* Starting Error */
|
||||
Error_Handler();
|
||||
}
|
||||
AS5045_CS_Init();
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
char buf[100];
|
||||
float angle_values[200];
|
||||
uint8_t angle_index = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
if (tim3_semaphore)
|
||||
if (flag_10kHz == SET)
|
||||
{
|
||||
tim3_semaphore = false;
|
||||
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
|
||||
flag_10kHz = RESET;
|
||||
// Do something every 10 kHz
|
||||
// BLINK LED1
|
||||
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
||||
uint8_t status = 0;
|
||||
uint16_t angle = AS5045_ReadAngle(&hspi2, &status);
|
||||
if (angle_index == 200)
|
||||
{
|
||||
angle_index = 0;
|
||||
float average = 0;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
average += angle_values[i];
|
||||
}
|
||||
average /= 20.0;
|
||||
float adcValue1 = GetCurrentFromADC(ReadADCValue(&hadc2, ADC_CHANNEL_15)); // Read ADC
|
||||
float adcValue2 = GetCurrentFromADC(ReadADCValue(&hadc2, ADC_CHANNEL_8)); // Read ADC
|
||||
float adcValue3 = GetCurrentFromADC(ReadADCValue(&hadc2, ADC_CHANNEL_9)); // Read ADC
|
||||
sprintf(buf, "ADC1:%f, ADC2:%f, ADC3:%f, ANGLE:%f\n", adcValue1, adcValue2, adcValue3, average);
|
||||
USART1_PutString(buf);
|
||||
}
|
||||
if (status == 0)
|
||||
{
|
||||
float degrees = NormalizeToDegrees(angle);
|
||||
angle_values[angle_index] = degrees;
|
||||
angle_index++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle the error
|
||||
if (status & AS5040_DIAG_OC_FAULT)
|
||||
{
|
||||
// Offset Compensation not finished
|
||||
}
|
||||
if (status & AS5040_DIAG_CO_FAULT)
|
||||
{
|
||||
// Cordic Overflow error
|
||||
}
|
||||
if (status & AS5040_DIAG_LIN_FAULT)
|
||||
{
|
||||
// Linearity Alarm error
|
||||
}
|
||||
}
|
||||
|
||||
// sprintf(buf, "Average: %f\r\n", degrees);
|
||||
// USART1_PutString(buf);
|
||||
// HAL_Delay(100);
|
||||
}
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
*/
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
||||
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
|
@ -156,16 +205,15 @@ void SystemClock_Config(void)
|
|||
}
|
||||
|
||||
/** Activate the Over-Drive mode
|
||||
*/
|
||||
*/
|
||||
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Initializes the CPU, AHB and APB buses clocks
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
|
||||
|
@ -178,23 +226,23 @@ void SystemClock_Config(void)
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief NVIC Configuration.
|
||||
* @retval None
|
||||
*/
|
||||
* @brief NVIC Configuration.
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_NVIC_Init(void)
|
||||
{
|
||||
/* TIM1_BRK_TIM9_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),1, 0));
|
||||
NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 0));
|
||||
NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
|
||||
/* TIM1_CC_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(TIM1_CC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),3, 0));
|
||||
NVIC_SetPriority(TIM1_CC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 3, 0));
|
||||
NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||
/* TIM2_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),4, 0));
|
||||
NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 4, 0));
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
/* ADC_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(ADC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),2, 0));
|
||||
NVIC_EnableIRQ(ADC_IRQn);
|
||||
HAL_NVIC_SetPriority(ADC_IRQn, 2, 0);
|
||||
HAL_NVIC_EnableIRQ(ADC_IRQn);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
@ -202,9 +250,9 @@ static void MX_NVIC_Init(void)
|
|||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
void Error_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
|
@ -216,14 +264,14 @@ void Error_Handler(void)
|
|||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t *file, uint32_t line)
|
||||
{
|
||||
/* USER CODE BEGIN 6 */
|
||||
|
|
196
Src/spi.c
196
Src/spi.c
|
@ -1,29 +1,60 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file spi.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the SPI instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file spi.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the SPI instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "spi.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
// AS5040 diagnostic bits
|
||||
|
||||
uint16_t AS5045_ReadAngle(SPI_HandleTypeDef *hspi, uint8_t *status)
|
||||
{
|
||||
uint16_t rawData = 0;
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
|
||||
HAL_SPI_Receive(&hspi2, (uint8_t *)&rawData, 2, HAL_MAX_DELAY);
|
||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
|
||||
*status = 0;
|
||||
if (rawData & AS5040_DIAG_OC_FAULT)
|
||||
{
|
||||
*status |= AS5040_DIAG_OC_FAULT;
|
||||
}
|
||||
if (rawData & AS5040_DIAG_CO_FAULT)
|
||||
{
|
||||
*status |= AS5040_DIAG_CO_FAULT;
|
||||
}
|
||||
if (rawData & AS5040_DIAG_LIN_FAULT)
|
||||
{
|
||||
*status |= AS5040_DIAG_LIN_FAULT;
|
||||
}
|
||||
return (rawData >> 5) & 0x3FFF;
|
||||
}
|
||||
|
||||
float NormalizeToDegrees(uint16_t angleValue)
|
||||
{
|
||||
float degrees = (angleValue * 360.0f) / 1024.0f;
|
||||
return degrees;
|
||||
}
|
||||
/* USER CODE END 0 */
|
||||
|
||||
SPI_HandleTypeDef hspi2;
|
||||
|
||||
/* SPI2 init function */
|
||||
void MX_SPI2_Init(void)
|
||||
{
|
||||
|
@ -32,57 +63,104 @@ void MX_SPI2_Init(void)
|
|||
|
||||
/* USER CODE END SPI2_Init 0 */
|
||||
|
||||
LL_SPI_InitTypeDef SPI_InitStruct = {0};
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
|
||||
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
||||
/**SPI2 GPIO Configuration
|
||||
PC1 ------> SPI2_MOSI
|
||||
PB10 ------> SPI2_SCK
|
||||
PB14 ------> SPI2_MISO
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_14;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN SPI2_Init 1 */
|
||||
|
||||
/* USER CODE END SPI2_Init 1 */
|
||||
SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
|
||||
SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
|
||||
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_16BIT;
|
||||
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
|
||||
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
|
||||
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
|
||||
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV64;
|
||||
SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
|
||||
SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
|
||||
SPI_InitStruct.CRCPoly = 10;
|
||||
LL_SPI_Init(SPI2, &SPI_InitStruct);
|
||||
LL_SPI_SetStandard(SPI2, LL_SPI_PROTOCOL_MOTOROLA);
|
||||
hspi2.Instance = SPI2;
|
||||
hspi2.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
|
||||
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi2.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
|
||||
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
hspi2.Init.CRCPolynomial = 10;
|
||||
if (HAL_SPI_Init(&hspi2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN SPI2_Init 2 */
|
||||
|
||||
if (HAL_SPI_Init(&hspi2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE END SPI2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(spiHandle->Instance==SPI2)
|
||||
{
|
||||
/* USER CODE BEGIN SPI2_MspInit 0 */
|
||||
|
||||
/* USER CODE END SPI2_MspInit 0 */
|
||||
/* SPI2 clock enable */
|
||||
__HAL_RCC_SPI2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**SPI2 GPIO Configuration
|
||||
PC1 ------> SPI2_MOSI
|
||||
PB10 ------> SPI2_SCK
|
||||
PB14 ------> SPI2_MISO
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_SPI2;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_14;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* SPI2 interrupt Init */
|
||||
HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(SPI2_IRQn);
|
||||
/* USER CODE BEGIN SPI2_MspInit 1 */
|
||||
|
||||
/* USER CODE END SPI2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
|
||||
{
|
||||
|
||||
if(spiHandle->Instance==SPI2)
|
||||
{
|
||||
/* USER CODE BEGIN SPI2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END SPI2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_SPI2_CLK_DISABLE();
|
||||
|
||||
/**SPI2 GPIO Configuration
|
||||
PC1 ------> SPI2_MOSI
|
||||
PB10 ------> SPI2_SCK
|
||||
PB14 ------> SPI2_MISO
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1);
|
||||
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_14);
|
||||
|
||||
/* SPI2 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(SPI2_IRQn);
|
||||
/* USER CODE BEGIN SPI2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END SPI2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f4xx_it.c
|
||||
* @brief Interrupt Service Routines.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file stm32f4xx_it.c
|
||||
* @brief Interrupt Service Routines.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
|
@ -55,10 +55,11 @@
|
|||
/* USER CODE END 0 */
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern ADC_HandleTypeDef hadc2;
|
||||
extern SPI_HandleTypeDef hspi2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern bool tim3_semaphore;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
volatile FlagStatus flag_10kHz = RESET;
|
||||
/* USER CODE END EV */
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -207,7 +208,7 @@ void ADC_IRQHandler(void)
|
|||
/* USER CODE BEGIN ADC_IRQn 0 */
|
||||
|
||||
/* USER CODE END ADC_IRQn 0 */
|
||||
|
||||
HAL_ADC_IRQHandler(&hadc2);
|
||||
/* USER CODE BEGIN ADC_IRQn 1 */
|
||||
|
||||
/* USER CODE END ADC_IRQn 1 */
|
||||
|
@ -246,7 +247,7 @@ void TIM1_CC_IRQHandler(void)
|
|||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM2_IRQn 0 */
|
||||
|
||||
|
||||
/* USER CODE END TIM2_IRQn 0 */
|
||||
/* USER CODE BEGIN TIM2_IRQn 1 */
|
||||
|
||||
|
@ -259,23 +260,39 @@ void TIM2_IRQHandler(void)
|
|||
void TIM3_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM3_IRQn 0 */
|
||||
// BLink Led1
|
||||
|
||||
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
||||
if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE) != RESET)
|
||||
// Check if update interrupt flag is set
|
||||
if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE) != RESET)
|
||||
{
|
||||
if (__HAL_TIM_GET_IT_SOURCE(&htim3, TIM_IT_UPDATE) != RESET)
|
||||
{
|
||||
if (__HAL_TIM_GET_IT_SOURCE(&htim3, TIM_IT_UPDATE) != RESET)
|
||||
{
|
||||
__HAL_TIM_CLEAR_IT(&htim3, TIM_IT_UPDATE);
|
||||
tim3_semaphore = true;
|
||||
}
|
||||
// Clear update interrupt flag
|
||||
__HAL_TIM_CLEAR_IT(&htim3, TIM_IT_UPDATE);
|
||||
|
||||
// Toggle or set the flag
|
||||
flag_10kHz = SET;
|
||||
}
|
||||
}
|
||||
/* USER CODE END TIM3_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim3);
|
||||
/* USER CODE BEGIN TIM3_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM3_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles SPI2 global interrupt.
|
||||
*/
|
||||
void SPI2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN SPI2_IRQn 0 */
|
||||
|
||||
/* USER CODE END SPI2_IRQn 0 */
|
||||
HAL_SPI_IRQHandler(&hspi2);
|
||||
/* USER CODE BEGIN SPI2_IRQn 1 */
|
||||
|
||||
/* USER CODE END SPI2_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
|
42
Src/tim.c
42
Src/tim.c
|
@ -1,21 +1,21 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file tim.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the TIM instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file tim.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the TIM instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2024 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "tim.h"
|
||||
|
@ -25,7 +25,7 @@
|
|||
/* USER CODE END 0 */
|
||||
|
||||
TIM_HandleTypeDef htim3;
|
||||
volatile bool tim3_semaphore;
|
||||
|
||||
/* TIM1 init function */
|
||||
void MX_TIM1_Init(void)
|
||||
{
|
||||
|
@ -148,7 +148,7 @@ void MX_TIM3_Init(void)
|
|||
htim3.Instance = TIM3;
|
||||
htim3.Init.Prescaler = 89;
|
||||
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim3.Init.Period = 199;
|
||||
htim3.Init.Period = 99;
|
||||
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
|
||||
|
@ -167,7 +167,11 @@ void MX_TIM3_Init(void)
|
|||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM3_Init 2 */
|
||||
|
||||
if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
|
||||
{
|
||||
/* Starting Error */
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE END TIM3_Init 2 */
|
||||
|
||||
}
|
||||
|
|
12
Src/usart.c
12
Src/usart.c
|
@ -21,7 +21,19 @@
|
|||
#include "usart.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
void USART1_PutChar(char ch)
|
||||
{
|
||||
while (!LL_USART_IsActiveFlag_TXE(USART1));
|
||||
LL_USART_TransmitData8(USART1, (uint8_t)ch);
|
||||
}
|
||||
|
||||
void USART1_PutString(char *str)
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
USART1_PutChar(*str++);
|
||||
}
|
||||
}
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* USART1 init function */
|
||||
|
|
12
extra_script.py
Normal file
12
extra_script.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
Import("env")
|
||||
|
||||
|
||||
# Dump build environment (for debug)
|
||||
# print(env.Dump())
|
||||
|
||||
# add floating point unit support STM32F4
|
||||
FPU_FLAGS = "-mfloat-abi=hard -mfpu=fpv4-sp-d16"
|
||||
# add -u _printf_float to LDFLAGS
|
||||
env.Append(LINKFLAGS="-u _printf_float")
|
||||
# env.Append(CCFLAGS=FPU_FLAGS)
|
||||
# env.Append(LINKFLAGS=FPU_FLAGS)
|
|
@ -10,7 +10,7 @@
|
|||
[platformio]
|
||||
include_dir = Inc
|
||||
src_dir = Src
|
||||
|
||||
lib_dir = Lib
|
||||
[env:robotroller]
|
||||
platform = ststm32
|
||||
board = genericSTM32F446RE
|
||||
|
@ -19,10 +19,8 @@ board_build.stm32cube.custom_config_header = yes
|
|||
build_flags =
|
||||
-D USE_FULL_LL_DRIVER
|
||||
-D USE_HAL_DRIVER
|
||||
|
||||
board_build.stm32cube.startup = startup_stm32f446retx.s
|
||||
board_build.stm32cube.ldscript = STM32F446RETx_FLASH.ld
|
||||
; board_upload.offset_address = 0x08008000
|
||||
upload_protocol = stlink
|
||||
debug_tool = stlink
|
||||
|
||||
monitor_speed = 115200
|
||||
monitor_parity = N
|
||||
extra_scripts = extra_script.py
|
|
@ -1,26 +1,18 @@
|
|||
#MicroXplorer Configuration settings - do not modify
|
||||
ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_4
|
||||
ADC1.Channel-1\#ChannelInjectedConversion=ADC_CHANNEL_8
|
||||
ADC1.Channel-2\#ChannelInjectedConversion=ADC_CHANNEL_9
|
||||
ADC1.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4
|
||||
ADC1.ContinuousConvMode=DISABLE
|
||||
ADC1.DiscontinuousConvMode=DISABLE
|
||||
ADC1.EOCSelection=ADC_EOC_SEQ_CONV
|
||||
ADC1.ExternalTrigInjecConv=ADC_EXTERNALTRIGINJECCONV_T1_TRGO
|
||||
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,master,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,EOCSelection,InjectedRank-1\#ChannelInjectedConversion,Channel-1\#ChannelInjectedConversion,SamplingTime-1\#ChannelInjectedConversion,InjectedOffset-1\#ChannelInjectedConversion,InjectedRank-2\#ChannelInjectedConversion,Channel-2\#ChannelInjectedConversion,SamplingTime-2\#ChannelInjectedConversion,InjectedOffset-2\#ChannelInjectedConversion,InjNumberOfConversion,ExternalTrigInjecConv,ClockPrescaler,InjectedConvMode,ContinuousConvMode,DiscontinuousConvMode,NbrOfConversion
|
||||
ADC1.InjNumberOfConversion=2
|
||||
ADC1.InjectedConvMode=None
|
||||
ADC1.InjectedOffset-1\#ChannelInjectedConversion=0
|
||||
ADC1.InjectedOffset-2\#ChannelInjectedConversion=0
|
||||
ADC1.InjectedRank-1\#ChannelInjectedConversion=1
|
||||
ADC1.InjectedRank-2\#ChannelInjectedConversion=2
|
||||
ADC1.NbrOfConversion=1
|
||||
ADC1.NbrOfConversionFlag=1
|
||||
ADC1.Rank-0\#ChannelRegularConversion=1
|
||||
ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
|
||||
ADC1.SamplingTime-1\#ChannelInjectedConversion=ADC_SAMPLETIME_3CYCLES
|
||||
ADC1.SamplingTime-2\#ChannelInjectedConversion=ADC_SAMPLETIME_3CYCLES
|
||||
ADC1.master=1
|
||||
ADC2.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_15
|
||||
ADC2.Channel-5\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||
ADC2.Channel-6\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||
ADC2.EOCSelection=ADC_EOC_SEQ_CONV
|
||||
ADC2.IPParameters=Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,NbrOfConversionFlag,InjNumberOfConversion,NbrOfConversion,Rank-5\#ChannelRegularConversion,Channel-5\#ChannelRegularConversion,SamplingTime-5\#ChannelRegularConversion,Rank-6\#ChannelRegularConversion,Channel-6\#ChannelRegularConversion,SamplingTime-6\#ChannelRegularConversion,EOCSelection
|
||||
ADC2.InjNumberOfConversion=0
|
||||
ADC2.NbrOfConversion=3
|
||||
ADC2.NbrOfConversionFlag=1
|
||||
ADC2.Rank-1\#ChannelRegularConversion=1
|
||||
ADC2.Rank-5\#ChannelRegularConversion=2
|
||||
ADC2.Rank-6\#ChannelRegularConversion=3
|
||||
ADC2.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
|
||||
ADC2.SamplingTime-5\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
|
||||
ADC2.SamplingTime-6\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
|
||||
CAN1.ABOM=ENABLE
|
||||
CAN1.BS1=CAN_BS1_7TQ
|
||||
CAN1.CalculateBaudRate=1000000
|
||||
|
@ -44,7 +36,7 @@ GPIO.groupedBy=Group By Peripherals
|
|||
KeepUserPlacement=false
|
||||
Mcu.CPN=STM32F446RET6
|
||||
Mcu.Family=STM32F4
|
||||
Mcu.IP0=ADC1
|
||||
Mcu.IP0=ADC2
|
||||
Mcu.IP1=CAN1
|
||||
Mcu.IP10=USART1
|
||||
Mcu.IP11=USART2
|
||||
|
@ -61,42 +53,40 @@ Mcu.Name=STM32F446R(C-E)Tx
|
|||
Mcu.Package=LQFP64
|
||||
Mcu.Pin0=PH0-OSC_IN
|
||||
Mcu.Pin1=PH1-OSC_OUT
|
||||
Mcu.Pin10=PB0
|
||||
Mcu.Pin11=PB1
|
||||
Mcu.Pin12=PB10
|
||||
Mcu.Pin13=PB12
|
||||
Mcu.Pin14=PB13
|
||||
Mcu.Pin15=PB14
|
||||
Mcu.Pin16=PB15
|
||||
Mcu.Pin17=PC6
|
||||
Mcu.Pin18=PA8
|
||||
Mcu.Pin19=PA9
|
||||
Mcu.Pin10=PB10
|
||||
Mcu.Pin11=PB12
|
||||
Mcu.Pin12=PB13
|
||||
Mcu.Pin13=PB14
|
||||
Mcu.Pin14=PB15
|
||||
Mcu.Pin15=PC6
|
||||
Mcu.Pin16=PA8
|
||||
Mcu.Pin17=PA9
|
||||
Mcu.Pin18=PA10
|
||||
Mcu.Pin19=PA11
|
||||
Mcu.Pin2=PC1
|
||||
Mcu.Pin20=PA10
|
||||
Mcu.Pin21=PA11
|
||||
Mcu.Pin22=PA12
|
||||
Mcu.Pin23=PA13
|
||||
Mcu.Pin24=PA14
|
||||
Mcu.Pin25=PC10
|
||||
Mcu.Pin26=PC11
|
||||
Mcu.Pin27=PC12
|
||||
Mcu.Pin28=PD2
|
||||
Mcu.Pin29=PB6
|
||||
Mcu.Pin20=PA12
|
||||
Mcu.Pin21=PA13
|
||||
Mcu.Pin22=PA14
|
||||
Mcu.Pin23=PC10
|
||||
Mcu.Pin24=PC11
|
||||
Mcu.Pin25=PC12
|
||||
Mcu.Pin26=PD2
|
||||
Mcu.Pin27=PB6
|
||||
Mcu.Pin28=PB7
|
||||
Mcu.Pin29=PB8
|
||||
Mcu.Pin3=PA0-WKUP
|
||||
Mcu.Pin30=PB7
|
||||
Mcu.Pin31=PB8
|
||||
Mcu.Pin32=PB9
|
||||
Mcu.Pin33=VP_SYS_VS_Systick
|
||||
Mcu.Pin34=VP_TIM1_VS_no_output4
|
||||
Mcu.Pin35=VP_TIM2_VS_ClockSourceINT
|
||||
Mcu.Pin36=VP_TIM3_VS_ClockSourceINT
|
||||
Mcu.Pin30=PB9
|
||||
Mcu.Pin31=VP_SYS_VS_Systick
|
||||
Mcu.Pin32=VP_TIM1_VS_no_output4
|
||||
Mcu.Pin33=VP_TIM2_VS_ClockSourceINT
|
||||
Mcu.Pin34=VP_TIM3_VS_ClockSourceINT
|
||||
Mcu.Pin4=PA1
|
||||
Mcu.Pin5=PA2
|
||||
Mcu.Pin6=PA3
|
||||
Mcu.Pin7=PA4
|
||||
Mcu.Pin8=PA5
|
||||
Mcu.Pin9=PA6
|
||||
Mcu.PinsNb=37
|
||||
Mcu.Pin7=PC5
|
||||
Mcu.Pin8=PB0
|
||||
Mcu.Pin9=PB1
|
||||
Mcu.PinsNb=35
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32F446RETx
|
||||
|
@ -111,6 +101,7 @@ NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
|||
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
|
||||
NVIC.SPI2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
|
||||
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false
|
||||
NVIC.TIM1_BRK_TIM9_IRQn=true\:1\:0\:true\:true\:true\:1\:true\:true\:true
|
||||
|
@ -143,15 +134,17 @@ PA2.Mode=Asynchronous
|
|||
PA2.Signal=USART2_TX
|
||||
PA3.Mode=Asynchronous
|
||||
PA3.Signal=USART2_RX
|
||||
PA4.Signal=ADCx_IN4
|
||||
PA5.Signal=ADCx_IN5
|
||||
PA6.Signal=ADCx_IN6
|
||||
PA8.Signal=S_TIM1_CH1
|
||||
PA9.Signal=S_TIM1_CH2
|
||||
PB0.GPIOParameters=GPIO_Label
|
||||
PB0.GPIO_Label=SENSE2
|
||||
PB0.Locked=true
|
||||
PB0.Signal=ADCx_IN8
|
||||
PB1.GPIOParameters=GPIO_Label
|
||||
PB1.GPIO_Label=SENSE1
|
||||
PB1.Locked=true
|
||||
PB1.Signal=ADCx_IN9
|
||||
PB10.Locked=true
|
||||
PB10.Mode=Full_Duplex_Master
|
||||
PB10.Signal=SPI2_SCK
|
||||
PB12.Mode=CAN_Activate
|
||||
|
@ -194,6 +187,10 @@ PC12.GPIOParameters=GPIO_Label
|
|||
PC12.GPIO_Label=LED3
|
||||
PC12.Locked=true
|
||||
PC12.Signal=GPIO_Output
|
||||
PC5.GPIOParameters=GPIO_Label
|
||||
PC5.GPIO_Label=SENSE3
|
||||
PC5.Locked=true
|
||||
PC5.Signal=ADCx_IN15
|
||||
PC6.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label
|
||||
PC6.GPIO_Label=EN_W
|
||||
PC6.GPIO_PuPd=GPIO_PULLDOWN
|
||||
|
@ -238,7 +235,7 @@ ProjectManager.StackSize=0x400
|
|||
ProjectManager.TargetToolchain=Other Toolchains (GPDSC)
|
||||
ProjectManager.ToolChainLocation=
|
||||
ProjectManager.UnderRoot=false
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-LL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_ADC1_Init-ADC1-false-LL-true,4-MX_TIM1_Init-TIM1-false-LL-true,5-MX_USART1_UART_Init-USART1-false-LL-true,6-MX_USART2_UART_Init-USART2-false-LL-true,7-MX_TIM2_Init-TIM2-false-LL-true,8-MX_SPI2_Init-SPI2-false-LL-true,9-MX_CAN1_Init-CAN1-false-HAL-true,10-MX_CAN2_Init-CAN2-false-HAL-true,11-MX_TIM3_Init-TIM3-false-HAL-true
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-LL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_TIM1_Init-TIM1-false-LL-true,4-MX_USART1_UART_Init-USART1-false-LL-true,5-MX_USART2_UART_Init-USART2-false-LL-true,6-MX_TIM2_Init-TIM2-false-LL-true,7-MX_SPI2_Init-SPI2-false-HAL-true,8-MX_CAN1_Init-CAN1-false-HAL-true,9-MX_CAN2_Init-CAN2-false-HAL-true,10-MX_TIM3_Init-TIM3-false-HAL-true,11-MX_ADC2_Init-ADC2-false-HAL-true
|
||||
RCC.AHBFreq_Value=180000000
|
||||
RCC.APB1CLKDivider=RCC_HCLK_DIV4
|
||||
RCC.APB1Freq_Value=45000000
|
||||
|
@ -284,15 +281,11 @@ RCC.VCOInputFreq_Value=2000000
|
|||
RCC.VCOOutputFreq_Value=360000000
|
||||
RCC.VCOSAIInputFreq_Value=500000
|
||||
RCC.VCOSAIOutputFreq_Value=96000000
|
||||
SH.ADCx_IN4.0=ADC1_IN4,IN4
|
||||
SH.ADCx_IN4.ConfNb=1
|
||||
SH.ADCx_IN5.0=ADC1_IN5,IN5
|
||||
SH.ADCx_IN5.ConfNb=1
|
||||
SH.ADCx_IN6.0=ADC1_IN6,IN6
|
||||
SH.ADCx_IN6.ConfNb=1
|
||||
SH.ADCx_IN8.0=ADC1_IN8,IN8
|
||||
SH.ADCx_IN15.0=ADC2_IN15,IN15
|
||||
SH.ADCx_IN15.ConfNb=1
|
||||
SH.ADCx_IN8.0=ADC2_IN8,IN8
|
||||
SH.ADCx_IN8.ConfNb=1
|
||||
SH.ADCx_IN9.0=ADC1_IN9,IN9
|
||||
SH.ADCx_IN9.0=ADC2_IN9,IN9
|
||||
SH.ADCx_IN9.ConfNb=1
|
||||
SH.S_TIM1_CH1.0=TIM1_CH1,PWM Generation1 CH1
|
||||
SH.S_TIM1_CH1.ConfNb=1
|
||||
|
@ -301,12 +294,12 @@ SH.S_TIM1_CH2.ConfNb=1
|
|||
SH.S_TIM1_CH3.0=TIM1_CH3,PWM Generation3 CH3
|
||||
SH.S_TIM1_CH3.ConfNb=1
|
||||
SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_64
|
||||
SPI2.CLKPhase=SPI_PHASE_2EDGE
|
||||
SPI2.CLKPhase=SPI_PHASE_1EDGE
|
||||
SPI2.CLKPolarity=SPI_POLARITY_LOW
|
||||
SPI2.CalculateBaudRate=703.125 KBits/s
|
||||
SPI2.DataSize=SPI_DATASIZE_16BIT
|
||||
SPI2.Direction=SPI_DIRECTION_2LINES
|
||||
SPI2.IPParameters=VirtualType,Mode,Direction,DataSize,CLKPolarity,CLKPhase,CalculateBaudRate,BaudRatePrescaler
|
||||
SPI2.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,CLKPhase,BaudRatePrescaler,CLKPolarity
|
||||
SPI2.Mode=SPI_MODE_MASTER
|
||||
SPI2.VirtualType=VM_MASTER
|
||||
TIM1.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
|
||||
|
@ -324,7 +317,7 @@ TIM1.TIM_MasterOutputTrigger=TIM_TRGO_OC4REF
|
|||
TIM2.IPParameters=TIM_MasterOutputTrigger
|
||||
TIM2.TIM_MasterOutputTrigger=TIM_TRGO_UPDATE
|
||||
TIM3.IPParameters=Period,Prescaler
|
||||
TIM3.Period=199
|
||||
TIM3.Period=99
|
||||
TIM3.Prescaler=89
|
||||
USART1.IPParameters=VirtualMode
|
||||
USART1.VirtualMode=VM_ASYNC
|
||||
|
|
48
vizualize.py
Normal file
48
vizualize.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
import serial
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.animation as animation
|
||||
import numpy as np
|
||||
|
||||
# Open the serial port. Replace 'COM3' with your serial port name.
|
||||
serial_port = '/dev/ttyUSB0'
|
||||
baud_rate = 115200 # Adjust as per your device configuration
|
||||
ser = serial.Serial(serial_port, baud_rate, timeout=1)
|
||||
|
||||
# Setup the matplotlib figure and axes for polar plot.
|
||||
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
|
||||
line, = ax.plot([], [], 'b-', lw=2) # Line object to update the angle
|
||||
ax.set_ylim(0, 1) # Set the radius range (fixed in this case)
|
||||
|
||||
# Adjust the orientation so that 0 is up and the angle proceeds clockwise.
|
||||
ax.set_theta_zero_location('N')
|
||||
ax.set_theta_direction(-1)
|
||||
|
||||
# Initialization function to clear the data.
|
||||
def init():
|
||||
line.set_data([], [])
|
||||
return (line,)
|
||||
|
||||
# Update function that reads from the serial port
|
||||
# and updates the plot with the new angle.
|
||||
def update(frame):
|
||||
line.set_data([], [])
|
||||
raw_data = ser.readline().strip()
|
||||
angle = 0
|
||||
if raw_data:
|
||||
try:
|
||||
angle = float(raw_data)
|
||||
except:
|
||||
angle = 0
|
||||
angle_rad = np.deg2rad(angle) # Convert to radians
|
||||
# Update the plot with the new angle.
|
||||
line.set_data([angle_rad, angle_rad], [0, 1])
|
||||
return (line,)
|
||||
|
||||
# Create the animation object with a suitable update interval (ms).
|
||||
ani = animation.FuncAnimation(fig, update, init_func=init, blit=True, interval=1)
|
||||
|
||||
# Start the plot animation
|
||||
plt.show()
|
||||
|
||||
# Closing the serial port after the plot is closed.
|
||||
ser.close()
|
Loading…
Add table
Add a link
Reference in a new issue