Teensy 4.1 core updated for C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
8 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
9 лет назад
9 лет назад
10 лет назад
10 лет назад
9 лет назад
9 лет назад
10 лет назад
9 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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(14)) ret |= (1<<0);
  568. if ((val & (1<<1)) && digitalReadFast(15)) ret |= (1<<1);
  569. if ((val & (1<<2)) && digitalReadFast(16)) ret |= (1<<2);
  570. if ((val & (1<<3)) && digitalReadFast(17)) ret |= (1<<3);
  571. if ((val & (1<<4)) && digitalReadFast(18)) ret |= (1<<4);
  572. if ((val & (1<<5)) && digitalReadFast(19)) ret |= (1<<5);
  573. return ret;
  574. }
  575. operator int () const __attribute__((always_inline)) {
  576. int ret = 0;
  577. if (digitalReadFast(14)) ret |= (1<<0);
  578. if (digitalReadFast(15)) ret |= (1<<1);
  579. if (digitalReadFast(15)) ret |= (1<<2);
  580. if (digitalReadFast(17)) ret |= (1<<3);
  581. if (digitalReadFast(18)) ret |= (1<<4);
  582. if (digitalReadFast(19)) 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. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  955. uint8_t newpinout = pinout;
  956. // More than two options so now 2 bits
  957. if (pin == 11) newpinout &= ~3;
  958. if (pin == 7) newpinout =(newpinout & ~0x3) | 1;
  959. if (pin == 28) newpinout = (newpinout & ~0x3) | 2;
  960. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  961. // First unconfigure previous pin
  962. switch (pinout & 3) {
  963. case 0: CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  964. case 1: CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  965. default: CORE_PIN28_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  966. }
  967. switch (newpinout & 3) {
  968. case 0: CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); break;
  969. case 1: CORE_PIN7_CONFIG = PORT_PCR_MUX(2); break;
  970. default: CORE_PIN28_CONFIG = PORT_PCR_MUX(2);
  971. }
  972. }
  973. pinout = newpinout;
  974. #else
  975. uint8_t newpinout = pinout;
  976. if (pin == 11) newpinout &= ~1;
  977. if (pin == 7) newpinout |= 1;
  978. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  979. if ((newpinout & 1) == 0) {
  980. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  981. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  982. } else {
  983. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  984. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  985. }
  986. }
  987. pinout = newpinout;
  988. #endif
  989. }
  990. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  991. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  992. uint8_t newpinout = pinout;
  993. // More than two options so now 2 bits
  994. if (pin == 12) newpinout &= ~0xc;
  995. if (pin == 8) newpinout =(newpinout & ~0xc) | 4;
  996. if (pin == 39) newpinout = (newpinout & ~0xc) | 8;
  997. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  998. // First unconfigure previous pin
  999. switch (pinout & 0xc) {
  1000. case 0: CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1001. case 0x4: CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1002. default: CORE_PIN39_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1003. }
  1004. switch (newpinout & 0xc) {
  1005. case 0: CORE_PIN12_CONFIG = PORT_PCR_MUX(2); break;
  1006. case 0x4: CORE_PIN8_CONFIG = PORT_PCR_MUX(2); break;
  1007. default: CORE_PIN39_CONFIG = PORT_PCR_MUX(2);
  1008. }
  1009. }
  1010. pinout = newpinout;
  1011. #else
  1012. uint8_t newpinout = pinout;
  1013. if (pin == 12) newpinout &= ~2;
  1014. if (pin == 8) newpinout |= 2;
  1015. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  1016. if ((newpinout & 2) == 0) {
  1017. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1018. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  1019. } else {
  1020. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1021. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  1022. }
  1023. }
  1024. pinout = newpinout;
  1025. #endif
  1026. }
  1027. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1028. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1029. uint8_t newpinout = pinout;
  1030. // More than two options so now 2 bits
  1031. if (pin == 13) newpinout &= ~0x30;
  1032. if (pin == 14) newpinout =(newpinout & ~0x30) | 0x10;
  1033. if (pin == 27) newpinout = (newpinout & ~0x30) | 0x20;
  1034. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  1035. // First unconfigure previous pin
  1036. switch (pinout & 0x30) {
  1037. case 0: CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1038. case 0x10: CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1039. default: CORE_PIN27_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1040. }
  1041. switch (newpinout & 0x30) {
  1042. case 0: CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); break;
  1043. case 0x10: CORE_PIN14_CONFIG = PORT_PCR_MUX(2); break;
  1044. default: CORE_PIN27_CONFIG = PORT_PCR_MUX(2);
  1045. }
  1046. }
  1047. pinout = newpinout;
  1048. #else
  1049. uint8_t newpinout = pinout;
  1050. if (pin == 13) newpinout &= ~4;
  1051. if (pin == 14) newpinout |= 4;
  1052. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  1053. if ((newpinout & 4) == 0) {
  1054. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1055. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1056. } else {
  1057. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1058. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  1059. }
  1060. }
  1061. pinout = newpinout;
  1062. #endif
  1063. }
  1064. friend class SPSRemulation;
  1065. friend class SPIFIFOclass;
  1066. private:
  1067. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1068. if (SPI0_CTAR0 == ctar) return;
  1069. uint32_t mcr = SPI0_MCR;
  1070. if (mcr & SPI_MCR_MDIS) {
  1071. SPI0_CTAR0 = ctar;
  1072. } else {
  1073. SPI0_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1074. SPI0_CTAR0 = ctar;
  1075. SPI0_MCR = mcr;
  1076. }
  1077. }
  1078. static uint8_t pinout;
  1079. public:
  1080. inline void enable_pins(void) __attribute__((always_inline)) {
  1081. //serial_print("enable_pins\n");
  1082. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1083. switch (pinout & 3) {
  1084. case 0: CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); break;
  1085. case 1: CORE_PIN7_CONFIG = PORT_PCR_MUX(2); break;
  1086. default: CORE_PIN28_CONFIG = PORT_PCR_MUX(2);
  1087. }
  1088. switch (pinout & 0xc) {
  1089. case 0: CORE_PIN12_CONFIG = PORT_PCR_MUX(2); break;
  1090. case 0x4: CORE_PIN8_CONFIG = PORT_PCR_MUX(2); break;
  1091. default: CORE_PIN39_CONFIG = PORT_PCR_MUX(2);
  1092. }
  1093. switch (pinout & 0x30) {
  1094. case 0: CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); break;
  1095. case 0x10: CORE_PIN14_CONFIG = PORT_PCR_MUX(2); break;
  1096. default: CORE_PIN27_CONFIG = PORT_PCR_MUX(2);
  1097. }
  1098. #else
  1099. if ((pinout & 1) == 0) {
  1100. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // DOUT/MOSI = 11 (PTC6)
  1101. } else {
  1102. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // DOUT/MOSI = 7 (PTD2)
  1103. }
  1104. if ((pinout & 2) == 0) {
  1105. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // DIN/MISO = 12 (PTC7)
  1106. } else {
  1107. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // DIN/MISO = 8 (PTD3)
  1108. }
  1109. if ((pinout & 4) == 0) {
  1110. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK = 13 (PTC5)
  1111. } else {
  1112. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK = 14 (PTD1)
  1113. }
  1114. #endif
  1115. }
  1116. inline void disable_pins(void) __attribute__((always_inline)) {
  1117. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1118. switch (pinout & 3) {
  1119. case 0: CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1120. case 1: CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1121. default: CORE_PIN28_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1122. }
  1123. switch (pinout & 0xc) {
  1124. case 0: CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1125. case 0x4: CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1126. default: CORE_PIN39_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1127. }
  1128. switch (pinout & 0x30) {
  1129. case 0: CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1130. case 0x10: CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); break;
  1131. default: CORE_PIN27_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1132. }
  1133. #else
  1134. //serial_print("disable_pins\n");
  1135. if ((pinout & 1) == 0) {
  1136. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1137. } else {
  1138. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1139. }
  1140. if ((pinout & 2) == 0) {
  1141. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1142. } else {
  1143. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1144. }
  1145. if ((pinout & 4) == 0) {
  1146. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1147. } else {
  1148. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1149. }
  1150. #endif
  1151. }
  1152. };
  1153. extern SPCRemulation SPCR;
  1154. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1155. class SPCR1emulation
  1156. {
  1157. public:
  1158. inline SPCR1emulation & operator = (int val) __attribute__((always_inline)) {
  1159. uint32_t ctar, mcr, sim6;
  1160. //serial_print("SPCR=");
  1161. //serial_phex(val);
  1162. //serial_print("\n");
  1163. sim6 = SIM_SCGC6;
  1164. if (!(sim6 & SIM_SCGC6_SPI1)) {
  1165. //serial_print("init1\n");
  1166. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI1;
  1167. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1168. }
  1169. if (!(val & (1<<SPE))) {
  1170. SPI1_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  1171. }
  1172. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  1173. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  1174. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1175. if (val & (1<<CPHA)) {
  1176. ctar |= SPI_CTAR_CPHA;
  1177. if ((val & 3) == 0) {
  1178. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  1179. } else if ((val & 3) == 1) {
  1180. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  1181. } else if ((val & 3) == 2) {
  1182. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  1183. } else {
  1184. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  1185. }
  1186. } else {
  1187. if ((val & 3) == 0) {
  1188. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1189. } else if ((val & 3) == 1) {
  1190. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  1191. } else if ((val & 3) == 2) {
  1192. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  1193. } else {
  1194. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  1195. }
  1196. }
  1197. ctar |= (SPI1_CTAR0 & SPI_CTAR_DBR);
  1198. update_ctar(ctar);
  1199. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  1200. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  1201. if (val & (1<<SPE)) {
  1202. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1203. SPI1_MCR = mcr;
  1204. enable_pins();
  1205. } else {
  1206. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1207. SPI1_MCR = mcr;
  1208. disable_pins();
  1209. }
  1210. //serial_print("MCR:");
  1211. //serial_phex32(SPI1_MCR);
  1212. //serial_print(", CTAR0:");
  1213. //serial_phex32(SPI1_CTAR0);
  1214. //serial_print("\n");
  1215. return *this;
  1216. }
  1217. inline SPCR1emulation & operator |= (int val) __attribute__((always_inline)) {
  1218. uint32_t sim6;
  1219. //serial_print("SPCR |= ");
  1220. //serial_phex(val);
  1221. //serial_print("\n");
  1222. sim6 = SIM_SCGC6;
  1223. if (!(sim6 & SIM_SCGC6_SPI1)) {
  1224. //serial_print("init2\n");
  1225. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI1;
  1226. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  1227. }
  1228. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1229. uint32_t ctar = SPI1_CTAR0;
  1230. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  1231. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1232. if ((val & 3) == 1) {
  1233. // TODO: implement - is this ever really needed
  1234. } else if ((val & 3) == 2) {
  1235. // TODO: implement - is this ever really needed
  1236. } else if ((val & 3) == 3) {
  1237. // TODO: implement - is this ever really needed
  1238. }
  1239. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  1240. ctar |= SPI_CTAR_CPHA;
  1241. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  1242. }
  1243. update_ctar(ctar);
  1244. }
  1245. if (val & (1<<MSTR)) SPI1_MCR |= SPI_MCR_MSTR;
  1246. if (val & (1<<SPE)) {
  1247. SPI1_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1248. enable_pins();
  1249. }
  1250. //serial_print("MCR:");
  1251. //serial_phex32(SPI1_MCR);
  1252. //serial_print(", CTAR0:");
  1253. //serial_phex32(SPI1_CTAR0);
  1254. //serial_print("\n");
  1255. return *this;
  1256. }
  1257. inline SPCR1emulation & operator &= (int val) __attribute__((always_inline)) {
  1258. //serial_print("SPCR &= ");
  1259. //serial_phex(val);
  1260. //serial_print("\n");
  1261. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  1262. if (!(val & (1<<SPE))) {
  1263. SPI1_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1264. disable_pins();
  1265. }
  1266. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1267. uint32_t ctar = SPI1_CTAR0;
  1268. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  1269. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  1270. if ((val & 3) == 0) {
  1271. // TODO: implement - is this ever really needed
  1272. } else if ((val & 3) == 1) {
  1273. // TODO: implement - is this ever really needed
  1274. } else if ((val & 3) == 2) {
  1275. // TODO: implement - is this ever really needed
  1276. }
  1277. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  1278. ctar &= ~SPI_CTAR_CPHA;
  1279. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  1280. }
  1281. update_ctar(ctar);
  1282. }
  1283. if (!(val & (1<<MSTR))) SPI1_MCR &= ~SPI_MCR_MSTR;
  1284. return *this;
  1285. }
  1286. inline int operator & (int val) const __attribute__((always_inline)) {
  1287. int ret = 0;
  1288. //serial_print("SPCR & ");
  1289. //serial_phex(val);
  1290. //serial_print(" MCR:");
  1291. //serial_phex32(SPI1_MCR);
  1292. //serial_print(", CTAR0:");
  1293. //serial_phex32(SPI1_CTAR0);
  1294. //serial_print("\n");
  1295. //serial_print("\n");
  1296. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  1297. if ((val & (1<<DORD)) && (SPI1_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  1298. if ((val & (1<<CPOL)) && (SPI1_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  1299. if ((val & (1<<CPHA)) && (SPI1_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  1300. if ((val & 3) == 3) {
  1301. uint32_t dbr = SPI1_CTAR0 & 15;
  1302. if (dbr <= 1) {
  1303. } else if (dbr <= 4) {
  1304. ret |= (1<<SPR0);
  1305. } else if (dbr <= 6) {
  1306. ret |= (1<<SPR1);
  1307. } else {
  1308. ret |= (1<<SPR1)|(1<<SPR0);
  1309. }
  1310. } else if ((val & 3) == 1) {
  1311. // TODO: implement - is this ever really needed
  1312. } else if ((val & 3) == 2) {
  1313. // TODO: implement - is this ever really needed
  1314. }
  1315. if (val & (1<<SPE) && (!(SPI1_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  1316. if (val & (1<<MSTR) && (SPI1_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  1317. //serial_print("ret = ");
  1318. //serial_phex(ret);
  1319. //serial_print("\n");
  1320. return ret;
  1321. }
  1322. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1323. // More options, so 2 bits
  1324. pinout &= ~3;
  1325. switch (pin) {
  1326. case 0: break;
  1327. case 21: pinout |= 1; break;
  1328. case 61: pinout |= 2; break;
  1329. case 59: pinout |= 3; break;
  1330. }
  1331. }
  1332. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1333. // More options, so 2 bits
  1334. pinout &= ~0xc;
  1335. switch (pin) {
  1336. case 1: break;
  1337. case 5: pinout |= 0x4; break;
  1338. case 61: pinout |= 0x8; break;
  1339. case 59: pinout |= 0xc; break;
  1340. }
  1341. }
  1342. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1343. // More options, so 2 bits
  1344. pinout &= ~0x30;
  1345. switch (pin) {
  1346. case 32: break;
  1347. case 20: pinout |= 0x10; break;
  1348. case 60: pinout |= 0x20; break;
  1349. }
  1350. }
  1351. inline void enable_pins(void) __attribute__((always_inline)) {
  1352. //serial_print("enable_pins\n");
  1353. // MOSI (SOUT)
  1354. switch (pinout & 0x3) {
  1355. case 0: CORE_PIN0_CONFIG = PORT_PCR_MUX(2); break;
  1356. case 1: CORE_PIN21_CONFIG = PORT_PCR_MUX(7); break;
  1357. case 2: CORE_PIN61_CONFIG = PORT_PCR_MUX(7); break;
  1358. case 3: CORE_PIN59_CONFIG = PORT_PCR_MUX(2); break;
  1359. }
  1360. // MISO (SIN)
  1361. switch (pinout & 0xc) {
  1362. case 0x0: CORE_PIN1_CONFIG = PORT_PCR_MUX(2); break;
  1363. case 0x4: CORE_PIN5_CONFIG = PORT_PCR_MUX(7); break;
  1364. case 0x8: CORE_PIN61_CONFIG = PORT_PCR_MUX(2); break;
  1365. case 0xc: CORE_PIN59_CONFIG = PORT_PCR_MUX(7); break;
  1366. }
  1367. // SCK
  1368. switch (pinout & 0x30) {
  1369. case 0x0: CORE_PIN32_CONFIG = PORT_PCR_MUX(2); break;
  1370. case 0x10: CORE_PIN20_CONFIG = PORT_PCR_MUX(7); break;
  1371. case 0x20: CORE_PIN60_CONFIG = PORT_PCR_MUX(2); break;
  1372. }
  1373. }
  1374. inline void disable_pins(void) __attribute__((always_inline)) {
  1375. switch (pinout & 0x3) {
  1376. case 0: CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1377. case 1: CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1378. case 2: CORE_PIN61_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1379. case 3: CORE_PIN59_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1380. }
  1381. switch (pinout & 0xc) {
  1382. case 0x0: CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1383. case 0x4: CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1384. case 0x8: CORE_PIN61_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1385. case 0xc: CORE_PIN59_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1386. }
  1387. switch (pinout & 0x30) {
  1388. case 0x0: CORE_PIN32_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1389. case 0x10: CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1390. case 0x20: CORE_PIN60_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1391. }
  1392. }
  1393. friend class SPIFIFO1class;
  1394. private:
  1395. static uint8_t pinout;
  1396. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1397. if (SPI1_CTAR0 == ctar) return;
  1398. uint32_t mcr = SPI1_MCR;
  1399. if (mcr & SPI_MCR_MDIS) {
  1400. SPI1_CTAR0 = ctar;
  1401. } else {
  1402. SPI1_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1403. SPI1_CTAR0 = ctar;
  1404. SPI1_MCR = mcr;
  1405. }
  1406. }
  1407. };
  1408. extern SPCR1emulation SPCR1;
  1409. ////////////////////
  1410. // SPI2
  1411. class SPCR2emulation
  1412. {
  1413. public:
  1414. inline SPCR2emulation & operator = (int val) __attribute__((always_inline)) {
  1415. uint32_t ctar, mcr, sim3;
  1416. //serial_print("SPCR=");
  1417. //serial_phex(val);
  1418. //serial_print("\n");
  1419. sim3 = SIM_SCGC3;
  1420. if (!(sim3 & SIM_SCGC3_SPI2)) {
  1421. //serial_print("init1\n");
  1422. SIM_SCGC3 = sim3 | SIM_SCGC3_SPI2;
  1423. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1424. }
  1425. if (!(val & (1<<SPE))) {
  1426. SPI2_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  1427. }
  1428. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  1429. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  1430. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1431. if (val & (1<<CPHA)) {
  1432. ctar |= SPI_CTAR_CPHA;
  1433. if ((val & 3) == 0) {
  1434. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  1435. } else if ((val & 3) == 1) {
  1436. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  1437. } else if ((val & 3) == 2) {
  1438. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  1439. } else {
  1440. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  1441. }
  1442. } else {
  1443. if ((val & 3) == 0) {
  1444. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1445. } else if ((val & 3) == 1) {
  1446. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  1447. } else if ((val & 3) == 2) {
  1448. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  1449. } else {
  1450. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  1451. }
  1452. }
  1453. ctar |= (SPI2_CTAR0 & SPI_CTAR_DBR);
  1454. update_ctar(ctar);
  1455. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  1456. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  1457. if (val & (1<<SPE)) {
  1458. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1459. SPI2_MCR = mcr;
  1460. enable_pins();
  1461. } else {
  1462. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1463. SPI2_MCR = mcr;
  1464. disable_pins();
  1465. }
  1466. //serial_print("MCR:");
  1467. //serial_phex32(SPI2_MCR);
  1468. //serial_print(", CTAR0:");
  1469. //serial_phex32(SPI2_CTAR0);
  1470. //serial_print("\n");
  1471. return *this;
  1472. }
  1473. inline SPCR2emulation & operator |= (int val) __attribute__((always_inline)) {
  1474. uint32_t sim3;
  1475. //serial_print("SPCR |= ");
  1476. //serial_phex(val);
  1477. //serial_print("\n");
  1478. sim3 = SIM_SCGC3;
  1479. if (!(sim3 & SIM_SCGC3_SPI2)) {
  1480. //serial_print("init2\n");
  1481. SIM_SCGC6 = sim3 | SIM_SCGC3_SPI2;
  1482. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  1483. }
  1484. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1485. uint32_t ctar = SPI2_CTAR0;
  1486. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  1487. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1488. if ((val & 3) == 1) {
  1489. // TODO: implement - is this ever really needed
  1490. } else if ((val & 3) == 2) {
  1491. // TODO: implement - is this ever really needed
  1492. } else if ((val & 3) == 3) {
  1493. // TODO: implement - is this ever really needed
  1494. }
  1495. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  1496. ctar |= SPI_CTAR_CPHA;
  1497. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  1498. }
  1499. update_ctar(ctar);
  1500. }
  1501. if (val & (1<<MSTR)) SPI2_MCR |= SPI_MCR_MSTR;
  1502. if (val & (1<<SPE)) {
  1503. SPI2_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1504. enable_pins();
  1505. }
  1506. //serial_print("MCR:");
  1507. //serial_phex32(SPI2_MCR);
  1508. //serial_print(", CTAR0:");
  1509. //serial_phex32(SPI2_CTAR0);
  1510. //serial_print("\n");
  1511. return *this;
  1512. }
  1513. inline SPCR2emulation & operator &= (int val) __attribute__((always_inline)) {
  1514. //serial_print("SPCR &= ");
  1515. //serial_phex(val);
  1516. //serial_print("\n");
  1517. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  1518. if (!(val & (1<<SPE))) {
  1519. SPI2_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1520. disable_pins();
  1521. }
  1522. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1523. uint32_t ctar = SPI2_CTAR0;
  1524. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  1525. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  1526. if ((val & 3) == 0) {
  1527. // TODO: implement - is this ever really needed
  1528. } else if ((val & 3) == 1) {
  1529. // TODO: implement - is this ever really needed
  1530. } else if ((val & 3) == 2) {
  1531. // TODO: implement - is this ever really needed
  1532. }
  1533. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  1534. ctar &= ~SPI_CTAR_CPHA;
  1535. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  1536. }
  1537. update_ctar(ctar);
  1538. }
  1539. if (!(val & (1<<MSTR))) SPI2_MCR &= ~SPI_MCR_MSTR;
  1540. return *this;
  1541. }
  1542. inline int operator & (int val) const __attribute__((always_inline)) {
  1543. int ret = 0;
  1544. //serial_print("SPCR & ");
  1545. //serial_phex(val);
  1546. //serial_print(" MCR:");
  1547. //serial_phex32(SPI2_MCR);
  1548. //serial_print(", CTAR0:");
  1549. //serial_phex32(SPI2_CTAR0);
  1550. //serial_print("\n");
  1551. //serial_print("\n");
  1552. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  1553. if ((val & (1<<DORD)) && (SPI2_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  1554. if ((val & (1<<CPOL)) && (SPI2_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  1555. if ((val & (1<<CPHA)) && (SPI2_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  1556. if ((val & 3) == 3) {
  1557. uint32_t dbr = SPI2_CTAR0 & 15;
  1558. if (dbr <= 1) {
  1559. } else if (dbr <= 4) {
  1560. ret |= (1<<SPR0);
  1561. } else if (dbr <= 6) {
  1562. ret |= (1<<SPR1);
  1563. } else {
  1564. ret |= (1<<SPR1)|(1<<SPR0);
  1565. }
  1566. } else if ((val & 3) == 1) {
  1567. // TODO: implement - is this ever really needed
  1568. } else if ((val & 3) == 2) {
  1569. // TODO: implement - is this ever really needed
  1570. }
  1571. if (val & (1<<SPE) && (!(SPI2_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  1572. if (val & (1<<MSTR) && (SPI2_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  1573. //serial_print("ret = ");
  1574. //serial_phex(ret);
  1575. //serial_print("\n");
  1576. return ret;
  1577. }
  1578. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1579. if (pin == 44) pinout &= ~1;
  1580. if (pin == 52) pinout |= 1;
  1581. }
  1582. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1583. if (pin == 45) pinout &= ~2;
  1584. if (pin == 51) pinout |= 2;
  1585. }
  1586. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1587. if (pin == 46) pinout &= ~4;
  1588. if (pin == 53) pinout |= 4;
  1589. }
  1590. inline void enable_pins(void) __attribute__((always_inline)) {
  1591. //serial_print("enable_pins\n");
  1592. if ((pinout & 1) == 0) {
  1593. CORE_PIN44_CONFIG = PORT_PCR_MUX(2);
  1594. } else {
  1595. CORE_PIN52_CONFIG = PORT_PCR_MUX(2);
  1596. }
  1597. if ((pinout & 2) == 0) {
  1598. CORE_PIN45_CONFIG = PORT_PCR_MUX(2);
  1599. } else {
  1600. CORE_PIN51_CONFIG = PORT_PCR_MUX(2);
  1601. }
  1602. if ((pinout & 4) == 0) {
  1603. CORE_PIN46_CONFIG = PORT_PCR_MUX(2);
  1604. } else {
  1605. CORE_PIN53_CONFIG = PORT_PCR_MUX(2);
  1606. }
  1607. }
  1608. inline void disable_pins(void) __attribute__((always_inline)) {
  1609. //serial_print("disable_pins\n");
  1610. if ((pinout & 1) == 0) {
  1611. CORE_PIN44_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1612. } else {
  1613. CORE_PIN52_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1614. }
  1615. if ((pinout & 2) == 0) {
  1616. CORE_PIN45_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1617. } else {
  1618. CORE_PIN51_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1619. }
  1620. if ((pinout & 4) == 0) {
  1621. CORE_PIN46_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1622. } else {
  1623. CORE_PIN53_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1624. }
  1625. }
  1626. friend class SPIFIFO1class;
  1627. private:
  1628. static uint8_t pinout;
  1629. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1630. if (SPI2_CTAR0 == ctar) return;
  1631. uint32_t mcr = SPI2_MCR;
  1632. if (mcr & SPI_MCR_MDIS) {
  1633. SPI2_CTAR0 = ctar;
  1634. } else {
  1635. SPI2_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1636. SPI2_CTAR0 = ctar;
  1637. SPI2_MCR = mcr;
  1638. }
  1639. }
  1640. };
  1641. extern SPCR2emulation SPCR2;
  1642. #endif
  1643. class SPSRemulation
  1644. {
  1645. public:
  1646. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1647. //serial_print("SPSR=");
  1648. //serial_phex(val);
  1649. //serial_print("\n");
  1650. uint32_t ctar = SPI0_CTAR0;
  1651. if (val & (1<<SPI2X)) {
  1652. ctar |= SPI_CTAR_DBR;
  1653. } else {
  1654. ctar &= ~SPI_CTAR_DBR;
  1655. }
  1656. SPCRemulation::update_ctar(ctar);
  1657. //serial_print("MCR:");
  1658. //serial_phex32(SPI0_MCR);
  1659. //serial_print(", CTAR0:");
  1660. //serial_phex32(SPI0_CTAR0);
  1661. //serial_print("\n");
  1662. return *this;
  1663. }
  1664. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1665. //serial_print("SPSR |= ");
  1666. //serial_phex(val);
  1667. //serial_print("\n");
  1668. if (val & (1<<SPI2X)) SPCRemulation::update_ctar(SPI0_CTAR0 |= SPI_CTAR_DBR);
  1669. return *this;
  1670. }
  1671. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1672. //serial_print("SPSR &= ");
  1673. //serial_phex(val);
  1674. //serial_print("\n");
  1675. if (!(val & (1<<SPI2X))) SPCRemulation::update_ctar(SPI0_CTAR0 &= ~SPI_CTAR_DBR);
  1676. return *this;
  1677. }
  1678. inline int operator & (int val) const __attribute__((always_inline)) {
  1679. int ret = 0;
  1680. //serial_print("SPSR & ");
  1681. //serial_phex(val);
  1682. //serial_print("\n");
  1683. // TODO: using SPI_SR_TCF isn't quite right. Control returns to the
  1684. // caller after the final edge that captures data, which is 1/2 cycle
  1685. // sooner than AVR returns. At 500 kHz and slower SPI, this can make
  1686. // a difference when digitalWrite is used to manually control the CS
  1687. // pin, and perhaps it could matter at high clocks if faster register
  1688. // access is used? But does it really matter? Do any SPI chips in
  1689. // practice really perform differently if CS negates early, after the
  1690. // final bit is clocked, but before the end of the whole clock cycle?
  1691. if ((val & (1<<SPIF)) && (SPI0_SR & SPI_SR_TCF)) ret = (1<<SPIF);
  1692. if ((val & (1<<SPI2X)) && (SPI0_CTAR0 & SPI_CTAR_DBR)) ret |= (1<<SPI2X);
  1693. //delayMicroseconds(50000);
  1694. return ret;
  1695. }
  1696. operator int () const __attribute__((always_inline)) {
  1697. int ret = 0;
  1698. //serial_print("SPSR (int)\n");
  1699. if (SPI0_SR & SPI_SR_TCF) ret = (1<<SPIF);
  1700. if (SPI0_CTAR0 & SPI_CTAR_DBR) ret |= (1<<SPI2X);
  1701. return ret;
  1702. }
  1703. };
  1704. extern SPSRemulation SPSR;
  1705. class SPDRemulation
  1706. {
  1707. public:
  1708. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1709. //serial_print("SPDR = ");
  1710. //serial_phex(val);
  1711. //serial_print("\n");
  1712. SPI0_MCR |= SPI_MCR_CLR_RXF; // discard any received data
  1713. SPI0_SR = SPI_SR_TCF;
  1714. //SPI0_SR = SPI_SR_EOQF;
  1715. //SPI0_PUSHR = (val & 255) | SPI0_PUSHR_EOQ;
  1716. SPI0_PUSHR = (val & 255);
  1717. return *this;
  1718. }
  1719. operator int () const __attribute__((always_inline)) {
  1720. uint32_t val;
  1721. val = SPI0_POPR & 255;
  1722. //serial_print("SPDR (int) ");
  1723. //serial_phex(val);
  1724. //serial_print("\n");
  1725. return val;
  1726. }
  1727. };
  1728. extern SPDRemulation SPDR;
  1729. #elif defined(KINETISL)
  1730. // SPI Control Register ­ SPCR
  1731. //#define SPIE 7 // SPI Interrupt Enable - not supported
  1732. //#define SPE 6 // SPI Enable
  1733. //#define DORD 5 // DORD: Data Order
  1734. //#define MSTR 4 // MSTR: Master/Slave Select
  1735. //#define CPOL 3 // CPOL: Clock Polarity
  1736. //#define CPHA 2 // CPHA: Clock Phase
  1737. //#define SPR1 1 // Clock: 3 = 125 kHz, 2 = 250 kHz, 1 = 1 MHz, 0->4 MHz
  1738. //#define SPR0 0
  1739. // SPI Status Register ­ SPSR
  1740. //#define SPIF 7 // SPIF: SPI Interrupt Flag
  1741. //#define WCOL 6 // WCOL: Write COLlision Flag - not implemented
  1742. //#define SPI2X 0 // SPI2X: Double SPI Speed Bit
  1743. // SPI Data Register ­ SPDR
  1744. class SPCRemulation
  1745. {
  1746. public:
  1747. inline SPCRemulation & operator = (int val) __attribute__((always_inline)) {
  1748. uint32_t sim4 = SIM_SCGC4;
  1749. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1750. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1751. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1752. }
  1753. uint32_t c1 = 0;
  1754. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1755. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1756. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1757. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1758. if (val & (1<<SPE)) c1 |= SPI_C1_SPE;
  1759. SPI0_C1 = c1;
  1760. SPI0_C2 = 0;
  1761. uint32_t br = SPI0_BR & 0x10;
  1762. switch (val & 3) {
  1763. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1764. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1765. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1766. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1767. }
  1768. if (val & (1<<SPE)) enable_pins();
  1769. else disable_pins();
  1770. return *this;
  1771. }
  1772. inline SPCRemulation & operator |= (int val) __attribute__((always_inline)) {
  1773. uint32_t sim4 = SIM_SCGC4;
  1774. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1775. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1776. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1777. }
  1778. uint32_t c1 = SPI0_C1;
  1779. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1780. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1781. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1782. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1783. if (val & (1<<SPE)) {
  1784. enable_pins();
  1785. c1 |= SPI_C1_SPE;
  1786. }
  1787. SPI0_C1 = c1;
  1788. SPI0_C2 = 0;
  1789. val &= 3;
  1790. if (val) {
  1791. uint32_t br = SPI0_BR;
  1792. uint32_t bits = baud2avr(br) | val;
  1793. br &= 0x10;
  1794. switch (bits) {
  1795. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1796. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1797. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1798. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1799. }
  1800. }
  1801. return *this;
  1802. }
  1803. inline SPCRemulation & operator &= (int val) __attribute__((always_inline)) {
  1804. uint32_t sim4 = SIM_SCGC4;
  1805. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1806. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1807. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1808. }
  1809. uint32_t c1 = SPI0_C1;
  1810. if (!(val & (1<<DORD))) c1 &= ~SPI_C1_LSBFE;
  1811. if (!(val & (1<<CPOL))) c1 &= ~SPI_C1_CPOL;
  1812. if (!(val & (1<<CPHA))) c1 &= ~SPI_C1_CPHA;
  1813. if (!(val & (1<<MSTR))) c1 &= ~SPI_C1_MSTR;
  1814. if (!(val & (1<<SPE))) {
  1815. disable_pins();
  1816. c1 &= ~SPI_C1_SPE;
  1817. }
  1818. SPI0_C1 = c1;
  1819. SPI0_C2 = 0;
  1820. val &= 3;
  1821. if (val < 3) {
  1822. uint32_t br = SPI0_BR;
  1823. uint32_t bits = baud2avr(br) & val;
  1824. br &= 0x10;
  1825. switch (bits) {
  1826. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1827. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1828. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1829. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1830. }
  1831. }
  1832. return *this;
  1833. }
  1834. inline int operator & (int val) const __attribute__((always_inline)) {
  1835. int ret = 0;
  1836. uint32_t sim4 = SIM_SCGC4;
  1837. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1838. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1839. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1840. }
  1841. uint32_t c1 = SPI0_C1;
  1842. if ((val & (1<<DORD)) && (c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1843. if ((val & (1<<CPOL)) && (c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1844. if ((val & (1<<CPHA)) && (c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1845. if ((val & (1<<MSTR)) && (c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1846. if ((val & (1<<SPE)) && (c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1847. uint32_t bits = baud2avr(SPI0_BR);
  1848. if ((val & (1<<SPR1)) && (bits & (1<<SPR1))) ret |= (1<<SPR1);
  1849. if ((val & (1<<SPR0)) && (bits & (1<<SPR0))) ret |= (1<<SPR0);
  1850. return ret;
  1851. }
  1852. operator int () const __attribute__((always_inline)) {
  1853. int ret = 0;
  1854. uint32_t sim4 = SIM_SCGC4;
  1855. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1856. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1857. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1858. }
  1859. uint32_t c1 = SPI0_C1;
  1860. if ((c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1861. if ((c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1862. if ((c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1863. if ((c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1864. if ((c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1865. ret |= baud2avr(SPI0_BR);
  1866. return ret;
  1867. }
  1868. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1869. uint8_t newpinout = pinout;
  1870. if (pin == 11) newpinout &= ~1;
  1871. if (pin == 7) newpinout |= 1;
  1872. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1873. if ((newpinout & 1) == 0) {
  1874. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1875. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1876. } else {
  1877. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1878. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  1879. }
  1880. }
  1881. pinout = newpinout;
  1882. }
  1883. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1884. uint8_t newpinout = pinout;
  1885. if (pin == 12) newpinout &= ~2;
  1886. if (pin == 8) newpinout |= 2;
  1887. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1888. if ((newpinout & 2) == 0) {
  1889. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1890. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  1891. } else {
  1892. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1893. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  1894. }
  1895. }
  1896. pinout = newpinout;
  1897. }
  1898. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1899. uint8_t newpinout = pinout;
  1900. if (pin == 13) newpinout &= ~4;
  1901. if (pin == 14) newpinout |= 4;
  1902. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1903. if ((newpinout & 4) == 0) {
  1904. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1905. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1906. } else {
  1907. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1908. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  1909. }
  1910. }
  1911. pinout = newpinout;
  1912. }
  1913. friend class SPSRemulation;
  1914. friend class SPIFIFOclass;
  1915. private:
  1916. static inline uint32_t baud2avr(uint32_t br) __attribute__((always_inline)) {
  1917. br &= 15;
  1918. if (br == 0) return 0;
  1919. if (br <= 2) return 1;
  1920. if (br <= 4) return 2;
  1921. return 3;
  1922. }
  1923. static uint8_t pinout;
  1924. public:
  1925. inline void enable_pins(void) __attribute__((always_inline)) {
  1926. //serial_print("enable_pins\n");
  1927. if ((pinout & 1) == 0) {
  1928. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // MOSI0 = 11 (PTC6)
  1929. } else {
  1930. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // MOSI0 = 7 (PTD2)
  1931. }
  1932. if ((pinout & 2) == 0) {
  1933. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // MISO0 = 12 (PTC7)
  1934. } else {
  1935. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // MISO0 = 8 (PTD3)
  1936. }
  1937. if ((pinout & 4) == 0) {
  1938. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK0 = 13 (PTC5)
  1939. } else {
  1940. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK0 = 14 (PTD1)
  1941. }
  1942. }
  1943. inline void disable_pins(void) __attribute__((always_inline)) {
  1944. //serial_print("disable_pins\n");
  1945. if ((pinout & 1) == 0) {
  1946. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1947. } else {
  1948. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1949. }
  1950. if ((pinout & 2) == 0) {
  1951. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1952. } else {
  1953. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1954. }
  1955. if ((pinout & 4) == 0) {
  1956. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1957. } else {
  1958. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1959. }
  1960. }
  1961. };
  1962. extern SPCRemulation SPCR;
  1963. class SPCR1emulation
  1964. {
  1965. public:
  1966. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1967. if (pin == 0) pinout &= ~1; // MOSI1 = 0 (PTB16)
  1968. if (pin == 21) pinout |= 1; // MOSI1 = 21 (PTD6)
  1969. }
  1970. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1971. if (pin == 1) pinout &= ~2; // MISO1 = 1 (PTB17)
  1972. if (pin == 5) pinout |= 2; // MISO1 = 5 (PTD7)
  1973. }
  1974. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1975. // SCK1 = 20 (PTD5) - no alternative pin
  1976. }
  1977. inline void enable_pins(void) __attribute__((always_inline)) {
  1978. //serial_print("enable_pins\n");
  1979. if ((pinout & 1) == 0) {
  1980. CORE_PIN0_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 0 (PTB16)
  1981. } else {
  1982. CORE_PIN21_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 21 (PTD6)
  1983. }
  1984. if ((pinout & 2) == 0) {
  1985. CORE_PIN1_CONFIG = PORT_PCR_MUX(2); // MISO1 = 1 (PTB17)
  1986. } else {
  1987. CORE_PIN5_CONFIG = PORT_PCR_MUX(2); // MISO1 = 5 (PTD7)
  1988. }
  1989. CORE_PIN20_CONFIG = PORT_PCR_MUX(2); // SCK1 = 20 (PTD5)
  1990. }
  1991. inline void disable_pins(void) __attribute__((always_inline)) {
  1992. //serial_print("disable_pins\n");
  1993. if ((pinout & 1) == 0) {
  1994. CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1995. } else {
  1996. CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1997. }
  1998. if ((pinout & 2) == 0) {
  1999. CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  2000. } else {
  2001. CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  2002. }
  2003. CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  2004. }
  2005. friend class SPIFIFO1class;
  2006. private:
  2007. static uint8_t pinout;
  2008. };
  2009. extern SPCR1emulation SPCR1;
  2010. class SPSRemulation
  2011. {
  2012. public:
  2013. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  2014. if (val & (1<<SPI2X)) {
  2015. SPI0_BR &= ~0x10;
  2016. } else {
  2017. SPI0_BR |= 0x10;
  2018. }
  2019. return *this;
  2020. }
  2021. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  2022. if (val & (1<<SPI2X)) SPI0_BR &= ~0x10;
  2023. return *this;
  2024. }
  2025. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  2026. if (!(val & (1<<SPI2X))) SPI0_BR |= 0x10;
  2027. return *this;
  2028. }
  2029. inline int operator & (int val) const __attribute__((always_inline)) {
  2030. int ret = 0;
  2031. if ((val & (1<<SPIF)) && (SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  2032. if ((val & (1<<SPI2X)) && (!(SPI0_BR & 0x10))) ret |= (1<<SPI2X);
  2033. return ret;
  2034. }
  2035. operator int () const __attribute__((always_inline)) {
  2036. int ret = 0;
  2037. if ((SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  2038. if (!(SPI0_BR & 0x10)) ret |= (1<<SPI2X);
  2039. return ret;
  2040. }
  2041. };
  2042. extern SPSRemulation SPSR;
  2043. class SPDRemulation
  2044. {
  2045. public:
  2046. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  2047. if ((SPI0_S & SPI_S_SPTEF)) {
  2048. uint32_t tmp __attribute__((unused)) = SPI0_DL;
  2049. }
  2050. SPI0_DL = val;
  2051. return *this;
  2052. }
  2053. operator int () const __attribute__((always_inline)) {
  2054. return SPI0_DL & 255;
  2055. }
  2056. };
  2057. extern SPDRemulation SPDR;
  2058. #endif // KINETISK
  2059. class SREGemulation
  2060. {
  2061. public:
  2062. operator int () const __attribute__((always_inline)) {
  2063. uint32_t primask;
  2064. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  2065. if (primask) return 0;
  2066. return (1<<7);
  2067. }
  2068. inline SREGemulation & operator = (int val) __attribute__((always_inline)) {
  2069. if (val & (1<<7)) {
  2070. __enable_irq();
  2071. } else {
  2072. __disable_irq();
  2073. }
  2074. return *this;
  2075. }
  2076. };
  2077. extern SREGemulation SREG;
  2078. // 22211
  2079. // 84062840
  2080. // 322111
  2081. // 17395173
  2082. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  2083. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  2084. #define EIMSK_pA 0x01000018 // pins 3, 4, 24
  2085. #define EIMSK_pB 0x020F0003 // pins 0, 1, 16-19, 25
  2086. #define EIMSK_pC 0x78C0BE00 // pins 9-13, 15, 22, 23, 27-30
  2087. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2088. #define EIMSK_pE 0x84000000 // pins 26, 31
  2089. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  2090. #define EIMSK_pA 0x1E000018 // pins 3, 4, 25-28
  2091. #define EIMSK_pB 0xE00F0003 // pins 0, 1, 16-19, 29-31
  2092. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  2093. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2094. #define EIMSK_pE 0x01000000 // pins 24
  2095. #endif
  2096. class EIMSKemulation // used by Adafruit_nRF8001 (only supports INT for pins 0 to 31)
  2097. {
  2098. public:
  2099. operator int () const __attribute__((always_inline)) {
  2100. int mask = 0;
  2101. volatile const uint32_t *icer = &NVIC_ICER0;
  2102. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  2103. if (icer[IRQ_PORTB >> 5] & (1 << (IRQ_PORTB & 31))) mask |= EIMSK_pB;
  2104. if (icer[IRQ_PORTC >> 5] & (1 << (IRQ_PORTC & 31))) mask |= EIMSK_pC;
  2105. if (icer[IRQ_PORTD >> 5] & (1 << (IRQ_PORTD & 31))) mask |= EIMSK_pD;
  2106. if (icer[IRQ_PORTE >> 5] & (1 << (IRQ_PORTE & 31))) mask |= EIMSK_pE;
  2107. return mask;
  2108. }
  2109. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  2110. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  2111. if (val & EIMSK_pB) NVIC_ENABLE_IRQ(IRQ_PORTB);
  2112. if (val & EIMSK_pC) NVIC_ENABLE_IRQ(IRQ_PORTC);
  2113. if (val & EIMSK_pD) NVIC_ENABLE_IRQ(IRQ_PORTD);
  2114. if (val & EIMSK_pE) NVIC_ENABLE_IRQ(IRQ_PORTE);
  2115. return *this;
  2116. }
  2117. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  2118. uint32_t n = val;
  2119. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  2120. if ((n | ~EIMSK_pB) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTB);
  2121. if ((n | ~EIMSK_pC) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTC);
  2122. if ((n | ~EIMSK_pD) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTD);
  2123. if ((n | ~EIMSK_pE) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTE);
  2124. return *this;
  2125. }
  2126. };
  2127. extern EIMSKemulation EIMSK;
  2128. #elif defined(__MKL26Z64__)
  2129. #define EIMSK_pA 0x00000018 // pins 3, 4, 24
  2130. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  2131. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2132. class EIMSKemulation // used by Adafruit_nRF8001
  2133. {
  2134. public:
  2135. operator int () const __attribute__((always_inline)) {
  2136. int mask = 0;
  2137. volatile const uint32_t *icer = &NVIC_ICER0;
  2138. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  2139. if (icer[IRQ_PORTCD >> 5] & (1 << (IRQ_PORTCD & 31))) mask |= (EIMSK_pC | EIMSK_pD);
  2140. return mask;
  2141. }
  2142. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  2143. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  2144. if (val & (EIMSK_pC | EIMSK_pD)) NVIC_ENABLE_IRQ(IRQ_PORTCD);
  2145. return *this;
  2146. }
  2147. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  2148. uint32_t n = val;
  2149. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  2150. if ((n | ~(EIMSK_pC | EIMSK_pD)) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTCD);
  2151. return *this;
  2152. }
  2153. };
  2154. extern EIMSKemulation EIMSK;
  2155. #endif
  2156. // these are not intended for public consumption...
  2157. #undef GPIO_BITBAND_ADDR
  2158. #undef CONFIG_PULLUP
  2159. #undef CONFIG_NOPULLUP
  2160. #undef GPIO_SETBIT_ATOMIC
  2161. #undef GPIO_CLRBIT_ATOMIC
  2162. #endif // __cplusplus
  2163. #endif