2 **************************************************************************
3 * @file at32f435_437_qspi.c
6 * @brief contain all the functions for qspi firmware library
7 **************************************************************************
8 * Copyright notice & Disclaimer
10 * The software Board Support Package (BSP) that is made available to
11 * download from Artery official website is the copyrighted work of Artery.
12 * Artery authorizes customers to use, copy, and distribute the BSP
13 * software and its related documentation for the purpose of design and
14 * development in conjunction with Artery microcontrollers. Use of the
15 * software is governed by this copyright notice and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
18 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
19 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
20 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
21 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 **************************************************************************
27 #include "at32f435_437_conf.h"
29 /** @addtogroup AT32F435_437_periph_driver
34 * @brief QSPI driver modules
38 #ifdef QSPI_MODULE_ENABLED
40 /** @defgroup QSPI_private_functions
45 * @brief enable/disable encryption for qspi.
46 * @note the function must be configured only when qspi in command-port mode!!!
47 * @param qspi_x: select the qspi peripheral.
48 * this parameter can be one of the following values:
50 * @param new_state (TRUE or FALSE)
53 void qspi_encryption_enable(qspi_type
* qspi_x
, confirm_state new_state
)
55 qspi_x
->ctrl_bit
.keyen
= new_state
;
59 * @brief set qspi sck mode.
60 * @note the function must be configured only when qspi in command-port mode!!!
61 * @param qspi_x: select the qspi peripheral.
62 * this parameter can be one of the following values:
64 * @param new_mode: new state to be set
65 * this parameter can be one of the following values:
70 void qspi_sck_mode_set(qspi_type
* qspi_x
, qspi_clk_mode_type new_mode
)
72 qspi_x
->ctrl_bit
.sckmode
= new_mode
;
76 * @brief set qspi clock division
77 * @note the function must be configured only when qspi in command-port mode!!!
78 * @param qspi_x: select the qspi peripheral.
79 * this parameter can be one of the following values:
81 * @param new_clkdiv: new division value
82 * this parameter can be one of the following values:
93 void qspi_clk_division_set(qspi_type
* qspi_x
, qspi_clk_div_type new_clkdiv
)
95 qspi_x
->ctrl_bit
.clkdiv
= new_clkdiv
;
99 * @brief enable/disable cache in xip mode
100 * @note the function must be configured only when qspi in command-port mode!!!
101 * @param qspi_x: select the qspi peripheral.
102 * this parameter can be one of the following values:
104 * @param new_state (TRUE or FALSE)
107 void qspi_xip_cache_bypass_set(qspi_type
* qspi_x
, confirm_state new_state
)
109 qspi_x
->xip_cmd_w3_bit
.bypassc
= new_state
;
113 * @brief enable/disable interrupt when command is completed
114 * @note the function must be configured only when qspi in command-port mode!!!
115 * @param qspi_x: select the qspi peripheral.
116 * this parameter can be one of the following values:
118 * @param new_state (TRUE or FALSE)
121 void qspi_interrupt_enable(qspi_type
* qspi_x
, confirm_state new_state
)
123 qspi_x
->ctrl2_bit
.cmdie
= new_state
;
127 * @brief get status flags.
128 * @param qspi_x: select the qspi peripheral.
129 * this parameter can be one of the following values:
131 * @param flag: specifies the flag to check.
132 * this parameter can be one of the following values:
133 * - QSPI_RXFIFORDY_FLAG
134 * - QSPI_TXFIFORDY_FLAG
136 * @retval the new state of usart_flag (SET or RESET).
138 flag_status
qspi_flag_get(qspi_type
* qspi_x
, uint32_t flag
)
140 flag_status bit_status
= RESET
;
143 case QSPI_RXFIFORDY_FLAG
:
144 bit_status
= (flag_status
)qspi_x
->fifosts_bit
.rxfifordy
;
146 case QSPI_TXFIFORDY_FLAG
:
147 bit_status
= (flag_status
)qspi_x
->fifosts_bit
.txfifordy
;
149 case QSPI_CMDSTS_FLAG
:
150 bit_status
= (flag_status
)qspi_x
->cmdsts_bit
.cmdsts
;
160 * @param qspi_x: select the qspi peripheral.
161 * this parameter can be one of the following values:
163 * @param flag: flags to be clear
164 * this parameter can be one of the following values:
168 void qspi_flag_clear(qspi_type
* qspi_x
, uint32_t flag
)
171 qspi_x
->cmdsts
= QSPI_CMDSTS_FLAG
;
175 * @brief set dma threshold for dma rx
176 * @note the function must be configured only when qspi in command-port mode!!!
177 * @param qspi_x: select the qspi peripheral.
178 * this parameter can be one of the following values:
180 * @param new_threshold: value to set
181 * this parameter can be one of the following values:
182 * - QSPI_DMA_FIFO_THOD_WORD08
183 * - QSPI_DMA_FIFO_THOD_WORD16
184 * - QSPI_DMA_FIFO_THOD_WORD32
187 void qspi_dma_rx_threshold_set(qspi_type
* qspi_x
, qspi_dma_fifo_thod_type new_threshold
)
189 qspi_x
->ctrl2_bit
.rxfifo_thod
= new_threshold
;
193 * @brief set dma threshold for dma tx
194 * @note the function must be configured only when qspi in command-port mode!!!
195 * @param qspi_x: select the qspi peripheral.
196 * this parameter can be one of the following values:
198 * @param new_threshold: value to set
199 * this parameter can be one of the following values:
200 * - QSPI_DMA_FIFO_THOD_WORD08
201 * - QSPI_DMA_FIFO_THOD_WORD16
202 * - QSPI_DMA_FIFO_THOD_WORD32
205 void qspi_dma_tx_threshold_set(qspi_type
* qspi_x
, qspi_dma_fifo_thod_type new_threshold
)
207 qspi_x
->ctrl2_bit
.txfifo_thod
= new_threshold
;
211 * @brief enable/disable dma transfer
212 * @note the function must be configured only when qspi in command-port mode!!!
213 * @param qspi_x: select the qspi peripheral.
214 * this parameter can be one of the following values:
216 * @param new_state (TRUE or FALSE)
219 void qspi_dma_enable(qspi_type
* qspi_x
, confirm_state new_state
)
221 qspi_x
->ctrl2_bit
.dmaen
= new_state
;
225 * @brief set wip position in status register of flash
226 * @note the function must be configured only when qspi in command-port mode!!!
227 * @param qspi_x: select the qspi peripheral.
228 * this parameter can be one of the following values:
230 * @param busy_pos: value to set
231 * this parameter can be one of the following values:
232 * - QSPI_BUSY_OFFSET_0
233 * - QSPI_BUSY_OFFSET_1
234 * - QSPI_BUSY_OFFSET_2
235 * - QSPI_BUSY_OFFSET_3
236 * - QSPI_BUSY_OFFSET_4
237 * - QSPI_BUSY_OFFSET_5
238 * - QSPI_BUSY_OFFSET_6
239 * - QSPI_BUSY_OFFSET_7
242 void qspi_busy_config(qspi_type
* qspi_x
, qspi_busy_pos_type busy_pos
)
244 qspi_x
->ctrl_bit
.busy
= busy_pos
;
248 * @brief enable xip mode or command-port mode.
249 * @param qspi_x: select the qspi peripheral.
250 * this parameter can be one of the following values:
252 * @param new_state (TRUE or FALSE)
255 void qspi_xip_enable(qspi_type
* qspi_x
, confirm_state new_state
)
257 /* skip if state is no change */
258 if(new_state
== (confirm_state
)(qspi_x
->ctrl_bit
.xipsel
))
263 /* wait until tx fifo emoty*/
264 while(qspi_x
->fifosts_bit
.txfifordy
== 0);
266 /* flush and reset qspi state */
267 qspi_x
->ctrl_bit
.xiprcmdf
= 1;
269 /* wait until action is finished */
270 while(qspi_x
->ctrl_bit
.abort
);
272 /* set xip mode to new state */
273 qspi_x
->ctrl_bit
.xipsel
= new_state
;
275 /* wait until abort is not set */
276 while(qspi_x
->ctrl_bit
.abort
);
278 /* wait until cache status valid*/
279 if(new_state
== TRUE
)
281 while( qspi_x
->xip_cmd_w3_bit
.csts
);
286 * @brief set command-port and qspi_x will start to work
287 * @param qspi_x: select the qspi peripheral.
288 * this parameter can be one of the following values:
290 * @param qspi_cmd_struct: pointer to qspi cmd structure
293 void qspi_cmd_operation_kick(qspi_type
* qspi_x
, qspi_cmd_type
* qspi_cmd_struct
)
295 uint32_t w1_val
= 0, w3_val
= 0;
297 /* config analyse cmd_w0 register */
298 qspi_x
->cmd_w0
= (uint32_t)qspi_cmd_struct
->address_code
;
300 /* config analyse cmd_w1 register */
301 w1_val
= (uint32_t)qspi_cmd_struct
->address_length
;
302 w1_val
|= (uint32_t)(qspi_cmd_struct
->second_dummy_cycle_num
<< 16);
303 w1_val
|= (uint32_t)(qspi_cmd_struct
->instruction_length
<< 24);
304 w1_val
|= (uint32_t)(qspi_cmd_struct
->pe_mode_enable
<< 28);
305 qspi_x
->cmd_w1
= w1_val
;
307 /* config analyse cmd_w2 register */
308 qspi_x
->cmd_w2
= (uint32_t)qspi_cmd_struct
->data_counter
;
310 /* config analyse cmd_w3 register */
311 w3_val
= (uint32_t)(qspi_cmd_struct
->write_data_enable
<< 1);
312 w3_val
|= (uint32_t)(qspi_cmd_struct
->read_status_enable
<< 2);
313 w3_val
|= (uint32_t)(qspi_cmd_struct
->read_status_config
<< 3);
314 w3_val
|= (uint32_t)(qspi_cmd_struct
->operation_mode
<< 5);
315 w3_val
|= (uint32_t)(qspi_cmd_struct
->pe_mode_operate_code
<< 16);
316 w3_val
|= (uint32_t)(qspi_cmd_struct
->instruction_code
<< 24);
317 qspi_x
->cmd_w3
= w3_val
;
321 * @brief initial xip mode for qspi_x
322 * @param qspi_x: select the qspi peripheral.
323 * this parameter can be one of the following values:
325 * @param xip_init_struct: pointer to xip init structure.
328 void qspi_xip_init(qspi_type
* qspi_x
, qspi_xip_type
* xip_init_struct
)
330 uint32_t xc0_val
= 0, xc1_val
= 0, xc2_val
= 0;
331 /* config analyse xip_cmd_w0 register */
332 xc0_val
= (uint32_t)xip_init_struct
->read_second_dummy_cycle_num
;
333 xc0_val
|= (uint32_t)(xip_init_struct
->read_operation_mode
<< 8);
334 xc0_val
|= (uint32_t)(xip_init_struct
->read_address_length
<< 11);
335 xc0_val
|= (uint32_t)(xip_init_struct
->read_instruction_code
<< 12);
336 qspi_x
->xip_cmd_w0
= xc0_val
;
338 /* config analyse xip_cmd_w1 register */
339 xc1_val
= (uint32_t)xip_init_struct
->write_second_dummy_cycle_num
;
340 xc1_val
|= (uint32_t)(xip_init_struct
->write_operation_mode
<< 8);
341 xc1_val
|= (uint32_t)(xip_init_struct
->write_address_length
<< 11);
342 xc1_val
|= (uint32_t)(xip_init_struct
->write_instruction_code
<< 12);
343 qspi_x
->xip_cmd_w1
= xc1_val
;
345 /* config analyse xip_cmd_w2 register */
346 xc2_val
= (uint32_t)xip_init_struct
->read_data_counter
;
347 xc2_val
|= (uint32_t)(xip_init_struct
->read_time_counter
<< 8);
348 xc2_val
|= (uint32_t)(xip_init_struct
->read_select_mode
<< 15);
349 xc2_val
|= (uint32_t)(xip_init_struct
->write_data_counter
<< 16);
350 xc2_val
|= (uint32_t)(xip_init_struct
->write_time_counter
<< 24);
351 xc2_val
|= (uint32_t)(xip_init_struct
->write_select_mode
<< 31);
352 qspi_x
->xip_cmd_w2
= xc2_val
;
356 * @brief read one byte from qspi device in command mode
357 * @param qspi_x: select the qspi peripheral.
358 * @retval 8-bit data.
360 uint8_t qspi_byte_read(qspi_type
* qspi_x
)
362 return qspi_x
->dt_u8
;
366 * @brief read one half-word from qspi device in command mode
367 * @param qspi_x: select the qspi peripheral.
368 * @retval 16-bit data.
370 uint16_t qspi_half_word_read(qspi_type
* qspi_x
)
372 return qspi_x
->dt_u16
;
376 * @brief read one word from qspi device in command mode
377 * @param qspi_x: select the qspi peripheral.
378 * @retval 32-bit data.
380 uint32_t qspi_word_read(qspi_type
* qspi_x
)
386 * @brief write one byte to qspi device in command mode
387 * @param qspi_x: select the qspi peripheral.
388 * @param value: 8-bit data.
391 void qspi_byte_write(qspi_type
* qspi_x
, uint8_t value
)
393 qspi_x
->dt_u8
= value
;
397 * @brief write one half-word to qspi device in command mode
398 * @param qspi_x: select the qspi peripheral.
399 * @param value: 16-bit data.
402 void qspi_half_word_write(qspi_type
* qspi_x
, uint16_t value
)
404 qspi_x
->dt_u16
= value
;
408 * @brief write one word to qspi device in command mode
409 * @param qspi_x: select the qspi peripheral.
410 * @param value: 32-bit data.
413 void qspi_word_write(qspi_type
* qspi_x
, uint32_t value
)