Skip to content

Commit

Permalink
Moved X12S ADC driver to HAL/LL
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Feb 13, 2023
1 parent 22752f8 commit 737f473
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 49 deletions.
1 change: 1 addition & 0 deletions radio/src/targets/common/arm/stm32/f4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f4xx_ll_rcc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_exti.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_adc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_spi.c
# HAL drivers
${STM32CUBE_DIR}/Src/stm32f4xx_hal.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_pwr.c
Expand Down
1 change: 1 addition & 0 deletions radio/src/targets/common/arm/stm32/stm32_hal_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h"
#elif defined(STM32F2)

// Workaround to prevent the other CMSIS header to be included
Expand Down
13 changes: 5 additions & 8 deletions radio/src/targets/horus/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,12 @@
#define ADC_RCC_APB1Periph 0
#define ADC_RCC_APB2Periph (RCC_APB2Periph_SPI4)
#define ADC_SPI SPI4
#define ADC_GPIO_AF GPIO_AF_SPI4
#define ADC_GPIO_AF LL_GPIO_AF_5
#define ADC_SPI_GPIO GPIOE
#define ADC_SPI_PIN_SCK GPIO_Pin_2 // PE.02
#define ADC_SPI_PIN_CS GPIO_Pin_4 // PE.04
#define ADC_SPI_PIN_MOSI GPIO_Pin_6 // PE.06
#define ADC_SPI_PIN_MISO GPIO_Pin_5 // PE.05
#define ADC_SPI_PinSource_SCK GPIO_PinSource2
#define ADC_SPI_PinSource_MISO GPIO_PinSource5
#define ADC_SPI_PinSource_MOSI GPIO_PinSource6
#define ADC_SPI_PIN_SCK LL_GPIO_PIN_2 // PE.02
#define ADC_SPI_PIN_CS LL_GPIO_PIN_4 // PE.04
#define ADC_SPI_PIN_MOSI LL_GPIO_PIN_6 // PE.06
#define ADC_SPI_PIN_MISO LL_GPIO_PIN_5 // PE.05

#define ADC_SPI_STICK_LH 0
#define ADC_SPI_STICK_LV 1
Expand Down
78 changes: 37 additions & 41 deletions radio/src/targets/horus/x12s_adc_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

#include "adc_driver.h"
#include "x12s_adc_driver.h"
#include "delays_driver.h"

#include "stm32_hal_ll.h"
#include "hal.h"

#include "opentx.h"

#define ADC_CS_HIGH() LL_GPIO_SetOutputPin(ADC_SPI_GPIO, ADC_SPI_PIN_CS)
#define ADC_CS_LOW() LL_GPIO_ResetOutputPin(ADC_SPI_GPIO, ADC_SPI_PIN_CS)
Expand All @@ -33,51 +36,44 @@

uint16_t SPIx_ReadWriteByte(uint16_t value)
{
while (SPI_I2S_GetFlagStatus(ADC_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(ADC_SPI, value);
while (!LL_SPI_IsActiveFlag_TXE(ADC_SPI));
LL_SPI_TransmitData16(ADC_SPI, value);

while (SPI_I2S_GetFlagStatus(ADC_SPI, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(ADC_SPI);
while (!LL_SPI_IsActiveFlag_RXNE(ADC_SPI));
return LL_SPI_ReceiveData16(ADC_SPI);
}

static void ADS7952_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;

GPIO_InitStructure.GPIO_Pin = ADC_SPI_PIN_MISO | ADC_SPI_PIN_SCK | ADC_SPI_PIN_MOSI;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(ADC_SPI_GPIO, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = ADC_SPI_PIN_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(ADC_SPI_GPIO, &GPIO_InitStructure);

GPIO_PinAFConfig(ADC_SPI_GPIO, ADC_SPI_PinSource_SCK, ADC_GPIO_AF);
GPIO_PinAFConfig(ADC_SPI_GPIO, ADC_SPI_PinSource_MISO, ADC_GPIO_AF);
GPIO_PinAFConfig(ADC_SPI_GPIO, ADC_SPI_PinSource_MOSI, ADC_GPIO_AF);

SPI_I2S_DeInit(ADC_SPI);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(ADC_SPI, &SPI_InitStructure);
SPI_Cmd(ADC_SPI, ENABLE);
SPI_I2S_ITConfig(ADC_SPI, SPI_I2S_IT_TXE, DISABLE);
SPI_I2S_ITConfig(ADC_SPI, SPI_I2S_IT_RXNE, DISABLE);
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);

pinInit.Pin = ADC_SPI_PIN_MISO | ADC_SPI_PIN_SCK | ADC_SPI_PIN_MOSI;
pinInit.Mode = LL_GPIO_MODE_ALTERNATE;
pinInit.Alternate = ADC_GPIO_AF;
LL_GPIO_Init(ADC_SPI_GPIO, &pinInit);

pinInit.Pin = ADC_SPI_PIN_CS;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.Alternate = LL_GPIO_AF_0;
LL_GPIO_Init(ADC_SPI_GPIO, &pinInit);

LL_SPI_DeInit(ADC_SPI);

LL_SPI_InitTypeDef spiInit;
LL_SPI_StructInit(&spiInit);

spiInit.TransferDirection = LL_SPI_FULL_DUPLEX;
spiInit.Mode = LL_SPI_MODE_MASTER;
spiInit.DataWidth = LL_SPI_DATAWIDTH_16BIT;
spiInit.NSS = LL_SPI_NSS_SOFT;
spiInit.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
LL_SPI_Init(ADC_SPI, &spiInit);

LL_SPI_Enable(ADC_SPI);

LL_SPI_DisableIT_TXE(ADC_SPI);
LL_SPI_DisableIT_RXNE(ADC_SPI);

ADC_CS_HIGH();
delay_01us(1);
Expand Down

0 comments on commit 737f473

Please sign in to comment.