big refactoring and features added
This commit is contained in:
135
App/Devices/adc_mux.c
Normal file
135
App/Devices/adc_mux.c
Normal file
@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @file adc_mux.c
|
||||
* @brief External photodiode ADC and internal STM32 ADC helpers.
|
||||
*/
|
||||
|
||||
#include "adc_mux.h"
|
||||
|
||||
#include "board_handles.h"
|
||||
#include "main.h"
|
||||
|
||||
uint16_t adc_mux_read_external_channel(uint8_t channel_index)
|
||||
{
|
||||
uint16_t result = 0u;
|
||||
uint32_t delay_counter;
|
||||
|
||||
HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_RESET);
|
||||
for (delay_counter = 0u; delay_counter < 500u; ++delay_counter)
|
||||
{
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_SET);
|
||||
for (delay_counter = 0u; delay_counter < 500u; ++delay_counter)
|
||||
{
|
||||
}
|
||||
|
||||
if (channel_index == 1u)
|
||||
{
|
||||
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_RESET);
|
||||
for (delay_counter = 0u; delay_counter < 500u; ++delay_counter)
|
||||
{
|
||||
}
|
||||
|
||||
LL_SPI_Enable(SPI4);
|
||||
delay_counter = 0u;
|
||||
while ((!LL_SPI_IsActiveFlag_RXNE(SPI4)) && (delay_counter <= 1000u))
|
||||
{
|
||||
++delay_counter;
|
||||
}
|
||||
LL_SPI_Disable(SPI4);
|
||||
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET);
|
||||
result = LL_SPI_ReceiveData16(SPI4);
|
||||
}
|
||||
else if (channel_index == 2u)
|
||||
{
|
||||
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_RESET);
|
||||
for (delay_counter = 0u; delay_counter < 500u; ++delay_counter)
|
||||
{
|
||||
}
|
||||
|
||||
LL_SPI_Enable(SPI5);
|
||||
delay_counter = 0u;
|
||||
while ((!LL_SPI_IsActiveFlag_RXNE(SPI5)) && (delay_counter <= 1000u))
|
||||
{
|
||||
++delay_counter;
|
||||
}
|
||||
LL_SPI_Disable(SPI5);
|
||||
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET);
|
||||
result = LL_SPI_ReceiveData16(SPI5);
|
||||
}
|
||||
else if (channel_index == 3u)
|
||||
{
|
||||
HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_RESET);
|
||||
for (delay_counter = 0u; delay_counter < 500u; ++delay_counter)
|
||||
{
|
||||
}
|
||||
|
||||
LL_SPI_Enable(SPI4);
|
||||
delay_counter = 0u;
|
||||
while ((!LL_SPI_IsActiveFlag_RXNE(SPI4)) && (delay_counter <= 1000u))
|
||||
{
|
||||
++delay_counter;
|
||||
}
|
||||
LL_SPI_Disable(SPI4);
|
||||
HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET);
|
||||
result = LL_SPI_ReceiveData16(SPI4);
|
||||
}
|
||||
else if (channel_index == 4u)
|
||||
{
|
||||
HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_RESET);
|
||||
for (delay_counter = 0u; delay_counter < 500u; ++delay_counter)
|
||||
{
|
||||
}
|
||||
|
||||
LL_SPI_Enable(SPI5);
|
||||
delay_counter = 0u;
|
||||
while ((!LL_SPI_IsActiveFlag_RXNE(SPI5)) && (delay_counter <= 1000u))
|
||||
{
|
||||
++delay_counter;
|
||||
}
|
||||
LL_SPI_Disable(SPI5);
|
||||
HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET);
|
||||
result = LL_SPI_ReceiveData16(SPI5);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t adc_mux_process_internal_adc_step(uint8_t step)
|
||||
{
|
||||
uint16_t output = 0u;
|
||||
|
||||
switch (step)
|
||||
{
|
||||
case 0u:
|
||||
HAL_ADC_Start(&hadc1);
|
||||
break;
|
||||
case 1u:
|
||||
HAL_ADC_PollForConversion(&hadc1, 100u);
|
||||
output = HAL_ADC_GetValue(&hadc1);
|
||||
break;
|
||||
case 2u:
|
||||
HAL_ADC_Stop(&hadc1);
|
||||
break;
|
||||
case 3u:
|
||||
HAL_ADC_Start(&hadc3);
|
||||
break;
|
||||
case 4u:
|
||||
HAL_ADC_PollForConversion(&hadc3, 100u);
|
||||
output = HAL_ADC_GetValue(&hadc3);
|
||||
break;
|
||||
case 5u:
|
||||
HAL_ADC_Stop(&hadc3);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
Reference in New Issue
Block a user