precision-converters-firmware
Loading...
Searching...
No Matches
app_config_stm32.h
Go to the documentation of this file.
1/***************************************************************************/
13#ifndef APP_CONFIG_STM32_H_
14#define APP_CONFIG_STM32_H_
15
16/******************************************************************************/
17/***************************** Include Files **********************************/
18/******************************************************************************/
19#include <stdint.h>
20
21#include "stm32_hal.h"
22#include "stm32_i2c.h"
23#include "stm32_irq.h"
24#include "stm32_gpio_irq.h"
25#include "stm32_gpio.h"
26#include "stm32_uart.h"
27#include "stm32_dma.h"
28#include "stm32_pwm.h"
29
30#ifdef STM32F469xx
31#include "stm32_spi.h"
32#include "stm32_usb_uart.h"
33#endif
34#ifdef STM32H563xx
35#include "stm32_i3c.h"
36#include "stm32_usb_uart.h"
37#endif
38
39/******************************************************************************/
40/********************** Macros and Constants Definition ***********************/
41/******************************************************************************/
42
43/* Timer IDs */
44#define TIMER_ID(x) x
45/* Timer Channels */
46#define TIMER_CHANNEL(x) x
47
48/* Total number of DMA channels necessary */
49#define AD405x_DMA_NUM_CHANNELS 2
50
51#ifdef STM32F469xx
52
53/* Note: The SDP-K1 board with the STM32F469NI MCU has been used
54* for developing the firmware. The below parameters will change depending
55* on the controller used. */
56#define TARGET_NAME SDP_K1
57
58/* Supports available in the platform */
59#define SPI_SUPPORT_AVAILABLE
60#define SDRAM_SUPPORT_AVAILABLE
61#define CONSOLE_STDIO_PORT_AVAILABLE
62
63#define I2C_EXTRA_PARAM_PTR NULL
64#define I2C_MAX_SPEED_HZ 100000
65
66/* Pin mapping for AD405X w.r.t Arduino Headers */
67#define I2C_DEV_ID 1 // I2C1
68#define UART_MODULE 5 // UART5
69#define UART_HANDLE huart5
70#define UART_IRQ UART5_IRQn
71#define SPI_DEVICE_ID 1 // SPI1
72#define SPI_CS_PIN_NUM 15 // PA_15
73#define SPI_CS_PORT_NUM 0 // PORTA = 0
74#define CNV_PIN_NUM 10 // PA_10
75#define CNV_PORT_NUM 0 // PORTA = 0
76#define GP0_PIN_NUM 15 // PB_15
77#define GP0_PORT_NUM 1 // PORTB = 1
78#define GP1_PIN_NUM 11 // PG_11
79#define GP1_PORT_NUM 6 // PORTG = 6
80#define STM32_GP1_IRQ EXTI15_10_IRQn
81#define STM32_DMA_CONT_TRIGGER DMA2_Stream2_IRQn
82#define STM32_DMA_SPI_RX_TRIGGER DMA2_Stream0_IRQn
83
84/* Timer specific macros used for calculating pwm
85 * period and duty cycle */
86#define CNV_TIMER_BURST_AVG_PRESCALER 7
87#define CNV_TIMER_PRESCALER 1
88#define CS_TIMER_PRESCALER 0
89#define TX_TRIGGER_TIMER_PRESCALER 0
90#define CNV_TIMER_CLK_DIVIDER 2
91#define CS_TIMER_CLK_DIVIDER 2
92#define TX_TRIGGER_TIMER_CLK_DIVIDER 2
93
94/* Timer Channels */
95#define CNV_TIMER_CHANNEL TIMER_CHANNEL(3)
96#define CS_TIMER_CHANNEL TIMER_CHANNEL(1)
97#define TX_TRIGGER_TIMER_CHANNEL TIMER_CHANNEL(1)
98
99#define CNV_TIMER_ID TIMER_ID(1)
100#define CS_TIMER_ID TIMER_ID(2)
101#define TX_TRIGGER_TIMER_ID TIMER_ID(8)
102
103#define CNV_TIMER_HANDLE &htim1
104#define CS_TIMER_HANDLE &htim2
105#define TX_TRIGGER_TIMER_HANDLE &htim8
106
107#define CNV_TIMER_TYPE STM32_PWM_TIMER_TIM
108#define CS_TIMER_TYPE STM32_PWM_TIMER_TIM
109#define TX_TRIGGER_TIMER_TYPE STM32_PWM_TIMER_TIM
110
111#define CNV_PWM_TIMER_IRQ_ID 0
112
113#define STM32_DMA_OPS stm32_dma_ops
114
115#define AD405x_TxDMA_HANDLE hdma_tim8_ch1
116#define AD405x_RxDMA_HANDLE hdma_spi1_rx
117#define AD405x_TxDMA_CHANNEL_NUM DMA_CHANNEL_7
118#define AD405x_RxDMA_CHANNEL_NUM DMA_CHANNEL_3
119#define Rx_DMA_IRQ_ID DMA2_Stream0_IRQn
120
121#endif
122
123#ifdef STM32H563xx
124
125/* Note: The NUCLEO-H563ZI board with the STM32H563ZI MCU has been used
126* for developing the firmware. The below parameters will change depending
127* on the controller used. */
128#define TARGET_NAME NUCLEO-H563ZI
129
130/* Supports available in the platform */
131#define I3C_SUPPORT_AVAILABLE
132#define CONSOLE_STDIO_PORT_AVAILABLE
133
134/**** I2C Parameters ****/
135#define I2C_DEV_ID 1 // I2C1
136#define I2C_MAX_SPEED_HZ 100000
137#define I2C_EXTRA_PARAM_PTR &i2c_extra_init_params
138/* I2C timing register value for standard mode of operation
139 * Check here for more understanding on I2C timing register
140 * configuration: https://wiki.analog.com/resources/no-os/drivers/i2c */
141#define I2C_TIMING 0x00000E14
142/**** End I2C Parameters ****/
143
144/**** I3C Parameters ****/
145#define I3C_DEV_ID 1 // I3C1
146#define I3C_HANDLE hi3c1
147#define I3C_CR_REG I3C_HANDLE.Instance->CR
148/**** End I3C Parameters ****/
149
150/**** UART Parameters ****/
151#define UART_MODULE 3 // UART3
152#define UART_HANDLE huart3
153#define UART_IRQ USART3_IRQn
154/**** End UART Parameters ****/
155
156/* Pin mapping for AD405X w.r.t Arduino Headers */
157#define CNV_PORT_NUM 0 // Unused
158#define CNV_PIN_NUM 0 // Unused
159#define GP0_PIN_NUM 15 // PD_15
160#define GP0_PORT_NUM 3 // PORTD = 3
161#define GP1_PIN_NUM 3 // PF_3
162#define GP1_PORT_NUM 5 // PORTF = 5
163#define STM32_GP1_IRQ EXTI3_IRQn
164
165/* Timer specific macros used for calculating pwm
166 * period and duty cycle */
167#define CNV_TIMER_BURST_AVG_PRESCALER 8
168#define CNV_TIMER_PRESCALER 1
169#define CNV_TIMER_CLK_DIVIDER 1
170
171/**** DMA Parameters ****/
172#define STM32_DMA_OPS stm32_gpdma_ops
173#define I3C_TX_DMA_CHANNEL_NUM (uint32_t)GPDMA1_Channel0
174#define I3C_RX_DMA_CHANNEL_NUM (uint32_t)GPDMA1_Channel1
175#define I3C_TX_DMA_HANDLE handle_GPDMA1_Channel0
176#define I3C_RX_DMA_HANDLE handle_GPDMA1_Channel1
177#define I3C_TX_DMA_IRQ_ID GPDMA1_Channel0_IRQn
178#define I3C_RX_DMA_IRQ_ID GPDMA1_Channel1_IRQn
179
180#define AD405x_TxDMA_HANDLE I3C_TX_DMA_HANDLE
181#define AD405x_RxDMA_HANDLE I3C_RX_DMA_HANDLE
182#define AD405x_TxDMA_CHANNEL_NUM I3C_TX_DMA_CHANNEL_NUM
183#define AD405x_RxDMA_CHANNEL_NUM I3C_RX_DMA_CHANNEL_NUM
184#define Tx_DMA_IRQ_ID I3C_TX_DMA_IRQ_ID
185#define Rx_DMA_IRQ_ID I3C_RX_DMA_IRQ_ID
186/**** End DMA Parameters ****/
187
188/**** Conversion Timer Parameters ****/
189#define CNV_TIMER_ID TIMER_ID(1)
190#define CNV_TIMER_CHANNEL TIMER_CHANNEL(1)
191#define CNV_TIMER_HANDLE &hlptim1
192#define CNV_TIMER_TYPE STM32_PWM_TIMER_LPTIM
193#define CNV_PWM_TIMER_IRQ_ID LPTIM1_IRQn
194/**** End Conversion Timer Parameters ****/
195
196#endif
197
198/* Redefine the init params structure mapping w.r.t. platform */
199#define uart_extra_init_params stm32_uart_extra_init_params
200#define vcom_extra_init_params stm32_vcom_extra_init_params
201#define i2c_extra_init_params stm32_i2c_extra_init_params
202#define cnv_extra_init_params stm32_gpio_cnv_extra_init_params
203#define pwm_extra_init_params stm32_pwm_cnv_extra_init_params
204#define pwm_gpio_extra_init_params stm32_pwm_gpio_extra_init_params
205#define gp0_extra_init_params stm32_gpio_gp0_extra_init_params
206#define gp1_extra_init_params stm32_gpio_gp1_extra_init_params
207#define trigger_gpio_irq_extra_params stm32_gpio_irq_extra_init_params
208#define dma_extra_init_params stm32_dma_extra_init_params
209#define cs_extra_init_params stm32_cs_extra_init_params
210#define tx_trigger_extra_init_params stm32_tx_trigger_extra_init_params
211#ifdef SPI_SUPPORT_AVAILABLE
212#define spi_extra_init_params stm32_spi_extra_init_params
213#endif
214#ifdef I3C_SUPPORT_AVAILABLE
215#define i3c_extra_init_params stm32_i3c_extra_init_params
216#endif
217
218/* Platform ops */
219#define gpio_ops stm32_gpio_ops
220#define i2c_ops stm32_i2c_ops
221#define uart_ops stm32_uart_ops
222#define vcom_ops stm32_usb_uart_ops
223#define pwm_ops stm32_pwm_ops
224#define trigger_gpio_irq_ops stm32_gpio_irq_ops
225#define dma_ops STM32_DMA_OPS
226#ifdef SPI_SUPPORT_AVAILABLE
227#define spi_ops stm32_spi_ops
228#endif
229#ifdef I3C_SUPPORT_AVAILABLE
230#define i3c_ops stm32_i3c_ops
231#endif
232
233#define MAX_SPI_SCLK 22500000
234#define MAX_SPI_SCLK_45MHz 45000000
235
236/* Define the max possible sampling (or output data) rate for a given platform.
237 * This is also used to find the time period to trigger a periodic conversion event.
238 * Note: Max possible ODR is 1000KSPS per channel for burst/continuous data capture on
239 * IIO client. This is derived by testing the firmware on STM32F469NI MCU @22Mhz SPI clock.
240 * The max possible ODR can vary from board to board and data continuity is not guaranteed
241 * above this ODR on IIO oscilloscope
242 */
243#if (APP_CAPTURE_MODE == WINDOWED_DATA_CAPTURE)
244#define SAMPLING_RATE_SPI_INTR 62500
245#else
246#define SAMPLING_RATE_SPI_INTR 30000 //Note: Can be set as high as 62500 with -O3 optimization on ARM GCC
247#endif
248#define CONV_TRIGGER_DUTY_CYCLE_NSEC(x) (x / 10)
249#define SAMPLING_RATE_SPI_DMA (1000000)
250#define SAMPLING_RATE_I3C_INTR (30000)
251#define SAMPLING_RATE_I3C_DMA (140000)
252
253/******************************************************************************/
254/********************** Public/Extern Declarations ****************************/
255/******************************************************************************/
256
257extern I2C_HandleTypeDef hi2c1;
258extern DMA_HandleTypeDef AD405x_TxDMA_HANDLE;
259extern DMA_HandleTypeDef AD405x_RxDMA_HANDLE;
260extern UART_HandleTypeDef UART_HANDLE;
261
262#ifdef STM32F469xx
263/* Conversion Pulse Timer */
264extern TIM_HandleTypeDef htim1;
265/* CS Pulse Timer */
266extern TIM_HandleTypeDef htim2;
267/* TX Trigger Pulse Timer */
268extern TIM_HandleTypeDef htim8;
269extern SPI_HandleTypeDef hspi1;
270extern USBD_HandleTypeDef hUsbDeviceHS;
271#endif
272
273#ifdef STM32H563xx
274extern LPTIM_HandleTypeDef hlptim1;
275extern I3C_HandleTypeDef I3C_HANDLE;
276extern PCD_HandleTypeDef hpcd_USB_DRD_FS;
277#endif
278
279extern volatile bool data_ready;
280
281extern struct stm32_uart_init_param stm32_uart_extra_init_params;
283#ifdef SPI_SUPPORT_AVAILABLE
284extern struct stm32_spi_init_param stm32_spi_extra_init_params;
285#endif
286#ifdef I3C_SUPPORT_AVAILABLE
287extern struct stm32_i3c_init_param stm32_i3c_extra_init_params;
288#endif
289extern struct stm32_i2c_init_param stm32_i2c_extra_init_params;
290extern struct stm32_gpio_init_param stm32_pwm_gpio_extra_init_params;
291extern struct stm32_gpio_init_param stm32_gpio_cnv_extra_init_params;
292extern struct stm32_gpio_init_param stm32_gpio_gp0_extra_init_params;
293extern struct stm32_gpio_init_param stm32_gpio_gp1_extra_init_params;
294extern struct stm32_gpio_init_param stm32_cs_pwm_gpio_extra_init_params;
295extern struct stm32_gpio_irq_init_param stm32_gpio_irq_extra_init_params;
296
297extern struct stm32_pwm_init_param stm32_pwm_cnv_extra_init_params;
298extern struct stm32_pwm_init_param stm32_dma_extra_init_params;
299extern struct stm32_pwm_init_param stm32_cs_extra_init_params;
300extern struct stm32_pwm_init_param stm32_tx_trigger_extra_init_params;
301extern struct stm32_dma_init_param stm32_spi_dma_extra_init_params;
302
303#ifdef SPI_SUPPORT_AVAILABLE
304extern struct stm32_dma_channel spi_dma_rxdma_channel;
305extern struct stm32_dma_channel spi_dma_txdma_channel;
306#endif
307#ifdef I3C_SUPPORT_AVAILABLE
308extern struct stm32_dma_channel i3c_dma_rxdma_channel;
309extern struct stm32_dma_channel i3c_dma_txdma_channel;
310#endif
311extern uint32_t rxdma_ndtr;
312extern volatile int dma_cycle_count;
313
314void stm32_system_init(void);
316void stm32_timer_enable(void);
317void stm32_timer_stop(void);
318void stm32_cs_output_gpio_config(bool is_gpio);
322void update_buff(uint8_t *local_buf, uint8_t *buf_start_addr);
323void halfcmplt_callback(DMA_HandleTypeDef * hdma);
324void receivecomplete_callback(DMA_HandleTypeDef * hdma);
325__weak unsigned int ux_device_stack_tasks_run(void);
326#endif /* APP_CONFIG_STM32_H_ */
int stm32_timer_stop(void)
Stop generating timer signals.
Definition app_config_stm32.c:281
int stm32_timer_enable(void)
Starts the timer signal generation for PWM.
Definition app_config_stm32.c:258
USBD_HandleTypeDef hUsbDeviceHS
void stm32_system_init(void)
Initialize the STM32 system peripherals.
Definition app_config_stm32.c:176
void receivecomplete_callback(DMA_HandleTypeDef *hdma)
Callback function to flag the transfer of number of requested samples.
Definition app_config_stm32.c:340
struct stm32_pwm_init_param stm32_tx_trigger_extra_init_params
Definition app_config_stm32.c:99
int stm32_abort_dma_transfer(void)
Abort DMA Transfers.
Definition app_config_stm32.c:315
struct stm32_usb_uart_init_param stm32_vcom_extra_init_params
Definition app_config_stm32.c:38
SPI_HandleTypeDef hspi1
void stm32_system_init_post_verification(void)
Initialize the STM32 system peripherals after the device has been verified.
Definition app_config_stm32.c:462
__weak unsigned int ux_device_stack_tasks_run(void)
Dummy function for USBx middleware used in STM32H563.
Definition app_config_stm32.c:786
DMA_HandleTypeDef AD405x_TxDMA_HANDLE
struct stm32_dma_init_param stm32_spi_dma_extra_init_params
struct stm32_i2c_init_param stm32_i2c_extra_init_params
Definition app_config_stm32.c:152
volatile int dma_cycle_count
Definition app_config_stm32.c:242
volatile bool data_ready
Definition ad405x_iio.c:137
UART_HandleTypeDef UART_HANDLE
uint32_t rxdma_ndtr
Definition app_config_stm32.c:245
void update_buff(uint8_t *local_buf, uint8_t *buf_start_addr)
Update buffer index.
Definition app_config_stm32.c:499
void stm32_cs_output_gpio_config(bool is_gpio)
Configures the chip select pin as output mode.
Definition app_config_stm32.c:309
void stm32_config_spi_data_frame_format(bool is_16_bit)
void halfcmplt_callback(DMA_HandleTypeDef *hdma)
Callback function to flag the capture of Half the number of requested samples.
Definition app_config_stm32.c:389
struct stm32_pwm_init_param stm32_cs_extra_init_params
Definition app_config_stm32.c:120
struct stm32_gpio_init_param stm32_gpio_cnv_extra_init_params
Definition app_config_stm32.c:64
struct stm32_pwm_init_param stm32_pwm_cnv_extra_init_params
Definition app_config_stm32.c:190
struct stm32_pwm_init_param stm32_dma_extra_init_params
struct stm32_gpio_init_param stm32_pwm_gpio_extra_init_params
Definition app_config_stm32.c:180
struct stm32_uart_init_param stm32_uart_extra_init_params
Definition app_config_stm32.c:102
struct stm32_gpio_irq_init_param stm32_gpio_irq_extra_init_params
Definition app_config_stm32.c:156
void stm32_config_cnv_prescalar(void)
Configures the prescalar according to the operating mode.
Definition app_config_stm32.c:683
DMA_HandleTypeDef AD405x_RxDMA_HANDLE
struct stm32_gpio_init_param stm32_cs_pwm_gpio_extra_init_params
Definition app_config_stm32.c:167
I2C_HandleTypeDef hi2c1
struct stm32_gpio_init_param stm32_gpio_gp0_extra_init_params
Definition app_config_stm32.c:144
struct stm32_gpio_init_param stm32_gpio_gp1_extra_init_params
Definition app_config_stm32.c:150
uint8_t local_buf[MAX_LOCAL_BUF_SIZE]
Definition ad405x_support.c:42
struct stm32_spi_init_param stm32_spi_extra_init_params
Definition app_config_stm32.c:43
TIM_HandleTypeDef htim1
TIM_HandleTypeDef htim2
TIM_HandleTypeDef htim8
VCOM driver for stm32 as a no_os_uart implementation.
Specific initialization parameters for stm32 UART over USB.
Definition stm32_usb_uart.h:27