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
133/**** I2C Parameters ****/
134#define I2C_DEV_ID 1 // I2C1
135#define I2C_MAX_SPEED_HZ 100000
136#define I2C_EXTRA_PARAM_PTR &i2c_extra_init_params
137/* I2C timing register value for standard mode of operation
138 * Check here for more understanding on I2C timing register
139 * configuration: https://wiki.analog.com/resources/no-os/drivers/i2c */
140#define I2C_TIMING 0x00000E14
141/**** End I2C Parameters ****/
142
143/**** I3C Parameters ****/
144#define I3C_DEV_ID 1 // I3C1
145#define I3C_HANDLE hi3c1
146#define I3C_CR_REG I3C_HANDLE.Instance->CR
147/**** End I3C Parameters ****/
148
149/**** UART Parameters ****/
150#define UART_MODULE 3 // UART3
151#define UART_HANDLE huart3
152#define UART_IRQ USART3_IRQn
153/**** End UART Parameters ****/
154
155/* Pin mapping for AD405X w.r.t Arduino Headers */
156#define CNV_PORT_NUM 0 // Unused
157#define CNV_PIN_NUM 0 // Unused
158#define GP0_PIN_NUM 15 // PD_15
159#define GP0_PORT_NUM 3 // PORTD = 3
160#define GP1_PIN_NUM 3 // PF_3
161#define GP1_PORT_NUM 5 // PORTF = 5
162#define STM32_GP1_IRQ EXTI3_IRQn
163
164/* Timer specific macros used for calculating pwm
165 * period and duty cycle */
166#define CNV_TIMER_BURST_AVG_PRESCALER 8
167#define CNV_TIMER_PRESCALER 1
168#define CNV_TIMER_CLK_DIVIDER 1
169
170/**** DMA Parameters ****/
171#define STM32_DMA_OPS stm32_gpdma_ops
172#define I3C_TX_DMA_CHANNEL_NUM (uint32_t)GPDMA1_Channel0
173#define I3C_RX_DMA_CHANNEL_NUM (uint32_t)GPDMA1_Channel1
174#define I3C_TX_DMA_HANDLE handle_GPDMA1_Channel0
175#define I3C_RX_DMA_HANDLE handle_GPDMA1_Channel1
176#define I3C_TX_DMA_IRQ_ID GPDMA1_Channel0_IRQn
177#define I3C_RX_DMA_IRQ_ID GPDMA1_Channel1_IRQn
178
179#define AD405x_TxDMA_HANDLE I3C_TX_DMA_HANDLE
180#define AD405x_RxDMA_HANDLE I3C_RX_DMA_HANDLE
181#define AD405x_TxDMA_CHANNEL_NUM I3C_TX_DMA_CHANNEL_NUM
182#define AD405x_RxDMA_CHANNEL_NUM I3C_RX_DMA_CHANNEL_NUM
183#define Tx_DMA_IRQ_ID I3C_TX_DMA_IRQ_ID
184#define Rx_DMA_IRQ_ID I3C_RX_DMA_IRQ_ID
185/**** End DMA Parameters ****/
186
187/**** Conversion Timer Parameters ****/
188#define CNV_TIMER_ID TIMER_ID(1)
189#define CNV_TIMER_CHANNEL TIMER_CHANNEL(1)
190#define CNV_TIMER_HANDLE &hlptim1
191#define CNV_TIMER_TYPE STM32_PWM_TIMER_LPTIM
192#define CNV_PWM_TIMER_IRQ_ID LPTIM1_IRQn
193/**** End Conversion Timer Parameters ****/
194
195#endif
196
197/* Redefine the init params structure mapping w.r.t. platform */
198#define uart_extra_init_params stm32_uart_extra_init_params
199#define vcom_extra_init_params stm32_vcom_extra_init_params
200#define i2c_extra_init_params stm32_i2c_extra_init_params
201#define cnv_extra_init_params stm32_gpio_cnv_extra_init_params
202#define pwm_extra_init_params stm32_pwm_cnv_extra_init_params
203#define pwm_gpio_extra_init_params stm32_pwm_gpio_extra_init_params
204#define gp0_extra_init_params stm32_gpio_gp0_extra_init_params
205#define gp1_extra_init_params stm32_gpio_gp1_extra_init_params
206#define trigger_gpio_irq_extra_params stm32_gpio_irq_extra_init_params
207#define dma_extra_init_params stm32_dma_extra_init_params
208#define cs_extra_init_params stm32_cs_extra_init_params
209#define tx_trigger_extra_init_params stm32_tx_trigger_extra_init_params
210#ifdef SPI_SUPPORT_AVAILABLE
211#define spi_extra_init_params stm32_spi_extra_init_params
212#endif
213#ifdef I3C_SUPPORT_AVAILABLE
214#define i3c_extra_init_params stm32_i3c_extra_init_params
215#endif
216
217/* Platform ops */
218#define gpio_ops stm32_gpio_ops
219#define i2c_ops stm32_i2c_ops
220#define uart_ops stm32_uart_ops
221#define vcom_ops stm32_usb_uart_ops
222#define pwm_ops stm32_pwm_ops
223#define trigger_gpio_irq_ops stm32_gpio_irq_ops
224#define dma_ops STM32_DMA_OPS
225#ifdef SPI_SUPPORT_AVAILABLE
226#define spi_ops stm32_spi_ops
227#endif
228#ifdef I3C_SUPPORT_AVAILABLE
229#define i3c_ops stm32_i3c_ops
230#endif
231
232#define MAX_SPI_SCLK 22500000
233#define MAX_SPI_SCLK_45MHz 45000000
234
235/* Define the max possible sampling (or output data) rate for a given platform.
236 * This is also used to find the time period to trigger a periodic conversion event.
237 * Note: Max possible ODR is 1000KSPS per channel for burst/continuous data capture on
238 * IIO client. This is derived by testing the firmware on STM32F469NI MCU @22Mhz SPI clock.
239 * The max possible ODR can vary from board to board and data continuity is not guaranteed
240 * above this ODR on IIO oscilloscope
241 */
242#if (APP_CAPTURE_MODE == WINDOWED_DATA_CAPTURE)
243#define SAMPLING_RATE_SPI_INTR 62500
244#else
245#define SAMPLING_RATE_SPI_INTR 30000 //Note: Can be set as high as 62500 with -O3 optimization on ARM GCC
246#endif
247#define CONV_TRIGGER_DUTY_CYCLE_NSEC(x) (x / 10)
248#define SAMPLING_RATE_SPI_DMA (1000000)
249#define SAMPLING_RATE_I3C_INTR (30000)
250#define SAMPLING_RATE_I3C_DMA (140000)
251
252/******************************************************************************/
253/********************** Public/Extern Declarations ****************************/
254/******************************************************************************/
255
256extern I2C_HandleTypeDef hi2c1;
257extern DMA_HandleTypeDef AD405x_TxDMA_HANDLE;
258extern DMA_HandleTypeDef AD405x_RxDMA_HANDLE;
259extern UART_HandleTypeDef UART_HANDLE;
260
261#ifdef STM32F469xx
262/* Conversion Pulse Timer */
263extern TIM_HandleTypeDef htim1;
264/* CS Pulse Timer */
265extern TIM_HandleTypeDef htim2;
266/* TX Trigger Pulse Timer */
267extern TIM_HandleTypeDef htim8;
268extern SPI_HandleTypeDef hspi1;
269extern USBD_HandleTypeDef hUsbDeviceHS;
270#endif
271
272#ifdef STM32H563xx
273extern LPTIM_HandleTypeDef hlptim1;
274extern I3C_HandleTypeDef I3C_HANDLE;
275extern PCD_HandleTypeDef hpcd_USB_DRD_FS;
276#endif
277
278extern volatile bool data_ready;
279
280extern struct stm32_uart_init_param stm32_uart_extra_init_params;
282#ifdef SPI_SUPPORT_AVAILABLE
283extern struct stm32_spi_init_param stm32_spi_extra_init_params;
284#endif
285#ifdef I3C_SUPPORT_AVAILABLE
286extern struct stm32_i3c_init_param stm32_i3c_extra_init_params;
287#endif
288extern struct stm32_i2c_init_param stm32_i2c_extra_init_params;
289extern struct stm32_gpio_init_param stm32_pwm_gpio_extra_init_params;
290extern struct stm32_gpio_init_param stm32_gpio_cnv_extra_init_params;
291extern struct stm32_gpio_init_param stm32_gpio_gp0_extra_init_params;
292extern struct stm32_gpio_init_param stm32_gpio_gp1_extra_init_params;
293extern struct stm32_gpio_init_param stm32_cs_pwm_gpio_extra_init_params;
294extern struct stm32_gpio_irq_init_param stm32_gpio_irq_extra_init_params;
295
296extern struct stm32_pwm_init_param stm32_pwm_cnv_extra_init_params;
297extern struct stm32_pwm_init_param stm32_dma_extra_init_params;
298extern struct stm32_pwm_init_param stm32_cs_extra_init_params;
299extern struct stm32_pwm_init_param stm32_tx_trigger_extra_init_params;
300extern struct stm32_dma_init_param stm32_spi_dma_extra_init_params;
301
302#ifdef SPI_SUPPORT_AVAILABLE
303extern struct stm32_dma_channel spi_dma_rxdma_channel;
304extern struct stm32_dma_channel spi_dma_txdma_channel;
305#endif
306#ifdef I3C_SUPPORT_AVAILABLE
307extern struct stm32_dma_channel i3c_dma_rxdma_channel;
308extern struct stm32_dma_channel i3c_dma_txdma_channel;
309#endif
310extern uint32_t rxdma_ndtr;
311extern volatile int dma_cycle_count;
312
313void stm32_system_init(void);
315void stm32_timer_enable(void);
316void stm32_timer_stop(void);
317void stm32_cs_output_gpio_config(bool is_gpio);
321void update_buff(uint8_t *local_buf, uint8_t *buf_start_addr);
322void halfcmplt_callback(DMA_HandleTypeDef * hdma);
323void receivecomplete_callback(DMA_HandleTypeDef * hdma);
324__weak unsigned int ux_device_stack_tasks_run(void);
325#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:785
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:682
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:46
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