[资料分享] CC2541 I2C EEPROM例程 AT24C512

littleshrimp   2015-11-2 15:24 楼主
94.cc2541 i2c eepro frame fm24v10.rar (22.24 KB)
(下载次数: 38, 2015-12-8 14:34 上传)
  1. #include "hal_types.h"
  2. #include "hal_defs.h"
  3. #include "ioCC254x_bitdef.h"
  4. #include <ioCC2541.h>
  5. #include "i2c.h"
  6. uint8 buf [20] = {8,2,3,4,5};
  7. uint8 bufr [20] = {0,0,0,0,0};
  8. void eeprom_test(void)
  9. {
  10. uint16 i;
  11. eepromInit(0x50);
  12. for(i=0;i<40;i++)
  13. {
  14. buf[i] = 255-i;
  15. }
  16. eepromWrite(0,buf,40);
  17. for(i=0;i<40;i++)
  18. {
  19. bufr[i] = 0;
  20. }
  21. for(i=0;i<65500;i++);
  22. eepromRead(0,bufr,20);
  23. }
  24. int main( void )
  25. {
  26. uint8 i;
  27. /****************************************************************************
  28. * Clock setup
  29. * See basic software example "clk_xosc_cc254x"
  30. */
  31. // Set system clock source to HS XOSC, with no pre-scaling.
  32. CLKCONCMD = (CLKCONCMD & ~(CLKCON_OSC | CLKCON_CLKSPD)) | CLKCON_CLKSPD_32M;
  33. // Wait until clock source has changed
  34. while (CLKCONSTA & CLKCON_OSC);
  35. eeprom_test();
  36. while(1)
  37. {
  38. }
  39. }
  1. /* ------------------------------------------------------------------------------------------------
  2. * Includes
  3. * ------------------------------------------------------------------------------------------------
  4. */
  5. #include "i2c.h"
  6. /* ------------------------------------------------------------------------------------------------
  7. * Constants
  8. * ------------------------------------------------------------------------------------------------
  9. */
  10. #define I2C_ENS1 BV(6)
  11. #define I2C_STA BV(5)
  12. #define I2C_STO BV(4)
  13. #define I2C_SI BV(3)
  14. #define I2C_AA BV(2)
  15. #define I2C_MST_RD_BIT BV(0) // Master RD/WRn bit to be OR'ed with Slave address.
  16. #define I2C_CLOCK_MASK 0x83
  17. #define I2C_PXIFG P2IFG
  18. #define I2C_IF P2IF
  19. #define I2C_IE BV(1)
  20. /* ------------------------------------------------------------------------------------------------
  21. * Typedefs
  22. * ------------------------------------------------------------------------------------------------
  23. */
  24. typedef enum
  25. {
  26. // HAL_I2C_MASTER mode statuses.
  27. mstStarted = 0x08,
  28. mstRepStart = 0x10,
  29. mstAddrAckW = 0x18,
  30. mstAddrNackW = 0x20,
  31. mstDataAckW = 0x28,
  32. mstDataNackW = 0x30,
  33. mstLostArb = 0x38,
  34. mstAddrAckR = 0x40,
  35. mstAddrNackR = 0x48,
  36. mstDataAckR = 0x50,
  37. mstDataNackR = 0x58,
  38. } i2cStatus_t;
  39. /* ------------------------------------------------------------------------------------------------
  40. * Macros
  41. * ------------------------------------------------------------------------------------------------
  42. */
  43. #define I2C_WRAPPER_DISABLE() st( I2CWC = 0x00; )
  44. #define I2C_CLOCK_RATE(x) st( I2CCFG &= ~I2C_CLOCK_MASK; \
  45. I2CCFG |= x; )
  46. #define I2C_SET_NACK() st( I2CCFG &= ~I2C_AA; )
  47. #define I2C_SET_ACK() st( I2CCFG |= I2C_AA; )
  48. // Enable I2C bus
  49. #define I2C_ENABLE() st( I2CCFG |= (I2C_ENS1); )
  50. #define I2C_DISABLE() st( I2CCFG &= ~(I2C_ENS1); )
  51. // Must clear SI before setting STA and then STA must be manually cleared.
  52. #define I2C_STRT() st ( \
  53. I2CCFG &= ~I2C_SI; \
  54. I2CCFG |= I2C_STA; \
  55. while ((I2CCFG & I2C_SI) == 0); \
  56. I2CCFG &= ~I2C_STA; \
  57. )
  58. // Must set STO before clearing SI.
  59. #define I2C_STOP() st ( \
  60. I2CCFG |= I2C_STO; \
  61. I2CCFG &= ~I2C_SI; \
  62. while ((I2CCFG & I2C_STO) != 0); \
  63. )
  64. // Stop clock-stretching and then read when it arrives.
  65. #define I2C_READ(_X_) st ( \
  66. I2CCFG &= ~I2C_SI; \
  67. while ((I2CCFG & I2C_SI) == 0); \
  68. (_X_) = I2CDATA; \
  69. )
  70. // First write new data and then stop clock-stretching.
  71. #define I2C_WRITE(_X_) st ( \
  72. I2CDATA = (_X_); \
  73. I2CCFG &= ~I2C_SI; \
  74. while ((I2CCFG & I2C_SI) == 0); \
  75. )
  76. /* ------------------------------------------------------------------------------------------------
  77. * Local Variables
  78. * ------------------------------------------------------------------------------------------------
  79. */
  80. static uint8 i2cAddr; // Target Slave address pre-shifted up by one leaving RD/WRn LSB as zero.
  81. /**************************************************************************************************
  82. * @fn i2cMstStrt
  83. *
  84. * @brief Attempt to send an I2C bus START and Slave Address as an I2C bus Master.
  85. *
  86. * input parameters
  87. *
  88. * @param RD_WRn - The LSB of the Slave Address as Read/~Write.
  89. *
  90. * @return The I2C status of the START request or of the Slave Address Ack.
  91. */
  92. static uint8 i2cMstStrt(uint8 RD_WRn)
  93. {
  94. I2C_STRT();
  95. if (I2CSTAT == mstStarted) /* A start condition has been transmitted */
  96. {
  97. I2C_WRITE(i2cAddr | RD_WRn);
  98. }
  99. return I2CSTAT;
  100. }
  101. /**************************************************************************************************
  102. * @fn HalI2CInit
  103. *
  104. * @brief Initialize the I2C bus as a Master.
  105. *
  106. * input parameters
  107. *
  108. * @param address - I2C slave address.
  109. * @param clockRate - I2C clock rate.
  110. *
  111. * output parameters
  112. *
  113. * None.
  114. *
  115. * @return None.
  116. */
  117. void eepromInit(uint8 address)
  118. {
  119. i2cAddr = address << 1;
  120. I2C_WRAPPER_DISABLE();
  121. I2CADDR = 0; // no multi master support at this time
  122. I2C_CLOCK_RATE(i2cClock_123KHZ);
  123. I2C_ENABLE();
  124. }
  125. /**************************************************************************************************
  126. * @fn HalI2CRead
  127. *
  128. * @brief Read from the I2C bus as a Master.
  129. *
  130. * input parameters
  131. *
  132. * @param len - Number of bytes to read.
  133. * @param pBuf - Pointer to the data buffer to put read bytes.
  134. *
  135. * output parameters
  136. *
  137. * None.
  138. *
  139. * @return The number of bytes successfully read.
  140. */
  141. bool eepromRead(uint16 addr, uint8 *pBuf, uint16 len)
  142. {
  143. uint8 cnt = 0;
  144. eepromWrite(0,(uint8 *)(&addr),0);
  145. if (i2cMstStrt(I2C_MST_RD_BIT) != mstAddrAckR)
  146. {
  147. len = 0;
  148. }
  149. // All bytes are ACK'd except for the last one which is NACK'd. If only
  150. // 1 byte is being read, a single NACK will be sent. Thus, we only want
  151. // to enable ACK if more than 1 byte is going to be read.
  152. if (len > 1)
  153. {
  154. I2C_SET_ACK();
  155. }
  156. while (len > 0)
  157. {
  158. // slave devices require NACK to be sent after reading last byte
  159. if (len == 1)
  160. {
  161. I2C_SET_NACK();
  162. }
  163. // read a byte from the I2C interface
  164. I2C_READ(*pBuf++);
  165. cnt++;
  166. len--;
  167. if (I2CSTAT != mstDataAckR)
  168. {
  169. if (I2CSTAT != mstDataNackR)
  170. {
  171. // something went wrong, so don't count last byte
  172. cnt--;
  173. }
  174. break;
  175. }
  176. }
  177. I2C_STOP();
  178. return cnt;
  179. }
  180. /**************************************************************************************************
  181. * @fn HalI2CWrite
  182. *
  183. * @brief Write to the I2C bus as a Master.
  184. *
  185. * input parameters
  186. *
  187. * @param len - Number of bytes to write.
  188. * @param pBuf - Pointer to the data buffer to write.
  189. *
  190. * output parameters
  191. *
  192. * None.
  193. *
  194. * @return The number of bytes successfully written.
  195. */
  196. uint8 eepromWrite(uint16 addr, uint8 *pBuf, uint16 len)
  197. {
  198. if (i2cMstStrt(0) != mstAddrAckW)
  199. {
  200. len = 0;
  201. }
  202. I2C_WRITE(addr>>8);
  203. I2C_WRITE(addr);
  204. // for (uint8 cnt = 0; cnt < 2; cnt++)
  205. // {
  206. // I2C_WRITE(0);
  207. //
  208. // if (I2CSTAT != mstDataAckW)
  209. // {
  210. // if (I2CSTAT == mstDataNackW)
  211. // {
  212. // len = cnt + 1;
  213. // }
  214. // else
  215. // {
  216. // len = cnt;
  217. // }
  218. // break;
  219. // }
  220. // }
  221. for (uint8 cnt = 0; cnt < len; cnt++)
  222. {
  223. I2C_WRITE(*pBuf++);
  224. if (I2CSTAT != mstDataAckW)
  225. {
  226. if (I2CSTAT == mstDataNackW)
  227. {
  228. len = cnt + 1;
  229. }
  230. else
  231. {
  232. len = cnt;
  233. }
  234. break;
  235. }
  236. }
  237. I2C_STOP();
  238. return len;
  239. }
本帖最后由 littleshrimp 于 2015-12-8 14:34 编辑
虾扯蛋,蛋扯虾,虾扯蛋扯虾

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复