NUCLEO-NFC01A1 board drivers.

Dependents:   Nucleo_NFC_Example I2C_NFC_Master Print_Entire_Nucleo_NFC01A1_Memory

Fork of X-NUCLEO-NFC01A1 by Enrico Gregoratto

Warning: Deprecated!

Official and up-to-date drivers can be found at this link.

Committer:
EnricoG
Date:
Sun Jan 04 18:21:34 2015 +0000
Revision:
4:1814ea3853a7
Parent:
3:0528e68eac4a
Updated poll delay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EnricoG 3:0528e68eac4a 1 /**
EnricoG 3:0528e68eac4a 2 ******************************************************************************
EnricoG 3:0528e68eac4a 3 * @file drv_I2C_M24SR_mbed.cpp
EnricoG 3:0528e68eac4a 4 * @author MMY Application Team
EnricoG 3:0528e68eac4a 5 * @version V1.0.0
EnricoG 3:0528e68eac4a 6 * @date 19-March-2014
EnricoG 3:0528e68eac4a 7 * @brief This file provides a set of functions needed to manage the I2C of
EnricoG 3:0528e68eac4a 8 * the M24SR device.
EnricoG 3:0528e68eac4a 9 ******************************************************************************
EnricoG 3:0528e68eac4a 10 * @attention
EnricoG 3:0528e68eac4a 11 *
EnricoG 3:0528e68eac4a 12 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
EnricoG 3:0528e68eac4a 13 *
EnricoG 3:0528e68eac4a 14 * Licensed under MMY-ST Liberty SW License Agreement V2, (the "License");
EnricoG 3:0528e68eac4a 15 * You may not use this file except in compliance with the License.
EnricoG 3:0528e68eac4a 16 * You may obtain a copy of the License at:
EnricoG 3:0528e68eac4a 17 *
EnricoG 3:0528e68eac4a 18 * http://www.st.com/software_license_agreement_liberty_v2
EnricoG 3:0528e68eac4a 19 *
EnricoG 3:0528e68eac4a 20 * Unless required by applicable law or agreed to in writing, software
EnricoG 3:0528e68eac4a 21 * distributed under the License is distributed on an "AS IS" BASIS,
EnricoG 3:0528e68eac4a 22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
EnricoG 3:0528e68eac4a 23 * See the License for the specific language governing permissions and
EnricoG 3:0528e68eac4a 24 * limitations under the License.
EnricoG 3:0528e68eac4a 25 *
EnricoG 3:0528e68eac4a 26 ******************************************************************************
EnricoG 3:0528e68eac4a 27 */
EnricoG 3:0528e68eac4a 28
EnricoG 3:0528e68eac4a 29 /* Includes ------------------------------------------------------------------*/
EnricoG 3:0528e68eac4a 30 #include "drv_I2C_M24SR.h"
EnricoG 3:0528e68eac4a 31 #include "mbed.h"
EnricoG 3:0528e68eac4a 32
EnricoG 3:0528e68eac4a 33 #define M24SR_SDA_PIN D14
EnricoG 3:0528e68eac4a 34 #define M24SR_SCL_PIN D15
EnricoG 3:0528e68eac4a 35 #define M24SR_GPO_PIN D12
EnricoG 3:0528e68eac4a 36 #define M24SR_RFDIS_PIN D11
EnricoG 3:0528e68eac4a 37
EnricoG 3:0528e68eac4a 38 #if defined __MBED__ /* F4 */
EnricoG 3:0528e68eac4a 39 #define M24SR_I2C_SPEED_10 10000
EnricoG 3:0528e68eac4a 40 #define M24SR_I2C_SPEED_100 100000
EnricoG 3:0528e68eac4a 41 #define M24SR_I2C_SPEED_400 400000
EnricoG 3:0528e68eac4a 42 #define M24SR_I2C_SPEED_1000 M24SR_I2C_SPEED_400 // 1MHz I2C not supported by mbed
EnricoG 3:0528e68eac4a 43 #else
EnricoG 3:0528e68eac4a 44 #error "Use only for mbed"
EnricoG 3:0528e68eac4a 45 #endif
EnricoG 3:0528e68eac4a 46
EnricoG 3:0528e68eac4a 47 #define M24SR_I2C_SPEED M24SR_I2C_SPEED_400
EnricoG 3:0528e68eac4a 48
EnricoG 3:0528e68eac4a 49 /* MBED objects */
EnricoG 3:0528e68eac4a 50 I2C i2c(I2C_SDA, I2C_SCL);
EnricoG 3:0528e68eac4a 51 DigitalOut pRFD(M24SR_RFDIS_PIN);
EnricoG 3:0528e68eac4a 52 DigitalIn pGPO(M24SR_GPO_PIN);
EnricoG 3:0528e68eac4a 53
EnricoG 3:0528e68eac4a 54 static uint8_t uSynchroMode = M24SR_WAITINGTIME_POLLING;
EnricoG 3:0528e68eac4a 55
EnricoG 3:0528e68eac4a 56 /**
EnricoG 3:0528e68eac4a 57 * @brief This function initializes the M24SR_I2C interface
EnricoG 3:0528e68eac4a 58 * @retval None
EnricoG 3:0528e68eac4a 59 */
EnricoG 3:0528e68eac4a 60 void M24SR_I2CInit(void)
EnricoG 3:0528e68eac4a 61 {
EnricoG 3:0528e68eac4a 62 i2c.frequency(M24SR_I2C_SPEED);
EnricoG 3:0528e68eac4a 63 pRFD.write(0);
EnricoG 3:0528e68eac4a 64 pGPO.mode(PullNone);
EnricoG 3:0528e68eac4a 65 }
EnricoG 3:0528e68eac4a 66
EnricoG 3:0528e68eac4a 67 /**
EnricoG 3:0528e68eac4a 68 * @brief this functions configure I2C synchronization mode
EnricoG 3:0528e68eac4a 69 * @param mode :
EnricoG 3:0528e68eac4a 70 * @retval None
EnricoG 3:0528e68eac4a 71 */
EnricoG 3:0528e68eac4a 72 void M24SR_SetI2CSynchroMode(uint8_t mode)
EnricoG 3:0528e68eac4a 73 {
EnricoG 3:0528e68eac4a 74 #if defined (I2C_GPO_SYNCHRO_ALLOWED)
EnricoG 3:0528e68eac4a 75 uSynchroMode = mode;
EnricoG 3:0528e68eac4a 76 #else
EnricoG 3:0528e68eac4a 77 if(mode == M24SR_WAITINGTIME_GPO || mode == M24SR_INTERRUPT_GPO)
EnricoG 3:0528e68eac4a 78 uSynchroMode = M24SR_WAITINGTIME_POLLING;
EnricoG 3:0528e68eac4a 79 else
EnricoG 3:0528e68eac4a 80 uSynchroMode = mode;
EnricoG 3:0528e68eac4a 81 #endif /* I2C_GPO_SYNCHRO_ALLOWED */
EnricoG 3:0528e68eac4a 82 }
EnricoG 3:0528e68eac4a 83
EnricoG 3:0528e68eac4a 84 /**
EnricoG 3:0528e68eac4a 85 * @brief This functions polls the I2C interface
EnricoG 3:0528e68eac4a 86 * @retval M24SR_STATUS_SUCCESS : the function is succesful
EnricoG 3:0528e68eac4a 87 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 3:0528e68eac4a 88 */
EnricoG 3:0528e68eac4a 89 int8_t M24SR_PollI2C(void)
EnricoG 3:0528e68eac4a 90 {
EnricoG 3:0528e68eac4a 91 int status = 1;
EnricoG 3:0528e68eac4a 92 uint32_t nbTry = 0;
EnricoG 3:0528e68eac4a 93 char buffer;
EnricoG 3:0528e68eac4a 94 //
EnricoG 3:0528e68eac4a 95 wait_ms(M24SR_POLL_DELAY);
EnricoG 3:0528e68eac4a 96 while (status != 0 && nbTry < M24SR_I2C_POLLING)
EnricoG 3:0528e68eac4a 97 {
EnricoG 3:0528e68eac4a 98 status = i2c.read(M24SR_ADDR, &buffer, 1, false);
EnricoG 3:0528e68eac4a 99 nbTry++;
EnricoG 3:0528e68eac4a 100 }
EnricoG 3:0528e68eac4a 101 if (status == 0)
EnricoG 3:0528e68eac4a 102 return M24SR_STATUS_SUCCESS;
EnricoG 3:0528e68eac4a 103 else
EnricoG 3:0528e68eac4a 104 {
EnricoG 3:0528e68eac4a 105 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 3:0528e68eac4a 106 }
EnricoG 3:0528e68eac4a 107 }
EnricoG 3:0528e68eac4a 108
EnricoG 3:0528e68eac4a 109 /**
EnricoG 3:0528e68eac4a 110 * @brief This functions reads a response of the M24SR device
EnricoG 3:0528e68eac4a 111 * @param NbByte : Number of byte to read (shall be >= 5)
EnricoG 3:0528e68eac4a 112 * @param pBuffer : Pointer to the buffer to send to the M24SR
EnricoG 3:0528e68eac4a 113 * @retval M24SR_STATUS_SUCCESS : The function is succesful
EnricoG 3:0528e68eac4a 114 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 3:0528e68eac4a 115 */
EnricoG 3:0528e68eac4a 116 int8_t M24SR_ReceiveI2Cresponse(uint8_t NbByte, uint8_t *pBuffer)
EnricoG 3:0528e68eac4a 117 {
EnricoG 3:0528e68eac4a 118 if (i2c.read(M24SR_ADDR, (char*)pBuffer, NbByte, false) == 0)
EnricoG 3:0528e68eac4a 119 return M24SR_STATUS_SUCCESS;
EnricoG 3:0528e68eac4a 120 else
EnricoG 3:0528e68eac4a 121 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 3:0528e68eac4a 122 }
EnricoG 3:0528e68eac4a 123
EnricoG 3:0528e68eac4a 124 /**
EnricoG 3:0528e68eac4a 125 * @brief This functions sends the command buffer
EnricoG 3:0528e68eac4a 126 * @param NbByte : Number of byte to send
EnricoG 3:0528e68eac4a 127 * @param pBuffer : pointer to the buffer to send to the M24SR
EnricoG 3:0528e68eac4a 128 * @retval M24SR_STATUS_SUCCESS : the function is succesful
EnricoG 3:0528e68eac4a 129 * @retval M24SR_ERROR_I2CTIMEOUT : The I2C timeout occured.
EnricoG 3:0528e68eac4a 130 */
EnricoG 3:0528e68eac4a 131 int8_t M24SR_SendI2Ccommand(uint8_t NbByte ,uint8_t *pBuffer)
EnricoG 3:0528e68eac4a 132 {
EnricoG 3:0528e68eac4a 133 if (i2c.write(M24SR_ADDR, (char*)pBuffer, NbByte, false) == 0)
EnricoG 3:0528e68eac4a 134 {
EnricoG 3:0528e68eac4a 135 return M24SR_STATUS_SUCCESS;
EnricoG 3:0528e68eac4a 136 }
EnricoG 3:0528e68eac4a 137 else
EnricoG 3:0528e68eac4a 138 {
EnricoG 3:0528e68eac4a 139 return M24SR_ERROR_I2CTIMEOUT;
EnricoG 3:0528e68eac4a 140 }
EnricoG 3:0528e68eac4a 141 }
EnricoG 3:0528e68eac4a 142
EnricoG 3:0528e68eac4a 143 /**
EnricoG 3:0528e68eac4a 144 * @brief This functions returns M24SR_STATUS_SUCCESS when a response is ready
EnricoG 3:0528e68eac4a 145 * @retval M24SR_STATUS_SUCCESS : a response of the M24LR is ready
EnricoG 3:0528e68eac4a 146 * @retval M24SR_ERROR_DEFAULT : the response of the M24LR is not ready
EnricoG 3:0528e68eac4a 147 */
EnricoG 3:0528e68eac4a 148 int8_t M24SR_IsAnswerReady(void)
EnricoG 3:0528e68eac4a 149 {
EnricoG 3:0528e68eac4a 150 uint16_t status ;
EnricoG 3:0528e68eac4a 151 uint32_t retry = 0xFFFFF;
EnricoG 3:0528e68eac4a 152 uint8_t stable = 0;
EnricoG 3:0528e68eac4a 153
EnricoG 3:0528e68eac4a 154 switch (uSynchroMode)
EnricoG 3:0528e68eac4a 155 {
EnricoG 3:0528e68eac4a 156 case M24SR_WAITINGTIME_POLLING :
EnricoG 3:0528e68eac4a 157 errchk(M24SR_PollI2C ( ));
EnricoG 3:0528e68eac4a 158 return M24SR_STATUS_SUCCESS;
EnricoG 3:0528e68eac4a 159
EnricoG 3:0528e68eac4a 160 case M24SR_WAITINGTIME_TIMEOUT :
EnricoG 3:0528e68eac4a 161 // M24SR FWI=5 => (256*16/fc)*2^5=9.6ms but M24SR ask for extended time to program up to 246Bytes.
EnricoG 3:0528e68eac4a 162 // can be improved by
EnricoG 3:0528e68eac4a 163 wait_ms(80);
EnricoG 3:0528e68eac4a 164 return M24SR_STATUS_SUCCESS;
EnricoG 3:0528e68eac4a 165
EnricoG 3:0528e68eac4a 166 case M24SR_WAITINGTIME_GPO :
EnricoG 3:0528e68eac4a 167 do
EnricoG 3:0528e68eac4a 168 {
EnricoG 3:0528e68eac4a 169 if(pGPO.read() == 0)
EnricoG 3:0528e68eac4a 170 {
EnricoG 3:0528e68eac4a 171 stable ++;
EnricoG 3:0528e68eac4a 172 }
EnricoG 3:0528e68eac4a 173 retry --;
EnricoG 3:0528e68eac4a 174 }
EnricoG 3:0528e68eac4a 175 while(stable <5 && retry>0);
EnricoG 3:0528e68eac4a 176 if(!retry)
EnricoG 3:0528e68eac4a 177 goto Error;
EnricoG 3:0528e68eac4a 178 return M24SR_STATUS_SUCCESS;
EnricoG 3:0528e68eac4a 179
EnricoG 3:0528e68eac4a 180 default :
EnricoG 3:0528e68eac4a 181 return M24SR_ERROR_DEFAULT;
EnricoG 3:0528e68eac4a 182 }
EnricoG 3:0528e68eac4a 183
EnricoG 3:0528e68eac4a 184 Error :
EnricoG 3:0528e68eac4a 185 return M24SR_ERROR_DEFAULT;
EnricoG 3:0528e68eac4a 186 }
EnricoG 3:0528e68eac4a 187
EnricoG 3:0528e68eac4a 188 /**
EnricoG 3:0528e68eac4a 189 * @brief This function enable or disable RF communication
EnricoG 3:0528e68eac4a 190 * @param OnOffChoice: GPO configuration to set
EnricoG 3:0528e68eac4a 191 */
EnricoG 3:0528e68eac4a 192 void M24SR_RFConfig_Hard(uint8_t OnOffChoice)
EnricoG 3:0528e68eac4a 193 {
EnricoG 3:0528e68eac4a 194 /* Disable RF */
EnricoG 3:0528e68eac4a 195 if ( OnOffChoice != 0 )
EnricoG 3:0528e68eac4a 196 {
EnricoG 3:0528e68eac4a 197 pRFD = 0;
EnricoG 3:0528e68eac4a 198 }
EnricoG 3:0528e68eac4a 199 else
EnricoG 3:0528e68eac4a 200 {
EnricoG 3:0528e68eac4a 201 pRFD = 1;
EnricoG 3:0528e68eac4a 202 }
EnricoG 3:0528e68eac4a 203 }
EnricoG 3:0528e68eac4a 204