New to using HAL, and I'm having several issues with setting up the SPI.
I'm using SPI 1 on an STM32F429ZGT6.
Here's my setup:
SPI_HandleTypeDef SPI_1;
void SPI_INIT(void)
{
__HAL_RCC_SPI1_CLK_ENABLE();
SPI_1.Instance = SPI1;
SPI_1.Init.Mode = SPI_MODE_MASTER;
SPI_1.Init.Direction = SPI_DIRECTION_2LINES;
SPI_1.Init.DataSize = SPI_DATASIZE_8BIT;
SPI_1.Init.CLKPolarity = SPI_POLARITY_HIGH;
SPI_1.Init.CLKPhase = SPI_PHASE_2EDGE;
SPI_1.Init.NSS = SPI_NSS_SOFT;
SPI_1.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPI_1.Init.TIMode = SPI_TIMODE_DISABLE;
SPI_1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPI_1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
HAL_SPI_Init(&SPI_1);
}
static void ConfigGPIOA(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
static void ConfigGPIOE(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStruct.Pin = SPI_CS02_Pin|SPI_CS03_Pin|SPI_CS00_Pin|SPI_CS01_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
}
Just initializing SPI 1 and configuring GPIO Pins 5, 6 and 7 for SPI1. I set up GPIOE Pins 0, 1, 2 and 3 for a manual chip select, and I'm writing to them for an idle high (initializing them as pullup doesn't seem to work)
Here's my main:
int main(void)
{
HAL_Init();
SystemClock_Config();
GPIOInit();
SPI_INIT();
for(spibyte=0; spibyte < 4; spibyte++)
{
CS_LOW();
if(spibyte == 0)
{
HAL_SPI_Transmit(&SPI_1, (uint8_t *)0x51, 2, 10);
}
if(spibyte >= 1)
{
HAL_SPI_Transmit(&SPI_1, (uint8_t *)0xFF, 2, 10);
}
_DELAY(TDISCS);
CS_HIGH();
}
}
GPIOInit() runs both ConfigGPIOA and ConfigGPIOE.
CS_LOW() and CS_HIGH() are macros that just pull the chip select up and down. I've watched the chip select line on the oscilloscope and it does switch.
Project builds and loads onto the board, but the MOSI pin is silent, and putting the scope on the clock pin, the clock line is pulled up once, comes back down, and remains that way.
I will also note that the clock idles low, even though I've clearly set the clock polarity to high on my SPI initiailization.
Is there something I'm missing? I don't understand why the SPI lines are acting this way.
Related
I'm trying to make the crouching/standing-up smoother and slower, as currently crouching/uncrouching is so quick(I mean transition between crouching/uncrouching)
private void Start()
{
startYScale = transform.localScale.y;
mainCamera = Camera.main;
}
private void PlayerMovement()
{
currentSpeed = moveSpeed;
move = controls.Player.Movement.ReadValue<Vector2>();
movement = (move.y * transform.forward) + (move.x * transform.right);
if (isRunning)
{
//run = controls.Player.Run.ReadValue<float>();
currentSpeed = runSpeed;
Vector3 running = (move.y * transform.forward) + (move.x * transform.right);
controller.Move(running * currentSpeed * Time.deltaTime);
}
if (crouching)
{
currentSpeed = crouchSpeed;
isRunning = false;
transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
// controller.radius = 0.2f;
}
else
{
currentSpeed = moveSpeed;
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
}
controller.Move(movement * currentSpeed * Time.deltaTime);
}
How would I fix that? is there a way to make the transition slower and smoother? please help.
Edit:
I tried changing the position of the camera too when crouching to make the transition from standing up to crouching smoother.
private void FixedUpdate()
{
var desierdHeight = crouching ? crouchYScale : startYScale;
if (controller.height != desierdHeight)
{
AdjustHeight(desierdHeight);
/* var camPos = playerCamera.transform.position;
camPos.y = controller.height;
playerCamera.transform.position = camPos;*/
playerCamera.transform.localPosition = new Vector3(0,controller.height, 0);
}
}
private void AdjustHeight(float height)
{
float center = height / 2;
controller.height = Mathf.Lerp(controller.height, height, crouchSpeed);
controller.center = Vector3.Lerp(controller.center, new Vector3(0, center,0), crouchSpeed);
}
but the transition is still fast.
I'm facing an issue when I pick up an object in my game. Whenever I pick up an object and look around while holding the object, it stretches based on the perspective. Here is an example of an object before and after picking up:
Before picking up:
After picking up:
How can I maintain the object's scale and prevent it from stretching?
public void PickupObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
/* priorConstraints = pickupRB.constraints; // <--- NEW
pickupRB.constraints = RigidbodyConstraints.FreezeAll; // <--- NEW*/
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
pickupRB.isKinematic = true;
pickupRB.transform.parent = PickupParent.transform;
// pickupRB.isKinematic = true;
// StartCoroutine(physicsObject.PickUp());
}
The following code snippet is in Update() :
if (currentlyPickedUpObject != null)
{
HoldingItemIcon.SetActive(true);
InteractIcon.SetActive(false);
CenterIcon.SetActive(false);
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
HoldingItemIcon.SetActive(false);
InteractIcon.SetActive(false);
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
pickupRB.AddForce(camerDirection * 500);
currentlyPickedUpObject = null;
pickupRB.transform.parent = null;
}
Throw = false;
}
public void BreakConnection()
{
pickupRB.isKinematic = false;
pickupRB.transform.parent = null;
pickupRB.constraints = priorConstraints;
// pickupRB.constraints = RigidbodyConstraints.None;
currentlyPickedUpObject = null;
lookObject = null;
physicsObject.pickedUp = false;
currentDist = 0;
pickupRB.useGravity = true;
}
pickupParent's lossy scale while an object is picked:
What you can do is have an empty gameObject as a child of your player. That object is going to be the pickableObjectsParent and give this parent object the scale values of 1/(player's scale).
I'm trying to throw an object after it is being held. Here is how the pickup works:
public void PickupObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
priorConstraints = pickupRB.constraints;
pickupRB.constraints = RigidbodyConstraints.FreezeAll;
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
pickupRB.isKinematic = true;
pickupRB.transform.parent = PickupParent.transform;
// pickupRB.isKinematic = true;
StartCoroutine(physicsObject.PickUp());
}
in the update():
if (currentlyPickedUpObject != null)
{
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
Debug.Log("Object is being thrown");
pickupRB.AddForce(camerDirection * 100);
}
Throw = false;
}
as shown above I was trying to add force to the direction the player to be able to throw the item in that direction in this part:
Vector3 camerDirection = mainCamera.transform.forward;
if (Throw)
{
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
Debug.Log("Object is being thrown");
pickupRB.AddForce(camerDirection * 100);
}
Throw = false;
The line is printed in the console when the throw button is pressed but nothing happens. How to fix this?
I think you're throwing the object but you're not releasing it, so on the next frame the currentlyPickedUpObject is not null so you go right back to moving it with the PickupParent again. Try setting it to null inside your if (Throw) statement so you don't keep moving it:
if (currentlyPickedUpObject != null)
{
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
Debug.Log("Object is being thrown");
pickupRB.AddForce(camerDirection * 100);
currentlyPickedUpObject = null; // <--- NEW
}
Throw = false;
}
The problem is when you pick the object up you are setting the pickupRB kinematic with line
pickupRB.isKinematic = true
So to fix this, you should add
pickupRB.isKinematic = false
before you add force to the rigidbody.
I have a problem with the UART communication between an ESP32 and a STM32. Everything is working fine until I hardreset the ESP32 then I see a frame error on RX and in TX a shift in the response.
response. As soon as I hardreset the STM32 then everything works again and I see the same output as input. Can someone
help me?
#include "main.h"
#include "ProductMain.h"
#include <string.h>
#include "DebugOutput.h"
#include "GUI.h"
uint8_t UART7_rxBuffer[2] = {0};
uint8_t pass1 = 99;
uint8_t rfidDetected = 0;
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart7;
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_UART7_Init(void);
static void MX_USART1_UART_Init(void);
char spi_buf ='A';
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MX_UART7_Init();
MX_USART1_UART_Init();
Product_init();
HAL_UART_Receive_IT (&huart7, UART7_rxBuffer, 2);
HAL_UART_Transmit_IT(&huart7, UART7_rxBuffer, 2);
while (1)
{
Product_loop(pass1);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 192;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV8;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_PLLCLK, RCC_MCODIV_1);
}
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_UART7_Init(void)
{
huart7.Instance = UART7;
huart7.Init.BaudRate = 9600;
huart7.Init.WordLength = UART_WORDLENGTH_8B;
huart7.Init.StopBits = UART_STOPBITS_1;
huart7.Init.Parity = UART_PARITY_NONE;
huart7.Init.Mode = UART_MODE_TX_RX;
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart7) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, DISPLAY_RESET_Pin|DISPLAY_DATA_CMD_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI1_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(SPI1_CS_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = DISPLAY_RESET_Pin|DISPLAY_DATA_CMD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_InitStruct.Pin = DISPLAY_BUSY_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DISPLAY_BUSY_GPIO_Port, &GPIO_InitStruct);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit_IT(&huart7, UART7_rxBuffer, 2);
HAL_UART_Receive_IT(&huart7, UART7_rxBuffer, 2);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
Best regards
Framing Error
Shift
I am trying to run a code I found some time ago here, that allows to choose an ADC channel from a few channels and to read it individually when necessary. No DMA involved. I use STM32L4R5. My idea is just to confirm proper ADC reading by switching status LEDs on and off if some level is available and it is not working at all so far (status led for is always ON).
For input signal source I use a GPIO pin, set to high, that passes through a 1k resistor and a LED so the voltage is about 0.7V.
I call __HAL_RCC_ADC_CLK_ENABLE(); and __HAL_RCC_GPIOC_CLK_ENABLE(); in the HAL_ADC_MspInit().
status_led() function is not shown but I use it a lot, no problem with it.
The rest of the code is as follows:
uint32_t pdlvl=0;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM1_Init();
// Test ADC reading voltage input
//
while(1){
pdlvl=read_ADC_channel(ADC_CHANNEL_13);
if(pdlvl<10){
status_led(4,1);
status_led(3,0);
}
if(pdlvl>10){
status_led(3,1);
status_led(4,0);
}
}
} // end of main
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_13;
sConfig.Rank = ADC_RANK_NONE;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_14;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
Pin initialization
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = ROUT1_Pin|ROUT2_Pin; // ADC_CHANNEL_13 and ADC_CHANNEL_14
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
Now the tricky part with pre-config of channels:
void config_ADC_channel(uint32_t channel, uint8_t val)
{
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = channel;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
if(val==1){
sConfig.Rank = 1;
}
else{
sConfig.Rank = ADC_RANK_NONE;
}
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
and the reading function
uint32_t read_ADC_channel(uint32_t channel)
{
uint32_t digital_result;
config_ADC_channel(channel, 1);
// HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
digital_result = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
config_ADC_channel(channel, 0);
return digital_result;
}
Any ideas where I am wrong?
Thank you!
I found the problem. I did not set correctly the Periphery clock source (initially generated by CubeMX, and modified source by me later):
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSE;
PeriphClkInit.PLLSAI1.PLLSAI1M = 6;
PeriphClkInit.PLLSAI1.PLLSAI1N = 20;
PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV2;
PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;