找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3705|回复: 1
打印 上一主题 下一主题
收起左侧

NXP LPC11xx I2C 驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:73735 发表于 2015-2-18 23:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 huge 于 2015-2-18 23:11 编辑
  1. /****************************************************************************
  2. *   $Id:: i2c.c 3662 2010-06-03 19:47:02Z usb00423                         $
  3. *   Project: NXP LPC11xx I2C example
  4. *
  5. *   Description:
  6. *     This file contains I2C code example which include I2C initialization,
  7. *     I2C interrupt handler, and APIs for I2C access.
  8. *
  9. ****************************************************************************
  10. * Software that is described herein is for illustrative purposes only
  11. * which provides customers with programming information regarding the
  12. * products. This software is supplied "AS IS" without any warranties.
  13. * NXP Semiconductors assumes no responsibility or liability for the
  14. * use of the software, conveys no license or title under any patent,
  15. * copyright, or mask work right to the product. NXP Semiconductors
  16. * reserves the right to make changes in the software without
  17. * notification. NXP Semiconductors also make no representation or
  18. * warranty that such application will be suitable for the specified
  19. * use without further testing or modification.
  20. ****************************************************************************/
  21. #include "LPC11xx.h"        /* LPC11xx Peripheral Registers */
  22. #include "type.h"
  23. #include "i2c.h"

  24. volatile uint32_t I2CMasterState = I2C_IDLE;
  25. volatile uint32_t I2CSlaveState = I2C_IDLE;
  26. volatile uint32_t timeout = 0;

  27. volatile uint32_t I2CMode;

  28. volatile uint8_t I2CMasterBuffer[BUFSIZE];
  29. volatile uint8_t I2CSlaveBuffer[BUFSIZE];
  30. volatile uint32_t I2CCount = 0;
  31. volatile uint32_t I2CReadLength;
  32. volatile uint32_t I2CWriteLength;

  33. volatile uint32_t RdIndex = 0;
  34. volatile uint32_t WrIndex = 0;

  35. /*
  36. From device to device, the I2C communication protocol may vary,
  37. in the example below, the protocol uses repeated start to read data from or
  38. write to the device:
  39. For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO
  40. for master write: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO
  41. Thus, in state 8, the address is always WRITE. in state 10, the address could
  42. be READ or WRITE depending on the I2C command.
  43. */   

  44. /*****************************************************************************
  45. ** Function name:        I2C_IRQHandler
  46. **
  47. ** Descriptions:        I2C interrupt handler, deal with master mode only.
  48. **
  49. ** parameters:        None
  50. ** Returned value:        None
  51. **
  52. *****************************************************************************/
  53. void I2C_IRQHandler(void)
  54. {
  55.   uint8_t StatValue;

  56.   timeout = 0;
  57.   /* this handler deals with master read and master write only */
  58.   StatValue = LPC_I2C->STAT;
  59.   switch ( StatValue )
  60.   {
  61. case 0x08:        /* A Start condition is issued. */
  62. WrIndex = 0;
  63. LPC_I2C->DAT = I2CMasterBuffer[WrIndex++];
  64. LPC_I2C->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
  65. break;
  66. case 0x10:        /* A repeated started is issued */
  67. RdIndex = 0;
  68. /* Send SLA with R bit set, */
  69. LPC_I2C->DAT = I2CMasterBuffer[WrIndex++];
  70. LPC_I2C->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
  71. break;
  72. case 0x18:        /* Regardless, it's a ACK */
  73. if ( I2CWriteLength == 1 )
  74. {
  75. LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
  76. I2CMasterState = I2C_NO_DATA;
  77. }
  78. else
  79. {
  80. LPC_I2C->DAT = I2CMasterBuffer[WrIndex++];
  81. }
  82. LPC_I2C->CONCLR = I2CONCLR_SIC;
  83. break;
  84. case 0x28:        /* Data byte has been transmitted, regardless ACK or NACK */
  85. if ( WrIndex < I2CWriteLength )
  86. {   
  87. LPC_I2C->DAT = I2CMasterBuffer[WrIndex++]; /* this should be the last one */
  88. }
  89. else
  90. {
  91. if ( I2CReadLength != 0 )
  92. {
  93. LPC_I2C->CONSET = I2CONSET_STA;        /* Set Repeated-start flag */
  94. }
  95. else
  96. {
  97. LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
  98. I2CMasterState = I2C_OK;
  99. }
  100. }
  101. LPC_I2C->CONCLR = I2CONCLR_SIC;
  102. break;

  103. case 0x30:
  104. LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
  105. I2CMasterState = I2C_NACK_ON_DATA;
  106. LPC_I2C->CONCLR = I2CONCLR_SIC;
  107. break;
  108. case 0x40:        /* Master Receive, SLA_R has been sent */
  109. if ( (RdIndex + 1) < I2CReadLength )
  110. {
  111. /* Will go to State 0x50 */
  112. LPC_I2C->CONSET = I2CONSET_AA;        /* assert ACK after data is received */
  113. }
  114. else
  115. {
  116. /* Will go to State 0x58 */
  117. LPC_I2C->CONCLR = I2CONCLR_AAC;        /* assert NACK after data is received */
  118. }
  119. LPC_I2C->CONCLR = I2CONCLR_SIC;
  120. break;
  121. case 0x50:        /* Data byte has been received, regardless following ACK or NACK */
  122. I2CSlaveBuffer[RdIndex++] = LPC_I2C->DAT;
  123. if ( (RdIndex + 1) < I2CReadLength )
  124. {   
  125. LPC_I2C->CONSET = I2CONSET_AA;        /* assert ACK after data is received */
  126. }
  127. else
  128. {
  129. LPC_I2C->CONCLR = I2CONCLR_AAC;        /* assert NACK on last byte */
  130. }
  131. LPC_I2C->CONCLR = I2CONCLR_SIC;
  132. break;
  133. case 0x58:
  134. I2CSlaveBuffer[RdIndex++] = LPC_I2C->DAT;
  135. I2CMasterState = I2C_OK;
  136. LPC_I2C->CONSET = I2CONSET_STO;        /* Set Stop flag */
  137. LPC_I2C->CONCLR = I2CONCLR_SIC;        /* Clear SI flag */
  138. break;

  139. case 0x20:        /* regardless, it's a NACK */
  140. case 0x48:
  141. LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
  142. I2CMasterState = I2C_NACK_ON_ADDRESS;
  143. LPC_I2C->CONCLR = I2CONCLR_SIC;
  144. break;
  145. case 0x38:        /* Arbitration lost, in this example, we don't
  146. deal with multiple master situation */
  147. default:
  148. I2CMasterState = I2C_ARBITRATION_LOST;
  149. LPC_I2C->CONCLR = I2CONCLR_SIC;       
  150. break;
  151.   }
  152.   return;
  153. }

  154. /*****************************************************************************
  155. ** Function name:        I2CStart
  156. **
  157. ** Descriptions:        Create I2C start condition, a timeout
  158. **        value is set if the I2C never gets started,
  159. **        and timed out. It's a fatal error.
  160. **
  161. ** parameters:        None
  162. ** Returned value:        true or false, return false if timed out
  163. **
  164. *****************************************************************************/
  165. uint32_t I2CStart( void )
  166. {
  167.   uint32_t timeout = 0;
  168.   uint32_t retVal = FALSE;

  169.   /*--- Issue a start condition ---*/
  170.   LPC_I2C->CONSET = I2CONSET_STA;        /* Set Start flag */
  171.    
  172.   /*--- Wait until START transmitted ---*/
  173.   while( 1 )
  174.   {
  175. if ( I2CMasterState == I2C_STARTED )
  176. {
  177. retVal = TRUE;
  178. break;       
  179. }
  180. if ( timeout >= MAX_TIMEOUT )
  181. {
  182. retVal = FALSE;
  183. break;
  184. }
  185. timeout++;
  186.   }
  187.   return( retVal );
  188. }

  189. /*****************************************************************************
  190. ** Function name:        I2CStop
  191. **
  192. ** Descriptions:        Set the I2C stop condition, if the routine
  193. **        never exit, it's a fatal bus error.
  194. **
  195. ** parameters:        None
  196. ** Returned value:        true or never return
  197. **
  198. *****************************************************************************/
  199. uint32_t I2CStop( void )
  200. {
  201.   LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
  202.   LPC_I2C->CONCLR = I2CONCLR_SIC;  /* Clear SI flag */
  203.             
  204.   /*--- Wait for STOP detected ---*/
  205.   while( LPC_I2C->CONSET & I2CONSET_STO );
  206.   return TRUE;
  207. }

  208. /*****************************************************************************
  209. ** Function name:        I2CInit
  210. **
  211. ** Descriptions:        Initialize I2C controller
  212. **
  213. ** parameters:        I2c mode is either MASTER or SLAVE
  214. ** Returned value:        true or false, return false if the I2C
  215. **        interrupt handler was not installed correctly
  216. **
  217. *****************************************************************************/
  218. uint32_t I2CInit( uint32_t I2cMode )
  219. {
  220.   LPC_SYSCON->PRESETCTRL |= (0x1<<1);

  221.   LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);
  222.   LPC_IOCON->PIO0_4 &= ~0x3F;        /*  I2C I/O config */
  223.   LPC_IOCON->PIO0_4 |= 0x01;        /* I2C SCL */
  224.   LPC_IOCON->PIO0_5 &= ~0x3F;       
  225.   LPC_IOCON->PIO0_5 |= 0x01;        /* I2C SDA */
  226.   /* IOCON may change in the next release, save change for future references. */
  227. //  LPC_IOCON->PIO0_4 |= (0x1<<10);        /* open drain pins */
  228. //  LPC_IOCON->PIO0_5 |= (0x1<<10);        /* open drain pins */

  229.   /*--- Clear flags ---*/
  230.   LPC_I2C->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;   

  231.   /*--- Reset registers ---*/
  232. #if FAST_MODE_PLUS
  233.   LPC_IOCON->PIO0_4 |= (0x2<<8);
  234.   LPC_IOCON->PIO0_5 |= (0x2<<8);
  235.   LPC_I2C->SCLL   = I2SCLL_HS_SCLL;
  236.   LPC_I2C->SCLH   = I2SCLH_HS_SCLH;
  237. #else
  238.   LPC_I2C->SCLL   = I2SCLL_SCLL;
  239.   LPC_I2C->SCLH   = I2SCLH_SCLH;
  240. #endif

  241.   if ( I2cMode == I2CSLAVE )
  242.   {
  243. LPC_I2C->ADR0 = PCF8594_ADDR;
  244.   }   

  245.   /* Enable the I2C Interrupt */
  246.   NVIC_EnableIRQ(I2C_IRQn);

  247.   LPC_I2C->CONSET = I2CONSET_I2EN;
  248.   return( TRUE );
  249. }

  250. /*****************************************************************************
  251. ** Function name:        I2CEngine
  252. **
  253. ** Descriptions:        The routine to complete a I2C transaction
  254. **        from start to stop. All the intermitten
  255. **        steps are handled in the interrupt handler.
  256. **        Before this routine is called, the read
  257. **        length, write length, I2C master buffer,
  258. **        and I2C command fields need to be filled.
  259. **        see i2cmst.c for more details.
  260. **
  261. ** parameters:        None
  262. ** Returned value:        true or false, return false only if the
  263. **        start condition can never be generated and
  264. **        timed out.
  265. **
  266. *****************************************************************************/
  267. uint32_t I2CEngine( void )
  268. {
  269.   RdIndex = 0;
  270.   WrIndex = 0;

  271.   /*--- Issue a start condition ---*/
  272.   LPC_I2C->CONSET = I2CONSET_STA;        /* Set Start flag */

  273.   I2CMasterState = I2C_BUSY;       

  274.   while ( I2CMasterState == I2C_BUSY )
  275.   {
  276. if ( timeout >= MAX_TIMEOUT )
  277. {
  278. I2CMasterState = I2C_TIME_OUT;
  279. break;
  280. }
  281. timeout++;
  282.   }
  283.   LPC_I2C->CONCLR = I2CONCLR_STAC;

  284.   return ( I2CMasterState );
  285. }

  286. /******************************************************************************
  287. **                            End Of File
  288. ******************************************************************************/


  289. /****************************************************************************
  290. *   $Id:: i2c.h 3662 2010-06-03 19:47:02Z usb00423                         $
  291. *   Project: NXP LPC11xx I2C example
  292. *
  293. *   Description:
  294. *     This file contains I2C code header definition.
  295. *
  296. ****************************************************************************
  297. * Software that is described herein is for illustrative purposes only
  298. * which provides customers with programming information regarding the
  299. * products. This software is supplied "AS IS" without any warranties.
  300. * NXP Semiconductors assumes no responsibility or liability for the
  301. * use of the software, conveys no license or title under any patent,
  302. * copyright, or mask work right to the product. NXP Semiconductors
  303. * reserves the right to make changes in the software without
  304. * notification. NXP Semiconductors also make no representation or
  305. * warranty that such application will be suitable for the specified
  306. * use without further testing or modification.
  307. ****************************************************************************/
  308. #ifndef __I2C_H
  309. #define __I2C_H

  310. /* If I2C SEEPROM is tested, make sure FAST_MODE_PLUS is 0.
  311. For board to board test, this flag can be turned on. */

  312. #define FAST_MODE_PLUS      0

  313. #define BUFSIZE             64
  314. #define MAX_TIMEOUT         0x00FFFFFF

  315. #define I2CMASTER           0x01
  316. #define I2CSLAVE            0x02

  317. #define PCF8594_ADDR        0xA0
  318. #define READ_WRITE          0x01

  319. #define RD_BIT              0x01

  320. #define I2C_IDLE              0
  321. #define I2C_STARTED           1
  322. #define I2C_RESTARTED         2
  323. #define I2C_REPEATED_START    3
  324. #define DATA_ACK              4
  325. #define DATA_NACK             5
  326. #define I2C_BUSY              6
  327. #define I2C_NO_DATA           7
  328. #define I2C_NACK_ON_ADDRESS   8
  329. #define I2C_NACK_ON_DATA      9
  330. #define I2C_ARBITRATION_LOST  10
  331. #define I2C_TIME_OUT          11
  332. #define I2C_OK                12

  333. #define I2CONSET_I2EN       (0x1<<6)  /* I2C Control Set Register */
  334. #define I2CONSET_AA         (0x1<<2)
  335. #define I2CONSET_SI         (0x1<<3)
  336. #define I2CONSET_STO        (0x1<<4)
  337. #define I2CONSET_STA        (0x1<<5)

  338. #define I2CONCLR_AAC        (0x1<<2)  /* I2C Control clear Register */
  339. #define I2CONCLR_SIC        (0x1<<3)
  340. #define I2CONCLR_STAC       (0x1<<5)
  341. #define I2CONCLR_I2ENC      (0x1<<6)

  342. #define I2DAT_I2C           0x00000000  /* I2C Data Reg */
  343. #define I2ADR_I2C           0x00000000  /* I2C Slave Address Reg */
  344. #define I2SCLH_SCLH         0x00000180  /* I2C SCL Duty Cycle High Reg */
  345. #define I2SCLL_SCLL         0x00000180  /* I2C SCL Duty Cycle Low Reg */
  346. #define I2SCLH_HS_SCLH        0x00000015  /* Fast Plus I2C SCL Duty Cycle High Reg */
  347. #define I2SCLL_HS_SCLL        0x00000015  /* Fast Plus I2C SCL Duty Cycle Low Reg */

  348. extern void I2C_IRQHandler( void );
  349. extern uint32_t I2CInit( uint32_t I2cMode );
  350. extern uint32_t I2CStart( void );
  351. extern uint32_t I2CStop( void );
  352. extern uint32_t I2CEngine( void );

  353. #endif /* end __I2C_H */
  354. /****************************************************************************
  355. **                            End Of File
  356. *****************************************************************************/





  357. /****************************************************************************
  358. *   $Id:: i2ctest.c 3662 2010-06-03 19:47:02Z usb00423                     $
  359. *   Project: NXP LPC11xx I2C example
  360. *
  361. *   Description:
  362. *     This file contains I2C test modules, main entry, to test I2C APIs.
  363. *
  364. ****************************************************************************
  365. * Software that is described herein is for illustrative purposes only
  366. * which provides customers with programming information regarding the
  367. * products. This software is supplied "AS IS" without any warranties.
  368. * NXP Semiconductors assumes no responsibility or liability for the
  369. * use of the software, conveys no license or title under any patent,
  370. * copyright, or mask work right to the product. NXP Semiconductors
  371. * reserves the right to make changes in the software without
  372. * notification. NXP Semiconductors also make no representation or
  373. * warranty that such application will be suitable for the specified
  374. * use without further testing or modification.
  375. ****************************************************************************/
  376. #include "LPC11xx.h"        /* LPC11xx Peripheral Registers */
  377. #include "type.h"
  378. #include "i2c.h"

  379. extern volatile uint32_t I2CCount;
  380. extern volatile uint8_t I2CMasterBuffer[BUFSIZE];
  381. extern volatile uint8_t I2CSlaveBuffer[BUFSIZE];
  382. extern volatile uint32_t I2CMasterState;
  383. extern volatile uint32_t I2CReadLength, I2CWriteLength;

  384. /*******************************************************************************
  385. **   Main Function  main()
  386. *******************************************************************************/
  387. int main (void)
  388. {
  389.   uint32_t i;

  390.   SystemInit();

  391.   if ( I2CInit( (uint32_t)I2CMASTER ) == FALSE )        /* initialize I2c */
  392.   {
  393. while ( 1 );        /* Fatal error */
  394.   }

  395.   /* In order to start the I2CEngine, the all the parameters
  396.   must be set in advance, including I2CWriteLength, I2CReadLength,
  397.   I2CCmd, and the I2cMasterBuffer which contains the stream
  398.   command/data to the I2c slave device.
  399.   (1) If it's a I2C write only, the number of bytes to be written is
  400.   I2CWriteLength, I2CReadLength is zero, the content will be filled
  401.   in the I2CMasterBuffer.
  402.   (2) If it's a I2C read only, the number of bytes to be read is
  403.   I2CReadLength, I2CWriteLength is 0, the read value will be filled
  404.   in the I2CMasterBuffer.
  405.   (3) If it's a I2C Write/Read with repeated start, specify the
  406.   I2CWriteLength, fill the content of bytes to be written in
  407.   I2CMasterBuffer, specify the I2CReadLength, after the repeated
  408.   start and the device address with RD bit set, the content of the
  409.   reading will be filled in I2CMasterBuffer index at
  410.   I2CMasterBuffer[I2CWriteLength+2].
  411.   
  412.   e.g. Start, DevAddr(W), WRByte1...WRByteN, Repeated-Start, DevAddr(R),
  413.   RDByte1...RDByteN Stop. The content of the reading will be filled
  414.   after (I2CWriteLength + two devaddr) bytes. */

  415.   /* Write SLA(W), address and one data byte */
  416.   I2CWriteLength = 6;
  417.   I2CReadLength = 0;
  418.   I2CMasterBuffer[0] = PCF8594_ADDR;
  419.   I2CMasterBuffer[1] = 0x00;        /* address */
  420.   I2CMasterBuffer[2] = 0x55;        /* Data0 */
  421.   I2CMasterBuffer[3] = 0xAA;        /* Data1 */
  422.   I2CMasterBuffer[4] = 0x12;        /* Data0 */
  423.   I2CMasterBuffer[5] = 0x34;        /* Data1 */
  424.   I2CEngine();

  425.   /* Be careful with below fixed delay. From device to device, or
  426.   even same device with different write length, or various I2C clock,
  427.   below delay length may need to be changed accordingly. Having
  428.   a break point before Write/Read start will be helpful to isolate
  429.   the problem. */
  430.   for ( i = 0; i < 0x200000; i++ );        /* Delay after write */

  431.   for ( i = 0; i < BUFSIZE; i++ )
  432.   {
  433. I2CSlaveBuffer[i] = 0x00;
  434.   }
  435.   /* Write SLA(W), address, SLA(R), and read one byte back. */
  436.   I2CWriteLength = 2;
  437.   I2CReadLength = 4;
  438.   I2CMasterBuffer[0] = PCF8594_ADDR;
  439.   I2CMasterBuffer[1] = 0x00;        /* address */
  440.   I2CMasterBuffer[2] = PCF8594_ADDR | RD_BIT;
  441.   I2CEngine();

  442.   /* Check the content of the Master and slave buffer */
  443.   while ( 1 );
  444.   return 0;
  445. }

  446. /******************************************************************************
  447. **                            End Of File
  448. ******************************************************************************/
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:75874 发表于 2015-4-1 16:57 | 只看该作者
您好,NXP的单片机用的多吗,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表