i2c.c 4.3 KB

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