中级
30 分钟

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

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

I2C to SPI Click with Nucleo 64 with STM32F410RB MCU

已发布 10月 08, 2024

点击板

I2C to SPI Click

开发板

Nucleo 64 with STM32F410RB MCU

编译器

NECTO Studio

微控制器单元

STM32F410RB

通过我们的桥接技术,将您的项目提升到接口精度的新高度。它能够弥合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

功能概述

开发板

Nucleo-64 搭载 STM32F410RB MCU 提供了一种经济高效且灵活的平台,供开发者探索新想法并原型设计他们的项目。该板利用 STM32 微控制器的多功能性,使用户能够为他们的项目选择最佳的性能与功耗平衡。它配备了 LQFP64 封装的 STM32 微控制器,并包含了如用户 LED(同时作为 ARDUINO® 信号)、用户和复位按钮,以及 32.768kHz 晶体振荡器用于精确的计时操作等基本组件。Nucleo-64 板设计考虑到扩展性和灵活性,它特有的 ARDUINO® Uno

V3 扩展连接器和 ST morpho 扩展引脚头,提供了对 STM32 I/O 的完全访问,以实现全面的项目整合。电源供应选项灵活,支持 ST-LINK USB VBUS 或外部电源,确保在各种开发环境中的适应性。该板还配备了一个具有 USB 重枚举功能的板载 ST-LINK 调试器/编程器,简化了编程和调试过程。此外,该板设计旨在简化高级开发,它的外部 SMPS 为 Vcore 逻辑供电提供高效支持,支持 USB 设备全速或 USB SNK/UFP 全速,并内置加密功能,提升了项目的功效

和安全性。通过外部 SMPS 实验的专用连接器、 用于  ST-LINK 的 USB 连接器以及 MIPI® 调试连接器,提供了更多的硬件接口和实验可能性。开发者将通过 STM32Cube MCU Package 提供的全面免费软件库和示例得到广泛支持。这些,加上与多种集成开发环境(IDE)的兼容性,包括 IAR Embedded Workbench®、MDK-ARM 和 STM32CubeIDE,确保了流畅且高效的开发体验,使用户能够充分利用 Nucleo-64 板在他们的项目中的能力。

Nucleo 64 with STM32C031C6 MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M4

MCU 内存 (KB)

128

硅供应商

STMicroelectronics

引脚数

64

RAM (字节)

32768

你完善了我!

配件

Click Shield for Nucleo-64 配备了两个专有的 mikroBUS™ 插座,使得所有的 Click board™ 设备都可以轻松地与 STM32 Nucleo-64 开发板连接。这样,Mikroe 允许其用户从不断增长的 Click boards™ 范围中添加任何功能,如 WiFi、GSM、GPS、蓝牙、ZigBee、环境传感器、LED、语音识别、电机控制、运动传感器等。您可以使用超过 1537 个 Click boards™,这些 Click boards™ 可以堆叠和集成。STM32 Nucleo-64 开发板基于 64 引脚封装的微控制器,采用 32 位 MCU,配备 ARM Cortex M4 处理器,运行速度为 84MHz,具有 512Kb Flash 和 96KB SRAM,分为两个区域,顶部区域代表 ST-Link/V2 调试器和编程器,而底部区域是一个实际的开发板。通过 USB 连接方便地控制和供电这些板子,以便直接对 Nucleo-64 开发板进行编程和高效调试,其中还需要额外的 USB 线连接到板子上的 USB 迷你接口。大多数 STM32 微控制器引脚都连接到了板子左右边缘的 IO 引脚上,然后连接到两个现有的 mikroBUS™ 插座上。该 Click Shield 还有几个开关,用于选择 mikroBUS™ 插座上模拟信号的逻辑电平和 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换器,使用任何 Click board™,无论 Click board™ 是否在 3.3V 或 5V 逻辑电压电平下运行。一旦将 STM32 Nucleo-64 开发板与我们的 Click Shield for Nucleo-64 连接,您就可以访问数百个工作于 3.3V 或 5V 逻辑电压电平的 Click boards™。

Click Shield for Nucleo-64 accessories 1 image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset
PC12
RST
SPI Chip Select
PB12
CS
SPI Clock
PB3
SCK
SPI Data OUT
PB4
MISO
SPI Data IN
PB5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
PC14
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PB8
SCL
I2C Data
PB9
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

原理图

I2C to SPI Click Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-64 front image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 64 with STM32F410RB MCU作为您的开发板开始。

Click Shield for Nucleo-64 front image hardware assembly
Nucleo 64 with STM32F401RE MCU front image hardware assembly
EEPROM 13 Click front image hardware assembly
Prog-cut hardware assembly
Nucleo-64 with STM32XXX MCU MB 1 Mini B Conn - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Clicker 4 for STM32F4 HA MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

通过调试模式的应用程序输出

1. 一旦代码示例加载完成,按下 "DEBUG" 按钮将启动构建过程,并将其编程到创建的设置上,然后进入调试模式。

2. 编程完成后,IDE 中将出现一个带有各种操作按钮的标题。点击绿色的 "PLAY" 按钮开始读取通过 Click board™ 获得的结果。获得的结果将在 "Application Output" 标签中显示。

DEBUG_Application_Output

软件支持

库描述

此库包含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 中找到。欢迎下载代码,或者您也可以复制下面的代码。

/*!
 * \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

额外支持

资源

喜欢这个项目吗?

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