中级
30 分钟

使用SC18IS602B和TM4C1294NCZAD在接口之间建立安全连接

I2C遇见SPI:革命性的接口桥接解决方案!

I2C to SPI Click with Fusion for Tiva v8

已发布 6月 25, 2024

点击板

I2C to SPI Click

开发板

Fusion for Tiva v8

编译器

NECTO Studio

微控制器单元

TM4C1294NCZAD

通过我们的桥接技术,将您的项目提升到接口精度的新高度。它能够弥合I2C和SPI之间的差距,优化数据交换,减少复杂性,并增强您电子应用中的兼容性。

A

A

硬件概览

它是如何工作的?

I2C to SPI Click基于两个NXP Semiconductor的SC18IS602B,这是一个I2C总线到SPI桥接器。该IC桥接了两种接口之间的数据通信,提供了许多附加功能,如可编程I/O、内部振荡器选项、低电平有效中断输出、低功耗模式等。SC18IS602B作为I2C总线的从机发送器或从机接收器以及SPI主机操作。SC18IS602B控制所有SPI总线特定的序列、协议和定时。它还具有自己的内部振荡器,并支持SPI芯片选择输出,在未使用时可配置为GPIO。这使得软件易于编写或从其他平台移植。I2C to SPI Click提供面向字节的I2C总线接口,支持高达400 kHz的数据传输。当I2C总线主机从Click板™读取数据时,该设备将是从机发送器。当I2C总线主机发送数据时,它也可以是从机接收器。SC18IS602B在任何时候都不会作

为I2C总线主机操作。然而,它确实具有在字节之间保持SCL线为低电平的能力,以完成其内部过程。SC18IS602B的从机地址由固定部分和可编程部分组成。从机地址的可编程部分使得尽可能多的此类设备能够连接到I2C总线上。由于SC18IS602B具有三个可编程地址位(由A2、A1和A0引脚定义),因此在同一总线上最多可以有八个此类设备。因此,该Click板™配备了三个SMD跳线,归类在ADDR SEL标签下,用于选择I2C从机地址。通过将跳线移动到所需位置,用户可以选择用于与主机MCU通信的地址。#RESET引脚执行SC18IS602B IC的硬件复位。#RESET引脚连接到mikroBUS™的RST引脚,并且为低电平有效。#INT允许主机MCU从SC18IS602B IC接收中断。在完成任何SPI传输后,

SC18IS602B生成中断。因此,SC18IS602B的#INT连接到mikroBUS™插座的INT引脚。可以通过发送“清除中断”命令来清除中断(INT引脚为高电平),尽管这不是必需的。这允许编写更优化的软件(固件),因为主机MCU不必连续轮询LSR寄存器以查看是否需要服务任何中断。SC18IS602B的数据手册提供了有关使用和配置SC18IS602B IC的更多信息。然而,Click板™由mikroSDK库支持,提供简化原型设计和固件开发的功能。该Click板™只能在3.3V逻辑电压水平下运行。在使用具有不同逻辑电平的MCU之前,必须进行适当的逻辑电压电平转换。此外,它配备了一个库,包含函数和示例代码,可用作进一步开发的参考。

I2C to SPI Click top side image
I2C to SPI Click bottom side image

功能概述

开发板

Fusion for TIVA v8 是一款专为快速开发嵌入式应用的需求而特别设计的开发板。它支持广泛的微控制器,如不同的32位ARM® Cortex®-M基础MCUs,来自Texas Instruments,无论它们的引脚数量如何,并且具有一系列独特功能,例如首次通过WiFi网络实现的嵌入式调试器/程序员。开发板布局合理,设计周到,使得最终用户可以在一个地方找到所有必要的元素,如开关、按钮、指示灯、连接器等。得益于创新的制造技术,Fusion for TIVA v8 提供了流畅而沉浸式的工作体验,允许在任何情况下、任何地方、任何

时候都能访问。Fusion for TIVA v8开发板的每个部分都包含了使同一板块运行最高效的必要组件。一个先进的集成CODEGRIP程序/调试模块提供许多有价值的编程/调试选项,包括对JTAG、SWD和SWO Trace(单线输出)的支持,并与Mikroe软件环境无缝集成。此外,它还包括一个干净且调节过的开发板电源供应模块。它可以使用广泛的外部电源,包括电池、外部12V电源供应和通过USB Type-C(USB-C)连接器的电源。通信选项如USB-UART、USB HOST/DEVICE、CAN(如果MCU卡支持的话)和以

太网也包括在内。此外,它还拥有广受好评的 mikroBUS™标准,为MCU卡提供了标准化插座(SiBRAIN标准),以及两种显示选项,用于TFT板线产品和基于字符的LCD。Fusion for TIVA v8 是Mikroe快速开发生态系统的一个组成部分。它由Mikroe软件工具原生支持,得益于大量不同的Click板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作和开发的许多方面。

Fusion for Tiva v8 horizontal image

微控制器概述 

MCU卡片 / MCU

default

类型

8th Generation

建筑

ARM Cortex-M4

MCU 内存 (KB)

1024

硅供应商

Texas Instruments

引脚数

212

RAM (字节)

262144

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset
PB6
RST
SPI Chip Select
PE7
CS
SPI Clock
PA2
SCK
SPI Data OUT
PA5
MISO
SPI Data IN
PA4
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
PB4
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PB2
SCL
I2C Data
PB3
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

I2C to SPI Click Schematic schematic

一步一步来

项目组装

Fusion for PIC v8 front image hardware assembly

从选择您的开发板和Click板™开始。以Fusion for Tiva v8作为您的开发板开始。

Fusion for PIC v8 front image hardware assembly
Buck 22 Click front image hardware assembly
SiBRAIN for PIC32MZ1024EFK144 front image hardware assembly
v8 SiBRAIN MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

此库包含I2C to SPI Click驱动程序的API。

关键功能:

  • i2ctospi_spi_write_byte - 此功能通过SPI将数据字节写入I2C to SPI Click上的SC18IS602B I2C总线到SPI桥接器的目标8位寄存器地址。

  • i2ctospi_spi_read_byte - 此功能通过SPI从I2C to SPI Click上的SC18IS602B I2C总线到SPI桥接器的目标8位寄存器地址读取数据字节。

  • i2ctospi_clear_interrupt - 此功能在完成任何SPI传输后清除由SC18IS602B生成的中断。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * \file 
 * \brief I2cToSpi Click example
 * 
 * # Description
 * I2C to SPi Click allows serving as an interface between a standard I2C-bus of a microcontroller 
 * and an SPi bus, which allows the microcontroller to communicate directly with SPi devices 
 * through its I2C-bus. By offering an I2C-bus slave-transmitter or slave-receiver and SPI master, 
 * this Click controls all the SPi bus-specific sequences, protocol, and timing. It also has its own 
 * internal oscillator, and it supports the SPi chip select output that may be configured as GPIO when not used.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialization driver enable's - I2C,
 * hardware reset, SS0 ( CS ) configured to be used as slave select outputs, set the configuration of SPI:
 * order MSB first, clock Idle low, leading-edge transition, SPI clock rate to 115kHz,
 * set SPI EEPROM write enable SS0, clear  interrupt,
 * clear RT5 register, sets starting time: hours, minutes and seconds ( enable counting ), also write log.
 * 
 * ## Application Task  
 * This is an example which demonstrates the use of RTC 5 click is wired to I2C to SPI click board.
 * I2C to SPI click communicates with register via the I2C interface,
 * serve as an interface between a standard I2C-bus of a microcontroller and an SPI bus.
 * RTC 5 click communicates with register via SPI interface.
 * In this examples, we display RTC time which we received reading from the target register 
 * address of MCP79510 chip on RTC 5 click board via I2C interface of I2C to SPI click board.
 * Results are being sent to the Usart Terminal where you can track their changes.
 * All data logs write on usb uart changes for every 1 sec.
 * 
 * *note:* 
 * <pre>
 * Additional Functions :
 *  - void display_log_uart( uint8_t value ) - Write the value of time or date as a two-digit number.
 *  - void rtc5_clear( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Clear RTCC and SRAM memory of RTC 5 click.
 *  - void rtc5_set_time_seconds( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t seconds ) - Set the seconds and enable counting.
 *  - uint8_t rtc5_get_time_seconds( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Get the seconds.
 *  - void rtc5_set_time_minutes( uint8_t minutes ) - Set the minutes.
 *  - uint8_t rtc5_get_time_minutes( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Get the minutes.
 *  - void rtc5_set_time_hours( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t hours ) - Set the hours.
 *  - uint8_t rtc5_get_time_hours( i2ctospi_t *ctx, i2ctospi_spi_t *spi ) - Get the hours.
 * </pre>
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "i2ctospi.h"

// ------------------------------------------------------------------ VARIABLES

static i2ctospi_t i2ctospi;
static i2ctospi_spi_t i2ctospi_spi;
static i2ctospi_gpio_t i2ctospi_gpio;
static log_t logger;

static uint8_t time_hours;
static uint8_t time_minutes;
static uint8_t time_seconds;
static uint8_t time_seconds_new = 0xFF;

// ------------------------------------------------------- ADDITIONAL FUNCTIONS

void display_log_uart ( uint8_t value )
{
    log_printf( &logger, " %d%d ", ( uint16_t )( value / 10 ), ( uint16_t )( value % 10 ) );
}

void rtc5_clear ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t reg_add;
    
    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = reg_add;

    for ( reg_add = 0; reg_add < 0x20; reg_add++ )
    {
        i2ctospi_spi_write_byte( ctx, spi, 0x00 );
        Delay_1us( );
    }

    spi->reg_addr = I2CTOSPI_RTC5_COMMAND_CLEAR;
    i2ctospi_spi_write_byte( ctx, spi, 0x00 );

    i2ctospi_clear_interrupt( ctx );
}

void rtc5_set_time_seconds ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t seconds )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t temp;

    ones = 0x00;
    tens = 0x00;

    seconds %= 60;

    ones = seconds % 10;

    tens = ( seconds / 10 ) << 4;

    temp = tens | ones;
    temp |= I2CTOSPI_RTC5_COMMAND_ENABLE_COUNTING;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_SEC;

    i2ctospi_spi_write_byte( ctx, spi, temp );
}

uint8_t rtc5_get_time_seconds ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t result_sec;
    uint8_t temp;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_READ;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_SEC;

    temp = i2ctospi_spi_read_byte( ctx, spi );

    ones = temp & 0x0F;

    tens = ( temp & 0x70 ) >> 4;

    result_sec = ( 10 * tens ) + ones;

    return result_sec;
}

void rtc5_set_time_minutes ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t minutes )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t temp;

    ones = 0x00;
    tens = 0x00;

    minutes %= 60;

    ones = minutes % 10;

    tens = ( minutes / 10 ) << 4;

    temp = tens | ones;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_MIN;

    i2ctospi_spi_write_byte( ctx, spi, temp );
}

uint8_t rtc5_get_time_minutes ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t result_min;
    uint8_t temp;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_READ;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_MIN;

    temp = i2ctospi_spi_read_byte( ctx, spi );

    ones = temp & 0x0F;

    tens = ( temp & 0x70 ) >> 4;

    result_min = ( 10 * tens ) + ones;

    return result_min;
}

void rtc5_set_time_hours ( i2ctospi_t *ctx, i2ctospi_spi_t *spi, uint8_t hours )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t temp;

    ones = 0x00;
    tens = 0x00;

    hours %= 24;

    ones = hours % 10;

    tens = ( hours / 10 ) << 4;

    temp = tens | ones;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_WRITE;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_HOUR,

    i2ctospi_spi_write_byte( ctx, spi, temp );
}

uint8_t rtc5_get_time_hours ( i2ctospi_t *ctx, i2ctospi_spi_t *spi )
{
    uint8_t ones;
    uint8_t tens;
    uint8_t result_hours;
    uint8_t temp;

    spi->slave_device = I2CTOSPI_SLAVEDEVICE_SS0;
    spi->function_id = I2CTOSPI_RTC5_COMMAND_READ;
    spi->reg_addr = I2CTOSPI_RTC5_REG_TIME_HOUR;

    temp = i2ctospi_spi_read_byte( ctx, spi );

    ones = temp & 0x0F;

    tens = ( temp & 0x30 ) >> 4;

    result_hours = ( 10 * tens ) + ones;

    return result_hours;
}

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    i2ctospi_cfg_t cfg;

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, "---- Application Init ----" );

    //  Click initialization.

    i2ctospi_cfg_setup( &cfg );
    I2CTOSPI_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    i2ctospi_init( &i2ctospi, &cfg );

    i2ctospi_default_cfg( &i2ctospi );
    
    //Set Time :  23h 59m 48s
    rtc5_clear( &i2ctospi, &i2ctospi_spi );  
    rtc5_set_time_hours( &i2ctospi, &i2ctospi_spi, 23 );
    Delay_1ms( );
    rtc5_set_time_minutes( &i2ctospi, &i2ctospi_spi, 59 );
    Delay_1ms( );
    rtc5_set_time_seconds( &i2ctospi, &i2ctospi_spi, 48 );
    Delay_1ms( );
}

void application_task ( void )
{
    time_seconds = rtc5_get_time_seconds( &i2ctospi, &i2ctospi_spi );
    Delay_1ms( );
    time_minutes = rtc5_get_time_minutes( &i2ctospi, &i2ctospi_spi );
    Delay_1ms( );
    time_hours = rtc5_get_time_hours( &i2ctospi, &i2ctospi_spi );
    Delay_1ms( );

    if ( time_seconds_new != time_seconds )
    {
        log_printf( &logger, " Time :  " );
    
        display_log_uart( time_hours );
        log_printf( &logger, ":" );
    
        display_log_uart( time_minutes );
        log_printf( &logger, ":" );
    
        display_log_uart( time_seconds );
        log_printf( &logger, "\r\n" );
        
        log_printf( &logger, "------------------\r\n" );

        time_seconds_new = time_seconds;
    }

    Delay_1ms( );
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}


// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。