]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - hw/hantek-6022bl/fw.c
a78350ecca3b4d23a4a84ac7503523e21c9a5164
[sigrok-firmware-fx2lafw.git] / hw / hantek-6022bl / fw.c
1 /*
2  * This file is part of the sigrok-firmware-fx2lafw project.
3  *
4  * Copyright (C) 2009 Ubixum, Inc.
5  * Copyright (C) 2015 Jochen Hoenicke
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <fx2macros.h>
22 #include <fx2ints.h>
23 #include <autovector.h>
24 #include <delay.h>
25 #include <setupdat.h>
26
27 #define SET_ANALOG_MODE() PA7 = 1
28
29 #define SET_COUPLING(x)
30
31 #define SET_CALIBRATION_PULSE(x)
32
33 /* Toggle the 1kHz calibration pin, only accurate up to ca. 8MHz. */
34 #define TOGGLE_CALIBRATION_PIN() PC2 = !PC2
35
36 #define LED_CLEAR() PC0 = 1; PC1 = 1;
37 #define LED_GREEN() PC0 = 1; PC1 = 0;
38 #define LED_RED()   PC0 = 0; PC1 = 1;
39
40 #define TIMER2_VAL 500
41
42 /* CTLx pin index (IFCLK, ADC clock input). */
43 #define CTL_BIT 0
44
45 #define OUT0 ((1 << CTL_BIT) << 4) /* OEx = 1, CTLx = 0 */
46
47 static const struct samplerate_info samplerates[] = {
48         { 48, 0x80,   0, 3, 0, 0x00, 0xea },
49         { 30, 0x80,   0, 3, 0, 0x00, 0xaa },
50         { 24,    1,   0, 2, 1, OUT0, 0xca },
51         { 16,    1,   1, 2, 0, OUT0, 0xca },
52         { 12,    2,   1, 2, 0, OUT0, 0xca },
53         {  8,    3,   2, 2, 0, OUT0, 0xca },
54         {  4,    6,   5, 2, 0, OUT0, 0xca },
55         {  2,   12,  11, 2, 0, OUT0, 0xca },
56         {  1,   24,  23, 2, 0, OUT0, 0xca },
57         { 50,   48,  47, 2, 0, OUT0, 0xca },
58         { 20,  120, 119, 2, 0, OUT0, 0xca },
59         { 10,  240, 239, 2, 0, OUT0, 0xca },
60 };
61
62 /*
63  * This sets three bits for each channel, one channel at a time.
64  * For channel 0 we want to set bits 1, 2 & 3
65  * For channel 1 we want to set bits 4, 5 & 6
66  *
67  * We convert the input values that are strange due to original
68  * firmware code into the value of the three bits as follows:
69  *
70  * val -> bits
71  * 1  -> 010b
72  * 2  -> 001b
73  * 5  -> 000b
74  * 10 -> 011b
75  *
76  * The third bit is always zero since there are only four outputs connected
77  * in the serial selector chip.
78  *
79  * The multiplication of the converted value by 0x24 sets the relevant bits in
80  * both channels and then we mask it out to only affect the channel currently
81  * requested.
82  */
83 static BOOL set_voltage(BYTE channel, BYTE val)
84 {
85         BYTE bits, mask;
86
87         switch (val) {
88         case 1:
89                 bits = 0x02;
90                 break;
91         case 2:
92                 bits = 0x01;
93                 break;
94         case 5:
95                 bits = 0x00;
96                 break;
97         case 10:
98                 bits = 0x03;
99                 break;
100         default:
101                 return FALSE;
102         }
103
104         bits = bits << (channel ? 1 : 4);
105         mask = (channel) ? 0x70 : 0x0e;
106         IOA = (IOA & ~mask) | (bits & mask);
107
108         return TRUE;
109 }
110
111 #include <scope.inc>