- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello folks,
I've a question regarding I2C communication in CY8CKIT-042 pioneer board.
What I want to do is a simple I2C communication; Write a byte and read a value from slave. It could be programmed in Arduino like below.
#include <Wire.h>
const int address = 0x40;
void setup()
{
// open the serial port
Serial.begin(57600);
// Start the I2C module
Wire.begin();
}
void loop()
{
// start a transmission to the slave
Wire.beginTransmission(address);
// request register 0
Wire.write((byte)0);
// issue the request
Wire.endTransmission();
// prepare to receive on byte
Wire.requestFrom(address, 1);
// wait for the data to be ready
while(!Wire.available());
// print the temperature
Serial.println(Wire.read(),DEC);
delay(1000);
}
I started this program with an example project, I2cCommMaster. When I try this project with my commands and slave address, I found that the master couldn't complete write, so my program holds here;
[main.c @ line:85]
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Waits until master completes write transfer */
}
Could you guys help me to make this code run? I attach all my code below for your information.
Thanks for your help in advance.
- <main. c >
-
- /*******************************************************************************
- * File Name: main.c
- *
- ********************************************************************************
- * Copyright 2014, Cypress Semiconductor Corporation. All rights reserved.
- * This software is owned by Cypress Semiconductor Corporation and is protected
- * by and subject to worldwide patent and copyright laws and treaties.
- * Therefore, you may use this software only as provided in the license agreement
- * accompanying the software package from which you obtained this software.
- * CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *******************************************************************************/
-
- #include "project.h"
- #include "header.h"
-
-
- /*******************************************************************************
- * Function Name: main
- ********************************************************************************
- * Summary:
- * The main function performs the following actions:
- * 1. Turns off the RGB LED.
- * 2. Starts the I2C master (SCB mode) component.
- * 3. Enables global interrupts.
- * 4. Sends command and status packets to the slave every 500mS.
- *
- * Parameters:
- * None
- *
- * Return:
- * None
- *
- *******************************************************************************/
- void main ( )
- {
- uint8 command = CMD_MSR_REL_HUM_NO_HOLD_MASTER ;
-
- RGB_LED_OFF ;
-
- I2CM_Start ( ) ;
-
-
- CyGlobalIntEnable ;
-
- /***************************************************************************
- * Main polling loop
- ***************************************************************************/
- for ( ;; )
- {
-
- if (0u == WriteCommandPacket (command ) )
- {
-
- if (0u == ReadStatusPacket ( ) )
- {
- RGB_LED_OFF ; /* Turn off status LED */
- }
- }
-
- CyDelay (500u ) ; /* Send command to the I2C slave every 500mS */
- }
- }
-
-
- /*******************************************************************************
- * WriteCommandPacket(): Writes command packet to I2C slave
- *******************************************************************************/
- uint32 WriteCommandPacket ( uint8 cmd )
- {
- uint8 buffer [BUFFER_SIZE ] ;
- uint32 status = TRANSFER_ERROR ;
-
- /* Initialize buffer with packet */
- buffer [PACKET_SOP_POS ] = PACKET_SOP ;
- buffer [PACKET_CMD_POS ] = cmd ;
- buffer [PACKET_EOP_POS ] = PACKET_EOP ;
-
- ( void ) I2CM_I2CMasterWriteBuf (I2C_SLAVE_ADDR , buffer , PACKET_SIZE , I2CM_I2C_MODE_COMPLETE_XFER ) ;
- //I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR, buffer, PACKET_SIZE, I2CM_I2C_MODE_COMPLETE_XFER);
- RGB_LED_ON_RED ;
- while (0u == (I2CM_I2CMasterStatus ( ) & I2CM_I2C_MSTAT_WR_CMPLT ) )
- {
- /* Waits until master completes write transfer */
- }
- RGB_LED_ON_GREEN ;
-
- /* Displays transfer status */
- if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus ( ) ) )
- {
- RGB_LED_ON_GREEN ;
-
- /* Check if all bytes was written */
- if (I2CM_I2CMasterGetWriteBufSize ( ) == BUFFER_SIZE )
- {
- status = TRANSFER_CMPLT ;
- }
- }
- else
- {
- RGB_LED_ON_RED ;
- }
-
- ( void ) I2CM_I2CMasterClearStatus ( ) ;
-
- return (status ) ;
- }
-
-
- /*******************************************************************************
- * ReadStatusPacket(): Reads status packet from I2C slave
- *******************************************************************************/
- uint32 ReadStatusPacket ( void )
- {
- uint8 buffer [BUFFER_SIZE ] ;
- uint32 status = TRANSFER_ERROR ;
-
- ( void ) I2CM_I2CMasterReadBuf (I2C_SLAVE_ADDR , buffer , PACKET_SIZE , I2CM_I2C_MODE_COMPLETE_XFER ) ;
-
- while (0u == (I2CM_I2CMasterStatus ( ) & I2CM_I2C_MSTAT_RD_CMPLT ) )
- {
- /* Waits until master complete read transfer */
- }
-
- /* Displays transfer status */
- if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus ( ) ) )
- {
- RGB_LED_ON_GREEN ;
-
- /* Check packet structure */
- if ( (I2CM_I2CMasterGetReadBufSize ( ) == BUFFER_SIZE ) &&
- (PACKET_SOP == buffer [PACKET_SOP_POS ] ) &&
- (PACKET_EOP == buffer [PACKET_EOP_POS ] ) )
- {
- /* Check packet status */
- if (STS_CMD_DONE == buffer [PACKET_STS_POS ] )
- {
- status = TRANSFER_CMPLT ;
- }
- }
- }
- else
- {
- RGB_LED_ON_RED ;
- status = TRANSFER_ERROR ;
- }
-
- ( void ) I2CM_I2CMasterClearStatus ( ) ;
-
- return (status ) ;
- }
-
-
- /* [] END OF FILE */
-
-
-
-
-
-
-
- <header. h >
- /*******************************************************************************
- * File Name: header.h
- *
- ********************************************************************************
- * Copyright 2014, Cypress Semiconductor Corporation. All rights reserved.
- * This software is owned by Cypress Semiconductor Corporation and is protected
- * by and subject to worldwide patent and copyright laws and treaties.
- * Therefore, you may use this software only as provided in the license agreement
- * accompanying the software package from which you obtained this software.
- * CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *******************************************************************************/
-
- #include <project.h>
-
- /***************************************
- * Function Prototypes
- ****************************************/
-
- /* Function prototypes */
- uint32 CheckSwitchState ( void ) ;
- uint32 WriteCommandPacket ( uint8 cmd ) ;
- uint32 ReadStatusPacket ( void ) ;
-
-
- /***************************************
- * Constants
- ****************************************/
-
- /* I2C slave address to communicate with */
- #define I2C_SLAVE_ADDR (0x40u) //(0x48u)//
-
- /* Buffer and packet size */
- #define BUFFER_SIZE (3u)
- #define PACKET_SIZE (BUFFER_SIZE)
-
- /* Packet positions */
- #define PACKET_SOP_POS (0u)
- #define PACKET_CMD_POS (1u)
- #define PACKET_STS_POS (PACKET_CMD_POS)
- #define PACKET_EOP_POS (2u)
-
- /* Start and end of packet markers */
- #define PACKET_SOP (0x01u)
- #define PACKET_EOP (0x17u)
-
- /* Command valid status */
- #define STS_CMD_DONE (0x00u)
- #define STS_CMD_FAIL (0xFFu)
-
- /* Command valid status */
- #define TRANSFER_CMPLT (0x00u)
- #define TRANSFER_ERROR (0xFFu)
-
- /* Commands set */
- //#define CMD_SET_OFF (0u)
- //#define CMD_SET_RED (1u)
- //#define CMD_SET_GREEN (2u)
- //#define CMD_SET_BLUE (3u)
- #define CMD_MSR_REL_HUM_HOLD_MASTER (0xE5u)
- #define CMD_MSR_REL_HUM_NO_HOLD_MASTER (0xF5u)
- #define CMD_MSR_TEMP_HOLD_MASTER (0xE3u)
- #define CMD_MSR_TEMP_NO_HOLD_MASTER (0xF3u)
- #define CMD_RESET (0xFEu)
-
-
- /***************************************
- * Macros
- ****************************************/
-
- /* Set LED RED color */
- #define RGB_LED_ON_RED \
- do{ \
- LED_RED_Write (0u); \
- LED_GREEN_Write(1u); \
- }while(0)
-
- /* Set LED GREEN color */
- #define RGB_LED_ON_GREEN \
- do{ \
- LED_RED_Write (1u); \
- LED_GREEN_Write(0u); \
- }while(0)
-
- /* Set LED BLUE color */
- #define RGB_LED_ON_BLUE \
- do{ \
- LED_RED_Write (1u); \
- LED_GREEN_Write(1u); \
- }while(0)
-
- /* Set LED TURN OFF */
- #define RGB_LED_OFF \
- do{ \
- LED_RED_Write (1u); \
- LED_GREEN_Write(1u); \
- }while(0)
-
-
- /* [] END OF FILE */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Generally speaking its better if you post a project archive so
all setup/settings/configuration can be seen.
Look at I2C clk and data lines to make sure pullups in place
and you see legit logic level signals.
“File” Creator
“Create Workspace Bundle”
If you are using a Pioneer board be aware of fixed pin assignments
on the board. Also PSOC 4 itself has dedicated pins for various
components. See table in -
http://www.cypress.com/?rID=93401 AN86439 - PSoC® 4 - Using GPIO Pins
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Dana for your help. I just figured out that there was my misunderstanding at packet allocation. Once I set the packet design, I could successfully communicate with my slave device. I will make sure to attach the archive if I've a question next time. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are always welcome!
Dana.