precision-converters-firmware
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_spi.h"
26 #include "stm32_gpio.h"
27 #include "stm32_uart.h"
28 #include "stm32_dma.h"
29 #include "stm32_pwm.h"
30 #include "stm32_usb_uart.h"
31 
32 /******************************************************************************/
33 /********************** Macros and Constants Definition ***********************/
34 /******************************************************************************/
35 /* Note: The SDP-K1 board with the STM32F469NI MCU has been used
36 * for developing the firmware. The below parameters will change depending
37 * on the controller used. */
38 #define TARGET_NAME SDP_K1
39 
40 /* Pin mapping for AD405X w.r.t Arduino Headers */
41 #define I2C_DEV_ID 1 // I2C1
42 #define UART_MODULE 5 // UART5
43 #define UART_IRQ UART5_IRQn
44 #define SPI_DEVICE_ID 1 // SPI1
45 #define SPI_CS_PIN_NUM 15 // PA_15
46 #define STM32_SPI_CS_PORT_BASE GPIOA
47 #define STM32_SPI_CS_PORT_NUM 0 // PORTA = 0
48 #define CNV_PIN_NUM 10 // PA_10
49 #define CNV_PORT_NUM 0 // PORTA = 0
50 #define GP0_PIN_NUM 15 // PB_15
51 #define GP0_PORT_NUM 1 // PORTB = 1
52 #define GP1_PIN_NUM 11 // PG_11
53 #define GP1_PORT_NUM 6 // PORTG = 6
54 #define STM32_GP1_IRQ EXTI15_10_IRQn
55 #define STM32_DMA_CONT_HANDLE hdma_tim1_ch2
56 #define STM32_DMA_CONT_TRIGGER DMA2_Stream2_IRQn
57 #define STM32_DMA_SPI_RX_TRIGGER DMA2_Stream0_IRQn
58 
59 /* Timer specific macros used for calculating pwm
60  * period and duty cycle */
61 #if (ADC_CAPTURE_MODE == BURST_AVERAGING_MODE)
62 #define TIMER_1_PRESCALER 7
63 #define TIMER_2_PRESCALER 3
64 #else
65 #define TIMER_1_PRESCALER 1
66 #define TIMER_2_PRESCALER 0
67 #endif
68 #define TIMER_1_CLK_DIVIDER 2
69 #define TIMER_2_CLK_DIVIDER 2
70 
71 /* Timer Channels */
72 #define TIMER_CHANNEL_1 1
73 #define TIMER_CHANNEL_2 2
74 #define TIMER_CHANNEL_3 3
75 
76 #define TIMER_8_PRESCALER 0
77 #define TIMER_8_CLK_DIVIDER 2
78 
79 #define TRIGGER_GPIO_PORT 0 // Unused macro
80 #define TRIGGER_GPIO_PIN PWM_TRIGGER
81 
82 #define TIMER1_ID 1
83 #define TIMER2_ID 2
84 #define TIMER8_ID 8
85 
86 #define AD405x_DMA_NUM_CHANNELS 2
87 
88 #define AD405x_TxDMA_CHANNEL_NUM DMA_CHANNEL_7
89 #define AD405x_RxDMA_CHANNEL_NUM DMA_CHANNEL_3
90 #define Rx_DMA_IRQ_ID DMA2_Stream0_IRQn
91 
92 /* Redefine the init params structure mapping w.r.t. platform */
93 #define uart_extra_init_params stm32_uart_extra_init_params
94 #define spi_extra_init_params stm32_spi_extra_init_params
95 #define cnv_extra_init_params stm32_gpio_cnv_extra_init_params
96 #define pwm_extra_init_params stm32_pwm_cnv_extra_init_params
97 #define pwm_gpio_extra_init_params stm32_pwm_gpio_extra_init_params
98 #define gp0_extra_init_params stm32_gpio_gp0_extra_init_params
99 #define gp1_extra_init_params stm32_gpio_gp1_extra_init_params
100 #define trigger_gpio_irq_extra_params stm32_gpio_irq_extra_init_params
101 #define dma_extra_init_params stm32_dma_extra_init_params
102 #define cs_extra_init_params stm32_cs_extra_init_params
103 #define tx_trigger_extra_init_params stm32_tx_trigger_extra_init_params
104 #define vcom_extra_init_params stm32_vcom_extra_init_params
105 
106 /* Platform ops */
107 #define gpio_ops stm32_gpio_ops
108 #define spi_ops stm32_spi_ops
109 #define i2c_ops stm32_i2c_ops
110 #define uart_ops stm32_uart_ops
111 #define pwm_ops stm32_pwm_ops
112 #define trigger_gpio_irq_ops stm32_gpio_irq_ops
113 #define dma_ops stm32_dma_ops
114 #define vcom_ops stm32_usb_uart_ops
115 
116 #define MAX_SPI_SCLK 22500000
117 #define MAX_SPI_SCLK_45MHz 45000000
118 
119 /* Define the max possible sampling (or output data) rate for a given platform.
120  * This is also used to find the time period to trigger a periodic conversion event.
121  * Note: Max possible ODR is 1000KSPS per channel for burst/continuous data capture on
122  * IIO client. This is derived by testing the firmware on STM32F469NI MCU @22Mhz SPI clock.
123  * The max possible ODR can vary from board to board and data continuity is not guaranteed
124  * above this ODR on IIO oscilloscope
125  */
126 #if (INTERFACE_MODE == SPI_INTERRUPT)
127 #if (APP_CAPTURE_MODE == WINDOWED_DATA_CAPTURE)
128 #define SAMPLING_RATE 62500
129 #else
130 #define SAMPLING_RATE 30000 //Note: Can be set as high as 62500 with -O3 optimization on ARM GCC
131 #endif
132 #define CONV_TRIGGER_DUTY_CYCLE_NSEC(x) (x / 10)
133 #else
134 #define SAMPLING_RATE (1000000)
135 #define DMA_MSB_DUTY_CYCLE_NS 200
136 #define DMA_LSB_DUTY_CYCLE_NS 600
137 #define OUTPUT_COMPARE_DUTY_CYCLE_NS 200
138 #define CHIP_SELECT_DUTY_CYCLE_NS 800
139 #endif
140 #define CONV_TRIGGER_PERIOD_NSEC(x) (((float)(1.0 / x) * 1000000) * 1000)
141 
142 /******************************************************************************/
143 /********************** Public/Extern Declarations ****************************/
144 /******************************************************************************/
145 extern I2C_HandleTypeDef hi2c1;
146 extern SPI_HandleTypeDef hspi1;
147 extern DMA_HandleTypeDef hdma_spi1_rx;
148 extern TIM_HandleTypeDef htim1;
149 extern TIM_HandleTypeDef htim2;
150 extern DMA_HandleTypeDef hdma_tim1_ch3;
151 extern DMA_HandleTypeDef hdma_tim1_ch2;
152 extern DMA_HandleTypeDef hdma_tim8_ch1;
153 extern UART_HandleTypeDef huart5;
154 extern USBD_HandleTypeDef hUsbDeviceHS;
155 extern volatile bool data_ready;
156 
157 extern struct stm32_uart_init_param stm32_uart_extra_init_params;
158 extern struct stm32_usb_uart_init_param stm32_vcom_extra_init_params;
159 extern struct stm32_spi_init_param stm32_spi_extra_init_params;
160 extern struct stm32_gpio_init_param stm32_pwm_gpio_extra_init_params;
161 extern struct stm32_gpio_init_param stm32_gpio_cnv_extra_init_params;
162 extern struct stm32_gpio_init_param stm32_gpio_gp0_extra_init_params;
163 extern struct stm32_gpio_init_param stm32_gpio_gp1_extra_init_params;
164 extern struct stm32_gpio_init_param stm32_cs_pwm_gpio_extra_init_params;
165 extern struct stm32_gpio_irq_init_param stm32_gpio_irq_extra_init_params;
166 
167 extern struct stm32_pwm_init_param stm32_pwm_cnv_extra_init_params;
168 #if (INTERFACE_MODE == SPI_DMA)
169 extern struct stm32_pwm_init_param stm32_dma_extra_init_params;
170 extern struct stm32_pwm_init_param stm32_oc_extra_init_params;
171 extern struct stm32_pwm_init_param stm32_cs_extra_init_params;
172 extern struct stm32_pwm_init_param stm32_tx_trigger_extra_init_params;
173 extern volatile bool ad405x_conversion_flag;
174 extern struct stm32_dma_init_param stm32_spi_dma_extra_init_params;
175 extern struct stm32_dma_channel rxdma_channel;
176 extern struct stm32_dma_channel txdma_channel;
177 extern uint32_t rxdma_ndtr;
178 extern int dma_cycle_count;
179 extern uint32_t callback_count;
180 #endif
181 
182 void halfcmplt_callback(DMA_HandleTypeDef * hdma);
183 void update_buff(uint32_t* local_buf, uint32_t* buf_start_addr);
184 void stm32_system_init(void);
185 void stm32_timer_enable(void);
186 void stm32_timer_stop(void);
187 void stm32_cs_output_gpio_config(bool is_gpio);
188 void stm32_cnv_output_gpio_config(bool is_gpio);
189 void stm32_configure_spi_dma(struct no_os_spi_init_param* spi_init_par,
190  struct no_os_spi_desc* spi_desc, bool is_dma_mode);
191 int stm32_abort_dma_transfer(void);
192 void receivecomplete_callback(DMA_HandleTypeDef * hdma);
193 
194 #endif /* APP_CONFIG_STM32_H_ */
UART_HandleTypeDef huart5
SPI_HandleTypeDef hspi1
struct stm32_dma_channel txdma_channel
Definition: app_config_stm32.c:29
DMA_HandleTypeDef hdma_tim1_ch2
struct stm32_dma_init_param stm32_spi_dma_extra_init_params
TIM_HandleTypeDef htim1
TIM_HandleTypeDef htim2
DMA_HandleTypeDef hdma_tim8_ch1
struct stm32_pwm_init_param stm32_oc_extra_init_params
volatile bool data_ready
Definition: ad405x_iio.c:163
volatile bool ad405x_conversion_flag
Definition: app_config_stm32.c:164
struct stm32_dma_channel rxdma_channel
Definition: app_config_stm32.c:39
uint32_t rxdma_ndtr
Definition: app_config_stm32.c:175
USBD_HandleTypeDef hUsbDeviceHS
void stm32_cs_output_gpio_config(bool is_gpio)
Configures the chip select pin as output mode.
Definition: app_config_stm32.c:384
uint32_t callback_count
Definition: app_config_stm32.c:172
void stm32_system_init(void)
Initialize the STM32 system peripherals.
Definition: app_config_stm32.c:111
void halfcmplt_callback(DMA_HandleTypeDef *hdma)
Callback function to flag the capture of half the number of requested samples.
Definition: app_config_stm32.c:268
struct stm32_pwm_init_param stm32_cs_extra_init_params
Definition: app_config_stm32.c:133
void stm32_timer_enable(void)
Starts the timer signal generation for PWM and OC channels all at once.
Definition: app_config_stm32.c:303
struct stm32_gpio_init_param stm32_gpio_cnv_extra_init_params
Definition: app_config_stm32.c:66
void receivecomplete_callback(DMA_HandleTypeDef *hdma)
Callback function to flag the capture of number of requested samples.
Definition: app_config_stm32.c:345
struct stm32_pwm_init_param stm32_pwm_cnv_extra_init_params
Definition: app_config_stm32.c:118
DMA_HandleTypeDef hdma_tim1_ch3
int dma_cycle_count
Definition: app_config_stm32.c:169
struct stm32_pwm_init_param stm32_tx_trigger_extra_init_params
Definition: app_config_stm32.c:143
void stm32_cnv_output_gpio_config(bool is_gpio)
Configures the conversion pin as output mode.
Definition: app_config_stm32.c:403
struct stm32_pwm_init_param stm32_dma_extra_init_params
int stm32_abort_dma_transfer(void)
Abort ongoing SPI RX DMA transfer.
Definition: app_config_stm32.c:420
struct stm32_gpio_init_param stm32_pwm_gpio_extra_init_params
Definition: app_config_stm32.c:108
struct stm32_uart_init_param stm32_uart_extra_init_params
Definition: app_config_stm32.c:49
struct stm32_usb_uart_init_param stm32_vcom_extra_init_params
Definition: app_config_stm32.c:54
struct stm32_gpio_irq_init_param stm32_gpio_irq_extra_init_params
Definition: app_config_stm32.c:84
DMA_HandleTypeDef hdma_spi1_rx
void stm32_configure_spi_dma(struct no_os_spi_init_param *spi_init_par, struct no_os_spi_desc *spi_desc, bool is_dma_mode)
void stm32_timer_stop(void)
Stops generating timer signals.
Definition: app_config_stm32.c:324
void update_buff(uint32_t *local_buf, uint32_t *buf_start_addr)
Update buffer index.
Definition: app_config_stm32.c:289
struct stm32_spi_init_param stm32_spi_extra_init_params
Definition: app_config_stm32.c:59
struct stm32_gpio_init_param stm32_cs_pwm_gpio_extra_init_params
Definition: app_config_stm32.c:95
I2C_HandleTypeDef hi2c1
struct stm32_gpio_init_param stm32_gpio_gp0_extra_init_params
Definition: app_config_stm32.c:72
struct stm32_gpio_init_param stm32_gpio_gp1_extra_init_params
Definition: app_config_stm32.c:78
uint8_t local_buf[MAX_LOCAL_BUF_SIZE]
Definition: ad405x_iio.c:172