目前共有5篇帖子。 字體大小:較小 - 100% (默認)▼  內容轉換:不轉換▼
 
點擊 回復
19 4
TLSR8258 I2C slave模式测试
一派掌門 二十級
1樓 發表于:2026-6-3 11:20
TLSR8258的7位I2C地址为0x2e。
I2C_MODE为I2C_MAPPING_MODE。
I2C引脚为PC0和PC1(I2C_GPIO_GROUP_C0C1)。
一派掌門 二十級
2樓 發表于:2026-6-3 11:22
/********************************************************************************************************
 * @file    app_map.c
 *
 * @brief   This is the source file for Telink MCU
 *
 * @author  Driver Group
 * @date    2018
 *
 * @par     Copyright (c) 2018, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *
 *          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 "app_config.h"

#if(I2C_MODE == I2C_MAPPING_MODE)

#define      I2C_MASTER_DEVICE            1   //i2c master demo
#define     I2C_SLAVE_DEVICE            2   //i2c slave demo

#define     I2C_DEVICE                    2



#define     BUFF_DATA_LEN                64
#define     I2C_CLK_SPEED                200000
volatile unsigned char i2c_tx_buff[BUFF_DATA_LEN] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
volatile unsigned char i2c_rx_buff[BUFF_DATA_LEN] = {0};
__attribute__((aligned(128))) unsigned char i2c_slave_mapping_buff[128] = {0};

volatile int irq_cnt = 0;
volatile int i2c_read_cnt = 0;
volatile int i2c_write_cnt = 0;
volatile int i2c_event = 0;

void user_init(void)
{
    sleep_ms(2000);
    //1.init the LED pin,for indication
    gpio_set_func(LED1 ,AS_GPIO);
    gpio_set_output_en(LED1, 1);         //enable output
    gpio_set_input_en(LED1 ,0);            //disable input
    gpio_write(LED1, 0);                  //LED Off

    gpio_set_func(LED2 ,AS_GPIO);
    gpio_set_output_en(LED2, 1);         //enable output
    gpio_set_input_en(LED2 ,0);            //disable input
    gpio_write(LED2, 1);                  //LED On
#if(MCU_CORE_B87||MCU_CORE_B89||MCU_CORE_B80 || MCU_CORE_B80B || MCU_CORE_TC321X)
    i2c_gpio_set(I2C_GPIO_SDA,I2C_GPIO_SCL);
#elif(MCU_CORE_B85)
    i2c_gpio_set(I2C_GPIO_SDA_SCL);
#endif

#if(I2C_DEVICE == I2C_MASTER_DEVICE)

    i2c_master_init(0x5C, (unsigned char)(CLOCK_SYS_CLOCK_HZ/(4*I2C_CLK_SPEED)) ); // 200KHz

#elif(I2C_DEVICE == I2C_SLAVE_DEVICE)

    memset(i2c_slave_mapping_buff, 0xde, sizeof(i2c_slave_mapping_buff));
    i2c_slave_init(0x5C, I2C_SLAVE_MAP, (unsigned char *)i2c_slave_mapping_buff + 64);
    irq_set_mask(FLD_IRQ_MIX_CMD_EN);
    irq_enable();

#endif
}

void dump_data(const void *data, int len)
{
    const uint8_t *p = data;

    while (len--)
        printf("%x", *p++);
    printf("\n");
}

void main_loop (void)
{
#if(I2C_DEVICE == I2C_MASTER_DEVICE)
    sleep_ms(500);
    i2c_tx_buff[0]++;
    i2c_write_series(0,0,(unsigned char*)i2c_tx_buff, BUFF_DATA_LEN);
    sleep_ms(500);
    i2c_read_series(0,0,(unsigned char*)i2c_rx_buff, BUFF_DATA_LEN);
    gpio_toggle(LED1);
#endif

    if (i2c_event)
    {
        i2c_event = 0;
        printf("%d: i2c_read_cnt=%d, i2c_write_cnt=%d\n", clock_time(), i2c_read_cnt, i2c_write_cnt);
        dump_data(i2c_slave_mapping_buff, sizeof(i2c_slave_mapping_buff));
    }
}


/**
 * @brief        This function serves to handle the interrupt of MCU
 * @param[in]     none
 * @return         none
 */
_attribute_ram_code_sec_noinline_ void irq_handler(void)
{
    irq_cnt ++;

    unsigned char  irq_status = i2c_get_interrupt_status(FLD_HOST_CMD_IRQ|FLD_HOST_READ_IRQ);

    if(irq_status & FLD_HOST_CMD_IRQ)
    {
        i2c_clear_interrupt_status(FLD_HOST_CMD_IRQ|FLD_HOST_READ_IRQ);//clear all irq status

        if(irq_status & FLD_HOST_READ_IRQ)
        {
            i2c_read_cnt ++;
            gpio_toggle(LED1);
        }
        else
        {
            i2c_write_cnt ++;
            gpio_toggle(LED2);
        }
        i2c_event = 1;
    }
}
#endif

 
一派掌門 二十級
4樓 發表于:2026-6-3 11:28
 
一派掌門 二十級
5樓 發表于:2026-6-3 11:32

全局变量i2c_slave_mapping_buff的数组长度为128。

__attribute__((aligned(128))) unsigned char i2c_slave_mapping_buff[128] = {0};


如果i2c_slave_init指定的缓冲区地址为i2c_slave_mapping_buff + 64,那么I2C读和写都是从&i2c_slave_mapping_buff[64]开始的。读和写是同一片区域。

i2c_slave_init(0x5C, I2C_SLAVE_MAP, (unsigned char *)i2c_slave_mapping_buff + 64);


如果i2c_slave_init指定的缓冲区地址为i2c_slave_mapping_buff,那么I2C写是从&i2c_slave_mapping_buff[0]开始的,但读依然还是从&i2c_slave_mapping_buff[64]开始的。读和写是不同的区域。
i2c_slave_init(0x5C, I2C_SLAVE_MAP, (unsigned char *)i2c_slave_mapping_buff);
 
巨大八爪鱼:这就是为什么i2c例程里面i2c_slave_mapping_buff要加64,为了读和写在同一片内存区域。
  2026-6-3 11:33 回復

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:19 回複數:4
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2026-6-3 11:33
 
©2010-2026 Purasbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。