2015-11-19 22:54:06 +01:00
|
|
|
// Module for HX711 load cell amplifier
|
|
|
|
// https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-10-09 05:34:24 +02:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2016-05-26 10:36:20 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-10-09 05:34:24 +02:00
|
|
|
#include "user_interface.h"
|
Initial pass at switching to RTOS SDK.
This compiles, links, and starts the RTOS without crashing and burning.
Lua environment does not yet start due to the different task architecture.
Known pain points:
- task implementation needs to be rewritten for RTOS (next up on my TODO)
- secure espconn does not exist, all secure espconn stuff has been #if 0'd
- lwip now built from within the RTOS SDK, but does not appear to include
MDNS support. Investigation needed.
- there is no access to FRC1 NMI, not sure if we ever actually used that
however. Also #if 0'd out for now.
- new timing constraints introduced by the RTOS, all use of ets_delay_us()
and os_delay_us() needs to be reviewed (the tsl2561 driver in particular).
- even more confusion with ets_ vs os_ vs c_ vs non-prefixed versions.
In the long run everything should be switched to non-prefixed versions.
- system_set_os_print() not available, needs to be reimplemented
- all the RTOS rodata is loaded into RAM, as it apparently uses some
constants while the flash isn't mapped, so our exception handler can't
work its magic. This should be narrowed down to the minimum possible
at some point.
- with each task having its own stack in RTOS, we probably need change
flash-page buffers from the stack to the heap in a bunch of places.
A single, shared, page buffer *might* be possible if we limit ourselves
to running NodeMCU in a single task.
- there's a ton of junk in the sdk-overrides now; over time the core code
should be updated to not need those shims
2016-05-24 07:05:01 +02:00
|
|
|
#include "esp_system.h"
|
|
|
|
|
2015-10-09 05:34:24 +02:00
|
|
|
static uint8_t data_pin;
|
|
|
|
static uint8_t clk_pin;
|
|
|
|
|
2015-10-09 05:44:19 +02:00
|
|
|
/*Lua: hx711.init(clk_pin,data_pin)*/
|
2015-10-09 05:34:24 +02:00
|
|
|
static int hx711_init(lua_State* L) {
|
|
|
|
clk_pin = luaL_checkinteger(L,1);
|
|
|
|
data_pin = luaL_checkinteger(L,2);
|
|
|
|
MOD_CHECK_ID( gpio, clk_pin );
|
|
|
|
MOD_CHECK_ID( gpio, data_pin );
|
|
|
|
|
|
|
|
platform_gpio_mode(clk_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
|
|
|
|
platform_gpio_mode(data_pin, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_FLOAT);
|
|
|
|
platform_gpio_write(clk_pin,1);//put chip to sleep.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HX711_MAX_WAIT 1000000
|
|
|
|
/*will only read chA@128gain*/
|
|
|
|
/*Lua: result = hx711.read()*/
|
|
|
|
static int ICACHE_FLASH_ATTR hx711_read(lua_State* L) {
|
|
|
|
uint32_t i;
|
|
|
|
int32_t data = 0;
|
|
|
|
//TODO: double check init has happened first.
|
|
|
|
|
|
|
|
//wakeup hx711
|
|
|
|
platform_gpio_write(clk_pin,0);
|
2015-11-19 22:54:06 +01:00
|
|
|
|
2015-10-09 05:34:24 +02:00
|
|
|
//wait for data ready. or time out.
|
|
|
|
//TODO: set pin inturrupt and come back to it. This may take up to 1/10 sec
|
|
|
|
// or maybe just make an async version too and have both available.
|
2015-11-04 02:46:37 +01:00
|
|
|
system_soft_wdt_feed(); //clear WDT... this may take a while.
|
2015-10-09 05:34:24 +02:00
|
|
|
for (i = 0; i<HX711_MAX_WAIT && platform_gpio_read(data_pin)==1;i++){
|
2015-10-09 05:44:19 +02:00
|
|
|
asm ("nop");
|
2015-10-09 05:34:24 +02:00
|
|
|
}
|
2015-11-19 22:54:06 +01:00
|
|
|
|
2015-10-09 05:34:24 +02:00
|
|
|
//Handle timeout error
|
|
|
|
if (i>=HX711_MAX_WAIT) {
|
|
|
|
return luaL_error( L, "ADC timeout!", ( unsigned )0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i<24 ; i++){ //clock in the 24 bits
|
|
|
|
platform_gpio_write(clk_pin,1);
|
|
|
|
platform_gpio_write(clk_pin,0);
|
|
|
|
data = data<<1;
|
|
|
|
if (platform_gpio_read(data_pin)==1) {
|
|
|
|
data = i==0 ? -1 : data|1; //signextend the first bit
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 22:54:06 +01:00
|
|
|
//add 25th clock pulse to prevent protocol error (probably not needed
|
2015-10-09 05:34:24 +02:00
|
|
|
// since we'll go to sleep immediately after and reset on wakeup.)
|
|
|
|
platform_gpio_write(clk_pin,1);
|
|
|
|
platform_gpio_write(clk_pin,0);
|
|
|
|
//sleep
|
|
|
|
platform_gpio_write(clk_pin,1);
|
|
|
|
lua_pushinteger( L, data );
|
2015-11-19 22:54:06 +01:00
|
|
|
return 1;
|
2015-10-09 05:34:24 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 03:29:37 +01:00
|
|
|
// Module function map
|
2015-12-16 06:04:58 +01:00
|
|
|
static const LUA_REG_TYPE hx711_map[] = {
|
2015-10-09 05:34:24 +02:00
|
|
|
{ LSTRKEY( "init" ), LFUNCVAL( hx711_init )},
|
|
|
|
{ LSTRKEY( "read" ), LFUNCVAL( hx711_read )},
|
|
|
|
{ LNILKEY, LNILVAL}
|
|
|
|
};
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
int luaopen_hx711(lua_State *L) {
|
2015-10-09 05:34:24 +02:00
|
|
|
// TODO: Make sure that the GPIO system is initialized
|
2015-12-12 04:27:31 +01:00
|
|
|
return 0;
|
2015-10-09 05:34:24 +02:00
|
|
|
}
|
2015-12-16 06:04:58 +01:00
|
|
|
|
|
|
|
NODEMCU_MODULE(HX711, "hx711", hx711_map, luaopen_hx711);
|