Skip to content

Commit

Permalink
Voltage scaling finishes. Set address fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonetech committed Apr 28, 2015
1 parent 78c58d1 commit 4bc1e15
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ msp430f%.nim: /usr/msp430/include/msp430f%.h msp430_c2nim.ed
c2nim -o:$@ $<
ed -s $@ < msp430_c2nim.ed

blink: blink.nim msp430usb.nim msp430.nim
blink: blink.nim msp430usb.nim msp430.nim toutf16

toutf16: toutf16.nim
nim c $@

all: blink

Expand Down
10 changes: 6 additions & 4 deletions blink.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ proc main =
PA.IES[8] = 1
PA.IE[8] = 1
PA.IFG = 0
usbinit()
enable_interrupts()

while true:
Expand All @@ -50,12 +51,13 @@ proc main =
of Off:
PJ.OUT[3] = 0

proc testvol =
PA.OUT = 5
PA.OUT = 5
#proc testvol =
# PA.OUT = 5
# PA.OUT = 5

main()
testvol()
#testvol()


# My first project is a PC joystick to USB adapter based on an
# Olimexino-5510 board (a MSP430 board resembling an Arduino Pro).
Expand Down
8 changes: 7 additions & 1 deletion msp430.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ declGPIO(PJ)
proc setVCoreUp*(level: range[0..3]) =
## Set core voltage level - only shift one level at a time!
# Open PMM registers for write access
PMMCTL0_H = 0xa5
PMMCTL0_H = 0xa5u8
# Disable the automatic resets while altering voltages
PMMRIE = 0
# Clear flags first - because they don't do it on their own
PMMIFG = 0
# Make sure no flags are set for iterative sequences
while (PMMIFG and (SVSMHDLYIFG or SVSMLDLYIFG)) != 0: continue
# Set SVS/SVM high side new level
Expand All @@ -82,6 +86,8 @@ proc setVCoreUp*(level: range[0..3]) =
while (PMMIFG and SVMLVLRIFG) == 0: continue
# Set SVS/SVM low side to new level
SVSMLCTL = cast[uint16](SVSLE or SVSLRVL0 * level or SVMLE or SVSMLRRL0 * level)
# Reenable power failure resets
PMMRIE = SVSHPE or SVSLPE
# Lock PMM registers for write access
PMMCTL0_H = 0x00

Expand Down
64 changes: 52 additions & 12 deletions msp430usb.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import msp430, unsigned
import msp430, unsigned, tables
# Encodings requires local OS support. So it must be spawned as
# a separate staticExec of a native program. Yay.
#import encodings
#import marshal

{.pragma: usbram, codegenDecl: "$# __attribute__((section(\"usbram\"))) $#".}

type
index = int8
Request* = enum
GetStatus = 0
ClearFeature = 1
Expand All @@ -16,26 +21,53 @@ type
SetInterface = 11
SynchFrame = 12
DescriptorType* = enum
Device = 1
Configuration = 2
String = 3
Interface = 4
Endpoint = 5
DeviceQualifier = 6
OtherSpeedConfiguration = 7
InterfacePower = 8
dtDevice = 1
dtConfiguration = 2
dtString = 3
dtInterface = 4
dtEndpoint = 5
dtDeviceQualifier = 6
dtOtherSpeedConfiguration = 7
dtInterfacePower = 8
UsbDeviceRequest* = tuple
bmRequestType: uint8
bRequest: uint8
wValue: uint16
wIndex: uint16
wLength: uint16
UsbDeviceDescriptor* = tuple
bLength: uint8
bDescriptorType: uint8
bcdUSB: uint16
bDeviceClass: uint8
bDeviceSubClass: uint8
bDeviceProtocol: uint8
bMaxPacketSize0: uint8
idVendor: uint16
idProduct: uint16
bcdDevice: uint16
iManufacturer: index
iProduct: index
iSerialNumber: index
bNumConfigurations: int8

var
UsbSetupBlock {.extern:"USBSUBLK".} : UsbDeviceRequest
ep0inbuf {.extern:"USBIEP0BUF".} : array[0..7, uint8]
ep0outbuf {.extern:"USBOEP0BUF".} : array[0..7, uint8]
#ep1buf {.usbram.} : array[0..7, int8] # = [1i8,2,3,4,5,6,7,8]
newaddress : uint8 = 0x80

const
serial = staticExec("./toutf16", "MSP430 Joystick")
deviceDescriptor0 = (
bLength: 18, bDescriptorType: dtDevice, bcdUSB: 0x0310,
bDeviceClass: 0, bDeviceSubClass: 0, bDeviceProtocol: 0,
bMaxPacketSize0: 8, idVendor: 0x16c0, idProduct: 0x27dc, # V-USB HID Joystick, must set serial number text
bcdDevice: 0, iManufacturer: 0, iProduct: 0, iSerialNumber: 0, bNumConfigurations: 0)
descriptors = {(ord(dtDevice) shl 8) or 0: deviceDescriptor0,
#(ord(dtString) shl 8) or 0: serial,
}.toTable

ISR:
proc USB_UBM() =
Expand All @@ -47,7 +79,11 @@ ISR:
case UsbSetupBlock.bmRequestType
of 0x80: # standard device in
case UsbSetupBlock.bRequest
of 6: # get descriptor
of ord(SetAddress):
newaddress = UsbSetupBlock.wValue and 0xff
UsbIepCnt0 = 0
of ord(GetDescriptor):
#let desc = ref descriptors[UsbSetupBlock.wValue]
case UsbSetupBlock.wValue
of 0x0100: # device descriptor 0
discard # TODO: produce descriptor
Expand All @@ -69,7 +105,10 @@ ISR:
#of USBVECINT_PWR_VBUSOn:
#of USBVECINT_PWR_VBUSOff:
#of USBVECINT_USB_TIMESTAMP:
of USBVECINT_INPUT_ENDPOINT0: discard
of USBVECINT_INPUT_ENDPOINT0:
if (newaddress and 0x80) == 0:
UsbFunAdr = newaddress
newaddress = newaddress or 0x80
of USBVECINT_OUTPUT_ENDPOINT0: discard
#of USBVECINT_RSTR: # reset - might want to auto-reset with FRSTE
#of USBVECINT_SUSR: # suspend
Expand Down Expand Up @@ -107,6 +146,7 @@ proc usbinit*() =
UsbPllCtl = cast[uint16](UPLLEN or UPFDEN)
UsbPllDivB = cast[uint16](UsbPll_setClk_val_4p0)
UsbPhyCtl = PUSEL # enable USB function of USB phy

# TODO: Wait 2ms and check PLL lock?
# Enable USB module and pull up resistor
USBCNF = USBCNF or cast[uint16](PUR_EN or USB_EN)
Expand All @@ -122,4 +162,4 @@ proc usbinit*() =
USBOEPCNT_0 = 0
# Enable interrupts for endpoint 0
USBIEPIE = 1
USBOEPIE = 1
USBOEPIE = 1
8 changes: 8 additions & 0 deletions toutf16.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import encodings

proc main () =
let orig = stdin.readAll()
let result = convert(orig, "utf-16", "utf-8")
stdout.write(result)

main ()

0 comments on commit 4bc1e15

Please sign in to comment.