i2c.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Module for interfacing with the I2C interface
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. // Lua: speed = i2c.setup( id, sda, scl, speed )
  6. static int i2c_setup( lua_State *L )
  7. {
  8. unsigned id = luaL_checkinteger( L, 1 );
  9. unsigned sda = luaL_checkinteger( L, 2 );
  10. unsigned scl = luaL_checkinteger( L, 3 );
  11. MOD_CHECK_ID( i2c, id );
  12. MOD_CHECK_ID( gpio, sda );
  13. MOD_CHECK_ID( gpio, scl );
  14. if ( sda == 0 )
  15. return luaL_error( L, "i2c SDA on D0 is not supported" );
  16. s32 speed = ( s32 )luaL_checkinteger( L, 4 );
  17. if ( speed <= 0 )
  18. return luaL_error( L, "wrong arg range" );
  19. speed = platform_i2c_setup( id, sda, scl, (u32)speed );
  20. if ( speed == 0 )
  21. return luaL_error( L, "failed to initialize i2c %d", id );
  22. lua_pushinteger( L, speed );
  23. return 1;
  24. }
  25. // Lua: i2c.start( id )
  26. static int i2c_start( lua_State *L )
  27. {
  28. unsigned id = luaL_checkinteger( L, 1 );
  29. MOD_CHECK_ID( i2c, id );
  30. if (platform_i2c_configured( id ) )
  31. platform_i2c_send_start( id );
  32. else
  33. luaL_error( L, "i2c %d is not configured", id );
  34. return 0;
  35. }
  36. // Lua: i2c.stop( id )
  37. static int i2c_stop( lua_State *L )
  38. {
  39. unsigned id = luaL_checkinteger( L, 1 );
  40. MOD_CHECK_ID( i2c, id );
  41. platform_i2c_send_stop( id );
  42. return 0;
  43. }
  44. // Lua: status = i2c.address( id, address, direction )
  45. static int i2c_address( lua_State *L )
  46. {
  47. unsigned id = luaL_checkinteger( L, 1 );
  48. int address = luaL_checkinteger( L, 2 );
  49. int direction = luaL_checkinteger( L, 3 );
  50. MOD_CHECK_ID( i2c, id );
  51. if ( address < 0 || address > 127 )
  52. return luaL_error( L, "wrong arg range" );
  53. lua_pushboolean( L, platform_i2c_send_address( id, (u16)address, direction ) );
  54. return 1;
  55. }
  56. // Lua: wrote = i2c.write( id, data1, [data2], ..., [datan] )
  57. // data can be either a string, a table or an 8-bit number
  58. static int i2c_write( lua_State *L )
  59. {
  60. unsigned id = luaL_checkinteger( L, 1 );
  61. const char *pdata;
  62. size_t datalen, i;
  63. int numdata;
  64. u32 wrote = 0;
  65. unsigned argn;
  66. MOD_CHECK_ID( i2c, id );
  67. if( lua_gettop( L ) < 2 )
  68. return luaL_error( L, "wrong arg type" );
  69. for( argn = 2; argn <= lua_gettop( L ); argn ++ )
  70. {
  71. // lua_isnumber() would silently convert a string of digits to an integer
  72. // whereas here strings are handled separately.
  73. if( lua_type( L, argn ) == LUA_TNUMBER )
  74. {
  75. numdata = ( int )luaL_checkinteger( L, argn );
  76. if( numdata < 0 || numdata > 255 )
  77. return luaL_error( L, "wrong arg range" );
  78. if( platform_i2c_send_byte( id, numdata ) != 1 )
  79. break;
  80. wrote ++;
  81. }
  82. else if( lua_istable( L, argn ) )
  83. {
  84. datalen = lua_objlen( L, argn );
  85. for( i = 0; i < datalen; i ++ )
  86. {
  87. lua_rawgeti( L, argn, i + 1 );
  88. numdata = ( int )luaL_checkinteger( L, -1 );
  89. lua_pop( L, 1 );
  90. if( numdata < 0 || numdata > 255 )
  91. return luaL_error( L, "wrong arg range" );
  92. if( platform_i2c_send_byte( id, numdata ) == 0 )
  93. break;
  94. }
  95. wrote += i;
  96. if( i < datalen )
  97. break;
  98. }
  99. else
  100. {
  101. pdata = luaL_checklstring( L, argn, &datalen );
  102. for( i = 0; i < datalen; i ++ )
  103. if( platform_i2c_send_byte( id, pdata[ i ] ) == 0 )
  104. break;
  105. wrote += i;
  106. if( i < datalen )
  107. break;
  108. }
  109. }
  110. lua_pushinteger( L, wrote );
  111. return 1;
  112. }
  113. // Lua: read = i2c.read( id, size )
  114. static int i2c_read( lua_State *L )
  115. {
  116. unsigned id = luaL_checkinteger( L, 1 );
  117. u32 size = ( u32 )luaL_checkinteger( L, 2 ), i;
  118. luaL_Buffer b;
  119. int data;
  120. MOD_CHECK_ID( i2c, id );
  121. if( size == 0 )
  122. return 0;
  123. luaL_buffinit( L, &b );
  124. for( i = 0; i < size; i ++ )
  125. if( ( data = platform_i2c_recv_byte( id, i < size - 1 ) ) == -1 )
  126. break;
  127. else
  128. luaL_addchar( &b, ( char )data );
  129. luaL_pushresult( &b );
  130. return 1;
  131. }
  132. // Module function map
  133. LROT_BEGIN(i2c, NULL, 0)
  134. LROT_FUNCENTRY( setup, i2c_setup )
  135. LROT_FUNCENTRY( start, i2c_start )
  136. LROT_FUNCENTRY( stop, i2c_stop )
  137. LROT_FUNCENTRY( address, i2c_address )
  138. LROT_FUNCENTRY( write, i2c_write )
  139. LROT_FUNCENTRY( read, i2c_read )
  140. LROT_NUMENTRY( FASTPLUS, PLATFORM_I2C_SPEED_FASTPLUS )
  141. LROT_NUMENTRY( FAST, PLATFORM_I2C_SPEED_FAST )
  142. LROT_NUMENTRY( SLOW, PLATFORM_I2C_SPEED_SLOW )
  143. LROT_NUMENTRY( TRANSMITTER, PLATFORM_I2C_DIRECTION_TRANSMITTER )
  144. LROT_NUMENTRY( RECEIVER, PLATFORM_I2C_DIRECTION_RECEIVER )
  145. LROT_END(i2c, NULL, 0)
  146. NODEMCU_MODULE(I2C, "i2c", i2c, NULL);