forked from urjaman/libfrser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnibble.c.example
More file actions
149 lines (118 loc) · 3.03 KB
/
nibble.c.example
File metadata and controls
149 lines (118 loc) · 3.03 KB
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
/*
This file was part of bbflash, now frser-m328lpcspi.
Copyright (C) 2013, Hao Liu and Robert L. Thompson
Copyright (C) 2013 Urja Rannikko <urjaman@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "nibble.h"
#define delay() asm("nop")
#define swap(x) do { asm volatile("swap %0" : "=r" (x) : "0" (x)); } while(0)
#define FRAME_DDR DDRD
#define FRAME_PORT PORTD
#define FRAME PD3
#define INIT_DDR DDRD
#define INIT_PORT PORTD
#define INIT PD4
void nibble_set_dir(uint8_t dir) {
if (!dir) {
DDRC = 0;
}
}
uint8_t nibble_read(void) {
uint8_t rv;
rv = PINC & 0xF;
return rv;
}
static void nibble_write_hi(uint8_t data) {
swap(data);
DDRC = (~data) & 0xF;
// data &= 0xF;
// while ((PINC & 0xF) != data);
}
void nibble_write(uint8_t data) {
DDRC = (~data) & 0xF;
// data &= 0xF;
// while ((PINC & 0xF) != data);
}
#define clock_low() do { CLK_PORT &= ~_BV(CLK); } while(0)
#define clock_high() do { CLK_PORT |= _BV(CLK); } while(0)
bool nibble_init(void) {
uint8_t i;
INIT_PORT &= ~_BV(INIT);
INIT_DDR |= _BV(INIT);
CLK_DDR |= _BV(CLK);
CLK_PORT |= _BV(CLK);
FRAME_DDR |= _BV(FRAME);
FRAME_PORT |= _BV(FRAME);
nibble_set_dir(OUTPUT);
nibble_write(0);
for (i = 0; i < 24; i++)
clock_cycle();
INIT_DDR &= ~_BV(INIT);
_delay_us(1); // Let pullup work
for (i = 0; i < 42; i++)
clock_cycle();
return true;
}
void nibble_cleanup(void) {
CLK_DDR &= ~_BV(CLK);
FRAME_DDR &= ~_BV(FRAME);
nibble_set_dir(INPUT);
}
void clocked_nibble_write(uint8_t value) {
clock_low();
nibble_write(value);
clock_high();
}
void clocked_nibble_write_hi(uint8_t value) {
clock_low();
nibble_write_hi(value);
clock_high();
}
uint8_t clocked_nibble_read(void) {
clock_cycle();
delay();
delay();
return nibble_read();
}
void nibble_abort(void) {
gpio_set(FRAME_PORT, FRAME);
nibble_set_dir(OUTPUT);
clock_high();
gpio_clear(FRAME_PORT, FRAME);
nibble_write(0xF);
clock_cycle();
clock_cycle();
clock_cycle();
clock_cycle();
gpio_set(FRAME_PORT, FRAME);
clock_cycle();
clock_cycle();
}
void nibble_start(uint8_t start) {
FRAME_PORT |= _BV(FRAME);
nibble_set_dir(OUTPUT);
clock_high();
FRAME_PORT &= ~_BV(FRAME);
nibble_write(start);
clock_cycle();
FRAME_PORT |= _BV(FRAME);
}
void nibble_hw_init(void) {
/* Do your port init if needed etc... */
/* Kick reset here so lpc/fwh.c doesnt need to know about how it is controlled. */
DDRD |= _BV(2); //!RST
_delay_us(1);
DDRD &= ~_BV(2);
_delay_us(1); // slow pullup
}