Mbed OS 文档翻译 之 参考(API(驱动(SPI)))

SPI

                                                                            

                                                                                  SPI 类层次结构

SPI 接口提供串行外设接口主机。

您可以使用此接口与 SPI 从设备进行通信,例如 FLASH 存储器,LCD 屏幕和其他模块或集成电路。

接口

                               Mbed OS 文档翻译 之 参考(API(驱动(SPI)))_第1张图片

                                                                                               引脚图

SPI 接口的默认设置为 1 MHz,8 位,模式 0。

您可以使用 SPI 接口从 SPI 端口写入数据字,返回从 SPI 从器件接收的数据。您还可以配置 SPI 时钟频率和格式。格式设置为数据字长度为 8 到 16 位,模式如下表所示:

模式 极性 相位
0 0 0
1 0 1
2 1 0
3 1 1

SPI 主控制器生成一个时钟,以同步驱动串行位流从器件。从机返回一个比特流,也与时钟同步。要与通过同一 SPI 外设连接的多个从器件通信,必须使用多个 SPI 对象,每个从器件对应一个,但使用不同的从器件选择(SSEL)和与每个从器件对应的配置对它们进行实例化。请注意,当应用程序代码调用 SPI 对象上的接口时,SPI 对象会自动使用当前对象的配置来配置 SPI 外设。

SPI 类参考

mbed::SPI 类参考

公共成员函数
  SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
void  format (int bits, int mode=0)
void  frequency (int hz=1000000)
virtual int  write (int value)
virtual int  write (const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length)
virtual void  lock (void)
virtual void  unlock (void)
void  set_default_write_value (char data)
template
int  transfer (const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t &callback, int event=SPI_EVENT_COMPLETE)
void  abort_transfer ()
void  clear_transfer_buffer ()
void  abort_all_transfers ()
int  set_dma_usage (DMAUsage usage)
受保护的成员函数
void  irq_handler_asynch (void)
int  transfer (const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
int  queue_transfer (const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
void  start_transfer (const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
void  aquire (void)
受保护的属性
spi_t  _spi
CThunk< SPI >  _irq
event_callback_t  _callback
DMAUsage  _usage
bool  _deep_sleep_locked
int  _bits
int  _mode
int  _hz
char  _write_fill
静态保护属性
static SPI *  _owner = NULL
static SingletonPtr< PlatformMutex >  _mutex

SPI hello, world

main.cpp                                                                                                                                                导入到 Mbed IDE

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
 
SPI spi(D11, D12, D13); // mosi, miso, sclk
DigitalOut cs(D0);
 
int main() {
    // Chip must be deselected
    cs = 1;

    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(8,3);
    spi.frequency(1000000);
 
    // Select the device by seting chip select low
    cs = 0;
 
    // Send 0x8f, the command to read the WHOAMI register
    spi.write(0x8F);
 
    // Send a dummy byte to receive the contents of the WHOAMI register
    int whoami = spi.write(0x00);
    printf("WHOAMI register = 0x%X\n", whoami);
 
    // Deselect the device
    cs = 1;
}

你可能感兴趣的:(Mbed,OS)