Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 11 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 11 година
пре 8 година
пре 10 година
пре 8 година
пре 10 година
пре 9 година
пре 9 година
пре 10 година
пре 10 година
пре 9 година
пре 9 година
пре 10 година
пре 9 година
пре 10 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef _avr_emulation_h_
  31. #define _avr_emulation_h_
  32. #include "kinetis.h"
  33. #include "core_pins.h"
  34. #include "pins_arduino.h"
  35. #ifdef __cplusplus
  36. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  37. #define CONFIG_PULLUP (PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS)
  38. #define CONFIG_NOPULLUP (PORT_PCR_MUX(1))
  39. #if defined(KINETISK)
  40. // bitband addressing for atomic access to data direction register
  41. #define GPIO_SETBIT_ATOMIC(reg, bit) (*(uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)) = 1)
  42. #define GPIO_CLRBIT_ATOMIC(reg, bit) (*(uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)) = 0)
  43. #elif defined(KINETISL)
  44. // bit manipulation engine for atomic access to data direction register
  45. #define GPIO_SETBIT_ATOMIC(reg, bit) (*(uint32_t *)(((uint32_t)&(reg) - 0xF8000000) | 0x480FF000) = 1 << (bit))
  46. #define GPIO_CLRBIT_ATOMIC(reg, bit) (*(uint32_t *)(((uint32_t)&(reg) - 0xF8000000) | 0x440FF000) = ~(1 << (bit)))
  47. #endif
  48. class PORTDemulation
  49. {
  50. public:
  51. inline PORTDemulation & operator = (int val) __attribute__((always_inline)) {
  52. digitalWriteFast(0, (val & (1<<0)));
  53. if (!(CORE_PIN0_DDRREG & CORE_PIN0_BIT))
  54. CORE_PIN0_CONFIG = ((val & (1<<0)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  55. digitalWriteFast(1, (val & (1<<1)));
  56. if (!(CORE_PIN1_DDRREG & CORE_PIN1_BIT))
  57. CORE_PIN1_CONFIG = ((val & (1<<1)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  58. digitalWriteFast(2, (val & (1<<2)));
  59. if (!(CORE_PIN2_DDRREG & CORE_PIN2_BIT))
  60. CORE_PIN2_CONFIG = ((val & (1<<2)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  61. digitalWriteFast(3, (val & (1<<3)));
  62. if (!(CORE_PIN3_DDRREG & CORE_PIN3_BIT))
  63. CORE_PIN3_CONFIG = ((val & (1<<3)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  64. digitalWriteFast(4, (val & (1<<4)));
  65. if (!(CORE_PIN4_DDRREG & CORE_PIN4_BIT))
  66. CORE_PIN4_CONFIG = ((val & (1<<4)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  67. digitalWriteFast(5, (val & (1<<5)));
  68. if (!(CORE_PIN5_DDRREG & CORE_PIN5_BIT))
  69. CORE_PIN5_CONFIG = ((val & (1<<5)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  70. digitalWriteFast(6, (val & (1<<6)));
  71. if (!(CORE_PIN6_DDRREG & CORE_PIN6_BIT))
  72. CORE_PIN6_CONFIG = ((val & (1<<6)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  73. digitalWriteFast(7, (val & (1<<7)));
  74. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT))
  75. CORE_PIN7_CONFIG = ((val & (1<<7)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  76. return *this;
  77. }
  78. inline PORTDemulation & operator |= (int val) __attribute__((always_inline)) {
  79. if (val & (1<<0)) {
  80. digitalWriteFast(0, HIGH);
  81. if (!(CORE_PIN0_DDRREG & CORE_PIN0_BIT)) CORE_PIN0_CONFIG = CONFIG_PULLUP;
  82. }
  83. if (val & (1<<1)) {
  84. digitalWriteFast(1, HIGH);
  85. if (!(CORE_PIN1_DDRREG & CORE_PIN1_BIT)) CORE_PIN1_CONFIG = CONFIG_PULLUP;
  86. }
  87. if (val & (1<<2)) {
  88. digitalWriteFast(2, HIGH);
  89. if (!(CORE_PIN2_DDRREG & CORE_PIN2_BIT)) CORE_PIN2_CONFIG = CONFIG_PULLUP;
  90. }
  91. if (val & (1<<3)) {
  92. digitalWriteFast(3, HIGH);
  93. if (!(CORE_PIN3_DDRREG & CORE_PIN3_BIT)) CORE_PIN3_CONFIG = CONFIG_PULLUP;
  94. }
  95. if (val & (1<<4)) {
  96. digitalWriteFast(4, HIGH);
  97. if (!(CORE_PIN4_DDRREG & CORE_PIN4_BIT)) CORE_PIN4_CONFIG = CONFIG_PULLUP;
  98. }
  99. if (val & (1<<5)) {
  100. digitalWriteFast(5, HIGH);
  101. if (!(CORE_PIN5_DDRREG & CORE_PIN5_BIT)) CORE_PIN5_CONFIG = CONFIG_PULLUP;
  102. }
  103. if (val & (1<<6)) {
  104. digitalWriteFast(6, HIGH);
  105. if (!(CORE_PIN6_DDRREG & CORE_PIN6_BIT)) CORE_PIN6_CONFIG = CONFIG_PULLUP;
  106. }
  107. if (val & (1<<7)) {
  108. digitalWriteFast(7, HIGH);
  109. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN7_CONFIG = CONFIG_PULLUP;
  110. }
  111. return *this;
  112. }
  113. inline PORTDemulation & operator &= (int val) __attribute__((always_inline)) {
  114. if (!(val & (1<<0))) {
  115. digitalWriteFast(0, LOW);
  116. if (!(CORE_PIN0_DDRREG & CORE_PIN0_BIT)) CORE_PIN0_CONFIG = CONFIG_NOPULLUP;
  117. }
  118. if (!(val & (1<<1))) {
  119. digitalWriteFast(1, LOW);
  120. if (!(CORE_PIN1_DDRREG & CORE_PIN1_BIT)) CORE_PIN1_CONFIG = CONFIG_NOPULLUP;
  121. }
  122. if (!(val & (1<<2))) {
  123. digitalWriteFast(2, LOW);
  124. if (!(CORE_PIN2_DDRREG & CORE_PIN2_BIT)) CORE_PIN2_CONFIG = CONFIG_NOPULLUP;
  125. }
  126. if (!(val & (1<<3))) {
  127. digitalWriteFast(3, LOW);
  128. if (!(CORE_PIN3_DDRREG & CORE_PIN3_BIT)) CORE_PIN3_CONFIG = CONFIG_NOPULLUP;
  129. }
  130. if (!(val & (1<<4))) {
  131. digitalWriteFast(4, LOW);
  132. if (!(CORE_PIN4_DDRREG & CORE_PIN4_BIT)) CORE_PIN4_CONFIG = CONFIG_NOPULLUP;
  133. }
  134. if (!(val & (1<<5))) {
  135. digitalWriteFast(5, LOW);
  136. if (!(CORE_PIN5_DDRREG & CORE_PIN5_BIT)) CORE_PIN5_CONFIG = CONFIG_NOPULLUP;
  137. }
  138. if (!(val & (1<<6))) {
  139. digitalWriteFast(6, LOW);
  140. if (!(CORE_PIN6_DDRREG & CORE_PIN6_BIT)) CORE_PIN6_CONFIG = CONFIG_NOPULLUP;
  141. }
  142. if (!(val & (1<<7))) {
  143. digitalWriteFast(7, LOW);
  144. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN7_CONFIG = CONFIG_NOPULLUP;
  145. }
  146. return *this;
  147. }
  148. };
  149. extern PORTDemulation PORTD;
  150. class PINDemulation
  151. {
  152. public:
  153. inline int operator & (int val) const __attribute__((always_inline)) {
  154. int ret = 0;
  155. if ((val & (1<<0)) && digitalReadFast(0)) ret |= (1<<0);
  156. if ((val & (1<<1)) && digitalReadFast(1)) ret |= (1<<1);
  157. if ((val & (1<<2)) && digitalReadFast(2)) ret |= (1<<2);
  158. if ((val & (1<<3)) && digitalReadFast(3)) ret |= (1<<3);
  159. if ((val & (1<<4)) && digitalReadFast(4)) ret |= (1<<4);
  160. if ((val & (1<<5)) && digitalReadFast(5)) ret |= (1<<5);
  161. if ((val & (1<<6)) && digitalReadFast(6)) ret |= (1<<6);
  162. if ((val & (1<<7)) && digitalReadFast(7)) ret |= (1<<7);
  163. return ret;
  164. }
  165. operator int () const __attribute__((always_inline)) {
  166. int ret = 0;
  167. if (digitalReadFast(0)) ret |= (1<<0);
  168. if (digitalReadFast(1)) ret |= (1<<1);
  169. if (digitalReadFast(2)) ret |= (1<<2);
  170. if (digitalReadFast(3)) ret |= (1<<3);
  171. if (digitalReadFast(4)) ret |= (1<<4);
  172. if (digitalReadFast(5)) ret |= (1<<5);
  173. if (digitalReadFast(6)) ret |= (1<<6);
  174. if (digitalReadFast(7)) ret |= (1<<7);
  175. return ret;
  176. }
  177. };
  178. extern PINDemulation PIND;
  179. class DDRDemulation
  180. {
  181. public:
  182. inline DDRDemulation & operator = (int val) __attribute__((always_inline)) {
  183. if (val & (1<<0)) set0(); else clr0();
  184. if (val & (1<<1)) set1(); else clr1();
  185. if (val & (1<<2)) set2(); else clr2();
  186. if (val & (1<<3)) set3(); else clr3();
  187. if (val & (1<<4)) set4(); else clr4();
  188. if (val & (1<<5)) set5(); else clr5();
  189. if (val & (1<<6)) set6(); else clr6();
  190. if (val & (1<<7)) set7(); else clr7();
  191. return *this;
  192. }
  193. inline DDRDemulation & operator |= (int val) __attribute__((always_inline)) {
  194. if (val & (1<<0)) set0();
  195. if (val & (1<<1)) set1();
  196. if (val & (1<<2)) set2();
  197. if (val & (1<<3)) set3();
  198. if (val & (1<<4)) set4();
  199. if (val & (1<<5)) set5();
  200. if (val & (1<<6)) set6();
  201. if (val & (1<<7)) set7();
  202. return *this;
  203. }
  204. inline DDRDemulation & operator &= (int val) __attribute__((always_inline)) {
  205. if (!(val & (1<<0))) clr0();
  206. if (!(val & (1<<1))) clr1();
  207. if (!(val & (1<<2))) clr2();
  208. if (!(val & (1<<3))) clr3();
  209. if (!(val & (1<<4))) clr4();
  210. if (!(val & (1<<5))) clr5();
  211. if (!(val & (1<<6))) clr6();
  212. if (!(val & (1<<7))) clr7();
  213. return *this;
  214. }
  215. private:
  216. inline void set0() __attribute__((always_inline)) {
  217. GPIO_SETBIT_ATOMIC(CORE_PIN0_DDRREG, CORE_PIN0_BIT);
  218. CORE_PIN0_CONFIG = CONFIG_PULLUP;
  219. }
  220. inline void set1() __attribute__((always_inline)) {
  221. GPIO_SETBIT_ATOMIC(CORE_PIN1_DDRREG, CORE_PIN1_BIT);
  222. CORE_PIN1_CONFIG = CONFIG_PULLUP;
  223. }
  224. inline void set2() __attribute__((always_inline)) {
  225. GPIO_SETBIT_ATOMIC(CORE_PIN2_DDRREG, CORE_PIN2_BIT);
  226. CORE_PIN2_CONFIG = CONFIG_PULLUP;
  227. }
  228. inline void set3() __attribute__((always_inline)) {
  229. GPIO_SETBIT_ATOMIC(CORE_PIN3_DDRREG, CORE_PIN3_BIT);
  230. CORE_PIN3_CONFIG = CONFIG_PULLUP;
  231. }
  232. inline void set4() __attribute__((always_inline)) {
  233. GPIO_SETBIT_ATOMIC(CORE_PIN4_DDRREG, CORE_PIN4_BIT);
  234. CORE_PIN4_CONFIG = CONFIG_PULLUP;
  235. }
  236. inline void set5() __attribute__((always_inline)) {
  237. GPIO_SETBIT_ATOMIC(CORE_PIN5_DDRREG, CORE_PIN5_BIT);
  238. CORE_PIN5_CONFIG = CONFIG_PULLUP;
  239. }
  240. inline void set6() __attribute__((always_inline)) {
  241. GPIO_SETBIT_ATOMIC(CORE_PIN6_DDRREG, CORE_PIN6_BIT);
  242. CORE_PIN6_CONFIG = CONFIG_PULLUP;
  243. }
  244. inline void set7() __attribute__((always_inline)) {
  245. GPIO_SETBIT_ATOMIC(CORE_PIN7_DDRREG, CORE_PIN7_BIT);
  246. CORE_PIN7_CONFIG = CONFIG_PULLUP;
  247. }
  248. inline void clr0() __attribute__((always_inline)) {
  249. CORE_PIN0_CONFIG = ((CORE_PIN0_PORTREG & CORE_PIN0_BITMASK)
  250. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  251. GPIO_CLRBIT_ATOMIC(CORE_PIN0_DDRREG, CORE_PIN0_BIT);
  252. }
  253. inline void clr1() __attribute__((always_inline)) {
  254. CORE_PIN1_CONFIG = ((CORE_PIN1_PORTREG & CORE_PIN1_BITMASK)
  255. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  256. GPIO_CLRBIT_ATOMIC(CORE_PIN1_DDRREG, CORE_PIN1_BIT);
  257. }
  258. inline void clr2() __attribute__((always_inline)) {
  259. CORE_PIN2_CONFIG = ((CORE_PIN2_PORTREG & CORE_PIN2_BITMASK)
  260. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  261. GPIO_CLRBIT_ATOMIC(CORE_PIN2_DDRREG, CORE_PIN2_BIT);
  262. }
  263. inline void clr3() __attribute__((always_inline)) {
  264. CORE_PIN3_CONFIG = ((CORE_PIN3_PORTREG & CORE_PIN3_BITMASK)
  265. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  266. GPIO_CLRBIT_ATOMIC(CORE_PIN3_DDRREG, CORE_PIN3_BIT);
  267. }
  268. inline void clr4() __attribute__((always_inline)) {
  269. CORE_PIN4_CONFIG = ((CORE_PIN4_PORTREG & CORE_PIN4_BITMASK)
  270. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  271. GPIO_CLRBIT_ATOMIC(CORE_PIN4_DDRREG, CORE_PIN4_BIT);
  272. }
  273. inline void clr5() __attribute__((always_inline)) {
  274. CORE_PIN5_CONFIG = ((CORE_PIN5_PORTREG & CORE_PIN5_BITMASK)
  275. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  276. GPIO_CLRBIT_ATOMIC(CORE_PIN5_DDRREG, CORE_PIN5_BIT);
  277. }
  278. inline void clr6() __attribute__((always_inline)) {
  279. CORE_PIN6_CONFIG = ((CORE_PIN6_PORTREG & CORE_PIN6_BITMASK)
  280. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  281. GPIO_CLRBIT_ATOMIC(CORE_PIN6_DDRREG, CORE_PIN6_BIT);
  282. }
  283. inline void clr7() __attribute__((always_inline)) {
  284. CORE_PIN7_CONFIG = ((CORE_PIN7_PORTREG & CORE_PIN7_BITMASK)
  285. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  286. GPIO_CLRBIT_ATOMIC(CORE_PIN7_DDRREG, CORE_PIN7_BIT);
  287. }
  288. };
  289. extern DDRDemulation DDRD;
  290. class PORTBemulation
  291. {
  292. public:
  293. inline PORTBemulation & operator = (int val) __attribute__((always_inline)) {
  294. digitalWriteFast(8, (val & (1<<0)));
  295. if (!(CORE_PIN8_DDRREG & CORE_PIN8_BIT))
  296. CORE_PIN8_CONFIG = ((val & (1<<0)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  297. digitalWriteFast(9, (val & (1<<1)));
  298. if (!(CORE_PIN9_DDRREG & CORE_PIN9_BIT))
  299. CORE_PIN9_CONFIG = ((val & (1<<1)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  300. digitalWriteFast(10, (val & (1<<2)));
  301. if (!(CORE_PIN10_DDRREG & CORE_PIN10_BIT))
  302. CORE_PIN10_CONFIG = ((val & (1<<2)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  303. digitalWriteFast(11, (val & (1<<3)));
  304. if (!(CORE_PIN11_DDRREG & CORE_PIN11_BIT))
  305. CORE_PIN11_CONFIG = ((val & (1<<3)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  306. digitalWriteFast(12, (val & (1<<4)));
  307. if (!(CORE_PIN12_DDRREG & CORE_PIN12_BIT))
  308. CORE_PIN12_CONFIG = ((val & (1<<4)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  309. digitalWriteFast(13, (val & (1<<5)));
  310. if (!(CORE_PIN13_DDRREG & CORE_PIN13_BIT))
  311. CORE_PIN13_CONFIG = ((val & (1<<5)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  312. return *this;
  313. }
  314. inline PORTBemulation & operator |= (int val) __attribute__((always_inline)) {
  315. if (val & (1<<0)) {
  316. digitalWriteFast(8, HIGH);
  317. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN8_CONFIG = CONFIG_PULLUP;
  318. }
  319. if (val & (1<<1)) {
  320. digitalWriteFast(9, HIGH);
  321. if (!(CORE_PIN7_DDRREG & CORE_PIN7_BIT)) CORE_PIN9_CONFIG = CONFIG_PULLUP;
  322. }
  323. if (val & (1<<2)) {
  324. digitalWriteFast(10, HIGH);
  325. if (!(CORE_PIN10_DDRREG & CORE_PIN10_BIT)) CORE_PIN10_CONFIG = CONFIG_PULLUP;
  326. }
  327. if (val & (1<<3)) {
  328. digitalWriteFast(11, HIGH);
  329. if (!(CORE_PIN11_DDRREG & CORE_PIN11_BIT)) CORE_PIN11_CONFIG = CONFIG_PULLUP;
  330. }
  331. if (val & (1<<4)) {
  332. digitalWriteFast(12, HIGH);
  333. if (!(CORE_PIN12_DDRREG & CORE_PIN12_BIT)) CORE_PIN12_CONFIG = CONFIG_PULLUP;
  334. }
  335. if (val & (1<<5)) {
  336. digitalWriteFast(13, HIGH);
  337. if (!(CORE_PIN13_DDRREG & CORE_PIN13_BIT)) CORE_PIN13_CONFIG = CONFIG_PULLUP;
  338. }
  339. return *this;
  340. }
  341. inline PORTBemulation & operator &= (int val) __attribute__((always_inline)) {
  342. if (!(val & (1<<0))) {
  343. digitalWriteFast(8, LOW);
  344. if (!(CORE_PIN8_DDRREG & CORE_PIN8_BIT)) CORE_PIN8_CONFIG = CONFIG_NOPULLUP;
  345. }
  346. if (!(val & (1<<1))) {
  347. digitalWriteFast(9, LOW);
  348. if (!(CORE_PIN9_DDRREG & CORE_PIN9_BIT)) CORE_PIN9_CONFIG = CONFIG_NOPULLUP;
  349. }
  350. if (!(val & (1<<2))) {
  351. digitalWriteFast(10, LOW);
  352. if (!(CORE_PIN10_DDRREG & CORE_PIN10_BIT)) CORE_PIN10_CONFIG = CONFIG_NOPULLUP;
  353. }
  354. if (!(val & (1<<3))) {
  355. digitalWriteFast(11, LOW);
  356. if (!(CORE_PIN11_DDRREG & CORE_PIN11_BIT)) CORE_PIN11_CONFIG = CONFIG_NOPULLUP;
  357. }
  358. if (!(val & (1<<4))) {
  359. digitalWriteFast(12, LOW);
  360. if (!(CORE_PIN12_DDRREG & CORE_PIN12_BIT)) CORE_PIN12_CONFIG = CONFIG_NOPULLUP;
  361. }
  362. if (!(val & (1<<5))) {
  363. digitalWriteFast(13, LOW);
  364. if (!(CORE_PIN13_DDRREG & CORE_PIN13_BIT)) CORE_PIN13_CONFIG = CONFIG_NOPULLUP;
  365. }
  366. return *this;
  367. }
  368. };
  369. extern PORTBemulation PORTB;
  370. class PINBemulation
  371. {
  372. public:
  373. inline int operator & (int val) const __attribute__((always_inline)) {
  374. int ret = 0;
  375. if ((val & (1<<0)) && digitalReadFast(8)) ret |= (1<<0);
  376. if ((val & (1<<1)) && digitalReadFast(9)) ret |= (1<<1);
  377. if ((val & (1<<2)) && digitalReadFast(10)) ret |= (1<<2);
  378. if ((val & (1<<3)) && digitalReadFast(11)) ret |= (1<<3);
  379. if ((val & (1<<4)) && digitalReadFast(12)) ret |= (1<<4);
  380. if ((val & (1<<5)) && digitalReadFast(13)) ret |= (1<<5);
  381. return ret;
  382. }
  383. operator int () const __attribute__((always_inline)) {
  384. int ret = 0;
  385. if (digitalReadFast(8)) ret |= (1<<0);
  386. if (digitalReadFast(9)) ret |= (1<<1);
  387. if (digitalReadFast(10)) ret |= (1<<2);
  388. if (digitalReadFast(11)) ret |= (1<<3);
  389. if (digitalReadFast(12)) ret |= (1<<4);
  390. if (digitalReadFast(13)) ret |= (1<<5);
  391. return ret;
  392. }
  393. };
  394. extern PINBemulation PINB;
  395. class DDRBemulation
  396. {
  397. public:
  398. inline DDRBemulation & operator = (int val) __attribute__((always_inline)) {
  399. if (val & (1<<0)) set0(); else clr0();
  400. if (val & (1<<1)) set1(); else clr1();
  401. if (val & (1<<2)) set2(); else clr2();
  402. if (val & (1<<3)) set3(); else clr3();
  403. if (val & (1<<4)) set4(); else clr4();
  404. if (val & (1<<5)) set5(); else clr5();
  405. return *this;
  406. }
  407. inline DDRBemulation & operator |= (int val) __attribute__((always_inline)) {
  408. if (val & (1<<0)) set0();
  409. if (val & (1<<1)) set1();
  410. if (val & (1<<2)) set2();
  411. if (val & (1<<3)) set3();
  412. if (val & (1<<4)) set4();
  413. if (val & (1<<5)) set5();
  414. return *this;
  415. }
  416. inline DDRBemulation & operator &= (int val) __attribute__((always_inline)) {
  417. if (!(val & (1<<0))) clr0();
  418. if (!(val & (1<<1))) clr1();
  419. if (!(val & (1<<2))) clr2();
  420. if (!(val & (1<<3))) clr3();
  421. if (!(val & (1<<4))) clr4();
  422. if (!(val & (1<<5))) clr5();
  423. return *this;
  424. }
  425. private:
  426. inline void set0() __attribute__((always_inline)) {
  427. GPIO_SETBIT_ATOMIC(CORE_PIN8_DDRREG, CORE_PIN8_BIT);
  428. CORE_PIN8_CONFIG = CONFIG_PULLUP;
  429. }
  430. inline void set1() __attribute__((always_inline)) {
  431. GPIO_SETBIT_ATOMIC(CORE_PIN9_DDRREG, CORE_PIN9_BIT);
  432. CORE_PIN9_CONFIG = CONFIG_PULLUP;
  433. }
  434. inline void set2() __attribute__((always_inline)) {
  435. GPIO_SETBIT_ATOMIC(CORE_PIN10_DDRREG, CORE_PIN10_BIT);
  436. CORE_PIN10_CONFIG = CONFIG_PULLUP;
  437. }
  438. inline void set3() __attribute__((always_inline)) {
  439. GPIO_SETBIT_ATOMIC(CORE_PIN11_DDRREG, CORE_PIN11_BIT);
  440. CORE_PIN11_CONFIG = CONFIG_PULLUP;
  441. }
  442. inline void set4() __attribute__((always_inline)) {
  443. GPIO_SETBIT_ATOMIC(CORE_PIN12_DDRREG, CORE_PIN12_BIT);
  444. CORE_PIN12_CONFIG = CONFIG_PULLUP;
  445. }
  446. inline void set5() __attribute__((always_inline)) {
  447. GPIO_SETBIT_ATOMIC(CORE_PIN13_DDRREG, CORE_PIN13_BIT);
  448. CORE_PIN13_CONFIG = CONFIG_PULLUP;
  449. }
  450. inline void clr0() __attribute__((always_inline)) {
  451. CORE_PIN8_CONFIG = ((CORE_PIN8_PORTREG & CORE_PIN8_BITMASK)
  452. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  453. GPIO_CLRBIT_ATOMIC(CORE_PIN8_DDRREG, CORE_PIN8_BIT);
  454. }
  455. inline void clr1() __attribute__((always_inline)) {
  456. CORE_PIN9_CONFIG = ((CORE_PIN9_PORTREG & CORE_PIN9_BITMASK)
  457. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  458. GPIO_CLRBIT_ATOMIC(CORE_PIN9_DDRREG, CORE_PIN9_BIT);
  459. }
  460. inline void clr2() __attribute__((always_inline)) {
  461. CORE_PIN10_CONFIG = ((CORE_PIN10_PORTREG & CORE_PIN10_BITMASK)
  462. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  463. GPIO_CLRBIT_ATOMIC(CORE_PIN10_DDRREG, CORE_PIN10_BIT);
  464. }
  465. inline void clr3() __attribute__((always_inline)) {
  466. CORE_PIN11_CONFIG = ((CORE_PIN11_PORTREG & CORE_PIN11_BITMASK)
  467. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  468. GPIO_CLRBIT_ATOMIC(CORE_PIN11_DDRREG, CORE_PIN11_BIT);
  469. }
  470. inline void clr4() __attribute__((always_inline)) {
  471. CORE_PIN12_CONFIG = ((CORE_PIN12_PORTREG & CORE_PIN12_BITMASK)
  472. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  473. GPIO_CLRBIT_ATOMIC(CORE_PIN12_DDRREG, CORE_PIN12_BIT);
  474. }
  475. inline void clr5() __attribute__((always_inline)) {
  476. CORE_PIN13_CONFIG = ((CORE_PIN13_PORTREG & CORE_PIN13_BITMASK)
  477. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  478. GPIO_CLRBIT_ATOMIC(CORE_PIN13_DDRREG, CORE_PIN13_BIT);
  479. }
  480. };
  481. extern DDRBemulation DDRB;
  482. class PORTCemulation
  483. {
  484. public:
  485. inline PORTCemulation & operator = (int val) __attribute__((always_inline)) {
  486. digitalWriteFast(14, (val & (1<<0)));
  487. if (!(CORE_PIN14_DDRREG & CORE_PIN14_BIT))
  488. CORE_PIN14_CONFIG = ((val & (1<<0)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  489. digitalWriteFast(15, (val & (1<<1)));
  490. if (!(CORE_PIN15_DDRREG & CORE_PIN15_BIT))
  491. CORE_PIN15_CONFIG = ((val & (1<<1)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  492. digitalWriteFast(16, (val & (1<<2)));
  493. if (!(CORE_PIN16_DDRREG & CORE_PIN16_BIT))
  494. CORE_PIN16_CONFIG = ((val & (1<<2)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  495. digitalWriteFast(17, (val & (1<<3)));
  496. if (!(CORE_PIN17_DDRREG & CORE_PIN17_BIT))
  497. CORE_PIN17_CONFIG = ((val & (1<<3)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  498. digitalWriteFast(18, (val & (1<<4)));
  499. if (!(CORE_PIN18_DDRREG & CORE_PIN18_BIT))
  500. CORE_PIN18_CONFIG = ((val & (1<<4)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  501. digitalWriteFast(19, (val & (1<<5)));
  502. if (!(CORE_PIN19_DDRREG & CORE_PIN19_BIT))
  503. CORE_PIN19_CONFIG = ((val & (1<<5)) ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  504. return *this;
  505. }
  506. inline PORTCemulation & operator |= (int val) __attribute__((always_inline)) {
  507. if (val & (1<<0)) {
  508. digitalWriteFast(14, HIGH);
  509. if (!(CORE_PIN14_DDRREG & CORE_PIN14_BIT)) CORE_PIN14_CONFIG = CONFIG_PULLUP;
  510. }
  511. if (val & (1<<1)) {
  512. digitalWriteFast(15, HIGH);
  513. if (!(CORE_PIN15_DDRREG & CORE_PIN15_BIT)) CORE_PIN15_CONFIG = CONFIG_PULLUP;
  514. }
  515. if (val & (1<<2)) {
  516. digitalWriteFast(16, HIGH);
  517. if (!(CORE_PIN16_DDRREG & CORE_PIN16_BIT)) CORE_PIN16_CONFIG = CONFIG_PULLUP;
  518. }
  519. if (val & (1<<3)) {
  520. digitalWriteFast(17, HIGH);
  521. if (!(CORE_PIN17_DDRREG & CORE_PIN17_BIT)) CORE_PIN17_CONFIG = CONFIG_PULLUP;
  522. }
  523. if (val & (1<<4)) {
  524. digitalWriteFast(18, HIGH);
  525. if (!(CORE_PIN18_DDRREG & CORE_PIN18_BIT)) CORE_PIN18_CONFIG = CONFIG_PULLUP;
  526. }
  527. if (val & (1<<5)) {
  528. digitalWriteFast(19, HIGH);
  529. if (!(CORE_PIN19_DDRREG & CORE_PIN19_BIT)) CORE_PIN19_CONFIG = CONFIG_PULLUP;
  530. }
  531. return *this;
  532. }
  533. inline PORTCemulation & operator &= (int val) __attribute__((always_inline)) {
  534. if (!(val & (1<<0))) {
  535. digitalWriteFast(14, LOW);
  536. if (!(CORE_PIN14_DDRREG & CORE_PIN14_BIT)) CORE_PIN14_CONFIG = CONFIG_NOPULLUP;
  537. }
  538. if (!(val & (1<<1))) {
  539. digitalWriteFast(15, LOW);
  540. if (!(CORE_PIN15_DDRREG & CORE_PIN15_BIT)) CORE_PIN15_CONFIG = CONFIG_NOPULLUP;
  541. }
  542. if (!(val & (1<<2))) {
  543. digitalWriteFast(16, LOW);
  544. if (!(CORE_PIN16_DDRREG & CORE_PIN16_BIT)) CORE_PIN16_CONFIG = CONFIG_NOPULLUP;
  545. }
  546. if (!(val & (1<<3))) {
  547. digitalWriteFast(17, LOW);
  548. if (!(CORE_PIN17_DDRREG & CORE_PIN17_BIT)) CORE_PIN17_CONFIG = CONFIG_NOPULLUP;
  549. }
  550. if (!(val & (1<<4))) {
  551. digitalWriteFast(18, LOW);
  552. if (!(CORE_PIN18_DDRREG & CORE_PIN18_BIT)) CORE_PIN18_CONFIG = CONFIG_NOPULLUP;
  553. }
  554. if (!(val & (1<<5))) {
  555. digitalWriteFast(19, LOW);
  556. if (!(CORE_PIN19_DDRREG & CORE_PIN19_BIT)) CORE_PIN19_CONFIG = CONFIG_NOPULLUP;
  557. }
  558. return *this;
  559. }
  560. };
  561. extern PORTCemulation PORTC;
  562. class PINCemulation
  563. {
  564. public:
  565. inline int operator & (int val) const __attribute__((always_inline)) {
  566. int ret = 0;
  567. if ((val & (1<<0)) && digitalReadFast(8)) ret |= (1<<0);
  568. if ((val & (1<<1)) && digitalReadFast(9)) ret |= (1<<1);
  569. if ((val & (1<<2)) && digitalReadFast(10)) ret |= (1<<2);
  570. if ((val & (1<<3)) && digitalReadFast(11)) ret |= (1<<3);
  571. if ((val & (1<<4)) && digitalReadFast(12)) ret |= (1<<4);
  572. if ((val & (1<<5)) && digitalReadFast(13)) ret |= (1<<5);
  573. return ret;
  574. }
  575. operator int () const __attribute__((always_inline)) {
  576. int ret = 0;
  577. if (digitalReadFast(8)) ret |= (1<<0);
  578. if (digitalReadFast(9)) ret |= (1<<1);
  579. if (digitalReadFast(10)) ret |= (1<<2);
  580. if (digitalReadFast(11)) ret |= (1<<3);
  581. if (digitalReadFast(12)) ret |= (1<<4);
  582. if (digitalReadFast(13)) ret |= (1<<5);
  583. return ret;
  584. }
  585. };
  586. extern PINCemulation PINC;
  587. class DDRCemulation
  588. {
  589. public:
  590. inline DDRCemulation & operator = (int val) __attribute__((always_inline)) {
  591. if (val & (1<<0)) set0(); else clr0();
  592. if (val & (1<<1)) set1(); else clr1();
  593. if (val & (1<<2)) set2(); else clr2();
  594. if (val & (1<<3)) set3(); else clr3();
  595. if (val & (1<<4)) set4(); else clr4();
  596. if (val & (1<<5)) set5(); else clr5();
  597. return *this;
  598. }
  599. inline DDRCemulation & operator |= (int val) __attribute__((always_inline)) {
  600. if (val & (1<<0)) set0();
  601. if (val & (1<<1)) set1();
  602. if (val & (1<<2)) set2();
  603. if (val & (1<<3)) set3();
  604. if (val & (1<<4)) set4();
  605. if (val & (1<<5)) set5();
  606. return *this;
  607. }
  608. inline DDRCemulation & operator &= (int val) __attribute__((always_inline)) {
  609. if (!(val & (1<<0))) clr0();
  610. if (!(val & (1<<1))) clr1();
  611. if (!(val & (1<<2))) clr2();
  612. if (!(val & (1<<3))) clr3();
  613. if (!(val & (1<<4))) clr4();
  614. if (!(val & (1<<5))) clr5();
  615. return *this;
  616. }
  617. private:
  618. inline void set0() __attribute__((always_inline)) {
  619. GPIO_SETBIT_ATOMIC(CORE_PIN14_DDRREG, CORE_PIN14_BIT);
  620. CORE_PIN14_CONFIG = CONFIG_PULLUP;
  621. }
  622. inline void set1() __attribute__((always_inline)) {
  623. GPIO_SETBIT_ATOMIC(CORE_PIN15_DDRREG, CORE_PIN15_BIT);
  624. CORE_PIN15_CONFIG = CONFIG_PULLUP;
  625. }
  626. inline void set2() __attribute__((always_inline)) {
  627. GPIO_SETBIT_ATOMIC(CORE_PIN16_DDRREG, CORE_PIN16_BIT);
  628. CORE_PIN16_CONFIG = CONFIG_PULLUP;
  629. }
  630. inline void set3() __attribute__((always_inline)) {
  631. GPIO_SETBIT_ATOMIC(CORE_PIN17_DDRREG, CORE_PIN17_BIT);
  632. CORE_PIN17_CONFIG = CONFIG_PULLUP;
  633. }
  634. inline void set4() __attribute__((always_inline)) {
  635. GPIO_SETBIT_ATOMIC(CORE_PIN18_DDRREG, CORE_PIN18_BIT);
  636. CORE_PIN18_CONFIG = CONFIG_PULLUP;
  637. }
  638. inline void set5() __attribute__((always_inline)) {
  639. GPIO_SETBIT_ATOMIC(CORE_PIN19_DDRREG, CORE_PIN19_BIT);
  640. CORE_PIN19_CONFIG = CONFIG_PULLUP;
  641. }
  642. inline void clr0() __attribute__((always_inline)) {
  643. CORE_PIN14_CONFIG = ((CORE_PIN14_PORTREG & CORE_PIN14_BITMASK)
  644. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  645. GPIO_CLRBIT_ATOMIC(CORE_PIN14_DDRREG, CORE_PIN14_BIT);
  646. }
  647. inline void clr1() __attribute__((always_inline)) {
  648. CORE_PIN15_CONFIG = ((CORE_PIN15_PORTREG & CORE_PIN15_BITMASK)
  649. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  650. GPIO_CLRBIT_ATOMIC(CORE_PIN15_DDRREG, CORE_PIN15_BIT);
  651. }
  652. inline void clr2() __attribute__((always_inline)) {
  653. CORE_PIN16_CONFIG = ((CORE_PIN16_PORTREG & CORE_PIN16_BITMASK)
  654. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  655. GPIO_CLRBIT_ATOMIC(CORE_PIN16_DDRREG, CORE_PIN16_BIT);
  656. }
  657. inline void clr3() __attribute__((always_inline)) {
  658. CORE_PIN17_CONFIG = ((CORE_PIN17_PORTREG & CORE_PIN17_BITMASK)
  659. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  660. GPIO_CLRBIT_ATOMIC(CORE_PIN17_DDRREG, CORE_PIN17_BIT);
  661. }
  662. inline void clr4() __attribute__((always_inline)) {
  663. CORE_PIN18_CONFIG = ((CORE_PIN18_PORTREG & CORE_PIN18_BITMASK)
  664. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  665. GPIO_CLRBIT_ATOMIC(CORE_PIN18_DDRREG, CORE_PIN18_BIT);
  666. }
  667. inline void clr5() __attribute__((always_inline)) {
  668. CORE_PIN19_CONFIG = ((CORE_PIN19_PORTREG & CORE_PIN19_BITMASK)
  669. ? CONFIG_PULLUP : CONFIG_NOPULLUP);
  670. GPIO_CLRBIT_ATOMIC(CORE_PIN19_DDRREG, CORE_PIN19_BIT);
  671. }
  672. };
  673. extern DDRCemulation DDRC;
  674. #define PINB0 0
  675. #define PINB1 1
  676. #define PINB2 2
  677. #define PINB3 3
  678. #define PINB4 4
  679. #define PINB5 5
  680. #define PINB6 6
  681. #define PINB7 7
  682. #define DDB0 0
  683. #define DDB1 1
  684. #define DDB2 2
  685. #define DDB3 3
  686. #define DDB4 4
  687. #define DDB5 5
  688. #define DDB6 6
  689. #define DDB7 7
  690. #define PORTB0 0
  691. #define PORTB1 1
  692. #define PORTB2 2
  693. #define PORTB3 3
  694. #define PORTB4 4
  695. #define PORTB5 5
  696. #define PORTB6 6
  697. #define PORTB7 7
  698. #define PINC0 0
  699. #define PINC1 1
  700. #define PINC2 2
  701. #define PINC3 3
  702. #define PINC4 4
  703. #define PINC5 5
  704. #define PINC6 6
  705. #define DDC0 0
  706. #define DDC1 1
  707. #define DDC2 2
  708. #define DDC3 3
  709. #define DDC4 4
  710. #define DDC5 5
  711. #define DDC6 6
  712. #define PORTC0 0
  713. #define PORTC1 1
  714. #define PORTC2 2
  715. #define PORTC3 3
  716. #define PORTC4 4
  717. #define PORTC5 5
  718. #define PORTC6 6
  719. #define PIND0 0
  720. #define PIND1 1
  721. #define PIND2 2
  722. #define PIND3 3
  723. #define PIND4 4
  724. #define PIND5 5
  725. #define PIND6 6
  726. #define PIND7 7
  727. #define DDD0 0
  728. #define DDD1 1
  729. #define DDD2 2
  730. #define DDD3 3
  731. #define DDD4 4
  732. #define DDD5 5
  733. #define DDD6 6
  734. #define DDD7 7
  735. #define PORTD0 0
  736. #define PORTD1 1
  737. #define PORTD2 2
  738. #define PORTD3 3
  739. #define PORTD4 4
  740. #define PORTD5 5
  741. #define PORTD6 6
  742. #define PORTD7 7
  743. #if 0
  744. extern "C" {
  745. void serial_print(const char *p);
  746. void serial_phex(uint32_t n);
  747. void serial_phex16(uint32_t n);
  748. void serial_phex32(uint32_t n);
  749. }
  750. #endif
  751. // SPI Control Register ­ SPCR
  752. #define SPIE 7 // SPI Interrupt Enable - not supported
  753. #define SPE 6 // SPI Enable
  754. #define DORD 5 // DORD: Data Order
  755. #define MSTR 4 // MSTR: Master/Slave Select
  756. #define CPOL 3 // CPOL: Clock Polarity
  757. #define CPHA 2 // CPHA: Clock Phase
  758. #define SPR1 1 // Clock: 3 = 125 kHz, 2 = 250 kHz, 1 = 1 MHz, 0->4 MHz
  759. #define SPR0 0
  760. // SPI Status Register ­ SPSR
  761. #define SPIF 7 // SPIF: SPI Interrupt Flag
  762. #define WCOL 6 // WCOL: Write COLlision Flag - not implemented
  763. #define SPI2X 0 // SPI2X: Double SPI Speed Bit
  764. // SPI Data Register ­ SPDR
  765. class SPCRemulation;
  766. class SPSRemulation;
  767. class SPDRemulation;
  768. #if defined(KINETISK)
  769. class SPCRemulation
  770. {
  771. public:
  772. inline SPCRemulation & operator = (int val) __attribute__((always_inline)) {
  773. uint32_t ctar, mcr, sim6;
  774. //serial_print("SPCR=");
  775. //serial_phex(val);
  776. //serial_print("\n");
  777. sim6 = SIM_SCGC6;
  778. if (!(sim6 & SIM_SCGC6_SPI0)) {
  779. //serial_print("init1\n");
  780. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI0;
  781. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  782. }
  783. if (!(val & (1<<SPE))) {
  784. SPI0_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  785. }
  786. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  787. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  788. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  789. if (val & (1<<CPHA)) {
  790. ctar |= SPI_CTAR_CPHA;
  791. if ((val & 3) == 0) {
  792. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  793. } else if ((val & 3) == 1) {
  794. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  795. } else if ((val & 3) == 2) {
  796. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  797. } else {
  798. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  799. }
  800. } else {
  801. if ((val & 3) == 0) {
  802. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  803. } else if ((val & 3) == 1) {
  804. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  805. } else if ((val & 3) == 2) {
  806. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  807. } else {
  808. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  809. }
  810. }
  811. ctar |= (SPI0_CTAR0 & SPI_CTAR_DBR);
  812. update_ctar(ctar);
  813. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  814. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  815. if (val & (1<<SPE)) {
  816. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  817. SPI0_MCR = mcr;
  818. enable_pins();
  819. } else {
  820. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  821. SPI0_MCR = mcr;
  822. disable_pins();
  823. }
  824. //serial_print("MCR:");
  825. //serial_phex32(SPI0_MCR);
  826. //serial_print(", CTAR0:");
  827. //serial_phex32(SPI0_CTAR0);
  828. //serial_print("\n");
  829. return *this;
  830. }
  831. inline SPCRemulation & operator |= (int val) __attribute__((always_inline)) {
  832. uint32_t sim6;
  833. //serial_print("SPCR |= ");
  834. //serial_phex(val);
  835. //serial_print("\n");
  836. sim6 = SIM_SCGC6;
  837. if (!(sim6 & SIM_SCGC6_SPI0)) {
  838. //serial_print("init2\n");
  839. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI0;
  840. SPI0_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  841. }
  842. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  843. uint32_t ctar = SPI0_CTAR0;
  844. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  845. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  846. if ((val & 3) == 1) {
  847. // TODO: implement - is this ever really needed
  848. } else if ((val & 3) == 2) {
  849. // TODO: implement - is this ever really needed
  850. } else if ((val & 3) == 3) {
  851. // TODO: implement - is this ever really needed
  852. }
  853. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  854. ctar |= SPI_CTAR_CPHA;
  855. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  856. }
  857. update_ctar(ctar);
  858. }
  859. if (val & (1<<MSTR)) SPI0_MCR |= SPI_MCR_MSTR;
  860. if (val & (1<<SPE)) {
  861. SPI0_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  862. enable_pins();
  863. }
  864. //serial_print("MCR:");
  865. //serial_phex32(SPI0_MCR);
  866. //serial_print(", CTAR0:");
  867. //serial_phex32(SPI0_CTAR0);
  868. //serial_print("\n");
  869. return *this;
  870. }
  871. inline SPCRemulation & operator &= (int val) __attribute__((always_inline)) {
  872. //serial_print("SPCR &= ");
  873. //serial_phex(val);
  874. //serial_print("\n");
  875. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  876. if (!(val & (1<<SPE))) {
  877. SPI0_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  878. disable_pins();
  879. }
  880. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  881. uint32_t ctar = SPI0_CTAR0;
  882. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  883. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  884. if ((val & 3) == 0) {
  885. // TODO: implement - is this ever really needed
  886. } else if ((val & 3) == 1) {
  887. // TODO: implement - is this ever really needed
  888. } else if ((val & 3) == 2) {
  889. // TODO: implement - is this ever really needed
  890. }
  891. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  892. ctar &= ~SPI_CTAR_CPHA;
  893. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  894. }
  895. update_ctar(ctar);
  896. }
  897. if (!(val & (1<<MSTR))) SPI0_MCR &= ~SPI_MCR_MSTR;
  898. return *this;
  899. }
  900. inline int operator & (int val) const __attribute__((always_inline)) {
  901. int ret = 0;
  902. //serial_print("SPCR & ");
  903. //serial_phex(val);
  904. //serial_print("\n");
  905. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  906. if ((val & (1<<DORD)) && (SPI0_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  907. if ((val & (1<<CPOL)) && (SPI0_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  908. if ((val & (1<<CPHA)) && (SPI0_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  909. if ((val & 3) == 3) {
  910. uint32_t dbr = SPI0_CTAR0 & 15;
  911. if (dbr <= 1) {
  912. } else if (dbr <= 4) {
  913. ret |= (1<<SPR0);
  914. } else if (dbr <= 6) {
  915. ret |= (1<<SPR1);
  916. } else {
  917. ret |= (1<<SPR1)|(1<<SPR0);
  918. }
  919. } else if ((val & 3) == 1) {
  920. // TODO: implement - is this ever really needed
  921. } else if ((val & 3) == 2) {
  922. // TODO: implement - is this ever really needed
  923. }
  924. if (val & (1<<SPE) && (!(SPI0_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  925. if (val & (1<<MSTR) && (SPI0_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  926. //serial_print("ret = ");
  927. //serial_phex(ret);
  928. //serial_print("\n");
  929. return ret;
  930. }
  931. operator int () const __attribute__((always_inline)) {
  932. int ret = 0;
  933. if ((SIM_SCGC6 & SIM_SCGC6_SPI0)) {
  934. int ctar = SPI0_CTAR0;
  935. if (ctar & SPI_CTAR_LSBFE) ret |= (1<<DORD);
  936. if (ctar & SPI_CTAR_CPOL) ret |= (1<<CPOL);
  937. if (ctar & SPI_CTAR_CPHA) ret |= (1<<CPHA);
  938. ctar &= 15;
  939. if (ctar <= 1) {
  940. } else if (ctar <= 4) {
  941. ret |= (1<<SPR0);
  942. } else if (ctar <= 6) {
  943. ret |= (1<<SPR1);
  944. } else {
  945. ret |= (1<<SPR1)|(1<<SPR0);
  946. }
  947. int mcr = SPI0_MCR;
  948. if (!(mcr & SPI_MCR_MDIS)) ret |= (1<<SPE);
  949. if (mcr & SPI_MCR_MSTR) ret |= (1<<MSTR);
  950. }
  951. return ret;
  952. }
  953. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  954. uint8_t newpinout = pinout;
  955. if (pin == 11) newpinout &= ~1;
  956. if (pin == 7) newpinout |= 1;
  957. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  958. if ((newpinout & 1) == 0) {
  959. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  960. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  961. } else {
  962. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  963. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  964. }
  965. }
  966. pinout = newpinout;
  967. }
  968. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  969. uint8_t newpinout = pinout;
  970. if (pin == 12) newpinout &= ~2;
  971. if (pin == 8) newpinout |= 2;
  972. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  973. if ((newpinout & 2) == 0) {
  974. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  975. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  976. } else {
  977. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  978. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  979. }
  980. }
  981. pinout = newpinout;
  982. }
  983. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  984. uint8_t newpinout = pinout;
  985. if (pin == 13) newpinout &= ~4;
  986. if (pin == 14) newpinout |= 4;
  987. if ((SIM_SCGC6 & SIM_SCGC6_SPI0) && newpinout != pinout) {
  988. if ((newpinout & 4) == 0) {
  989. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  990. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  991. } else {
  992. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  993. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  994. }
  995. }
  996. pinout = newpinout;
  997. }
  998. friend class SPSRemulation;
  999. friend class SPIFIFOclass;
  1000. private:
  1001. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1002. if (SPI0_CTAR0 == ctar) return;
  1003. uint32_t mcr = SPI0_MCR;
  1004. if (mcr & SPI_MCR_MDIS) {
  1005. SPI0_CTAR0 = ctar;
  1006. } else {
  1007. SPI0_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1008. SPI0_CTAR0 = ctar;
  1009. SPI0_MCR = mcr;
  1010. }
  1011. }
  1012. static uint8_t pinout;
  1013. public:
  1014. inline void enable_pins(void) __attribute__((always_inline)) {
  1015. //serial_print("enable_pins\n");
  1016. if ((pinout & 1) == 0) {
  1017. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // DOUT/MOSI = 11 (PTC6)
  1018. } else {
  1019. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // DOUT/MOSI = 7 (PTD2)
  1020. }
  1021. if ((pinout & 2) == 0) {
  1022. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // DIN/MISO = 12 (PTC7)
  1023. } else {
  1024. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // DIN/MISO = 8 (PTD3)
  1025. }
  1026. if ((pinout & 4) == 0) {
  1027. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK = 13 (PTC5)
  1028. } else {
  1029. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK = 14 (PTD1)
  1030. }
  1031. }
  1032. inline void disable_pins(void) __attribute__((always_inline)) {
  1033. //serial_print("disable_pins\n");
  1034. if ((pinout & 1) == 0) {
  1035. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1036. } else {
  1037. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1038. }
  1039. if ((pinout & 2) == 0) {
  1040. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1041. } else {
  1042. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1043. }
  1044. if ((pinout & 4) == 0) {
  1045. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1046. } else {
  1047. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1048. }
  1049. }
  1050. };
  1051. extern SPCRemulation SPCR;
  1052. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1053. class SPCR1emulation
  1054. {
  1055. public:
  1056. inline SPCR1emulation & operator = (int val) __attribute__((always_inline)) {
  1057. uint32_t ctar, mcr, sim6;
  1058. //serial_print("SPCR=");
  1059. //serial_phex(val);
  1060. //serial_print("\n");
  1061. sim6 = SIM_SCGC6;
  1062. if (!(sim6 & SIM_SCGC6_SPI1)) {
  1063. //serial_print("init1\n");
  1064. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI1;
  1065. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1066. }
  1067. if (!(val & (1<<SPE))) {
  1068. SPI1_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  1069. }
  1070. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  1071. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  1072. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1073. if (val & (1<<CPHA)) {
  1074. ctar |= SPI_CTAR_CPHA;
  1075. if ((val & 3) == 0) {
  1076. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  1077. } else if ((val & 3) == 1) {
  1078. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  1079. } else if ((val & 3) == 2) {
  1080. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  1081. } else {
  1082. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  1083. }
  1084. } else {
  1085. if ((val & 3) == 0) {
  1086. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1087. } else if ((val & 3) == 1) {
  1088. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  1089. } else if ((val & 3) == 2) {
  1090. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  1091. } else {
  1092. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  1093. }
  1094. }
  1095. ctar |= (SPI1_CTAR0 & SPI_CTAR_DBR);
  1096. update_ctar(ctar);
  1097. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  1098. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  1099. if (val & (1<<SPE)) {
  1100. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1101. SPI1_MCR = mcr;
  1102. enable_pins();
  1103. } else {
  1104. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1105. SPI1_MCR = mcr;
  1106. disable_pins();
  1107. }
  1108. //serial_print("MCR:");
  1109. //serial_phex32(SPI1_MCR);
  1110. //serial_print(", CTAR0:");
  1111. //serial_phex32(SPI1_CTAR0);
  1112. //serial_print("\n");
  1113. return *this;
  1114. }
  1115. inline SPCR1emulation & operator |= (int val) __attribute__((always_inline)) {
  1116. uint32_t sim6;
  1117. //serial_print("SPCR |= ");
  1118. //serial_phex(val);
  1119. //serial_print("\n");
  1120. sim6 = SIM_SCGC6;
  1121. if (!(sim6 & SIM_SCGC6_SPI1)) {
  1122. //serial_print("init2\n");
  1123. SIM_SCGC6 = sim6 | SIM_SCGC6_SPI1;
  1124. SPI1_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  1125. }
  1126. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1127. uint32_t ctar = SPI1_CTAR0;
  1128. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  1129. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1130. if ((val & 3) == 1) {
  1131. // TODO: implement - is this ever really needed
  1132. } else if ((val & 3) == 2) {
  1133. // TODO: implement - is this ever really needed
  1134. } else if ((val & 3) == 3) {
  1135. // TODO: implement - is this ever really needed
  1136. }
  1137. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  1138. ctar |= SPI_CTAR_CPHA;
  1139. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  1140. }
  1141. update_ctar(ctar);
  1142. }
  1143. if (val & (1<<MSTR)) SPI1_MCR |= SPI_MCR_MSTR;
  1144. if (val & (1<<SPE)) {
  1145. SPI1_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1146. enable_pins();
  1147. }
  1148. //serial_print("MCR:");
  1149. //serial_phex32(SPI1_MCR);
  1150. //serial_print(", CTAR0:");
  1151. //serial_phex32(SPI1_CTAR0);
  1152. //serial_print("\n");
  1153. return *this;
  1154. }
  1155. inline SPCR1emulation & operator &= (int val) __attribute__((always_inline)) {
  1156. //serial_print("SPCR &= ");
  1157. //serial_phex(val);
  1158. //serial_print("\n");
  1159. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  1160. if (!(val & (1<<SPE))) {
  1161. SPI1_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1162. disable_pins();
  1163. }
  1164. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1165. uint32_t ctar = SPI1_CTAR0;
  1166. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  1167. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  1168. if ((val & 3) == 0) {
  1169. // TODO: implement - is this ever really needed
  1170. } else if ((val & 3) == 1) {
  1171. // TODO: implement - is this ever really needed
  1172. } else if ((val & 3) == 2) {
  1173. // TODO: implement - is this ever really needed
  1174. }
  1175. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  1176. ctar &= ~SPI_CTAR_CPHA;
  1177. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  1178. }
  1179. update_ctar(ctar);
  1180. }
  1181. if (!(val & (1<<MSTR))) SPI1_MCR &= ~SPI_MCR_MSTR;
  1182. return *this;
  1183. }
  1184. inline int operator & (int val) const __attribute__((always_inline)) {
  1185. int ret = 0;
  1186. //serial_print("SPCR & ");
  1187. //serial_phex(val);
  1188. //serial_print(" MCR:");
  1189. //serial_phex32(SPI1_MCR);
  1190. //serial_print(", CTAR0:");
  1191. //serial_phex32(SPI1_CTAR0);
  1192. //serial_print("\n");
  1193. //serial_print("\n");
  1194. SIM_SCGC6 |= SIM_SCGC6_SPI1;
  1195. if ((val & (1<<DORD)) && (SPI1_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  1196. if ((val & (1<<CPOL)) && (SPI1_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  1197. if ((val & (1<<CPHA)) && (SPI1_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  1198. if ((val & 3) == 3) {
  1199. uint32_t dbr = SPI1_CTAR0 & 15;
  1200. if (dbr <= 1) {
  1201. } else if (dbr <= 4) {
  1202. ret |= (1<<SPR0);
  1203. } else if (dbr <= 6) {
  1204. ret |= (1<<SPR1);
  1205. } else {
  1206. ret |= (1<<SPR1)|(1<<SPR0);
  1207. }
  1208. } else if ((val & 3) == 1) {
  1209. // TODO: implement - is this ever really needed
  1210. } else if ((val & 3) == 2) {
  1211. // TODO: implement - is this ever really needed
  1212. }
  1213. if (val & (1<<SPE) && (!(SPI1_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  1214. if (val & (1<<MSTR) && (SPI1_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  1215. //serial_print("ret = ");
  1216. //serial_phex(ret);
  1217. //serial_print("\n");
  1218. return ret;
  1219. }
  1220. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1221. #ifdef USE_SDCARD_PINS
  1222. // More options, so 2 bits
  1223. pinout &= ~3;
  1224. switch (pin) {
  1225. case 0: break;
  1226. case 21: pinout |= 1; break;
  1227. case 61: pinout |= 2; break;
  1228. case 59: pinout |= 3; break;
  1229. }
  1230. #else
  1231. if (pin == 0) pinout &= ~1; // MOSI1 = 0 (PTB16)
  1232. if (pin == 21) pinout |= 1; // MOSI1 = 21 (PTD6)
  1233. #endif
  1234. }
  1235. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1236. #ifdef USE_SDCARD_PINS
  1237. // More options, so 2 bits
  1238. pinout &= ~0xc;
  1239. switch (pin) {
  1240. case 1: break;
  1241. case 5: pinout |= 0x4; break;
  1242. case 61: pinout |= 0x8; break;
  1243. case 59: pinout |= 0xc; break;
  1244. }
  1245. #else
  1246. if (pin == 1) pinout &= ~2; // MISO1 = 1 (PTB17)
  1247. if (pin == 5) pinout |= 2; // MISO1 = 5 (PTD7)
  1248. #endif
  1249. }
  1250. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1251. #ifdef USE_SDCARD_PINS
  1252. // More options, so 2 bits
  1253. pinout &= ~0x30;
  1254. switch (pin) {
  1255. case 20: break;
  1256. case 32: pinout |= 0x10; break;
  1257. case 60: pinout |= 0x20; break;
  1258. }
  1259. #else
  1260. if (pin == 20) pinout &= ~4; // SCK = 20 (PTD5)
  1261. if (pin == 32) pinout |= 4; // MISO1 = 32 (PTB11)
  1262. #endif
  1263. }
  1264. inline void enable_pins(void) __attribute__((always_inline)) {
  1265. //serial_print("enable_pins\n");
  1266. #ifdef USE_SDCARD_PINS
  1267. // MOSI (SOUT)
  1268. switch (pinout & 0x3) {
  1269. case 0: CORE_PIN0_CONFIG = PORT_PCR_MUX(2); break;
  1270. case 1: CORE_PIN21_CONFIG = PORT_PCR_MUX(7); break;
  1271. case 2: CORE_PIN61_CONFIG = PORT_PCR_MUX(7); break;
  1272. case 3: CORE_PIN59_CONFIG = PORT_PCR_MUX(2); break;
  1273. }
  1274. // MISO (SIN)
  1275. switch (pinout & 0xc) {
  1276. case 0x0: CORE_PIN1_CONFIG = PORT_PCR_MUX(2); break;
  1277. case 0x4: CORE_PIN5_CONFIG = PORT_PCR_MUX(7); break;
  1278. case 0x8: CORE_PIN61_CONFIG = PORT_PCR_MUX(2); break;
  1279. case 0xc: CORE_PIN59_CONFIG = PORT_PCR_MUX(7); break;
  1280. }
  1281. // SCK
  1282. switch (pinout & 0x30) {
  1283. case 0x0: CORE_PIN20_CONFIG = PORT_PCR_MUX(7); break;
  1284. case 0x10: CORE_PIN32_CONFIG = PORT_PCR_MUX(2); break;
  1285. case 0x20: CORE_PIN60_CONFIG = PORT_PCR_MUX(2); break;
  1286. }
  1287. #else
  1288. if ((pinout & 1) == 0) {
  1289. CORE_PIN0_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 0 (PTB16)
  1290. } else {
  1291. CORE_PIN21_CONFIG = PORT_PCR_MUX(7); // MOSI1 = 21 (PTD6)
  1292. }
  1293. if ((pinout & 2) == 0) {
  1294. CORE_PIN1_CONFIG = PORT_PCR_MUX(2); // MISO1 = 1 (PTB17)
  1295. } else {
  1296. CORE_PIN5_CONFIG = PORT_PCR_MUX(7); // MISO1 = 5 (PTD7)
  1297. }
  1298. if ((pinout & 4) == 0) {
  1299. CORE_PIN20_CONFIG = PORT_PCR_MUX(7); // SCK1 = 20 (PTD5)
  1300. } else {
  1301. CORE_PIN32_CONFIG = PORT_PCR_MUX(2); // MISO1 = 5 (PTD7)
  1302. }
  1303. #endif
  1304. }
  1305. inline void disable_pins(void) __attribute__((always_inline)) {
  1306. #ifdef USE_SDCARD_PINS
  1307. switch (pinout & 0x3) {
  1308. case 0: CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1309. case 1: CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1310. case 2: CORE_PIN61_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1311. case 3: CORE_PIN59_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1312. }
  1313. switch (pinout & 0xc) {
  1314. case 0x0: CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1315. case 0x4: CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1316. case 0x8: CORE_PIN61_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1317. case 0xc: CORE_PIN59_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1318. }
  1319. switch (pinout & 0x30) {
  1320. case 0x0: CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1321. case 0x10: CORE_PIN32_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1322. case 0x20: CORE_PIN60_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1323. }
  1324. #else
  1325. //serial_print("disable_pins\n");
  1326. if ((pinout & 1) == 0) {
  1327. CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1328. } else {
  1329. CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1330. }
  1331. if ((pinout & 2) == 0) {
  1332. CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1333. } else {
  1334. CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1335. }
  1336. if ((pinout & 4) == 0) {
  1337. CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); // SCK1 = 20 (PTD5)
  1338. } else {
  1339. CORE_PIN32_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); // MISO1 = 5 (PTD7)
  1340. }
  1341. #endif
  1342. }
  1343. friend class SPIFIFO1class;
  1344. private:
  1345. static uint8_t pinout;
  1346. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1347. if (SPI1_CTAR0 == ctar) return;
  1348. uint32_t mcr = SPI1_MCR;
  1349. if (mcr & SPI_MCR_MDIS) {
  1350. SPI1_CTAR0 = ctar;
  1351. } else {
  1352. SPI1_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1353. SPI1_CTAR0 = ctar;
  1354. SPI1_MCR = mcr;
  1355. }
  1356. }
  1357. };
  1358. extern SPCR1emulation SPCR1;
  1359. ////////////////////
  1360. // SPI2
  1361. class SPCR2emulation
  1362. {
  1363. public:
  1364. inline SPCR2emulation & operator = (int val) __attribute__((always_inline)) {
  1365. uint32_t ctar, mcr, sim3;
  1366. //serial_print("SPCR=");
  1367. //serial_phex(val);
  1368. //serial_print("\n");
  1369. sim3 = SIM_SCGC3;
  1370. if (!(sim3 & SIM_SCGC3_SPI2)) {
  1371. //serial_print("init1\n");
  1372. SIM_SCGC3 = sim3 | SIM_SCGC3_SPI2;
  1373. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1374. }
  1375. if (!(val & (1<<SPE))) {
  1376. SPI2_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  1377. }
  1378. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  1379. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  1380. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1381. if (val & (1<<CPHA)) {
  1382. ctar |= SPI_CTAR_CPHA;
  1383. if ((val & 3) == 0) {
  1384. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  1385. } else if ((val & 3) == 1) {
  1386. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  1387. } else if ((val & 3) == 2) {
  1388. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  1389. } else {
  1390. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  1391. }
  1392. } else {
  1393. if ((val & 3) == 0) {
  1394. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1395. } else if ((val & 3) == 1) {
  1396. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  1397. } else if ((val & 3) == 2) {
  1398. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  1399. } else {
  1400. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  1401. }
  1402. }
  1403. ctar |= (SPI2_CTAR0 & SPI_CTAR_DBR);
  1404. update_ctar(ctar);
  1405. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  1406. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  1407. if (val & (1<<SPE)) {
  1408. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1409. SPI2_MCR = mcr;
  1410. enable_pins();
  1411. } else {
  1412. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1413. SPI2_MCR = mcr;
  1414. disable_pins();
  1415. }
  1416. //serial_print("MCR:");
  1417. //serial_phex32(SPI2_MCR);
  1418. //serial_print(", CTAR0:");
  1419. //serial_phex32(SPI2_CTAR0);
  1420. //serial_print("\n");
  1421. return *this;
  1422. }
  1423. inline SPCR2emulation & operator |= (int val) __attribute__((always_inline)) {
  1424. uint32_t sim3;
  1425. //serial_print("SPCR |= ");
  1426. //serial_phex(val);
  1427. //serial_print("\n");
  1428. sim3 = SIM_SCGC3;
  1429. if (!(sim3 & SIM_SCGC3_SPI2)) {
  1430. //serial_print("init2\n");
  1431. SIM_SCGC6 = sim3 | SIM_SCGC3_SPI2;
  1432. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  1433. }
  1434. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1435. uint32_t ctar = SPI2_CTAR0;
  1436. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  1437. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1438. if ((val & 3) == 1) {
  1439. // TODO: implement - is this ever really needed
  1440. } else if ((val & 3) == 2) {
  1441. // TODO: implement - is this ever really needed
  1442. } else if ((val & 3) == 3) {
  1443. // TODO: implement - is this ever really needed
  1444. }
  1445. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  1446. ctar |= SPI_CTAR_CPHA;
  1447. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  1448. }
  1449. update_ctar(ctar);
  1450. }
  1451. if (val & (1<<MSTR)) SPI2_MCR |= SPI_MCR_MSTR;
  1452. if (val & (1<<SPE)) {
  1453. SPI2_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1454. enable_pins();
  1455. }
  1456. //serial_print("MCR:");
  1457. //serial_phex32(SPI2_MCR);
  1458. //serial_print(", CTAR0:");
  1459. //serial_phex32(SPI2_CTAR0);
  1460. //serial_print("\n");
  1461. return *this;
  1462. }
  1463. inline SPCR2emulation & operator &= (int val) __attribute__((always_inline)) {
  1464. //serial_print("SPCR &= ");
  1465. //serial_phex(val);
  1466. //serial_print("\n");
  1467. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  1468. if (!(val & (1<<SPE))) {
  1469. SPI2_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1470. disable_pins();
  1471. }
  1472. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1473. uint32_t ctar = SPI2_CTAR0;
  1474. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  1475. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  1476. if ((val & 3) == 0) {
  1477. // TODO: implement - is this ever really needed
  1478. } else if ((val & 3) == 1) {
  1479. // TODO: implement - is this ever really needed
  1480. } else if ((val & 3) == 2) {
  1481. // TODO: implement - is this ever really needed
  1482. }
  1483. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  1484. ctar &= ~SPI_CTAR_CPHA;
  1485. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  1486. }
  1487. update_ctar(ctar);
  1488. }
  1489. if (!(val & (1<<MSTR))) SPI2_MCR &= ~SPI_MCR_MSTR;
  1490. return *this;
  1491. }
  1492. inline int operator & (int val) const __attribute__((always_inline)) {
  1493. int ret = 0;
  1494. //serial_print("SPCR & ");
  1495. //serial_phex(val);
  1496. //serial_print(" MCR:");
  1497. //serial_phex32(SPI2_MCR);
  1498. //serial_print(", CTAR0:");
  1499. //serial_phex32(SPI2_CTAR0);
  1500. //serial_print("\n");
  1501. //serial_print("\n");
  1502. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  1503. if ((val & (1<<DORD)) && (SPI2_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  1504. if ((val & (1<<CPOL)) && (SPI2_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  1505. if ((val & (1<<CPHA)) && (SPI2_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  1506. if ((val & 3) == 3) {
  1507. uint32_t dbr = SPI2_CTAR0 & 15;
  1508. if (dbr <= 1) {
  1509. } else if (dbr <= 4) {
  1510. ret |= (1<<SPR0);
  1511. } else if (dbr <= 6) {
  1512. ret |= (1<<SPR1);
  1513. } else {
  1514. ret |= (1<<SPR1)|(1<<SPR0);
  1515. }
  1516. } else if ((val & 3) == 1) {
  1517. // TODO: implement - is this ever really needed
  1518. } else if ((val & 3) == 2) {
  1519. // TODO: implement - is this ever really needed
  1520. }
  1521. if (val & (1<<SPE) && (!(SPI2_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  1522. if (val & (1<<MSTR) && (SPI2_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  1523. //serial_print("ret = ");
  1524. //serial_phex(ret);
  1525. //serial_print("\n");
  1526. return ret;
  1527. }
  1528. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1529. if (pin == 44) pinout &= ~1;
  1530. if (pin == 52) pinout |= 1;
  1531. }
  1532. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1533. if (pin == 45) pinout &= ~2;
  1534. if (pin == 51) pinout |= 2;
  1535. }
  1536. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1537. if (pin == 46) pinout &= ~4;
  1538. if (pin == 53) pinout |= 4;
  1539. }
  1540. inline void enable_pins(void) __attribute__((always_inline)) {
  1541. //serial_print("enable_pins\n");
  1542. if ((pinout & 1) == 0) {
  1543. CORE_PIN44_CONFIG = PORT_PCR_MUX(2);
  1544. } else {
  1545. CORE_PIN52_CONFIG = PORT_PCR_MUX(2);
  1546. }
  1547. if ((pinout & 2) == 0) {
  1548. CORE_PIN45_CONFIG = PORT_PCR_MUX(2);
  1549. } else {
  1550. CORE_PIN51_CONFIG = PORT_PCR_MUX(2);
  1551. }
  1552. if ((pinout & 4) == 0) {
  1553. CORE_PIN46_CONFIG = PORT_PCR_MUX(2);
  1554. } else {
  1555. CORE_PIN53_CONFIG = PORT_PCR_MUX(2);
  1556. }
  1557. }
  1558. inline void disable_pins(void) __attribute__((always_inline)) {
  1559. //serial_print("disable_pins\n");
  1560. if ((pinout & 1) == 0) {
  1561. CORE_PIN44_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1562. } else {
  1563. CORE_PIN52_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1564. }
  1565. if ((pinout & 2) == 0) {
  1566. CORE_PIN45_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1567. } else {
  1568. CORE_PIN51_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1569. }
  1570. if ((pinout & 4) == 0) {
  1571. CORE_PIN46_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1572. } else {
  1573. CORE_PIN53_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1574. }
  1575. }
  1576. friend class SPIFIFO1class;
  1577. private:
  1578. static uint8_t pinout;
  1579. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1580. if (SPI2_CTAR0 == ctar) return;
  1581. uint32_t mcr = SPI2_MCR;
  1582. if (mcr & SPI_MCR_MDIS) {
  1583. SPI2_CTAR0 = ctar;
  1584. } else {
  1585. SPI2_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1586. SPI2_CTAR0 = ctar;
  1587. SPI2_MCR = mcr;
  1588. }
  1589. }
  1590. };
  1591. extern SPCR2emulation SPCR2;
  1592. #endif
  1593. class SPSRemulation
  1594. {
  1595. public:
  1596. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1597. //serial_print("SPSR=");
  1598. //serial_phex(val);
  1599. //serial_print("\n");
  1600. uint32_t ctar = SPI0_CTAR0;
  1601. if (val & (1<<SPI2X)) {
  1602. ctar |= SPI_CTAR_DBR;
  1603. } else {
  1604. ctar &= ~SPI_CTAR_DBR;
  1605. }
  1606. SPCRemulation::update_ctar(ctar);
  1607. //serial_print("MCR:");
  1608. //serial_phex32(SPI0_MCR);
  1609. //serial_print(", CTAR0:");
  1610. //serial_phex32(SPI0_CTAR0);
  1611. //serial_print("\n");
  1612. return *this;
  1613. }
  1614. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1615. //serial_print("SPSR |= ");
  1616. //serial_phex(val);
  1617. //serial_print("\n");
  1618. if (val & (1<<SPI2X)) SPCRemulation::update_ctar(SPI0_CTAR0 |= SPI_CTAR_DBR);
  1619. return *this;
  1620. }
  1621. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1622. //serial_print("SPSR &= ");
  1623. //serial_phex(val);
  1624. //serial_print("\n");
  1625. if (!(val & (1<<SPI2X))) SPCRemulation::update_ctar(SPI0_CTAR0 &= ~SPI_CTAR_DBR);
  1626. return *this;
  1627. }
  1628. inline int operator & (int val) const __attribute__((always_inline)) {
  1629. int ret = 0;
  1630. //serial_print("SPSR & ");
  1631. //serial_phex(val);
  1632. //serial_print("\n");
  1633. // TODO: using SPI_SR_TCF isn't quite right. Control returns to the
  1634. // caller after the final edge that captures data, which is 1/2 cycle
  1635. // sooner than AVR returns. At 500 kHz and slower SPI, this can make
  1636. // a difference when digitalWrite is used to manually control the CS
  1637. // pin, and perhaps it could matter at high clocks if faster register
  1638. // access is used? But does it really matter? Do any SPI chips in
  1639. // practice really perform differently if CS negates early, after the
  1640. // final bit is clocked, but before the end of the whole clock cycle?
  1641. if ((val & (1<<SPIF)) && (SPI0_SR & SPI_SR_TCF)) ret = (1<<SPIF);
  1642. if ((val & (1<<SPI2X)) && (SPI0_CTAR0 & SPI_CTAR_DBR)) ret |= (1<<SPI2X);
  1643. //delayMicroseconds(50000);
  1644. return ret;
  1645. }
  1646. operator int () const __attribute__((always_inline)) {
  1647. int ret = 0;
  1648. //serial_print("SPSR (int)\n");
  1649. if (SPI0_SR & SPI_SR_TCF) ret = (1<<SPIF);
  1650. if (SPI0_CTAR0 & SPI_CTAR_DBR) ret |= (1<<SPI2X);
  1651. return ret;
  1652. }
  1653. };
  1654. extern SPSRemulation SPSR;
  1655. class SPDRemulation
  1656. {
  1657. public:
  1658. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1659. //serial_print("SPDR = ");
  1660. //serial_phex(val);
  1661. //serial_print("\n");
  1662. SPI0_MCR |= SPI_MCR_CLR_RXF; // discard any received data
  1663. SPI0_SR = SPI_SR_TCF;
  1664. //SPI0_SR = SPI_SR_EOQF;
  1665. //SPI0_PUSHR = (val & 255) | SPI0_PUSHR_EOQ;
  1666. SPI0_PUSHR = (val & 255);
  1667. return *this;
  1668. }
  1669. operator int () const __attribute__((always_inline)) {
  1670. uint32_t val;
  1671. val = SPI0_POPR & 255;
  1672. //serial_print("SPDR (int) ");
  1673. //serial_phex(val);
  1674. //serial_print("\n");
  1675. return val;
  1676. }
  1677. };
  1678. extern SPDRemulation SPDR;
  1679. #elif defined(KINETISL)
  1680. // SPI Control Register ­ SPCR
  1681. //#define SPIE 7 // SPI Interrupt Enable - not supported
  1682. //#define SPE 6 // SPI Enable
  1683. //#define DORD 5 // DORD: Data Order
  1684. //#define MSTR 4 // MSTR: Master/Slave Select
  1685. //#define CPOL 3 // CPOL: Clock Polarity
  1686. //#define CPHA 2 // CPHA: Clock Phase
  1687. //#define SPR1 1 // Clock: 3 = 125 kHz, 2 = 250 kHz, 1 = 1 MHz, 0->4 MHz
  1688. //#define SPR0 0
  1689. // SPI Status Register ­ SPSR
  1690. //#define SPIF 7 // SPIF: SPI Interrupt Flag
  1691. //#define WCOL 6 // WCOL: Write COLlision Flag - not implemented
  1692. //#define SPI2X 0 // SPI2X: Double SPI Speed Bit
  1693. // SPI Data Register ­ SPDR
  1694. class SPCRemulation
  1695. {
  1696. public:
  1697. inline SPCRemulation & operator = (int val) __attribute__((always_inline)) {
  1698. uint32_t sim4 = SIM_SCGC4;
  1699. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1700. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1701. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1702. }
  1703. uint32_t c1 = 0;
  1704. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1705. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1706. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1707. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1708. if (val & (1<<SPE)) c1 |= SPI_C1_SPE;
  1709. SPI0_C1 = c1;
  1710. SPI0_C2 = 0;
  1711. uint32_t br = SPI0_BR & 0x10;
  1712. switch (val & 3) {
  1713. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1714. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1715. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1716. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1717. }
  1718. if (val & (1<<SPE)) enable_pins();
  1719. else disable_pins();
  1720. return *this;
  1721. }
  1722. inline SPCRemulation & operator |= (int val) __attribute__((always_inline)) {
  1723. uint32_t sim4 = SIM_SCGC4;
  1724. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1725. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1726. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1727. }
  1728. uint32_t c1 = SPI0_C1;
  1729. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1730. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1731. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1732. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1733. if (val & (1<<SPE)) {
  1734. enable_pins();
  1735. c1 |= SPI_C1_SPE;
  1736. }
  1737. SPI0_C1 = c1;
  1738. SPI0_C2 = 0;
  1739. val &= 3;
  1740. if (val) {
  1741. uint32_t br = SPI0_BR;
  1742. uint32_t bits = baud2avr(br) | val;
  1743. br &= 0x10;
  1744. switch (bits) {
  1745. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1746. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1747. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1748. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1749. }
  1750. }
  1751. return *this;
  1752. }
  1753. inline SPCRemulation & operator &= (int val) __attribute__((always_inline)) {
  1754. uint32_t sim4 = SIM_SCGC4;
  1755. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1756. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1757. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1758. }
  1759. uint32_t c1 = SPI0_C1;
  1760. if (!(val & (1<<DORD))) c1 &= ~SPI_C1_LSBFE;
  1761. if (!(val & (1<<CPOL))) c1 &= ~SPI_C1_CPOL;
  1762. if (!(val & (1<<CPHA))) c1 &= ~SPI_C1_CPHA;
  1763. if (!(val & (1<<MSTR))) c1 &= ~SPI_C1_MSTR;
  1764. if (!(val & (1<<SPE))) {
  1765. disable_pins();
  1766. c1 &= ~SPI_C1_SPE;
  1767. }
  1768. SPI0_C1 = c1;
  1769. SPI0_C2 = 0;
  1770. val &= 3;
  1771. if (val < 3) {
  1772. uint32_t br = SPI0_BR;
  1773. uint32_t bits = baud2avr(br) & val;
  1774. br &= 0x10;
  1775. switch (bits) {
  1776. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1777. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1778. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1779. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1780. }
  1781. }
  1782. return *this;
  1783. }
  1784. inline int operator & (int val) const __attribute__((always_inline)) {
  1785. int ret = 0;
  1786. uint32_t sim4 = SIM_SCGC4;
  1787. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1788. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1789. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1790. }
  1791. uint32_t c1 = SPI0_C1;
  1792. if ((val & (1<<DORD)) && (c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1793. if ((val & (1<<CPOL)) && (c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1794. if ((val & (1<<CPHA)) && (c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1795. if ((val & (1<<MSTR)) && (c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1796. if ((val & (1<<SPE)) && (c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1797. uint32_t bits = baud2avr(SPI0_BR);
  1798. if ((val & (1<<SPR1)) && (bits & (1<<SPR1))) ret |= (1<<SPR1);
  1799. if ((val & (1<<SPR0)) && (bits & (1<<SPR0))) ret |= (1<<SPR0);
  1800. return ret;
  1801. }
  1802. operator int () const __attribute__((always_inline)) {
  1803. int ret = 0;
  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 ((c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1811. if ((c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1812. if ((c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1813. if ((c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1814. if ((c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1815. ret |= baud2avr(SPI0_BR);
  1816. return ret;
  1817. }
  1818. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1819. uint8_t newpinout = pinout;
  1820. if (pin == 11) newpinout &= ~1;
  1821. if (pin == 7) newpinout |= 1;
  1822. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1823. if ((newpinout & 1) == 0) {
  1824. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1825. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1826. } else {
  1827. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1828. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  1829. }
  1830. }
  1831. pinout = newpinout;
  1832. }
  1833. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1834. uint8_t newpinout = pinout;
  1835. if (pin == 12) newpinout &= ~2;
  1836. if (pin == 8) newpinout |= 2;
  1837. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1838. if ((newpinout & 2) == 0) {
  1839. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1840. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  1841. } else {
  1842. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1843. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  1844. }
  1845. }
  1846. pinout = newpinout;
  1847. }
  1848. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1849. uint8_t newpinout = pinout;
  1850. if (pin == 13) newpinout &= ~4;
  1851. if (pin == 14) newpinout |= 4;
  1852. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1853. if ((newpinout & 4) == 0) {
  1854. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1855. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1856. } else {
  1857. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1858. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  1859. }
  1860. }
  1861. pinout = newpinout;
  1862. }
  1863. friend class SPSRemulation;
  1864. friend class SPIFIFOclass;
  1865. private:
  1866. static inline uint32_t baud2avr(uint32_t br) __attribute__((always_inline)) {
  1867. br &= 15;
  1868. if (br == 0) return 0;
  1869. if (br <= 2) return 1;
  1870. if (br <= 4) return 2;
  1871. return 3;
  1872. }
  1873. static uint8_t pinout;
  1874. public:
  1875. inline void enable_pins(void) __attribute__((always_inline)) {
  1876. //serial_print("enable_pins\n");
  1877. if ((pinout & 1) == 0) {
  1878. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // MOSI0 = 11 (PTC6)
  1879. } else {
  1880. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // MOSI0 = 7 (PTD2)
  1881. }
  1882. if ((pinout & 2) == 0) {
  1883. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // MISO0 = 12 (PTC7)
  1884. } else {
  1885. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // MISO0 = 8 (PTD3)
  1886. }
  1887. if ((pinout & 4) == 0) {
  1888. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK0 = 13 (PTC5)
  1889. } else {
  1890. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK0 = 14 (PTD1)
  1891. }
  1892. }
  1893. inline void disable_pins(void) __attribute__((always_inline)) {
  1894. //serial_print("disable_pins\n");
  1895. if ((pinout & 1) == 0) {
  1896. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1897. } else {
  1898. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1899. }
  1900. if ((pinout & 2) == 0) {
  1901. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1902. } else {
  1903. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1904. }
  1905. if ((pinout & 4) == 0) {
  1906. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1907. } else {
  1908. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1909. }
  1910. }
  1911. };
  1912. extern SPCRemulation SPCR;
  1913. class SPCR1emulation
  1914. {
  1915. public:
  1916. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1917. if (pin == 0) pinout &= ~1; // MOSI1 = 0 (PTB16)
  1918. if (pin == 21) pinout |= 1; // MOSI1 = 21 (PTD6)
  1919. }
  1920. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1921. if (pin == 1) pinout &= ~2; // MISO1 = 1 (PTB17)
  1922. if (pin == 5) pinout |= 2; // MISO1 = 5 (PTD7)
  1923. }
  1924. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1925. // SCK1 = 20 (PTD5) - no alternative pin
  1926. }
  1927. inline void enable_pins(void) __attribute__((always_inline)) {
  1928. //serial_print("enable_pins\n");
  1929. if ((pinout & 1) == 0) {
  1930. CORE_PIN0_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 0 (PTB16)
  1931. } else {
  1932. CORE_PIN21_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 21 (PTD6)
  1933. }
  1934. if ((pinout & 2) == 0) {
  1935. CORE_PIN1_CONFIG = PORT_PCR_MUX(2); // MISO1 = 1 (PTB17)
  1936. } else {
  1937. CORE_PIN5_CONFIG = PORT_PCR_MUX(2); // MISO1 = 5 (PTD7)
  1938. }
  1939. CORE_PIN20_CONFIG = PORT_PCR_MUX(2); // SCK1 = 20 (PTD5)
  1940. }
  1941. inline void disable_pins(void) __attribute__((always_inline)) {
  1942. //serial_print("disable_pins\n");
  1943. if ((pinout & 1) == 0) {
  1944. CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1945. } else {
  1946. CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1947. }
  1948. if ((pinout & 2) == 0) {
  1949. CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1950. } else {
  1951. CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1952. }
  1953. CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1954. }
  1955. friend class SPIFIFO1class;
  1956. private:
  1957. static uint8_t pinout;
  1958. };
  1959. extern SPCR1emulation SPCR1;
  1960. class SPSRemulation
  1961. {
  1962. public:
  1963. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1964. if (val & (1<<SPI2X)) {
  1965. SPI0_BR &= ~0x10;
  1966. } else {
  1967. SPI0_BR |= 0x10;
  1968. }
  1969. return *this;
  1970. }
  1971. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1972. if (val & (1<<SPI2X)) SPI0_BR &= ~0x10;
  1973. return *this;
  1974. }
  1975. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1976. if (!(val & (1<<SPI2X))) SPI0_BR |= 0x10;
  1977. return *this;
  1978. }
  1979. inline int operator & (int val) const __attribute__((always_inline)) {
  1980. int ret = 0;
  1981. if ((val & (1<<SPIF)) && (SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  1982. if ((val & (1<<SPI2X)) && (!(SPI0_BR & 0x10))) ret |= (1<<SPI2X);
  1983. return ret;
  1984. }
  1985. operator int () const __attribute__((always_inline)) {
  1986. int ret = 0;
  1987. if ((SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  1988. if (!(SPI0_BR & 0x10)) ret |= (1<<SPI2X);
  1989. return ret;
  1990. }
  1991. };
  1992. extern SPSRemulation SPSR;
  1993. class SPDRemulation
  1994. {
  1995. public:
  1996. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1997. if ((SPI0_S & SPI_S_SPTEF)) {
  1998. uint32_t tmp __attribute__((unused)) = SPI0_DL;
  1999. }
  2000. SPI0_DL = val;
  2001. return *this;
  2002. }
  2003. operator int () const __attribute__((always_inline)) {
  2004. return SPI0_DL & 255;
  2005. }
  2006. };
  2007. extern SPDRemulation SPDR;
  2008. #endif // KINETISK
  2009. class SREGemulation
  2010. {
  2011. public:
  2012. operator int () const __attribute__((always_inline)) {
  2013. uint32_t primask;
  2014. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  2015. if (primask) return 0;
  2016. return (1<<7);
  2017. }
  2018. inline SREGemulation & operator = (int val) __attribute__((always_inline)) {
  2019. if (val & (1<<7)) {
  2020. __enable_irq();
  2021. } else {
  2022. __disable_irq();
  2023. }
  2024. return *this;
  2025. }
  2026. };
  2027. extern SREGemulation SREG;
  2028. // 22211
  2029. // 84062840
  2030. // 322111
  2031. // 17395173
  2032. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  2033. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  2034. #define EIMSK_pA 0x01000018 // pins 3, 4, 24
  2035. #define EIMSK_pB 0x020F0003 // pins 0, 1, 16-19, 25
  2036. #define EIMSK_pC 0x78C0BE00 // pins 9-13, 15, 22, 23, 27-30
  2037. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2038. #define EIMSK_pE 0x84000000 // pins 26, 31
  2039. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  2040. #define EIMSK_pA 0x1E000018 // pins 3, 4, 25-28
  2041. #define EIMSK_pB 0xE00F0003 // pins 0, 1, 16-19, 29-31
  2042. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  2043. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2044. #define EIMSK_pE 0x01000000 // pins 24
  2045. #endif
  2046. class EIMSKemulation // used by Adafruit_nRF8001 (only supports INT for pins 0 to 31)
  2047. {
  2048. public:
  2049. operator int () const __attribute__((always_inline)) {
  2050. int mask = 0;
  2051. volatile const uint32_t *icer = &NVIC_ICER0;
  2052. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  2053. if (icer[IRQ_PORTB >> 5] & (1 << (IRQ_PORTB & 31))) mask |= EIMSK_pB;
  2054. if (icer[IRQ_PORTC >> 5] & (1 << (IRQ_PORTC & 31))) mask |= EIMSK_pC;
  2055. if (icer[IRQ_PORTD >> 5] & (1 << (IRQ_PORTD & 31))) mask |= EIMSK_pD;
  2056. if (icer[IRQ_PORTE >> 5] & (1 << (IRQ_PORTE & 31))) mask |= EIMSK_pE;
  2057. return mask;
  2058. }
  2059. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  2060. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  2061. if (val & EIMSK_pB) NVIC_ENABLE_IRQ(IRQ_PORTB);
  2062. if (val & EIMSK_pC) NVIC_ENABLE_IRQ(IRQ_PORTC);
  2063. if (val & EIMSK_pD) NVIC_ENABLE_IRQ(IRQ_PORTD);
  2064. if (val & EIMSK_pE) NVIC_ENABLE_IRQ(IRQ_PORTE);
  2065. return *this;
  2066. }
  2067. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  2068. uint32_t n = val;
  2069. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  2070. if ((n | ~EIMSK_pB) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTB);
  2071. if ((n | ~EIMSK_pC) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTC);
  2072. if ((n | ~EIMSK_pD) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTD);
  2073. if ((n | ~EIMSK_pE) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTE);
  2074. return *this;
  2075. }
  2076. };
  2077. extern EIMSKemulation EIMSK;
  2078. #elif defined(__MKL26Z64__)
  2079. #define EIMSK_pA 0x00000018 // pins 3, 4, 24
  2080. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  2081. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2082. class EIMSKemulation // used by Adafruit_nRF8001
  2083. {
  2084. public:
  2085. operator int () const __attribute__((always_inline)) {
  2086. int mask = 0;
  2087. volatile const uint32_t *icer = &NVIC_ICER0;
  2088. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  2089. if (icer[IRQ_PORTCD >> 5] & (1 << (IRQ_PORTCD & 31))) mask |= (EIMSK_pC | EIMSK_pD);
  2090. return mask;
  2091. }
  2092. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  2093. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  2094. if (val & (EIMSK_pC | EIMSK_pD)) NVIC_ENABLE_IRQ(IRQ_PORTCD);
  2095. return *this;
  2096. }
  2097. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  2098. uint32_t n = val;
  2099. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  2100. if ((n | ~(EIMSK_pC | EIMSK_pD)) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTCD);
  2101. return *this;
  2102. }
  2103. };
  2104. extern EIMSKemulation EIMSK;
  2105. #endif
  2106. // these are not intended for public consumption...
  2107. #undef GPIO_BITBAND_ADDR
  2108. #undef CONFIG_PULLUP
  2109. #undef CONFIG_NOPULLUP
  2110. #undef GPIO_SETBIT_ATOMIC
  2111. #undef GPIO_CLRBIT_ATOMIC
  2112. #endif // __cplusplus
  2113. #endif