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 
19 /********************************************************************************************************
20  * @file	mspi.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #pragma once
28 
29 #include "compiler.h"
30 #include "gpio.h"
31 #include "reg_include/mspi_reg.h"
32 
33 /**
34   * @brief     This function servers to set the spi wait.
35   * @return    none.
36   */
mspi_wait(void)37 _attribute_ram_code_sec_ static inline void mspi_wait(void){
38 	while(reg_mspi_status & FLD_MSPI_BUSY);
39 }
40 
41 /**
42  * @brief     This function servers to enable read triggle spi.
43  * @return    none.
44  */
mspi_fm_rd_en(void)45 _attribute_ram_code_sec_ static inline void mspi_fm_rd_en(void){
46 	reg_mspi_fm |= FLD_MSPI_RD_TRIG_EN;
47 }
48 
49 /**
50  * @brief     This function servers to disable read triggle spi.
51  * @return    none.
52  */
mspi_fm_rd_dis(void)53 _attribute_ram_code_sec_ static inline void mspi_fm_rd_dis(void){
54 	reg_mspi_fm &= ~FLD_MSPI_RD_TRIG_EN;
55 }
56 
57 /**
58  * @brief     This function servers to set spi interface csn signal.
59  * @return    none.
60  */
mspi_high(void)61 _attribute_ram_code_sec_ static inline void mspi_high(void){
62 	reg_mspi_fm |= FLD_MSPI_CSN;
63 }
64 
65 /**
66  * @brief     This function servers to clear spi interface csn signal.
67  * @return    none.
68  */
mspi_low(void)69 _attribute_ram_code_sec_ static inline void mspi_low(void){
70 	reg_mspi_fm &= ~FLD_MSPI_CSN;
71 }
72 
73 /**
74  * @brief		This function servers to gets the spi data.
75  * @return		the spi data.
76  */
mspi_get(void)77 _attribute_ram_code_sec_ static inline unsigned char mspi_get(void){
78 	return reg_mspi_data;
79 }
80 
81 /**
82  * @brief		This function servers to write the spi.
83  * @param[in]	c	- the char need to be write.
84  * @return		none.
85  */
mspi_write(unsigned char c)86 _attribute_ram_code_sec_ static inline void mspi_write(unsigned char c){
87 	reg_mspi_data = c;
88 }
89 
90 /**
91  * @brief		This function servers to control the write.
92  * @param[in]	c	- need to be write.
93  * @return		none.
94  */
mspi_fm_write(unsigned char c)95 _attribute_ram_code_sec_ static inline void mspi_fm_write(unsigned char c){
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 	mspi_write(0);		// dummy, issue clock
105 	mspi_wait();
106 	return mspi_get();
107 }
108 
109 /**
110  * @brief		This function serves to Stop XIP operation before flash.
111  * @return		none.
112  */
mspi_stop_xip(void)113 _attribute_ram_code_sec_ static inline void mspi_stop_xip(void)
114 {
115 	mspi_wait();	//wait xip busy=0
116 	mspi_high();	//mspi_cn=1, stop xip read
117 	while(gpio_get_level(GPIO_PF3) == 0);	//wait cn=1
118 }
119 
120 
121 
122