stm32_eval_i2c_tsensor.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32_eval_i2c_tsensor.c
00004   * @author  MCD Application Team
00005   * @version V4.3.0
00006   * @date    10/15/2010
00007   * @brief   This file provides a set of functions needed to manage the I2C LM75 
00008   *          temperature sensor mounted on STM32xx-EVAL board (refer to stm32_eval.h
00009   *          to know about the boards supporting this sensor). 
00010   *          It implements a high level communication layer for read and write 
00011   *          from/to this sensor. The needed STM32 hardware resources (I2C and 
00012   *          GPIO) are defined in stm32xx_eval.h file, and the initialization is 
00013   *          performed in LM75_LowLevel_Init() function declared in stm32xx_eval.c 
00014   *          file.
00015   *            
00016   *          Note:
00017   *          -----    
00018   *          This driver uses the DMA method to send and receive data on I2C bus,
00019   *          which allows higher efficiency and reliability  of the communication.
00020   *                 
00021   *          You can easily tailor this driver to any other development board, 
00022   *          by just adapting the defines for hardware resources and 
00023   *          LM75_LowLevel_Init() function.
00024   *
00025   *     +-----------------------------------------------------------------+
00026   *     |                        Pin assignment                           |                 
00027   *     +---------------------------------------+-----------+-------------+
00028   *     |  STM32 I2C Pins                       |   STLM75  |   Pin       |
00029   *     +---------------------------------------+-----------+-------------+
00030   *     | LM75_I2C_SDA_PIN/ SDA                 |   SDA     |    1        |
00031   *     | LM75_I2C_SCL_PIN/ SCL                 |   SCL     |    2        |
00032   *     | LM75_I2C_SMBUSALERT_PIN/ SMBUS ALERT  |   OS/INT  |    3        |
00033   *     | .                                     |   GND     |    4  (0V)  |
00034   *     | .                                     |   GND     |    5  (0V)  |
00035   *     | .                                     |   GND     |    6  (0V)  |
00036   *     | .                                     |   GND     |    7  (0V)  |
00037   *     | .                                     |   VDD     |    8  (3.3V)|
00038   *     +---------------------------------------+-----------+-------------+
00039   ******************************************************************************
00040   * @copy
00041   *
00042   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00043   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00044   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00045   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00046   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00047   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00048   *
00049   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00050   */
00051 
00052 /* Includes ------------------------------------------------------------------*/
00053 #include "stm32_eval_i2c_tsensor.h"
00054 
00055 /** @addtogroup Utilities
00056   * @{
00057   */
00058   
00059 /** @addtogroup STM32_EVAL
00060   * @{
00061   */ 
00062 
00063 /** @addtogroup Common
00064   * @{
00065   */
00066   
00067 /** @addtogroup STM32_EVAL_I2C_TSENSOR
00068   * @brief      This file includes the LM75 Temperature Sensor driver of 
00069   *             STM32-EVAL boards.
00070   * @{
00071   */ 
00072 
00073 /** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Types
00074   * @{
00075   */ 
00076 /**
00077   * @}
00078   */
00079 
00080 /** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Defines
00081   * @{
00082   */ 
00083 #define LM75_SD_SET                0x01 /*!< Set SD bit in the configuration register */
00084 #define LM75_SD_RESET              0xFE /*!< Reset SD bit in the configuration register */
00085 /**
00086   * @}
00087   */ 
00088 
00089 /** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Macros
00090   * @{
00091   */ 
00092 /**
00093   * @}
00094   */ 
00095   
00096 /** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Variables
00097   * @{
00098   */ 
00099   
00100 __IO uint32_t  LM75_Timeout = LM75_LONG_TIMEOUT; 
00101 /**
00102   * @}
00103   */ 
00104 
00105 /** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Function_Prototypes
00106   * @{
00107   */ 
00108 static void LM75_DMA_Config(LM75_DMADirection_TypeDef Direction, uint8_t* buffer, uint8_t NumData);
00109 
00110 /**
00111   * @}
00112   */ 
00113 
00114 
00115 /** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Functions
00116   * @{
00117   */ 
00118 
00119 /**
00120   * @brief  DeInitializes the LM75_I2C.
00121   * @param  None
00122   * @retval None
00123   */
00124 void LM75_DeInit(void)
00125 {
00126   LM75_LowLevel_DeInit();
00127 }
00128 
00129 /**
00130   * @brief  Initializes the LM75_I2C.
00131   * @param  None
00132   * @retval None
00133   */
00134 void LM75_Init(void)
00135 {
00136   I2C_InitTypeDef   I2C_InitStructure;
00137   
00138   LM75_LowLevel_Init();
00139   
00140   I2C_DeInit(LM75_I2C);
00141 
00142   /*!< LM75_I2C Init */
00143   I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost;
00144   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
00145   I2C_InitStructure.I2C_OwnAddress1 = 0x00;
00146   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
00147   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
00148   I2C_InitStructure.I2C_ClockSpeed = LM75_I2C_SPEED;
00149   I2C_Init(LM75_I2C, &I2C_InitStructure);
00150 
00151   /*!< Enable SMBus Alert interrupt */
00152   I2C_ITConfig(LM75_I2C, I2C_IT_ERR, ENABLE);
00153 
00154   /*!< LM75_I2C Init */
00155   I2C_Cmd(LM75_I2C, ENABLE);
00156 }
00157 
00158 
00159 /**
00160   * @brief  Configure the DMA Peripheral used to handle communication via I2C.
00161   * @param  None
00162   * @retval None
00163   */
00164 
00165 static void LM75_DMA_Config(LM75_DMADirection_TypeDef Direction, uint8_t* buffer, uint8_t NumData)
00166 {
00167   DMA_InitTypeDef DMA_InitStructure;
00168   
00169   RCC_AHBPeriphClockCmd(LM75_DMA_CLK, ENABLE);
00170   
00171   /* Initialize the DMA_PeripheralBaseAddr member */
00172   DMA_InitStructure.DMA_PeripheralBaseAddr = LM75_I2C_DR;
00173   /* Initialize the DMA_MemoryBaseAddr member */
00174   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer;
00175    /* Initialize the DMA_PeripheralInc member */
00176   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00177   /* Initialize the DMA_MemoryInc member */
00178   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00179   /* Initialize the DMA_PeripheralDataSize member */
00180   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
00181   /* Initialize the DMA_MemoryDataSize member */
00182   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00183   /* Initialize the DMA_Mode member */
00184   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
00185   /* Initialize the DMA_Priority member */
00186   DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
00187   /* Initialize the DMA_M2M member */
00188   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00189   
00190   /* If using DMA for Reception */
00191   if (Direction == LM75_DMA_RX)
00192   {
00193     /* Initialize the DMA_DIR member */
00194     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00195     
00196     /* Initialize the DMA_BufferSize member */
00197     DMA_InitStructure.DMA_BufferSize = NumData;
00198     
00199     DMA_DeInit(LM75_DMA_RX_CHANNEL);
00200     
00201     DMA_Init(LM75_DMA_RX_CHANNEL, &DMA_InitStructure);
00202   }
00203    /* If using DMA for Transmission */
00204   else if (Direction == LM75_DMA_TX)
00205   { 
00206     /* Initialize the DMA_DIR member */
00207     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00208     
00209     /* Initialize the DMA_BufferSize member */
00210     DMA_InitStructure.DMA_BufferSize = NumData;
00211     
00212     DMA_DeInit(LM75_DMA_TX_CHANNEL);
00213     
00214     DMA_Init(LM75_DMA_TX_CHANNEL, &DMA_InitStructure);
00215   }
00216 }
00217 
00218 
00219 /**
00220   * @brief  Checks the LM75 status.
00221   * @param  None
00222   * @retval ErrorStatus: LM75 Status (ERROR or SUCCESS).
00223   */
00224 ErrorStatus LM75_GetStatus(void)
00225 {
00226   uint32_t I2C_TimeOut = I2C_TIMEOUT;
00227 
00228   /*!< Clear the LM75_I2C AF flag */
00229   I2C_ClearFlag(LM75_I2C, I2C_FLAG_AF);
00230 
00231   /*!< Enable LM75_I2C acknowledgement if it is already disabled by other function */
00232   I2C_AcknowledgeConfig(LM75_I2C, ENABLE);
00233 
00234   /*---------------------------- Transmission Phase ---------------------------*/
00235 
00236   /*!< Send LM75_I2C START condition */
00237   I2C_GenerateSTART(LM75_I2C, ENABLE);
00238 
00239   /*!< Test on LM75_I2C EV5 and clear it */
00240   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) && I2C_TimeOut)  /*!< EV5 */
00241   {
00242     I2C_TimeOut--;
00243   }
00244   if (I2C_TimeOut == 0)
00245   {
00246     return ERROR;
00247   }
00248   
00249   I2C_TimeOut = I2C_TIMEOUT;
00250 
00251   /*!< Send STLM75 slave address for write */
00252   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00253 
00254   while ((!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && I2C_TimeOut)/* EV6 */
00255   {
00256     I2C_TimeOut--;
00257   }
00258 
00259   if ((I2C_GetFlagStatus(LM75_I2C, I2C_FLAG_AF) != 0x00) || (I2C_TimeOut == 0))
00260   {
00261     return ERROR;
00262   }
00263   else
00264   {
00265     return SUCCESS;
00266   }
00267 }
00268 /**
00269   * @brief  Read the specified register from the LM75.
00270   * @param  RegName: specifies the LM75 register to be read.
00271   *              This member can be one of the following values:  
00272   *                  - LM75_REG_TEMP: temperature register
00273   *                  - LM75_REG_TOS: Over-limit temperature register
00274   *                  - LM75_REG_THYS: Hysteresis temperature register
00275   * @retval LM75 register value.
00276   */
00277 uint16_t LM75_ReadReg(uint8_t RegName)
00278 {   
00279   uint8_t LM75_BufferRX[2] ={0,0};
00280   uint16_t tmp = 0;    
00281   
00282    /* Test on BUSY Flag */
00283   LM75_Timeout = LM75_LONG_TIMEOUT;
00284   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00285   {
00286     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00287   }
00288   
00289   /* Configure DMA Peripheral */
00290   LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2);  
00291   
00292   /* Enable DMA NACK automatic generation */
00293   I2C_DMALastTransferCmd(LM75_I2C, ENABLE);
00294   
00295   /* Enable the I2C peripheral */
00296   I2C_GenerateSTART(LM75_I2C, ENABLE);
00297   
00298   /* Test on SB Flag */
00299   LM75_Timeout = LM75_FLAG_TIMEOUT;
00300   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00301   {
00302     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00303   }
00304   
00305   /* Send device address for write */
00306   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00307   
00308   /* Test on ADDR Flag */
00309   LM75_Timeout = LM75_FLAG_TIMEOUT;
00310   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
00311   {
00312     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00313   }
00314   
00315   /* Send the device's internal address to write to */
00316   I2C_SendData(LM75_I2C, RegName);  
00317   
00318   /* Test on TXE FLag (data sent) */
00319   LM75_Timeout = LM75_FLAG_TIMEOUT;
00320   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00321   {
00322     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00323   }
00324   
00325   /* Send START condition a second time */  
00326   I2C_GenerateSTART(LM75_I2C, ENABLE);
00327   
00328   /* Test on SB Flag */
00329   LM75_Timeout = LM75_FLAG_TIMEOUT;
00330   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00331   {
00332     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00333   }
00334   
00335   /* Send LM75 address for read */
00336   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver);
00337   
00338   /* Test on ADDR Flag */
00339   LM75_Timeout = LM75_FLAG_TIMEOUT;
00340   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))   
00341   {
00342     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00343   }
00344   
00345   /* Enable I2C DMA request */
00346   I2C_DMACmd(LM75_I2C,ENABLE);
00347   
00348   /* Enable DMA RX Channel */
00349   DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE);
00350   
00351   /* Wait until DMA Transfer Complete */
00352   LM75_Timeout = LM75_LONG_TIMEOUT;
00353   while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG))
00354   {
00355     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00356   }        
00357   
00358   /* Send STOP Condition */
00359   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00360   
00361   /* Disable DMA RX Channel */
00362   DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE);
00363   
00364   /* Disable I2C DMA request */  
00365   I2C_DMACmd(LM75_I2C,DISABLE);
00366   
00367   /* Clear DMA RX Transfer Complete Flag */
00368   DMA_ClearFlag(LM75_DMA_RX_TCFLAG);
00369   
00370   /*!< Store LM75_I2C received data */
00371   tmp = (uint16_t)(LM75_BufferRX[0] << 8);
00372   tmp |= LM75_BufferRX[1];
00373   
00374   /* return a Reg value */
00375   return (uint16_t)tmp;  
00376 }
00377 
00378 /**
00379   * @brief  Write to the specified register of the LM75.
00380   * @param  RegName: specifies the LM75 register to be written.
00381   *              This member can be one of the following values:    
00382   *                  - LM75_REG_TOS: Over-limit temperature register
00383   *                  - LM75_REG_THYS: Hysteresis temperature register
00384   * @param  RegValue: value to be written to LM75 register.  
00385   * @retval None
00386   */
00387 uint8_t LM75_WriteReg(uint8_t RegName, uint16_t RegValue)
00388 {   
00389   uint8_t LM75_BufferTX[2] ={0,0};
00390   LM75_BufferTX[0] = (uint8_t)(RegValue >> 8);
00391   LM75_BufferTX[1] = (uint8_t)(RegValue);
00392   
00393   /* Test on BUSY Flag */
00394   LM75_Timeout = LM75_LONG_TIMEOUT;
00395   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00396   {
00397     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00398   }
00399   
00400   /* Configure DMA Peripheral */
00401   LM75_DMA_Config(LM75_DMA_TX, (uint8_t*)LM75_BufferTX, 2);
00402   
00403   /* Enable the I2C peripheral */
00404   I2C_GenerateSTART(LM75_I2C, ENABLE);
00405   
00406   /* Test on SB Flag */
00407   LM75_Timeout = LM75_FLAG_TIMEOUT;
00408   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB) == RESET) 
00409   {
00410     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00411   }
00412   
00413   /* Transmit the slave address and enable writing operation */
00414   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00415   
00416   /* Test on ADDR Flag */
00417   LM75_Timeout = LM75_FLAG_TIMEOUT;
00418   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
00419   {
00420     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00421   }
00422   
00423   /* Transmit the first address for r/w operations */
00424   I2C_SendData(LM75_I2C, RegName);
00425   
00426   /* Test on TXE FLag (data sent) */
00427   LM75_Timeout = LM75_FLAG_TIMEOUT;
00428   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00429   {
00430     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00431   }
00432   
00433   /* Enable I2C DMA request */
00434   I2C_DMACmd(LM75_I2C,ENABLE);
00435   
00436   /* Enable DMA TX Channel */
00437   DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE);
00438   
00439   /* Wait until DMA Transfer Complete */
00440   LM75_Timeout = LM75_LONG_TIMEOUT;
00441   while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG))
00442   {
00443     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00444   }  
00445   
00446   /* Wait until BTF Flag is set before generating STOP */
00447   LM75_Timeout = LM75_LONG_TIMEOUT;
00448   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))  
00449   {
00450     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00451   }
00452   
00453   /* Send STOP Condition */
00454   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00455   
00456   /* Disable DMA TX Channel */
00457   DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE);
00458   
00459   /* Disable I2C DMA request */  
00460   I2C_DMACmd(LM75_I2C,DISABLE);
00461   
00462   /* Clear DMA TX Transfer Complete Flag */
00463   DMA_ClearFlag(LM75_DMA_TX_TCFLAG);
00464   
00465   return LM75_OK;
00466 }
00467 
00468 /**
00469   * @brief  Read Temperature register of LM75: double temperature value.
00470   * @param  None
00471   * @retval LM75 measured temperature value.
00472   */
00473 uint16_t LM75_ReadTemp(void)
00474 {   
00475   uint8_t LM75_BufferRX[2] ={0,0};
00476   uint16_t tmp = 0;
00477   
00478   /* Test on BUSY Flag */
00479   LM75_Timeout = LM75_LONG_TIMEOUT;
00480   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00481   {
00482     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00483   }
00484   
00485   /* Configure DMA Peripheral */
00486   LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2);  
00487   
00488   /* Enable DMA NACK automatic generation */
00489   I2C_DMALastTransferCmd(LM75_I2C, ENABLE);
00490   
00491   /* Enable the I2C peripheral */
00492   I2C_GenerateSTART(LM75_I2C, ENABLE);
00493   
00494   /* Test on SB Flag */
00495   LM75_Timeout = LM75_FLAG_TIMEOUT;
00496   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00497   {
00498     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00499   }
00500   
00501   /* Send device address for write */
00502   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00503   
00504   /* Test on ADDR Flag */
00505   LM75_Timeout = LM75_FLAG_TIMEOUT;
00506   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
00507   {
00508     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00509   }
00510   
00511   /* Send the device's internal address to write to */
00512   I2C_SendData(LM75_I2C, LM75_REG_TEMP);  
00513   
00514   /* Test on TXE FLag (data sent) */
00515   LM75_Timeout = LM75_FLAG_TIMEOUT;
00516   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00517   {
00518     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00519   }
00520   
00521   /* Send START condition a second time */  
00522   I2C_GenerateSTART(LM75_I2C, ENABLE);
00523   
00524   /* Test on SB Flag */
00525   LM75_Timeout = LM75_FLAG_TIMEOUT;
00526   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00527   {
00528     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00529   }
00530   
00531   /* Send LM75 address for read */
00532   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver);
00533   
00534   /* Test on ADDR Flag */
00535   LM75_Timeout = LM75_FLAG_TIMEOUT;
00536   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))   
00537   {
00538     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00539   }
00540   
00541   /* Enable I2C DMA request */
00542   I2C_DMACmd(LM75_I2C,ENABLE);
00543   
00544   /* Enable DMA RX Channel */
00545   DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE);
00546   
00547   /* Wait until DMA Transfer Complete */
00548   LM75_Timeout = LM75_LONG_TIMEOUT;
00549   while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG))
00550   {
00551     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00552   }        
00553   
00554   /* Send STOP Condition */
00555   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00556   
00557   /* Disable DMA RX Channel */
00558   DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE);
00559   
00560   /* Disable I2C DMA request */  
00561   I2C_DMACmd(LM75_I2C,DISABLE);
00562   
00563   /* Clear DMA RX Transfer Complete Flag */
00564   DMA_ClearFlag(LM75_DMA_RX_TCFLAG);
00565   
00566   /*!< Store LM75_I2C received data */
00567   tmp = (uint16_t)(LM75_BufferRX[0] << 8);
00568   tmp |= LM75_BufferRX[1];    
00569   
00570   /*!< Return Temperature value */
00571   return (uint16_t)(tmp >> 7);
00572 }
00573 
00574 /**
00575   * @brief  Read the configuration register from the LM75.
00576   * @param  None
00577   * @retval LM75 configuration register value.
00578   */
00579 uint8_t LM75_ReadConfReg(void)
00580 {   
00581   uint8_t LM75_BufferRX[2] ={0,0}; 
00582   
00583   /* Test on BUSY Flag */
00584   LM75_Timeout = LM75_LONG_TIMEOUT;
00585   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00586   {
00587     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00588   }
00589   
00590   /* Configure DMA Peripheral */
00591   LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2);  
00592   
00593   /* Enable DMA NACK automatic generation */
00594   I2C_DMALastTransferCmd(LM75_I2C, ENABLE);
00595   
00596   /* Enable the I2C peripheral */
00597   I2C_GenerateSTART(LM75_I2C, ENABLE);
00598   
00599   /* Test on SB Flag */
00600   LM75_Timeout = LM75_FLAG_TIMEOUT;
00601   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00602   {
00603     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00604   }
00605   
00606   /* Send device address for write */
00607   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00608   
00609   /* Test on ADDR Flag */
00610   LM75_Timeout = LM75_FLAG_TIMEOUT;
00611   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
00612   {
00613     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00614   }
00615   
00616   /* Send the device's internal address to write to */
00617   I2C_SendData(LM75_I2C, LM75_REG_CONF);  
00618   
00619   /* Test on TXE FLag (data sent) */
00620   LM75_Timeout = LM75_FLAG_TIMEOUT;
00621   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00622   {
00623     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00624   }
00625   
00626   /* Send START condition a second time */  
00627   I2C_GenerateSTART(LM75_I2C, ENABLE);
00628   
00629   /* Test on SB Flag */
00630   LM75_Timeout = LM75_FLAG_TIMEOUT;
00631   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00632   {
00633     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00634   }
00635   
00636   /* Send LM75 address for read */
00637   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver);
00638   
00639   /* Test on ADDR Flag */
00640   LM75_Timeout = LM75_FLAG_TIMEOUT;
00641   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))   
00642   {
00643     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00644   }
00645   
00646   /* Enable I2C DMA request */
00647   I2C_DMACmd(LM75_I2C,ENABLE);
00648   
00649   /* Enable DMA RX Channel */
00650   DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE);
00651   
00652   /* Wait until DMA Transfer Complete */
00653   LM75_Timeout = LM75_LONG_TIMEOUT;
00654   while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG))
00655   {
00656     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00657   }        
00658   
00659   /* Send STOP Condition */
00660   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00661   
00662   /* Disable DMA RX Channel */
00663   DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE);
00664   
00665   /* Disable I2C DMA request */  
00666   I2C_DMACmd(LM75_I2C,DISABLE);
00667   
00668   /* Clear DMA RX Transfer Complete Flag */
00669   DMA_ClearFlag(LM75_DMA_RX_TCFLAG);
00670   
00671   /*!< Return Temperature value */
00672   return (uint8_t)LM75_BufferRX[0];
00673 }
00674 
00675 /**
00676   * @brief  Write to the configuration register of the LM75.
00677   * @param  RegValue: sepecifies the value to be written to LM75 configuration 
00678   *         register.
00679   * @retval None
00680   */
00681 uint8_t LM75_WriteConfReg(uint8_t RegValue)
00682 {   
00683   uint8_t LM75_BufferTX = 0;  
00684   LM75_BufferTX = (uint8_t)(RegValue);
00685   
00686   /* Test on BUSY Flag */
00687   LM75_Timeout = LM75_LONG_TIMEOUT;
00688   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00689   {
00690     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00691   }
00692   
00693   /* Configure DMA Peripheral */
00694   LM75_DMA_Config(LM75_DMA_TX, (uint8_t*)(&LM75_BufferTX), 1);
00695   
00696   /* Enable the I2C peripheral */
00697   I2C_GenerateSTART(LM75_I2C, ENABLE);
00698   
00699   /* Test on SB Flag */
00700   LM75_Timeout = LM75_FLAG_TIMEOUT;
00701   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB) == RESET) 
00702   {
00703     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00704   }
00705   
00706   /* Transmit the slave address and enable writing operation */
00707   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00708   
00709   /* Test on ADDR Flag */
00710   LM75_Timeout = LM75_FLAG_TIMEOUT;
00711   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
00712   {
00713     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00714   }
00715   
00716   /* Transmit the first address for r/w operations */
00717   I2C_SendData(LM75_I2C, LM75_REG_CONF);
00718   
00719   /* Test on TXE FLag (data sent) */
00720   LM75_Timeout = LM75_FLAG_TIMEOUT;
00721   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00722   {
00723     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00724   }
00725   
00726   /* Enable I2C DMA request */
00727   I2C_DMACmd(LM75_I2C,ENABLE);
00728   
00729   /* Enable DMA TX Channel */
00730   DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE);
00731   
00732   /* Wait until DMA Transfer Complete */
00733   LM75_Timeout = LM75_LONG_TIMEOUT;
00734   while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG))
00735   {
00736     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00737   }  
00738   
00739   /* Wait until BTF Flag is set before generating STOP */
00740   LM75_Timeout = LM75_LONG_TIMEOUT;
00741   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00742   {
00743     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00744   }
00745   
00746   /* Send STOP Condition */
00747   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00748   
00749   /* Disable DMA TX Channel */
00750   DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE);
00751   
00752   /* Disable I2C DMA request */  
00753   I2C_DMACmd(LM75_I2C,DISABLE);
00754   
00755   /* Clear DMA TX Transfer Complete Flag */
00756   DMA_ClearFlag(LM75_DMA_TX_TCFLAG);  
00757   
00758   return LM75_OK;
00759   
00760 }
00761 
00762 /**
00763   * @brief  Enables or disables the LM75.
00764   * @param  NewState: specifies the LM75 new status. This parameter can be ENABLE
00765   *         or DISABLE.  
00766   * @retval None
00767   */
00768 uint8_t LM75_ShutDown(FunctionalState NewState)
00769 {   
00770   uint8_t LM75_BufferRX[2] ={0,0};
00771   uint8_t LM75_BufferTX = 0;
00772   __IO uint8_t RegValue = 0;    
00773   
00774   /* Test on BUSY Flag */
00775   LM75_Timeout = LM75_LONG_TIMEOUT;
00776   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00777   {
00778     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00779   }
00780   
00781   /* Configure DMA Peripheral */
00782   LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2);  
00783   
00784   /* Enable DMA NACK automatic generation */
00785   I2C_DMALastTransferCmd(LM75_I2C, ENABLE);
00786   
00787   /* Enable the I2C peripheral */
00788   I2C_GenerateSTART(LM75_I2C, ENABLE);
00789   
00790   /* Test on SB Flag */
00791   LM75_Timeout = LM75_FLAG_TIMEOUT;
00792   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00793   {
00794     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00795   }
00796   
00797   /* Send device address for write */
00798   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00799   
00800   /* Test on ADDR Flag */
00801   LM75_Timeout = LM75_FLAG_TIMEOUT;
00802   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
00803   {
00804     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00805   }
00806   
00807   /* Send the device's internal address to write to */
00808   I2C_SendData(LM75_I2C, LM75_REG_CONF);  
00809   
00810   /* Test on TXE FLag (data sent) */
00811   LM75_Timeout = LM75_FLAG_TIMEOUT;
00812   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00813   {
00814     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00815   }
00816   
00817   /* Send START condition a second time */  
00818   I2C_GenerateSTART(LM75_I2C, ENABLE);
00819   
00820   /* Test on SB Flag */
00821   LM75_Timeout = LM75_FLAG_TIMEOUT;
00822   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) 
00823   {
00824     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00825   }
00826   
00827   /* Send LM75 address for read */
00828   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver);
00829   
00830   /* Test on ADDR Flag */
00831   LM75_Timeout = LM75_FLAG_TIMEOUT;
00832   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))   
00833   {
00834     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00835   }
00836   
00837   /* Enable I2C DMA request */
00838   I2C_DMACmd(LM75_I2C,ENABLE);
00839   
00840   /* Enable DMA RX Channel */
00841   DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE);
00842   
00843   /* Wait until DMA Transfer Complete */
00844   LM75_Timeout = LM75_LONG_TIMEOUT;
00845   while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG))
00846   {
00847     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00848   }        
00849   
00850   /* Send STOP Condition */
00851   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00852   
00853   /* Disable DMA RX Channel */
00854   DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE);
00855   
00856   /* Disable I2C DMA request */  
00857   I2C_DMACmd(LM75_I2C,DISABLE);
00858   
00859   /* Clear DMA RX Transfer Complete Flag */
00860   DMA_ClearFlag(LM75_DMA_RX_TCFLAG);
00861   
00862   /*!< Get received data */
00863   RegValue = (uint8_t)LM75_BufferRX[0];
00864   
00865   /*---------------------------- Transmission Phase ---------------------------*/
00866   
00867   /*!< Enable or disable SD bit */
00868   if (NewState != DISABLE)
00869   {
00870     /*!< Enable LM75 */
00871     LM75_BufferTX = RegValue & LM75_SD_RESET;
00872   }
00873   else
00874   {
00875     /*!< Disable LM75 */
00876     LM75_BufferTX = RegValue | LM75_SD_SET;
00877   }  
00878   
00879   /* Test on BUSY Flag */
00880   LM75_Timeout = LM75_LONG_TIMEOUT;
00881   while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) 
00882   {
00883     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00884   }
00885   
00886   /* Configure DMA Peripheral */
00887   LM75_DMA_Config(LM75_DMA_TX, (uint8_t*)(&LM75_BufferTX), 1);
00888   
00889   /* Enable the I2C peripheral */
00890   I2C_GenerateSTART(LM75_I2C, ENABLE);
00891   
00892   /* Test on SB Flag */
00893   LM75_Timeout = LM75_FLAG_TIMEOUT;
00894   while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB) == RESET) 
00895   {
00896     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00897   }
00898   
00899   /* Transmit the slave address and enable writing operation */
00900   I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
00901   
00902   /* Test on ADDR Flag */
00903   LM75_Timeout = LM75_FLAG_TIMEOUT;
00904   while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
00905   {
00906     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00907   }
00908   
00909   /* Transmit the first address for r/w operations */
00910   I2C_SendData(LM75_I2C, LM75_REG_CONF);
00911   
00912   /* Test on TXE FLag (data sent) */
00913   LM75_Timeout = LM75_FLAG_TIMEOUT;
00914   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00915   {
00916     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00917   }
00918   
00919   /* Enable I2C DMA request */
00920   I2C_DMACmd(LM75_I2C,ENABLE);
00921   
00922   /* Enable DMA TX Channel */
00923   DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE);
00924   
00925   /* Wait until DMA Transfer Complete */
00926   LM75_Timeout = LM75_LONG_TIMEOUT;
00927   while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG))
00928   {
00929     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00930   }  
00931   
00932   /* Wait until BTF Flag is set before generating STOP */
00933   LM75_Timeout = LM75_LONG_TIMEOUT;
00934   while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))  
00935   {
00936     if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
00937   }
00938   
00939   /* Send STOP Condition */
00940   I2C_GenerateSTOP(LM75_I2C, ENABLE);
00941   
00942   /* Disable DMA TX Channel */
00943   DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE);
00944   
00945   /* Disable I2C DMA request */  
00946   I2C_DMACmd(LM75_I2C,DISABLE);
00947   
00948   /* Clear DMA TX Transfer Complete Flag */
00949   DMA_ClearFlag(LM75_DMA_TX_TCFLAG);  
00950   
00951   return LM75_OK;
00952 }
00953 
00954 /**
00955   * @}
00956   */
00957 
00958 
00959 /**
00960   * @}
00961   */
00962 
00963 
00964 /**
00965   * @}
00966   */
00967 
00968 /**
00969   * @}
00970   */
00971 
00972 /**
00973   * @}
00974   */
00975 
00976 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32