Sunday, September 17, 2006

Factor code to upload to the S1 MP3 Player

Futher to my post on using Factor to program the S1 MP3 Player, I got some basic code working to upload Z80 code and execute it.

It requires updates to the usb code in my Factor repository:
darcs get http://www.bluishcoder.co.nz/repos/factor

I've created a darcs repository to host the code. It's best to get the repository from within the Factor 'contrib' subdirectory so you can use the standard module system to load the code:
darcs get http://www.bluishcoder.co.nz/repos/s1mp3

libusb
must also be installed. Once all that's done you can test things out by plugging in the MP3 player and navigating to the 'Update Firmware' menu option. The MP3 player then sits waiting to receive code.

The following will load a Z80 assembled file called 'test.bin', upload it to the MP3 player and start executing it:
"contrib/s1mp3" require
USE: s1mp3
"test.bin" s1mp3-run-file


The Z80 file must be assembled with an origin of 0x3400. For a quick test I've included in the repository Carlos Aguilar's LCD autodetect program. The binary is in 'detect/detect.bin' and can e sent to the player with:
"contrib/s1mp3/detect/detect.bin" s1mp3-run-file

This should find details about the LCD display on the device and display it. Hopefully this code will work on all supported Factor platforms as long as libusb is available. This covers Windows, Linux and Mac OS X that I know of.

If you get errors due to 'permissions' it may be because your user does not have permission to access the USB device directly. On Linux this can be fixed by running Factor as root. A better option though is to set up hotplug or udev to change the permission on the device when it is plugged in so you can access it.

Categories: ,

9 Comments:

Anonymous Anonymous said...

I'm trying this on FreeBSD 6.1 (with libusb-0.1.12_2 from the ports tree). After the USE: and "s1mp3-run-file" step I get "Operating system signal 11". I'm running as root. Any ideas?

6:41 AM  
Blogger Chris Double said...

Did use put a filename before the 's1mp3-run-file'? So:

USE: s1mp3
"/somewhere/here/myfile.bin" s1mp3-run-file

If that doesn't work let me know and I'll see what I can do.

9:22 AM  
Anonymous Anonymous said...

I used the file from the example, thus:

"contrib/s1mp3" require
USE: s1mp3
"contrib/s1mp3/detect/detect.bin" s1mp3-run-file

That's when I get the "signal 11"

3:49 PM  
Blogger Chris Double said...

Ahh, found what I think is the problem. My error detection is not so good :)

When you plug your MP3 player in, navigate to the the 'System', 'Update Firmware' menu item. Then do the 's1mp3-run-file' commands, etc.

When you use that option on the MP3 player it actually changes the USB device type that the PC detects. Without it, I try to find the device number and it fails - I don't handle this correctly and it gives the 'signal 11' which means stack underflow in Factor.

The 'Update Firmware' changes the USB type and waits for code to be sent to it which it executes.

You might get a 'permission denied' or similar error after doing that - if using Linux that means you need to either run Factor as root, using sudo, or change the permissions on the USB device when it is plugged in.

Hope that helps.

4:57 PM  
Blogger rog. said...

(not as anonymous any more)

I'm pretty sure I did the 'Update Firmware' menu item as you describe in your original post. I'll try again later. In the mean time, here is the trace from the f session in case there is an error message I haven't relayed:

Loading factor.image... relocating... done
Factor 0.85 on freebsd/x86
scratchpad "contrib/s1mp3" require
Loading module contrib/s1mp3...
Loading resource:contrib/s1mp3/load.factor
Loading module contrib/usb...
Loading resource:contrib/usb/load.factor
Loading resource:contrib/usb/usb-common.factor
Loading resource:contrib/usb/usb-unix.factor
Loading resource:contrib/usb/usb.factor
Loading resource:contrib/usb/usb.facts
Loading resource:contrib/s1mp3/s1mp3.factor
Compiling CMD-WRITE-FWARE
Compiling usb_bus-location
Compiling usb_release_interface
Compiling define-packed-field
Compiling usb_interrupt_read
Compiling usb_device_descriptor-bDeviceSubClass
Compiling usb_config_descriptor-iConfiguration
Compiling set-usb_device_descriptor-iProduct
Compiling seq>byte-array
Compiling s1mp3-init
Compiling init
Compiling usb_init
Compiling usb_find_busses
Compiling usb_find_devices
Compiling usb_device_descriptor-nth
Compiling usb_device-nth
Compiling check-error
Compiling usb_strerror
Compiling CMD-INIT
Compiling set-CMD-undef0x04
Compiling usb_control_msg
Compiling set-usb_device-next
Compiling set-usb_device_descriptor-idProduct
Compiling usb_close
Compiling product-id-matches?
Compiling usb_device-descriptor
Compiling usb_device_descriptor-idProduct
Compiling CMD-MAX-TX
Compiling CMD-undef0x1b
Compiling set-usb_config_descriptor-wTotalLength
Compiling CMD-WRITE-BREC
Compiling byte-array-length
Compiling _CMD-SHUTDOWN
Compiling usb_device_descriptor-iManufacturer
Compiling usb_interrupt_write
Compiling send-command-data
Compiling CMD-mode
Compiling CMD-MODE-READ
Compiling usb_bulk_write
Compiling CMD-id
Compiling CMD-unkwn0x17
Compiling usb_device_descriptor-bLength
Compiling set-usb_device-bus
Compiling set-CMD-cmd
Compiling usb_device_descriptor-idVendor
Compiling set-usb_bus-next
Compiling set-usb_config_descriptor-bmAttributes
Compiling s1mp3-send-command
Compiling send-command-header
Compiling set-CMD-id
Compiling send-command-trailer
Compiling CMD-BUFSIZE
Compiling usb_bulk_read
Compiling usb_config_descriptor-bDescriptorType
Compiling _CMD_RWX_BREC
Compiling set-usb_bus-root_dev
Compiling set-usb_device_descriptor-bDescriptorType
Compiling set-usb_config_descriptor-extra
Compiling usb_config_descriptor-bConfigurationValue
Compiling usb_set_debug
Compiling usb_config_descriptor-interface
Compiling usb_resetep
Compiling usb_device-filename
Compiling _CMD-INIT
Compiling set-usb_device_descriptor-bDeviceSubClass
Compiling set-usb_bus-location
Compiling usb_device-dev
Compiling CMD-GET-SYSINFO
Compiling _CMD_WRITE_FWARE
Compiling usb_get_busses
Compiling usb_device-children
Compiling set-usb_device-devnum
Compiling usb_bus-devices
Compiling usb_device_descriptor-bNumConfigurations
Compiling CMD-undef0x0e
Compiling s1mp3-run-code
Compiling set-CMD-mode
Compiling set-CMD-size
Compiling set-CMD-unkwn0x13
Compiling set-CMD-unkwn0x17
Compiling set-CMD-undef0x1b
Compiling usb_device_descriptor-bDeviceClass
Compiling usb_claim_interface
Compiling usb_config_descriptor-nth
Compiling set-usb_device_descriptor-iManufacturer
Compiling CMD-MODE-WRITE
Compiling set-usb_device_descriptor-bLength
Compiling set-usb_device_descriptor-idVendor
Compiling CMD-size
Compiling set-usb_config_descriptor-bDescriptorType
Compiling s1mp3-open
Literal value expected
Nesting: V{ s1mp3-open find-devices bus-each }
Compiling usb_reset
Compiling usb_device_descriptor-bcdDevice;
Compiling set-usb_config_descriptor-bConfigurationValue
Compiling CMD-ENTER-FMODE
Compiling usb_device-prev
Compiling set-usb_config_descriptor-iConfiguration
Compiling vendor-id-matches?
Compiling set-usb_config_descriptor-interface
Compiling set-usb_device-filename
Compiling PACKED-FIELD:
Compiling usb_config_descriptor-bLength
Compiling usb_device_descriptor-bMaxPacketSize0
Compiling usb_open
Compiling usb_get_descriptor
Compiling set-usb_device-dev
Compiling usb_config_descriptor-bNumInterfaces
Compiling CMD-unkwn0x13
Compiling s1mp3-close
Compiling set-usb_device-children
Compiling set-usb_bus-devices
Compiling set-CMD-undef0x0e
Compiling device-each
Literal value expected
Nesting: V{ device-each }
Compiling CMD-READ-FLASH
Compiling usb_config_descriptor-MaxPower
Compiling usb_device_descriptor-iSerialNumber
Compiling set-usb_device_descriptor-bNumConfigurations
Compiling usb_device_descriptor-bcdUSB
Compiling usb_bus-prev
Compiling set-usb_device_descriptor-bDeviceClass
Compiling usb_device-config
Compiling usb_set_altinterface
Compiling _CMD_READ_SYSINFO
Compiling s1mp3-run-file
The word stream-copy does not have a stack effect
Nesting: V{ s1mp3-run-file load-file contents }
Compiling usb_bus-root_dev
Compiling display-devices
The word bus-each does not have a stack effect
Nesting: V{ display-devices }
Compiling usb_get_descriptor_by_endpoint
Compiling CMD-HEADER-ID
Compiling usb_get_driver_np
FFI: Undefined symbol "usb_get_driver_np"
Compiling bus-each
The word bus-each does not have a stack effect
Nesting: f
Compiling usb_bus-nth
Compiling usb_device_descriptor-bDeviceProtocol
Compiling usb_config_descriptor-extralen
Compiling is-device?
Compiling usb_bus-dirname
Compiling _CMD-ENTER-FMODE
Compiling usb_device-num_children
Compiling s1mp3-upload
Compiling s1mp3-upload-cmd
Compiling CMD-READ-BREC
Compiling set-usb_device_descriptor-bcdDevice;
Compiling usb_set_configuration
Compiling usb_get_string_simple
Compiling set-usb_device-prev
Compiling CMD-undef0x0d
Compiling _CMD_READ_FLASH
Compiling CMD-nth
Compiling set-usb_config_descriptor-bLength
Compiling find-devices
The word find-devices does not have a stack effect
Nesting: f
Compiling set-usb_device_descriptor-bMaxPacketSize0
Compiling usb_device_descriptor-iProduct
Compiling set-usb_config_descriptor-bNumInterfaces
Compiling usb_device
Compiling usb_device_descriptor-bDescriptorType
Compiling CMD-undef0x04
Compiling usb_device-next
Compiling set-usb_bus-prev
Compiling set-usb_config_descriptor-MaxPower
Compiling set-usb_device-config
Compiling usb_config_descriptor-wTotalLength
Compiling load-file
The word load-file does not have a stack effect
Nesting: f
Compiling usb_clear_halt
Compiling set-usb_device-descriptor
Compiling CMD-SHUTDOWN
Compiling set-usb_device_descriptor-bcdUSB
Compiling usb_config_descriptor-bmAttributes
Compiling set-usb_device_descriptor-bDeviceProtocol
Compiling set-usb_device_descriptor-iSerialNumber
Compiling usb_get_string
Compiling usb_device-bus
Compiling set-usb_config_descriptor-extralen
Compiling usb_bus-next
Compiling set-usb_bus-dirname
Compiling set-usb_device-num_children
Compiling CMD-cmd
Compiling usb_config_descriptor-extra
Compiling usb_device-devnum
Compiling set-CMD-undef0x0d
scratchpad USE: s1mp3
scratchpad "contrib/s1mp3/detect/detect.bin" s1mp3-run-file
Operating system signal 11

2:53 AM  
Blogger Chris Double said...

It's possible I'm missing something to cause it not to work. With the MP3 player plugged in can you list the output of:

"contrib/s1mp3" require
USE: s1mp3
USE: usb
s1mp3-init
display-devices

This will list the product and vendor id's of all plugged in USB devices. Can you also do the 'display-devices' call with the MP3 player set to 'Firmware Upgrade'. It should show a different number.

Does FreeBSD have 'lsusb'? If so, doing lsusb will give the same information as the above.

11:46 PM  
Anonymous rog said...

ran that, as root, and got this:

scratchpad USE: s1mp3
scratchpad USE: usb
scratchpad s1mp3-init
scratchpad display-devices
/dev/usb0 -
Operating system signal 11

Debugger commands:

:help - documentation for this error
:s - data stack at exception time
:r - retain stack at exception time
:c - call stack at exception time
:get ( var -- value ) accesses variables at time of the error


FreeBSD's equivalent to lsusb is "usbdevs", the output of which is shown below:

Controller /dev/usb0:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
port 1 powered
port 2 powered
Controller /dev/usb1:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
port 1 powered
port 2 powered
Controller /dev/usb2:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
port 1 powered
port 2 powered
Controller /dev/usb3:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
port 1 powered
port 2 powered
Controller /dev/usb4:
addr 1: high speed, self powered, config 1, EHCI root hub(0x0000), Intel(0x0000), rev 1.00
port 1 addr 2: high speed, self powered, config 1, product 0x2504(0x2504), vendor 0x0424(0x0424), rev 0.01

that 0x2504 device is the s1mp3 player

2:13 AM  
Anonymous rog said...

Here are the forth stacks, if they are of any use:

scratchpad :c
[
terpri " " write dup USB_DEVICE-FILENAME write " - " write
dup usb_device-descriptor usb_device_descriptor-bLength
number>string write " - " write dup usb_device-descriptor
usb_device_descriptor-idVendor >hex write " - " write
usb_device-descriptor usb_device_descriptor-idProduct >hex
write
]
[ over >r pick >r CALL r> r> ]
[ # 2KEEP >r usb_device-next r> over # # if ]
[
dup usb_bus-dirname write " - " write usb_bus-devices #
DEVICE-EACH
terpri
]
[ over >r pick >r CALL r> r> ]
[ # 2KEEP >r usb_bus-next r> over # # if ]
[ >c drop CALL c> drop ]
[ LISTEN listener ]
[ # clone >n CALL ndrop ]
[ boot run-user-init "shell" get "shells" lookup EXECUTE 0 exit
]
scratchpad :s
{ kernel-error 4 11 f }
{ kernel-error 4 11 f }

2:15 AM  
Anonymous rog said...

sorry -- blew that usbdevs output line. This is better:

Controller /dev/usb3:
addr 1: full speed, self powered, config 1, UHCI root hub(0x0000), Intel(0x0000), rev 1.00
port 1 addr 2: full speed, power 100 mA, config 1, USB 2.0(FS) FLASH DISK(0x1100), vendor 0x10d6(0x10d6), rev 1.00
port 2 powered

it's the FLASH DISK on port 1 addr 2

3:08 AM  

Post a Comment

<< Home