blob: 7cd7e3cfd56328bfcc2db19b90bd6b4c53eaa848 [file] [log] [blame]
/******************************************************************************
* @file app_i2c_master.c
*
* @brief for TLSR chips
*
* @author public@telink-semi.com;
* @date Sep. 30, 2010
*
* @attention
*
* Copyright (C) 2019-2020 Telink Semiconductor (Shanghai) Co., Ltd.
*
* 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 "tl_common.h"
#include "drivers.h"
#if(DRIVER_TEST_MODE == TEST_IIC && I2C_DEMO_SELECT == I2C_DEMO_MASTER)
/**************************************************************************************
8258 Demo Code Config
1. select i2c demo: master or slave( config it in app_config.h )
2. if i2c slave demo, select slave working mode: mapping mode or dma mode
3. if i2c master demo, select the peer device i2c slave address mode:
826x/8255 mapping mode/dma mode or other not telink's device
here selecting peer slave device mode is for master sending address byte length
*************************************************************************************/
#if(I2C_DEMO_SELECT == I2C_DEMO_MASTER)
//826x: 8261/8266/8267/8269
//825x: 8255/8258
//82xx: 8261/8266/8267/8269 and 8255/8258
#define SLAVE_TELINK_825x_DMA_MODE 1 //slave 825x dma mode, master send 3 byte sram address(0x40000~0x4ffff)
#define SLAVE_TELINK_82xx_MAPPING_MODE 2 //slave 826x/825x mapping mode, master no need send address(address length is 0)
#define SLAVE_TELINK_826x_DMA_MODE 3 //slave 826x dma mode, master send 2 byte sram address(0x8000~0xffff)
#define SLAVE_OTHER_I2C_DEVICE 4 //slave not telink mcu, address length may be 1 or 2 or others.
#define PEER_SLAVE_DEVICE SLAVE_TELINK_825x_DMA_MODE
#endif
/**************************************************************************************
*
*************************************************************************************/
/*************** i2c master data buffer *****************/
//write buff
volatile unsigned char i2c_master_tx_buff[DBG_DATA_LEN] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
//read buff
volatile unsigned char i2c_master_rx_buff[DBG_DATA_LEN] = {0};
unsigned char master_rx_buff_debug[DBG_DATA_LEN *DBG_DATA_NUM]; //store i2c master reading data here to debug
int master_rx_index = 0;
void i2c_master_test_init(void)
{
//I2C pin set
i2c_gpio_set(I2C_GPIO_SDA_C0,I2C_GPIO_SCL_C1); //SDA/CK : C0/C1
//slave device id 0x5C(write) 0x5D(read)
//i2c clock 200K, only master need set i2c clock
i2c_master_init(0x5C, (unsigned char)(CLOCK_SYS_CLOCK_HZ/(4*200000)) );
}
void i2c_master_mainloop(void)
{
WaitMs(1000); //1 S
#if (PEER_SLAVE_DEVICE == SLAVE_TELINK_825x_DMA_MODE)
i2c_master_tx_buff[0] += 1;
//825x slave dma mode, sram address(0x40000~0x4FFFF) length should be 3 byte
i2c_write_series(SLAVE_DMA_MODE_ADDR_WRITE, 3, (unsigned char *)i2c_master_tx_buff, DBG_DATA_LEN);
i2c_read_series(SLAVE_DMA_MODE_ADDR_READ, 3, (unsigned char *)i2c_master_rx_buff, DBG_DATA_LEN);
/*********** copy the data read by i2c master from slave for debug ****************/
memcpy( (unsigned char *)(master_rx_buff_debug + master_rx_index*DBG_DATA_LEN), (unsigned char *)i2c_master_rx_buff, DBG_DATA_LEN);
master_rx_index ++;
if(master_rx_index>=DBG_DATA_NUM){
master_rx_index = 0;
}
#elif (PEER_SLAVE_DEVICE == SLAVE_TELINK_82xx_MAPPING_MODE)
i2c_master_tx_buff[0] += 1;
//slave mapping mode, no need send address information, so address length should be 0
i2c_write_series(0, 0, (unsigned char*)i2c_master_tx_buff, DBG_DATA_LEN);
i2c_read_series(0, 0, (unsigned char*)i2c_master_rx_buff, DBG_DATA_LEN);
/*********** copy the data read by i2c master from slave for debug ****************/
memcpy((unsigned char*)(master_rx_buff_debug + master_rx_index*DBG_DATA_LEN), (unsigned char*)i2c_master_rx_buff, DBG_DATA_LEN);
master_rx_index ++;
if(master_rx_index>=DBG_DATA_NUM){
master_rx_index = 0;
}
#elif (PEER_SLAVE_DEVICE == SLAVE_TELINK_826x_DMA_MODE)
need add code here
#elif (PEER_SLAVE_DEVICE == SLAVE_OTHER_I2C_DEVICE)
need add code here
#endif
}
_attribute_ram_code_ void app_i2c_test_irq_proc(void)
{
}
#endif //end of I2C_DEMO_SELECT == I2C_DEMO_MASTER