From 25de3c5f2dacb86c593f89c7c0e4bee80d89fe64 Mon Sep 17 00:00:00 2001 From: Donald Delmar Davis Date: Sun, 21 Apr 2013 18:50:35 -0700 Subject: Make a quick reset script. --- Makefile | 4 +- libmaple/MinSysex.c | 15 ++++- support/scripts/sysexreset.py | 152 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 5 deletions(-) create mode 100755 support/scripts/sysexreset.py diff --git a/Makefile b/Makefile index 2eac32b..f2a19d5 100644 --- a/Makefile +++ b/Makefile @@ -101,10 +101,10 @@ include $(SRCROOT)/build-targets.mk # USB ID for DFU upload -- FIXME: do something smarter with this BOARD_USB_VENDOR_ID := 1EAF BOARD_USB_PRODUCT_ID := 0003 -UPLOAD_ram := $(SUPPORT_PATH)/scripts/reset.py && \ +UPLOAD_ram := $(SUPPORT_PATH)/scripts/sysexreset.py && \ sleep 1 && \ $(DFU) -a0 -d $(BOARD_USB_VENDOR_ID):$(BOARD_USB_PRODUCT_ID) -D $(BUILD_PATH)/$(BOARD).bin -R -UPLOAD_flash := $(SUPPORT_PATH)/scripts/reset.py && \ +UPLOAD_flash := $(SUPPORT_PATH)/scripts/sysexreset.py && \ sleep 1 && \ $(DFU) -a1 -d $(BOARD_USB_VENDOR_ID):$(BOARD_USB_PRODUCT_ID) -D $(BUILD_PATH)/$(BOARD).bin -R diff --git a/libmaple/MinSysex.c b/libmaple/MinSysex.c index b7ff71c..62f9327 100644 --- a/libmaple/MinSysex.c +++ b/libmaple/MinSysex.c @@ -4,7 +4,7 @@ // // Created by Donald D Davis on 4/11/13. // Copyright (c) 2013 Suspect Devices. All rights reserved. -// +// Modified BSD Liscense /* 0xF0 SysEx 0x7E Non-Realtime @@ -72,7 +72,7 @@ const uint8 standardIDResponse[]={ }; //#define STANDARD_ID_RESPONSE_LENGTH (sizeof(standardIDResponse)) -typedef enum {NOT_IN_SYSEX=0,IN_SYSEX,COULD_BE_MY_SYSEX,YUP_ITS_MY_SYSEX,ITS_NOT_MY_SYSEX,I_GOT_MY_SYSEX} sysexStates; +typedef enum {NOT_IN_SYSEX=0,COULD_BE_MY_SYSEX,YUP_ITS_MY_SYSEX,ITS_NOT_MY_SYSEX} sysexStates; volatile uint8 sysexBuffer[MAX_SYSEX_SIZE]; volatile sysexStates sysexState; volatile int sysexFinger=0; @@ -97,7 +97,13 @@ static void wait_reset(void) { nvic_sys_reset(); } - +/* -----------------------------------------------------------------------------dealWithItQuickly() + * Note: at this point we have established that the sysex belongs to us. + * So we need to respond to any generic requests like information requests. + * We also need to handle requests which are meant for us. At the moment this is just the + * reset request. + * + */ void dealWithItQuickly(){ switch (sysexBuffer[1]) { case USYSEX_NON_REAL_TIME: @@ -144,6 +150,9 @@ void dealWithItQuickly(){ ;//turn the led on? } +/* -----------------------------------------------------------------------------LglSysexHandler() + * The idea here is to identify which Sysex's belong to us and deal with them. + */ void LglSysexHandler(uint8 *midiBufferRx,uint32 *rx_offset,uint32 *n_unread_bytes) { MIDI_EVENT_PACKET_t * midiPackets = (MIDI_EVENT_PACKET_t *) (midiBufferRx+(*rx_offset)); uint8 nPackets=((*n_unread_bytes)-(*rx_offset))/4; diff --git a/support/scripts/sysexreset.py b/support/scripts/sysexreset.py new file mode 100755 index 0000000..2a57b4c --- /dev/null +++ b/support/scripts/sysexreset.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python + + +'''---------------------------------------------------------------- sysexreset.py + + this resets the maple midi using sysex. + + + Copyright (c) 2011 Donald Delmar Davis, Suspect Devices + + Permission is hereby granted, free of charge, to any person obtaining a copy of this + software and associated documentation files (the "Software"), to deal in the Software + without restriction, including without limitation the rights to use, copy, modify, + merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to the following + conditions: + + The above copyright notice and this permission notice shall be included in all copies + or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +''' +import rtmidi_python as rtmidi +import time + +# this stuff should probably go in a constants file. +SYSEX_BEGIN =0xf0 +SYSEX_NON_REAL_TIME =0x7e +SYSEX_NON_REAL =0x7f +SYSEX_ALL_CHANNELS =0x7f +SYSEX_GENERAL_INFO =0x06 +SYSEX_IDENTITY_REQUEST =0x01 +SYSEX_IDENTITY_REPLY =0x02 +SYSEX_END_SYSEX =0xF7 + + +LEAFLABS_MMA_VENDOR_1 =0x7D +LEAFLABS_MMA_VENDOR_2 =0x1E +LEAFLABS_MMA_VENDOR_3 =0x4F +LGL_RESET_CMD =0x1e + + +askfortheids=[SYSEX_BEGIN,SYSEX_NON_REAL_TIME,SYSEX_ALL_CHANNELS, + SYSEX_GENERAL_INFO,SYSEX_IDENTITY_REQUEST,SYSEX_END_SYSEX] + +resetthedevice=[SYSEX_BEGIN, + LEAFLABS_MMA_VENDOR_1, + LEAFLABS_MMA_VENDOR_2, + LEAFLABS_MMA_VENDOR_3, + 0x0a, #device/channel is currently ignored... + LGL_RESET_CMD, + SYSEX_END_SYSEX] + +we_are_done_here=False + +''' + this is our callback routine for midi messages. + It handles sysexes sent to it and the universal sysex id response. +''' +def handleOurSysexes(message, time_stamp): + global we_are_done_here + #print "?"+str(message)+"?" + if (len(message)>4 and message[0]==SYSEX_BEGIN): #if its a sysex message + if (message[1]==125 and message[2]==51 and message[3]==51): + print "DBG:",decodeHexLine(message) + elif (message[1]==SYSEX_NON_REAL_TIME and message[3]==SYSEX_GENERAL_INFO + and message[4]==SYSEX_IDENTITY_REPLY ): + print 'MANUFACTURER ID: 0x%02X 0x%02X 0x%02X' % ( + message[5],message[6],message[7]) + print "FAMILY ID : 0x%02X%02x" % ( + message[8], message[9]) + print "PRODUCT ID : 0x%02X%02x" % ( + message[10], message[11]) + print "VERSION : 0x%02X%02x%02X%02x" % ( + message[12],message[13],message[14],message[15]) + if (message[15] == ord('!')) : + print "YAY LETS GET LOADED!!!" + midi_out.send_message(resetthedevice) + we_are_done_here=True #exit() + + + +#:llaaaattd1..dncs +def decodeHexLine(payload): +# print payload + #length = int(chr(payload[5])+chr(payload[6]),16) + #address = int(chr(payload[7])+chr(payload[8])+chr(payload[9])+chr(payload[10]),16) + #type = int(chr(payload[11])+chr(payload[12]),16) + #checksum = int(chr(payload[len(payload)-2])+chr(payload[len(payload)-2]),16) + high=None + decoded='' + for byte in payload[13:len(payload)-3] : + if high is None: + high=byte + else: + pair=chr(high)+chr(byte) + decoded=decoded+chr(int(pair,16)) + high=None + return decoded + +# main code begins here. + +midi_in = rtmidi.MidiIn() +midi_out = rtmidi.MidiOut() +print midi_in.ports +midi_in.callback = handleOurSysexes +midi_in.ignore_types(False,False,False) +midi_in.open_port(0) +midi_out.open_port(0) +time.sleep(2) +midi_out.send_message(askfortheids) + + +while not we_are_done_here: + time.sleep(1) + #print "requesting id" + #midi_out.send_message(askfortheids) + + +""" + http://www.blitter.com/~russtopia/MIDI/~jglatt/tech/midispec.htm + + 0xF0 SysEx + 0x7E Non-Realtime + 0x7F The SysEx channel. Could be from 0x00 to 0x7F. Here we set it to "disregard channel". + 0x06 Sub-ID -- General Information + 0x01 Sub-ID2 -- Identity Request + 0xF7 End of SysEx + Here is the Identity Reply message: + 0xF0 SysEx + 0x7E Non-Realtime + 0x7F The SysEx channel. Could be from 0x00 to 0x7F. + Here we set it to "disregard channel". + 0x06 Sub-ID -- General Information + 0x02 Sub-ID2 -- Identity Reply + 0xID Manufacturer's ID + 0xf1 The f1 and f2 bytes make up the family code. Each + 0xf2 manufacturer assigns different family codes to his products. + 0xp1 The p1 and p2 bytes make up the model number. Each + 0xp2 manufacturer assigns different model numbers to his products. + 0xv1 The v1, v2, v3 and v4 bytes make up the version number. + 0xv2 + 0xv3 + 0xv4 + 0xF7 End of SysEx +""" \ No newline at end of file -- cgit v1.2.3