Dear Sir,
only two commands are supported, one is write channel (xxxxxB2B1B0), the other is read channel status(xxxxxB2B1B0).
The following is my code, are preamble filled correctly? any comments will be appreciated!
/*
* pca9540b.c
*
* Created on: 2017年9月19日
* Author: lenovo
*/
#include <cyu3error.h>
#include <cyu3i2c.h>
#include <cyu3os.h>
//#include <cyu3types.h>
//#include "pca9540b.h"
#define PCA9540B_IIC_ADDR 0x70
const uint8_t PCA9540B_IIC_WRITEADDR = 0xe0;//PCA9540B_IIC_ADDR << 1;
const uint8_t PCA9540B_IIC_READADDR = 0xe1;//(PCA9540B_IIC_ADDR << 1)|1;
// control byte true table
// D7 D6 D5 D4 D3 D2 D1 D0 channel
// x x x x x 0 x x no channel selected
// x x x x x 1 0 0 CH0 enable
// x x x x x 1 0 1 CH1 enable
// x x x x x 1 1 x no channel selected
// 0 0 0 0 0 0 0 0 no channel selected;power-up default state
const uint8_t PCA9540B_CH[3] =
{
0x04, // channel 0
0x05, // channel 1
0x00 // both of channels 0,1 are disable
};
uint32_t pca9540b_SetCh(uint32_t ch)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
uint8_t data[32] ={0}; //just for api calling,if NULL is called,0x41 will be return by CyU3PI2cTransmitBytes()
static uint32_t ch_1 = 2; // PCA9540B_CH[2] = 0x00, all are disable
if(ch > 2) return CY_U3P_ERROR_BAD_ARGUMENT; // only 0,1 channel is permitted
if ( ch == ch_1) return CY_U3P_SUCCESS; // no real switch action
preamble.buffer[0] = PCA9540B_IIC_WRITEADDR;
preamble.buffer[1] = PCA9540B_CH[ch];
preamble.length = 2;
preamble.ctrlMask = 0x0000;
status = CyU3PI2cTransmitBytes(&preamble,data,0,0);
if(status != CY_U3P_SUCCESS)
CyU3PDebugPrint(4,"\n\rErr: pca9540b_SetCh failed! status = %x\n\r",status);
else
ch_1 = ch;
return status;
}
// read channel configuration
uint32_t pca9540b_GetCh(uint8_t *ch_status)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
preamble.buffer[0] = PCA9540B_IIC_READADDR;
preamble.length = 1;
preamble.ctrlMask = 0x0000;
status = CyU3PI2cReceiveBytes(&preamble,ch_status,1,0); // only one byte read
CyU3PThreadSleep(1);
if (status != CY_U3P_SUCCESS)
CyU3PDebugPrint(4,"\n\rErr: pca9540b_GetCh failed! status = %x\n\r",status);
return status;
}
Solved! Go to Solution.
Dear Sir,
I just put control byte in data buffer, other than in preamble.buffer , then it works fine.
preamble.buffer[0] = PCA9540B_IIC_WRITEADDR;
//preamble.buffer[1] = PCA9540B_CH[ch];
//preamble.length = 2;
preamble.length = 1;
preamble.ctrlMask = 0x0000;
data[0] = PCA9540B_CH[ch]; // fill control byte in data section and send count is 1
status = CyU3PI2cTransmitBytes(&preamble,data,1,0);
it seems DATA section shouldn't be NULL.
Thanks.
David