Skip to content

Instantly share code, notes, and snippets.

@kmcallister
Created July 9, 2014 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmcallister/87078a18d96d11692db4 to your computer and use it in GitHub Desktop.
Save kmcallister/87078a18d96d11692db4 to your computer and use it in GitHub Desktop.
controlling three LEDs with Digispark
#!/bin/bash -xe
gcc -O3 -Wall -o host host.c -I/usr/include/libusb-1.0 -lusb-1.0
avr-gcc -O3 -Wall -DF_CPU=16500000L -I usb/ -o stacklights stacklights.c usb/usbdrvasm.S usb/usbdrv.c usb/osccal.c -mmcu=attiny85
avr-size stacklights
avr-objcopy -j .text -j .data -O ihex stacklights stacklights.hex
~/proj/electronics/micronucleus/commandline/micronucleus --run stacklights.hex
# usb/ contains V-USB files copied from DigisparkUSB:
#
# asmcommon.inc
# osccal.c
# osccal.h
# usbconfig.h
# usbdrvasm165.inc
# usbdrvasm.S
# usbdrv.c
# usbdrv.h
# usbportability.h
#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>
static const int CONTROL_REQUEST_TYPE_OUT = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
static const int HID_SET_REPORT = 0x09;
static const int HID_REPORT_TYPE_OUTPUT = 0x02;
static const int INTERFACE_NUMBER = 0;
static const int TIMEOUT_MS = 5000;
#define DIE do { fprintf(stderr, "Died at line %d\n", __LINE__); abort(); } while (0)
#define CHECK(_e) do { if (! (_e)) DIE; } while (0)
int main(int argc, char *argv[]) {
struct libusb_device_handle *devh = NULL;
int val;
CHECK(argc >= 2);
val = atoi(argv[1]);
CHECK(0 <= libusb_init(NULL));
devh = libusb_open_device_with_vid_pid(NULL, 0x16c0, 0x05df);
CHECK(devh != NULL);
libusb_detach_kernel_driver(devh, INTERFACE_NUMBER);
CHECK(0 <= libusb_claim_interface(devh, INTERFACE_NUMBER));
CHECK(0 <= libusb_control_transfer(
devh,
CONTROL_REQUEST_TYPE_OUT,
HID_SET_REPORT,
(HID_REPORT_TYPE_OUTPUT<<8) | (val & 0x7),
INTERFACE_NUMBER,
NULL,
0,
TIMEOUT_MS));
libusb_release_interface(devh, 0);
libusb_close(devh);
libusb_exit(NULL);
return 0;
}
#include <avr/io.h>
#include <util/delay.h>
#include "usbdrv.h"
typedef unsigned char byte;
const PROGMEM char usbHidReportDescriptor[22] = {
0x06, 0x00, 0xff, // USAGE_PAGE (Generic Desktop)
0x09, 0x01, // USAGE (Vendor Usage 1)
0xa1, 0x01, // COLLECTION (Application)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x09, 0x00, // USAGE (Undefined)
0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf)
0xc0 // END_COLLECTION
};
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (usbRequest_t*) data;
if (((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)
&& (rq->bRequest == USBRQ_HID_SET_REPORT)) {
PORTB = (PORTB & ~7) | (rq->wValue.bytes[0] & 7);
}
return 0;
}
int main() {
cli();
usbInit();
usbDeviceDisconnect();
_delay_ms(250);
usbDeviceConnect();
DDRB |= 7;
sei();
for (;;) {
usbPoll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment