Date:2010-08-24 00:56:02 (9 years 22 days ago)
Author:Werner Almesberger
Commit:5514c336a503d1b7febfaeec78b71d956c75d928
Message:Completed the basic counter firmware. Now it just has to work ...

- cntr/fw/cntr/Makefile, cntr/fw/cntr/descr.c, cntr/fw/cntr/ep0.c: enabled
the usual USB stack
- cntr/fw/cntr/cntr.c: initialize and poll USB
- cntr/fw/cntr/cntr.c, cntr/fw/cntr/ep0.c: added counter application
- cntr/fw/common/config.h (PLATFORM_SETUP): enable the crossbar
Files: cntr/fw/cntr/Makefile (1 diff)
cntr/fw/cntr/cntr.c (3 diffs)
cntr/fw/cntr/descr.c (1 diff)
cntr/fw/cntr/ep0.c (1 diff)
cntr/fw/common/config.h (1 diff)

Change Details

cntr/fw/cntr/Makefile
1212
1313
1414MAIN = cntr
15OBJS = $(MAIN) # usb descr version ep0
15OBJS = $(MAIN) usb descr version ep0
1616
1717F32XBASE = ../../../../f32xbase
1818
cntr/fw/cntr/cntr.c
1111 */
1212
1313
14#include <stdint.h>
15
1416#include "regs.h"
1517#include "io.h"
1618#include "usb.h"
...... 
1820#include "version.h"
1921
2022
23/*
24 * Free-running 32 bit counter. The lower two bytes are from hardware Timer 0.
25 * The upper two bytes are maintained by software. At the maximum input clock
26 * frequency of 6 MHz, it wraps around every 11.9 minutes, leaving the host
27 * plenty of time to read it.
28 */
29
30uint8_t cntr[4];
31
32
2133static void init_io(void)
2234{
2335    /*
...... 
4658}
4759
4860
61static void setup_counter(void)
62{
63    TCON = TR0; /* enable Timer 0 */
64    TMOD = C_T0 | T0M0; /* clock from T0, mode 1: 16 bit counter */
65
66    XBR1 |= T0E; /* route T0 to port */
67
68    P0SKIP = 0x7f; /* assign T0 to P0_7 */
69}
70
71
72static void read_counter(void)
73{
74    uint8_t th;
75
76    th = TH0;
77    while (1) {
78        cntr[0] = TL0;
79        if (th == TH0)
80            break;
81        th = TH0;
82    }
83    if (th < cntr[1]) {
84        cntr[2]++;
85        if (!cntr[2])
86            cntr[3]++;
87    }
88    cntr[1] = th;
89}
90
91
4992void main(void)
5093{
5194    init_io();
95    setup_counter();
5296
53// usb_init();
54// ep0_init();
97    usb_init();
98    ep0_init();
5599
56100    while (1) {
57// usb_poll();
101        read_counter();
102        usb_poll();
58103    }
59104}
cntr/fw/cntr/descr.c
1/*
2 * atspi/descr.c - USB descriptors
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include "usb.h"
15
16#include "config.h"
17
18
19/*
20 * Device descriptor
21 */
22
23const uint8_t device_descriptor[18] = {
24    18, /* bLength */
25    USB_DT_DEVICE, /* bDescriptorType */
26    LE(0x200), /* bcdUSB */
27    USB_CLASS_VENDOR_SPEC, /* bDeviceClass */
28    0x00, /* bDeviceSubClass */
29    0x00, /* bDeviceProtocol */
30    EP0_SIZE, /* bMaxPacketSize */
31    LE(USB_VENDOR), /* idVendor */
32    LE(USB_PRODUCT), /* idProduct */
33    LE(0x0001), /* bcdDevice */
34    0, /* iManufacturer */
35    0, /* iProduct */
36    0, /* iSerialNumber */
37    1 /* bNumConfigurations */
38};
39
40
41/*
42 * Our configuration
43 *
44 * We're always bus-powered.
45 */
46
47const uint8_t config_descriptor[] = {
48    9, /* bLength */
49    USB_DT_CONFIG, /* bDescriptorType */
50    LE(9+9), /* wTotalLength */
51    1, /* bNumInterfaces */
52    1, /* bConfigurationValue (> 0 !) */
53    0, /* iConfiguration */
54    USB_ATTR_BUS_POWERED, /* bmAttributes */
55    15, /* bMaxPower */
56
57    /* Interface #0 */
58
59    9, /* bLength */
60    USB_DT_INTERFACE, /* bDescriptorType */
61    0, /* bInterfaceNumber */
62    0, /* bAlternateSetting */
63    0, /* bNumEndpoints */
64    USB_CLASS_VENDOR_SPEC, /* bInterfaceClass */
65    0, /* bInterfaceSubClass */
66    0, /* bInterfaceProtocol */
67    0, /* iInterface */
68};
cntr/fw/cntr/ep0.c
1/*
2 * cntr/ep0.c - EP0 extension protocol
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#ifndef NULL
17#define NULL 0
18#endif
19
20#include "regs.h"
21#include "usb.h"
22#include "cntr/ep0.h"
23#include "version.h"
24
25
26#define debug(...)
27#define error(...)
28
29
30/*
31 * SDCC 2.8.0 had a number of code generation bugs that appeared in the big
32 * switch statement of my_setup. SDCC_FORCE_UPDATE forced the value of the
33 * "size" variable to be written to memory. This work-around doesn't seem
34 * to be necessary with 2.9.0, but we keep it around, just in case.
35 *
36 * Unfortunately, the setup->bRequest garbling bug is still with us. Without
37 * the evaluation forced with SDCC_FORCE_EVAL, sdcc gets confused about the
38 * value of setup->bRequest and then rejects all SETUP requests.
39 */
40
41#define SDCC_FORCE_EVAL(type, value) \
42    do { \
43    static volatile type foo; \
44    foo = value; \
45    } while (0)
46
47#define SDCC_FORCE_UPDATE(type, var) \
48    do { \
49    volatile type foo; \
50    foo = var; \
51    var = foo; \
52    } while (0)
53
54
55extern uint8_t cntr[8];
56
57static const uint8_t id[] = { EP0CNTR_MAJOR, EP0CNTR_MINOR, HW_TYPE };
58static __xdata uint8_t buf[128];
59
60
61#define BUILD_OFFSET 7 /* '#' plus "65535" plus ' ' */
62
63
64static __bit my_setup(struct setup_request *setup) __reentrant
65{
66    unsigned tmp;
67    uint8_t size, i;
68
69    switch (setup->bmRequestType | setup->bRequest << 8) {
70    case CNTR_FROM_DEV(CNTR_ID):
71        debug("CNTR_ID\n");
72        if (setup->wLength > 3)
73            return 0;
74        usb_send(&ep0, id, setup->wLength, NULL, NULL);
75        return 1;
76    case CNTR_FROM_DEV(CNTR_BUILD):
77        debug("CNTR_BUILD\n");
78        tmp = build_number;
79        for (i = BUILD_OFFSET-2; tmp; i--) {
80            buf[i] = (tmp % 10)+'0';
81            tmp /= 10;
82        }
83        buf[i] = '#';
84        buf[BUILD_OFFSET-1] = ' ';
85        for (size = 0; build_date[size]; size++)
86            buf[BUILD_OFFSET+size] = build_date[size];
87        size += BUILD_OFFSET-i+1;
88        SDCC_FORCE_EVAL(uint8_t, setup->bRequest);
89        if (size > setup->wLength)
90            return 0;
91        usb_send(&ep0, buf+i, size, NULL, NULL);
92        return 1;
93
94    case CNTR_TO_DEV(CNTR_RESET):
95        debug("CNTR_RESET\n");
96        RSTSRC = SWRSF;
97        while (1);
98
99    case CNTR_FROM_DEV(CNTR_READ):
100        debug("CNTR_READ\n");
101        buf[0] = cntr[0];
102        buf[1] = cntr[1];
103        buf[2] = cntr[2];
104        buf[3] = cntr[3];
105        usb_send(&ep0, buf, 4, NULL, NULL);
106        return 1;
107
108    default:
109        error("Unrecognized SETUP: 0x%02x 0x%02x ...\n",
110            setup->bmRequestType, setup->bRequest);
111        return 0;
112    }
113}
114
115
116void ep0_init(void)
117{
118    user_setup = my_setup;
119}
cntr/fw/common/config.h
2525 * This turns on the LED, to indicate that we're in the boot loader.
2626 */
2727
28#define PLATFORM_SETUP \
29    LED_MODE |= 1 << LED_BIT;
28#define PLATFORM_SETUP \
29    LED_MODE |= 1 << LED_BIT; \
30    XBR1 = XBARE;
3031
3132
3233/*

Archive Download the corresponding diff file



interactive