summaryrefslogtreecommitdiff
path: root/firmware/drivers/serial.c
blob: 91e2a6911f38433349daf35a2cb35293ad5c4ce1 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Alan Korr & Nick Robinson
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdlib.h>
#include "button.h"
#include "config.h"
#include "sh7034.h"
#include "system.h"
#include "kernel.h"
#include "backlight.h"
#include "adc.h"
#include "lcd.h"
#include "serial.h"

/* Received byte identifiers */
#define PLAY  0xC1
#define STOP  0xC2
#define PREV  0xC4
#define NEXT  0xC8
#define VOLUP 0xD0
#define VOLDN 0xE0

#ifdef SCREENDUMP
#define SCRDMP 0xF0

static void screen_dump(void);
#endif

void serial_setup (void) 
{ 
    char dummy;
    dummy = SSR1;
    SSR1 = 0;
    SMR1 = 0x00;
    SCR1 = 0;
    BRR1 = (FREQ/(32*9600))-1;

    /* let the hardware settle */
    sleep(1);

    SCR1 = 0x50;

    /* This enables the serial Rx interrupt*/
    IPRE = (IPRE & 0x0FFF) | 0x8000; /* Set to medium priority */
}

static void process_byte(int byte)
{
    int btn = 0;

    switch (byte)
    {
        case STOP:
#ifdef HAVE_RECORDER_KEYPAD
            btn = BUTTON_OFF;
#else
            btn = BUTTON_STOP;
#endif
            break;

        case PLAY:
            btn = BUTTON_PLAY;
            break;

        case VOLUP:
            btn = BUTTON_VOL_UP;
            break;

        case VOLDN:
            btn = BUTTON_VOL_DOWN;
            break;

        case PREV:
            btn = BUTTON_LEFT;
            break;

        case NEXT:
            btn = BUTTON_RIGHT;
            break;
            
#ifdef SCREENDUMP
        case SCRDMP:
            screen_dump();
            break;
#endif
    }

    if ( btn ) {
        queue_post(&button_queue, btn, NULL);
        backlight_on();
        queue_post(&button_queue, btn | BUTTON_REL, NULL);
    }
}

#pragma interrupt
void REI1 (void)
{
    SSR1 = SSR1 & ~0x10; /* Clear FER */
    SSR1 = SSR1 & ~0x40; /* Clear RDRF */
}

#pragma interrupt
void RXI1 (void)
{
    unsigned char serial_byte;
    serial_byte = RDR1;
    SSR1 = SSR1 & ~0x40; /* Clear RDRF */
    process_byte(serial_byte);
}

#ifdef SCREENDUMP
static void serial_enable_tx(void)
{
    SCR1 |= 0x20;
}

static void serial_tx(unsigned char ch)
{
    while (!(SSR1 & SCI_TDRE))
    {
        ;
    }
    
    /*
     * Write data into TDR and clear TDRE
     */
    TDR1 = ch;
    SSR1 &= ~SCI_TDRE;
}

static void screen_dump(void)
{
    int x, y;
    int level;

    serial_enable_tx();

    level = set_irq_level(15);
    for(y = 0;y < LCD_HEIGHT/8;y++)
    {
        for(x = 0;x < LCD_WIDTH;x++)
        {
            serial_tx(lcd_framebuffer[x][y]);
        }
    }
    set_irq_level(level);
}

#endif