i2c.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /* ****************************************************************************
  2. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Maxim Integrated
  23. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  24. * Products, Inc. Branding Policy.
  25. *
  26. * The mere transfer of this software does not imply any licenses
  27. * of trade secrets, proprietary technology, copyrights, patents,
  28. * trademarks, maskwork rights, or any other form of intellectual
  29. * property whatsoever. Maxim Integrated Products, Inc. retains all
  30. * ownership rights.
  31. *
  32. * $Date: 2020-02-03 10:33:50 -0600 (Mon, 03 Feb 2020) $
  33. * $Revision: 51326 $
  34. *
  35. *************************************************************************** */
  36. #include <stddef.h>
  37. #include <stdint.h>
  38. #include "mxc_config.h"
  39. #include "mxc_assert.h"
  40. #include "mxc_lock.h"
  41. #include "mxc_sys.h"
  42. #include "i2c.h"
  43. #include <stdio.h>
  44. #include "mxc_delay.h"
  45. /* **** Definitions **** */
  46. #define I2C_ERROR (MXC_F_I2C_INT_FL0_ARB_ER | MXC_F_I2C_INT_FL0_TO_ER | MXC_F_I2C_INT_FL0_ADDR_NACK_ER | \
  47. MXC_F_I2C_INT_FL0_DATA_ER | MXC_F_I2C_INT_FL0_DO_NOT_RESP_ER | MXC_F_I2C_INT_FL0_START_ER | \
  48. MXC_F_I2C_INT_FL0_STOP_ER)
  49. #define MASTER 1
  50. #define SLAVE 0
  51. /* For high speed mode, if the I2C bus capacitance is greater than 100pF, set this value to ((capacitance - 100) / 3).
  52. Otherwise leave it at 0. */
  53. #define HS_SCALE_FACTOR (0)
  54. #define T_LOW_MIN (160 + (160 * HS_SCALE_FACTOR / 100)) /* tLOW minimum in nanoseconds */
  55. #define T_HIGH_MIN (60 + (60 * HS_SCALE_FACTOR / 100)) /* tHIGH minimum in nanoseconds */
  56. #define T_R_MAX_HS (40 + (40 * HS_SCALE_FACTOR / 100)) /* tR maximum for high speed mode in nanoseconds */
  57. #define T_F_MAX_HS (40 + (40 * HS_SCALE_FACTOR / 100)) /* tF maximum for high speed mode in nanoseconds */
  58. #define T_AF_MIN (10 + (10 * HS_SCALE_FACTOR / 100)) /* tAF minimun in nanoseconds */
  59. /* **** Variable Declaration **** */
  60. // Saves the state of the non-blocking requests
  61. typedef struct {
  62. i2c_req_t *req;
  63. i2c_state_t state;
  64. uint8_t num_wr; // keep track of number of bytes loaded in the fifo during slave transmit
  65. } i2c_req_state_t;
  66. static i2c_req_state_t states[MXC_I2C_INSTANCES];
  67. /* **** Function Prototypes **** */
  68. static void I2C_MasterHandler(mxc_i2c_regs_t *i2c);
  69. static void I2C_SlaveHandler(mxc_i2c_regs_t *i2c);
  70. static void I2C_FreeCallback(int i2c_num, int error);
  71. static void I2C_Recover(mxc_i2c_regs_t *i2c);
  72. /* ************************************************************************** */
  73. static int I2C_Setspeed(mxc_i2c_regs_t * i2c, i2c_speed_t i2cspeed)
  74. {
  75. uint32_t ticks, ticks_lo, ticks_hi;
  76. if (i2cspeed == I2C_HS_MODE) {
  77. uint32_t sys_freq, tPCLK, targBusFreq, tSCLmin, cklMin, ckhMin, ckh_cklMin;
  78. /* Compute dividers for high speed mode. */
  79. sys_freq = SYS_I2C_GetFreq(i2c);
  80. MXC_ASSERT(sys_freq >= 1000);
  81. tPCLK = 1000000 / (sys_freq / 1000);
  82. MXC_ASSERT(tPCLK > 0)
  83. targBusFreq = i2cspeed - ((i2cspeed/2) * HS_SCALE_FACTOR / 100);
  84. if(targBusFreq < 1000) {
  85. return E_BAD_PARAM;
  86. }
  87. tSCLmin = 1000000 / (targBusFreq / 1000);
  88. cklMin = ((T_LOW_MIN + T_F_MAX_HS + (tPCLK - 1) - T_AF_MIN) / tPCLK) - 1;
  89. ckhMin = ((T_HIGH_MIN + T_R_MAX_HS + (tPCLK - 1) - T_AF_MIN) / tPCLK) - 1;
  90. ckh_cklMin = ((tSCLmin + (tPCLK - 1)) / tPCLK) - 2;
  91. ticks_lo = (cklMin > (ckh_cklMin - ckhMin)) ? (cklMin) : (ckh_cklMin - ckhMin);
  92. ticks_hi = ckhMin;
  93. if((ticks_lo > (MXC_F_I2C_HS_CLK_HS_CLK_LO >> MXC_F_I2C_HS_CLK_HS_CLK_LO_POS)) ||
  94. (ticks_hi > (MXC_F_I2C_HS_CLK_HS_CLK_HI >> MXC_F_I2C_HS_CLK_HS_CLK_HI_POS))) {
  95. return E_BAD_PARAM;
  96. }
  97. /* Write results to destination registers. */
  98. i2c->hs_clk = (ticks_lo << MXC_F_I2C_HS_CLK_HS_CLK_LO_POS) | (ticks_hi <<
  99. MXC_F_I2C_HS_CLK_HS_CLK_HI_POS);
  100. /* Still need to load dividers for the preamble that each high-speed transaction starts with.
  101. Switch setting to fast mode and fall out of if statement. */
  102. i2cspeed = I2C_FAST_MODE;
  103. }
  104. /* Get the number of periph clocks needed to achieve selected speed. */
  105. ticks = SYS_I2C_GetFreq(i2c) / i2cspeed;
  106. /* For a 50% duty cycle, half the ticks will be spent high and half will be low. */
  107. ticks_hi = (ticks >> 1) - 1;
  108. ticks_lo = (ticks >> 1) - 1;
  109. /* Account for rounding error in odd tick counts. */
  110. if (ticks & 1) {
  111. ticks_hi++;
  112. }
  113. /* Will results fit into 9 bit registers? (ticks_hi will always be >= ticks_lo. No need to check ticks_lo.) */
  114. if (ticks_hi > 0x1FF) {
  115. return E_BAD_PARAM;
  116. }
  117. /* 0 is an invalid value for the destination registers. (ticks_hi will always be >= ticks_lo. No need to check ticks_hi.) */
  118. if (ticks_lo == 0) {
  119. return E_BAD_PARAM;
  120. }
  121. /* Write results to destination registers. */
  122. i2c->clk_lo = ticks_lo;
  123. i2c->clk_hi = ticks_hi;
  124. return E_NO_ERROR;
  125. }
  126. /* ************************************************************************** */
  127. int I2C_Init(mxc_i2c_regs_t *i2c, i2c_speed_t i2cspeed, const sys_cfg_i2c_t* sys_cfg)
  128. {
  129. int err;
  130. int idx = MXC_I2C_GET_IDX(i2c);
  131. // Check the base pointer
  132. MXC_ASSERT(idx >= 0);
  133. // Set system level configurations
  134. if ((err = SYS_I2C_Init(i2c, sys_cfg)) != E_NO_ERROR) {
  135. return err;
  136. }
  137. // Always disable the HW autoflush on data NACK and let the SW handle the flushing.
  138. i2c->tx_ctrl0 |= 0x20;
  139. states[idx].num_wr = 0;
  140. i2c->ctrl = 0; // clear configuration bits
  141. i2c->ctrl = MXC_F_I2C_CTRL_I2C_EN; // Enable I2C
  142. i2c->master_ctrl = 0; // clear master configuration bits
  143. i2c->status = 0; // clear status bits
  144. /* If either SDA or SCL is already low, there is a problem.
  145. * Try reclaiming the bus by sending clocks until we have control of the SDA line.
  146. * Follow procedure defined in i2c spec.
  147. */
  148. if ((i2c->ctrl & (MXC_F_I2C_CTRL_SCL | MXC_F_I2C_CTRL_SDA)) !=
  149. (MXC_F_I2C_CTRL_SCL | MXC_F_I2C_CTRL_SDA)) {
  150. int i, have_control;
  151. // Set SCL/SDA as software controlled.
  152. i2c->ctrl |= MXC_F_I2C_CTRL_SW_OUT_EN;
  153. // Try to get control of SDA.
  154. for (i = 0; i < 16; i++) {
  155. have_control = 1;
  156. // Drive SCL low and check its state.
  157. i2c->ctrl &= ~(MXC_F_I2C_CTRL_SCL_OUT);
  158. mxc_delay(MXC_DELAY_USEC(5));
  159. if ((i2c->ctrl & MXC_F_I2C_CTRL_SCL) == MXC_F_I2C_CTRL_SCL) {
  160. have_control = 0;
  161. }
  162. // Drive SDA low and check its state.
  163. i2c->ctrl &= ~(MXC_F_I2C_CTRL_SDA_OUT);
  164. mxc_delay(MXC_DELAY_USEC(5));
  165. if ((i2c->ctrl & MXC_F_I2C_CTRL_SDA) == MXC_F_I2C_CTRL_SDA) {
  166. have_control = 0;
  167. }
  168. // Release SDA and check its state.
  169. i2c->ctrl |= (MXC_F_I2C_CTRL_SDA_OUT);
  170. mxc_delay(MXC_DELAY_USEC(5));
  171. if ((i2c->ctrl & MXC_F_I2C_CTRL_SDA) != MXC_F_I2C_CTRL_SDA) {
  172. have_control = 0;
  173. }
  174. // Release SCL and check its state.
  175. i2c->ctrl |= (MXC_F_I2C_CTRL_SCL_OUT);
  176. mxc_delay(MXC_DELAY_USEC(5));
  177. if ((i2c->ctrl & MXC_F_I2C_CTRL_SCL) != MXC_F_I2C_CTRL_SCL) {
  178. have_control = 0;
  179. }
  180. if (have_control) {
  181. // Issue stop
  182. // Drive SDA low.
  183. i2c->ctrl &= ~(MXC_F_I2C_CTRL_SDA_OUT);
  184. mxc_delay(MXC_DELAY_USEC(5));
  185. // Release SDA.
  186. i2c->ctrl |= (MXC_F_I2C_CTRL_SDA_OUT);
  187. mxc_delay(MXC_DELAY_USEC(5));
  188. break;
  189. }
  190. }
  191. if (!have_control) {
  192. return E_COMM_ERR;
  193. }
  194. }
  195. i2c->ctrl = 0; // clear configuration bits
  196. i2c->ctrl = MXC_F_I2C_CTRL_I2C_EN; // Enable I2C
  197. i2c->master_ctrl = 0; // clear master configuration bits
  198. i2c->status= 0; // clear status bits
  199. // Check for HS mode
  200. if (i2cspeed == I2C_HS_MODE) {
  201. i2c->ctrl |= MXC_F_I2C_CTRL_HS_MODE; // Enable HS mode
  202. }
  203. // Disable and clear interrupts
  204. i2c->int_en0 = 0;
  205. i2c->int_en1 = 0;
  206. i2c->int_fl0 = i2c->int_fl0;
  207. i2c->int_fl1 = i2c->int_fl1;
  208. i2c->timeout = 0x0; // set timeout
  209. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH; // clear the RX FIFO
  210. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH; // clear the TX FIFO
  211. return I2C_Setspeed(i2c, i2cspeed);
  212. }
  213. /* ************************************************************************** */
  214. int I2C_Shutdown(mxc_i2c_regs_t *i2c)
  215. {
  216. int i2c_num, err;
  217. // Check the base pointer
  218. i2c_num = MXC_I2C_GET_IDX(i2c);
  219. MXC_ASSERT(i2c_num >= 0);
  220. // Disable and clear interrupts
  221. i2c->int_en0 = 0;
  222. i2c->int_en1 = 0;
  223. i2c->int_fl0 = i2c->int_fl0;
  224. i2c->int_fl1 = i2c->int_fl1;
  225. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH; // clear the RX FIFO
  226. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH; // clear the TX FIFO
  227. // Call all of the pending callbacks for this I2C
  228. if (states[i2c_num].req != NULL) {
  229. I2C_Recover(i2c);
  230. I2C_FreeCallback(i2c_num, E_SHUTDOWN);
  231. }
  232. i2c->ctrl = 0;
  233. // Clears system level configurations
  234. if ((err = SYS_I2C_Shutdown(i2c)) != E_NO_ERROR) {
  235. return err;
  236. }
  237. return E_NO_ERROR;
  238. }
  239. /* ************************************************************************** */
  240. int I2C_MasterWrite(mxc_i2c_regs_t *i2c, uint8_t addr, const uint8_t* data, int len, int restart)
  241. {
  242. int save_len = len;
  243. if (len == 0) {
  244. return E_NO_ERROR;
  245. }
  246. // Clear the lock out bit (W1C) in case it is set.
  247. i2c->int_fl0 = MXC_F_I2C_INT_FL0_TX_LOCK_OUT;
  248. i2c->int_fl0 = i2c->int_fl0;
  249. // Enable master mode
  250. i2c->ctrl |= MXC_F_I2C_CTRL_MST;
  251. // Load FIFO with slave address for WRITE and as much data as we can
  252. while (i2c->status & MXC_F_I2C_STATUS_TX_FULL) {}
  253. i2c->fifo = addr & ~(0x1);
  254. while ((len > 0) && !(i2c->status & MXC_F_I2C_STATUS_TX_FULL)) {
  255. i2c->fifo = *data++;
  256. len--;
  257. }
  258. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_START;
  259. // Write remaining data to FIFO
  260. while (len > 0) {
  261. // Check for errors
  262. if (i2c->int_fl0 & I2C_ERROR) {
  263. // Set the stop bit
  264. i2c->master_ctrl &= ~(MXC_F_I2C_MASTER_CTRL_RESTART);
  265. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  266. while (!(i2c->int_fl0 & (MXC_F_I2C_INT_FL0_STOP))) {}
  267. return E_COMM_ERR;
  268. }
  269. if (!(i2c->status & MXC_F_I2C_STATUS_TX_FULL)) {
  270. i2c->fifo = *data++;
  271. len--;
  272. }
  273. }
  274. if (restart) {
  275. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_RESTART;
  276. } else {
  277. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  278. }
  279. // Wait for Done or time out if enabled
  280. while (!(i2c->int_fl0 & (MXC_F_I2C_INT_FL0_DONE | I2C_ERROR ))) {}
  281. i2c->int_fl0 = MXC_F_I2C_INT_FL0_DONE;
  282. // Wait for Stop
  283. if (!restart) {
  284. while (!(i2c->int_fl0 & (MXC_F_I2C_INT_FL0_STOP ))) {}
  285. i2c->int_fl0 = MXC_F_I2C_INT_FL0_STOP;
  286. }
  287. // Check for errors
  288. if (i2c->int_fl0 & I2C_ERROR) {
  289. return E_COMM_ERR;
  290. }
  291. return save_len;
  292. }
  293. /* ************************************************************************** */
  294. int I2C_MasterRead(mxc_i2c_regs_t *i2c, uint8_t addr, uint8_t* data, int len, int restart)
  295. {
  296. int save_len = len;
  297. if (len == 0) {
  298. return E_NO_ERROR;
  299. }
  300. if (len > 256) {
  301. return E_BAD_PARAM;
  302. }
  303. i2c->int_fl0 = MXC_F_I2C_INT_FL0_TX_LOCK_OUT;
  304. i2c->int_fl0 = i2c->int_fl0;
  305. // Make sure the I2C has been initialized
  306. if (!(i2c->ctrl & MXC_F_I2C_CTRL_I2C_EN)) {
  307. return E_UNINITIALIZED;
  308. }
  309. // Enable master mode
  310. i2c->ctrl |= MXC_F_I2C_CTRL_MST;
  311. // Set receive count
  312. i2c->rx_ctrl1= len;
  313. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_START;
  314. // Load FIFO with slave address
  315. while (i2c->status & MXC_F_I2C_STATUS_TX_FULL) {}
  316. i2c->fifo = (addr | 1);
  317. // Wait for all data to be received or error
  318. while (len > 0) {
  319. // Check for errors
  320. if (i2c->int_fl0 & I2C_ERROR) {
  321. // Set the stop bit
  322. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  323. return E_COMM_ERR;
  324. }
  325. if (!(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  326. *data++ = i2c->fifo;
  327. len--;
  328. }
  329. }
  330. if (i2c->int_fl0 & I2C_ERROR) {
  331. // Set the stop bit
  332. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  333. return E_COMM_ERR;
  334. }
  335. if (restart) {
  336. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_RESTART;
  337. } else {
  338. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  339. }
  340. // Wait for Done or time out if enabled
  341. while (!(i2c->int_fl0 & (MXC_F_I2C_INT_FL0_DONE | I2C_ERROR ))) {}
  342. i2c->int_fl0 = MXC_F_I2C_INT_FL0_DONE;
  343. // Wait for Stop
  344. if (!restart) {
  345. while (!(i2c->int_fl0 & (MXC_F_I2C_INT_FL0_STOP | I2C_ERROR))) {
  346. }
  347. i2c->int_fl0 = MXC_F_I2C_INT_FL0_STOP;
  348. }
  349. // Check for errors
  350. if (i2c->int_fl0 & I2C_ERROR) {
  351. return E_COMM_ERR;
  352. }
  353. return save_len;
  354. }
  355. /* ************************************************************************** */
  356. int I2C_Slave(mxc_i2c_regs_t *i2c, uint8_t addr, const uint8_t* read_data, int read_len,
  357. uint8_t* write_data, int write_len, int* tx_num, int* rx_num,
  358. i2c_autoflush_disable_t sw_autoflush_disable)
  359. {
  360. int i2c_num;
  361. i2c_num = MXC_I2C_GET_IDX(i2c);
  362. if ((read_data == NULL) && (write_data == NULL)) {
  363. return E_NULL_PTR;
  364. }
  365. // Make sure the I2C has been initialized
  366. if (!(i2c->ctrl & MXC_F_I2C_CTRL_I2C_EN)) {
  367. return E_UNINITIALIZED;
  368. }
  369. if ((read_len == 0) && (write_len == 0)) {
  370. return E_NO_ERROR;
  371. }
  372. if (mxc_get_lock((uint32_t*)&states[i2c_num].req, 1) != E_NO_ERROR) {
  373. return E_BUSY;
  374. }
  375. // Disable master mode
  376. i2c->ctrl &= ~MXC_F_I2C_CTRL_MST;
  377. // Clear any previous errors
  378. i2c->int_fl0 = i2c->int_fl0;
  379. i2c->int_fl1 = i2c->int_fl1;
  380. // Set the slave address
  381. i2c->slave_addr = (addr >> 1);
  382. // Wait for address match
  383. while (!(i2c->int_fl0 & MXC_F_I2C_INT_FL0_ADDR_MATCH) && !(i2c->int_fl0 & I2C_ERROR)) {
  384. }
  385. i2c->int_fl0 = MXC_F_I2C_INT_FL0_ADDR_MATCH;
  386. i2c->int_fl0 = MXC_F_I2C_INT_FL0_TX_LOCK_OUT;
  387. if (i2c->int_fl0 & I2C_ERROR) {
  388. if (!sw_autoflush_disable) {
  389. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  390. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH;
  391. }
  392. mxc_free_lock((uint32_t*)&states[i2c_num]);
  393. return E_COMM_ERR;
  394. }
  395. // See if we're reading or writing
  396. if (i2c->ctrl & MXC_F_I2C_CTRL_READ) {
  397. // This is the master read/slave write case
  398. if (read_data == NULL || read_len == 0) {
  399. mxc_free_lock((uint32_t*)&states[i2c_num]);
  400. return E_NULL_PTR;
  401. }
  402. // Wait for all data to be received or error
  403. while (read_len > 0) {
  404. // Check for errors
  405. if (i2c->int_fl0 & I2C_ERROR) {
  406. *tx_num = states[i2c_num].num_wr - ((i2c->tx_ctrl1 & MXC_F_I2C_TX_CTRL1_TX_FIFO) >> MXC_F_I2C_TX_CTRL1_TX_FIFO_POS);
  407. states[i2c_num].num_wr = 0;
  408. if (!sw_autoflush_disable) {
  409. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  410. }
  411. mxc_free_lock((uint32_t*)&states[i2c_num]);
  412. return E_COMM_ERR;
  413. }
  414. // Check for nack from master
  415. if (i2c->int_fl0 & MXC_F_I2C_INT_FL0_TX_LOCK_OUT) {
  416. break;
  417. }
  418. // Check for done bit
  419. if (i2c->int_fl0 & MXC_F_I2C_INT_FL0_DONE) {
  420. break;
  421. }
  422. if (!(i2c->status & MXC_F_I2C_STATUS_TX_FULL)) {
  423. i2c->fifo = *read_data++;
  424. states[i2c_num].num_wr++;
  425. read_len--;
  426. }
  427. }
  428. // Wait for Done
  429. while (!(i2c->int_fl0 & MXC_F_I2C_INT_FL0_DONE)) {}
  430. // Calculate number of bytes sent by the slave
  431. *tx_num = states[i2c_num].num_wr - ((i2c->tx_ctrl1 & MXC_F_I2C_TX_CTRL1_TX_FIFO) >> MXC_F_I2C_TX_CTRL1_TX_FIFO_POS);
  432. states[i2c_num].num_wr = 0;
  433. if (!sw_autoflush_disable) {
  434. // Flush the TX FIFO
  435. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  436. }
  437. } else {
  438. // This is the master write/slave read case
  439. if (write_data == NULL || write_len == 0) {
  440. mxc_free_lock((uint32_t*)&states[i2c_num]);
  441. return E_NULL_PTR;
  442. }
  443. // Wait for all data to be written or error
  444. while (write_len > 0) {
  445. // Check for errors
  446. if (i2c->int_fl0 & I2C_ERROR) {
  447. if (!sw_autoflush_disable) {
  448. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH;
  449. }
  450. mxc_free_lock((uint32_t*)&states[i2c_num]);
  451. return E_COMM_ERR;
  452. }
  453. // Check for done bit
  454. if (i2c->int_fl0 & MXC_F_I2C_INT_FL0_DONE) {
  455. break;
  456. }
  457. if (!(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  458. *write_data++ = i2c->fifo;
  459. (*rx_num)++;
  460. write_len--;
  461. }
  462. }
  463. // Wait for Done
  464. while (!(i2c->int_fl0 & MXC_F_I2C_INT_FL0_DONE)) {
  465. }
  466. // Flush the FIFO
  467. if (!sw_autoflush_disable) {
  468. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH;
  469. }
  470. }
  471. // Check for errors
  472. if (i2c->int_fl0 & I2C_ERROR) {
  473. // Flush the FIFO
  474. if (!sw_autoflush_disable) {
  475. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  476. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH;
  477. }
  478. mxc_free_lock((uint32_t*)&states[i2c_num]);
  479. return E_COMM_ERR;
  480. }
  481. mxc_free_lock((uint32_t*)&states[i2c_num]);
  482. return E_NO_ERROR;
  483. }
  484. /* ************************************************************************** */
  485. int I2C_MasterAsync(mxc_i2c_regs_t *i2c, i2c_req_t *req)
  486. {
  487. int i2c_num;
  488. i2c_num = MXC_I2C_GET_IDX(i2c);
  489. if (req->state == I2C_STATE_READING) {
  490. // Check the parameters
  491. if (req->rx_len == 0) {
  492. return E_NO_ERROR;
  493. }
  494. if (req->rx_data == NULL) {
  495. return E_NULL_PTR;
  496. }
  497. } else {
  498. // Check the parameters
  499. if (req->tx_len == 0) {
  500. return E_NO_ERROR;
  501. }
  502. if (req->tx_data == NULL) {
  503. return E_NULL_PTR;
  504. }
  505. }
  506. // Make sure the I2C has been initialized
  507. if (!(i2c->ctrl & MXC_F_I2C_CTRL_I2C_EN)) {
  508. return E_UNINITIALIZED;
  509. }
  510. // Attempt to register this request
  511. if (mxc_get_lock((uint32_t*)&states[i2c_num].req, (uint32_t)req) != E_NO_ERROR) {
  512. return E_BUSY;
  513. }
  514. states[i2c_num].state = req->state;
  515. states[i2c_num].req = req;
  516. // Enable master mode
  517. i2c->ctrl |= MXC_F_I2C_CTRL_MST;
  518. // Clear the byte counters
  519. req->tx_num = 0;
  520. req->rx_num = 0;
  521. // Disable and clear the interrupts
  522. i2c->int_en0 = 0;
  523. i2c->int_en1 = 0;
  524. i2c->int_fl0 = i2c->int_fl0;
  525. i2c->int_fl1 = i2c->int_fl1;
  526. // Start the transaction
  527. I2C_MasterHandler(i2c);
  528. return E_NO_ERROR;
  529. }
  530. /* ************************************************************************** */
  531. static void I2C_MasterHandler(mxc_i2c_regs_t *i2c)
  532. {
  533. uint32_t int0, inten0 = 0;
  534. int rx_remain, tx_remain, i2c_num;
  535. i2c_req_t *req;
  536. i2c_num = MXC_I2C_GET_IDX(i2c);
  537. req = states[i2c_num].req;
  538. // Check for errors
  539. int0 = i2c->int_fl0;
  540. if (int0 & I2C_ERROR) {
  541. // Set the done bit
  542. i2c->master_ctrl &= ~(MXC_F_I2C_MASTER_CTRL_RESTART);
  543. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  544. i2c->int_en0 = 0;
  545. if (req->callback != NULL) {
  546. I2C_Recover(i2c);
  547. I2C_FreeCallback(i2c_num, E_COMM_ERR);
  548. }
  549. return;
  550. }
  551. rx_remain = req->rx_len - req->rx_num;
  552. tx_remain = req->tx_len - req->tx_num;
  553. if (req->restart) {
  554. // Check for DONE interrupt
  555. if ((int0 & MXC_F_I2C_INT_FL0_DONE)) {
  556. // Read out any data in the RX FIFO
  557. while (rx_remain && !(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  558. *(req->rx_data)++ = i2c->fifo;
  559. req->rx_num++;
  560. rx_remain--;
  561. }
  562. i2c->int_en0 = 0;
  563. if (req->callback != NULL) {
  564. I2C_Recover(i2c);
  565. I2C_FreeCallback(i2c_num, E_NO_ERROR);
  566. }
  567. return;
  568. }
  569. } else {
  570. // Check for STOP interrupt
  571. if ((int0 & MXC_F_I2C_INT_FL0_STOP)) {
  572. i2c->int_en0 = 0;
  573. if (req->callback != NULL) {
  574. I2C_Recover(i2c);
  575. I2C_FreeCallback(i2c_num, E_NO_ERROR);
  576. }
  577. return;
  578. }
  579. // Check for DONE interrupt
  580. if ((int0 & MXC_F_I2C_INT_FL0_DONE)) {
  581. // Read out any data in the RX FIFO
  582. while (rx_remain && !(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  583. *(req->rx_data)++ = i2c->fifo;
  584. req->rx_num++;
  585. rx_remain--;
  586. }
  587. return;
  588. }
  589. }
  590. // Clear the interrupts
  591. i2c->int_fl0 = int0;
  592. if (states[i2c_num].state == I2C_STATE_READING) {
  593. // Read out any data in the RX FIFO
  594. while (rx_remain && !(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  595. *(req->rx_data)++ = i2c->fifo;
  596. req->rx_num++;
  597. rx_remain--;
  598. }
  599. // Load the slave address if we haven't already started reading the data
  600. if (rx_remain == req->rx_len) {
  601. i2c->fifo = (req->addr | 1);
  602. // Set the RX Count
  603. i2c->rx_ctrl1 = req->rx_len;
  604. // Start transmission if idle
  605. if (!(i2c->status & MXC_F_I2C_STATUS_BUS)) {
  606. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_START;
  607. }
  608. // Set restart or stop
  609. if (req->restart) {
  610. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_RESTART;
  611. } else {
  612. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  613. inten0 |= MXC_F_I2C_INT_EN0_STOP;
  614. }
  615. }
  616. // Set the RX threshold interrupt level
  617. if (rx_remain >= (MXC_I2C_FIFO_DEPTH - 1)) {
  618. i2c->rx_ctrl1 = ((i2c->rx_ctrl1 & ~(MXC_F_I2C_RX_CTRL0_RX_THRESH)) |
  619. (MXC_I2C_FIFO_DEPTH - 1) << MXC_F_I2C_RX_CTRL0_RX_THRESH_POS);
  620. inten0 |= MXC_F_I2C_INT_EN0_RX_THRESH;
  621. }else{
  622. i2c->rx_ctrl1 = ((i2c->rx_ctrl1 & ~(MXC_F_I2C_RX_CTRL0_RX_THRESH)) |
  623. (rx_remain) << MXC_F_I2C_RX_CTRL0_RX_THRESH_POS);
  624. inten0 |= MXC_F_I2C_INT_EN0_RX_THRESH;
  625. }
  626. } else {
  627. // Load the slave address if we haven't already started writing the data
  628. if (tx_remain == req->tx_len) {
  629. i2c->fifo = req->addr;
  630. // Start transmission if idle
  631. if (!(i2c->status & MXC_F_I2C_STATUS_BUS)) {
  632. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_START;
  633. }
  634. }
  635. // Fill the FIFO
  636. while ((tx_remain > 0) && !(i2c->status & MXC_F_I2C_STATUS_TX_FULL)) {
  637. i2c->fifo = *(req->tx_data)++;
  638. req->tx_num++;
  639. tx_remain--;
  640. }
  641. // Set the TX threshold interrupt level, or restart/stop
  642. if (tx_remain) {
  643. i2c->tx_ctrl1 = ((i2c->tx_ctrl1 & ~(MXC_F_I2C_TX_CTRL0_TX_THRESH)) | (1 << MXC_F_I2C_TX_CTRL0_TX_THRESH_POS));
  644. inten0 |= MXC_F_I2C_INT_EN0_TX_THRESH;
  645. }
  646. // Set restart or stop if at the end of the transaction since these actions happen at the moment the bit is set.
  647. else if (req->restart) {
  648. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_RESTART;
  649. } else {
  650. i2c->master_ctrl |= MXC_F_I2C_MASTER_CTRL_STOP;
  651. inten0 |= MXC_F_I2C_INT_EN0_STOP;
  652. }
  653. }
  654. inten0 |= (MXC_F_I2C_INT_EN0_DONE | I2C_ERROR);
  655. i2c->int_en0 = inten0;
  656. }
  657. /* ************************************************************************** */
  658. int I2C_SlaveAsync(mxc_i2c_regs_t *i2c, i2c_req_t *req)
  659. {
  660. int i2c_num;
  661. i2c_num = MXC_I2C_GET_IDX(i2c);
  662. // Make sure the I2C has been initialized
  663. if (!(i2c->ctrl & MXC_F_I2C_CTRL_I2C_EN)) {
  664. return E_UNINITIALIZED;
  665. }
  666. // Attempt to register this request
  667. if (mxc_get_lock((uint32_t*)&states[i2c_num].req, (uint32_t)req) != E_NO_ERROR) {
  668. return E_BUSY;
  669. }
  670. states[i2c_num].req = req;
  671. // Set Slave Address
  672. i2c->slave_addr = (req->addr >> 1);
  673. // Clear the byte counters
  674. req->tx_num = 0;
  675. req->rx_num = 0;
  676. // Disable and clear the interrupts
  677. i2c->int_en0 = 0;
  678. i2c->int_en1 = 0;
  679. i2c->int_fl0 = i2c->int_fl0;
  680. i2c->int_fl1 = i2c->int_fl1;
  681. i2c->int_en0 |= MXC_F_I2C_INT_EN0_ADDR_MATCH;
  682. return E_NO_ERROR;
  683. }
  684. /* ************************************************************************** */
  685. static void I2C_SlaveHandler(mxc_i2c_regs_t *i2c)
  686. {
  687. uint32_t int0, inten0 = 0;
  688. int rx_remain, tx_remain, i2c_num;
  689. i2c_req_t *req;
  690. i2c_num = MXC_I2C_GET_IDX(i2c);
  691. req = states[i2c_num].req;
  692. if ( i2c->int_fl0 & MXC_F_I2C_INT_FL0_ADDR_MATCH ) {
  693. i2c->int_fl0 |=MXC_F_I2C_INT_EN0_STOP;
  694. }
  695. // Check for errors
  696. int0 = i2c->int_fl0;
  697. if (int0 & I2C_ERROR) {
  698. i2c->int_en0 = 0;
  699. // Calculate the number of bytes sent by the slave
  700. req->tx_num = states[i2c_num].num_wr - ((i2c->tx_ctrl1 & MXC_F_I2C_TX_CTRL1_TX_FIFO) >> MXC_F_I2C_TX_CTRL1_TX_FIFO_POS);
  701. if (!req->sw_autoflush_disable) {
  702. // Manually clear the TXFIFO
  703. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  704. }
  705. states[i2c_num].num_wr = 0;
  706. if (req->callback != NULL) {
  707. I2C_Recover(i2c);
  708. I2C_FreeCallback(i2c_num, E_COMM_ERR);
  709. }
  710. return;
  711. }
  712. rx_remain = req->rx_len - req->rx_num;
  713. tx_remain = req->tx_len - states[i2c_num].num_wr;
  714. //Check if Master Write has been called and if there is a rx_data buffer
  715. if ((i2c->int_fl0 & MXC_F_I2C_INT_FL0_TX_LOCK_OUT) && !(i2c->ctrl & MXC_F_I2C_CTRL_READ)) {
  716. i2c->int_en0 = 0;
  717. if (req->rx_data == NULL) {
  718. I2C_Recover(i2c);
  719. I2C_FreeCallback(i2c_num, E_NULL_PTR);
  720. return;
  721. }
  722. }
  723. // Check for DONE interrupt
  724. if (int0 & MXC_F_I2C_INT_EN0_DONE) {
  725. // Read out any data in the RX FIFO
  726. while (rx_remain && !(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  727. *(req->rx_data)++ = i2c->fifo;
  728. req->rx_num++;
  729. rx_remain--;
  730. }
  731. // Calculate the number of bytes sent by the slave
  732. req->tx_num = states[i2c_num].num_wr - ((i2c->tx_ctrl1 & MXC_F_I2C_TX_CTRL1_TX_FIFO) >> MXC_F_I2C_TX_CTRL1_TX_FIFO_POS);
  733. if (!req->sw_autoflush_disable) {
  734. // Manually clear the TXFIFO
  735. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  736. }
  737. states[i2c_num].num_wr = 0;
  738. i2c->int_en0 = 0;
  739. if (req->callback != NULL) {
  740. if (i2c->int_fl0 & MXC_F_I2C_INT_FL0_STOP) {
  741. I2C_Recover(i2c);
  742. } else {
  743. i2c->int_fl0 = i2c->int_fl0;
  744. i2c->int_fl1 = i2c->int_fl1;
  745. }
  746. I2C_FreeCallback(i2c_num, E_NO_ERROR);
  747. }
  748. return;
  749. }
  750. // Clear the interrupts
  751. i2c->int_fl0 = int0;
  752. if (i2c->ctrl & MXC_F_I2C_CTRL_READ) {
  753. i2c->int_en0 = 0;
  754. if (req->tx_data == NULL) {
  755. I2C_Recover(i2c);
  756. I2C_FreeCallback(i2c_num, E_NULL_PTR);
  757. return;
  758. }
  759. // Fill the FIFO
  760. while ((tx_remain > 0) && !(i2c->status & MXC_F_I2C_STATUS_TX_FULL)) {
  761. i2c->fifo = *(req->tx_data)++;
  762. states[i2c_num].num_wr++;
  763. tx_remain--;
  764. }
  765. // Set the TX threshold interrupt level
  766. if (tx_remain) {
  767. i2c->tx_ctrl0 = ((i2c->tx_ctrl0 & ~(MXC_F_I2C_TX_CTRL0_TX_THRESH)) | (1 << MXC_F_I2C_TX_CTRL0_TX_THRESH_POS));
  768. inten0 |= MXC_F_I2C_INT_EN0_TX_THRESH;
  769. }
  770. } else {
  771. // Read out any data in the RX FIFO
  772. while (rx_remain && !(i2c->status & MXC_F_I2C_STATUS_RX_EMPTY)) {
  773. *(req->rx_data)++ = i2c->fifo;
  774. req->rx_num++;
  775. rx_remain--;
  776. }
  777. // Set the RX threshold interrupt level
  778. if (rx_remain >= (MXC_I2C_FIFO_DEPTH - 1)) {
  779. i2c->rx_ctrl0 = ((i2c->rx_ctrl0 & ~(MXC_F_I2C_RX_CTRL0_RX_THRESH)) |
  780. (MXC_I2C_FIFO_DEPTH - 1) << MXC_F_I2C_RX_CTRL0_RX_THRESH_POS);
  781. inten0 |= MXC_F_I2C_INT_EN0_RX_THRESH;
  782. }else{
  783. i2c->rx_ctrl0 = ((i2c->rx_ctrl0 & ~(MXC_F_I2C_RX_CTRL0_RX_THRESH)) |
  784. (rx_remain) << MXC_F_I2C_RX_CTRL0_RX_THRESH_POS);
  785. inten0 |= MXC_F_I2C_INT_EN0_RX_THRESH;
  786. }
  787. }
  788. inten0 |= (MXC_F_I2C_INT_EN0_DONE | I2C_ERROR | MXC_F_I2C_INT_EN0_TX_LOCK_OUT);
  789. i2c->int_en0 = inten0;
  790. }
  791. /* ************************************************************************** */
  792. void I2C_Handler(mxc_i2c_regs_t *i2c)
  793. {
  794. if (i2c->ctrl & MXC_F_I2C_CTRL_MST && i2c->int_fl0) {
  795. // Service master interrupts if we're in master mode
  796. I2C_MasterHandler(i2c);
  797. } else if (i2c->int_fl0 || i2c->int_fl1) {
  798. // Service the slave interrupts
  799. I2C_SlaveHandler(i2c);
  800. }
  801. }
  802. /* ************************************************************************** */
  803. void I2C_DrainRX(mxc_i2c_regs_t *i2c)
  804. {
  805. i2c->rx_ctrl0 |= MXC_F_I2C_RX_CTRL0_RX_FLUSH;
  806. }
  807. /* ************************************************************************** */
  808. void I2C_DrainTX(mxc_i2c_regs_t *i2c)
  809. {
  810. i2c->tx_ctrl0 |= MXC_F_I2C_TX_CTRL0_TX_FLUSH;
  811. }
  812. /* ************************************************************************* */
  813. static void I2C_FreeCallback(int i2c_num, int error)
  814. {
  815. // Save the request
  816. i2c_req_t *temp_req = states[i2c_num].req;
  817. mxc_free_lock((uint32_t*)&states[i2c_num].req);
  818. // Callback if not NULL
  819. if (temp_req->callback != NULL) {
  820. temp_req->callback(temp_req, error);
  821. }
  822. }
  823. /* ************************************************************************* */
  824. static void I2C_Recover(mxc_i2c_regs_t *i2c)
  825. {
  826. // Disable and clear interrupts
  827. i2c->int_en0 = 0;
  828. i2c->int_en1 = 0;
  829. i2c->int_fl0 = i2c->int_fl0;
  830. i2c->int_fl1 = i2c->int_fl1;
  831. i2c->ctrl = 0;
  832. i2c->ctrl = MXC_F_I2C_CTRL_I2C_EN;
  833. }
  834. /* ************************************************************************* */
  835. int I2C_AbortAsync(i2c_req_t *req)
  836. {
  837. int i2c_num;
  838. mxc_i2c_regs_t *i2c;
  839. // Find the request, set to NULL
  840. for (i2c_num = 0; i2c_num < MXC_I2C_INSTANCES; i2c_num++) {
  841. if (req == states[i2c_num].req) {
  842. i2c = MXC_I2C_GET_I2C(i2c_num);
  843. I2C_Recover(i2c);
  844. I2C_FreeCallback(i2c_num, E_ABORT);
  845. return E_NO_ERROR;
  846. }
  847. }
  848. return E_BAD_PARAM;
  849. }
  850. /* ************************************************************************* */
  851. int I2C_SetTimeout(mxc_i2c_regs_t *i2c, int us){
  852. uint32_t timeout;
  853. timeout = (PeripheralClock/1000000) * us;
  854. if(timeout > 0xFFFF){
  855. return E_BAD_PARAM;
  856. }
  857. i2c->timeout = timeout;
  858. return E_NO_ERROR;
  859. }
  860. /* ************************************************************************* */
  861. void I2C_ClearTimeout (mxc_i2c_regs_t *i2c)
  862. {
  863. i2c->timeout = 0;
  864. }