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.  
  26. volatile uint32_t I2CMasterState = I2C_IDLE;
  27. volatile uint32_t I2CSlaveState = I2C_IDLE;
  28.  
  29. volatile uint32_t I2CMode;
  30.  
  31. volatile uint8_t I2CWrBuffer[BUFSIZE];
  32. volatile uint8_t I2CRdBuffer[BUFSIZE];
  33. volatile uint32_t I2CReadLength;
  34. volatile uint32_t I2CWriteLength;
  35.  
  36. ;
  37. ;
  38.  
  39. /*
  40. From device to device, the I2C communication protocol may vary,
  41. in the example below, the protocol uses repeated start to read data from or
  42. write to the device:
  43. For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO
  44. for master write: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO
  45. Thus, in state 8, the address is always WRITE. in state 10, the address could
  46. be READ or WRITE depending on the I2C command.
  47. */
  48.  
  49. /*****************************************************************************
  50. ** Function name:I2C_IRQHandler
  51. **
  52. ** Descriptions:I2C interrupt handler, deal with master mode only.
  53. **
  54. ** parameters:None
  55. ** Returned value:None
  56. **
  57. *****************************************************************************/
  58. void I2C_IRQHandler(void)
  59. {
  60. uint8_t StatValue;
  61.  
  62. /* this handler deals with master read and master write only */
  63. StatValue = LPC_I2C->STAT;
  64. switch ( StatValue )
  65. {
  66. case 0x60:/* An own SLA_W has been received. */
  67. case 0x68:
  68. RdIndex = ;
  69. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_W is received */
  70. LPC_I2C->CONCLR = I2CONCLR_SIC;
  71. I2CSlaveState = I2C_WR_STARTED;
  72. break;
  73. case 0x80:/* data receive */
  74. case 0x90:
  75. if ( I2CSlaveState == I2C_WR_STARTED )
  76. {
  77. I2CRdBuffer[RdIndex++] = LPC_I2C->DAT;
  78. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after data is received */
  79. }
  80. else
  81. {
  82. LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
  83. }
  84. LPC_I2C->CONCLR = I2CONCLR_SIC;
  85. break;
  86. case 0xA8:/* An own SLA_R has been received. */
  87. case 0xB0:
  88. RdIndex = ;
  89. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_R is received */
  90. LPC_I2C->CONCLR = I2CONCLR_SIC;
  91. I2CSlaveState = I2C_RD_STARTED;
  92. WrIndex = I2CRdBuffer[];/* The 1st byte is the index. */
  93. break;
  94. case 0xB8:/* Data byte has been transmitted */
  95. case 0xC8:
  96. if ( I2CSlaveState == I2C_RD_STARTED )
  97. {
  98. LPC_I2C->DAT = I2CRdBuffer[WrIndex+];/* write the same data back to master */
  99. WrIndex++;/* Need to skip the index byte in RdBuffer */
  100. LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK */
  101. }
  102. else
  103. {
  104. LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
  105. }
  106. LPC_I2C->CONCLR = I2CONCLR_SIC;
  107. break;
  108.  
  109. case 0xC0:/* Data byte has been transmitted, NACK */
  110. LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
  111. LPC_I2C->CONCLR = I2CONCLR_SIC;
  112. I2CSlaveState = DATA_NACK;
  113. break;
  114.  
  115. case 0xA0:/* Stop condition or repeated start has */
  116. LPC_I2C->CONSET = I2CONSET_AA;/* been received, assert ACK. */
  117. LPC_I2C->CONCLR = I2CONCLR_SIC;
  118. I2CSlaveState = I2C_IDLE;
  119. break;
  120.  
  121. default:
  122. LPC_I2C->CONCLR = I2CONCLR_SIC;
  123. LPC_I2C->CONSET = I2CONSET_I2EN | I2CONSET_SI;
  124. break;
  125. }
  126. return;
  127. }
  128.  
  129. /*****************************************************************************
  130. ** Function name:I2CSlaveInit
  131. **
  132. ** Descriptions:Initialize I2C controller
  133. **
  134. ** parameters:I2c mode is either MASTER or SLAVE
  135. ** Returned value:true or false, return false if the I2C
  136. **interrupt handler was not installed correctly
  137. **
  138. *****************************************************************************/
  139. void I2CSlaveInit( void )
  140. {
  141. /* SSP and I2C reset are overlapped, a known bug,
  142. for now, both SSP and I2C use bit 0 for reset enable.
  143. Once the problem is fixed, change to "#if 1". */
  144. #if 1
  145. LPC_SYSCON->PRESETCTRL |= ();
  146. #else
  147. LPC_SYSCON->PRESETCTRL |= ();
  148. #endif
  149. LPC_SYSCON->SYSAHBCLKCTRL |= (<<);
  150. LPC_IOCON->PIO0_4 &= ~0x3F;/* I2C I/O config */
  151. LPC_IOCON->PIO0_4 |= 0x01;/* I2C SCL */
  152. LPC_IOCON->PIO0_5 &= ~0x3F;
  153. LPC_IOCON->PIO0_5 |= 0x01;/* I2C SDA */
  154.  
  155. /*--- Clear flags ---*/
  156. LPC_I2C->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;
  157.  
  158. /*--- Reset registers ---*/
  159. #if FAST_MODE_PLUS
  160. LPC_IOCON->PIO0_4 |= ();
  161. LPC_IOCON->PIO0_5 |= ();
  162. LPC_I2C->SCLL = I2SCLL_HS_SCLL;
  163. LPC_I2C->SCLH = I2SCLH_HS_SCLH;
  164. #else
  165. LPC_I2C->SCLL = I2SCLL_SCLL;
  166. LPC_I2C->SCLH = I2SCLH_SCLH;
  167. #endif
  168.  
  169. LPC_I2C->ADR0 = PCF8594_ADDR;
  170. I2CSlaveState = I2C_IDLE;
  171.  
  172. /* Enable the I2C Interrupt */
  173. NVIC_EnableIRQ(I2C_IRQn);
  174.  
  175. LPC_I2C->CONSET = I2CONSET_I2EN | I2CONSET_SI;
  176. return;
  177. }
  178.  
  179. /******************************************************************************
  180. ** End Of File
  181. ******************************************************************************/
  182.  
  183. /****************************************************************************
  184. * $Id:: i2cslave.h 3635 2010-06-02 00:31:46Z usb00423 $
  185. * Project: NXP LPC11xx I2C Slave example
  186. *
  187. * Description:
  188. * This file contains I2C slave code header definition.
  189. *
  190. ****************************************************************************
  191. * Software that is described herein is for illustrative purposes only
  192. * which provides customers with programming information regarding the
  193. * products. This software is supplied "AS IS" without any warranties.
  194. * NXP Semiconductors assumes no responsibility or liability for the
  195. * use of the software, conveys no license or title under any patent,
  196. * copyright, or mask work right to the product. NXP Semiconductors
  197. * reserves the right to make changes in the software without
  198. * notification. NXP Semiconductors also make no representation or
  199. * warranty that such application will be suitable for the specified
  200. * use without further testing or modification.
  201. ****************************************************************************/
  202. #ifndef __I2CSLAVE_H
  203. #define __I2CSLAVE_H
  204.  
  205. #define FAST_MODE_PLUS 1
  206.  
  207. #define BUFSIZE 6
  208. #define MAX_TIMEOUT 0x00FFFFFF
  209.  
  210. #define PCF8594_ADDR 0xA0
  211. #define READ_WRITE 0x01
  212.  
  213. #define RD_BIT 0x01
  214.  
  215. #define I2C_IDLE 0
  216. #define I2C_STARTED 1
  217. #define I2C_RESTARTED 2
  218. #define I2C_REPEATED_START 3
  219. #define DATA_ACK 4
  220. #define DATA_NACK 5
  221. #define I2C_WR_STARTED 6
  222. #define I2C_RD_STARTED 7
  223.  
  224. #define I2CONSET_I2EN (0x1<<6) /* I2C Control Set Register */
  225. #define I2CONSET_AA (0x1<<2)
  226. #define I2CONSET_SI (0x1<<3)
  227. #define I2CONSET_STO (0x1<<4)
  228. #define I2CONSET_STA (0x1<<5)
  229.  
  230. #define I2CONCLR_AAC (0x1<<2) /* I2C Control clear Register */
  231. #define I2CONCLR_SIC (0x1<<3)
  232. #define I2CONCLR_STAC (0x1<<5)
  233. #define I2CONCLR_I2ENC (0x1<<6)
  234.  
  235. #define I2DAT_I2C 0x00000000 /* I2C Data Reg */
  236. #define I2ADR_I2C 0x00000000 /* I2C Slave Address Reg */
  237. #define I2SCLH_SCLH 0x00000180 /* I2C SCL Duty Cycle High Reg */
  238. #define I2SCLL_SCLL 0x00000180 /* I2C SCL Duty Cycle Low Reg */
  239. #define I2SCLH_HS_SCLH 0x00000020 /* Fast Plus I2C SCL Duty Cycle High Reg */
  240. #define I2SCLL_HS_SCLL 0x00000020 /* Fast Plus I2C SCL Duty Cycle Low Reg */
  241.  
  242. extern void I2C_IRQHandler( void );
  243. extern void I2CSlaveInit( void );
  244.  
  245. #endif /* end __I2CSLAVE_H */
  246. /****************************************************************************
  247. ** End Of File
  248. *****************************************************************************/
  249.  
  250. /****************************************************************************
  251. * $Id:: i2cslvtst.c 3635 2010-06-02 00:31:46Z usb00423 $
  252. * Project: NXP LPC11xx I2C example
  253. *
  254. * Description:
  255. * This file contains I2C slave test modules, main entry, to test I2C
  256. * slave APIs.
  257. *
  258. ****************************************************************************
  259. * Software that is described herein is for illustrative purposes only
  260. * which provides customers with programming information regarding the
  261. * products. This software is supplied "AS IS" without any warranties.
  262. * NXP Semiconductors assumes no responsibility or liability for the
  263. * use of the software, conveys no license or title under any patent,
  264. * copyright, or mask work right to the product. NXP Semiconductors
  265. * reserves the right to make changes in the software without
  266. * notification. NXP Semiconductors also make no representation or
  267. * warranty that such application will be suitable for the specified
  268. * use without further testing or modification.
  269. ****************************************************************************/
  270. #include "LPC11xx.h"/* LPC11xx Peripheral Registers */
  271. #include "type.h"
  272. #include "i2cslave.h"
  273.  
  274. extern volatile uint8_t I2CWrBuffer[BUFSIZE];
  275. extern volatile uint8_t I2CRdBuffer[BUFSIZE];
  276. extern volatile uint32_t I2CSlaveState;
  277. extern volatile uint32_t I2CReadLength, I2CWriteLength;
  278.  
  279. /*******************************************************************************
  280. ** Main Function main()
  281. *******************************************************************************/
  282. int main (void)
  283. {
  284. uint32_t i;
  285.  
  286. SystemInit();
  287.  
  288. ; i < BUFSIZE; i++ )
  289. {
  290. I2CRdBuffer[i] = 0x00;
  291. }
  292.  
  293. I2CSlaveInit();/* initialize I2c */
  294.  
  295. /* When the NACK occurs, the master has stopped the
  296. communication. Just check the content of I2CRd/WrBuffer. */
  297. while ( I2CSlaveState != DATA_NACK );
  298. ;
  299. }
  300.  
  301. /******************************************************************************
  302. ** End Of File
  303. ******************************************************************************/

NXP LPC11xx I2C Slave 从机程序的更多相关文章

  1. java: Runtime和Process调用本机程序

    java: Runtime和Process调用本机程序 调用纸牌程序,Process用来销毁程序 import java.io.IOException; public class RunTimeDem ...

  2. JAVA - ATM机程序

    ATM机程序 UnionPayTest.java package oo.day06.work; public class UnionPayTest { } interface UnionPay{ // ...

  3. vc++MFC开发上位机程序

    用vc++MFC开发过不少跟单片机通讯的上位机程序了.搞懂了MFC架构,开发还是很快的,与底层单片机程序通讯,可以用串口.usb.网络.短信形式.串口现在用的越来越少了,一般电脑跟单片机在一块,使用串 ...

  4. VC++编写简单串口上位机程序

    VC++编写简单串口上位机程序   转载: http://blog.sina.com.cn/s/articlelist_1809084904_0_1.html VC++编写简单串口上位机程序 串口通信 ...

  5. 如何在linux中测试i2c slave模式驱动的功能?

    1. 硬件要求 1.1 需要两台机器,一台作为i2c master(记为M),另一台作为i2c slave(记为S) 1.2 使用杜邦线连接两台机器的i2c信号线 2. 使能内核选项CONFIG_I2 ...

  6. 模拟I2C从机程序

    ;Slave.asm SCL BIT P1. SDA BIT P1. ;---------------------------- ORG RESET: SETB SCL SETB SDA CALL I ...

  7. QT编写上位机程序一定要初始化变量以及谨慎操作指针

    背景: 在编写QT上位机界面时,界面在运行的时候经常出现卡死或者直接挂掉的怪现象. 正文: 上位机有个函数为check_receive():该函数的作用为定时调用循环检测USB是否有数据.若有,则将信 ...

  8. 第一次尝试使用JAVA编写的ATM机程序

    package study; import java.util.Scanner; public class ATM { private static int[] users = { 111111, 2 ...

  9. tomcat如何按站点调试本机程序

    1.配置host host地址:c:\windows\system32\drivers\etc 配置本机域名: # localhost name resolution is handled withi ...

随机推荐

  1. flume学习安装

    近期项目组有需求点击流日志须要自己收集,学习了一下flume而且成功安装了.相关信息记录一下. 1)下载flume1.5版本号  wget http://www.apache.org/dyn/clos ...

  2. Java语言基础(八)

    Java语言基础(八) 一.数学运算  + - * /  % (1)凡是byte  short  char类型都按int类型的计算   看看上面的代码,为什么出错! 我已经将100转成byte类型,( ...

  3. [转] 再叙TIME_WAIT

    http://huoding.com/2013/12/31/316 之所以起这样一个题目是因为很久以前我曾经写过一篇介绍TIME_WAIT的文章,不过当时基本属于浅尝辄止,并没深入说明问题的来龙去脉, ...

  4. xslt语法之---If Else

    大家都知道,XSL中是没有if else的,那么要想实现if else该怎么办呢? 其实很简单 <xsl:choose> <xsl:when test="position( ...

  5. HttpContext.Current

    HttpContext. Response 直接这样写会报错 是因为 httpcontext没有提供response 这个静态的方法. 通过这样写就可以 ASP.NET还为它提供了一个静态属性Http ...

  6. WisDom.Net 框架设计(三) 数据缓存

    WisDom.Net  --数据缓存 1.几种缓存方式       1.静态全局变量 C#静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明 ...

  7. Uri、UriMatcher、ContentUris详解

    http://blog.csdn.net/feng88724/article/details/6331396 1.Uri 通用资源标志符(Universal Resource Identifier, ...

  8. ILMerge合并程序

    在DOS窗口中,进入到ILMerge的安装目录 中 如图所示,之后写合并代码, 使用命令进行捆绑,以如图为例,将CSkin.dll和MyTool.exe捆绑成一个新的newtool.exe文件./ou ...

  9. Java反射学习(java reflect)(二)

    ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...

  10. 关于JavaScript的类的继承

    其实最一开始学JS的时候就看过继承的实现.当时只是去试着理解从书上看来的代码段而已.今天又重新思考了一下,感觉这是一个思维探索演进的结果. 继承,即复用. 如果抛开继承的固有思想,让b复用a的成员,最 ...