Skip to content

Commit

Permalink
I2c in c (#485)
Browse files Browse the repository at this point in the history
* update i2c timings. limit retry behaviour. allow runtime i2c timing adjust. fix pullup init

* print uses string.format, rm old unused auto-clojure of math lib

* temp files

* Ceify most of crowlib and other extras

* simplify ii.lua by ceify a lil bit and sharing fns

* initial hardcoded solution working for some jf cmds

* introducing 2nd module to test full search. WIP

* code gen working but not built by make yet

* make heirarchy. gcc pragma

* cleanup outdated makefile bits
  • Loading branch information
trentgill authored Jun 1, 2023
1 parent e134db1 commit d36afc0
Show file tree
Hide file tree
Showing 17 changed files with 1,086 additions and 337 deletions.
34 changes: 6 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ DEFS = -DUSE_STDPERIPH_DRIVER -DSTM32F7XX -DARM_MATH_CM7 -DHSE_VALUE=8000000
DEFS += -DSTM32F722xx -DUSE_HAL_DRIVER
STARTUP = $(CUBE)/CMSIS/Device/ST/STM32F7xx/Source/Templates/gcc/startup_stm32f722xx.s

# MCFLAGS = -march=armv4e-m -mthumb
# MCFLAGS = -mthumb -march=armv4e-m -mfloat-abi=hard -mfpu=fpv4-sp-d16
MCFLAGS = -mthumb -march=armv7e-m -mfloat-abi=hard -mfpu=fpv4-sp-d16

STM32_INCLUDES = \
Expand All @@ -49,13 +47,12 @@ OPTIMIZE = -O2

CFLAGS += -std=c99
CFLAGS += -Wall
CFLAGS += -Wno-unused-function
CFLAGS += -Wno-unused-function -Wno-unused-value
CFLAGS += $(MCFLAGS)
CFLAGS += $(OPTIMIZE)
CFLAGS += $(DEFS) -I. -I./ $(STM32_INCLUDES)
CFLAGS += -fsingle-precision-constant -Wdouble-promotion
CFLAGS += -DLUA_32BITS
# CFLAGS += -DLUA_32BITS -DLUA_COMPAT_5_2
CFLAGS += -fno-common
CFLAGS += -DVERSION=\"$(GIT_VERSION)\"
CFLAGS += -ffunction-sections -fdata-sections # provides majority of LTO binary size reduction
Expand Down Expand Up @@ -129,39 +126,25 @@ all: $(TARGET).hex $(BIN)

# fennel script conversion to lua
FNL_SRC = $(wildcard lua/*.fnl) \
#FNL_SRC = $(wildcard util/*.fnl) \

FNL_PP = $(FNL_SRC:%.fnl=%.lua)

# i2c descriptors
II_SRCD = lua/ii
II_SRC = $(wildcard $(II_SRCD)/*.lua)
II_TARGET = $(addprefix $(BUILD_DIR)/ii_, $(notdir $(II_SRC)))

$(II_TARGET): util/ii_lua_module.lua

$(BUILD_DIR)/ii_%.lua: $(II_SRCD)/%.lua util/ii_lua_module.lua | $(BUILD_DIR)
@lua util/ii_lua_module.lua $< $@
@echo "ii-lua-module $< -> $@"

$(BUILD_DIR)/iihelp.lua: $(II_SRC) util/ii_lua_help.lua | $(BUILD_DIR)
@lua util/ii_lua_help.lua $(II_SRCD) $@
@echo "ii-lua-help $@"

$(BUILD_DIR)/ii_c_layer.h: $(II_SRC) util/ii_c_layer.lua | $(BUILD_DIR)
@lua util/ii_c_layer.lua $(II_SRCD) $@
@echo "ii-c-layer $@"

$(BUILD_DIR)/ii_lualink.h: $(II_SRC) util/ii_lualinker.lua | $(BUILD_DIR)
@lua util/ii_lualinker.lua $(II_SRCD) $@
@echo "ii-lualinker $@"
$(BUILD_DIR)/ii_mod_gen.h: $(II_SRC) util/ii_mod_gen.lua | $(BUILD_DIR)
@lua util/ii_mod_gen.lua $(II_SRCD) $@
@echo "ii-mod-gen $@"


### destination sources

# lua srcs: these get converted to bytecode strings wrapped in c-headers
# LUA_SRC = $(wildcard lua/*.lua)
LUA_SRC += lua/asl.lua
LUA_SRC += lua/asllib.lua
LUA_SRC += lua/calibrate.lua
Expand All @@ -177,8 +160,6 @@ LUA_SRC += lua/quote.lua
LUA_SRC += lua/sequins.lua
LUA_SRC += lua/timeline.lua
LUA_SRC += lua/hotswap.lua
LUA_SRC += $(BUILD_DIR)/iihelp.lua
LUA_SRC += $(II_TARGET)

LUA_PP = $(LUA_SRC:%.lua=%.lua.h)
LUA_PP: $(LUA_SRC)
Expand All @@ -200,9 +181,9 @@ OBJS += Startup.o
$(OBJS): $(LUA_PP)

# specific objects that require built dependencies (ii)
$(OBJDIR)/lib/l_bootstrap.o: $(LUA_PP) $(BUILD_DIR)/ii_lualink.h
# $(OBJDIR)/lib/lualink.o: $(LUA_PP) $(BUILD_DIR)/ii_lualink.h
$(OBJDIR)/lib/l_bootstrap.o: $(LUA_PP) #$(BUILD_DIR)/ii_lualink.h
$(OBJDIR)/lib/ii.o: $(BUILD_DIR)/ii_c_layer.h
$(OBJDIR)/lib/l_ii_mod.o: $(BUILD_DIR)/ii_mod_gen.h

# generate the build directory
$(BUILD_DIR):
Expand Down Expand Up @@ -232,7 +213,6 @@ tests:

# include all DEP files in the makefile
# will rebuild elements if dependent C headers are changed
# FIXME: currently causes compiler warning due to missing .lua.h files
-include $(DEP)

$(TARGET).hex: $(EXECUTABLE)
Expand All @@ -259,7 +239,6 @@ flash: $(BIN)

debug:
make flash TRACE=1
#st-flash write $(BIN) 0x08020000
stlink-trace -c 216

dfu: $(BIN)
Expand All @@ -272,7 +251,6 @@ dfureset:

pydfu: $(TARGET).dfu $(BIN)
@python3 util/pydfu.py -u $<
# @python3 util/pydfu.py --vid 0x0483 --pid 0xDF11 -u $<

$(TARGET).dfu: $(BIN)
python3 util/dfu.py -D 0x0483:0xDF11 -b 0x08020000:$^ $@
Expand Down
9 changes: 6 additions & 3 deletions lib/caw.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ static C_cmd_t _find_cmd( char* str, uint32_t len )
return C_none;
}

static uint8_t _is_multiline( char* first_char )
static uint8_t _is_multiline( char* first_char, uint32_t len )
{
// need at least 3 chars to be a valid multiline sequence
if(len < 3){ return 0; }
// buffer must lead with the backticks
if( *first_char++ == '`' ){
if( *first_char++ == '`' ){
if( *first_char == '`' ){ return 1; }
Expand Down Expand Up @@ -161,7 +164,7 @@ C_cmd_t Caw_try_receive( void )
retcmd = C_none; // no action
goto exit;
}
if( _is_multiline( (char*)buf ) ){
if( _is_multiline( (char*)buf, len ) ){
multiline ^= 1;
if(!multiline){
retcmd = C_repl;
Expand All @@ -176,7 +179,7 @@ C_cmd_t Caw_try_receive( void )
}
}
}
if( pReader + len > USB_RX_BUFFER ){ // overflow protection
if( (pReader + len) >= USB_RX_BUFFER ){ // overflow protection
pReader = 0;
Caw_send_luachunk("!chunk too long!");
printf("!chunk too long!\n");
Expand Down
22 changes: 18 additions & 4 deletions lib/ii.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ uint8_t ii_leader_enqueue_bytes( uint8_t address

void ii_leader_process( void )
{
static bool retrying = false; // flag to monitor if this is a retry

if( !I2C_is_ready() ){ return; } // I2C lib is busy

int ix = queue_front(l_qix);
if( ix < 0 ){ return; } // queue is empty!
ii_q_t* q = &l_iq[ix];
Expand All @@ -187,7 +190,7 @@ void ii_leader_process( void )
)) ){
if( error & 0x6 ){ error_action( 1 ); }
printf("leadRx failed %i\n",error);
return; // EARLY RETURN. DOESN'T POP QUEUE. WILL RETRY
goto retry; // attempt retry, or abandon
}
} else {
if( (error = I2C_LeadTx( q->address
Expand All @@ -196,10 +199,21 @@ void ii_leader_process( void )
)) ){
if( error & 2 ){ error_action( 1 ); }
printf("leadTx failed %i\n",error);
return; // EARLY RETURN. DOESN'T POP QUEUE. WILL RETRY
goto retry; // attempt retry, or abandon
}
}
queue_dequeue(l_qix); // pop the value as it was used
queue_dequeue(l_qix); // pop the value from queue as it was successfully used
return;

retry:
if(retrying){
retrying = false;
queue_dequeue(l_qix); // pop the value, abandoning this message
} else {
retrying = true;
// DON'T POP QUEUE. we'll try it next time
}
return;
}


Expand Down Expand Up @@ -318,7 +332,7 @@ static void error_action( int error_code )
{
switch( error_code ){
case 0: // Ack Failed
printf("I2C_ERROR_AF\n"); // means can't find device
// printf("I2C_ERROR_AF\n"); // means can't find device
// TODO make this a global variable which can be checked by user
// becomes a basic way to ask "was the message received"
break;
Expand Down
28 changes: 20 additions & 8 deletions lib/l_bootstrap.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "l_bootstrap.h"

#include <string.h>
#include <stdbool.h>

#include "l_crowlib.h"

Expand All @@ -14,14 +15,21 @@
#include "build/input.h"
#include "build/output.h"
#include "build/ii.h"
#include "build/iihelp.h" // generated lua stub for loading i2c modules
// #include "build/iihelp.h" // generated lua stub for loading i2c modules
#include "build/calibrate.h"
#include "build/sequins.h"
#include "build/quote.h"
#include "build/timeline.h"
#include "build/hotswap.h"

#include "build/ii_lualink.h" // generated C header for linking to lua
// #include "build/ii_lualink.h" // generated C header for linking to lua

struct lua_lib_locator{
const char* name;
const unsigned char* addr_of_luacode;
const bool stripped;
const unsigned int len;
};

static int _open_lib( lua_State *L, const struct lua_lib_locator* lib, const char* name );
static void lua_full_gc(lua_State* L);
Expand All @@ -37,7 +45,7 @@ const struct lua_lib_locator Lua_libs[] =
, { "lua_output" , build_output_lc , true, build_output_lc_len}
, { "lua_public" , build_public_lc , true, build_public_lc_len}
, { "lua_ii" , build_ii_lc , true, build_ii_lc_len}
, { "build_iihelp" , build_iihelp_lc , true, build_iihelp_lc_len}
// , { "build_iihelp" , build_iihelp_lc , true, build_iihelp_lc_len}
, { "lua_calibrate" , build_calibrate_lc , true, build_calibrate_lc_len}
, { "lua_sequins" , build_sequins_lc , true, build_sequins_lc_len}
, { "lua_quote" , build_quote_lc , true, build_quote_lc_len}
Expand All @@ -60,6 +68,10 @@ void l_bootstrap_init(lua_State* L){
// _c = dofile('lua/crowlib.lua')
lua_pushliteral(L, "lua/crowlib.lua");
l_bootstrap_dofile(L); // hotrod without l_call
lua_settop(L, 0);

// _c = {}
lua_newtable(L);
lua_setglobal(L, "_c");

// crow = _c
Expand Down Expand Up @@ -110,11 +122,11 @@ int l_bootstrap_dofile(lua_State* L)
case 1: lua_full_gc(L); return 1;
default: break;
}
switch( _open_lib( L, Lua_ii_libs, cname ) ){
case -1: goto fail;
case 1: lua_full_gc(L); return 1;
default: break;
}
// switch( _open_lib( L, Lua_ii_libs, cname ) ){
// case -1: goto fail;
// case 1: lua_full_gc(L); return 1;
// default: break;
// }
printf("can't open library: %s\n", (char*)cname);

fail:
Expand Down
Loading

0 comments on commit d36afc0

Please sign in to comment.