Check-in [cd62352ec5]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: user: pine tags: trunk EDITED .fossil-settings/ignore-glob EDITED doc/geda/k20-fm.sch ADDED soft/firmware/Makefile ADDED soft/firmware/incrcnt.tcl ADDED soft/firmware/lib/sys/debug.h ADDED soft/firmware/lib/sys/stimer.c ADDED soft/firmware/lib/sys/stimer.d ADDED soft/firmware/lib/sys/stimer.h ADDED soft/firmware/lib/sys/sys.h ADDED soft/firmware/lib/sys/types.h ADDED soft/firmware/lib/sys/uart.c ADDED soft/firmware/lib/sys/uart.d ADDED soft/firmware/lib/sys/uart.h ADDED soft/firmware/src/fusb/fusb.c ADDED soft/firmware/src/fusb/fusb.d ADDED soft/firmware/src/fusb/fusb.h ADDED soft/firmware/src/fusb/fusb_proto.c ADDED soft/firmware/src/fusb/fusb_proto.d ADDED soft/firmware/src/fusb/fusb_proto.h ADDED soft/firmware/src/fusb/fusb_reg.c ADDED soft/firmware/src/fusb/fusb_reg.d ADDED soft/firmware/src/fusb/fusb_reg.h ADDED soft/firmware/src/led.c ADDED soft/firmware/src/led.d ADDED soft/firmware/src/led.h ADDED soft/firmware/src/main.c ADDED soft/firmware/src/main.d ADDED soft/firmware/src/main_.c ADDED soft/firmware/src/u2s.c ADDED soft/firmware/src/u2s.d ADDED soft/firmware/src/u2s.h ADDED soft/firmware/src/u2s_shared.h ADDED soft/firmware/src/util.c ADDED soft/firmware/src/util.d ADDED soft/firmware/src/util.h ADDED soft/firmware/src/version.h ADDED soft/libk20/Makefile ADDED soft/libk20/k20tool ADDED soft/libk20/k20tool.c
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:cd62352ec52151509d0f04c345eb1c3a8b2fd778
User & Date: pine 2016-05-08 07:50:00
Context
2016-05-10
12:28
user: pine tags: trunk ADDED soft/JK20/build.xml ADDED soft/JK20/src/Config.java ADDED soft/JK20/src/DPrint.java ADDED soft/JK20/src/JK20.java EDITED soft/firmware/src/u2s.c check-in: 0e5172d98f user: pine tags: trunk
2016-05-08
07:50
user: pine tags: trunk EDITED .fossil-settings/ignore-glob EDITED doc/geda/k20-fm.sch ADDED soft/firmware/Makefile ADDED soft/firmware/incrcnt.tcl ADDED soft/firmware/lib/sys/debug.h ADDED soft/firmware/lib/sys/stimer.c ADDED soft/firmware/lib/sys/stimer.d ADDED soft/firmware/lib/sys/stimer.h ADDED soft/firmware/lib/sys/sys.h ADDED soft/firmware/lib/sys/types.h ADDED soft/firmware/lib/sys/uart.c ADDED soft/firmware/lib/sys/uart.d ADDED soft/firmware/lib/sys/uart.h ADDED soft/firmware/src/fusb/fusb.c ADDED soft/firmware/src/fusb/fusb.d ADDED soft/firmware/src/fusb/fusb.h ADDED soft/firmware/src/fusb/fusb_proto.c ADDED soft/firmware/src/fusb/fusb_proto.d ADDED soft/firmware/src/fusb/fusb_proto.h ADDED soft/firmware/src/fusb/fusb_reg.c ADDED soft/firmware/src/fusb/fusb_reg.d ADDED soft/firmware/src/fusb/fusb_reg.h ADDED soft/firmware/src/led.c ADDED soft/firmware/src/led.d ADDED soft/firmware/src/led.h ADDED soft/firmware/src/main.c ADDED soft/firmware/src/main.d ADDED soft/firmware/src/main_.c ADDED soft/firmware/src/u2s.c ADDED soft/firmware/src/u2s.d ADDED soft/firmware/src/u2s.h ADDED soft/firmware/src/u2s_shared.h ADDED soft/firmware/src/util.c ADDED soft/firmware/src/util.d ADDED soft/firmware/src/util.h ADDED soft/firmware/src/version.h ADDED soft/libk20/Makefile ADDED soft/libk20/k20tool ADDED soft/libk20/k20tool.c check-in: cd62352ec5 user: pine tags: trunk
07:04
user: pine tags: trunk EDITED .fossil-settings/ignore-glob EDITED doc/geda/board.pcb EDITED doc/geda/k20-fm.sch check-in: 6cf07fb0dd user: pine tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to .fossil-settings/ignore-glob.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
29
30
31
32
33
34
35
36
37
*session.vim
*geda/*.cmd
*geda/*.pcb-
*geda/*.sch~
*geda/*.pcb*~
*geda/*new.pcb
*geda/*/packages
*geda/*/#*
*geda/toprint/*
*.bak*
*tags
*.o
*.hex
*.bin
*.map
................................................................................
soft/firmware/*.hxl
soft/firmware/*.obj
soft/firmware/*.rlf
soft/firmware/*.sdb
soft/firmware/*.sym
soft/firmware/*.p1
soft/firmware/*.pre
soft/libk20/testlib





<

|
|







 







|

1
2
3
4

5
6
7
8
9
10
11
12
13
14
..
28
29
30
31
32
33
34
35
36
*session.vim
*geda/*.cmd
*geda/*.pcb-
*geda/*.sch~

*geda/*new.pcb
*geda/*~*
*geda/*#*
*geda/toprint/*
*.bak*
*tags
*.o
*.hex
*.bin
*.map
................................................................................
soft/firmware/*.hxl
soft/firmware/*.obj
soft/firmware/*.rlf
soft/firmware/*.sdb
soft/firmware/*.sym
soft/firmware/*.p1
soft/firmware/*.pre
soft/libk20/k20tool

Changes to doc/geda/k20-fm.sch.

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
T 49900 64850 5 10 0 0 0 0 1
device=RESISTOR
T 49700 64800 5 10 1 1 0 0 1
refdes=R2
T 49500 64500 5 10 0 0 0 0 1
footprint=1206
T 50000 64300 5 10 1 1 180 0 1
value=1.5k
}
N 50400 64600 50800 64600 4
C 35100 57000 1 270 0 led-1.sym
{
T 35700 56200 5 10 0 0 270 0 1
device=LED
T 35700 57000 5 10 1 1 270 0 1







|







302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
T 49900 64850 5 10 0 0 0 0 1
device=RESISTOR
T 49700 64800 5 10 1 1 0 0 1
refdes=R2
T 49500 64500 5 10 0 0 0 0 1
footprint=1206
T 50000 64300 5 10 1 1 180 0 1
value=1.5k *
}
N 50400 64600 50800 64600 4
C 35100 57000 1 270 0 led-1.sym
{
T 35700 56200 5 10 0 0 270 0 1
device=LED
T 35700 57000 5 10 1 1 270 0 1

Added soft/firmware/Makefile.



















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
# Makefile for for XC8 compiller
#

export PATH+=:$(XC8_TOOLCHAIN_PATH)

TARGET      = main.hex
PK2CMD_CHIP = PIC18F14K50
XC8_CHIP    = 18f14k50

PK2CMD_FLAGS += -A3.3 # voltage

CC = xc8

CFLAGS = --chip=$(XC8_CHIP)
CFLAGS += --MSGDISABLE=1273
CFLAGS += -Ilib/sys
CFLAGS += -DTARGET
CFLAGS += --asmlist

SRC_PATH    = src
LIBSYS_PATH = lib/sys

OBJS  = $(SRC_PATH)/main.p1
OBJS += $(SRC_PATH)/led.p1
OBJS += $(SRC_PATH)/led.p1
OBJS += $(SRC_PATH)/util.p1
OBJS += $(SRC_PATH)/u2s.p1
OBJS += $(SRC_PATH)/fusb/fusb.p1
OBJS += $(SRC_PATH)/fusb/fusb_reg.p1
OBJS += $(SRC_PATH)/fusb/fusb_proto.p1

# =================================

CFLAGS+= -I$(LIBSYS_PATH)

LIBSYS_OBJS = $(LIBSYS_PATH)/stimer.p1
LIBSYS_OBJS += $(LIBSYS_PATH)/uart.p1

# =================================

$(TARGET): $(OBJS) $(LIBSYS_OBJS)
	$(CC) $(CFLAGS) -Mmain.map --asmlist $^

%.p1 : %.c
	$(CC) $(CFLAGS) -O$*.p1 --pass1 $*.c

$(OBJS): src/*.h src/fusb/*.h

DIRS = $(SRC_PATH)
DIRS += $(SRC_PATH)/fusb
DIRS += $(LIBSYS_PATH)
DIRS += ./

CLEAREXT = funclist *.hex *.ihx *.lnk *.lst *.rel *.o *.rst *.sym *.cod *.mem *.as *.d *.obj *.p1 *.pre *.sdb *.hxl *.rlf *.cof *.map *.cmf

clean:
	rm -f $(OBJS) $(LIBSYS_OBJS) $(TARGET) *.cod *.lst
	for i in $(DIRS); do \
	    for ext in $(CLEAREXT); do \
		rm -f $$i/$$ext; \
	    done \
	done

.PHONY: cc_help
cc_help:
	$(CC) --help

.PHONY: prog read starte startpk2 halt

prog: $(TARGET)
	pk2cmd -P$(PK2CMD_CHIP) $(PK2CMD_FLAGS) -J -M -F$(TARGET)
	@./incrcnt.tcl

read:
	pk2cmd -P$(PK2CMD_CHIP) $(PK2CMD_FLAGS) -J -GF./out.hex
	
starte:
	pk2cmd -P$(PK2CMD_CHIP) $(PK2CMD_FLAGS) -R

startpk2:
	pk2cmd -P$(PK2CMD_CHIP) $(PK2CMD_FLAGS) -T -R

halt:
	pk2cmd -P$(PK2CMD_CHIP)




Added soft/firmware/incrcnt.tcl.



















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/sh
#\
exec tclsh "$0" "$@"

set MAXCHARS 10

set fd [open progcnt a+]
seek $fd 0 start

set cnt [string trim [read $fd]]
seek $fd 0 start

if {[string length $cnt] == 0} {
    set cnt 0
}

incr cnt
puts "\r\n\033\[1;37mprogram cycle = $cnt \033\[0m\r\n"

while {[string length $cnt] < $MAXCHARS} {append cnt { }}

puts -nonewline $fd $cnt

close $fd

Added soft/firmware/lib/sys/debug.h.







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef DEBUG_H
#define DEBUG_H

#include <uart.h>

#define USE_DEBUG

#ifdef USE_DEBUG
    #define debug_str(s) _debug_str((uint8*)s)
    #define debug_hex(addr, len) _debug_hex(addr, len)
    #define debug_char(c) putchar(c)
#else
    #define debug_str(s)
    #define debug_hex(addr, len)
    #define debug_char(c)
#endif

#endif

Added soft/firmware/lib/sys/stimer.c.



























































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//#include <pic16/pic18fregs.h>
#include <xc.h>
#include "stimer.h"

#define T0CON_PSA    (1 << 3)
#define T0CON_T0SE   (1 << 4)
#define T0CON_T0CS   (1 << 5)
#define T0CON_T08BIT (1 << 6)
#define T0CON_TMR0ON (1 << 7)


volatile uint16 jiffies;

/*
 * setup TIMER0 for system tick
 *
 *
 */
void stimer_init()
{

    /*
     * Fosc = 12 MHz
     *
     * we use PLL, so 
     * Fclk = Fosc * 4
     *
     *
     * 1/64 prescaler
     * 8-bit
     * internal instruction cycle clock
     *
     * TMR0 = 256 - ((F/4/64)/1000)
     */
    T0CON = 0x05 | T0CON_T08BIT;
    TMR0L = T0VAL;

    INTCONbits.GIEH = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.TMR0IE = 1;

    T0CON |= T0CON_TMR0ON;

    jiffies = 0;
}


/*
 *
 */
void stimer_isr()
{
    if (INTCONbits.TMR0IE && INTCONbits.TMR0IF)
    {
        INTCONbits.TMR0IF = 0;
        TMR0L = T0VAL;
        jiffies ++;
    }
}


/*
 * set current timer to value of system jiffies
 */
void stimer_set(uint16 *ct)
{
    uint16 j;

    di();
    j = jiffies;
    ei();

    *ct = j;
}

/*
 * calculate difference between current timer and system jiffies
 */
uint16 stimer_delta(uint16 ct)
{
    uint16 j;

    di();
    j = jiffies;
    ei();

    if (j >= ct)
        return (j - ct);
    else
        return (65535 - ct + j + 1);
}


Added soft/firmware/lib/sys/stimer.d.



>
1
 lib/sys/stimer.d  lib/sys/stimer.p1:  lib/sys/stimer.c lib/sys/types.h lib/sys/sys.h lib/sys/stimer.h

Added soft/firmware/lib/sys/stimer.h.







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SYSTIMER_H
#define SYSTIMER_H

#include <types.h>
#include <sys.h>

#define T0PSC    64       /* prescaler */
#define T0RANGE  1000     /* ms */
#define T0VAL    (256 - (CCLK/4/T0PSC/T0RANGE))

extern volatile uint16 jiffies;

void stimer_init();
void stimer_isr();
void stimer_set(uint16 *ct);
uint16 stimer_delta(uint16 ct);

#endif

Added soft/firmware/lib/sys/sys.h.



























>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef SYS_H
#define SYS_H

#define FCLK     12000000
#define PLL      4
#define CCLK     (FCLK * PLL)


//#define di()  INTCONbits.GIE = 0;
//#define ei()  INTCONbits.GIE = 1;

#endif

Added soft/firmware/lib/sys/types.h.

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TYPES_H
#define TYPES_H

typedef signed char         int8;
typedef unsigned char       uint8;
typedef signed short        int16;
typedef unsigned short      uint16;
typedef signed long long    int32;
typedef unsigned long long  uint32;

#ifndef NULL
    #define NULL ((void*)0)
#endif

#endif

Added soft/firmware/lib/sys/uart.c.

















































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <xc.h>
#include "sys.h"
#include "uart.h"

#define UART_SPEED 57600
#define UART_DIVIDER (CCLK/(16*UART_SPEED) - 1)

struct uart_buf_t rxbuf;
struct uart_buf_t txbuf;

/*
 *
 */
void uart_init()
{
    TRISBbits.TRISB7 = 0;   /* TX */
    TRISBbits.TRISB5 = 1;   /* RX */
    TXSTAbits.BRGH = 1;
    /*
     * baudrate for BRGH = 1, asynchronous mode
     *
     * BAUD = Fosc/(16*(n+1))
     *
     * n = Fosc/(16*BAUD) - 1
     *
     * n = (7372800*4)/(16*57600) - 1 = 32
     *
     */
    SPBRG = (UART_DIVIDER & 0xff);
    SPBRGH = (UART_DIVIDER >> 8) & 0xff;

    RCSTAbits.SPEN = 1;     /* enable serial port */
    RCSTAbits.CREN = 1;     /* continues receive enable */
    TXSTAbits.TXEN = 1;     /* transmit enable */

    PIE1bits.RCIE = 1;

    rxbuf.c = 0;
    txbuf.c = 0;
    txbuf.p = 0;
}

/*
 *
 */
void uart_isr()
{
    uint8 ch;

    if (PIR1bits.RCIF)
    {
        ch = RCREG; /* clear RCIF flag */
        if (rxbuf.c < UARTBUF_SIZE)
            rxbuf.buf[rxbuf.c++] = ch;
    }

    if (PIR1bits.TXIF)
    {
        if (txbuf.c == 0)
        {
            PIE1bits.TXIE = 0;
        } else {
            TXREG = txbuf.buf[txbuf.p];
            txbuf.c--;
            txbuf.p++;
            if (txbuf.p >= UARTBUF_SIZE)
                txbuf.p = 0;
        }
    }

}

///*
// * initiate uart transmission
// */
//void uart_send()
//{
//    if (txbuf.c == 0)
//        return;
//
//    di();
//    TXREG = txbuf.buf[0];
//    txbuf.p = 1;
//    txbuf.c--;
//    ei();
//    PIE1bits.TXIE = 1;
//}


/*
 * simple putchar
 */
void putchar(uint8 ch)
{
    while (!PIR1bits.TXIF);
    TXREG = ch;
    uint8 p;
}

/*
 *
 */
void _debug_str(uint8 *s)
{
    while (*s != 0)
        putchar(*s++);
}

/*
 *
 */
void _debug_hex(void *h, uint8 len)
{
    uint8 *n;
    uint8 v;

    n = (uint8*)h + len - 1;

    while (len--)
    {
        v = (*n & 0xf0) >> 4;
        if (v < 10)
            putchar('0' + v);
        else
            putchar('a' - 10 + v);

        v = *n & 0x0f;
        if (v < 10)
            putchar('0' + v);
        else
            putchar('a' - 10 + v);

        n--;
    }
}

Added soft/firmware/lib/sys/uart.d.



>
1
 lib/sys/uart.d  lib/sys/uart.p1:  lib/sys/uart.c lib/sys/uart.h lib/sys/types.h lib/sys/sys.h

Added soft/firmware/lib/sys/uart.h.

























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef UART_H
#define UART_H

#include "types.h"


#define UARTBUF_SIZE 32

struct uart_buf_t {
    uint8 buf[UARTBUF_SIZE];
    uint8 c;
    uint8 p;
};

extern struct uart_buf_t rxbuf;
extern struct uart_buf_t txbuf;

void uart_init();
void uart_isr();
//void uart_send();


void _debug_str(uint8 *s);
void _debug_hex(void *h, uint8 len);
void putchar(uint8 ch);

#endif

Added soft/firmware/src/fusb/fusb.c.











































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * XXX TODO valid data toggle synchronization
 *
 */
#include <xc.h>
#include <debug.h>
#include "../util.h"
#include "../u2s.h"
#include "fusb.h"
#include "fusb_reg.h"
#include "fusb_proto.h"


//#define DEBUGUSB

#ifdef DEBUGUSB
    #define DEBUGUSB_STR(s)    debug_str(s)
    #define DEBUGUSB_CHAR(c)   debug_char(c)
    #define DEBUGUSB_HEX(h, l) debug_hex(h, l)
#else
    #define DEBUGUSB_STR(s)
    #define DEBUGUSB_CHAR(c)
    #define DEBUGUSB_HEX(h, l)
#endif


struct usbstate_t {
    /* info for IN transactions */
    uint8 pp; /* ping pong */
    struct {
        uint8 p;   /* pointer for IN packet */
        uint8 c;   /* counter for IN packet */
        uint8 *buf;/* pointer for data */
    } in[2];
};

static struct usbstate_t usbstate = {0};

#define SEND_DATA0  0
#define SEND_DATA1  1
#define SEND_STALL  1
static void fusb_send_control(uint8 dts, uint8 stall);
static void fusb_control_ep_process(uint8 dir);
static void fusb_ep_process(uint8 epn, uint8 dir, uint8 ppbi);
static void fusb_send(uint8 stall);

#define USTAT_DIR_OUT  0
#define USTAT_DIR_IN   4
#define USTAT_PP_ODD   2
#define USTAT_PP_EVEN  0


static uint8 setaddr = 0;

/*
 *
 */
void fusb_init()
{
    uint8 i;
    /*
     * Even/Odd ping-pong buffers enabled for Endpoints 1 to 15
     * Full-speed device
     * USB on-chip pull-up disabled
     * Eye pattern test disabled
     */
    UCFG = (0x03 << 0) | (1 << 2) | (1 << 4);
//    UCFG = (0x03 << 0) | (1 << 2);

    UIE = UIE_URSTIE  |
          UIE_ACTVIE  |
          UIE_TRNIE   |
          UIE_IDLEIE  |
          UIE_STALLIE;

    /*
     * setup control endpoint
     */
    bdtable.ep0bdout.BDCNT   = (EP0_BUFSIZE & 0xff);
    bdtable.ep0bdout.BDADDRL = ((uint16)&bdtable.ep0buf.out) & 0xff; /* XXX using of constant is better */
    bdtable.ep0bdout.BDADDRH = ((uint16)&bdtable.ep0buf.out) >> 8; /* XXX using of constant is better */
    bdtable.ep0bdout.BDSTAT  = ((EP0_BUFSIZE >> 8) & 0x03) | BDSTAT_CPU_SIE_UOWN;

    bdtable.ep0bdin.BDCNT   = 0;
    bdtable.ep0bdin.BDADDRL = ((uint16)&bdtable.ep0buf.in) & 0xff; /* XXX using of constant is better */
    bdtable.ep0bdin.BDADDRH = ((uint16)&bdtable.ep0buf.in) >> 8; /* XXX using of constant is better */
    bdtable.ep0bdin.BDSTAT  = ((EP0_BUFSIZE >> 8) & 0x03) | BDSTAT_CPU_BSTALL;

    /*
     * setup other endpoints
     */
    for (i = 0; i < 2; i++)
    {
        bdtable.ep1bdout[i].BDCNT   = (EP1_BUFSIZE & 0xff);
        bdtable.ep1bdout[i].BDADDRL = ((uint16)&bdtable.ep1buf.out[i]) & 0xff; /* XXX using of constant is better */
        bdtable.ep1bdout[i].BDADDRH = ((uint16)&bdtable.ep1buf.out[i]) >> 8; /* XXX using of constant is better */
        bdtable.ep1bdout[i].BDSTAT  = ((EP1_BUFSIZE >> 8) & 0x03) | BDSTAT_CPU_SIE_UOWN;

        bdtable.ep1bdin[i].BDCNT   = 0;
        bdtable.ep1bdin[i].BDADDRL = ((uint16)&bdtable.ep1buf.in[i]) & 0xff; /* XXX using of constant is better */
        bdtable.ep1bdin[i].BDADDRH = ((uint16)&bdtable.ep1buf.in[i]) >> 8; /* XXX using of constant is better */
        bdtable.ep1bdin[i].BDSTAT  = ((EP1_BUFSIZE >> 8) & 0x03) | BDSTAT_CPU_BSTALL | BDSTAT_CPU_DTS;
    }


    UEP0 = UEP_EPINEN | UEP_EPOUTEN | UEP_EPHSHK;
    UEP1 = UEP_EPINEN | UEP_EPOUTEN | UEP_EPHSHK | UEP_EPCONDIS;
    UEP2 = 0;
    UEP3 = 0;
    UEP4 = 0;
    UEP5 = 0;
    UEP6 = 0;
    UEP7 = 0;

    UEIE = 0xff;

    UCON = 0;
    UCONbits.USBEN = 1;

//    PIE2bits.USBIE = 1;
    usbstate.pp = 1;
}

/*
 *
 */
void fusb_process()
{
    uint8 ustat;
    uint8 endp;
//    uint8 dir;
//    uint8 ppbi;

    /* NOTE */
    if (!PIR2bits.USBIF)
        return;

    if (PIR2bits.USBIF)
    {
        PIR2bits.USBIF = 0;

        if (UIRbits.URSTIF)
        {
            UIRbits.URSTIF = 0;
            UADDR = 0;
            DEBUGUSB_CHAR('R'); 
        }
        if (UIRbits.UERRIF)
        {
            UIRbits.UERRIF = 0;
            DEBUGUSB_CHAR('E'); 

//            {
//                uint8 reg;
//
//                reg = UEIR;
//                DEBUGUSB_STR("\r\nUEIR   = ");DEBUGUSB_HEX(&reg, 1); DEBUGUSB_STR("\r\n");
//            }
        }

        if(UIRbits.ACTVIF && UIEbits.ACTVIE)
        {
            UCONbits.SUSPND = 0;
            while (UIRbits.ACTVIF)
            {
                UIRbits.ACTVIF = 0;
                DEBUGUSB_CHAR('A'); 
            }
        }

        if (UIEbits.TRNIE && UIRbits.TRNIF)
        {
            DEBUGUSB_CHAR('t');
            /* get number of endpoint */
            ustat = USTAT;
            endp  = (ustat >> 3) & 0x07;

            if (endp)
            {
                fusb_ep_process(endp, ustat & (1 << 2), ustat & (1 << 1));
//                reg = USTAT;
//                DEBUGUSB_STR("\r\nUSTAT   = ");DEBUGUSB_HEX(&reg, 1); DEBUGUSB_STR("\r\n");
//                util_printarray((uint8*)bdtable.ep0buf.out, bdtable.ep0bdout.BDCNT);

            } else {
                fusb_control_ep_process((USTAT >> 2) & 0x01);
            }

            UIRbits.TRNIF = 0;
            DEBUGUSB_CHAR('T');
            DEBUGUSB_CHAR('\r');
            DEBUGUSB_CHAR('\n');
        }
        if (UIRbits.IDLEIF)
        {
            UIRbits.IDLEIF = 0;
            DEBUGUSB_CHAR('I'); 
        }
        if (UIRbits.STALLIF)
        {
            UIRbits.STALLIF = 0;
            DEBUGUSB_CHAR('S'); 
        }
        if (UIRbits.SOFIF)
        {
            UIRbits.SOFIF = 0;
//            DEBUGUSB_CHAR('F'); 
        }
    }
}

/*
 * process control endpoint requests
 */
static void fusb_control_ep_process(uint8 dir)
{
    struct dev_req_t *dr;

    if (dir == 0)
    {
        /* OUT or SETUP token */

//        DEBUGUSB_CHAR('\n');
//        util_printarray((uint8*)bdtable.ep0buf.out, bdtable.ep0bdout.BDCNT);
//        DEBUGUSB_CHAR('\n');
        
        /* standard device request */
        dr = (struct dev_req_t*)bdtable.ep0buf.out;

        if (bdtable.ep0bdout.BDCNT)
        {
            if ((dr->bmRequestType == 0x80) && dr->bRequest == GET_DESCRIPTOR)
            {
                uint8 desc  = (dr->wValue >> 8);
                uint8 index = dr->wValue & 0xff;

                if (desc == DESC_TYPE_DEVICE)
                {
                    usbstate.in[0].p   = 0;
                    //usbstate.in[0].c   = sizeof(struct USB_Device_Descriptor);
                    usbstate.in[0].c   = 18;
                    usbstate.in[0].buf = (uint8*)&device_desc;
                    fusb_send_control(SEND_DATA1, 0);
                    DEBUGUSB_STR("gD");
                } else if (desc == DESC_TYPE_DEVICE_QUALIFIER) {
                    /* we operate only on full speed, send stall */
                    fusb_send_control(0, SEND_STALL);
                    DEBUGUSB_STR("gQ");
                } else if (desc == DESC_TYPE_CONFIGURATION) {

                        usbstate.in[0].p   = 0;
                        if (dr->wLength == 32)
                            usbstate.in[0].c   = 32;
                        else
                            usbstate.in[0].c   = 9;
                        usbstate.in[0].buf = (uint8*)configuration;
                        fusb_send_control(SEND_DATA1, 0);
                        DEBUGUSB_STR("gC");
                } else {
                    util_printarray((uint8*)bdtable.ep0buf.out, bdtable.ep0bdout.BDCNT);
                    DEBUGUSB_STR("gU");
                }

            } else if ((dr->bmRequestType == 0x00) && dr->bRequest == SET_ADDRESS) {
                DEBUGUSB_CHAR('A');
                usbstate.in[0].p   = 0;
                usbstate.in[0].c   = 0;
                fusb_send_control(SEND_DATA1, 0); 

                setaddr = dr->wValue & 0xff;
            } else if ((dr->bmRequestType == 0x00) && dr->bRequest == SET_CONFIGURATION) {
                /* TODO valid processing */
                DEBUGUSB_CHAR('s');
                usbstate.in[0].p   = 0;
                usbstate.in[0].c   = 0;
                usbstate.in[0].buf = NULL;
                fusb_send_control(SEND_DATA1, 0); 
            } else {
                DEBUGUSB_CHAR('h');
                util_printarray((uint8*)bdtable.ep0buf.out, bdtable.ep0bdout.BDCNT);
            }
        } else {
            DEBUGUSB_CHAR('0');
        }
        UCONbits.PKTDIS    = 0;
        bdtable.ep0bdout.BDCNT  = (EP0_BUFSIZE & 0xff);
        bdtable.ep0bdout.BDSTAT = BDSTAT_CPU_SIE_UOWN; /* XXX */
        DEBUGUSB_CHAR('o');
    } else {
        /* IN token */
        if (setaddr)
        {
            UADDR = setaddr;
            setaddr = 0;
        } else {
            fusb_send_control((bdtable.ep0bdin.BDSTAT & BDSTAT_CPU_DTS) ? SEND_DATA0 : SEND_DATA1, 0);
        }
        DEBUGUSB_CHAR('i');
    }
}

/*
 *
 */
static void fusb_send_control(uint8 dts, uint8 stall)
{
    uint8 c, p, *buf;

    if (stall)
    {
        bdtable.ep0bdin.BDCNT  = 0;
        bdtable.ep0bdin.BDSTAT = BDSTAT_CPU_DTS | BDSTAT_CPU_DTSEN | BDSTAT_CPU_BSTALL | BDSTAT_CPU_SIE_UOWN;
        return;
    }

    c = usbstate.in[0].c;
    if (c > EP0_BUFSIZE)
        c = EP0_BUFSIZE;

    p   = usbstate.in[0].p;

    if (c && usbstate.in[0].buf)
    {
        buf = &usbstate.in[0].buf[p];
        util_memcpy(bdtable.ep0buf.in, buf, c);
    }

    bdtable.ep0bdin.BDCNT  = c;
    if (dts)
    {
        bdtable.ep0bdin.BDSTAT = BDSTAT_CPU_DTS | BDSTAT_CPU_DTSEN | BDSTAT_CPU_SIE_UOWN;
    } else {
        bdtable.ep0bdin.BDSTAT = BDSTAT_CPU_DTSEN | BDSTAT_CPU_SIE_UOWN;
    }

    usbstate.in[0].c -= c;
    usbstate.in[0].p += c;

}

/*
 * process endpoint transactions
 *
 * ARGS
 *     epn    endpoint number
 *     dir    direction
 *     ppbi   ping pong BD pointer indicator bit
 *
 */
static void fusb_ep_process(uint8 epn, uint8 dir, uint8 ppbi)
{
    uint8 i;

    DEBUGUSB_STR("\r\n");
    DEBUGUSB_STR("epn  = ");DEBUGUSB_HEX(&epn, 1); DEBUGUSB_STR("\r\n");
    DEBUGUSB_STR("dir  = ");DEBUGUSB_HEX(&dir, 1); DEBUGUSB_STR("\r\n");
    DEBUGUSB_STR("ppbi = ");DEBUGUSB_HEX(&ppbi, 1); DEBUGUSB_STR("\r\n");

    /* NOTE only EP1 used */
    if (epn != 1)
        return;

    if (ppbi == USTAT_PP_ODD)
        i = 1;
    else
        i = 0;

    if (dir == USTAT_DIR_OUT)
    {
        u2s_receive((uint8*)bdtable.ep1buf.out[i], bdtable.ep1bdout[i].BDCNT);

        bdtable.ep1bdout[i].BDCNT  = (EP1_BUFSIZE & 0xff);
        bdtable.ep1bdout[i].BDSTAT = BDSTAT_CPU_SIE_UOWN;
    } else {
//        fusb_send(SEND_STALL);
    }
}


/*
 *
 */
static void fusb_send(uint8 stall)
{
    uint8 c, p, pp, *buf;

    c = usbstate.in[1].c;
    if (c > EP1_BUFSIZE)
        c = EP1_BUFSIZE;

    if (c == 0)
        return;


    if (stall)
    {
        bdtable.ep1bdin[pp].BDCNT  = 0;
        bdtable.ep1bdin[pp].BDSTAT = BDSTAT_CPU_DTS | BDSTAT_CPU_DTSEN | BDSTAT_CPU_BSTALL | BDSTAT_CPU_SIE_UOWN;
        return;
    }

    usbstate.pp ^= 0x01;
    pp = usbstate.pp & 0x01;

    p   = usbstate.in[1].p;

    if (c && usbstate.in[1].buf)
    {
        buf = &usbstate.in[1].buf[p];
        util_memcpy(bdtable.ep1buf.in[pp], buf, c);
    }

    bdtable.ep1bdin[pp].BDCNT  = c;

    if (bdtable.ep1bdin[pp ^ 0x01].BDSTAT & BDSTAT_CPU_DTS)
    {
        bdtable.ep1bdin[pp].BDSTAT = BDSTAT_CPU_DTSEN | BDSTAT_CPU_SIE_UOWN;
    } else {
        bdtable.ep1bdin[pp].BDSTAT = BDSTAT_CPU_DTS | BDSTAT_CPU_DTSEN | BDSTAT_CPU_SIE_UOWN;
    }

    usbstate.in[1].c -= c;
    usbstate.in[1].p += c;
}

/*
 *
 */
void fusb_send_wrap(uint8 *buf, uint8 len)
{
    DEBUGUSB_STR("send wrap\r\n");
    usbstate.in[1].p   = 0;
    usbstate.in[1].c   = len;
    usbstate.in[1].buf = buf;
    fusb_send(0); 
}

Added soft/firmware/src/fusb/fusb.d.



>
1
 src/fusb/fusb.d  src/fusb/fusb.p1:  src/fusb/fusb.c src/fusb/../util.h lib/sys/debug.h src/fusb/fusb_reg.h src/fusb/fusb_proto.h lib/sys/uart.h lib/sys/types.h src/fusb/../u2s.h src/fusb/fusb.h

Added soft/firmware/src/fusb/fusb.h.























>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
#ifndef FUSB_H
#define FUSB_H

#include <types.h>

void fusb_init();
void fusb_process();
void fusb_send_wrap(uint8 *buf, uint8 len);

#endif

Added soft/firmware/src/fusb/fusb_proto.c.























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "fusb_proto.h"
#include "fusb_reg.h"

#define VID  0xFFFF
#define PID  0x0001

const struct USB_Device_Descriptor device_desc = {
    18,                            /* Size of this descriptor in bytes */
    DESC_TYPE_DEVICE,              /* Device descriptor type */
    0x0200,                        /* USB Spec Release Number in BCD format */
    0xff,                          /* Class Code */
    0x00,                          /* Subclass code */
    0xff,                          /* Protocol code */
    EP0_BUFSIZE,                   /* Max packet size for EP0 */
    VID,                           /* Vendor ID */
    PID,                           /* Product ID */
    0x0100,                        /* Device release number in BCD format */
    0,                             /* Manufacturer string index */
    0,                             /* Product string index */
    0,                             /* Device serial number string index */
    1                              /* Number of possible configurations */
};

const uint8 configuration[] = {
    /* Configuration */
    0x09,                       /* Descriptor length (9 bytes)              */
    0x02,                       /* Descriptor type (Configuration)          */
    32, 0x00,                   /* Total data length (32 bytes)             */
    0x01,                       /* Interface supported (1)                  */
    0x01,                       /* Configuration value (1)                  */
    0x00,                       /* Index of string descriptor (None)        */
    0x80,                       /* Configuration (no self powered )         */
    250,                        /* XXX Maximum power consumption (500mA)    */
    /* Interface */
    0x09,                       /* Descriptor length (9 bytes)        */
    0x04,                       /* Descriptor type (Interface)        */
    0x00,                       /* Number of interface (0)            */
    0x00,                       /* Alternate setting (0)              */
    0x02,                       /* Number of interface endpoint (2)   */
    0xff,                       /* Class code                         */
    0x00,                       /* Subclass code ()                   */
    0xff,                       /* Protocol code ()                   */
    0x00,                       /* Index of string()                  */
    /* Endpoint */
    0x07,                       /* Descriptor length (7 bytes)               */
    0x05,                       /* Descriptor type (Endpoint)                */
    0x81,                       /* Encoded address (Respond to IN) EP1       */
    0x02,                       /* Endpoint attribute (Bulk transfer)        */
    0x32, 0x00,                 /* Maximum packet size (50 bytes)            */
    0x01,                       /* Polling interval                          */
    /* Endpoint */
    0x07,                       /* Descriptor length (7 bytes)              */
    0x05,                       /* Descriptor type (Endpoint)               */
    0x01,                       /* Encoded address (Respond to OUT) EP1     */
    0x02,                       /* Endpoint attribute (Bulk transfer)       */
    0x32, 0x00,                 /* Maximum packet size (50 bytes)           */
    0x01,                       /* Polling interval                         */
};

Added soft/firmware/src/fusb/fusb_proto.d.



>
1
 src/fusb/fusb_proto.d  src/fusb/fusb_proto.p1:  src/fusb/fusb_proto.c src/fusb/fusb_reg.h src/fusb/fusb_proto.h lib/sys/types.h

Added soft/firmware/src/fusb/fusb_proto.h.



















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef USBPROTO_H
#define USBPROTO_H

#include <types.h>

/*
 * device requests
 */
struct dev_req_t {
    uint8  bmRequestType;
    uint8  bRequest;
    uint16 wValue;
    uint16 wIndex;
    uint16 wLength;
};

/******************************************************************************
 * Standard request codes (table 9-4)
 *****************************************************************************/
#define GET_DESCRIPTOR    6
#define SET_ADDRESS       5
#define SET_CONFIGURATION 9
/******************************************************************************
 * Descriptor Types (table 9-5)
 *****************************************************************************/
#define DESC_TYPE_DEVICE                    0x01
#define DESC_TYPE_CONFIGURATION             0x02
#define DESC_TYPE_STRING                    0x03
#define DESC_TYPE_INTERFACE                 0x04
#define DESC_TYPE_ENDPOINT                  0x05
#define DESC_TYPE_DEVICE_QUALIFIER          0x06
#define DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07
#define DESC_TYPE_INTERFACE_POWER           0x08
/******************************************************************************
 * USB Device Descriptor (table 9-8)
 *****************************************************************************/
#pragma pack 1
struct USB_Device_Descriptor {
    uint8  bLength;
    uint8  bDescriptorType;
    uint16 bcdUSB;
    uint8  bDeviceClass;
    uint8  bDeviceSubClass;
    uint8  bDeviceProtocol;
    uint8  bMaxPacketSize0;
    uint16 idVendor;
    uint16 idProduct;
    uint16 bcdDevice;
    uint8  iManufacturer;
    uint8  iProduct;
    uint8  iSerialNumber;
    uint8  bNumConfigurations;
};
/******************************************************************************
 * Device_Qualifier (table 9-9)
 *****************************************************************************/
struct USB_Device_Qualifier {
    uint8  bLength;
    uint8  bDescriptorType;
    uint16 bcdUSB;
    uint8  bDeviceClass;
    uint8  bDeviceSubClass;
    uint8  bDeviceProtocol;
    uint8  bMaxPacketSize0;
    uint8  bNumConfigurations;
    uint8  reserved;
};
/******************************************************************************
 * Configuration Descriptor (table 9-10)
 *****************************************************************************/
struct USB_Configuration_Descriptor {
    uint8  bLength;
    uint8  bDescriptorType;
    uint16 wTotalLength;
    uint8  bNumInterfaces;
    uint8  bConfigurationValue;
    uint8  iConfiguration;
    uint8  bmAttributes;
    uint8  bMaxPower; 
};



extern const struct USB_Device_Descriptor device_desc;
extern const struct USB_Configuration_Descriptor conf_desc;
extern const const uint8 configuration[];

#endif

Added soft/firmware/src/fusb/fusb_reg.c.









>
>
>
>
1
2
3
4
#include "fusb_reg.h"

volatile struct bdtable_t bdtable @ USBRAM_START;

Added soft/firmware/src/fusb/fusb_reg.d.



>
1
 src/fusb/fusb_reg.d  src/fusb/fusb_reg.p1:  src/fusb/fusb_reg.c src/fusb/fusb_reg.h lib/sys/types.h

Added soft/firmware/src/fusb/fusb_reg.h.





















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef USBREG_H
#define USBREG_H

#include <types.h>


#define UEP_EPSTALL  (1 << 0)
#define UEP_EPINEN   (1 << 1)
#define UEP_EPOUTEN  (1 << 2)
#define UEP_EPCONDIS (1 << 3)
#define UEP_EPHSHK   (1 << 4)

/* USB interrupt enable register */
#define UIE_URSTIE   (1 << 0) /* USB Reset */
#define UIE_UERRIE   (1 << 1) /* USB Error */
#define UIE_ACTVIE   (1 << 2) /* Bus Activity Detect */
#define UIE_TRNIE    (1 << 3) /* Transaction Complete */
#define UIE_IDLEIE   (1 << 4) /* Idle Detect */
#define UIE_STALLIE  (1 << 5) /* Stall Handshake */
#define UIE_SOFIE    (1 << 6) /* Start-of-Frame Token */

/* Buffer descriptor status (CPU mode) */
#define BDSTAT_CPU_BSTALL    (1 << 2)
#define BDSTAT_CPU_DTSEN     (1 << 3)
#define BDSTAT_CPU_DTS       (1 << 6)
#define BDSTAT_CPU_SIE_UOWN  (1 << 7)

struct bdregs_t {
    uint8 BDSTAT;
    uint8 BDCNT;
    uint8 BDADDRL;
    uint8 BDADDRH;
};

#define USBRAM_START 0x200
#define USBRAM_SIZE  256

/*
 * Buffer Descriptors Table
 *
 * Even/Odd ping-pong buffers enabled for Endpoints 1 to 15
 *
 * NOTE check that this structure not exceed USBRAM_SIZE
 * NOTE IN and OUT endpoints can be with different sizes
 *
 *
 */
#define EP0_BUFSIZE  8
#define EP1_BUFSIZE  50

/* buffer descriptor table */
struct bdtable_t {
    struct bdregs_t ep0bdout;
    struct bdregs_t ep0bdin;
    struct bdregs_t ep1bdout[2];
    struct bdregs_t ep1bdin[2];

    struct {
        uint8 out[EP0_BUFSIZE];
        uint8 in[EP0_BUFSIZE];
    } ep0buf;
    struct {
        uint8 out[EP1_BUFSIZE][2];
        uint8 in[EP1_BUFSIZE][2];
    } ep1buf;
};

//extern volatile __sfr __at (0x200) BD0STAT;

extern volatile struct bdtable_t bdtable @ USBRAM_START;


#endif

Added soft/firmware/src/led.c.











































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <xc.h>
#include <debug.h>
#include "led.h"
#include "stimer.h"

static uint16 to;

static uint8 nled;

#define LED_ON()      do {LATCbits.LATC5 = 0;} while (0)
#define LED_OFF()     do {LATCbits.LATC5 = 1;} while (0)
#define LED_SWITCH()  do {LATCbits.LATC5 ^= 1;} while (0)

void led_init()
{
    TRISCbits.TRISC5 = 0;

    stimer_set(&to);

    nled = 0;
    LED_ON();
}

#define MIN_INTERVAL 50

#define MAX_INTERVAL (1000 / MIN_INTERVAL)
/*
 *
 */
void led_process()
{
    if (stimer_delta(to) >= MIN_INTERVAL)
    {
        stimer_set(&to);

        if (nled)
        {
            if (--nled == 0)
                LED_OFF();
            else
                LED_SWITCH();
        }
    }
}

/*
 *
 */
void led_blink(uint8 c)
{
    nled = c << 1;
}

Added soft/firmware/src/led.d.



>
1
 src/led.d  src/led.p1:  src/led.c src/led.h lib/sys/debug.h lib/sys/uart.h lib/sys/types.h lib/sys/sys.h lib/sys/stimer.h

Added soft/firmware/src/led.h.























>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
#ifndef LED_H
#define LED_H

#include <types.h>

void led_init();
void led_process();
void led_blink(uint8 c);

#endif

Added soft/firmware/src/main.c.











































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <xc.h>
#include <stimer.h>
#include <uart.h>
#include <debug.h>
#include "version.h"
#include "led.h"
#include "fusb/fusb.h"
#include "u2s.h"

#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config IESO   = OFF
#pragma config FOSC   = HS
#pragma config PLLEN  = ON
#pragma config FCMEN  = OFF
#pragma config PCLKEN = ON
#pragma config BOREN  = SBORDIS
#pragma config BORV   = 30
#pragma config PWRTEN = ON
#pragma config WDTPS  = 8192       /* XXX TODO */
#pragma config WDTEN  = OFF        /* XXX TODO */
#pragma config MCLRE  = ON
#pragma config HFOFST = OFF
#pragma config DEBUG  = OFF
#pragma config STVREN = ON
#pragma config XINST  = OFF
#pragma config BBSIZ  = OFF
#pragma config LVP    = OFF

/*
 *
 */
void interrupt hisr()
{
    stimer_isr();
}

/*
 *
 */
void init()
{
    u2s_init();
    led_init();
    stimer_init();
    uart_init();
    fusb_init();

    INTCONbits.GIEH = 1;
    INTCONbits.PEIE = 1;
}

/*
 *
 */
void main()
{
    init();

    debug_str(MODULE_NAME", v"VERSION_STRING"\r\n");

    led_blink(4);
    while (1)
    {
        led_process();
        fusb_process();
    }
}

Added soft/firmware/src/main.d.



>
1
 src/main.d  src/main.p1:  src/main.c src/led.h lib/sys/debug.h lib/sys/uart.h src/version.h lib/sys/types.h lib/sys/sys.h src/fusb/fusb.h lib/sys/stimer.h src/u2s.h

Added soft/firmware/src/main_.c.













































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * sdcc
 */
#include <pic16/pic18fregs.h>
#include <types.h>
#include <stimer.h>
#include "version.h"
#include "led.h"
#include "uart.h"
#include "debug.h"
#include "usb.h"

#define CONF1L_UNDEF 0x38
#define CONF1H_UNDEF 0xff
#define CONF2L_UNDEF 0x1f
#define CONF2H_UNDEF 0x1f
#define CONF3H_UNDEF 0x88
#define CONF4L_UNDEF 0xcd

static __code char __at(__CONFIG1L) conf1L = 
    CONF1L_UNDEF & _USBDIV_OFF_1L & _CPUDIV_NOCLKDIV_1L;

static __code char __at(__CONFIG1H) conf1H = 
    CONF1H_UNDEF & _FOSC_HS_1H & _PLLEN_ON_1H & _PCLKEN_ON_1H & _FCMEN_OFF_1H & _IESO_OFF_1H;

static __code char __at(__CONFIG2L) conf2L = 
    CONF2L_UNDEF & _BOREN_OFF_2L;

static __code char __at(__CONFIG2H) conf2H = 
    CONF2H_UNDEF & _WDTEN_OFF_2H; /* TODO postscaller */

static __code char __at(__CONFIG3H) conf3H = 
    CONF3H_UNDEF & _MCLRE_ON_3H & _HFOFST_ON_3H;

static __code char __at(__CONFIG4L) conf4L = 
    CONF4L_UNDEF & _STVREN_ON_4L & _LVP_OFF_4L & _BBSIZ_OFF_4L & _XINST_OFF_4L;



/*
 *
 */
void hisr() __interrupt 1
{
//    stimer_isr();
    uart_isr();
    usb_isr();
}


/*
 *
 */
void main(void)
{
    uart_init();

    debug_str("\r\n-= u2s, v."VERSION_STRING", "__DATE__" "__TIME__" =-\r\n");
//    led_init();
//    stimer_init();
    INTCONbits.GIEH = 1;
    INTCONbits.PEIE = 1;

    while(1)
    {
//        led_process();
        usb_process();
    }
}

Added soft/firmware/src/u2s.c.























































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <xc.h>
#include <debug.h>
#include "util.h"
#include "u2s.h"
#include "fusb/fusb.h"
#include "fusb/fusb_reg.h"
#include "led.h"
#include "u2s_shared.h"

//#define DEBUGU2S

#ifdef DEBUGU2S
    #define DEBUGU2S_STR(s)    debug_str(s)
    #define DEBUGU2S_CHAR(c)   debug_char(c)
    #define DEBUGU2S_HEX(h, l) debug_hex(h, l)
#else
    #define DEBUGU2S_STR(s)
    #define DEBUGU2S_CHAR(c)
    #define DEBUGU2S_HEX(h, l)
#endif

#define U2S_BUF_SIZE    EP1_BUFSIZE
static uint8 u2s_buf[U2S_BUF_SIZE];

static void u2s_error(uint8 error);

static uint8 _rda_write(uint8* data);
static uint8 _rda_read(uint8* data);

//static void u2s_spi_setup(struct u2s_spi_setup_options *options);
//static void u2s_spi_exec(
//        struct u2s_spi_exec_options *options,
//        uint8 *txbuf, uint8 *rxbuf, int len);
//static void u2s_spi_exec2(
//        struct u2s_spi_exec_options *options, uint8 cs_gap,
//        uint8 *txbuf, uint8 *rxbuf, int len);
//static void u2s_io_dir(union u2s_io_map_t *dir, union u2s_io_map_t *mask);
//static void u2s_io_control(union u2s_io_map_t *control, union u2s_io_map_t *mask);
//static uint8 u2s_io_read();
//static void u2s_swd_raw(uint8 *iodir, uint8 *txbuf, uint8 *rxbuf, uint8 nbits);
//static void u2s_swd_exec(struct cmd_swd_exec_req_t *req, struct cmd_swd_exec_resp_t *resp);

//#define  IO0_TRIS    TRISCbits.TRISC0
//#define  IO1_TRIS    TRISCbits.TRISC1
//#define  IO2_TRIS    TRISCbits.TRISC5
//#define  IO3_TRIS    TRISCbits.TRISC4
//#define  KEY_TRIS    TRISCbits.TRISC2
//#define MOSI_TRIS    TRISCbits.TRISC7
//#define MISO_TRIS    TRISBbits.TRISB4
//#define  SCK_TRIS    TRISBbits.TRISB6
//
//#define  IO0_LAT    LATCbits.LATC0
//#define  IO1_LAT    LATCbits.LATC1
//#define  IO2_LAT    LATCbits.LATC5
//#define  IO3_LAT    LATCbits.LATC4
//#define  KEY_LAT    LATCbits.LATC2
//#define MOSI_LAT    LATCbits.LATC7
//#define MISO_LAT    LATBbits.LATB4
//#define  SCK_LAT    LATBbits.LATB6
//
//#define  IO0_PORT    PORTCbits.RC0
//#define  IO1_PORT    PORTCbits.RC1
//#define  IO2_PORT    PORTCbits.RC5
//#define  IO3_PORT    PORTCbits.RC4
//#define  KEY_PORT    PORTCbits.RC2
//#define MOSI_PORT    PORTCbits.RC7
//#define MISO_PORT    PORTBbits.RB4
//#define  SCK_PORT    PORTBbits.RB6

/*
 * send data to host
 */
#define u2s_send(buf, len) fusb_send_wrap(buf, len)


/*
 * initialize IO, etc
 */
void u2s_init()
{
    ANSEL  = 0;
    ANSELH = 0;

//    IO0_TRIS  = 1;
//    IO1_TRIS  = 1;
//    IO2_TRIS  = 1;
//    IO3_TRIS  = 1;
//    KEY_TRIS  = 0;
//    MOSI_TRIS = 1;
//    MISO_TRIS = 1;
//    SCK_TRIS  = 1;
//
//    IO0_LAT  = 0;
//    IO1_LAT  = 0;
//    IO2_LAT  = 0;
//    IO3_LAT  = 0;
//    KEY_LAT  = 1; /* NOTE */
//    MOSI_LAT = 0;
//    MISO_LAT = 0;
//    SCK_LAT  = 0;
}

/*
 * receive data from host
 */
void u2s_receive(uint8 *buf, uint8 len)
{
    struct u2s_req_t *req;
    struct u2s_resp_t *resp;
    uint8 olen;

    led_blink(2);

#ifdef DEBUGU2S
    DEBUGU2S_STR("u2s receive:");
    util_printarray(buf, len);
#endif

    req = (struct u2s_req_t *)buf;
    resp = (struct u2s_resp_t *)u2s_buf;

    resp->cmd = req->cmd;

    olen = U2S_REQ_HEAD_SIZE;
    switch (req->cmd)
    {
        case U2S_CMD_RDA_WRITE:
            if (len != (U2S_REQ_HEAD_SIZE + sizeof(union rda_write_data_t)))
            {
                u2s_error(U2S_ERROR_CMDLENMISMATCH);
                return;
            }
            if (_rda_write(req->rda_write.data) != 0)
            {
                u2s_error(U2S_ERROR_I2C_NACK);
                return;
            }
            break;
        case U2S_CMD_RDA_READ:
            if (len != U2S_REQ_HEAD_SIZE)
            {
                u2s_error(U2S_ERROR_CMDLENMISMATCH);
                return;
            }
            if (_rda_read(resp->rda_read.data) != 0)
            {
                u2s_error(U2S_ERROR_I2C_NACK);
                return;
            }
            olen += sizeof(union rda_write_data_t);
            break;
        default:
            u2s_error(U2S_ERROR_CMDUNKNOWN);
            return;
    }

    u2s_send(u2s_buf, olen);
}

/*
 *
 */
static void u2s_error(uint8 error)
{
    u2s_buf[0] = U2S_CMD_ERROR;
    u2s_buf[1] = error;
    u2s_send(u2s_buf, 2);
}

/*
 *
 * RETURN
 *     0 on success, nonzero on error
 */
static uint8 _rda_write(uint8* data)
{
#ifdef DEBUGU2S
    DEBUGU2S_STR("rda write:");
    util_printarray(data, sizeof(union rda_write_data_t));
#endif

    /* TODO */

    return 0;
}

/*
 *
 * RETURN
 *     0 on success, nonzero on error
 */
static uint8 _rda_read(uint8* data)
{

    /* TODO */

#ifdef DEBUGU2S
    DEBUGU2S_STR("rda write:");
    util_printarray(data, sizeof(union rda_read_data_t));
#endif
    return 0;
}

Added soft/firmware/src/u2s.d.



>
1
 src/u2s.d  src/u2s.p1:  src/u2s.c src/led.h lib/sys/debug.h src/fusb/fusb_reg.h src/util.h lib/sys/uart.h lib/sys/types.h src/fusb/fusb.h src/u2s.h src/u2s_shared.h

Added soft/firmware/src/u2s.h.





















>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
#ifndef HW_U2S_C
#define HW_U2S_C

#include <types.h>

void u2s_init();
void u2s_receive(uint8 *buf, uint8 len);

#endif

Added soft/firmware/src/u2s_shared.h.























































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef U2S_SHARED_H
#define U2S_SHARED_H

#include <types.h>

#define HOST_ENDIAN_LITTLE       /* XXX TODO BIG ENDIAN */

/*
 * NOTE target is PIC18 micro, which have 1 byte alligment
 */
#ifndef TARGET
    #pragma pack(push, 1)
#endif

#define RDA_REGS_SIZE    (2 * 6) /* 0x02 to 0x07 for write, 0x0A to 0x0F for read; 16-bit regs, BE */
union rda_read_data_t {
    uint8 data[RDA_REGS_SIZE];
    struct {
#ifdef HOST_ENDIAN_LITTLE
        struct {
            uint8 readchan_high :2; /* 8-9 */
            uint8 st            :1; /* 10 */
            uint8 blk_e         :1; /* 11 */
            uint8 rdss          :1; /* 12 */
            uint8 sf            :1; /* 13 */
            uint8 stc           :1; /* 14 */
            uint8 rdsr          :1; /* 15 */
            /* */
            uint8 readchan_low  :8; /* 0-7 */
        } reg0a;
        struct {
            uint8 fm_true       :1; /* 8 */
            uint8 rssi          :7; /* 9-15 */
            /* */
            uint8 blerb         :2; /* 0-1 */
            uint8 blera         :2; /* 2-3 */
            uint8 abcd_e        :1; /* 4   */
            uint8 reserved      :2; /* 5-6 */
            uint8 fm_ready      :1; /* 7   */
        } reg0b;
        struct {
            uint8 rdsa_high;
            uint8 rdsa_low;
        } reg0c;
        struct {
            uint8 rdsb_high;
            uint8 rdsb_low;
        } reg0d;
        struct {
            uint8 rdsc_high;
            uint8 rdsc_low;
        } reg0e;
        struct {
            uint8 rdsd_high;
            uint8 rdsd_low;
        } reg0f;
#else
#error TODO: HOST BIG ENDIAN
#endif
    };
}


struct u2s_req_t {
#define U2S_REQ_HEAD_SIZE  1
#define U2S_MAX_DATA_SIZE     (50 /* XXX EP1_BUF_SIZE */ - U2S_REQ_HEAD_SIZE)
    uint8 cmd;

    union {
        union rda_write_data_t {
            uint8 data[RDA_REGS_SIZE];
            struct {
#ifdef HOST_ENDIAN_LITTLE
                struct {
                    uint8 seek         :1; /* 8   */
                    uint8 seekup       :1; /* 9   */
                    uint8 rclk_direct  :1; /* 10  */
                    uint8 rclk_nonclb  :1; /* 11  */
                    uint8 bass         :1; /* 12  */
                    uint8 mono         :1; /* 13  */
                    uint8 dmute        :1; /* 14  */
                    uint8 dhiz         :1; /* 15  */
                    /* */
                    uint8 enable       :1; /* 0   */
                    uint8 soft_reset   :1; /* 1   */
                    uint8 new_method   :1; /* 2   */
                    uint8 rds_en       :1; /* 3   */
#define RDA_CLK_MODE_32768_KHZ   0
#define RDA_CLK_MODE_12_MHZ      1
#define RDA_CLK_MODE_24_MHZ      5
#define RDA_CLK_MODE_13_MHZ      2
#define RDA_CLK_MODE_26_MHZ      6
#define RDA_CLK_MODE_19_2_MHZ    3
#define RDA_CLK_MODE_38_4_MHZ    7
                    uint8 clk_mode     :3; /* 4-6 */
                    uint8 skmode       :1; /* 7   */
                } reg02;
                struct {
#define RDA_SET_CHAN(preg, val) do {(preg)->chan_low = (val) & 0x03; (preg)->chan_high = (val) >> 2; } while (0);
                    uint8 chan_high    :8; /* 8-15 */
                    /* */
#define RDA_SPACE_100_KHZ  0
#define RDA_SPACE_200_KHZ  1
#define RDA_SPACE_50_KHZ   2
#define RDA_SPACE_25_KHZ   3
                    uint8 space        :2; /* 0-1 */
#define RDA_BAND_87_108_MHZ   0
#define RDA_BAND_76_91_MHZ    1
#define RDA_BAND_76_108_MHZ   2
#define RDA_BAND_65_76_MHZ    3
                    uint8 band         :2; /* 2-3 */
                    uint8 tune         :1; /* 4   */
                    uint8 direct_mode  :1; /* 5   */
                    uint8 chan_low     :2; /* 6-7 */
                } reg03;
                struct {
                    uint8 afcd         :1; /* 8   */
                    uint8 softmute_en  :1; /* 9   */
                    uint8 rsvd0        :1; /* 10  */
                    uint8 de           :1; /* 11  */
                    uint8 rsvd1        :2; /* 12-13 */
                    uint8 stcien       :1; /* 14   */
                    uint8 rsvd2        :1; /* 15   */
                    /* */
#define RDA_GPIO1_MODE_HIGHZ    0
#define RDA_GPIO1_MODE_RESERVED 1
#define RDA_GPIO1_MODE_LOW      2
#define RDA_GPIO1_MODE_HIGH     3
                    uint8 gpio1        :2; /* 0-1 */
#define RDA_GPIO2_MODE_HIGHZ    0
#define RDA_GPIO2_MODE_INT      1
#define RDA_GPIO2_MODE_LOW      2
#define RDA_GPIO2_MODE_HIGH     3
                    uint8 gpio2        :2; /* 2-3 */
#define RDA_GPIO3_MODE_HIGHZ       0
#define RDA_GPIO3_MODE_MONO_STEREO 1
#define RDA_GPIO3_MODE_LOW         2
#define RDA_GPIO3_MODE_HIGH        3
                    uint8 gpio3        :2; /* 4-5 */
                    uint8 i2s_enabled  :1; /* 6   */
                    uint8 rsvd         :1; /* 7   */
                } reg04;
                struct {
                    /* */
                    uint8 seekth       :4; /* 8-11  */
                    uint8 rsvd         :3; /* 12-14 */
                    uint8 int_mode     :1; /* 15    */
#define RDA_VOLUME_MAX    15
                    uint8 volume       :4; /* 0-3  */
                    uint8 rsvd0        :2; /* 4-5  */
                    uint8 lna_port_sel :2; /* 6-7  */
                } reg05;
                struct {
                    uint8 ws_i_edge    :1; /* 8  */
                    uint8 data_signed  :1; /* 9  */
                    uint8 sclk_i_edge  :1; /* 10 */
                    uint8 sw_lr        :1; /* 11 */
                    uint8 i2s_mode     :1; /* 12 */
                    uint8 open_mode    :2; /* 13-14 */
                    uint8 rsvd         :1; /* 15 */
                    /* */
                    uint8 r_dely       :1; /* 0 */
                    uint8 l_dely       :1; /* 1 */
                    uint8 sclk_o_edge  :1; /* 2 */
                    uint8 sw_o_edge    :1; /* 3 */
#define RDA_WS_STEP_8_KBPS         0
#define RDA_WS_STEP_11_025_KBPS    1
#define RDA_WS_STEP_12_KBPS        2
#define RDA_WS_STEP_16_KBPS        3
#define RDA_WS_STEP_22_05_KBPS     4
#define RDA_WS_STEP_24_KBPS        5
#define RDA_WS_STEP_32_KBPS        6
#define RDA_WS_STEP_44_1_KBPS      7
#define RDA_WS_STEP_48_KBPS        8
                    uint8 i2s_sw_cnt   :4; /* 4-7 */
                } reg06;
                struct {
                    uint8 rsvd0         :1; /* 8     */
                    uint8 _65m_50m_mode :1; /* 9     */
                    uint8 th_sofrblend  :5; /* 10-14 */
                    uint8 rsvd1         :1; /* 15    */
                    /* */
                    uint8 freq_mode    :1; /* 0      */
                    uint8 softblend_en :1; /* 1      */
                    uint8 seek_th_old  :6; /* 2      */
                } reg07;
#else
    #error TODO: HOST BIG ENDIAN
#endif
            };
        } rda_write;
        uint8 buf[U2S_MAX_DATA_SIZE];
    };
};

struct u2s_resp_t {
    uint8 cmd;

    union {
        union rda_read_data_t rda_read;
        uint8 error;
        uint8 buf[U2S_MAX_DATA_SIZE];
    };
};

#define U2S_CMD_RDA_WRITE    0x01
#define U2S_CMD_RDA_READ     0x02
#define U2S_CMD_ERROR        0xFF

#define U2S_ERROR_CMDUNKNOWN       1
#define U2S_ERROR_CMDLENMISMATCH   2
#define U2S_ERROR_I2C_NACK         3

#ifndef TARGET
    #pragma pack(pop)
#endif

#endif

Added soft/firmware/src/util.c.













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <debug.h>
#include "util.h"

/*
 *
 */
void util_printarray(uint8* addr, uint8 len)
{
    uint8 i;

    i = 0;
    while (len--)
    {
        if ((i % 16) == 0)
            debug_str("\r\n");

        debug_hex(addr, 1); debug_str(" ");
        i++;
        addr++;
    }
    debug_str("\r\n");
}

/*
 *
 */
void util_memcpy(void *dst, void *src, uint8 cnt)
{
    uint8 *d;
    uint8 *s;

    d = dst;
    s = src;

    while (cnt--)
        *(d++) = *(s++);
}

Added soft/firmware/src/util.d.



>
1
 src/util.d  src/util.p1:  src/util.c lib/sys/debug.h src/util.h lib/sys/uart.h lib/sys/types.h

Added soft/firmware/src/util.h.































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef UTIL_H
#define UTIL_H

#include <types.h>

/*
 * TODO make as library or use "defines" to disable some 
 * funcitons
 */

void util_printarray(uint8* addr, uint8 len);
void util_memcpy(void *dst, void *src, uint8 cnt);

#endif

Added soft/firmware/src/version.h.



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef VERSION_H
#define VERSION_H

#define MODULE_NAME "U2S"

#define MAJOR  0
#define MINOR  1
#define BUILD  1

#define QUOTEME_(x) #x
#define QUOTEME(x) QUOTEME_(x)

#define VERSION_STRING QUOTEME(MAJOR.MINOR.BUILD)


#endif

Added soft/libk20/Makefile.









































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
#
#

K20TOOL = k20tool

CC = gcc
LD = gcc

CFLAGS  += -Wall

#LDFLAGS +=

#
#
#
.PHONY: all clean
all: $(K20TOOL)

#
#
#
K20TOOL_CFLAGS  += $(CFLAGS)
K20TOOL_LDFLAGS += $(LDFLAGS)

K20TOOL_OBJS += k20tool.c

k20tool.o: k20tool.c
	$(CC) -c $(K20TOOL_CFLAGS) $^

K20TOOL: k20tool.o
	$(LD) $(K20TOOL_LDFLAGS) -o $@

clean:
	rm -f $(K20TOOL) *.o

Added soft/libk20/k20tool.c.



























>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

/*
 *
 */
int main(int argc, char** argv)
{
    printf("Hello\r\n");


    return 0;
}