Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

avr_emulation.h 67KB

před 9 roky
před 9 roky
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  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. // More options, so 2 bits
  1222. pinout &= ~3;
  1223. switch (pin) {
  1224. case 0: break;
  1225. case 21: pinout |= 1; break;
  1226. case 61: pinout |= 2; break;
  1227. case 59: pinout |= 3; break;
  1228. }
  1229. }
  1230. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1231. // More options, so 2 bits
  1232. pinout &= ~0xc;
  1233. switch (pin) {
  1234. case 1: break;
  1235. case 5: pinout |= 0x4; break;
  1236. case 61: pinout |= 0x8; break;
  1237. case 59: pinout |= 0xc; break;
  1238. }
  1239. }
  1240. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1241. // More options, so 2 bits
  1242. pinout &= ~0x30;
  1243. switch (pin) {
  1244. case 20: break;
  1245. case 32: pinout |= 0x10; break;
  1246. case 60: pinout |= 0x20; break;
  1247. }
  1248. }
  1249. inline void enable_pins(void) __attribute__((always_inline)) {
  1250. //serial_print("enable_pins\n");
  1251. // MOSI (SOUT)
  1252. switch (pinout & 0x3) {
  1253. case 0: CORE_PIN0_CONFIG = PORT_PCR_MUX(2); break;
  1254. case 1: CORE_PIN21_CONFIG = PORT_PCR_MUX(7); break;
  1255. case 2: CORE_PIN61_CONFIG = PORT_PCR_MUX(7); break;
  1256. case 3: CORE_PIN59_CONFIG = PORT_PCR_MUX(2); break;
  1257. }
  1258. // MISO (SIN)
  1259. switch (pinout & 0xc) {
  1260. case 0x0: CORE_PIN1_CONFIG = PORT_PCR_MUX(2); break;
  1261. case 0x4: CORE_PIN5_CONFIG = PORT_PCR_MUX(7); break;
  1262. case 0x8: CORE_PIN61_CONFIG = PORT_PCR_MUX(2); break;
  1263. case 0xc: CORE_PIN59_CONFIG = PORT_PCR_MUX(7); break;
  1264. }
  1265. // SCK
  1266. switch (pinout & 0x30) {
  1267. case 0x0: CORE_PIN20_CONFIG = PORT_PCR_MUX(7); break;
  1268. case 0x10: CORE_PIN32_CONFIG = PORT_PCR_MUX(2); break;
  1269. case 0x20: CORE_PIN60_CONFIG = PORT_PCR_MUX(2); break;
  1270. }
  1271. }
  1272. inline void disable_pins(void) __attribute__((always_inline)) {
  1273. switch (pinout & 0x3) {
  1274. case 0: CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1275. case 1: CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1276. case 2: CORE_PIN61_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1277. case 3: CORE_PIN59_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1278. }
  1279. switch (pinout & 0xc) {
  1280. case 0x0: CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1281. case 0x4: CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1282. case 0x8: CORE_PIN61_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1283. case 0xc: CORE_PIN59_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1284. }
  1285. switch (pinout & 0x30) {
  1286. case 0x0: CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1287. case 0x10: CORE_PIN32_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1288. case 0x20: CORE_PIN60_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1); break;
  1289. }
  1290. }
  1291. friend class SPIFIFO1class;
  1292. private:
  1293. static uint8_t pinout;
  1294. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1295. if (SPI1_CTAR0 == ctar) return;
  1296. uint32_t mcr = SPI1_MCR;
  1297. if (mcr & SPI_MCR_MDIS) {
  1298. SPI1_CTAR0 = ctar;
  1299. } else {
  1300. SPI1_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1301. SPI1_CTAR0 = ctar;
  1302. SPI1_MCR = mcr;
  1303. }
  1304. }
  1305. };
  1306. extern SPCR1emulation SPCR1;
  1307. ////////////////////
  1308. // SPI2
  1309. class SPCR2emulation
  1310. {
  1311. public:
  1312. inline SPCR2emulation & operator = (int val) __attribute__((always_inline)) {
  1313. uint32_t ctar, mcr, sim3;
  1314. //serial_print("SPCR=");
  1315. //serial_phex(val);
  1316. //serial_print("\n");
  1317. sim3 = SIM_SCGC3;
  1318. if (!(sim3 & SIM_SCGC3_SPI2)) {
  1319. //serial_print("init1\n");
  1320. SIM_SCGC3 = sim3 | SIM_SCGC3_SPI2;
  1321. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1322. }
  1323. if (!(val & (1<<SPE))) {
  1324. SPI2_MCR |= SPI_MCR_MDIS; // TODO: use bitband for atomic access
  1325. }
  1326. ctar = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1);
  1327. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE;
  1328. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1329. if (val & (1<<CPHA)) {
  1330. ctar |= SPI_CTAR_CPHA;
  1331. if ((val & 3) == 0) {
  1332. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_ASC(1);
  1333. } else if ((val & 3) == 1) {
  1334. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_ASC(4);
  1335. } else if ((val & 3) == 2) {
  1336. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_ASC(6);
  1337. } else {
  1338. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_ASC(7);
  1339. }
  1340. } else {
  1341. if ((val & 3) == 0) {
  1342. ctar |= SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1);
  1343. } else if ((val & 3) == 1) {
  1344. ctar |= SPI_CTAR_BR(4) | SPI_CTAR_CSSCK(4);
  1345. } else if ((val & 3) == 2) {
  1346. ctar |= SPI_CTAR_BR(6) | SPI_CTAR_CSSCK(6);
  1347. } else {
  1348. ctar |= SPI_CTAR_BR(7) | SPI_CTAR_CSSCK(7);
  1349. }
  1350. }
  1351. ctar |= (SPI2_CTAR0 & SPI_CTAR_DBR);
  1352. update_ctar(ctar);
  1353. mcr = SPI_MCR_DCONF(0) | SPI_MCR_PCSIS(0x1F);
  1354. if (val & (1<<MSTR)) mcr |= SPI_MCR_MSTR;
  1355. if (val & (1<<SPE)) {
  1356. mcr &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1357. SPI2_MCR = mcr;
  1358. enable_pins();
  1359. } else {
  1360. mcr |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1361. SPI2_MCR = mcr;
  1362. disable_pins();
  1363. }
  1364. //serial_print("MCR:");
  1365. //serial_phex32(SPI2_MCR);
  1366. //serial_print(", CTAR0:");
  1367. //serial_phex32(SPI2_CTAR0);
  1368. //serial_print("\n");
  1369. return *this;
  1370. }
  1371. inline SPCR2emulation & operator |= (int val) __attribute__((always_inline)) {
  1372. uint32_t sim3;
  1373. //serial_print("SPCR |= ");
  1374. //serial_phex(val);
  1375. //serial_print("\n");
  1376. sim3 = SIM_SCGC3;
  1377. if (!(sim3 & SIM_SCGC3_SPI2)) {
  1378. //serial_print("init2\n");
  1379. SIM_SCGC6 = sim3 | SIM_SCGC3_SPI2;
  1380. SPI2_CTAR0 = SPI_CTAR_FMSZ(7) | SPI_CTAR_PBR(1) | SPI_CTAR_BR(1);
  1381. }
  1382. if (val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1383. uint32_t ctar = SPI2_CTAR0;
  1384. if (val & (1<<DORD)) ctar |= SPI_CTAR_LSBFE; // TODO: use bitband
  1385. if (val & (1<<CPOL)) ctar |= SPI_CTAR_CPOL;
  1386. if ((val & 3) == 1) {
  1387. // TODO: implement - is this ever really needed
  1388. } else if ((val & 3) == 2) {
  1389. // TODO: implement - is this ever really needed
  1390. } else if ((val & 3) == 3) {
  1391. // TODO: implement - is this ever really needed
  1392. }
  1393. if (val & (1<<CPHA) && !(ctar & SPI_CTAR_CPHA)) {
  1394. ctar |= SPI_CTAR_CPHA;
  1395. // TODO: clear SPI_CTAR_CSSCK, set SPI_CTAR_ASC
  1396. }
  1397. update_ctar(ctar);
  1398. }
  1399. if (val & (1<<MSTR)) SPI2_MCR |= SPI_MCR_MSTR;
  1400. if (val & (1<<SPE)) {
  1401. SPI2_MCR &= ~(SPI_MCR_MDIS | SPI_MCR_HALT);
  1402. enable_pins();
  1403. }
  1404. //serial_print("MCR:");
  1405. //serial_phex32(SPI2_MCR);
  1406. //serial_print(", CTAR0:");
  1407. //serial_phex32(SPI2_CTAR0);
  1408. //serial_print("\n");
  1409. return *this;
  1410. }
  1411. inline SPCR2emulation & operator &= (int val) __attribute__((always_inline)) {
  1412. //serial_print("SPCR &= ");
  1413. //serial_phex(val);
  1414. //serial_print("\n");
  1415. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  1416. if (!(val & (1<<SPE))) {
  1417. SPI2_MCR |= (SPI_MCR_MDIS | SPI_MCR_HALT);
  1418. disable_pins();
  1419. }
  1420. if ((val & ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) != ((1<<DORD)|(1<<CPOL)|(1<<CPHA)|3)) {
  1421. uint32_t ctar = SPI2_CTAR0;
  1422. if (!(val & (1<<DORD))) ctar &= ~SPI_CTAR_LSBFE; // TODO: use bitband
  1423. if (!(val & (1<<CPOL))) ctar &= ~SPI_CTAR_CPOL;
  1424. if ((val & 3) == 0) {
  1425. // TODO: implement - is this ever really needed
  1426. } else if ((val & 3) == 1) {
  1427. // TODO: implement - is this ever really needed
  1428. } else if ((val & 3) == 2) {
  1429. // TODO: implement - is this ever really needed
  1430. }
  1431. if (!(val & (1<<CPHA)) && (ctar & SPI_CTAR_CPHA)) {
  1432. ctar &= ~SPI_CTAR_CPHA;
  1433. // TODO: set SPI_CTAR_ASC, clear SPI_CTAR_CSSCK
  1434. }
  1435. update_ctar(ctar);
  1436. }
  1437. if (!(val & (1<<MSTR))) SPI2_MCR &= ~SPI_MCR_MSTR;
  1438. return *this;
  1439. }
  1440. inline int operator & (int val) const __attribute__((always_inline)) {
  1441. int ret = 0;
  1442. //serial_print("SPCR & ");
  1443. //serial_phex(val);
  1444. //serial_print(" MCR:");
  1445. //serial_phex32(SPI2_MCR);
  1446. //serial_print(", CTAR0:");
  1447. //serial_phex32(SPI2_CTAR0);
  1448. //serial_print("\n");
  1449. //serial_print("\n");
  1450. SIM_SCGC3 |= SIM_SCGC3_SPI2;
  1451. if ((val & (1<<DORD)) && (SPI2_CTAR0 & SPI_CTAR_LSBFE)) ret |= (1<<DORD);
  1452. if ((val & (1<<CPOL)) && (SPI2_CTAR0 & SPI_CTAR_CPOL)) ret |= (1<<CPOL);
  1453. if ((val & (1<<CPHA)) && (SPI2_CTAR0 & SPI_CTAR_CPHA)) ret |= (1<<CPHA);
  1454. if ((val & 3) == 3) {
  1455. uint32_t dbr = SPI2_CTAR0 & 15;
  1456. if (dbr <= 1) {
  1457. } else if (dbr <= 4) {
  1458. ret |= (1<<SPR0);
  1459. } else if (dbr <= 6) {
  1460. ret |= (1<<SPR1);
  1461. } else {
  1462. ret |= (1<<SPR1)|(1<<SPR0);
  1463. }
  1464. } else if ((val & 3) == 1) {
  1465. // TODO: implement - is this ever really needed
  1466. } else if ((val & 3) == 2) {
  1467. // TODO: implement - is this ever really needed
  1468. }
  1469. if (val & (1<<SPE) && (!(SPI2_MCR & SPI_MCR_MDIS))) ret |= (1<<SPE);
  1470. if (val & (1<<MSTR) && (SPI2_MCR & SPI_MCR_MSTR)) ret |= (1<<MSTR);
  1471. //serial_print("ret = ");
  1472. //serial_phex(ret);
  1473. //serial_print("\n");
  1474. return ret;
  1475. }
  1476. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1477. if (pin == 44) pinout &= ~1;
  1478. if (pin == 52) pinout |= 1;
  1479. }
  1480. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1481. if (pin == 45) pinout &= ~2;
  1482. if (pin == 51) pinout |= 2;
  1483. }
  1484. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1485. if (pin == 46) pinout &= ~4;
  1486. if (pin == 53) pinout |= 4;
  1487. }
  1488. inline void enable_pins(void) __attribute__((always_inline)) {
  1489. //serial_print("enable_pins\n");
  1490. if ((pinout & 1) == 0) {
  1491. CORE_PIN44_CONFIG = PORT_PCR_MUX(2);
  1492. } else {
  1493. CORE_PIN52_CONFIG = PORT_PCR_MUX(2);
  1494. }
  1495. if ((pinout & 2) == 0) {
  1496. CORE_PIN45_CONFIG = PORT_PCR_MUX(2);
  1497. } else {
  1498. CORE_PIN51_CONFIG = PORT_PCR_MUX(2);
  1499. }
  1500. if ((pinout & 4) == 0) {
  1501. CORE_PIN46_CONFIG = PORT_PCR_MUX(2);
  1502. } else {
  1503. CORE_PIN53_CONFIG = PORT_PCR_MUX(2);
  1504. }
  1505. }
  1506. inline void disable_pins(void) __attribute__((always_inline)) {
  1507. //serial_print("disable_pins\n");
  1508. if ((pinout & 1) == 0) {
  1509. CORE_PIN44_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1510. } else {
  1511. CORE_PIN52_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1512. }
  1513. if ((pinout & 2) == 0) {
  1514. CORE_PIN45_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1515. } else {
  1516. CORE_PIN51_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1517. }
  1518. if ((pinout & 4) == 0) {
  1519. CORE_PIN46_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1520. } else {
  1521. CORE_PIN53_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1522. }
  1523. }
  1524. friend class SPIFIFO1class;
  1525. private:
  1526. static uint8_t pinout;
  1527. static inline void update_ctar(uint32_t ctar) __attribute__((always_inline)) {
  1528. if (SPI2_CTAR0 == ctar) return;
  1529. uint32_t mcr = SPI2_MCR;
  1530. if (mcr & SPI_MCR_MDIS) {
  1531. SPI2_CTAR0 = ctar;
  1532. } else {
  1533. SPI2_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
  1534. SPI2_CTAR0 = ctar;
  1535. SPI2_MCR = mcr;
  1536. }
  1537. }
  1538. };
  1539. extern SPCR2emulation SPCR2;
  1540. #endif
  1541. class SPSRemulation
  1542. {
  1543. public:
  1544. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1545. //serial_print("SPSR=");
  1546. //serial_phex(val);
  1547. //serial_print("\n");
  1548. uint32_t ctar = SPI0_CTAR0;
  1549. if (val & (1<<SPI2X)) {
  1550. ctar |= SPI_CTAR_DBR;
  1551. } else {
  1552. ctar &= ~SPI_CTAR_DBR;
  1553. }
  1554. SPCRemulation::update_ctar(ctar);
  1555. //serial_print("MCR:");
  1556. //serial_phex32(SPI0_MCR);
  1557. //serial_print(", CTAR0:");
  1558. //serial_phex32(SPI0_CTAR0);
  1559. //serial_print("\n");
  1560. return *this;
  1561. }
  1562. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1563. //serial_print("SPSR |= ");
  1564. //serial_phex(val);
  1565. //serial_print("\n");
  1566. if (val & (1<<SPI2X)) SPCRemulation::update_ctar(SPI0_CTAR0 |= SPI_CTAR_DBR);
  1567. return *this;
  1568. }
  1569. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1570. //serial_print("SPSR &= ");
  1571. //serial_phex(val);
  1572. //serial_print("\n");
  1573. if (!(val & (1<<SPI2X))) SPCRemulation::update_ctar(SPI0_CTAR0 &= ~SPI_CTAR_DBR);
  1574. return *this;
  1575. }
  1576. inline int operator & (int val) const __attribute__((always_inline)) {
  1577. int ret = 0;
  1578. //serial_print("SPSR & ");
  1579. //serial_phex(val);
  1580. //serial_print("\n");
  1581. // TODO: using SPI_SR_TCF isn't quite right. Control returns to the
  1582. // caller after the final edge that captures data, which is 1/2 cycle
  1583. // sooner than AVR returns. At 500 kHz and slower SPI, this can make
  1584. // a difference when digitalWrite is used to manually control the CS
  1585. // pin, and perhaps it could matter at high clocks if faster register
  1586. // access is used? But does it really matter? Do any SPI chips in
  1587. // practice really perform differently if CS negates early, after the
  1588. // final bit is clocked, but before the end of the whole clock cycle?
  1589. if ((val & (1<<SPIF)) && (SPI0_SR & SPI_SR_TCF)) ret = (1<<SPIF);
  1590. if ((val & (1<<SPI2X)) && (SPI0_CTAR0 & SPI_CTAR_DBR)) ret |= (1<<SPI2X);
  1591. //delayMicroseconds(50000);
  1592. return ret;
  1593. }
  1594. operator int () const __attribute__((always_inline)) {
  1595. int ret = 0;
  1596. //serial_print("SPSR (int)\n");
  1597. if (SPI0_SR & SPI_SR_TCF) ret = (1<<SPIF);
  1598. if (SPI0_CTAR0 & SPI_CTAR_DBR) ret |= (1<<SPI2X);
  1599. return ret;
  1600. }
  1601. };
  1602. extern SPSRemulation SPSR;
  1603. class SPDRemulation
  1604. {
  1605. public:
  1606. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1607. //serial_print("SPDR = ");
  1608. //serial_phex(val);
  1609. //serial_print("\n");
  1610. SPI0_MCR |= SPI_MCR_CLR_RXF; // discard any received data
  1611. SPI0_SR = SPI_SR_TCF;
  1612. //SPI0_SR = SPI_SR_EOQF;
  1613. //SPI0_PUSHR = (val & 255) | SPI0_PUSHR_EOQ;
  1614. SPI0_PUSHR = (val & 255);
  1615. return *this;
  1616. }
  1617. operator int () const __attribute__((always_inline)) {
  1618. uint32_t val;
  1619. val = SPI0_POPR & 255;
  1620. //serial_print("SPDR (int) ");
  1621. //serial_phex(val);
  1622. //serial_print("\n");
  1623. return val;
  1624. }
  1625. };
  1626. extern SPDRemulation SPDR;
  1627. #elif defined(KINETISL)
  1628. // SPI Control Register ­ SPCR
  1629. //#define SPIE 7 // SPI Interrupt Enable - not supported
  1630. //#define SPE 6 // SPI Enable
  1631. //#define DORD 5 // DORD: Data Order
  1632. //#define MSTR 4 // MSTR: Master/Slave Select
  1633. //#define CPOL 3 // CPOL: Clock Polarity
  1634. //#define CPHA 2 // CPHA: Clock Phase
  1635. //#define SPR1 1 // Clock: 3 = 125 kHz, 2 = 250 kHz, 1 = 1 MHz, 0->4 MHz
  1636. //#define SPR0 0
  1637. // SPI Status Register ­ SPSR
  1638. //#define SPIF 7 // SPIF: SPI Interrupt Flag
  1639. //#define WCOL 6 // WCOL: Write COLlision Flag - not implemented
  1640. //#define SPI2X 0 // SPI2X: Double SPI Speed Bit
  1641. // SPI Data Register ­ SPDR
  1642. class SPCRemulation
  1643. {
  1644. public:
  1645. inline SPCRemulation & operator = (int val) __attribute__((always_inline)) {
  1646. uint32_t sim4 = SIM_SCGC4;
  1647. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1648. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1649. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1650. }
  1651. uint32_t c1 = 0;
  1652. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1653. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1654. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1655. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1656. if (val & (1<<SPE)) c1 |= SPI_C1_SPE;
  1657. SPI0_C1 = c1;
  1658. SPI0_C2 = 0;
  1659. uint32_t br = SPI0_BR & 0x10;
  1660. switch (val & 3) {
  1661. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1662. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1663. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1664. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1665. }
  1666. if (val & (1<<SPE)) enable_pins();
  1667. else disable_pins();
  1668. return *this;
  1669. }
  1670. inline SPCRemulation & operator |= (int val) __attribute__((always_inline)) {
  1671. uint32_t sim4 = SIM_SCGC4;
  1672. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1673. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1674. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1675. }
  1676. uint32_t c1 = SPI0_C1;
  1677. if (val & (1<<DORD)) c1 |= SPI_C1_LSBFE;
  1678. if (val & (1<<CPOL)) c1 |= SPI_C1_CPOL;
  1679. if (val & (1<<CPHA)) c1 |= SPI_C1_CPHA;
  1680. if (val & (1<<MSTR)) c1 |= SPI_C1_MSTR;
  1681. if (val & (1<<SPE)) {
  1682. enable_pins();
  1683. c1 |= SPI_C1_SPE;
  1684. }
  1685. SPI0_C1 = c1;
  1686. SPI0_C2 = 0;
  1687. val &= 3;
  1688. if (val) {
  1689. uint32_t br = SPI0_BR;
  1690. uint32_t bits = baud2avr(br) | val;
  1691. br &= 0x10;
  1692. switch (bits) {
  1693. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1694. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1695. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1696. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1697. }
  1698. }
  1699. return *this;
  1700. }
  1701. inline SPCRemulation & operator &= (int val) __attribute__((always_inline)) {
  1702. uint32_t sim4 = SIM_SCGC4;
  1703. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1704. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1705. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1706. }
  1707. uint32_t c1 = SPI0_C1;
  1708. if (!(val & (1<<DORD))) c1 &= ~SPI_C1_LSBFE;
  1709. if (!(val & (1<<CPOL))) c1 &= ~SPI_C1_CPOL;
  1710. if (!(val & (1<<CPHA))) c1 &= ~SPI_C1_CPHA;
  1711. if (!(val & (1<<MSTR))) c1 &= ~SPI_C1_MSTR;
  1712. if (!(val & (1<<SPE))) {
  1713. disable_pins();
  1714. c1 &= ~SPI_C1_SPE;
  1715. }
  1716. SPI0_C1 = c1;
  1717. SPI0_C2 = 0;
  1718. val &= 3;
  1719. if (val < 3) {
  1720. uint32_t br = SPI0_BR;
  1721. uint32_t bits = baud2avr(br) & val;
  1722. br &= 0x10;
  1723. switch (bits) {
  1724. case 0: SPI0_BR = br | SPI_BR_SPR(0); break;
  1725. case 1: SPI0_BR = br | SPI_BR_SPR(2); break;
  1726. case 2: SPI0_BR = br | SPI_BR_SPR(4); break;
  1727. default: SPI0_BR = br | SPI_BR_SPR(5); break;
  1728. }
  1729. }
  1730. return *this;
  1731. }
  1732. inline int operator & (int val) const __attribute__((always_inline)) {
  1733. int ret = 0;
  1734. uint32_t sim4 = SIM_SCGC4;
  1735. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1736. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1737. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1738. }
  1739. uint32_t c1 = SPI0_C1;
  1740. if ((val & (1<<DORD)) && (c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1741. if ((val & (1<<CPOL)) && (c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1742. if ((val & (1<<CPHA)) && (c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1743. if ((val & (1<<MSTR)) && (c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1744. if ((val & (1<<SPE)) && (c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1745. uint32_t bits = baud2avr(SPI0_BR);
  1746. if ((val & (1<<SPR1)) && (bits & (1<<SPR1))) ret |= (1<<SPR1);
  1747. if ((val & (1<<SPR0)) && (bits & (1<<SPR0))) ret |= (1<<SPR0);
  1748. return ret;
  1749. }
  1750. operator int () const __attribute__((always_inline)) {
  1751. int ret = 0;
  1752. uint32_t sim4 = SIM_SCGC4;
  1753. if (!(sim4 & SIM_SCGC4_SPI0)) {
  1754. SIM_SCGC4 = sim4 | SIM_SCGC4_SPI0;
  1755. SPI0_BR = SPI_BR_SPR(0) | SPI_BR_SPPR(1);
  1756. }
  1757. uint32_t c1 = SPI0_C1;
  1758. if ((c1 & SPI_C1_LSBFE)) ret |= (1<<DORD);
  1759. if ((c1 & SPI_C1_CPOL)) ret |= (1<<CPOL);
  1760. if ((c1 & SPI_C1_CPHA)) ret |= (1<<CPHA);
  1761. if ((c1 & SPI_C1_MSTR)) ret |= (1<<MSTR);
  1762. if ((c1 & SPI_C1_SPE)) ret |= (1<<SPE);
  1763. ret |= baud2avr(SPI0_BR);
  1764. return ret;
  1765. }
  1766. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1767. uint8_t newpinout = pinout;
  1768. if (pin == 11) newpinout &= ~1;
  1769. if (pin == 7) newpinout |= 1;
  1770. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1771. if ((newpinout & 1) == 0) {
  1772. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1773. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1774. } else {
  1775. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1776. CORE_PIN7_CONFIG = PORT_PCR_MUX(2);
  1777. }
  1778. }
  1779. pinout = newpinout;
  1780. }
  1781. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1782. uint8_t newpinout = pinout;
  1783. if (pin == 12) newpinout &= ~2;
  1784. if (pin == 8) newpinout |= 2;
  1785. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1786. if ((newpinout & 2) == 0) {
  1787. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1788. CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
  1789. } else {
  1790. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1791. CORE_PIN8_CONFIG = PORT_PCR_MUX(2);
  1792. }
  1793. }
  1794. pinout = newpinout;
  1795. }
  1796. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1797. uint8_t newpinout = pinout;
  1798. if (pin == 13) newpinout &= ~4;
  1799. if (pin == 14) newpinout |= 4;
  1800. if ((SIM_SCGC4 & SIM_SCGC4_SPI0) && newpinout != pinout) {
  1801. if ((newpinout & 4) == 0) {
  1802. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1803. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
  1804. } else {
  1805. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1806. CORE_PIN14_CONFIG = PORT_PCR_MUX(2);
  1807. }
  1808. }
  1809. pinout = newpinout;
  1810. }
  1811. friend class SPSRemulation;
  1812. friend class SPIFIFOclass;
  1813. private:
  1814. static inline uint32_t baud2avr(uint32_t br) __attribute__((always_inline)) {
  1815. br &= 15;
  1816. if (br == 0) return 0;
  1817. if (br <= 2) return 1;
  1818. if (br <= 4) return 2;
  1819. return 3;
  1820. }
  1821. static uint8_t pinout;
  1822. public:
  1823. inline void enable_pins(void) __attribute__((always_inline)) {
  1824. //serial_print("enable_pins\n");
  1825. if ((pinout & 1) == 0) {
  1826. CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // MOSI0 = 11 (PTC6)
  1827. } else {
  1828. CORE_PIN7_CONFIG = PORT_PCR_MUX(2); // MOSI0 = 7 (PTD2)
  1829. }
  1830. if ((pinout & 2) == 0) {
  1831. CORE_PIN12_CONFIG = PORT_PCR_MUX(2); // MISO0 = 12 (PTC7)
  1832. } else {
  1833. CORE_PIN8_CONFIG = PORT_PCR_MUX(2); // MISO0 = 8 (PTD3)
  1834. }
  1835. if ((pinout & 4) == 0) {
  1836. CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2); // SCK0 = 13 (PTC5)
  1837. } else {
  1838. CORE_PIN14_CONFIG = PORT_PCR_MUX(2); // SCK0 = 14 (PTD1)
  1839. }
  1840. }
  1841. inline void disable_pins(void) __attribute__((always_inline)) {
  1842. //serial_print("disable_pins\n");
  1843. if ((pinout & 1) == 0) {
  1844. CORE_PIN11_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1845. } else {
  1846. CORE_PIN7_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1847. }
  1848. if ((pinout & 2) == 0) {
  1849. CORE_PIN12_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1850. } else {
  1851. CORE_PIN8_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1852. }
  1853. if ((pinout & 4) == 0) {
  1854. CORE_PIN13_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1855. } else {
  1856. CORE_PIN14_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1857. }
  1858. }
  1859. };
  1860. extern SPCRemulation SPCR;
  1861. class SPCR1emulation
  1862. {
  1863. public:
  1864. inline void setMOSI(uint8_t pin) __attribute__((always_inline)) {
  1865. if (pin == 0) pinout &= ~1; // MOSI1 = 0 (PTB16)
  1866. if (pin == 21) pinout |= 1; // MOSI1 = 21 (PTD6)
  1867. }
  1868. inline void setMISO(uint8_t pin) __attribute__((always_inline)) {
  1869. if (pin == 1) pinout &= ~2; // MISO1 = 1 (PTB17)
  1870. if (pin == 5) pinout |= 2; // MISO1 = 5 (PTD7)
  1871. }
  1872. inline void setSCK(uint8_t pin) __attribute__((always_inline)) {
  1873. // SCK1 = 20 (PTD5) - no alternative pin
  1874. }
  1875. inline void enable_pins(void) __attribute__((always_inline)) {
  1876. //serial_print("enable_pins\n");
  1877. if ((pinout & 1) == 0) {
  1878. CORE_PIN0_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 0 (PTB16)
  1879. } else {
  1880. CORE_PIN21_CONFIG = PORT_PCR_MUX(2); // MOSI1 = 21 (PTD6)
  1881. }
  1882. if ((pinout & 2) == 0) {
  1883. CORE_PIN1_CONFIG = PORT_PCR_MUX(2); // MISO1 = 1 (PTB17)
  1884. } else {
  1885. CORE_PIN5_CONFIG = PORT_PCR_MUX(2); // MISO1 = 5 (PTD7)
  1886. }
  1887. CORE_PIN20_CONFIG = PORT_PCR_MUX(2); // SCK1 = 20 (PTD5)
  1888. }
  1889. inline void disable_pins(void) __attribute__((always_inline)) {
  1890. //serial_print("disable_pins\n");
  1891. if ((pinout & 1) == 0) {
  1892. CORE_PIN0_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1893. } else {
  1894. CORE_PIN21_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1895. }
  1896. if ((pinout & 2) == 0) {
  1897. CORE_PIN1_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1898. } else {
  1899. CORE_PIN5_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1900. }
  1901. CORE_PIN20_CONFIG = PORT_PCR_SRE | PORT_PCR_MUX(1);
  1902. }
  1903. friend class SPIFIFO1class;
  1904. private:
  1905. static uint8_t pinout;
  1906. };
  1907. extern SPCR1emulation SPCR1;
  1908. class SPSRemulation
  1909. {
  1910. public:
  1911. inline SPSRemulation & operator = (int val) __attribute__((always_inline)) {
  1912. if (val & (1<<SPI2X)) {
  1913. SPI0_BR &= ~0x10;
  1914. } else {
  1915. SPI0_BR |= 0x10;
  1916. }
  1917. return *this;
  1918. }
  1919. inline SPSRemulation & operator |= (int val) __attribute__((always_inline)) {
  1920. if (val & (1<<SPI2X)) SPI0_BR &= ~0x10;
  1921. return *this;
  1922. }
  1923. inline SPSRemulation & operator &= (int val) __attribute__((always_inline)) {
  1924. if (!(val & (1<<SPI2X))) SPI0_BR |= 0x10;
  1925. return *this;
  1926. }
  1927. inline int operator & (int val) const __attribute__((always_inline)) {
  1928. int ret = 0;
  1929. if ((val & (1<<SPIF)) && (SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  1930. if ((val & (1<<SPI2X)) && (!(SPI0_BR & 0x10))) ret |= (1<<SPI2X);
  1931. return ret;
  1932. }
  1933. operator int () const __attribute__((always_inline)) {
  1934. int ret = 0;
  1935. if ((SPI0_S & SPI_S_SPRF)) ret = (1<<SPIF);
  1936. if (!(SPI0_BR & 0x10)) ret |= (1<<SPI2X);
  1937. return ret;
  1938. }
  1939. };
  1940. extern SPSRemulation SPSR;
  1941. class SPDRemulation
  1942. {
  1943. public:
  1944. inline SPDRemulation & operator = (int val) __attribute__((always_inline)) {
  1945. if ((SPI0_S & SPI_S_SPTEF)) {
  1946. uint32_t tmp __attribute__((unused)) = SPI0_DL;
  1947. }
  1948. SPI0_DL = val;
  1949. return *this;
  1950. }
  1951. operator int () const __attribute__((always_inline)) {
  1952. return SPI0_DL & 255;
  1953. }
  1954. };
  1955. extern SPDRemulation SPDR;
  1956. #endif // KINETISK
  1957. class SREGemulation
  1958. {
  1959. public:
  1960. operator int () const __attribute__((always_inline)) {
  1961. uint32_t primask;
  1962. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  1963. if (primask) return 0;
  1964. return (1<<7);
  1965. }
  1966. inline SREGemulation & operator = (int val) __attribute__((always_inline)) {
  1967. if (val & (1<<7)) {
  1968. __enable_irq();
  1969. } else {
  1970. __disable_irq();
  1971. }
  1972. return *this;
  1973. }
  1974. };
  1975. extern SREGemulation SREG;
  1976. // 22211
  1977. // 84062840
  1978. // 322111
  1979. // 17395173
  1980. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1981. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  1982. #define EIMSK_pA 0x01000018 // pins 3, 4, 24
  1983. #define EIMSK_pB 0x020F0003 // pins 0, 1, 16-19, 25
  1984. #define EIMSK_pC 0x78C0BE00 // pins 9-13, 15, 22, 23, 27-30
  1985. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  1986. #define EIMSK_pE 0x84000000 // pins 26, 31
  1987. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1988. #define EIMSK_pA 0x1E000018 // pins 3, 4, 25-28
  1989. #define EIMSK_pB 0xE00F0003 // pins 0, 1, 16-19, 29-31
  1990. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  1991. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  1992. #define EIMSK_pE 0x01000000 // pins 24
  1993. #endif
  1994. class EIMSKemulation // used by Adafruit_nRF8001 (only supports INT for pins 0 to 31)
  1995. {
  1996. public:
  1997. operator int () const __attribute__((always_inline)) {
  1998. int mask = 0;
  1999. volatile const uint32_t *icer = &NVIC_ICER0;
  2000. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  2001. if (icer[IRQ_PORTB >> 5] & (1 << (IRQ_PORTB & 31))) mask |= EIMSK_pB;
  2002. if (icer[IRQ_PORTC >> 5] & (1 << (IRQ_PORTC & 31))) mask |= EIMSK_pC;
  2003. if (icer[IRQ_PORTD >> 5] & (1 << (IRQ_PORTD & 31))) mask |= EIMSK_pD;
  2004. if (icer[IRQ_PORTE >> 5] & (1 << (IRQ_PORTE & 31))) mask |= EIMSK_pE;
  2005. return mask;
  2006. }
  2007. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  2008. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  2009. if (val & EIMSK_pB) NVIC_ENABLE_IRQ(IRQ_PORTB);
  2010. if (val & EIMSK_pC) NVIC_ENABLE_IRQ(IRQ_PORTC);
  2011. if (val & EIMSK_pD) NVIC_ENABLE_IRQ(IRQ_PORTD);
  2012. if (val & EIMSK_pE) NVIC_ENABLE_IRQ(IRQ_PORTE);
  2013. return *this;
  2014. }
  2015. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  2016. uint32_t n = val;
  2017. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  2018. if ((n | ~EIMSK_pB) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTB);
  2019. if ((n | ~EIMSK_pC) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTC);
  2020. if ((n | ~EIMSK_pD) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTD);
  2021. if ((n | ~EIMSK_pE) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTE);
  2022. return *this;
  2023. }
  2024. };
  2025. extern EIMSKemulation EIMSK;
  2026. #elif defined(__MKL26Z64__)
  2027. #define EIMSK_pA 0x00000018 // pins 3, 4, 24
  2028. #define EIMSK_pC 0x00C0BE00 // pins 9-13, 15, 22, 23
  2029. #define EIMSK_pD 0x003041E4 // pins 2, 5-8, 14, 20, 21
  2030. class EIMSKemulation // used by Adafruit_nRF8001
  2031. {
  2032. public:
  2033. operator int () const __attribute__((always_inline)) {
  2034. int mask = 0;
  2035. volatile const uint32_t *icer = &NVIC_ICER0;
  2036. if (icer[IRQ_PORTA >> 5] & (1 << (IRQ_PORTA & 31))) mask |= EIMSK_pA;
  2037. if (icer[IRQ_PORTCD >> 5] & (1 << (IRQ_PORTCD & 31))) mask |= (EIMSK_pC | EIMSK_pD);
  2038. return mask;
  2039. }
  2040. inline EIMSKemulation & operator |= (int val) __attribute__((always_inline)) {
  2041. if (val & EIMSK_pA) NVIC_ENABLE_IRQ(IRQ_PORTA);
  2042. if (val & (EIMSK_pC | EIMSK_pD)) NVIC_ENABLE_IRQ(IRQ_PORTCD);
  2043. return *this;
  2044. }
  2045. inline EIMSKemulation & operator &= (int val) __attribute__((always_inline)) {
  2046. uint32_t n = val;
  2047. if ((n | ~EIMSK_pA) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTA);
  2048. if ((n | ~(EIMSK_pC | EIMSK_pD)) != 0xFFFFFFFF) NVIC_DISABLE_IRQ(IRQ_PORTCD);
  2049. return *this;
  2050. }
  2051. };
  2052. extern EIMSKemulation EIMSK;
  2053. #endif
  2054. // these are not intended for public consumption...
  2055. #undef GPIO_BITBAND_ADDR
  2056. #undef CONFIG_PULLUP
  2057. #undef CONFIG_NOPULLUP
  2058. #undef GPIO_SETBIT_ATOMIC
  2059. #undef GPIO_CLRBIT_ATOMIC
  2060. #endif // __cplusplus
  2061. #endif