Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
i2cconfig.cpp
00001 //***************************************************************************** 00002 // i2cconfig.c 00003 // 00004 // I2C features APIs 00005 // 00006 // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ 00007 // 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions 00011 // are met: 00012 // 00013 // Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the 00019 // distribution. 00020 // 00021 // Neither the name of Texas Instruments Incorporated nor the names of 00022 // its contributors may be used to endorse or promote products derived 00023 // from this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00026 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00027 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00028 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00029 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00030 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00031 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00032 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00033 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00034 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00035 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 //***************************************************************************** 00038 //***************************************************************************** 00039 // 00040 //! \addtogroup i2cconfig 00041 //! @{ 00042 // 00043 //***************************************************************************** 00044 #include <stdbool.h> 00045 #include <stdint.h> 00046 #include "mbed.h" 00047 00048 #include "cli_uart.h" 00049 #include "osi.h" 00050 00051 #include "i2cconfig.h" 00052 #include "cli_uart.h" 00053 #include "myBoardInit.h" 00054 00055 00056 00057 I2C i2c(PB_9, PB_8); 00058 00059 OsiLockObj_t g_i2cLock; 00060 00061 00062 //***************************************************************************** 00063 // 00064 //! I2CInit 00065 //! 00066 //! \param Delay 00067 //! \return None 00068 // 00069 //***************************************************************************** 00070 uint32_t I2CInit() 00071 { 00072 00073 i2c.frequency (100000); 00074 00075 return 0; 00076 } 00077 //**************************************************************************** 00078 // 00079 //! Invokes the I2C driver APIs to read from the device. This assumes the 00080 //! device local address to read from is set using the I2CWrite API. 00081 //! 00082 //! \param ucDevAddr is the device I2C slave address 00083 //! \param ucBuffer is the pointer to the read data to be placed 00084 //! \param ulSize is the length of data to be read 00085 //! \param ucFlags Flag 00086 //! 00087 //! This function works in a polling mode, 00088 //! 1. Writes the device register address to be written to. 00089 //! 2. In a loop, reads all the bytes over I2C 00090 //! 00091 //! \return 0: Success, < 0: Failure. 00092 // 00093 //**************************************************************************** 00094 int32_t I2CBufferRead(int32_t ucDevAddr, uint8_t *ucBuffer, 00095 int32_t ulSize, unsigned char ucFlags) 00096 { 00097 // Uart_Write((uint8_t*)"I2CBufferRead \n\r"); 00098 /* 00099 i2c.start(); 00100 00101 // Set I2C slave read address 00102 i2c.write(ucDevAddr); 00103 if(ulSize == 1){ 00104 ucBuffer[0] = i2c.read(0); 00105 }else{ 00106 for(int i=0;i<ulSize;i++){ 00107 ucBuffer[i] = i2c.read(0); 00108 } 00109 } 00110 00111 i2c.stop(); 00112 */ 00113 int32_t err = 0; 00114 00115 // RTOS_MUTEX_ACQUIRE(&g_i2cLock); 00116 00117 err = i2c.read(ucDevAddr,(char*)ucBuffer,ulSize); 00118 if(err == 1){ 00119 Uart_Write((uint8_t*)"Return error I2C read\n\r"); 00120 // RTOS_MUTEX_RELEASE(&g_i2cLock); 00121 return -1; 00122 } 00123 wait_ms(1); 00124 // RTOS_MUTEX_RELEASE(&g_i2cLock); 00125 return 0; 00126 } 00127 //**************************************************************************** 00128 // 00129 //! Invokes the I2C driver APIs to write to the specified address 00130 //! 00131 //! \param ucDevAddr is the device I2C slave address 00132 //! \param ucBuffer is the pointer to the data to be written 00133 //! \param ulSize is the length of data to be written 00134 //! \param ucFlags 00135 //! 00136 //! This function works in a polling mode, 00137 //! 1. Writes the device register address to be written to. 00138 //! 2. In a loop, writes all the bytes over I2C 00139 //! 00140 //! \return 0: Success, < 0: Failure. 00141 // 00142 //**************************************************************************** 00143 00144 int32_t I2CBufferWrite(int32_t ucDevAddr, uint8_t *ucBuffer, 00145 int32_t ulSize,unsigned char ucFlags) 00146 { 00147 // Uart_Write((uint8_t*)"I2CBufferWrite \n\r"); 00148 /* 00149 i2c.start(); 00150 00151 // Set I2C slave write address 00152 i2c.write(ucDevAddr); 00153 00154 if(ulSize == 1){ 00155 i2c.write(ucBuffer[0]); 00156 }else{ 00157 for(int i=0;i<ulSize;i++){ 00158 i2c.write(ucBuffer[i]); 00159 } 00160 } 00161 00162 i2c.stop(); 00163 */ 00164 int32_t err = 0; 00165 00166 // RTOS_MUTEX_ACQUIRE(&g_i2cLock); 00167 err = i2c.write(ucDevAddr,(char*)ucBuffer,ulSize); 00168 if(err == 1){ 00169 Uart_Write((uint8_t*)"Return error I2C write\n\r"); 00170 // RTOS_MUTEX_RELEASE(&g_i2cLock); 00171 return -1; 00172 } 00173 wait_ms(1); 00174 // RTOS_MUTEX_RELEASE(&g_i2cLock); 00175 return 0; 00176 } 00177 00178 //***************************************************************************** 00179 // 00180 // Close the Doxygen group. 00181 //! @} 00182 // 00183 //***************************************************************************** 00184 00185
Generated on Tue Jul 12 2022 22:22:38 by
1.7.2