Teensy 4.1 core updated for C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1652 linhas
52KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef _avr_emulation_h_
  31. #define _avr_emulation_h_
  32. #include "kinetis.h"
  33. #include "core_pins.h"
  34. #include "pins_arduino.h"
  35. #ifdef __cplusplus
  36. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  37. #define CONFIG_PULLUP (PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS)
  38. #define CONFIG_NOPULLUP (PORT_PCR_MUX(1))
  39. #if defined(KINETISK)
  40. // bitband addressing for atomic access to data direction register
  41. #define GPIO_SETBIT_ATOMIC(reg, bit) (*(uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)) = 1)
  42. #define GPIO_CLRBIT_ATOMIC(reg, bit) (*(uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)) = 0)
  43. #elif defined(KINETISL)
  44. // bit manipulation engine for atomic access to data direction register
  45. #define GPIO_SETBIT_ATOMIC(reg, bit) (*(uint32_t *)(((uint32_t)&(reg) - 0xF8000000) | 0x480FF000) = 1 << (bit))
  46. #define GPIO_CLRBIT_ATOMIC(reg, bit) (*(uint32_t *)(((uint32_t)&(reg) - 0xF8000000) | 0x440FF000) = ~(1 << (bit)))
  47. #endif
  48. class PORTDemulation
  49. {
  50. public:
  51. inline PORTDemulation & operator = (int val) __attribute__((always_inline)) {
  52. digitalWriteFast(0, (val & (1<<0)));
  53. if (!(CORE_PIN0_DDRREG & CORE_PIN0_BIT))
  54. CORE_PIN0_CONFIG = ((val & (1<<0)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  55. digitalWriteFast(1, (val & (1<<1)));
  56. if (!(CORE_PIN1_DDRREG & CORE_PIN1_BIT))
  57. CORE_PIN1_CONFIG = ((val & (1<<1)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  58. digitalWriteFast(2, (val & (1<<2)));
  59. if (!(CORE_PIN2_DDRREG & CORE_PIN2_BIT))
  60. CORE_PIN2_CONFIG = ((val & (1<<2)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  61. digitalWriteFast(3, (val & (1<<3)));
  62. if (!(CORE_PIN3_DDRREG & CORE_PIN3_BIT))
  63. CORE_PIN3_CONFIG = ((val & (1<<3)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  64. digitalWriteFast(4, (val & (1<<4)));
  65. if (!(CORE_PIN4_DDRREG & CORE_PIN4_BIT))
  66. CORE_PIN4_CONFIG = ((val & (1<<4)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  67. digitalWriteFast(5, (val & (1<<5)));
  68. if (!(CORE_PIN5_DDRREG & CORE_PIN5_BIT))
  69. CORE_PIN5_CONFIG = ((val & (1<<5)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  70. digitalWriteFast(6, (val & (1<<6)));
  71. if (!(CORE_PIN6_DDRREG & CORE_PIN6_BIT))
  72. CORE_PIN6_CONFIG = ((val & (1<<6)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  73. digitalWriteFast(7, (val & (1<<7)));
  74. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT))
  75. CORE_PIN7_CONFIG = ((val & (1<<7)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  76. return *this;
  77. }
  78. inline PORTDemulation & operator |= (int val) __attribute__((always_inline)) {
  79. if (val & (1<<0)) {
  80. digitalWriteFast(0, HIGH);
  81. if (!(CORE_PIN0_DDRREG & CORE_PIN0_BIT)) CORE_PIN0_CONFIG = CONFIG_PULLUP;
  82. }
  83. if (val & (1<<1)) {
  84. digitalWriteFast(1, HIGH);
  85. if (!(CORE_PIN1_DDRREG & CORE_PIN1_BIT)) CORE_PIN1_CONFIG = CONFIG_PULLUP;
  86. }
  87. if (val & (1<<2)) {
  88. digitalWriteFast(2, HIGH);
  89. if (!(CORE_PIN2_DDRREG & CORE_PIN2_BIT)) CORE_PIN2_CONFIG = CONFIG_PULLUP;
  90. }
  91. if (val & (1<<3)) {
  92. digitalWriteFast(3, HIGH);
  93. if (!(CORE_PIN3_DDRREG & CORE_PIN3_BIT)) CORE_PIN3_CONFIG = CONFIG_PULLUP;
  94. }
  95. if (val & (1<<4)) {
  96. digitalWriteFast(4, HIGH);
  97. if (!(CORE_PIN4_DDRREG & CORE_PIN4_BIT)) CORE_PIN4_CONFIG = CONFIG_PULLUP;
  98. }
  99. if (val & (1<<5)) {
  100. digitalWriteFast(5, HIGH);
  101. if (!(CORE_PIN5_DDRREG & CORE_PIN5_BIT)) CORE_PIN5_CONFIG = CONFIG_PULLUP;
  102. }
  103. if (val & (1<<6)) {
  104. digitalWriteFast(6, HIGH);
  105. if (!(CORE_PIN6_DDRREG & CORE_PIN6_BIT)) CORE_PIN6_CONFIG = CONFIG_PULLUP;
  106. }
  107. if (val & (1<<7)) {
  108. digitalWriteFast(7, HIGH);
  109. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN7_CONFIG = CONFIG_PULLUP;
  110. }
  111. return *this;
  112. }
  113. inline PORTDemulation & operator &= (int val) __attribute__((always_inline)) {
  114. if (!(val & (1<<0))) {
  115. digitalWriteFast(0, LOW);
  116. if (!(CORE_PIN0_DDRREG & CORE_PIN0_BIT)) CORE_PIN0_CONFIG = CONFIG_NOPULLUP;
  117. }
  118. if (!(val & (1<<1))) {
  119. digitalWriteFast(1, LOW);
  120. if (!(CORE_PIN1_DDRREG & CORE_PIN1_BIT)) CORE_PIN1_CONFIG = CONFIG_NOPULLUP;
  121. }
  122. if (!(val & (1<<2))) {
  123. digitalWriteFast(2, LOW);
  124. if (!(CORE_PIN2_DDRREG & CORE_PIN2_BIT)) CORE_PIN2_CONFIG = CONFIG_NOPULLUP;
  125. }
  126. if (!(val & (1<<3))) {
  127. digitalWriteFast(3, LOW);
  128. if (!(CORE_PIN3_DDRREG & CORE_PIN3_BIT)) CORE_PIN3_CONFIG = CONFIG_NOPULLUP;
  129. }
  130. if (!(val & (1<<4))) {
  131. digitalWriteFast(4, LOW);
  132. if (!(CORE_PIN4_DDRREG & CORE_PIN4_BIT)) CORE_PIN4_CONFIG = CONFIG_NOPULLUP;
  133. }
  134. if (!(val & (1<<5))) {
  135. digitalWriteFast(5, LOW);
  136. if (!(CORE_PIN5_DDRREG & CORE_PIN5_BIT)) CORE_PIN5_CONFIG = CONFIG_NOPULLUP;
  137. }
  138. if (!(val & (1<<6))) {
  139. digitalWriteFast(6, LOW);
  140. if (!(CORE_PIN6_DDRREG & CORE_PIN6_BIT)) CORE_PIN6_CONFIG = CONFIG_NOPULLUP;
  141. }
  142. if (!(val & (1<<7))) {
  143. digitalWriteFast(7, LOW);
  144. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN7_CONFIG = CONFIG_NOPULLUP;
  145. }
  146. return *this;
  147. }
  148. };
  149. extern PORTDemulation PORTD;
  150. class PINDemulation
  151. {
  152. public:
  153. inline int operator & (int val) const __attribute__((always_inline)) {
  154. int ret = 0;
  155. if ((val & (1<<0)) && digitalReadFast(0)) ret |= (1<<0);
  156. if ((val & (1<<1)) && digitalReadFast(1)) ret |= (1<<1);
  157. if ((val & (1<<2)) && digitalReadFast(2)) ret |= (1<<2);
  158. if ((val & (1<<3)) && digitalReadFast(3)) ret |= (1<<3);
  159. if ((val & (1<<4)) && digitalReadFast(4)) ret |= (1<<4);
  160. if ((val & (1<<5)) && digitalReadFast(5)) ret |= (1<<5);
  161. if ((val & (1<<6)) && digitalReadFast(6)) ret |= (1<<6);
  162. if ((val & (1<<7)) && digitalReadFast(7)) ret |= (1<<7);
  163. return ret;
  164. }
  165. operator int () const __attribute__((always_inline)) {
  166. int ret = 0;
  167. if (digitalReadFast(0)) ret |= (1<<0);
  168. if (digitalReadFast(1)) ret |= (1<<1);
  169. if (digitalReadFast(2)) ret |= (1<<2);
  170. if (digitalReadFast(3)) ret |= (1<<3);
  171. if (digitalReadFast(4)) ret |= (1<<4);
  172. if (digitalReadFast(5)) ret |= (1<<5);
  173. if (digitalReadFast(6)) ret |= (1<<6);
  174. if (digitalReadFast(7)) ret |= (1<<7);
  175. return ret;
  176. }
  177. };
  178. extern PINDemulation PIND;
  179. class DDRDemulation
  180. {
  181. public:
  182. inline DDRDemulation & operator = (int val) __attribute__((always_inline)) {
  183. if (val & (1<<0)) set0(); else clr0();
  184. if (val & (1<<1)) set1(); else clr1();
  185. if (val & (1<<2)) set2(); else clr2();
  186. if (val & (1<<3)) set3(); else clr3();
  187. if (val & (1<<4)) set4(); else clr4();
  188. if (val & (1<<5)) set5(); else clr5();
  189. if (val & (1<<6)) set6(); else clr6();
  190. if (val & (1<<7)) set7(); else clr7();
  191. return *this;
  192. }
  193. inline DDRDemulation & operator |= (int val) __attribute__((always_inline)) {
  194. if (val & (1<<0)) set0();
  195. if (val & (1<<1)) set1();
  196. if (val & (1<<2)) set2();
  197. if (val & (1<<3)) set3();
  198. if (val & (1<<4)) set4();
  199. if (val & (1<<5)) set5();
  200. if (val & (1<<6)) set6();
  201. if (val & (1<<7)) set7();
  202. return *this;
  203. }
  204. inline DDRDemulation & operator &= (int val) __attribute__((always_inline)) {
  205. if (!(val & (1<<0))) clr0();
  206. if (!(val & (1<<1))) clr1();
  207. if (!(val & (1<<2))) clr2();
  208. if (!(val & (1<<3))) clr3();
  209. if (!(val & (1<<4))) clr4();
  210. if (!(val & (1<<5))) clr5();
  211. if (!(val & (1<<6))) clr6();
  212. if (!(val & (1<<7))) clr7();
  213. return *this;
  214. }
  215. private:
  216. inline void set0() __attribute__((always_inline)) {
  217. GPIO_SETBIT_ATOMIC(CORE_PIN0_DDRREG, CORE_PIN0_BIT);
  218. CORE_PIN0_CONFIG = CONFIG_PULLUP;
  219. }
  220. inline void set1() __attribute__((always_inline)) {
  221. GPIO_SETBIT_ATOMIC(CORE_PIN1_DDRREG, CORE_PIN1_BIT);
  222. CORE_PIN1_CONFIG = CONFIG_PULLUP;
  223. }
  224. inline void set2() __attribute__((always_inline)) {
  225. GPIO_SETBIT_ATOMIC(CORE_PIN2_DDRREG, CORE_PIN2_BIT);
  226. CORE_PIN2_CONFIG = CONFIG_PULLUP;
  227. }
  228. inline void set3() __attribute__((always_inline)) {
  229. GPIO_SETBIT_ATOMIC(CORE_PIN3_DDRREG, CORE_PIN3_BIT);
  230. CORE_PIN3_CONFIG = CONFIG_PULLUP;
  231. }
  232. inline void set4() __attribute__((always_inline)) {
  233. GPIO_SETBIT_ATOMIC(CORE_PIN4_DDRREG, CORE_PIN4_BIT);
  234. CORE_PIN4_CONFIG = CONFIG_PULLUP;
  235. }
  236. inline void set5() __attribute__((always_inline)) {
  237. GPIO_SETBIT_ATOMIC(CORE_PIN5_DDRREG, CORE_PIN5_BIT);
  238. CORE_PIN5_CONFIG = CONFIG_PULLUP;
  239. }
  240. inline void set6() __attribute__((always_inline)) {
  241. GPIO_SETBIT_ATOMIC(CORE_PIN6_DDRREG, CORE_PIN6_BIT);
  242. CORE_PIN6_CONFIG = CONFIG_PULLUP;
  243. }
  244. inline void set7() __attribute__((always_inline)) {
  245. GPIO_SETBIT_ATOMIC(CORE_PIN7_DDRREG, CORE_PIN7_BIT);
  246. CORE_PIN7_CONFIG = CONFIG_PULLUP;
  247. }
  248. inline void clr0() __attribute__((always_inline)) {
  249. CORE_PIN0_CONFIG = ((CORE_PIN0_PORTREG & CORE_PIN0_BITMASK)
  250. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  251. GPIO_CLRBIT_ATOMIC(CORE_PIN0_DDRREG, CORE_PIN0_BIT);
  252. }
  253. inline void clr1() __attribute__((always_inline)) {
  254. CORE_PIN1_CONFIG = ((CORE_PIN1_PORTREG & CORE_PIN1_BITMASK)
  255. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  256. GPIO_CLRBIT_ATOMIC(CORE_PIN1_DDRREG, CORE_PIN1_BIT);
  257. }
  258. inline void clr2() __attribute__((always_inline)) {
  259. CORE_PIN2_CONFIG = ((CORE_PIN2_PORTREG & CORE_PIN2_BITMASK)
  260. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  261. GPIO_CLRBIT_ATOMIC(CORE_PIN2_DDRREG, CORE_PIN2_BIT);
  262. }
  263. inline void clr3() __attribute__((always_inline)) {
  264. CORE_PIN3_CONFIG = ((CORE_PIN3_PORTREG & CORE_PIN3_BITMASK)
  265. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  266. GPIO_CLRBIT_ATOMIC(CORE_PIN3_DDRREG, CORE_PIN3_BIT);
  267. }
  268. inline void clr4() __attribute__((always_inline)) {
  269. CORE_PIN4_CONFIG = ((CORE_PIN4_PORTREG & CORE_PIN4_BITMASK)
  270. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  271. GPIO_CLRBIT_ATOMIC(CORE_PIN4_DDRREG, CORE_PIN4_BIT);
  272. }
  273. inline void clr5() __attribute__((always_inline)) {
  274. CORE_PIN5_CONFIG = ((CORE_PIN5_PORTREG & CORE_PIN5_BITMASK)
  275. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  276. GPIO_CLRBIT_ATOMIC(CORE_PIN5_DDRREG, CORE_PIN5_BIT);
  277. }
  278. inline void clr6() __attribute__((always_inline)) {
  279. CORE_PIN6_CONFIG = ((CORE_PIN6_PORTREG & CORE_PIN6_BITMASK)
  280. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  281. GPIO_CLRBIT_ATOMIC(CORE_PIN6_DDRREG, CORE_PIN6_BIT);
  282. }
  283. inline void clr7() __attribute__((always_inline)) {
  284. CORE_PIN7_CONFIG = ((CORE_PIN7_PORTREG & CORE_PIN7_BITMASK)
  285. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  286. GPIO_CLRBIT_ATOMIC(CORE_PIN7_DDRREG, CORE_PIN7_BIT);
  287. }
  288. };
  289. extern DDRDemulation DDRD;
  290. class PORTBemulation
  291. {
  292. public:
  293. inline PORTBemulation & operator = (int val) __attribute__((always_inline)) {
  294. digitalWriteFast(8, (val & (1<<0)));
  295. if (!(CORE_PIN8_DDRREG & CORE_PIN8_BIT))
  296. CORE_PIN8_CONFIG = ((val & (1<<0)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  297. digitalWriteFast(9, (val & (1<<1)));
  298. if (!(CORE_PIN9_DDRREG & CORE_PIN9_BIT))
  299. CORE_PIN9_CONFIG = ((val & (1<<1)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  300. digitalWriteFast(10, (val & (1<<2)));
  301. if (!(CORE_PIN10_DDRREG & CORE_PIN10_BIT))
  302. CORE_PIN10_CONFIG = ((val & (1<<2)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  303. digitalWriteFast(11, (val & (1<<3)));
  304. if (!(CORE_PIN11_DDRREG & CORE_PIN11_BIT))
  305. CORE_PIN11_CONFIG = ((val & (1<<3)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  306. digitalWriteFast(12, (val & (1<<4)));
  307. if (!(CORE_PIN12_DDRREG & CORE_PIN12_BIT))
  308. CORE_PIN12_CONFIG = ((val & (1<<4)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  309. digitalWriteFast(13, (val & (1<<5)));
  310. if (!(CORE_PIN13_DDRREG & CORE_PIN13_BIT))
  311. CORE_PIN13_CONFIG = ((val & (1<<5)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  312. return *this;
  313. }
  314. inline PORTBemulation & operator |= (int val) __attribute__((always_inline)) {
  315. if (val & (1<<0)) {
  316. digitalWriteFast(8, HIGH);
  317. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN8_CONFIG = CONFIG_PULLUP;
  318. }
  319. if (val & (1<<1)) {
  320. digitalWriteFast(9, HIGH);
  321. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN9_CONFIG = CONFIG_PULLUP;
  322. }
  323. if (val & (1<<2)) {
  324. digitalWriteFast(10, HIGH);
  325. if (!(CORE_PIN10_DDRREG & CORE_PIN10_BIT)) CORE_PIN10_CONFIG = CONFIG_PULLUP;
  326. }
  327. if (val & (1<<3)) {
  328. digitalWriteFast(11, HIGH);
  329. if (!(CORE_PIN11_DDRREG & CORE_PIN11_BIT)) CORE_PIN11_CONFIG = CONFIG_PULLUP;
  330. }
  331. if (val & (1<<4)) {
  332. digitalWriteFast(12, HIGH);
  333. if (!(CORE_PIN12_DDRREG & CORE_PIN12_BIT)) CORE_PIN12_CONFIG = CONFIG_PULLUP;
  334. }
  335. if (val & (1<<5)) {
  336. digitalWriteFast(13, HIGH);
  337. if (!(CORE_PIN13_DDRREG & CORE_PIN13_BIT)) CORE_PIN13_CONFIG = CONFIG_PULLUP;
  338. }
  339. return *this;
  340. }
  341. inline PORTBemulation & operator &= (int val) __attribute__((always_inline)) {
  342. if (!(val & (1<<0))) {
  343. digitalWriteFast(8, LOW);
  344. if (!(CORE_PIN8_DDRREG & CORE_PIN8_BIT)) CORE_PIN8_CONFIG = CONFIG_NOPULLUP;
  345. }
  346. if (!(val & (1<<1))) {
  347. digitalWriteFast(9, LOW);
  348. if (!(CORE_PIN9_DDRREG & CORE_PIN9_BIT)) CORE_PIN9_CONFIG = CONFIG_NOPULLUP;
  349. }
  350. if (!(val & (1<<2))) {
  351. digitalWriteFast(10, LOW);
  352. if (!(CORE_PIN10_DDRREG & CORE_PIN10_BIT)) CORE_PIN10_CONFIG = CONFIG_NOPULLUP;
  353. }
  354. if (!(val & (1<<3))) {
  355. digitalWriteFast(11, LOW);
  356. if (!(CORE_PIN11_DDRREG & CORE_PIN11_BIT)) CORE_PIN11_CONFIG = CONFIG_NOPULLUP;
  357. }
  358. if (!(val & (1<<4))) {
  359. digitalWriteFast(12, LOW);
  360. if (!(CORE_PIN12_DDRREG & CORE_PIN12_BIT)) CORE_PIN12_CONFIG = CONFIG_NOPULLUP;
  361. }
  362. if (!(val & (1<<5))) {
  363. digitalWriteFast(13, LOW);
  364. if (!(CORE_PIN13_DDRREG & CORE_PIN13_BIT)) CORE_PIN13_CONFIG = CONFIG_NOPULLUP;
  365. }
  366. return *this;
  367. }
  368. };
  369. extern PORTBemulation PORTB;
  370. class PINBemulation
  371. {
  372. public:
  373. inline int operator & (int val) const __attribute__((always_inline)) {
  374. int ret = 0;
  375. if ((val & (1<<0)) && digitalReadFast(8)) ret |= (1<<0);
  376. if ((val & (1<<1)) && digitalReadFast(9)) ret |= (1<<1);
  377. if ((val & (1<<2)) && digitalReadFast(10)) ret |= (1<<2);
  378. if ((val & (1<<3)) && digitalReadFast(11)) ret |= (1<<3);
  379. if ((val & (1<<4)) && digitalReadFast(12)) ret |= (1<<4);
  380. if ((val & (1<<5)) && digitalReadFast(13)) ret |= (1<<5);
  381. return ret;
  382. }
  383. operator int () const __attribute__((always_inline)) {
  384. int ret = 0;
  385. if (digitalReadFast(8)) ret |= (1<<0);
  386. if (digitalReadFast(9)) ret |= (1<<1);
  387. if (digitalReadFast(10)) ret |= (1<<2);
  388. if (digitalReadFast(11)) ret |= (1<<3);
  389. if (digitalReadFast(12)) ret |= (1<<4);
  390. if (digitalReadFast(13)) ret |= (1<<5);
  391. return ret;
  392. }
  393. };
  394. extern PINBemulation PINB;
  395. class DDRBemulation
  396. {
  397. public:
  398. inline DDRBemulation & operator = (int val) __attribute__((always_inline)) {
  399. if (val & (1<<0)) set0(); else clr0();
  400. if (val & (1<<1)) set1(); else clr1();
  401. if (val & (1<<2)) set2(); else clr2();
  402. if (val & (1<<3)) set3(); else clr3();
  403. if (val & (1<<4)) set4(); else clr4();
  404. if (val & (1<<5)) set5(); else clr5();
  405. return *this;
  406. }
  407. inline DDRBemulation & operator |= (int val) __attribute__((always_inline)) {
  408. if (val & (1<<0)) set0();
  409. if (val & (1<<1)) set1();
  410. if (val & (1<<2)) set2();
  411. if (val & (1<<3)) set3();
  412. if (val & (1<<4)) set4();
  413. if (val & (1<<5)) set5();
  414. return *this;
  415. }
  416. inline DDRBemulation & operator &= (int val) __attribute__((always_inline)) {
  417. if (!(val & (1<<0))) clr0();
  418. if (!(val & (1<<1))) clr1();
  419. if (!(val & (1<<2))) clr2();
  420. if (!(val & (1<<3))) clr3();
  421. if (!(val & (1<<4))) clr4();
  422. if (!(val & (1<<5))) clr5();
  423. return *this;
  424. }
  425. private:
  426. inline void set0() __attribute__((always_inline)) {
  427. GPIO_SETBIT_ATOMIC(CORE_PIN8_DDRREG, CORE_PIN8_BIT);
  428. CORE_PIN8_CONFIG = CONFIG_PULLUP;
  429. }
  430. inline void set1() __attribute__((always_inline)) {
  431. GPIO_SETBIT_ATOMIC(CORE_PIN9_DDRREG, CORE_PIN9_BIT);
  432. CORE_PIN9_CONFIG = CONFIG_PULLUP;
  433. }
  434. inline void set2() __attribute__((always_inline)) {
  435. GPIO_SETBIT_ATOMIC(CORE_PIN10_DDRREG, CORE_PIN10_BIT);
  436. CORE_PIN10_CONFIG = CONFIG_PULLUP;
  437. }
  438. inline void set3() __attribute__((always_inline)) {
  439. GPIO_SETBIT_ATOMIC(CORE_PIN11_DDRREG, CORE_PIN11_BIT);
  440. CORE_PIN11_CONFIG = CONFIG_PULLUP;
  441. }
  442. inline void set4() __attribute__((always_inline)) {
  443. GPIO_SETBIT_ATOMIC(CORE_PIN12_DDRREG, CORE_PIN12_BIT);
  444. CORE_PIN12_CONFIG = CONFIG_PULLUP;
  445. }
  446. inline void set5() __attribute__((always_inline)) {
  447. GPIO_SETBIT_ATOMIC(CORE_PIN13_DDRREG, CORE_PIN13_BIT);
  448. CORE_PIN13_CONFIG = CONFIG_PULLUP;
  449. }
  450. inline void clr0() __attribute__((always_inline)) {
  451. CORE_PIN8_CONFIG = ((CORE_PIN8_PORTREG & CORE_PIN8_BITMASK)
  452. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  453. GPIO_CLRBIT_ATOMIC(CORE_PIN8_DDRREG, CORE_PIN8_BIT);
  454. }
  455. inline void clr1() __attribute__((always_inline)) {
  456. CORE_PIN9_CONFIG = ((CORE_PIN9_PORTREG & CORE_PIN9_BITMASK)
  457. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  458. GPIO_CLRBIT_ATOMIC(CORE_PIN9_DDRREG, CORE_PIN9_BIT);
  459. }
  460. inline void clr2() __attribute__((always_inline)) {
  461. CORE_PIN10_CONFIG = ((CORE_PIN10_PORTREG & CORE_PIN10_BITMASK)
  462. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  463. GPIO_CLRBIT_ATOMIC(CORE_PIN10_DDRREG, CORE_PIN10_BIT);
  464. }
  465. inline void clr3() __attribute__((always_inline)) {
  466. CORE_PIN11_CONFIG = ((CORE_PIN11_PORTREG & CORE_PIN11_BITMASK)
  467. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  468. GPIO_CLRBIT_ATOMIC(CORE_PIN11_DDRREG, CORE_PIN11_BIT);
  469. }
  470. inline void clr4() __attribute__((always_inline)) {
  471. CORE_PIN12_CONFIG = ((CORE_PIN12_PORTREG & CORE_PIN12_BITMASK)
  472. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  473. GPIO_CLRBIT_ATOMIC(CORE_PIN12_DDRREG, CORE_PIN12_BIT);
  474. }
  475. inline void clr5() __attribute__((always_inline)) {
  476. CORE_PIN13_CONFIG = ((CORE_PIN13_PORTREG & CORE_PIN13_BITMASK)
  477. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  478. GPIO_CLRBIT_ATOMIC(CORE_PIN13_DDRREG, CORE_PIN13_BIT);
  479. }
  480. };
  481. extern DDRBemulation DDRB;
  482. class PORTCemulation
  483. {
  484. public:
  485. inline PORTCemulation & operator = (int val) __attribute__((always_inline)) {
  486. digitalWriteFast(14, (val & (1<<0)));
  487. if (!(CORE_PIN14_DDRREG & CORE_PIN14_BIT))
  488. CORE_PIN14_CONFIG = ((val & (1<<0)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  489. digitalWriteFast(15, (val & (1<<1)));
  490. if (!(CORE_PIN15_DDRREG & CORE_PIN15_BIT))
  491. CORE_PIN15_CONFIG = ((val & (1<<1)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  492. digitalWriteFast(16, (val & (1<<2)));
  493. if (!(CORE_PIN16_DDRREG & CORE_PIN16_BIT))
  494. CORE_PIN16_CONFIG = ((val & (1<<2)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  495. digitalWriteFast(17, (val & (1<<3)));
  496. if (!(CORE_PIN17_DDRREG & CORE_PIN17_BIT))
  497. CORE_PIN17_CONFIG = ((val & (1<<3)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  498. digitalWriteFast(18, (val & (1<<4)));
  499. if (!(CORE_PIN18_DDRREG & CORE_PIN18_BIT))
  500. CORE_PIN18_CONFIG = ((val & (1<<4)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  501. digitalWriteFast(19, (val & (1<<5)));
  502. if (!(CORE_PIN19_DDRREG & CORE_PIN19_BIT))
  503. CORE_PIN19_CONFIG = ((val & (1<<5)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  504. return *this;
  505. }
  506. inline PORTCemulation & operator |= (int val) __attribute__((always_inline)) {
  507. if (val & (1<<0)) {
  508. digitalWriteFast(14, HIGH);
  509. if (!(CORE_PIN14_DDRREG & CORE_PIN14_BIT)) CORE_PIN14_CONFIG = CONFIG_PULLUP;
  510. }
  511. if (val & (1<<1)) {
  512. digitalWriteFast(15, HIGH);
  513. if (!(CORE_PIN15_DDRREG & CORE_PIN15_BIT)) CORE_PIN15_CONFIG = CONFIG_PULLUP;
  514. }
  515. if (val & (1<<2)) {
  516. digitalWriteFast(16, HIGH);
  517. if (!(CORE_PIN16_DDRREG & CORE_PIN16_BIT)) CORE_PIN16_CONFIG = CONFIG_PULLUP;
  518. }
  519. if (val & (1<<3)) {
  520. digitalWriteFast(17, HIGH);
  521. if (!(CORE_PIN17_DDRREG & CORE_PIN17_BIT)) CORE_PIN17_CONFIG = CONFIG_PULLUP;
  522. }
  523. if (val & (1<<4)) {
  524. digitalWriteFast(18, HIGH);
  525. if (!(CORE_PIN18_DDRREG & CORE_PIN18_BIT)) CORE_PIN18_CONFIG = CONFIG_PULLUP;
  526. }
  527. if (val & (1<<5)) {
  528. digitalWriteFast(19, HIGH);
  529. if (!(CORE_PIN19_DDRREG & CORE_PIN19_BIT)) CORE_PIN19_CONFIG = CONFIG_PULLUP;
  530. }
  531. return *this;
  532. }
  533. inline PORTCemulation & operator &= (int val) __attribute__((always_inline)) {
  534. if (!(val & (1<<0))) {
  535. digitalWriteFast(14, LOW);
  536. if (!(CORE_PIN14_DDRREG & CORE_PIN14_BIT)) CORE_PIN14_CONFIG = CONFIG_NOPULLUP;
  537. }
  538. if (!(val & (1<<1))) {
  539. digitalWriteFast(15, LOW);
  540. if (!(CORE_PIN15_DDRREG & CORE_PIN15_BIT)) CORE_PIN15_CONFIG = CONFIG_NOPULLUP;
  541. }
  542. if (!(val & (1<<2))) {
  543. digitalWriteFast(16, LOW);
  544. if (!(CORE_PIN16_DDRREG & CORE_PIN16_BIT)) CORE_PIN16_CONFIG = CONFIG_NOPULLUP;
  545. }
  546. if (!(val & (1<<3))) {
  547. digitalWriteFast(17, LOW);
  548. if (!(CORE_PIN17_DDRREG & CORE_PIN17_BIT)) CORE_PIN17_CONFIG = CONFIG_NOPULLUP;
  549. }
  550. if (!(val & (1<<4))) {
  551. digitalWriteFast(18, LOW);
  552. if (!(CORE_PIN18_DDRREG & CORE_PIN18_BIT)) CORE_PIN18_CONFIG = CONFIG_NOPULLUP;
  553. }
  554. if (!(val & (1<<5))) {
  555. digitalWriteFast(19, LOW);
  556. if (!(CORE_PIN19_DDRREG & CORE_PIN19_BIT)) CORE_PIN19_CONFIG = CONFIG_NOPULLUP;
  557. }
  558. return *this;
  559. }
  560. };
  561. extern PORTCemulation PORTC;
  562. class PINCemulation
  563. {
  564. public:
  565. inline int operator & (int val) const __attribute__((always_inline)) {
  566. int ret = 0;
  567. if ((val & (1<<0)) && digitalReadFast(8)) ret |= (1<<0);
  568. if ((val & (1<<1)) && digitalReadFast(9)) ret |= (1<<1);
  569. if ((val & (1<<2)) && digitalReadFast(10)) ret |= (1<<2);
  570. if ((val & (1<<3)) && digitalReadFast(11)) ret |= (1<<3);
  571. if ((val & (1<<4)) && digitalReadFast(12)) ret |= (1<<4);
  572. if ((val & (1<<5)) && digitalReadFast(13)) ret |= (1<<5);
  573. return ret;
  574. }
  575. operator int () const __attribute__((always_inline)) {
  576. int ret = 0;
  577. if (digitalReadFast(8)) ret |= (1<<0);
  578. if (digitalReadFast(9)) ret |= (1<<1);
  579. if (digitalReadFast(10)) ret |= (1<<2);
  580. if (digitalReadFast(11)) ret |= (1<<3);
  581. if (digitalReadFast(12)) ret |= (1<<4);
  582. if (digitalReadFast(13)) ret |= (1<<5);
  583. return ret;
  584. }
  585. };
  586. extern PINCemulation PINC;
  587. class DDRCemulation
  588. {
  589. public:
  590. inline DDRCemulation & operator = (int val) __attribute__((always_inline)) {
  591. if (val & (1<<0)) set0(); else clr0();
  592. if (val & (1<<1)) set1(); else clr1();
  593. if (val & (1<<2)) set2(); else clr2();
  594. if (val & (1<<3)) set3(); else clr3();
  595. if (val & (1<<4)) set4(); else clr4();
  596. if (val & (1<<5)) set5(); else clr5();
  597. return *this;
  598. }
  599. inline DDRCemulation & operator |= (int val) __attribute__((always_inline)) {
  600. if (val & (1<<0)) set0();
  601. if (val & (1<<1)) set1();
  602. if (val & (1<<2)) set2();
  603. if (val & (1<<3)) set3();
  604. if (val & (1<<4)) set4();
  605. if (val & (1<<5)) set5();
  606. return *this;
  607. }
  608. inline DDRCemulation & operator &= (int val) __attribute__((always_inline)) {
  609. if (!(val & (1<<0))) clr0();
  610. if (!(val & (1<<1))) clr1();
  611. if (!(val & (1<<2))) clr2();
  612. if (!(val & (1<<3))) clr3();
  613. if (!(val & (1<<4))) clr4();
  614. if (!(val & (1<<5))) clr5();
  615. return *this;
  616. }
  617. private:
  618. inline void set0() __attribute__((always_inline)) {
  619. GPIO_SETBIT_ATOMIC(CORE_PIN14_DDRREG, CORE_PIN14_BIT);
  620. CORE_PIN14_CONFIG = CONFIG_PULLUP;
  621. }
  622. inline void set1() __attribute__((always_inline)) {
  623. GPIO_SETBIT_ATOMIC(CORE_PIN15_DDRREG, CORE_PIN15_BIT);
  624. CORE_PIN15_CONFIG = CONFIG_PULLUP;
  625. }
  626. inline void set2() __attribute__((always_inline)) {
  627. GPIO_SETBIT_ATOMIC(CORE_PIN16_DDRREG, CORE_PIN16_BIT);
  628. CORE_PIN16_CONFIG = CONFIG_PULLUP;
  629. }
  630. inline void set3() __attribute__((always_inline)) {
  631. GPIO_SETBIT_ATOMIC(CORE_PIN17_DDRREG, CORE_PIN17_BIT);
  632. CORE_PIN17_CONFIG = CONFIG_PULLUP;
  633. }
  634. inline void set4() __attribute__((always_inline)) {
  635. GPIO_SETBIT_ATOMIC(CORE_PIN18_DDRREG, CORE_PIN18_BIT);
  636. CORE_PIN18_CONFIG = CONFIG_PULLUP;
  637. }
  638. inline void set5() __attribute__((always_inline)) {
  639. GPIO_SETBIT_ATOMIC(CORE_PIN19_DDRREG, CORE_PIN19_BIT);
  640. CORE_PIN19_CONFIG = CONFIG_PULLUP;
  641. }
  642. inline void clr0() __attribute__((always_inline)) {
  643. CORE_PIN14_CONFIG = ((CORE_PIN14_PORTREG & CORE_PIN14_BITMASK)
  644. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  645. GPIO_CLRBIT_ATOMIC(CORE_PIN14_DDRREG, CORE_PIN14_BIT);
  646. }
  647. inline void clr1() __attribute__((always_inline)) {
  648. CORE_PIN15_CONFIG = ((CORE_PIN15_PORTREG & CORE_PIN15_BITMASK)
  649. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  650. GPIO_CLRBIT_ATOMIC(CORE_PIN15_DDRREG, CORE_PIN15_BIT);
  651. }
  652. inline void clr2() __attribute__((always_inline)) {
  653. CORE_PIN16_CONFIG = ((CORE_PIN16_PORTREG & CORE_PIN16_BITMASK)
  654. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  655. GPIO_CLRBIT_ATOMIC(CORE_PIN16_DDRREG, CORE_PIN16_BIT);
  656. }
  657. inline void clr3() __attribute__((always_inline)) {
  658. CORE_PIN17_CONFIG = ((CORE_PIN17_PORTREG & CORE_PIN17_BITMASK)
  659. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  660. GPIO_CLRBIT_ATOMIC(CORE_PIN17_DDRREG, CORE_PIN17_BIT);
  661. }
  662. inline void clr4() __attribute__((always_inline)) {
  663. CORE_PIN18_CONFIG = ((CORE_PIN18_PORTREG & CORE_PIN18_BITMASK)
  664. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  665. GPIO_CLRBIT_ATOMIC(CORE_PIN18_DDRREG, CORE_PIN18_BIT);
  666. }
  667. inline void clr5() __attribute__((always_inline)) {
  668. CORE_PIN19_CONFIG = ((CORE_PIN19_PORTREG & CORE_PIN19_BITMASK)
  669. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  670. GPIO_CLRBIT_ATOMIC(CORE_PIN19_DDRREG, CORE_PIN19_BIT);
  671. }
  672. };
  673. extern DDRCemulation DDRC;
  674. #define PINB0 0
  675. #define PINB1 1
  676. #define PINB2 2
  677. #define PINB3 3
  678. #define PINB4 4
  679. #define PINB5 5
  680. #define PINB6 6
  681. #define PINB7 7
  682. #define DDB0 0
  683. #define DDB1 1
  684. #define DDB2 2
  685. #define DDB3 3
  686. #define DDB4 4
  687. #define DDB5 5
  688. #define DDB6 6
  689. #define DDB7 7
  690. #define PORTB0 0
  691. #define PORTB1 1
  692. #define PORTB2 2
  693. #define PORTB3 3
  694. #define PORTB4 4
  695. #define PORTB5 5
  696. #define PORTB6 6
  697. #define PORTB7 7
  698. #define PINC0 0
  699. #define PINC1 1
  700. #define PINC2 2
  701. #define PINC3 3
  702. #define PINC4 4
  703. #define PINC5 5
  704. #define PINC6 6
  705. #define DDC0 0
  706. #define DDC1 1
  707. #define DDC2 2
  708. #define DDC3 3
  709. #define DDC4 4
  710. #define DDC5 5
  711. #define DDC6 6
  712. #define PORTC0 0
  713. #define PORTC1 1
  714. #define PORTC2 2
  715. #define PORTC3 3
  716. #define PORTC4 4
  717. #define PORTC5 5
  718. #define PORTC6 6
  719. #define PIND0 0
  720. #define PIND1 1
  721. #define PIND2 2
  722. #define PIND3 3
  723. #define PIND4 4
  724. #define PIND5 5
  725. #define PIND6 6
  726. #define PIND7 7
  727. #define DDD0 0
  728. #define DDD1 1
  729. #define DDD2 2
  730. #define DDD3 3
  731. #define DDD4 4
  732. #define DDD5 5
  733. #define DDD6 6
  734. #define DDD7 7
  735. #define PORTD0 0
  736. #define PORTD1 1
  737. #define PORTD2 2
  738. #define PORTD3 3
  739. #define PORTD4 4
  740. #define PORTD5 5
  741. #define PORTD6 6
  742. #define PORTD7 7
  743. #if 0
  744. extern "C" {
  745. void serial_print(const char *p);
  746. void serial_phex(uint32_t n);
  747. void serial_phex16(uint32_t n);
  748. void serial_phex32(uint32_t n);
  749. }
  750. #endif
  751. // SPI Control Register ­ SPCR
  752. #define SPIE 7 // SPI Interrupt Enable - not supported
  753. #define SPE 6 // SPI Enable
  754. #define DORD 5 // DORD: Data Order
  755. #define MSTR 4 // MSTR: Master/Slave Select
  756. #define CPOL 3 // CPOL: Clock Polarity
  757. #define CPHA 2 // CPHA: Clock Phase
  758. #define SPR1 1 // Clock: 3 = 125 kHz, 2 = 250 kHz, 1 = 1 MHz, 0->4 MHz
  759. #define SPR0 0
  760. // SPI Status Register ­ SPSR
  761. #define SPIF 7 // SPIF: SPI Interrupt Flag
  762. #define WCOL 6 // WCOL: Write COLlision Flag - not implemented
  763. #define SPI2X 0 // SPI2X: Double SPI Speed Bit
  764. // SPI Data Register ­ SPDR
  765. class SPCRemulation;
  766. class SPSRemulation;
  767. class SPDRemulation;
  768. #if defined(KINETISK)
  769. class SPCRemulation
  770. {
  771. public:
  772. inline SPCRemulation & operator = (int val) __attribute__((always_inline)) {
  773. uint32_t ctar, mcr, sim6;
  774. //serial_print("SPCR=");
  775. //serial_phex(val);
  776. //serial_print("\n");
  777. sim6 = SIM_SCGC6;
  778. if (!(sim6 & SIM_SCGC6_SPI0)) {
  779. //serial_print("init1\n");
  780. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI0;
  781. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  782. }
  783. if (!(val & (1<<SPE))) {
  784. SPI0_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  785. }
  786. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  787. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  788. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  789. if (val & (1<<CPHA)) {
  790. ctar |= SPI_CTAR_CPHA;
  791. if ((val & 3) == 0) {
  792. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  793. } else if ((val & 3) == 1) {
  794. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  795. } else if ((val & 3) == 2) {
  796. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  797. } else {
  798. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  799. }
  800. } else {
  801. if ((val & 3) == 0) {
  802. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  803. } else if ((val & 3) == 1) {
  804. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  805. } else if ((val & 3) == 2) {
  806. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  807. } else {
  808. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  809. }
  810. }
  811. ctar |= (SPI0_CTAR0 & SPI_CTAR_DBR);
  812. update_ctar(ctar);
  813. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  814. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  815. if (val & (1<<SPE)) {
  816. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  817. SPI0_MCR = mcr;
  818. enable_pins();
  819. } else {
  820. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  821. SPI0_MCR = mcr;
  822. disable_pins();
  823. }
  824. //serial_print("MCR:");
  825. //serial_phex32(SPI0_MCR);
  826. //serial_print(", CTAR0:");
  827. //serial_phex32(SPI0_CTAR0);
  828. //serial_print("\n");
  829. return *this;
  830. }
  831. inline SPCRemulation & operator |= (int val) __attribute__((always_inline)) {
  832. uint32_t sim6;
  833. //serial_print("SPCR |= ");
  834. //serial_phex(val);
  835. //serial_print("\n");
  836. sim6 = SIM_SCGC6;
  837. if (!(sim6 & SIM_SCGC6_SPI0)) {
  838. //serial_print("init2\n");
  839. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI0;
  840. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  841. }
  842. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  843. uint32_t ctar = SPI0_CTAR0;
  844. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  845. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  846. if ((val & 3) == 1) {
  847. // TODO: implement - is this ever really needed
  848. } else if ((val & 3) == 2) {
  849. // TODO: implement - is this ever really needed
  850. } else if ((val & 3) == 3) {
  851. // TODO: implement - is this ever really needed
  852. }
  853. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  854. ctar |= SPI_CTAR_CPHA;
  855. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  856. }
  857. update_ctar(ctar);
  858. }
  859. if (val & (1<<MSTR)) SPI0_MCR |= SPI_MCR_MSTR;
  860. if (val & (1<<SPE)) {
  861. SPI0_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  862. enable_pins();
  863. }
  864. //serial_print("MCR:");
  865. //serial_phex32(SPI0_MCR);
  866. //serial_print(", CTAR0:");
  867. //serial_phex32(SPI0_CTAR0);
  868. //serial_print("\n");
  869. return *this;
  870. }
  871. inline SPCRemulation & operator &= (int val) __attribute__((always_inline)) {
  872. //serial_print("SPCR &= ");
  873. //serial_phex(val);
  874. //serial_print("\n");
  875. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  876. if (!(val & (1<<SPE))) {
  877. SPI0_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  878. disable_pins();
  879. }
  880. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  881. uint32_t ctar = SPI0_CTAR0;
  882. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  883. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  884. if ((val & 3) == 0) {
  885. // TODO: implement - is this ever really needed
  886. } else if ((val & 3) == 1) {
  887. // TODO: implement - is this ever really needed
  888. } else if ((val & 3) == 2) {
  889. // TODO: implement - is this ever really needed
  890. }
  891. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  892. ctar &= ~SPI_CTAR_CPHA;
  893. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  894. }
  895. update_ctar(ctar);
  896. }
  897. if (!(val & (1<<MSTR))) SPI0_MCR &= ~SPI_MCR_MSTR;
  898. return *this;
  899. }
  900. inline int operator & (int val) const __attribute__((always_inline)) {
  901. int ret = 0;
  902. //serial_print("SPCR & ");
  903. //serial_phex(val);
  904. //serial_print("\n");
  905. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  906. if ((val & (1<<DORD)) && (SPI0_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  907. if ((val & (1<<CPOL)) && (SPI0_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  908. if ((val & (1<<CPHA)) && (SPI0_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  909. if ((val & 3) == 3) {
  910. uint32_t dbr = SPI0_CTAR0 & 15;
  911. if (dbr <= 1) {
  912. } else if (dbr <= 4) {
  913. ret |= (1<<SPR0);
  914. } else if (dbr <= 6) {
  915. ret |= (1<<SPR1);
  916. } else {
  917. ret |= (1<<SPR1)|(1<<SPR0);
  918. }
  919. } else if ((val & 3) == 1) {
  920. // TODO: implement - is this ever really needed
  921. } else if ((val & 3) == 2) {
  922. // TODO: implement - is this ever really needed
  923. }
  924. if (val & (1<<SPE) && (!(SPI0_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  925. if (val & (1<<MSTR) && (SPI0_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  926. //serial_print("ret = ");
  927. //serial_phex(ret);
  928. //serial_print("\n");
  929. return ret;
  930. }
  931. operator int () const __attribute__((always_inline)) {
  932. int ret = 0;
  933. if ((SIM_SCGC6 & SIM_SCGC6_SPI0)) {
  934. int ctar = SPI0_CTAR0;
  935. if (ctar & SPI_CTAR_LSBFE) ret |= (1<<DORD);
  936. if (ctar & SPI_CTAR_CPOL) ret |= (1<<CPOL);
  937. if (ctar & SPI_CTAR_CPHA) ret |= (1<<CPHA);
  938. ctar &= 15;
  939. if (ctar <= 1) {
  940. } else if (ctar <= 4) {
  941. ret |= (1<<SPR0);
  942. } else if (ctar <= 6) {
  943. ret |= (1<<SPR1);
  944. } else {
  945. ret |= (1<<SPR1)|(1<<SPR0);
  946. }
  947. int mcr = SPI0_MCR;
  948. if (!(mcr & SPI_MCR_MDIS)) ret |= (1<<SPE);
  949. if (mcr & SPI_MCR_MSTR) ret |= (1<<MSTR);
  950. }
  951. return ret;
  952. }
  953. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  954. uint8_t newpinout = pinout;
  955. if (pin == 11) newpinout &= ~1;
  956. if (pin == 7) newpinout |= 1;
  957. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  958. if ((newpinout & 1) == 0) {
  959. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  960. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  961. } else {
  962. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  963. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  964. }
  965. }
  966. pinout = newpinout;
  967. }
  968. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  969. uint8_t newpinout = pinout;
  970. if (pin == 12) newpinout &= ~2;
  971. if (pin == 8) newpinout |= 2;
  972. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  973. if ((newpinout & 2) == 0) {
  974. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  975. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  976. } else {
  977. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  978. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  979. }
  980. }
  981. pinout = newpinout;
  982. }
  983. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  984. uint8_t newpinout = pinout;
  985. if (pin == 13) newpinout &= ~4;
  986. if (pin == 14) newpinout |= 4;
  987. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  988. if ((newpinout & 4) == 0) {
  989. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  990. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  991. } else {
  992. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  993. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  994. }
  995. }
  996. pinout = newpinout;
  997. }
  998. friend class SPSRemulation;
  999. friend class SPIFIFOclass;
  1000. private:
  1001. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1002. if (SPI0_CTAR0 == ctar) return;
  1003. uint32_t mcr = SPI0_MCR;
  1004. if (mcr & SPI_MCR_MDIS) {
  1005. SPI0_CTAR0 = ctar;
  1006. } else {
  1007. SPI0_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1008. SPI0_CTAR0 = ctar;
  1009. SPI0_MCR = mcr;
  1010. }
  1011. }
  1012. static uint8_t pinout;
  1013. public:
  1014. inline void enable_pins(void) __attribute__((always_inline)) {
  1015. //serial_print("enable_pins\n");
  1016. if ((pinout & 1) == 0) {
  1017. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // DOUT/MOSI = 11 (PTC6)
  1018. } else {
  1019. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // DOUT/MOSI = 7 (PTD2)
  1020. }
  1021. if ((pinout & 2) == 0) {
  1022. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // DIN/MISO = 12 (PTC7)
  1023. } else {
  1024. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // DIN/MISO = 8 (PTD3)
  1025. }
  1026. if ((pinout & 4) == 0) {
  1027. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK = 13 (PTC5)
  1028. } else {
  1029. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK = 14 (PTD1)
  1030. }
  1031. }
  1032. inline void disable_pins(void) __attribute__((always_inline)) {
  1033. //serial_print("disable_pins\n");
  1034. if ((pinout & 1) == 0) {
  1035. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1036. } else {
  1037. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1038. }
  1039. if ((pinout & 2) == 0) {
  1040. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1041. } else {
  1042. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1043. }
  1044. if ((pinout & 4) == 0) {
  1045. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1046. } else {
  1047. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1048. }
  1049. }
  1050. };
  1051. extern SPCRemulation SPCR;
  1052. class SPSRemulation
  1053. {
  1054. public:
  1055. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1056. //serial_print("SPSR=");
  1057. //serial_phex(val);
  1058. //serial_print("\n");
  1059. uint32_t ctar = SPI0_CTAR0;
  1060. if (val & (1<<SPI2X)) {
  1061. ctar |= SPI_CTAR_DBR;
  1062. } else {
  1063. ctar &= ~SPI_CTAR_DBR;
  1064. }
  1065. SPCRemulation::update_ctar(ctar);
  1066. //serial_print("MCR:");
  1067. //serial_phex32(SPI0_MCR);
  1068. //serial_print(", CTAR0:");
  1069. //serial_phex32(SPI0_CTAR0);
  1070. //serial_print("\n");
  1071. return *this;
  1072. }
  1073. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1074. //serial_print("SPSR |= ");
  1075. //serial_phex(val);
  1076. //serial_print("\n");
  1077. if (val & (1<<SPI2X)) SPCRemulation::update_ctar(SPI0_CTAR0 |= SPI_CTAR_DBR);
  1078. return *this;
  1079. }
  1080. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1081. //serial_print("SPSR &= ");
  1082. //serial_phex(val);
  1083. //serial_print("\n");
  1084. if (!(val & (1<<SPI2X))) SPCRemulation::update_ctar(SPI0_CTAR0 &= ~SPI_CTAR_DBR);
  1085. return *this;
  1086. }
  1087. inline int operator & (int val) const __attribute__((always_inline)) {
  1088. int ret = 0;
  1089. //serial_print("SPSR & ");
  1090. //serial_phex(val);
  1091. //serial_print("\n");
  1092. // TODO: using SPI_SR_TCF isn't quite right. Control returns to the
  1093. // caller after the final edge that captures data, which is 1/2 cycle
  1094. // sooner than AVR returns. At 500 kHz and slower SPI, this can make
  1095. // a difference when digitalWrite is used to manually control the CS
  1096. // pin, and perhaps it could matter at high clocks if faster register
  1097. // access is used? But does it really matter? Do any SPI chips in
  1098. // practice really perform differently if CS negates early, after the
  1099. // final bit is clocked, but before the end of the whole clock cycle?
  1100. if ((val & (1<<SPIF)) && (SPI0_SR & SPI_SR_TCF)) ret = (1<<SPIF);
  1101. if ((val & (1<<SPI2X)) && (SPI0_CTAR0 & SPI_CTAR_DBR)) ret |= (1<<SPI2X);
  1102. //delayMicroseconds(50000);
  1103. return ret;
  1104. }
  1105. operator int () const __attribute__((always_inline)) {
  1106. int ret = 0;
  1107. //serial_print("SPSR (int)\n");
  1108. if (SPI0_SR & SPI_SR_TCF) ret = (1<<SPIF);
  1109. if (SPI0_CTAR0 & SPI_CTAR_DBR) ret |= (1<<SPI2X);
  1110. return ret;
  1111. }
  1112. };
  1113. extern SPSRemulation SPSR;
  1114. class SPDRemulation
  1115. {
  1116. public:
  1117. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1118. //serial_print("SPDR = ");
  1119. //serial_phex(val);
  1120. //serial_print("\n");
  1121. SPI0_MCR |= SPI_MCR_CLR_RXF; // discard any received data
  1122. SPI0_SR = SPI_SR_TCF;
  1123. //SPI0_SR = SPI_SR_EOQF;
  1124. //SPI0_PUSHR = (val & 255) | SPI0_PUSHR_EOQ;
  1125. SPI0_PUSHR = (val & 255);
  1126. return *this;
  1127. }
  1128. operator int () const __attribute__((always_inline)) {
  1129. uint32_t val;
  1130. val = SPI0_POPR & 255;
  1131. //serial_print("SPDR (int) ");
  1132. //serial_phex(val);
  1133. //serial_print("\n");
  1134. return val;
  1135. }
  1136. };
  1137. extern SPDRemulation SPDR;
  1138. #elif defined(KINETISL)
  1139. // SPI Control Register ­ SPCR
  1140. //#define SPIE 7 // SPI Interrupt Enable - not supported
  1141. //#define SPE 6 // SPI Enable
  1142. //#define DORD 5 // DORD: Data Order
  1143. //#define MSTR 4 // MSTR: Master/Slave Select
  1144. //#define CPOL 3 // CPOL: Clock Polarity
  1145. //#define CPHA 2 // CPHA: Clock Phase
  1146. //#define SPR1 1 // Clock: 3 = 125 kHz, 2 = 250 kHz, 1 = 1 MHz, 0->4 MHz
  1147. //#define SPR0 0
  1148. // SPI Status Register ­ SPSR
  1149. //#define SPIF 7 // SPIF: SPI Interrupt Flag
  1150. //#define WCOL 6 // WCOL: Write COLlision Flag - not implemented
  1151. //#define SPI2X 0 // SPI2X: Double SPI Speed Bit
  1152. // SPI Data Register ­ SPDR
  1153. class SPCRemulation
  1154. {
  1155. public:
  1156. inline SPCRemulation & operator = (int val) __attribute__((always_inline)) {
  1157. uint32_t sim4 = SIM_SCGC4;
  1158. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1159. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1160. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1161. }
  1162. uint32_t c1 = 0;
  1163. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1164. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1165. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1166. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1167. if (val & (1<<SPE)) c1 |= SPI_C1_SPE;
  1168. SPI0_C1 = c1;
  1169. SPI0_C2 = 0;
  1170. uint32_t br = SPI0_BR & 0x10;
  1171. switch (val & 3) {
  1172. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1173. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1174. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1175. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1176. }
  1177. if (val & (1<<SPE)) enable_pins();
  1178. else disable_pins();
  1179. return *this;
  1180. }
  1181. inline SPCRemulation & operator |= (int val) __attribute__((always_inline)) {
  1182. uint32_t sim4 = SIM_SCGC4;
  1183. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1184. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1185. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1186. }
  1187. uint32_t c1 = SPI0_C1;
  1188. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1189. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1190. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1191. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1192. if (val & (1<<SPE)) {
  1193. enable_pins();
  1194. c1 |= SPI_C1_SPE;
  1195. }
  1196. SPI0_C1 = c1;
  1197. SPI0_C2 = 0;
  1198. val &= 3;
  1199. if (val) {
  1200. uint32_t br = SPI0_BR;
  1201. uint32_t bits = baud2avr(br) | val;
  1202. br &= 0x10;
  1203. switch (bits) {
  1204. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1205. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1206. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1207. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1208. }
  1209. }
  1210. return *this;
  1211. }
  1212. inline SPCRemulation & operator &= (int val) __attribute__((always_inline)) {
  1213. uint32_t sim4 = SIM_SCGC4;
  1214. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1215. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1216. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1217. }
  1218. uint32_t c1 = SPI0_C1;
  1219. if (!(val & (1<<DORD))) c1 &= ~SPI_C1_LSBFE;
  1220. if (!(val & (1<<CPOL))) c1 &= ~SPI_C1_CPOL;
  1221. if (!(val & (1<<CPHA))) c1 &= ~SPI_C1_CPHA;
  1222. if (!(val & (1<<MSTR))) c1 &= ~SPI_C1_MSTR;
  1223. if (!(val & (1<<SPE))) {
  1224. disable_pins();
  1225. c1 &= ~SPI_C1_SPE;
  1226. }
  1227. SPI0_C1 = c1;
  1228. SPI0_C2 = 0;
  1229. val &= 3;
  1230. if (val < 3) {
  1231. uint32_t br = SPI0_BR;
  1232. uint32_t bits = baud2avr(br) & val;
  1233. br &= 0x10;
  1234. switch (bits) {
  1235. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1236. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1237. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1238. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1239. }
  1240. }
  1241. return *this;
  1242. }
  1243. inline int operator & (int val) const __attribute__((always_inline)) {
  1244. int ret = 0;
  1245. uint32_t sim4 = SIM_SCGC4;
  1246. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1247. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1248. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1249. }
  1250. uint32_t c1 = SPI0_C1;
  1251. if ((val & (1<<DORD)) && (c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1252. if ((val & (1<<CPOL)) && (c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1253. if ((val & (1<<CPHA)) && (c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1254. if ((val & (1<<MSTR)) && (c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1255. if ((val & (1<<SPE)) && (c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1256. uint32_t bits = baud2avr(SPI0_BR);
  1257. if ((val & (1<<SPR1)) && (bits & (1<<SPR1))) ret |= (1<<SPR1);
  1258. if ((val & (1<<SPR0)) && (bits & (1<<SPR0))) ret |= (1<<SPR0);
  1259. return ret;
  1260. }
  1261. operator int () const __attribute__((always_inline)) {
  1262. int ret = 0;
  1263. uint32_t sim4 = SIM_SCGC4;
  1264. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1265. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1266. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1267. }
  1268. uint32_t c1 = SPI0_C1;
  1269. if ((c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1270. if ((c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1271. if ((c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1272. if ((c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1273. if ((c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1274. ret |= baud2avr(SPI0_BR);
  1275. return ret;
  1276. }
  1277. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1278. uint8_t newpinout = pinout;
  1279. if (pin == 11) newpinout &= ~1;
  1280. if (pin == 7) newpinout |= 1;
  1281. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1282. if ((newpinout & 1) == 0) {
  1283. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1284. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1285. } else {
  1286. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1287. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  1288. }
  1289. }
  1290. pinout = newpinout;
  1291. }
  1292. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1293. uint8_t newpinout = pinout;
  1294. if (pin == 12) newpinout &= ~2;
  1295. if (pin == 8) newpinout |= 2;
  1296. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1297. if ((newpinout & 2) == 0) {
  1298. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1299. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  1300. } else {
  1301. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1302. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  1303. }
  1304. }
  1305. pinout = newpinout;
  1306. }
  1307. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1308. uint8_t newpinout = pinout;
  1309. if (pin == 13) newpinout &= ~4;
  1310. if (pin == 14) newpinout |= 4;
  1311. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1312. if ((newpinout & 4) == 0) {
  1313. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1314. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1315. } else {
  1316. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1317. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  1318. }
  1319. }
  1320. pinout = newpinout;
  1321. }
  1322. friend class SPSRemulation;
  1323. friend class SPIFIFOclass;
  1324. private:
  1325. static inline uint32_t baud2avr(uint32_t br) __attribute__((always_inline)) {
  1326. br &= 15;
  1327. if (br == 0) return 0;
  1328. if (br <= 2) return 1;
  1329. if (br <= 4) return 2;
  1330. return 3;
  1331. }
  1332. static uint8_t pinout;
  1333. public:
  1334. inline void enable_pins(void) __attribute__((always_inline)) {
  1335. //serial_print("enable_pins\n");
  1336. if ((pinout & 1) == 0) {
  1337. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // MOSI0 = 11 (PTC6)
  1338. } else {
  1339. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // MOSI0 = 7 (PTD2)
  1340. }
  1341. if ((pinout & 2) == 0) {
  1342. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // MISO0 = 12 (PTC7)
  1343. } else {
  1344. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // MISO0 = 8 (PTD3)
  1345. }
  1346. if ((pinout & 4) == 0) {
  1347. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK0 = 13 (PTC5)
  1348. } else {
  1349. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK0 = 14 (PTD1)
  1350. }
  1351. }
  1352. inline void disable_pins(void) __attribute__((always_inline)) {
  1353. //serial_print("disable_pins\n");
  1354. if ((pinout & 1) == 0) {
  1355. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1356. } else {
  1357. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1358. }
  1359. if ((pinout & 2) == 0) {
  1360. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1361. } else {
  1362. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1363. }
  1364. if ((pinout & 4) == 0) {
  1365. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1366. } else {
  1367. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1368. }
  1369. }
  1370. };
  1371. extern SPCRemulation SPCR;
  1372. class SPCR1emulation
  1373. {
  1374. public:
  1375. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1376. if (pin == 0) pinout &= ~1; // MOSI1 = 0 (PTB16)
  1377. if (pin == 21) pinout |= 1; // MOSI1 = 21 (PTD6)
  1378. }
  1379. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1380. if (pin == 1) pinout &= ~2; // MISO1 = 1 (PTB17)
  1381. if (pin == 5) pinout |= 2; // MISO1 = 5 (PTD7)
  1382. }
  1383. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1384. // SCK1 = 20 (PTD5) - no alternative pin
  1385. }
  1386. inline void enable_pins(void) __attribute__((always_inline)) {
  1387. //serial_print("enable_pins\n");
  1388. if ((pinout & 1) == 0) {
  1389. CORE_PIN0_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 0 (PTB16)
  1390. } else {
  1391. CORE_PIN21_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 21 (PTD6)
  1392. }
  1393. if ((pinout & 2) == 0) {
  1394. CORE_PIN1_CONFIG = PORT_PCR_MUX(2); // MISO1 = 1 (PTB17)
  1395. } else {
  1396. CORE_PIN5_CONFIG = PORT_PCR_MUX(2); // MISO1 = 5 (PTD7)
  1397. }
  1398. CORE_PIN20_CONFIG = PORT_PCR_MUX(2); // SCK1 = 20 (PTD5)
  1399. }
  1400. inline void disable_pins(void) __attribute__((always_inline)) {
  1401. //serial_print("disable_pins\n");
  1402. if ((pinout & 1) == 0) {
  1403. CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1404. } else {
  1405. CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1406. }
  1407. if ((pinout & 2) == 0) {
  1408. CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1409. } else {
  1410. CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1411. }
  1412. CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1413. }
  1414. friend class SPIFIFO1class;
  1415. private:
  1416. static uint8_t pinout;
  1417. };
  1418. extern SPCR1emulation SPCR1;
  1419. class SPSRemulation
  1420. {
  1421. public:
  1422. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1423. if (val & (1<<SPI2X)) {
  1424. SPI0_BR &= ~0x10;
  1425. } else {
  1426. SPI0_BR |= 0x10;
  1427. }
  1428. return *this;
  1429. }
  1430. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1431. if (val & (1<<SPI2X)) SPI0_BR &= ~0x10;
  1432. return *this;
  1433. }
  1434. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1435. if (!(val & (1<<SPI2X))) SPI0_BR |= 0x10;
  1436. return *this;
  1437. }
  1438. inline int operator & (int val) const __attribute__((always_inline)) {
  1439. int ret = 0;
  1440. if ((val & (1<<SPIF)) && (SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  1441. if ((val & (1<<SPI2X)) && (!(SPI0_BR & 0x10))) ret |= (1<<SPI2X);
  1442. return ret;
  1443. }
  1444. operator int () const __attribute__((always_inline)) {
  1445. int ret = 0;
  1446. if ((SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  1447. if (!(SPI0_BR & 0x10)) ret |= (1<<SPI2X);
  1448. return ret;
  1449. }
  1450. };
  1451. extern SPSRemulation SPSR;
  1452. class SPDRemulation
  1453. {
  1454. public:
  1455. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1456. if ((SPI0_S & SPI_S_SPTEF)) {
  1457. uint32_t tmp __attribute__((unused)) = SPI0_DL;
  1458. }
  1459. SPI0_DL = val;
  1460. return *this;
  1461. }
  1462. operator int () const __attribute__((always_inline)) {
  1463. return SPI0_DL & 255;
  1464. }
  1465. };
  1466. extern SPDRemulation SPDR;
  1467. #endif // KINETISK
  1468. class SREGemulation
  1469. {
  1470. public:
  1471. operator int () const __attribute__((always_inline)) {
  1472. uint32_t primask;
  1473. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  1474. if (primask) return 0;
  1475. return (1<<7);
  1476. }
  1477. inline SREGemulation & operator = (int val) __attribute__((always_inline)) {
  1478. if (val & (1<<7)) {
  1479. __enable_irq();
  1480. } else {
  1481. __disable_irq();
  1482. }
  1483. return *this;
  1484. }
  1485. };
  1486. extern SREGemulation SREG;
  1487. // 22211
  1488. // 84062840
  1489. // 322111
  1490. // 17395173
  1491. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1492. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  1493. #define EIMSK_pA 0x01000018 // pins 3, 4, 24
  1494. #define EIMSK_pB 0x020F0003 // pins 0, 1, 16-19, 25
  1495. #define EIMSK_pC 0x78C0BE00 // pins 9-13, 15, 22, 23, 27-30
  1496. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  1497. #define EIMSK_pE 0x84000000 // pins 26, 31
  1498. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1499. #define EIMSK_pA 0x1E000018 // pins 3, 4, 25-28
  1500. #define EIMSK_pB 0xE00F0003 // pins 0, 1, 16-19, 29-31
  1501. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  1502. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  1503. #define EIMSK_pE 0x01000000 // pins 24
  1504. #endif
  1505. class EIMSKemulation // used by Adafruit_nRF8001 (only supports INT for pins 0 to 31)
  1506. {
  1507. public:
  1508. operator int () const __attribute__((always_inline)) {
  1509. int mask = 0;
  1510. volatile const uint32_t *icer = &NVIC_ICER0;
  1511. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  1512. if (icer[IRQ_PORTB >> 5] & (1 << (IRQ_PORTB & 31))) mask |= EIMSK_pB;
  1513. if (icer[IRQ_PORTC >> 5] & (1 << (IRQ_PORTC & 31))) mask |= EIMSK_pC;
  1514. if (icer[IRQ_PORTD >> 5] & (1 << (IRQ_PORTD & 31))) mask |= EIMSK_pD;
  1515. if (icer[IRQ_PORTE >> 5] & (1 << (IRQ_PORTE & 31))) mask |= EIMSK_pE;
  1516. return mask;
  1517. }
  1518. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  1519. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  1520. if (val & EIMSK_pB) NVIC_ENABLE_IRQ(IRQ_PORTB);
  1521. if (val & EIMSK_pC) NVIC_ENABLE_IRQ(IRQ_PORTC);
  1522. if (val & EIMSK_pD) NVIC_ENABLE_IRQ(IRQ_PORTD);
  1523. if (val & EIMSK_pE) NVIC_ENABLE_IRQ(IRQ_PORTE);
  1524. return *this;
  1525. }
  1526. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  1527. uint32_t n = val;
  1528. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  1529. if ((n | ~EIMSK_pB) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTB);
  1530. if ((n | ~EIMSK_pC) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTC);
  1531. if ((n | ~EIMSK_pD) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTD);
  1532. if ((n | ~EIMSK_pE) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTE);
  1533. return *this;
  1534. }
  1535. };
  1536. extern EIMSKemulation EIMSK;
  1537. #elif defined(__MKL26Z64__)
  1538. #define EIMSK_pA 0x00000018 // pins 3, 4, 24
  1539. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  1540. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  1541. class EIMSKemulation // used by Adafruit_nRF8001
  1542. {
  1543. public:
  1544. operator int () const __attribute__((always_inline)) {
  1545. int mask = 0;
  1546. volatile const uint32_t *icer = &NVIC_ICER0;
  1547. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  1548. if (icer[IRQ_PORTCD >> 5] & (1 << (IRQ_PORTCD & 31))) mask |= (EIMSK_pC | EIMSK_pD);
  1549. return mask;
  1550. }
  1551. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  1552. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  1553. if (val & (EIMSK_pC | EIMSK_pD)) NVIC_ENABLE_IRQ(IRQ_PORTCD);
  1554. return *this;
  1555. }
  1556. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  1557. uint32_t n = val;
  1558. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  1559. if ((n | ~(EIMSK_pC | EIMSK_pD)) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTCD);
  1560. return *this;
  1561. }
  1562. };
  1563. extern EIMSKemulation EIMSK;
  1564. #endif
  1565. // these are not intended for public consumption...
  1566. #undef GPIO_BITBAND_ADDR
  1567. #undef CONFIG_PULLUP
  1568. #undef CONFIG_NOPULLUP
  1569. #undef GPIO_SETBIT_ATOMIC
  1570. #undef GPIO_CLRBIT_ATOMIC
  1571. #endif // __cplusplus
  1572. #endif