2014-12-22 12:35:05 +01:00
|
|
|
// Module for interfacing with adc
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
#include "c_types.h"
|
2015-03-26 17:52:55 +01:00
|
|
|
#include "user_interface.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
// Lua: read(id) , return system adc
|
2015-01-05 03:09:51 +01:00
|
|
|
static int adc_sample( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
unsigned id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( adc, id );
|
|
|
|
unsigned val = 0xFFFF & system_adc_read();
|
|
|
|
lua_pushinteger( L, val );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-26 17:52:55 +01:00
|
|
|
// Lua: readvdd33()
|
|
|
|
static int adc_readvdd33( lua_State* L )
|
|
|
|
{
|
2015-11-10 05:30:59 +01:00
|
|
|
lua_pushinteger(L, system_get_vdd33 ());
|
2015-03-26 17:52:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-18 21:45:15 +02:00
|
|
|
// Lua: adc.force_init_mode(x)
|
|
|
|
static int adc_init107( lua_State *L )
|
|
|
|
{
|
|
|
|
uint8_t byte107 = luaL_checkinteger (L, 1);
|
|
|
|
|
2017-03-02 04:48:44 +01:00
|
|
|
uint32 init_sector = flash_rom_get_sec_num () - 4;
|
2016-05-18 21:45:15 +02:00
|
|
|
|
|
|
|
// Note 32bit alignment so we can safely cast to uint32 for the flash api
|
|
|
|
char init_data[SPI_FLASH_SEC_SIZE] __attribute__((aligned(4)));
|
|
|
|
|
2017-05-16 16:50:36 +02:00
|
|
|
if (SPI_FLASH_RESULT_OK != flash_read (
|
2016-05-18 21:45:15 +02:00
|
|
|
init_sector * SPI_FLASH_SEC_SIZE,
|
|
|
|
(uint32 *)init_data, sizeof(init_data)))
|
|
|
|
return luaL_error(L, "flash read error");
|
|
|
|
|
|
|
|
// If it's already the correct value, we don't need to force it
|
|
|
|
if (init_data[107] == byte107)
|
|
|
|
{
|
|
|
|
lua_pushboolean (L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nope, it differs, we need to rewrite it
|
|
|
|
init_data[107] = byte107;
|
2017-05-16 16:50:36 +02:00
|
|
|
if (SPI_FLASH_RESULT_OK != flash_erase (init_sector))
|
2016-05-18 21:45:15 +02:00
|
|
|
return luaL_error(L, "flash erase error");
|
|
|
|
|
2017-05-16 16:50:36 +02:00
|
|
|
if (SPI_FLASH_RESULT_OK != flash_write (
|
2016-05-18 21:45:15 +02:00
|
|
|
init_sector * SPI_FLASH_SEC_SIZE,
|
|
|
|
(uint32 *)init_data, sizeof(init_data)))
|
|
|
|
return luaL_error(L, "flash write error");
|
|
|
|
|
|
|
|
lua_pushboolean (L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Module function map
|
2015-12-16 06:04:58 +01:00
|
|
|
static const LUA_REG_TYPE adc_map[] = {
|
2015-12-13 03:29:37 +01:00
|
|
|
{ LSTRKEY( "read" ), LFUNCVAL( adc_sample ) },
|
2016-05-18 21:45:15 +02:00
|
|
|
{ LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33 ) },
|
|
|
|
{ LSTRKEY( "force_init_mode" ), LFUNCVAL( adc_init107 ) },
|
|
|
|
{ LSTRKEY( "INIT_ADC" ), LNUMVAL( 0x00 ) },
|
|
|
|
{ LSTRKEY( "INIT_VDD33" ),LNUMVAL( 0xff ) },
|
2014-12-22 12:35:05 +01:00
|
|
|
{ LNILKEY, LNILVAL }
|
|
|
|
};
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
NODEMCU_MODULE(ADC, "adc", adc_map, NULL);
|