This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.cpp
More file actions
144 lines (104 loc) · 2.82 KB
/
Copy pathEncoder.cpp
File metadata and controls
144 lines (104 loc) · 2.82 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
#include "Encoder.h"
DBlock * currentBlock = new DBlock;
int SDblock = 5;
int Blockstelle = 0;
encD * redptr ;
void EncoR (){
redptr->R ++;
}
void EncoL (){
redptr->L ++;
}
uint8_t wait4r(){
digitalWrite(LED_BUILTIN, HIGH);
uint8_t resp;
do{
resp = SPI.transfer(0xff);
}while(resp == 0xff);
digitalWrite(LED_BUILTIN, LOW);
return resp;
}
void initSD(){
SPISettings SPIset(200000, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(SPIset);
SPI.begin();
pinMode(nss, OUTPUT);
digitalWrite(nss, HIGH);
for(int i=0; i == 5;i++) SPI.transfer16(0xffff);
//Software Reset
digitalWrite(nss, LOW);
SPI.transfer(0xffff);
SPI.transfer(0x40);
SPI.transfer16(0x0000);
SPI.transfer16(0x0000);
SPI.transfer(0x95);
wait4r();
delay(4000);
do{
delay(1000);
//cmd55
SPI.transfer(0x77);
SPI.transfer16(0x0000);
SPI.transfer16(0x0000);
SPI.transfer(0x01);
//acmd41
SPI.transfer(0x69);
SPI.transfer16(0x4000);
SPI.transfer16(0x0000);
SPI.transfer(0x01);
}while(wait4r() != 0x00);
SPI.setClockDivider(21);
}
void sendsbSD(DBlock * Data){
while(SPI.transfer(0xff) == 0);//wait until not busy
//cmd24
SPI.transfer(0x58);
SPI.transfer16(0x0000);
SPI.transfer16(SDblock);
SPI.transfer(0x00);
wait4r();
SPI.transfer(0x00);
//Data Packet
SPI.transfer(0xfe);
SPI.transfer(Data, 512);
SPI.transfer16(0x0000);
if(( 0b00011111 & SPI.transfer(0xff)) != 0b00101) /*bad*/;
delete Data;
SDblock++;
}
void TC7_Handler()
{
// We need to get the status to clear it and allow the interrupt to fire again
TC_GetStatus(TC2, 1);
if(Blockstelle < encWerte){
redptr = &(currentBlock->enc[Blockstelle]);
}else{
//neuen Block erstellen
DBlock * DBtempPtr = new DBlock;
//Blöcke verknüpfen
currentBlock->ptrN = DBtempPtr;
DBtempPtr->ptrP = currentBlock;
//sendsbSD(currentBlock);
//aktuellen Block auswechseln
currentBlock = DBtempPtr;
Blockstelle = 0;
redptr = &(currentBlock->enc[Blockstelle]);
}
Blockstelle ++;
}
void TimerInit(){
//von http://2manyprojects.net/timer-interrupts
/* turn on the timer clock in the power management controller */
pmc_set_writeprotect(false); // disable write protection for pmc registers
pmc_enable_periph_clk(ID_TC7); // enable peripheral clock TC7
/* we want wavesel 01 with RC */
TC_Configure(/* clock */TC2,/* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
TC_SetRC(TC2, 1, 65600);
TC_Start(TC2, 1);
// enable timer interrupts on the timer
TC2->TC_CHANNEL[1].TC_IER=TC_IER_CPCS; // IER = interrupt enable register
TC2->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS; // IDR = interrupt disable register
/* Enable the interrupt in the nested vector interrupt controller */
/* TC4_IRQn where 4 is the timer number * timer channels (3) + the channel number (=(1*3)+1) for timer1 channel1 */
NVIC_EnableIRQ(TC7_IRQn);
}