• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #pragma once
19 
20 #include "compiler.h"
21 #include "gpio.h"
22 #include "reg_include/mspi_reg.h"
23 
24 /**
25   * @brief     This function servers to set the spi wait.
26   * @return    none.
27   */
mspi_wait(void)28 _attribute_ram_code_sec_ static inline void mspi_wait(void)
29 {
30     while (reg_mspi_status & FLD_MSPI_BUSY) {
31     }
32 }
33 
34 /**
35  * @brief     This function servers to enable read triggle spi.
36  * @return    none.
37  */
mspi_fm_rd_en(void)38 _attribute_ram_code_sec_ static inline void mspi_fm_rd_en(void)
39 {
40     reg_mspi_fm |= FLD_MSPI_RD_TRIG_EN;
41 }
42 
43 /**
44  * @brief     This function servers to disable read triggle spi.
45  * @return    none.
46  */
mspi_fm_rd_dis(void)47 _attribute_ram_code_sec_ static inline void mspi_fm_rd_dis(void)
48 {
49     reg_mspi_fm &= ~FLD_MSPI_RD_TRIG_EN;
50 }
51 
52 /**
53  * @brief     This function servers to set spi interface csn signal.
54  * @return    none.
55  */
mspi_high(void)56 _attribute_ram_code_sec_ static inline void mspi_high(void)
57 {
58     reg_mspi_fm |= FLD_MSPI_CSN;
59 }
60 
61 /**
62  * @brief     This function servers to clear spi interface csn signal.
63  * @return    none.
64  */
mspi_low(void)65 _attribute_ram_code_sec_ static inline void mspi_low(void)
66 {
67     reg_mspi_fm &= ~FLD_MSPI_CSN;
68 }
69 
70 /**
71  * @brief		This function servers to gets the spi data.
72  * @return		the spi data.
73  */
mspi_get(void)74 _attribute_ram_code_sec_ static inline unsigned char mspi_get(void)
75 {
76     return reg_mspi_data;
77 }
78 
79 /**
80  * @brief		This function servers to write the spi.
81  * @param[in]	c	- the char need to be write.
82  * @return		none.
83  */
mspi_write(unsigned char c)84 _attribute_ram_code_sec_ static inline void mspi_write(unsigned char c)
85 {
86     reg_mspi_data = c;
87 }
88 
89 /**
90  * @brief		This function servers to control the write.
91  * @param[in]	c	- need to be write.
92  * @return		none.
93  */
mspi_fm_write(unsigned char c)94 _attribute_ram_code_sec_ static inline void mspi_fm_write(unsigned char c)
95 {
96     reg_mspi_fm = c;
97 }
98 
99 /**
100  * @brief		This function servers to spi read.
101  * @return		read result.
102  */
mspi_read(void)103 _attribute_ram_code_sec_ static inline unsigned char mspi_read(void)
104 {
105     mspi_write(0);  // dummy, issue clock
106     mspi_wait();
107     return mspi_get();
108 }
109 
110 /**
111  * @brief		This function serves to Stop XIP operation before flash.
112  * @return		none.
113  */
mspi_stop_xip(void)114 _attribute_ram_code_sec_ static inline void mspi_stop_xip(void)
115 {
116     mspi_wait();  // wait xip busy=0
117     mspi_high();  // mspi_cn=1, stop xip read
118     while (gpio_get_level(GPIO_PF3) == 0) {
119     }  // wait cn=1
120 }
121