找回密码
 立即注册

QQ登录

只需一步,快速开始

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

NXP LPC11xx I2C Slave 从机程序

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

  25. volatile uint32_t I2CMasterState = I2C_IDLE;
  26. volatile uint32_t I2CSlaveState = I2C_IDLE;

  27. volatile uint32_t I2CMode;

  28. volatile uint8_t I2CWrBuffer[BUFSIZE];
  29. volatile uint8_t I2CRdBuffer[BUFSIZE];
  30. volatile uint32_t I2CReadLength;
  31. volatile uint32_t I2CWriteLength;

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

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

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

  55.   /* this handler deals with master read and master write only */
  56.   StatValue = LPC_I2C->STAT;
  57.   switch ( StatValue )
  58.   {
  59. case 0x60:/* An own SLA_W has been received. */
  60. case 0x68:
  61. RdIndex = 0;
  62. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_W is received */
  63. LPC_I2C->CONCLR = I2CONCLR_SIC;
  64. I2CSlaveState = I2C_WR_STARTED;
  65. break;
  66. case 0x80:/*  data receive */
  67. case 0x90:
  68. if ( I2CSlaveState == I2C_WR_STARTED )
  69. {
  70. I2CRdBuffer[RdIndex++] = LPC_I2C->DAT;
  71. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after data is received */
  72. }
  73. else
  74. {
  75. LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
  76. }
  77. LPC_I2C->CONCLR = I2CONCLR_SIC;
  78. break;
  79. case 0xA8:/* An own SLA_R has been received. */
  80. case 0xB0:
  81. RdIndex = 0;
  82. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_R is received */
  83. LPC_I2C->CONCLR = I2CONCLR_SIC;
  84. I2CSlaveState = I2C_RD_STARTED;
  85. WrIndex = I2CRdBuffer[0];/* The 1st byte is the index. */
  86. break;
  87. case 0xB8:/* Data byte has been transmitted */
  88. case 0xC8:
  89. if ( I2CSlaveState == I2C_RD_STARTED )
  90. {
  91. LPC_I2C->DAT = I2CRdBuffer[WrIndex+1];/* write the same data back to master */
  92. WrIndex++;/* Need to skip the index byte in RdBuffer */
  93. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK  */
  94. }
  95. else
  96. {
  97. LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK  */
  98. }
  99. LPC_I2C->CONCLR = I2CONCLR_SIC;
  100. break;

  101. case 0xC0:/* Data byte has been transmitted, NACK */
  102. LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK  */
  103. LPC_I2C->CONCLR = I2CONCLR_SIC;
  104. I2CSlaveState = DATA_NACK;
  105. break;

  106. case 0xA0:/* Stop condition or repeated start has */
  107. LPC_I2C->CONSET = I2CONSET_AA;/* been received, assert ACK.  */
  108. LPC_I2C->CONCLR = I2CONCLR_SIC;
  109. I2CSlaveState = I2C_IDLE;
  110. break;

  111. default:
  112. LPC_I2C->CONCLR = I2CONCLR_SIC;
  113. LPC_I2C->CONSET = I2CONSET_I2EN | I2CONSET_SI;
  114. break;
  115.   }
  116.   return;
  117. }

  118. /*****************************************************************************
  119. ** Function name:I2CSlaveInit
  120. **
  121. ** Descriptions:Initialize I2C controller
  122. **
  123. ** parameters:I2c mode is either MASTER or SLAVE
  124. ** Returned value:true or false, return false if the I2C
  125. **interrupt handler was not installed correctly
  126. **
  127. *****************************************************************************/
  128. void I2CSlaveInit( void )
  129. {
  130.   /* SSP and I2C reset are overlapped, a known bug,
  131.   for now, both SSP and I2C use bit 0 for reset enable.
  132.   Once the problem is fixed, change to "#if 1". */
  133. #if 1
  134.   LPC_SYSCON->PRESETCTRL |= (0x1<<1);
  135. #else
  136.   LPC_SYSCON->PRESETCTRL |= (0x1<<0);
  137. #endif
  138.   LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);
  139.   LPC_IOCON->PIO0_4 &= ~0x3F;/*  I2C I/O config */
  140.   LPC_IOCON->PIO0_4 |= 0x01;/* I2C SCL */
  141.   LPC_IOCON->PIO0_5 &= ~0x3F;
  142.   LPC_IOCON->PIO0_5 |= 0x01;/* I2C SDA */

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

  145.   /*--- Reset registers ---*/
  146. #if FAST_MODE_PLUS
  147.   LPC_IOCON->PIO0_4 |= (0x1<<9);
  148.   LPC_IOCON->PIO0_5 |= (0x1<<9);
  149.   LPC_I2C->SCLL   = I2SCLL_HS_SCLL;
  150.   LPC_I2C->SCLH   = I2SCLH_HS_SCLH;
  151. #else
  152.   LPC_I2C->SCLL   = I2SCLL_SCLL;
  153.   LPC_I2C->SCLH   = I2SCLH_SCLH;
  154. #endif

  155.   LPC_I2C->ADR0 = PCF8594_ADDR;   
  156.   I2CSlaveState = I2C_IDLE;
  157.   
  158.   /* Enable the I2C Interrupt */
  159.   NVIC_EnableIRQ(I2C_IRQn);

  160.   LPC_I2C->CONSET = I2CONSET_I2EN | I2CONSET_SI;
  161.   return;
  162. }

  163. /******************************************************************************
  164. **                            End Of File
  165. ******************************************************************************/





  166. /****************************************************************************
  167. *   $Id:: i2cslave.h 3635 2010-06-02 00:31:46Z usb00423                    $
  168. *   Project: NXP LPC11xx I2C Slave example
  169. *
  170. *   Description:
  171. *     This file contains I2C slave code header definition.
  172. *
  173. ****************************************************************************
  174. * Software that is described herein is for illustrative purposes only
  175. * which provides customers with programming information regarding the
  176. * products. This software is supplied "AS IS" without any warranties.
  177. * NXP Semiconductors assumes no responsibility or liability for the
  178. * use of the software, conveys no license or title under any patent,
  179. * copyright, or mask work right to the product. NXP Semiconductors
  180. * reserves the right to make changes in the software without
  181. * notification. NXP Semiconductors also make no representation or
  182. * warranty that such application will be suitable for the specified
  183. * use without further testing or modification.
  184. ****************************************************************************/
  185. #ifndef __I2CSLAVE_H
  186. #define __I2CSLAVE_H

  187. #define FAST_MODE_PLUS      1

  188. #define BUFSIZE             6
  189. #define MAX_TIMEOUT         0x00FFFFFF

  190. #define PCF8594_ADDR        0xA0
  191. #define READ_WRITE          0x01

  192. #define RD_BIT              0x01

  193. #define I2C_IDLE            0
  194. #define I2C_STARTED         1
  195. #define I2C_RESTARTED       2
  196. #define I2C_REPEATED_START  3
  197. #define DATA_ACK            4
  198. #define DATA_NACK           5
  199. #define I2C_WR_STARTED      6
  200. #define I2C_RD_STARTED      7

  201. #define I2CONSET_I2EN       (0x1<<6)  /* I2C Control Set Register */
  202. #define I2CONSET_AA         (0x1<<2)
  203. #define I2CONSET_SI         (0x1<<3)
  204. #define I2CONSET_STO        (0x1<<4)
  205. #define I2CONSET_STA        (0x1<<5)

  206. #define I2CONCLR_AAC        (0x1<<2)  /* I2C Control clear Register */
  207. #define I2CONCLR_SIC        (0x1<<3)
  208. #define I2CONCLR_STAC       (0x1<<5)
  209. #define I2CONCLR_I2ENC      (0x1<<6)

  210. #define I2DAT_I2C           0x00000000  /* I2C Data Reg */
  211. #define I2ADR_I2C           0x00000000  /* I2C Slave Address Reg */
  212. #define I2SCLH_SCLH         0x00000180  /* I2C SCL Duty Cycle High Reg */
  213. #define I2SCLL_SCLL         0x00000180  /* I2C SCL Duty Cycle Low Reg */
  214. #define I2SCLH_HS_SCLH      0x00000020  /* Fast Plus I2C SCL Duty Cycle High Reg */
  215. #define I2SCLL_HS_SCLL      0x00000020  /* Fast Plus I2C SCL Duty Cycle Low Reg */


  216. extern void I2C_IRQHandler( void );
  217. extern void I2CSlaveInit( void );

  218. #endif /* end __I2CSLAVE_H */
  219. /****************************************************************************
  220. **                            End Of File
  221. *****************************************************************************/



  222. /****************************************************************************
  223. *   $Id:: i2cslvtst.c 3635 2010-06-02 00:31:46Z usb00423                   $
  224. *   Project: NXP LPC11xx I2C example
  225. *
  226. *   Description:
  227. *     This file contains I2C slave test modules, main entry, to test I2C
  228. *     slave APIs.
  229. *
  230. ****************************************************************************
  231. * Software that is described herein is for illustrative purposes only
  232. * which provides customers with programming information regarding the
  233. * products. This software is supplied "AS IS" without any warranties.
  234. * NXP Semiconductors assumes no responsibility or liability for the
  235. * use of the software, conveys no license or title under any patent,
  236. * copyright, or mask work right to the product. NXP Semiconductors
  237. * reserves the right to make changes in the software without
  238. * notification. NXP Semiconductors also make no representation or
  239. * warranty that such application will be suitable for the specified
  240. * use without further testing or modification.
  241. ****************************************************************************/
  242. #include "LPC11xx.h"/* LPC11xx Peripheral Registers */
  243. #include "type.h"
  244. #include "i2cslave.h"

  245. extern volatile uint8_t I2CWrBuffer[BUFSIZE];
  246. extern volatile uint8_t I2CRdBuffer[BUFSIZE];
  247. extern volatile uint32_t I2CSlaveState;
  248. extern volatile uint32_t I2CReadLength, I2CWriteLength;

  249. /*******************************************************************************
  250. **   Main Function  main()
  251. *******************************************************************************/
  252. int main (void)
  253. {
  254.   uint32_t i;

  255.   SystemInit();

  256.   for ( i = 0; i < BUFSIZE; i++ )
  257.   {
  258. I2CRdBuffer[i] = 0x00;
  259.   }
  260.   
  261.   I2CSlaveInit();/* initialize I2c */

  262.   /* When the NACK occurs, the master has stopped the
  263.   communication. Just check the content of I2CRd/WrBuffer. */
  264.   while ( I2CSlaveState != DATA_NACK );
  265.   return 0;
  266. }

  267. /******************************************************************************
  268. **                            End Of File
  269. ******************************************************************************/
复制代码


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

使用道具 举报

沙发
ID:677816 发表于 2019-12-30 20:11 | 只看该作者
您好,请问您测试这段程序么,我移植了这段程序, mcu发送sla+w,1114不回复ack
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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