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"
|
|
|
|
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.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 );
|
2019-02-17 19:26:29 +01:00
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2020-04-27 02:13:38 +02:00
|
|
|
uint32_t init_data[SPI_FLASH_SEC_SIZE/sizeof(uint32_t)];
|
|
|
|
partition_item_t pd_pt = {0,0,0};
|
|
|
|
uint32_t init_sector;
|
2016-05-18 21:45:15 +02:00
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
luaL_argcheck(L, cast(uint8_t, byte107+1) < 2, 1, "Invalid mode");
|
|
|
|
system_partition_get_item(SYSTEM_PARTITION_PHY_DATA, &pd_pt);
|
|
|
|
init_sector = platform_flash_get_sector_of_address(pd_pt.addr);
|
2016-05-18 21:45:15 +02:00
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (pd_pt.size == 0 ||
|
|
|
|
platform_s_flash_read(init_data, pd_pt.addr, sizeof(init_data))==0)
|
|
|
|
return luaL_error(L, "flash read error");
|
2016-05-18 21:45:15 +02:00
|
|
|
|
|
|
|
// If it's already the correct value, we don't need to force it
|
2020-04-27 02:13:38 +02:00
|
|
|
if (cast(uint8_t *, init_data)[107] == byte107) {
|
2016-05-18 21:45:15 +02:00
|
|
|
lua_pushboolean (L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
cast(uint8_t *, init_data)[107] = byte107;
|
|
|
|
/* Only do erase if toggling 0x00 to 0xFF */
|
|
|
|
if(byte107 && platform_flash_erase_sector(init_sector) != PLATFORM_OK)
|
2016-05-18 21:45:15 +02:00
|
|
|
return luaL_error(L, "flash erase error");
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if(platform_flash_write(init_data, pd_pt.addr, sizeof(init_data))==0)
|
|
|
|
return luaL_error(L, "flash write error");
|
2016-05-18 21:45:15 +02:00
|
|
|
|
|
|
|
lua_pushboolean (L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(adc, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( read, adc_sample )
|
|
|
|
LROT_FUNCENTRY( readvdd33, adc_readvdd33 )
|
|
|
|
LROT_FUNCENTRY( force_init_mode, adc_init107 )
|
|
|
|
LROT_NUMENTRY( INIT_ADC, 0x00 )
|
|
|
|
LROT_NUMENTRY( INIT_VDD33, 0xff )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(adc, NULL, 0)
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
NODEMCU_MODULE(ADC, "adc", adc, NULL);
|