From ec725f7e334dec3c953b14b6111f261c282cc935 Mon Sep 17 00:00:00 2001 From: Paul Molloy Date: Tue, 1 Aug 2017 17:17:13 -0500 Subject: [PATCH] Getting closer to how I'd like to handle dictionary calls with lua. Effectively deleted old dictionary call function/files. Created lua_usb_vend_xfr function so lua can directly send and receive vendor setup transfers. Dictionary calls are more like function calls now, and all args aren't required so the LED can be turned on for example in lua like so: dict_pinport("LED_ON") general format is: dict_name( opcode, operand, misc, datastring ) Also added ability to store opcode's return length in shared dict library files with RL=number in the comments following the opcode. Negative numbers designate OUT transfers, positive for IN. Default value can be determined by each dictionary's calling function. Decided pinport is 1 for SUCCESS/ERROR CODE. Also have default return data means with second byte giving length of return data in bytes that follows. dictionary call function reports any errors reported by the device and returns any return data from the device excluding error code / data len Now time to start implementing some of these dictionaries on the device. --- firmware/source/pinport_al.h | 3 +- firmware/source/usb.c | 18 +++- host/scripts/inlretro.lua | 151 +++++++++++++++++++++++++++-- host/source/inlprog.c | 9 +- host/source/{ => old}/dictionary.c | 0 host/source/{ => old}/dictionary.h | 0 host/source/usb_operations.c | 97 ++++++++++++++++-- host/source/usb_operations.h | 13 ++- shared/shared_dict_pinport.h | 4 +- shared/shared_errors.h | 78 +++++++-------- 10 files changed, 301 insertions(+), 72 deletions(-) rename host/source/{ => old}/dictionary.c (100%) rename host/source/{ => old}/dictionary.h (100%) diff --git a/firmware/source/pinport_al.h b/firmware/source/pinport_al.h index ef9d6a2..f03ec95 100644 --- a/firmware/source/pinport_al.h +++ b/firmware/source/pinport_al.h @@ -717,7 +717,7 @@ #define _ADDRL(low) A76bank->ODR = (A76bank->ODR & 0xFCFF) | ((low & 0xC0)<<2);A50bank->ODR = (A50bank->ODR & 0xFF03) | ((low & 0x3F)<<2) //clocks must be initialized, Data bus clear #define _ADDRH(high) _DATA_OP(); _DATA_SET(high); _AHL_CLK(); _DATA_IP(); - #define _ADDR(hword) ADDRL(hword); _ADDRH(hword<<8) + #define _ADDR(hword) ADDRL(hword); _ADDRH(hword>>8) #endif //STM_ADAPTER @@ -730,6 +730,7 @@ #define _ADDRL(low) GPIOA->PORT = low //clocks must be initialized, Data bus clear #define _ADDRH(high) _DATA_OP(); _DATA_SET(high); _AHL_CLK(); _DATA_IP(); + #define _ADDR(hword) ADDRL(hword); _ADDRH(hword>>8) #endif //AVR_KAZZO diff --git a/firmware/source/usb.c b/firmware/source/usb.c index dfa3a54..2c94ccd 100644 --- a/firmware/source/usb.c +++ b/firmware/source/usb.c @@ -76,6 +76,16 @@ uint16_t usbFunctionSetup(uint8_t data[8]) { rv[RETURN_LEN_IDX] = 0; //reset to zero, number of bytes in return data (excluding ERR & LEN) //now it's the opcode's responsiblity to update these values //rv[RETURN_DATA] start of return data + + //fake some return data + rv[RETURN_ERR_IDX] = SUCCESS; + rv[RETURN_LEN_IDX] = 3; + rv[RETURN_DATA] = 0xAA; + rv[RETURN_DATA+1] = 0xDE; + rv[RETURN_DATA+2] = 0xAD; + rv[RETURN_DATA+3] = 0xBE; + rv[RETURN_DATA+4] = 0xEF; + rv[RETURN_DATA+5] = 0xCC; /* (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data * block and return the length of the data in 'usbFunctionSetup()'. The driver @@ -105,7 +115,7 @@ uint16_t usbFunctionSetup(uint8_t data[8]) { switch(spacket->bRequest) { case DICT_PINPORT: - switch (spacket->opcode) { + //Turn on LED #ifdef STM_CORE RCC->AHBENR |= (IOP_LED_EN); @@ -113,6 +123,10 @@ uint16_t usbFunctionSetup(uint8_t data[8]) { // PCb_OP_EN(LEDbank, LED); // PCb_SET_HI(LEDbank, LED); CTL_IP_PU(LEDbank, LED); + + + + switch (spacket->opcode) { /* case PP_OPCODE_ONLY_MIN ... PP_OPCODE_ONLY_MAX: rv[RV_ERR_IDX] = pinport_opcode_only( spacket->opcode ); @@ -133,9 +147,9 @@ uint16_t usbFunctionSetup(uint8_t data[8]) { rv[RV_ERR_IDX] = pinport_opcode_8b_return( spacket->opcode, &rv[RV_DATA0_IDX]); rlen ++; break; - */ default: //pinport opcode min/max definition error rv[RETURN_ERR_IDX] = ERR_BAD_PP_OP_MINMAX; + */ } break; //end of PINPORT diff --git a/host/scripts/inlretro.lua b/host/scripts/inlretro.lua index 603cab7..7266527 100644 --- a/host/scripts/inlretro.lua +++ b/host/scripts/inlretro.lua @@ -20,7 +20,9 @@ function create_dict_tables( table, file ) local count = 0 local define = 0 local define_end = 0 - local comment = 0 + local slash = 0 + local line + local comment for line in io.lines() do count = count + 1 --search for multiline comment opening, starting at index 1, plain search = true @@ -31,26 +33,32 @@ function create_dict_tables( table, file ) end define, define_end = string.find( line, "#define") if define then - comment = string.find(line, "//") + slash = string.find(line, "//") --check for comment following define, if present cut out comment - if comment and (comment>define) then - line = string.sub(line, 1, comment-1 ) + if slash and (slash>define) then + --store comment contents for later parsing + comment = string.sub(line, slash, -1) + line = string.sub(line, 1, slash-1 ) end --check for comment prior to define, skip if present - if not (comment and (comment0) printf("Returning device handle to main\n"); + + //set this module's pointer to handle so it can be referenced by lua + lua_usb_handle = handle; + return handle; error: @@ -247,7 +253,7 @@ void close_usb(libusb_context *context, libusb_device_handle *handle) * ERROR if unable to transfer USBtransfer's wLength number of bytes * prints libusb_error if there was usb problem */ -int usb_transfer( USBtransfer *transfer ) +int usb_vendor_transfer( USBtransfer *transfer ) { check( transfer->wLength <= MAX_VUSB, "Can't transfer more than %d bytes!", MAX_VUSB); //check( transfer->wLength <= MAX_VUSB_LONGXFR, "Can't transfer more than %d bytes!", MAX_VUSB_LONGXFR); @@ -263,13 +269,13 @@ int usb_transfer( USBtransfer *transfer ) //but all avr operations should have a return value success/error code //one way to control whether those retrun values are read back is endpoint direction - uint16_t wValue = transfer->wValueMSB; - wValue = wValue << 8; - wValue |= transfer->wValueLSB; + uint16_t wValue = transfer->wValue; +// wValue = wValue << 8; +// wValue |= transfer->wValueLSB; - uint16_t wIndex = transfer->wIndexMSB; - wIndex = wIndex << 8; - wIndex |= transfer->wIndexLSB; + uint16_t wIndex = transfer->wIndex; +// wIndex = wIndex << 8; +// wIndex |= transfer->wIndexLSB; debug("reqtype h: %x \n", ( LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | transfer->endpoint)); debug("request h: %x d: %d", transfer->request, transfer->request); @@ -304,3 +310,80 @@ int usb_transfer( USBtransfer *transfer ) error: return -1; } + + +// initialize usb transfer based on args passed in from lua and transfer setup packet over USB + +int lua_usb_vend_xfr (lua_State *L) { +/* +typedef struct USBtransfer { + libusb_device_handle *handle; + uint8_t endpoint; + uint8_t request; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; + unsigned char *data; +} USBtransfer; +*/ + uint8_t data_buff[MAX_VUSB]; + int i; + const char *lua_out_string; + int xfr_count = 0; //return count + int rv = 0; //number of return values + + USBtransfer usb_xfr; + usb_xfr.handle = lua_usb_handle; + usb_xfr.endpoint = luaL_checknumber(L, 1); /* get endpoint argument */ + usb_xfr.request = luaL_checknumber(L, 2); /* get request argument */ + usb_xfr.wValue = luaL_checknumber(L, 3); /* get wValue argument */ + usb_xfr.wIndex = luaL_checknumber(L, 4); /* get wIndex argument */ + usb_xfr.wLength = luaL_checknumber(L, 5); /* get wLength argument */ + check( (usb_xfr.wLength <= MAX_VUSB), "Can't transfer more than %d bytes!", MAX_VUSB); + if ( usb_xfr.endpoint == LIBUSB_ENDPOINT_OUT ) { + //OUT transfer sending data to device + lua_out_string = luaL_checkstring(L, 6); /* get data argument */ + //2 rules for lua strings in C: don't pop it, and don't modify it!!! + //copy lua string over to data buffer + for( i=0; i #include +#include "lua/lua.h" +#include "lua/lauxlib.h" +#include "lua/lualib.h" + //list of included dictionaries for defining request, wValue, and wIndex fields #include "shared_dictionaries.h" @@ -89,10 +93,8 @@ typedef struct USBtransfer { libusb_device_handle *handle; uint8_t endpoint; uint8_t request; - uint8_t wValueMSB; - uint8_t wValueLSB; - uint8_t wIndexMSB; - uint8_t wIndexLSB; + uint16_t wValue; + uint16_t wIndex; uint16_t wLength; unsigned char *data; } USBtransfer; @@ -142,6 +144,7 @@ void close_usb(libusb_context *context, libusb_device_handle *handle); // LIBUSB_ERROR_PIPE if the control request was not supported by the device // LIBUSB_ERROR_NO_DEVICE if the device has been disconnected // another LIBUSB_ERROR code on other failures -int usb_transfer( USBtransfer *transfer ); +int usb_vendor_transfer( USBtransfer *transfer ); +int lua_usb_vend_xfr(lua_State *L); #endif diff --git a/shared/shared_dict_pinport.h b/shared/shared_dict_pinport.h index 30e6922..481687e 100644 --- a/shared/shared_dict_pinport.h +++ b/shared/shared_dict_pinport.h @@ -157,8 +157,8 @@ #define LED_IP 65 #define LED_OP 66 -#define LED_OFF 67 -#define LED_ON 68 +#define LED_OFF 67 // test outRL = 6whee! +#define LED_ON 68 // test outRL = 5whee! #define IRQ_IP 69 #define IRQ_OP 70 diff --git a/shared/shared_errors.h b/shared/shared_errors.h index cf0d4a1..0e22bbb 100644 --- a/shared/shared_errors.h +++ b/shared/shared_errors.h @@ -4,10 +4,8 @@ #define SUCCESS 0 #define GEN_FAIL 0xFF -/* -#define FALSE 0 -#define NILL 0 -*/ +//#define FALSE 0 +//#define NILL 0 //greater than 128 are possible avr return codes #define ERR_UNKN_DICTIONARY 128 @@ -20,43 +18,41 @@ #define ERR_BAD_OPER_OP_MINMAX 135 -#define ERR_UNKN_PP_OPCODE_ONLY 140 -#define ERR_UNKN_PP_OPCODE_8BOP 141 -#define ERR_UNKN_PP_OPCODE_16BOP 142 -#define ERR_UNKN_PP_OPCODE_24BOP 143 -#define ERR_UNKN_PP_OPCODE_8BRV 144 +//#define ERR_UNKN_PP_OPCODE_ONLY 140 +//#define ERR_UNKN_PP_OPCODE_8BOP 141 +//#define ERR_UNKN_PP_OPCODE_16BOP 142 +//#define ERR_UNKN_PP_OPCODE_24BOP 143 +//#define ERR_UNKN_PP_OPCODE_8BRV 144 +// +//#define ERR_UNKN_IO_OPCODE_ONLY 150 +//#define ERR_UNKN_IO_OPCODE_RTN 151 +// +//#define ERR_UNKN_NES_OPCODE_24BOP 160 +//#define ERR_UNKN_NES_OPCODE_16BOP_8BRV 161 +// +////reserved libusb erro 165 +// +//#define ERR_UNKN_SNES_OPCODE_24BOP 170 +//#define ERR_UNKN_SNES_OPCODE_24BOP_8BRV 171 +// +//#define ERR_UNKN_BUFF_OPCODE_NRV 180 +//#define ERR_UNKN_BUFF_OPCODE_RV 181 +//#define ERR_UNKN_BUFF_OPCODE_BUFN_NRV 182 +// +//#define ERR_BUFF_ALLOC_RANGE 190 +//#define ERR_BUFF_STATUS_ALREADY_ALLOC 191 +//#define ERR_BUFF_ID_ALREADY_ALLOC 192 +//#define ERR_BUFF_RAW_ALREADY_ALLOC 193 +//#define ERR_BUFF_ALLOC_SIZE_ZERO 194 +//#define ERR_BUFF_UNSUP_MEM_TYPE 195 +// +//#define ERR_OUT_CURLDBUF_STATUS 200 +//#define ERR_OUT_CURLDBUF_TO_SMALL 201 +// +//#define ERR_UNKN_OPER_OPCODE_NRV 210 +//#define ERR_UNKN_OPER_OPCODE_RV 211 +// +////max error number 255 -/* -#define ERR_UNKN_IO_OPCODE_ONLY 150 -#define ERR_UNKN_IO_OPCODE_RTN 151 - -#define ERR_UNKN_NES_OPCODE_24BOP 160 -#define ERR_UNKN_NES_OPCODE_16BOP_8BRV 161 - -//reserved libusb erro 165 - -#define ERR_UNKN_SNES_OPCODE_24BOP 170 -#define ERR_UNKN_SNES_OPCODE_24BOP_8BRV 171 - -#define ERR_UNKN_BUFF_OPCODE_NRV 180 -#define ERR_UNKN_BUFF_OPCODE_RV 181 -#define ERR_UNKN_BUFF_OPCODE_BUFN_NRV 182 - -#define ERR_BUFF_ALLOC_RANGE 190 -#define ERR_BUFF_STATUS_ALREADY_ALLOC 191 -#define ERR_BUFF_ID_ALREADY_ALLOC 192 -#define ERR_BUFF_RAW_ALREADY_ALLOC 193 -#define ERR_BUFF_ALLOC_SIZE_ZERO 194 -#define ERR_BUFF_UNSUP_MEM_TYPE 195 - -#define ERR_OUT_CURLDBUF_STATUS 200 -#define ERR_OUT_CURLDBUF_TO_SMALL 201 - -#define ERR_UNKN_OPER_OPCODE_NRV 210 -#define ERR_UNKN_OPER_OPCODE_RV 211 - -//max error number 255 - -*/ #endif