1 /** mbed Microcontroller Library 2 ****************************************************************************** 3 * @file spi_api.h 4 * @author 5 * @version V1.0.0 6 * @brief This file provides following mbed SPI API 7 ****************************************************************************** 8 * @attention 9 * 10 * Copyright (c) 2006-2013 ARM Limited 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 ****************************************************************************** 24 */ 25 #ifndef MBED_SPI_API_H 26 #define MBED_SPI_API_H 27 28 #include "device.h" 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 /** @addtogroup spi SPI 35 * @ingroup hal 36 * @brief spi functions 37 * @{ 38 */ 39 40 41 #if ((defined(CONFIG_PLATFORM_8711B) && (CONFIG_PLATFORM_8711B == 1)) || (defined (CONFIG_PLATFORM_8721D) && (CONFIG_PLATFORM_8721D == 1))) 42 ///@name AmebaZ and AmebaD 43 ///@{ 44 typedef enum { 45 MBED_SPI0 = 0xF0, /*!< means SPI0 */ 46 MBED_SPI1 = 0xF1, /*!< means SPI1 */ 47 } MBED_SPI_IDX; 48 ///@} 49 #endif //CONFIG_PLATFORM_8711B and CONFIG_PLATFORM_8721D 50 51 52 ///@name Ameba Common 53 ///@{ 54 55 typedef struct spi_s spi_t; 56 57 /** 58 * @brief Initializes the SPI device, include clock/function/interrupt/SPI registers. 59 * @param obj: spi object define in application software. 60 * @param mosi: MOSI PinName according to pinmux spec. 61 * @param miso: MISO PinName according to pinmux spec. 62 * @param sclk: SCLK PinName according to pinmux spec. 63 * @param ssel: CS PinName according to pinmux spec. 64 * @retval none 65 * @note must set obj->spi_index to MBED_SPI0 or MBED_SPI1 before using spi_init 66 */ 67 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel); 68 69 /** 70 * @brief Deinitializes the SPI device, include interrupt/DMA/DISABLE SPI. 71 * @param obj: spi object define in application software. 72 * @retval none 73 */ 74 void spi_free(spi_t *obj); 75 76 /** 77 * @brief Set SPI format,include DFS/Phase/Polarity. 78 * @param obj: spi object define in application software. 79 * @param bits: data frame size, 4-16 supported. 80 * @param mode: this parameter can be one of the following values: 81 * @arg 0 : [Polarity,Phase]=[0,0] 82 * @arg 1 : [Polarity,Phase]=[0,1] 83 * @arg 2 : [Polarity,Phase]=[1,0] 84 * @arg 3 : [Polarity,Phase]=[1,1] 85 * @param slave: this parameter can be one of the following values: 86 * @arg 0 : indicates role-master 87 * @arg 1 : indicates role-slave 88 * @retval none 89 */ 90 void spi_format(spi_t *obj, int bits, int mode, int slave); 91 92 /** 93 * @brief Set SPI baudrate. 94 * @param obj: spi master object define in application software. 95 * @param hz: baudrate for SPI bus 96 * @retval none 97 * @note "hz" should be less or equal to half of the SPI IpClk 98 */ 99 void spi_frequency(spi_t *obj, int hz); 100 101 /** 102 * @brief Master send one frame use SPI. 103 * @param obj: spi master object define in application software. 104 * @param value: the data to transmit. 105 * @retval : data received from slave 106 */ 107 int spi_master_write(spi_t *obj, int value); 108 109 /** 110 * @brief Get slave readable && busy state. 111 * @param obj: spi slave object define in application software. 112 * @retval : slave Readable && Busy State 113 */ 114 int spi_slave_receive(spi_t *obj); 115 116 /** 117 * @brief Slave receive one frame use SPI. 118 * @param obj: spi slave object define in application software. 119 * @retval : data received from master 120 */ 121 int spi_slave_read(spi_t *obj); 122 123 /** 124 * @brief Slave send one frame use SPI. 125 * @param obj: spi slave object define in application software. 126 * @param value: the data to transmit. 127 * @retval none 128 */ 129 void spi_slave_write(spi_t *obj, int value); 130 131 /** 132 * @brief Get SPI busy state. 133 * @param obj: spi object define in application software. 134 * @retval : current busy state 135 */ 136 int spi_busy(spi_t *obj); 137 138 /** 139 * @brief SPI device to flush rx fifo. 140 * @param obj: spi object define in application software. 141 * @retval none 142 */ 143 void spi_flush_rx_fifo(spi_t *obj); 144 145 /** 146 * @brief Open SPI device clock. 147 * @param obj: spi object define in application software. 148 * @retval none 149 */ 150 void spi_enable(spi_t *obj); 151 152 /** 153 * @brief Close SPI device clock. 154 * @param obj: spi object define in application software. 155 * @retval none 156 */ 157 void spi_disable(spi_t *obj); 158 ///@} 159 160 /*\@}*/ 161 162 #ifdef __cplusplus 163 } 164 #endif 165 166 #endif 167