The settle time values used here are 100/20. However, these values depend on the actual used RC combination and voltage drop by LED. The intermediate results are written to TX (PB3, pin 2 – using Serial).
For schematic details, see introductory comment block in the rf24ping85.ino sketch.
1
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <stdio.h>
21#include <SPI.h>
22#include <Arduino.h>
24
25
26#if defined(ARDUINO) && !defined(__arm__)
27#if defined(__AVR_ATtinyX5__) || defined(__AVR_ATtinyX4__)
28#define RF24_TINY
29#endif
30#endif
31
32
33
34#if defined(RF24_TINY)
35
36
37#define CE_PIN 3
38#define CSN_PIN 3
39
40#else
41
42#define CE_PIN 7
43#define CSN_PIN 8
44
45#endif
46
47#define MAX_HIGH 100
48#define MAX_LOW 100
49#define MINIMAL 8
50
51
52
53
54
55uint8_t csnHighSettle = MAX_HIGH;
56uint8_t csnLowSettle = MAX_LOW;
57
58
59void ce(bool level) {
61}
62
63
64void csn(bool mode) {
65 if (CE_PIN != CSN_PIN) {
67 } else {
68
70 PORTB |= (1 << PINB2);
72 } else {
73 PORTB &= ~(1 << PINB2);
75 }
76 }
77}
78
79
80uint8_t read_register(uint8_t reg) {
82 SPI.transfer(reg);
83 uint8_t result = SPI.transfer(0xff);
85 return result;
86}
87
88
89void write_register(uint8_t reg, uint8_t value) {
92 SPI.transfer(value);
94}
95
96
97void setup(void) {
98
99#ifndef __AVR_ATtinyX313__
100
101
102
103 Serial.begin(115200);
104 SPI.begin();
105
107 if (CSN_PIN != CE_PIN)
109
110
111
112
115
116
117
120
121
122
123
126
127
129 Serial.print("Scanning for optimal setting time for csn");
130
131
132
133
134 uint8_t result;
135 bool success = true;
136 uint8_t bottom_success;
137 bool bottom_found;
138 uint8_t value[] = { 5, 10 };
139 uint8_t limit[] = { MAX_HIGH, MAX_LOW };
140 uint8_t advice[] = { MAX_HIGH, MAX_LOW };
141
142
143 for (uint8_t k = 0; k < 2; k++) {
144 bottom_found = false;
145 bottom_success = 0;
146 while (bottom_success < 255) {
147 csnHighSettle = limit[0];
148 csnLowSettle = limit[1];
149
150 uint8_t i = 0;
151 while (i < 255 && success) {
152 for (uint8_t j = 0; j < 2; j++) {
153 write_register(
EN_AA, value[j]);
154 result = read_register(
EN_AA);
155 if (value[j] != result) {
156 success = false;
157 }
158 }
159 i++;
160 }
161
162 if (!success) {
163 Serial.print("Settle Not OK. csnHigh=");
164 Serial.print(limit[0], DEC);
165 Serial.print(" csnLow=");
166 Serial.println(limit[1], DEC);
167 limit[k]++;
168 bottom_found = true;
169 bottom_success = 0;
170 success = true;
171 } else {
172 Serial.print("Settle OK. csnHigh=");
173 Serial.print(limit[0], DEC);
174 Serial.print(" csnLow=");
175 Serial.println(limit[1], DEC);
176 if (!bottom_found) {
177 limit[k]--;
178 if (limit[k] == MINIMAL) {
179 bottom_found = true;
180 bottom_success = 0;
181 success = true;
182 }
183 } else {
184 bottom_success++;
185 }
186 }
187 }
188 Serial.print("Settle value found for ");
189 if (k == 0) {
190 Serial.print("csnHigh: ");
191 } else {
192 Serial.print("csnLow: ");
193 }
194 Serial.println(limit[k], DEC);
195 advice[k] = limit[k] + (limit[k] / 10);
196 limit[k] = 100;
197 }
198 Serial.print("Advised Settle times are: csnHigh=");
199 Serial.print(advice[0], DEC);
200 Serial.print(" csnLow=");
201 Serial.println(advice[1], DEC);
202
203#endif
204}
205
206
207void loop(void) {}
#define pinMode(pin, direction)
#define delayMicroseconds(usec)
#define digitalWrite(pin, value)