U8x8lib.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. /*
  2. U8x8lib.cpp
  3. Arduino specific low level functions
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "U8x8lib.h"
  29. #ifdef ARDUINO
  30. #ifdef U8X8_HAVE_HW_SPI
  31. #include <SPI.h>
  32. #endif
  33. #ifdef U8X8_HAVE_HW_I2C
  34. #include <Wire.h>
  35. #endif
  36. #endif
  37. /*=============================================*/
  38. size_t U8X8::write(uint8_t v)
  39. {
  40. if ( v == '\n' )
  41. {
  42. uint8_t dy = u8x8_pgm_read(u8x8.font+3); /* new 2019 format */
  43. ty+=dy;
  44. tx=0;
  45. }
  46. else
  47. {
  48. uint8_t dx = u8x8_pgm_read(u8x8.font+2); /* new 2019 format */
  49. u8x8_DrawGlyph(&u8x8, tx, ty, v);
  50. tx+=dx;
  51. }
  52. return 1;
  53. }
  54. /*=============================================*/
  55. /*=== ARDUINO GPIO & DELAY ===*/
  56. #ifdef ARDUINO
  57. #ifdef U8X8_USE_PINS
  58. extern "C" uint8_t u8x8_gpio_and_delay_arduino(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  59. {
  60. uint8_t i;
  61. switch(msg)
  62. {
  63. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  64. for( i = 0; i < U8X8_PIN_CNT; i++ )
  65. if ( u8x8->pins[i] != U8X8_PIN_NONE )
  66. {
  67. if ( i < U8X8_PIN_OUTPUT_CNT )
  68. {
  69. pinMode(u8x8->pins[i], OUTPUT);
  70. }
  71. else
  72. {
  73. #ifdef INPUT_PULLUP
  74. pinMode(u8x8->pins[i], INPUT_PULLUP);
  75. #else
  76. pinMode(u8x8->pins[i], OUTPUT);
  77. digitalWrite(u8x8->pins[i], 1);
  78. #endif
  79. }
  80. }
  81. break;
  82. #ifndef __AVR__
  83. /* this case is not compiled for any AVR, because AVR uC are so slow */
  84. /* that this delay does not matter */
  85. case U8X8_MSG_DELAY_NANO:
  86. delayMicroseconds(arg_int==0?0:1);
  87. break;
  88. #endif
  89. case U8X8_MSG_DELAY_10MICRO:
  90. /* not used at the moment */
  91. break;
  92. case U8X8_MSG_DELAY_100NANO:
  93. /* not used at the moment */
  94. break;
  95. case U8X8_MSG_DELAY_MILLI:
  96. delay(arg_int);
  97. break;
  98. case U8X8_MSG_DELAY_I2C:
  99. /* arg_int is 1 or 4: 100KHz (5us) or 400KHz (1.25us) */
  100. delayMicroseconds(arg_int<=2?5:2);
  101. break;
  102. case U8X8_MSG_GPIO_I2C_CLOCK:
  103. case U8X8_MSG_GPIO_I2C_DATA:
  104. if ( arg_int == 0 )
  105. {
  106. pinMode(u8x8_GetPinValue(u8x8, msg), OUTPUT);
  107. digitalWrite(u8x8_GetPinValue(u8x8, msg), 0);
  108. }
  109. else
  110. {
  111. #ifdef INPUT_PULLUP
  112. pinMode(u8x8_GetPinValue(u8x8, msg), INPUT_PULLUP);
  113. #else
  114. pinMode(u8x8_GetPinValue(u8x8, msg), OUTPUT);
  115. digitalWrite(u8x8_GetPinValue(u8x8, msg), 1);
  116. #endif
  117. }
  118. break;
  119. default:
  120. if ( msg >= U8X8_MSG_GPIO(0) )
  121. {
  122. i = u8x8_GetPinValue(u8x8, msg);
  123. if ( i != U8X8_PIN_NONE )
  124. {
  125. if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
  126. {
  127. digitalWrite(i, arg_int);
  128. }
  129. else
  130. {
  131. if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
  132. {
  133. // call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
  134. yield();
  135. }
  136. u8x8_SetGPIOResult(u8x8, digitalRead(i) == 0 ? 0 : 1);
  137. }
  138. }
  139. break;
  140. }
  141. return 0;
  142. }
  143. return 1;
  144. }
  145. #endif // U8X8_USE_PINS
  146. /*=============================================*/
  147. /*=== 3 WIRE SOFTWARE SPI ===*/
  148. /*
  149. replacement for a more faster u8x8_byte_3wire_sw_spi
  150. in general u8x8_byte_3wire_sw_spi could be a fallback:
  151. uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  152. {
  153. return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
  154. }
  155. */
  156. #ifndef __AVR_ARCH__
  157. #define __AVR_ARCH__ 0
  158. #endif
  159. #if !defined(U8X8_USE_PINS)
  160. /* no pin information (very strange), so fallback */
  161. uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  162. {
  163. return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
  164. }
  165. #elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
  166. /* this function completly replaces u8x8_byte_4wire_sw_spi*/
  167. extern "C" uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  168. {
  169. uint8_t i;
  170. uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
  171. uint16_t b;
  172. uint8_t *data;
  173. /* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
  174. /* so, it should be possible to use multiple displays with different pins */
  175. static volatile uint8_t *arduino_clock_port;
  176. static uint8_t arduino_clock_mask;
  177. static uint8_t arduino_clock_n_mask;
  178. static volatile uint8_t *arduino_data_port;
  179. static uint8_t arduino_data_mask;
  180. static uint8_t arduino_data_n_mask;
  181. static uint8_t last_dc;
  182. switch(msg)
  183. {
  184. case U8X8_MSG_BYTE_SEND:
  185. data = (uint8_t *)arg_ptr;
  186. if ( takeover_edge == 0 )
  187. {
  188. while( arg_int > 0 )
  189. {
  190. b = *data;
  191. if ( last_dc != 0 )
  192. b |= 256;
  193. data++;
  194. arg_int--;
  195. /* issue 156, check for speed */
  196. #if F_CPU <= 17000000
  197. if ( b == 0 )
  198. {
  199. *arduino_data_port &= arduino_data_n_mask;
  200. for( i = 0; i < 9; i++ )
  201. {
  202. *arduino_clock_port |= arduino_clock_mask;
  203. *arduino_clock_port &= arduino_clock_n_mask;
  204. }
  205. }
  206. else
  207. #endif
  208. {
  209. for( i = 0; i < 9; i++ )
  210. {
  211. if ( b & 256 )
  212. *arduino_data_port |= arduino_data_mask;
  213. else
  214. *arduino_data_port &= arduino_data_n_mask;
  215. *arduino_clock_port |= arduino_clock_mask;
  216. b <<= 1;
  217. *arduino_clock_port &= arduino_clock_n_mask;
  218. }
  219. }
  220. }
  221. }
  222. else
  223. {
  224. while( arg_int > 0 )
  225. {
  226. b = *data;
  227. if ( last_dc != 0 )
  228. b |= 256;
  229. data++;
  230. arg_int--;
  231. /* issue 156, check for speed */
  232. #if F_CPU <= 17000000
  233. if ( b == 0 )
  234. {
  235. *arduino_data_port &= arduino_data_n_mask;
  236. for( i = 0; i < 9; i++ )
  237. {
  238. *arduino_clock_port &= arduino_clock_n_mask;
  239. *arduino_clock_port |= arduino_clock_mask;
  240. }
  241. }
  242. else
  243. #endif
  244. {
  245. for( i = 0; i < 9; i++ )
  246. {
  247. if ( b & 256 )
  248. *arduino_data_port |= arduino_data_mask;
  249. else
  250. *arduino_data_port &= arduino_data_n_mask;
  251. *arduino_clock_port &= arduino_clock_n_mask;
  252. b <<= 1;
  253. *arduino_clock_port |= arduino_clock_mask;
  254. }
  255. }
  256. }
  257. }
  258. break;
  259. case U8X8_MSG_BYTE_INIT:
  260. /* disable chipselect */
  261. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  262. /* no wait required here */
  263. /* for SPI: setup correct level of the clock signal */
  264. u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
  265. break;
  266. case U8X8_MSG_BYTE_SET_DC:
  267. last_dc = arg_int;
  268. break;
  269. case U8X8_MSG_BYTE_START_TRANSFER:
  270. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  271. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  272. /* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
  273. arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
  274. arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
  275. arduino_clock_n_mask = ~arduino_clock_mask;
  276. /* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_DATA] */
  277. arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
  278. arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
  279. arduino_data_n_mask = ~arduino_data_mask;
  280. break;
  281. case U8X8_MSG_BYTE_END_TRANSFER:
  282. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  283. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  284. break;
  285. default:
  286. return 0;
  287. }
  288. return 1;
  289. }
  290. #else
  291. /* fallback */
  292. uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  293. {
  294. return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
  295. }
  296. #endif
  297. /*=============================================*/
  298. /*=== 4 WIRE SOFTWARE SPI ===*/
  299. /*
  300. replacement for a more faster u8x8_byte_4wire_sw_spi
  301. in general u8x8_byte_4wire_sw_spi could be a fallback:
  302. uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  303. {
  304. return u8x8_byte_4wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
  305. }
  306. */
  307. #ifndef __AVR_ARCH__
  308. #define __AVR_ARCH__ 0
  309. #endif
  310. #if !defined(U8X8_USE_PINS)
  311. /* no pin information (very strange), so fallback */
  312. uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  313. {
  314. return u8x8_byte_4wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
  315. }
  316. #elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
  317. /* this function completly replaces u8x8_byte_4wire_sw_spi*/
  318. extern "C" uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  319. {
  320. uint8_t SREG_backup;
  321. uint8_t i, b;
  322. uint8_t *data;
  323. uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
  324. //uint8_t not_takeover_edge = 1 - takeover_edge;
  325. /* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
  326. /* so, it should be possible to use multiple displays with different pins */
  327. static volatile uint8_t *arduino_clock_port;
  328. static uint8_t arduino_clock_mask;
  329. static uint8_t arduino_clock_n_mask;
  330. static volatile uint8_t *arduino_data_port;
  331. static uint8_t arduino_data_mask;
  332. static uint8_t arduino_data_n_mask;
  333. switch(msg)
  334. {
  335. case U8X8_MSG_BYTE_SEND:
  336. data = (uint8_t *)arg_ptr;
  337. if ( takeover_edge == 0 )
  338. {
  339. while( arg_int > 0 )
  340. {
  341. b = *data;
  342. data++;
  343. arg_int--;
  344. SREG_backup = SREG; cli();
  345. /* issue 156, check for speed */
  346. #if F_CPU <= 17000000
  347. if ( b == 0 )
  348. {
  349. *arduino_data_port &= arduino_data_n_mask;
  350. for( i = 0; i < 8; i++ )
  351. {
  352. *arduino_clock_port |= arduino_clock_mask;
  353. *arduino_clock_port &= arduino_clock_n_mask;
  354. }
  355. }
  356. else
  357. #endif
  358. {
  359. for( i = 0; i < 8; i++ )
  360. {
  361. if ( b & 128 )
  362. *arduino_data_port |= arduino_data_mask;
  363. else
  364. *arduino_data_port &= arduino_data_n_mask;
  365. *arduino_clock_port |= arduino_clock_mask;
  366. b <<= 1;
  367. *arduino_clock_port &= arduino_clock_n_mask;
  368. }
  369. }
  370. SREG = SREG_backup;
  371. }
  372. }
  373. else
  374. {
  375. while( arg_int > 0 )
  376. {
  377. b = *data;
  378. data++;
  379. arg_int--;
  380. SREG_backup = SREG; cli();
  381. /* issue 156, check for speed */
  382. #if F_CPU <= 17000000
  383. if ( b == 0 )
  384. {
  385. *arduino_data_port &= arduino_data_n_mask;
  386. for( i = 0; i < 8; i++ )
  387. {
  388. *arduino_clock_port &= arduino_clock_n_mask;
  389. *arduino_clock_port |= arduino_clock_mask;
  390. }
  391. }
  392. else
  393. #endif
  394. {
  395. for( i = 0; i < 8; i++ )
  396. {
  397. if ( b & 128 )
  398. *arduino_data_port |= arduino_data_mask;
  399. else
  400. *arduino_data_port &= arduino_data_n_mask;
  401. *arduino_clock_port &= arduino_clock_n_mask;
  402. b <<= 1;
  403. *arduino_clock_port |= arduino_clock_mask;
  404. }
  405. }
  406. SREG = SREG_backup;
  407. }
  408. }
  409. break;
  410. case U8X8_MSG_BYTE_INIT:
  411. /* disable chipselect */
  412. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  413. /* no wait required here */
  414. /* for SPI: setup correct level of the clock signal */
  415. u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
  416. break;
  417. case U8X8_MSG_BYTE_SET_DC:
  418. u8x8_gpio_SetDC(u8x8, arg_int);
  419. break;
  420. case U8X8_MSG_BYTE_START_TRANSFER:
  421. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  422. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  423. /* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
  424. arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
  425. arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
  426. arduino_clock_n_mask = ~arduino_clock_mask;
  427. /* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_DATA] */
  428. arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
  429. arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
  430. arduino_data_n_mask = ~arduino_data_mask;
  431. break;
  432. case U8X8_MSG_BYTE_END_TRANSFER:
  433. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  434. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  435. break;
  436. default:
  437. return 0;
  438. }
  439. return 1;
  440. }
  441. #elif defined(__SAM3X8E__) /* Arduino DUE */
  442. /* this function completly replaces u8x8_byte_4wire_sw_spi*/
  443. extern "C" uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  444. {
  445. uint8_t i, b;
  446. uint16_t us = ((u8x8->display_info->sck_pulse_width_ns + 999)/1000);
  447. uint8_t *data;
  448. uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
  449. //uint8_t not_takeover_edge = 1 - takeover_edge;
  450. /* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
  451. /* so, it should be possible to use multiple displays with different pins */
  452. /*
  453. static volatile uint32_t *arduino_clock_port;
  454. static uint32_t arduino_clock_mask;
  455. static uint32_t arduino_clock_n_mask;
  456. static volatile uint32_t *arduino_data_port;
  457. static uint32_t arduino_data_mask;
  458. static uint32_t arduino_data_n_mask;
  459. */
  460. static WoReg *arduinoSetClockPort, *arduinoUnsetClockPort;
  461. static uint32_t arduino_clock_mask;
  462. static WoReg *arduinoSetDataPort, *arduinoUnsetDataPort;
  463. static uint32_t arduino_data_mask;
  464. switch(msg)
  465. {
  466. case U8X8_MSG_BYTE_SEND:
  467. data = (uint8_t *)arg_ptr;
  468. if ( takeover_edge == 0 )
  469. {
  470. while( arg_int > 0 )
  471. {
  472. b = *data;
  473. data++;
  474. arg_int--;
  475. {
  476. for( i = 0; i < 8; i++ )
  477. {
  478. /*
  479. if ( b & 128 )
  480. *arduino_data_port |= arduino_data_mask;
  481. else
  482. *arduino_data_port &= arduino_data_n_mask;
  483. */
  484. if (b & 128)
  485. *arduinoSetDataPort = arduino_data_mask;
  486. else
  487. *arduinoUnsetDataPort = arduino_data_mask;
  488. //delayMicroseconds(us);
  489. //*arduino_clock_port |= arduino_clock_mask;
  490. *arduinoSetClockPort = arduino_clock_mask;
  491. b <<= 1;
  492. delayMicroseconds(us);
  493. //*arduino_clock_port &= arduino_clock_n_mask;
  494. *arduinoUnsetClockPort = arduino_clock_mask;
  495. }
  496. }
  497. }
  498. }
  499. else
  500. {
  501. while( arg_int > 0 )
  502. {
  503. b = *data;
  504. data++;
  505. arg_int--;
  506. {
  507. for( i = 0; i < 8; i++ )
  508. {
  509. /*
  510. if ( b & 128 )
  511. *arduino_data_port |= arduino_data_mask;
  512. else
  513. *arduino_data_port &= arduino_data_n_mask;
  514. */
  515. if (b & 128)
  516. *arduinoSetDataPort = arduino_data_mask;
  517. else
  518. *arduinoUnsetDataPort = arduino_data_mask;
  519. //delayMicroseconds(us);
  520. //*arduino_clock_port &= arduino_clock_n_mask;
  521. *arduinoUnsetClockPort = arduino_clock_mask;
  522. b <<= 1;
  523. delayMicroseconds(us);
  524. //*arduino_clock_port |= arduino_clock_mask;
  525. *arduinoSetClockPort = arduino_clock_mask;
  526. }
  527. }
  528. }
  529. }
  530. break;
  531. case U8X8_MSG_BYTE_INIT:
  532. /* disable chipselect */
  533. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  534. /* no wait required here */
  535. /* for SPI: setup correct level of the clock signal */
  536. u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
  537. break;
  538. case U8X8_MSG_BYTE_SET_DC:
  539. u8x8_gpio_SetDC(u8x8, arg_int);
  540. break;
  541. case U8X8_MSG_BYTE_START_TRANSFER:
  542. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  543. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  544. /* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
  545. /*
  546. arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
  547. arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
  548. arduino_clock_n_mask = ~arduino_clock_mask;
  549. arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
  550. arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
  551. arduino_data_n_mask = ~arduino_data_mask;
  552. */
  553. arduinoSetClockPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK])->PIO_SODR;
  554. arduinoUnsetClockPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK])->PIO_CODR;
  555. arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
  556. arduinoSetDataPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA])->PIO_SODR;
  557. arduinoUnsetDataPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA])->PIO_CODR;
  558. arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
  559. break;
  560. case U8X8_MSG_BYTE_END_TRANSFER:
  561. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  562. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  563. break;
  564. default:
  565. return 0;
  566. }
  567. return 1;
  568. }
  569. #else
  570. /* fallback */
  571. uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  572. {
  573. return u8x8_byte_4wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
  574. }
  575. #endif
  576. /*=============================================*/
  577. /*=== 3 WIRE HARDWARE SPI with 8 bit HW SPI Subsystem ===*/
  578. /*
  579. references:
  580. https://github.com/olikraus/ucglib/blob/master/cppsrc/Ucglib.cpp#L581
  581. https://github.com/olikraus/u8g2/issues/1041
  582. */
  583. static uint8_t arduino_hw_spi_3w_buffer[9];
  584. static uint8_t arduino_hw_spi_3w_bytepos;
  585. static uint16_t arduino_hw_spi_3w_dc; // 0 = dc==0, 256 = dc==1
  586. static void arduino_hw_spi_3w_init()
  587. {
  588. memset(arduino_hw_spi_3w_buffer, 0, 9);
  589. arduino_hw_spi_3w_bytepos = 0;
  590. }
  591. static void arduino_hw_spi_3w_flush(void)
  592. {
  593. #ifdef U8X8_HAVE_HW_SPI
  594. uint8_t i;
  595. for(i = 0; i <= arduino_hw_spi_3w_bytepos; i++)
  596. {
  597. SPI.transfer(arduino_hw_spi_3w_buffer[i]);
  598. }
  599. #endif
  600. }
  601. static void arduino_hw_spi_3w_sendbyte(uint8_t data)
  602. {
  603. static union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } data16; // well well, not legal ISO 9899 code
  604. data16.val = (arduino_hw_spi_3w_dc + data) << (7 - arduino_hw_spi_3w_bytepos);
  605. #ifdef __BYTE_ORDER__
  606. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  607. arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.msb;
  608. ++arduino_hw_spi_3w_bytepos;
  609. arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.lsb;
  610. #else
  611. arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.lsb;
  612. ++arduino_hw_spi_3w_bytepos;
  613. arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.msb;
  614. #endif
  615. #else // __BYTE_ORDER__ not defined (no gcc)
  616. // assume little endian
  617. arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.msb;
  618. ++arduino_hw_spi_3w_bytepos;
  619. arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.lsb;
  620. #endif
  621. if (arduino_hw_spi_3w_bytepos == 8)
  622. {
  623. arduino_hw_spi_3w_flush();
  624. arduino_hw_spi_3w_init();
  625. }
  626. }
  627. extern "C" uint8_t u8x8_byte_arduino_3wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  628. {
  629. #ifdef U8X8_HAVE_HW_SPI
  630. uint8_t *data;
  631. uint8_t internal_spi_mode;
  632. switch(msg)
  633. {
  634. case U8X8_MSG_BYTE_SEND:
  635. data = (uint8_t *)arg_ptr;
  636. while(arg_int > 0) {
  637. arduino_hw_spi_3w_sendbyte((uint8_t)*data);
  638. data++;
  639. arg_int--;
  640. }
  641. break;
  642. case U8X8_MSG_BYTE_INIT:
  643. if ( u8x8->bus_clock == 0 ) /* issue 769 */
  644. u8x8->bus_clock = u8x8->display_info->sck_clock_hz;
  645. /* disable chipselect */
  646. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  647. #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
  648. /* ESP32 has the following begin: SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
  649. /* not sure about ESP8266 */
  650. if ( u8x8->pins[U8X8_PIN_I2C_CLOCK] != U8X8_PIN_NONE && u8x8->pins[U8X8_PIN_I2C_DATA] != U8X8_PIN_NONE )
  651. {
  652. /* SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
  653. /* actually MISO is not used, but what else could be used here??? */
  654. SPI.begin(u8x8->pins[U8X8_PIN_I2C_CLOCK], MISO, u8x8->pins[U8X8_PIN_I2C_DATA]);
  655. }
  656. else
  657. {
  658. SPI.begin();
  659. }
  660. #else
  661. SPI.begin();
  662. #endif
  663. break;
  664. case U8X8_MSG_BYTE_SET_DC:
  665. arduino_hw_spi_3w_dc = arg_int ? 256 : 0;
  666. break;
  667. case U8X8_MSG_BYTE_START_TRANSFER:
  668. /* SPI mode has to be mapped to the mode of the current controller;
  669. at least Uno, Due, 101 have different SPI_MODEx values */
  670. internal_spi_mode = 0;
  671. switch(u8x8->display_info->spi_mode) {
  672. case 0: internal_spi_mode = SPI_MODE0; break;
  673. case 1: internal_spi_mode = SPI_MODE1; break;
  674. case 2: internal_spi_mode = SPI_MODE2; break;
  675. case 3: internal_spi_mode = SPI_MODE3; break;
  676. }
  677. #if ARDUINO >= 10600
  678. SPI.beginTransaction(
  679. SPISettings(u8x8->bus_clock, MSBFIRST, internal_spi_mode));
  680. #else
  681. SPI.begin();
  682. if (u8x8->display_info->sck_pulse_width_ns < 70)
  683. SPI.setClockDivider(SPI_CLOCK_DIV2);
  684. else if (u8x8->display_info->sck_pulse_width_ns < 140)
  685. SPI.setClockDivider(SPI_CLOCK_DIV4);
  686. else
  687. SPI.setClockDivider(SPI_CLOCK_DIV8);
  688. SPI.setDataMode(internal_spi_mode);
  689. SPI.setBitOrder(MSBFIRST);
  690. #endif
  691. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  692. u8x8->gpio_and_delay_cb(
  693. u8x8,
  694. U8X8_MSG_DELAY_NANO,
  695. u8x8->display_info->post_chip_enable_wait_ns,
  696. NULL);
  697. arduino_hw_spi_3w_init();
  698. break;
  699. case U8X8_MSG_BYTE_END_TRANSFER:
  700. u8x8->gpio_and_delay_cb(
  701. u8x8,
  702. U8X8_MSG_DELAY_NANO,
  703. u8x8->display_info->pre_chip_disable_wait_ns,
  704. NULL);
  705. if (arduino_hw_spi_3w_bytepos)
  706. arduino_hw_spi_3w_flush();
  707. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  708. #if ARDUINO >= 10600
  709. SPI.endTransaction();
  710. #else
  711. SPI.end();
  712. #endif
  713. break;
  714. default:
  715. return 0;
  716. }
  717. #endif // U8X8_HAVE_HW_SPI
  718. return 1;
  719. }
  720. /*=============================================*/
  721. /*=== 4 WIRE HARDWARE SPI ===*/
  722. #ifdef U8X8_USE_PINS
  723. extern "C" uint8_t u8x8_byte_arduino_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  724. {
  725. #ifdef U8X8_HAVE_HW_SPI
  726. uint8_t *data;
  727. uint8_t internal_spi_mode;
  728. switch(msg)
  729. {
  730. case U8X8_MSG_BYTE_SEND:
  731. // 1.6.5 offers a block transfer, but the problem is, that the
  732. // buffer is overwritten with the incoming data
  733. // so it can not be used...
  734. // SPI.transfer((uint8_t *)arg_ptr, arg_int);
  735. data = (uint8_t *)arg_ptr;
  736. while( arg_int > 0 )
  737. {
  738. SPI.transfer((uint8_t)*data);
  739. data++;
  740. arg_int--;
  741. }
  742. break;
  743. case U8X8_MSG_BYTE_INIT:
  744. if ( u8x8->bus_clock == 0 ) /* issue 769 */
  745. u8x8->bus_clock = u8x8->display_info->sck_clock_hz;
  746. /* disable chipselect */
  747. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  748. /* no wait required here */
  749. /* for SPI: setup correct level of the clock signal */
  750. // removed, use SPI.begin() instead: pinMode(11, OUTPUT);
  751. // removed, use SPI.begin() instead: pinMode(13, OUTPUT);
  752. // removed, use SPI.begin() instead: digitalWrite(13, u8x8_GetSPIClockPhase(u8x8));
  753. /* setup hardware with SPI.begin() instead of previous digitalWrite() and pinMode() calls */
  754. /* issue #377 */
  755. /* issue #378: removed ESP8266 support, which is implemented differently */
  756. #if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
  757. /* ESP32 has the following begin: SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
  758. /* not sure about ESP8266 */
  759. if ( u8x8->pins[U8X8_PIN_I2C_CLOCK] != U8X8_PIN_NONE && u8x8->pins[U8X8_PIN_I2C_DATA] != U8X8_PIN_NONE )
  760. {
  761. /* SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
  762. /* actually MISO is not used, but what else could be used here??? */
  763. SPI.begin(u8x8->pins[U8X8_PIN_I2C_CLOCK], MISO, u8x8->pins[U8X8_PIN_I2C_DATA]);
  764. }
  765. else
  766. {
  767. SPI.begin();
  768. }
  769. #else
  770. SPI.begin();
  771. #endif
  772. break;
  773. case U8X8_MSG_BYTE_SET_DC:
  774. u8x8_gpio_SetDC(u8x8, arg_int);
  775. break;
  776. case U8X8_MSG_BYTE_START_TRANSFER:
  777. /* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  778. internal_spi_mode = 0;
  779. switch(u8x8->display_info->spi_mode)
  780. {
  781. case 0: internal_spi_mode = SPI_MODE0; break;
  782. case 1: internal_spi_mode = SPI_MODE1; break;
  783. case 2: internal_spi_mode = SPI_MODE2; break;
  784. case 3: internal_spi_mode = SPI_MODE3; break;
  785. }
  786. #if ARDUINO >= 10600
  787. SPI.beginTransaction(SPISettings(u8x8->bus_clock, MSBFIRST, internal_spi_mode));
  788. #else
  789. SPI.begin();
  790. if ( u8x8->display_info->sck_pulse_width_ns < 70 )
  791. SPI.setClockDivider( SPI_CLOCK_DIV2 );
  792. else if ( u8x8->display_info->sck_pulse_width_ns < 140 )
  793. SPI.setClockDivider( SPI_CLOCK_DIV4 );
  794. else
  795. SPI.setClockDivider( SPI_CLOCK_DIV8 );
  796. SPI.setDataMode(internal_spi_mode);
  797. SPI.setBitOrder(MSBFIRST);
  798. #endif
  799. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  800. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  801. break;
  802. case U8X8_MSG_BYTE_END_TRANSFER:
  803. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  804. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  805. #if ARDUINO >= 10600
  806. SPI.endTransaction();
  807. #else
  808. SPI.end();
  809. #endif
  810. break;
  811. default:
  812. return 0;
  813. }
  814. #else /* U8X8_HAVE_HW_SPI */
  815. #endif /* U8X8_HAVE_HW_SPI */
  816. return 1;
  817. }
  818. /* issue #244 */
  819. extern "C" uint8_t u8x8_byte_arduino_2nd_hw_spi(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  820. {
  821. #ifdef U8X8_HAVE_2ND_HW_SPI
  822. uint8_t *data;
  823. uint8_t internal_spi_mode;
  824. switch(msg)
  825. {
  826. case U8X8_MSG_BYTE_SEND:
  827. // 1.6.5 offers a block transfer, but the problem is, that the
  828. // buffer is overwritten with the incoming data
  829. // so it can not be used...
  830. // SPI.transfer((uint8_t *)arg_ptr, arg_int);
  831. data = (uint8_t *)arg_ptr;
  832. while( arg_int > 0 )
  833. {
  834. SPI1.transfer((uint8_t)*data);
  835. data++;
  836. arg_int--;
  837. }
  838. break;
  839. case U8X8_MSG_BYTE_INIT:
  840. if ( u8x8->bus_clock == 0 ) /* issue 769 */
  841. u8x8->bus_clock = u8x8->display_info->sck_clock_hz;
  842. /* disable chipselect */
  843. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  844. /* no wait required here */
  845. /* for SPI1: setup correct level of the clock signal */
  846. // removed, use SPI.begin() instead: pinMode(11, OUTPUT);
  847. // removed, use SPI.begin() instead: pinMode(13, OUTPUT);
  848. // removed, use SPI.begin() instead: digitalWrite(13, u8x8_GetSPIClockPhase(u8x8));
  849. /* setup hardware with SPI.begin() instead of previous digitalWrite() and pinMode() calls */
  850. SPI1.begin();
  851. break;
  852. case U8X8_MSG_BYTE_SET_DC:
  853. u8x8_gpio_SetDC(u8x8, arg_int);
  854. break;
  855. case U8X8_MSG_BYTE_START_TRANSFER:
  856. /* SPI1 mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
  857. internal_spi_mode = 0;
  858. switch(u8x8->display_info->spi_mode)
  859. {
  860. case 0: internal_spi_mode = SPI_MODE0; break;
  861. case 1: internal_spi_mode = SPI_MODE1; break;
  862. case 2: internal_spi_mode = SPI_MODE2; break;
  863. case 3: internal_spi_mode = SPI_MODE3; break;
  864. }
  865. #if ARDUINO >= 10600
  866. SPI1.beginTransaction(SPISettings(u8x8->bus_clock, MSBFIRST, internal_spi_mode));
  867. #else
  868. SPI1.begin();
  869. if ( u8x8->display_info->sck_pulse_width_ns < 70 )
  870. SPI1.setClockDivider( SPI_CLOCK_DIV2 );
  871. else if ( u8x8->display_info->sck_pulse_width_ns < 140 )
  872. SPI1.setClockDivider( SPI_CLOCK_DIV4 );
  873. else
  874. SPI1.setClockDivider( SPI_CLOCK_DIV8 );
  875. SPI1.setDataMode(internal_spi_mode);
  876. SPI1.setBitOrder(MSBFIRST);
  877. #endif
  878. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  879. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  880. break;
  881. case U8X8_MSG_BYTE_END_TRANSFER:
  882. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  883. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  884. #if ARDUINO >= 10600
  885. SPI1.endTransaction();
  886. #else
  887. SPI1.end();
  888. #endif
  889. break;
  890. default:
  891. return 0;
  892. }
  893. #else
  894. #endif
  895. return 1;
  896. }
  897. /*=============================================*/
  898. /* fast SW I2C for AVR uC */
  899. #if !defined(U8X8_USE_PINS)
  900. /* no pin information (very strange), so fallback */
  901. extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  902. {
  903. return u8x8_byte_sw_i2c(u8x8, msg,arg_int, arg_ptr);
  904. }
  905. #elif !defined(U8X8_USE_ARDUINO_AVR_SW_I2C_OPTIMIZATION)
  906. extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  907. {
  908. return u8x8_byte_sw_i2c(u8x8, msg,arg_int, arg_ptr);
  909. }
  910. #elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
  911. /* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
  912. /* so, it should be possible to use multiple displays with different pins */
  913. static volatile uint8_t *arduino_i2c_clock_port;
  914. static uint8_t arduino_i2c_clock_mask;
  915. static uint8_t arduino_i2c_clock_n_mask;
  916. static volatile uint8_t *arduino_i2c_data_port;
  917. static uint8_t arduino_i2c_data_mask;
  918. static uint8_t arduino_i2c_data_n_mask;
  919. /*
  920. software i2c,
  921. ignores ACK response (which is anyway not provided by some displays)
  922. also does not allow reading from the device
  923. */
  924. static void i2c_delay(u8x8_t *u8x8) U8X8_NOINLINE;
  925. static void i2c_delay(u8x8_t *u8x8)
  926. {
  927. //u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_10MICRO, u8x8->display_info->i2c_bus_clock_100kHz);
  928. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_I2C, u8x8->display_info->i2c_bus_clock_100kHz);
  929. }
  930. static void i2c_init(u8x8_t *u8x8)
  931. {
  932. *arduino_i2c_clock_port |= arduino_i2c_clock_mask;
  933. *arduino_i2c_data_port |= arduino_i2c_data_mask;
  934. i2c_delay(u8x8);
  935. }
  936. /* actually, the scl line is not observed, so this procedure does not return a value */
  937. static void i2c_read_scl_and_delay(u8x8_t *u8x8)
  938. {
  939. /* set as input (line will be high) */
  940. *arduino_i2c_clock_port |= arduino_i2c_clock_mask;
  941. i2c_delay(u8x8);
  942. }
  943. static void i2c_clear_scl(u8x8_t *u8x8)
  944. {
  945. *arduino_i2c_clock_port &= arduino_i2c_clock_n_mask;
  946. }
  947. static void i2c_read_sda(u8x8_t *u8x8)
  948. {
  949. /* set as input (line will be high) */
  950. *arduino_i2c_data_port |= arduino_i2c_data_mask;
  951. }
  952. static void i2c_clear_sda(u8x8_t *u8x8)
  953. {
  954. /* set open collector and drive low */
  955. *arduino_i2c_data_port &= arduino_i2c_data_n_mask;
  956. }
  957. static void i2c_start(u8x8_t *u8x8)
  958. {
  959. if ( u8x8->i2c_started != 0 )
  960. {
  961. /* if already started: do restart */
  962. i2c_read_sda(u8x8); /* SDA = 1 */
  963. i2c_delay(u8x8);
  964. i2c_read_scl_and_delay(u8x8);
  965. }
  966. i2c_read_sda(u8x8);
  967. /* send the start condition, both lines go from 1 to 0 */
  968. i2c_clear_sda(u8x8);
  969. i2c_delay(u8x8);
  970. i2c_clear_scl(u8x8);
  971. u8x8->i2c_started = 1;
  972. }
  973. static void i2c_stop(u8x8_t *u8x8)
  974. {
  975. /* set SDA to 0 */
  976. i2c_clear_sda(u8x8);
  977. i2c_delay(u8x8);
  978. /* now release all lines */
  979. i2c_read_scl_and_delay(u8x8);
  980. /* set SDA to 1 */
  981. i2c_read_sda(u8x8);
  982. i2c_delay(u8x8);
  983. u8x8->i2c_started = 0;
  984. }
  985. static void i2c_write_bit(u8x8_t *u8x8, uint8_t val)
  986. {
  987. if (val)
  988. i2c_read_sda(u8x8);
  989. else
  990. i2c_clear_sda(u8x8);
  991. i2c_delay(u8x8);
  992. i2c_read_scl_and_delay(u8x8);
  993. i2c_clear_scl(u8x8);
  994. }
  995. static void i2c_read_bit(u8x8_t *u8x8)
  996. {
  997. //uint8_t val;
  998. /* do not drive SDA */
  999. i2c_read_sda(u8x8);
  1000. i2c_delay(u8x8);
  1001. i2c_read_scl_and_delay(u8x8);
  1002. i2c_read_sda(u8x8);
  1003. i2c_delay(u8x8);
  1004. i2c_clear_scl(u8x8);
  1005. //return val;
  1006. }
  1007. static void i2c_write_byte(u8x8_t *u8x8, uint8_t b)
  1008. {
  1009. i2c_write_bit(u8x8, b & 128);
  1010. i2c_write_bit(u8x8, b & 64);
  1011. i2c_write_bit(u8x8, b & 32);
  1012. i2c_write_bit(u8x8, b & 16);
  1013. i2c_write_bit(u8x8, b & 8);
  1014. i2c_write_bit(u8x8, b & 4);
  1015. i2c_write_bit(u8x8, b & 2);
  1016. i2c_write_bit(u8x8, b & 1);
  1017. /* read ack from client */
  1018. /* 0: ack was given by client */
  1019. /* 1: nothing happend during ack cycle */
  1020. i2c_read_bit(u8x8);
  1021. }
  1022. extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  1023. {
  1024. uint8_t *data;
  1025. switch(msg)
  1026. {
  1027. case U8X8_MSG_BYTE_SEND:
  1028. data = (uint8_t *)arg_ptr;
  1029. while( arg_int > 0 )
  1030. {
  1031. i2c_write_byte(u8x8, *data);
  1032. data++;
  1033. arg_int--;
  1034. }
  1035. break;
  1036. case U8X8_MSG_BYTE_INIT:
  1037. pinMode(u8x8->pins[U8X8_PIN_I2C_CLOCK], OUTPUT);
  1038. digitalWrite(u8x8->pins[U8X8_PIN_I2C_CLOCK], 1);
  1039. pinMode(u8x8->pins[U8X8_PIN_I2C_DATA], OUTPUT);
  1040. digitalWrite(u8x8->pins[U8X8_PIN_I2C_DATA], 1);
  1041. i2c_init(u8x8);
  1042. break;
  1043. case U8X8_MSG_BYTE_SET_DC:
  1044. break;
  1045. case U8X8_MSG_BYTE_START_TRANSFER:
  1046. /* there is no consistency checking for u8x8->pins[U8X8_PIN_I2C_CLOCK] */
  1047. arduino_i2c_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_I2C_CLOCK]));
  1048. arduino_i2c_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_I2C_CLOCK]);
  1049. arduino_i2c_clock_n_mask = ~arduino_i2c_clock_mask;
  1050. /* there is no consistency checking for u8x8->pins[U8X8_PIN_I2C_DATA] */
  1051. arduino_i2c_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_I2C_DATA]));
  1052. arduino_i2c_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_I2C_DATA]);
  1053. arduino_i2c_data_n_mask = ~arduino_i2c_data_mask;
  1054. i2c_start(u8x8);
  1055. i2c_write_byte(u8x8, u8x8_GetI2CAddress(u8x8));
  1056. break;
  1057. case U8X8_MSG_BYTE_END_TRANSFER:
  1058. i2c_stop(u8x8);
  1059. break;
  1060. default:
  1061. return 0;
  1062. }
  1063. return 1;
  1064. }
  1065. #else
  1066. /* not AVR architecture, fallback */
  1067. extern "C" uint8_t u8x8_byte_arduino_sw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  1068. {
  1069. return u8x8_byte_sw_i2c(u8x8, msg,arg_int, arg_ptr);
  1070. }
  1071. #endif
  1072. /*=============================================*/
  1073. /*=== HARDWARE I2C ===*/
  1074. extern "C" uint8_t u8x8_byte_arduino_hw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  1075. {
  1076. #ifdef U8X8_HAVE_HW_I2C
  1077. switch(msg)
  1078. {
  1079. case U8X8_MSG_BYTE_SEND:
  1080. Wire.write((uint8_t *)arg_ptr, (int)arg_int);
  1081. break;
  1082. case U8X8_MSG_BYTE_INIT:
  1083. if ( u8x8->bus_clock == 0 ) /* issue 769 */
  1084. u8x8->bus_clock = u8x8->display_info->i2c_bus_clock_100kHz * 100000UL;
  1085. #if defined(ESP8266) || defined(ARDUINO_ARCH_ESP8266) || defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
  1086. /* for ESP8266/ESP32, Wire.begin has two more arguments: clock and data */
  1087. if ( u8x8->pins[U8X8_PIN_I2C_CLOCK] != U8X8_PIN_NONE && u8x8->pins[U8X8_PIN_I2C_DATA] != U8X8_PIN_NONE )
  1088. {
  1089. // second argument for the wire lib is the clock pin. In u8g2, the first argument of the clock pin in the clock/data pair
  1090. Wire.begin(u8x8->pins[U8X8_PIN_I2C_DATA] , u8x8->pins[U8X8_PIN_I2C_CLOCK]);
  1091. }
  1092. else
  1093. {
  1094. Wire.begin();
  1095. }
  1096. #else
  1097. Wire.begin();
  1098. #endif
  1099. break;
  1100. case U8X8_MSG_BYTE_SET_DC:
  1101. break;
  1102. case U8X8_MSG_BYTE_START_TRANSFER:
  1103. #if ARDUINO >= 10600
  1104. /* not sure when the setClock function was introduced, but it is there since 1.6.0 */
  1105. /* if there is any error with Wire.setClock() just remove this function call */
  1106. Wire.setClock(u8x8->bus_clock);
  1107. #endif
  1108. Wire.beginTransmission(u8x8_GetI2CAddress(u8x8)>>1);
  1109. break;
  1110. case U8X8_MSG_BYTE_END_TRANSFER:
  1111. Wire.endTransmission();
  1112. break;
  1113. default:
  1114. return 0;
  1115. }
  1116. #endif
  1117. return 1;
  1118. }
  1119. extern "C" uint8_t u8x8_byte_arduino_2nd_hw_i2c(U8X8_UNUSED u8x8_t *u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  1120. {
  1121. #ifdef U8X8_HAVE_2ND_HW_I2C
  1122. switch(msg)
  1123. {
  1124. case U8X8_MSG_BYTE_SEND:
  1125. Wire1.write((uint8_t *)arg_ptr, (int)arg_int);
  1126. break;
  1127. case U8X8_MSG_BYTE_INIT:
  1128. if ( u8x8->bus_clock == 0 ) /* issue 769 */
  1129. u8x8->bus_clock = u8x8->display_info->i2c_bus_clock_100kHz * 100000UL;
  1130. Wire1.begin();
  1131. break;
  1132. case U8X8_MSG_BYTE_SET_DC:
  1133. break;
  1134. case U8X8_MSG_BYTE_START_TRANSFER:
  1135. #if ARDUINO >= 10600
  1136. /* not sure when the setClock function was introduced, but it is there since 1.6.0 */
  1137. /* if there is any error with Wire.setClock() just remove this function call */
  1138. Wire1.setClock(u8x8->bus_clock);
  1139. #endif
  1140. Wire1.beginTransmission(u8x8_GetI2CAddress(u8x8)>>1);
  1141. break;
  1142. case U8X8_MSG_BYTE_END_TRANSFER:
  1143. Wire1.endTransmission();
  1144. break;
  1145. default:
  1146. return 0;
  1147. }
  1148. #endif
  1149. return 1;
  1150. }
  1151. #endif // U8X8_USE_PINS
  1152. /*=============================================*/
  1153. /*
  1154. replacement for a more faster u8x8_byte_8bit_8080mode
  1155. in general u8x8_byte_8bit_8080mode could be a fallback:
  1156. uint8_t u8x8_byte_arduino_8bit_8080mode(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1157. {
  1158. return u8x8_byte_8bit_8080mode(u8x8, msg,arg_int, arg_ptr);
  1159. }
  1160. */
  1161. #ifndef __AVR_ARCH__
  1162. #define __AVR_ARCH__ 0
  1163. #endif
  1164. #if !defined(U8X8_USE_PINS)
  1165. /* no pin information (very strange), so fallback */
  1166. extern "C" uint8_t u8x8_byte_arduino_8bit_8080mode(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1167. {
  1168. return u8x8_byte_8bit_8080mode(u8x8, msg,arg_int, arg_ptr);
  1169. }
  1170. #elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
  1171. /* this function completly replaces u8x8_byte_8bit_8080mode*/
  1172. extern "C" uint8_t u8x8_byte_arduino_8bit_8080mode(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1173. {
  1174. uint8_t i, b;
  1175. uint8_t *data;
  1176. /* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
  1177. /* so, it should be possible to use multiple displays with different pins */
  1178. static volatile uint8_t *arduino_e_port;
  1179. static volatile uint8_t arduino_e_mask;
  1180. static volatile uint8_t arduino_e_n_mask;
  1181. static volatile uint8_t *arduino_data_port[8];
  1182. static volatile uint8_t arduino_data_mask[8];
  1183. static volatile uint8_t arduino_data_n_mask[8];
  1184. switch(msg)
  1185. {
  1186. case U8X8_MSG_BYTE_SEND:
  1187. data = (uint8_t *)arg_ptr;
  1188. while( arg_int > 0 )
  1189. {
  1190. b = *data;
  1191. data++;
  1192. arg_int--;
  1193. for( i = 0; i < 8; i++ )
  1194. {
  1195. if ( b & 1 )
  1196. *arduino_data_port[i] |= arduino_data_mask[i];
  1197. else
  1198. *arduino_data_port[i] &= arduino_data_n_mask[i];
  1199. b >>= 1;
  1200. }
  1201. *arduino_e_port &= arduino_e_n_mask;
  1202. /* AVR Architecture is very slow, extra call is not required */
  1203. //u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->sda_setup_time_ns);
  1204. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->data_setup_time_ns);
  1205. *arduino_e_port |= arduino_e_mask;
  1206. /* AVR Architecture is very slow, extra call is not required */
  1207. //u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->sck_pulse_width_ns);
  1208. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->write_pulse_width_ns);
  1209. }
  1210. break;
  1211. case U8X8_MSG_BYTE_INIT:
  1212. /* disable chipselect */
  1213. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1214. /* no wait required here */
  1215. /* ensure that the enable signal is high */
  1216. u8x8_gpio_call(u8x8, U8X8_MSG_GPIO_E, 1);
  1217. break;
  1218. case U8X8_MSG_BYTE_SET_DC:
  1219. u8x8_gpio_SetDC(u8x8, arg_int);
  1220. break;
  1221. case U8X8_MSG_BYTE_START_TRANSFER:
  1222. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
  1223. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1224. /* there is no consistency checking for u8x8->pins[U8X8_PIN_E] */
  1225. arduino_e_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_E]));
  1226. arduino_e_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_E]);
  1227. arduino_e_n_mask = ~arduino_e_mask;
  1228. /* there is no consistency checking for u8x8->pins[U8X8_PIN_D0] */
  1229. for( i = 0; i < 8; i++ )
  1230. {
  1231. arduino_data_port[i] = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_D0+i]));
  1232. arduino_data_mask[i] = digitalPinToBitMask(u8x8->pins[U8X8_PIN_D0+i]);
  1233. arduino_data_n_mask[i] = ~arduino_data_mask[i];
  1234. }
  1235. break;
  1236. case U8X8_MSG_BYTE_END_TRANSFER:
  1237. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1238. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1239. break;
  1240. default:
  1241. return 0;
  1242. }
  1243. return 1;
  1244. }
  1245. #else
  1246. /* fallback */
  1247. extern "C" uint8_t u8x8_byte_arduino_8bit_8080mode(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1248. {
  1249. return u8x8_byte_8bit_8080mode(u8x8, msg,arg_int, arg_ptr);
  1250. }
  1251. #endif
  1252. /*=============================================*/
  1253. /*
  1254. replacement for a more faster u8x8_byte_ks0108
  1255. in general u8x8_byte_ks0108 could be a fallback:
  1256. uint8_t u8x8_byte_arduino_ks0108(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1257. {
  1258. return u8x8_byte_ks0108(u8x8, msg,arg_int, arg_ptr);
  1259. }
  1260. */
  1261. #ifndef __AVR_ARCH__
  1262. #define __AVR_ARCH__ 0
  1263. #endif
  1264. #if !defined(U8X8_USE_PINS)
  1265. /* no pin information (very strange), so fallback */
  1266. extern "C" uint8_t u8x8_byte_arduino_ks0108(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1267. {
  1268. return u8x8_byte_ks0108(u8x8, msg,arg_int, arg_ptr);
  1269. }
  1270. #elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
  1271. /* this function completly replaces u8x8_byte_ks0108*/
  1272. extern "C" uint8_t u8x8_byte_arduino_ks0108(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1273. {
  1274. uint8_t i, b;
  1275. uint8_t *data;
  1276. /* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
  1277. /* so, it should be possible to use multiple displays with different pins */
  1278. static volatile uint8_t *arduino_e_port;
  1279. static volatile uint8_t arduino_e_mask;
  1280. static volatile uint8_t arduino_e_n_mask;
  1281. static volatile uint8_t *arduino_data_port[8];
  1282. static volatile uint8_t arduino_data_mask[8];
  1283. static volatile uint8_t arduino_data_n_mask[8];
  1284. switch(msg)
  1285. {
  1286. case U8X8_MSG_BYTE_SEND:
  1287. data = (uint8_t *)arg_ptr;
  1288. while( arg_int > 0 )
  1289. {
  1290. b = *data;
  1291. data++;
  1292. arg_int--;
  1293. for( i = 0; i < 8; i++ )
  1294. {
  1295. if ( b & 1 )
  1296. *arduino_data_port[i] |= arduino_data_mask[i];
  1297. else
  1298. *arduino_data_port[i] &= arduino_data_n_mask[i];
  1299. b >>= 1;
  1300. }
  1301. *arduino_e_port |= arduino_e_mask;
  1302. /* AVR Architecture is very slow, extra call is not required */
  1303. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->data_setup_time_ns);
  1304. *arduino_e_port &= arduino_e_n_mask;
  1305. /* AVR Architecture is very slow, extra call is not required */
  1306. u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->write_pulse_width_ns);
  1307. }
  1308. break;
  1309. case U8X8_MSG_BYTE_INIT:
  1310. /* disable chipselect */
  1311. u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
  1312. /* no wait required here */
  1313. /* ensure that the enable signal is low */
  1314. u8x8_gpio_call(u8x8, U8X8_MSG_GPIO_E, 0);
  1315. break;
  1316. case U8X8_MSG_BYTE_SET_DC:
  1317. u8x8_gpio_SetDC(u8x8, arg_int);
  1318. break;
  1319. case U8X8_MSG_BYTE_START_TRANSFER:
  1320. u8x8_byte_set_ks0108_cs(u8x8, arg_int);
  1321. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
  1322. /* there is no consistency checking for u8x8->pins[U8X8_PIN_E] */
  1323. arduino_e_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_E]));
  1324. arduino_e_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_E]);
  1325. arduino_e_n_mask = ~arduino_e_mask;
  1326. /* there is no consistency checking for u8x8->pins[U8X8_PIN_D0] */
  1327. for( i = 0; i < 8; i++ )
  1328. {
  1329. arduino_data_port[i] = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_D0+i]));
  1330. arduino_data_mask[i] = digitalPinToBitMask(u8x8->pins[U8X8_PIN_D0+i]);
  1331. arduino_data_n_mask[i] = ~arduino_data_mask[i];
  1332. }
  1333. break;
  1334. case U8X8_MSG_BYTE_END_TRANSFER:
  1335. u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
  1336. u8x8_byte_set_ks0108_cs(u8x8, arg_int);
  1337. break;
  1338. default:
  1339. return 0;
  1340. }
  1341. return 1;
  1342. }
  1343. #else
  1344. /* fallback */
  1345. extern "C" uint8_t u8x8_byte_arduino_ks0108(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  1346. {
  1347. return u8x8_byte_ks0108(u8x8, msg,arg_int, arg_ptr);
  1348. }
  1349. #endif
  1350. #endif /*ARDUINO*/
  1351. #ifdef U8X8_USE_PINS
  1352. /*
  1353. use U8X8_PIN_NONE as value for "reset", if there is no reset line
  1354. */
  1355. void u8x8_SetPin_4Wire_SW_SPI(u8x8_t *u8x8, uint8_t clock, uint8_t data, uint8_t cs, uint8_t dc, uint8_t reset)
  1356. {
  1357. u8x8_SetPin(u8x8, U8X8_PIN_SPI_CLOCK, clock);
  1358. u8x8_SetPin(u8x8, U8X8_PIN_SPI_DATA, data);
  1359. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1360. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1361. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1362. }
  1363. #ifdef _obsolete_com_specific_setup
  1364. void u8x8_Setup_4Wire_SW_SPI(u8x8_t *u8x8, u8x8_msg_cb display_cb, uint8_t clock, uint8_t data, uint8_t cs, uint8_t dc, uint8_t reset)
  1365. {
  1366. u8x8_Setup(u8x8, display_cb, u8x8_cad_001, u8x8_byte_4wire_sw_spi, u8x8_gpio_and_delay_arduino);
  1367. /* assign individual pin values (only for ARDUINO, if pin_list is available) */
  1368. u8x8_SetPin(u8x8, U8X8_PIN_SPI_CLOCK, clock);
  1369. u8x8_SetPin(u8x8, U8X8_PIN_SPI_DATA, data);
  1370. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1371. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1372. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1373. }
  1374. #endif /* obsolete com specific setup */
  1375. void u8x8_SetPin_3Wire_SW_SPI(u8x8_t *u8x8, uint8_t clock, uint8_t data, uint8_t cs, uint8_t reset)
  1376. {
  1377. u8x8_SetPin(u8x8, U8X8_PIN_SPI_CLOCK, clock);
  1378. u8x8_SetPin(u8x8, U8X8_PIN_SPI_DATA, data);
  1379. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1380. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1381. }
  1382. #ifdef _obsolete_com_specific_setup
  1383. void u8x8_Setup_3Wire_SW_SPI(u8x8_t *u8x8, u8x8_msg_cb display_cb, uint8_t clock, uint8_t data, uint8_t cs, uint8_t reset)
  1384. {
  1385. u8x8_Setup(u8x8, display_cb, u8x8_cad_001, u8x8_byte_3wire_sw_spi, u8x8_gpio_and_delay_arduino);
  1386. /* assign individual pin values (only for ARDUINO, if pin_list is available) */
  1387. u8x8_SetPin(u8x8, U8X8_PIN_SPI_CLOCK, clock);
  1388. u8x8_SetPin(u8x8, U8X8_PIN_SPI_DATA, data);
  1389. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1390. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1391. }
  1392. #endif /* obsolete com specific setup */
  1393. /*
  1394. use U8X8_PIN_NONE as value for "reset", if there is no reset line
  1395. */
  1396. void u8x8_SetPin_3Wire_HW_SPI(u8x8_t *u8x8, uint8_t cs, uint8_t reset)
  1397. {
  1398. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1399. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1400. }
  1401. /*
  1402. use U8X8_PIN_NONE as value for "reset", if there is no reset line
  1403. */
  1404. void u8x8_SetPin_4Wire_HW_SPI(u8x8_t *u8x8, uint8_t cs, uint8_t dc, uint8_t reset)
  1405. {
  1406. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1407. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1408. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1409. }
  1410. void u8x8_SetPin_ST7920_HW_SPI(u8x8_t *u8x8, uint8_t cs, uint8_t reset)
  1411. {
  1412. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1413. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1414. }
  1415. #ifdef _obsolete_com_specific_setup
  1416. void u8x8_Setup_4Wire_HW_SPI(u8x8_t *u8x8, u8x8_msg_cb display_cb, uint8_t cs, uint8_t dc, uint8_t reset)
  1417. {
  1418. u8x8_Setup(u8x8, display_cb, u8x8_cad_001, u8x8_byte_arduino_hw_spi, u8x8_gpio_and_delay_arduino);
  1419. /* assign individual pin values (only for ARDUINO, if pin_list is available) */
  1420. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1421. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1422. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1423. }
  1424. #endif /* obsolete com specific setup */
  1425. void u8x8_SetPin_SW_I2C(u8x8_t *u8x8, uint8_t clock, uint8_t data, uint8_t reset)
  1426. {
  1427. u8x8_SetPin(u8x8, U8X8_PIN_I2C_CLOCK, clock);
  1428. u8x8_SetPin(u8x8, U8X8_PIN_I2C_DATA, data);
  1429. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1430. }
  1431. #ifdef _obsolete_com_specific_setup
  1432. void u8x8_Setup_SSD13xx_SW_I2C(u8x8_t *u8x8, u8x8_msg_cb display_cb, uint8_t clock, uint8_t data, uint8_t reset)
  1433. {
  1434. u8x8_Setup(u8x8, display_cb, u8x8_cad_001, u8x8_byte_ssd13xx_sw_i2c, u8x8_gpio_and_delay_arduino);
  1435. /* assign individual pin values (only for ARDUINO, if pin_list is available) */
  1436. u8x8_SetPin(u8x8, U8X8_PIN_I2C_CLOCK, clock);
  1437. u8x8_SetPin(u8x8, U8X8_PIN_I2C_DATA, data);
  1438. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1439. }
  1440. #endif /* obsolete com specific setup */
  1441. void u8x8_SetPin_HW_I2C(u8x8_t *u8x8, uint8_t reset, uint8_t clock, uint8_t data)
  1442. {
  1443. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1444. u8x8_SetPin(u8x8, U8X8_PIN_I2C_CLOCK, clock);
  1445. u8x8_SetPin(u8x8, U8X8_PIN_I2C_DATA, data);
  1446. }
  1447. void u8x8_SetPin_8Bit_6800(u8x8_t *u8x8, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t enable, uint8_t cs, uint8_t dc, uint8_t reset)
  1448. {
  1449. u8x8_SetPin(u8x8, U8X8_PIN_D0, d0);
  1450. u8x8_SetPin(u8x8, U8X8_PIN_D1, d1);
  1451. u8x8_SetPin(u8x8, U8X8_PIN_D2, d2);
  1452. u8x8_SetPin(u8x8, U8X8_PIN_D3, d3);
  1453. u8x8_SetPin(u8x8, U8X8_PIN_D4, d4);
  1454. u8x8_SetPin(u8x8, U8X8_PIN_D5, d5);
  1455. u8x8_SetPin(u8x8, U8X8_PIN_D6, d6);
  1456. u8x8_SetPin(u8x8, U8X8_PIN_D7, d7);
  1457. u8x8_SetPin(u8x8, U8X8_PIN_E, enable);
  1458. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1459. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1460. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1461. }
  1462. #ifdef _obsolete_com_specific_setup
  1463. void u8x8_Setup_8Bit_6800(u8x8_t *u8x8, u8x8_msg_cb display_cb, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t enable, uint8_t cs, uint8_t dc, uint8_t reset)
  1464. {
  1465. u8x8_Setup(u8x8, display_cb, u8x8_cad_001, u8x8_byte_8bit_6800mode, u8x8_gpio_and_delay_arduino);
  1466. /* assign individual pin values (only for ARDUINO, if pin_list is available) */
  1467. u8x8_SetPin(u8x8, U8X8_PIN_D0, d0);
  1468. u8x8_SetPin(u8x8, U8X8_PIN_D1, d1);
  1469. u8x8_SetPin(u8x8, U8X8_PIN_D2, d2);
  1470. u8x8_SetPin(u8x8, U8X8_PIN_D3, d3);
  1471. u8x8_SetPin(u8x8, U8X8_PIN_D4, d4);
  1472. u8x8_SetPin(u8x8, U8X8_PIN_D5, d5);
  1473. u8x8_SetPin(u8x8, U8X8_PIN_D6, d6);
  1474. u8x8_SetPin(u8x8, U8X8_PIN_D7, d7);
  1475. u8x8_SetPin(u8x8, U8X8_PIN_E, enable);
  1476. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1477. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1478. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1479. }
  1480. #endif /* obsolete com specific setup */
  1481. void u8x8_SetPin_8Bit_8080(u8x8_t *u8x8, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t wr, uint8_t cs, uint8_t dc, uint8_t reset)
  1482. {
  1483. u8x8_SetPin(u8x8, U8X8_PIN_D0, d0);
  1484. u8x8_SetPin(u8x8, U8X8_PIN_D1, d1);
  1485. u8x8_SetPin(u8x8, U8X8_PIN_D2, d2);
  1486. u8x8_SetPin(u8x8, U8X8_PIN_D3, d3);
  1487. u8x8_SetPin(u8x8, U8X8_PIN_D4, d4);
  1488. u8x8_SetPin(u8x8, U8X8_PIN_D5, d5);
  1489. u8x8_SetPin(u8x8, U8X8_PIN_D6, d6);
  1490. u8x8_SetPin(u8x8, U8X8_PIN_D7, d7);
  1491. u8x8_SetPin(u8x8, U8X8_PIN_E, wr);
  1492. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1493. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1494. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1495. }
  1496. #ifdef _obsolete_com_specific_setup
  1497. void u8x8_Setup_8Bit_8080(u8x8_t *u8x8, u8x8_msg_cb display_cb, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t wr, uint8_t cs, uint8_t dc, uint8_t reset)
  1498. {
  1499. u8x8_Setup(u8x8, display_cb, u8x8_cad_001, u8x8_byte_8bit_8080mode, u8x8_gpio_and_delay_arduino);
  1500. /* assign individual pin values (only for ARDUINO, if pin_list is available) */
  1501. u8x8_SetPin(u8x8, U8X8_PIN_D0, d0);
  1502. u8x8_SetPin(u8x8, U8X8_PIN_D1, d1);
  1503. u8x8_SetPin(u8x8, U8X8_PIN_D2, d2);
  1504. u8x8_SetPin(u8x8, U8X8_PIN_D3, d3);
  1505. u8x8_SetPin(u8x8, U8X8_PIN_D4, d4);
  1506. u8x8_SetPin(u8x8, U8X8_PIN_D5, d5);
  1507. u8x8_SetPin(u8x8, U8X8_PIN_D6, d6);
  1508. u8x8_SetPin(u8x8, U8X8_PIN_D7, d7);
  1509. u8x8_SetPin(u8x8, U8X8_PIN_E, wr);
  1510. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  1511. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1512. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1513. }
  1514. #endif /* obsolete com specific setup */
  1515. void u8x8_SetPin_KS0108(u8x8_t *u8x8, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t enable, uint8_t dc, uint8_t cs0, uint8_t cs1, uint8_t cs2, uint8_t reset)
  1516. {
  1517. u8x8_SetPin(u8x8, U8X8_PIN_D0, d0);
  1518. u8x8_SetPin(u8x8, U8X8_PIN_D1, d1);
  1519. u8x8_SetPin(u8x8, U8X8_PIN_D2, d2);
  1520. u8x8_SetPin(u8x8, U8X8_PIN_D3, d3);
  1521. u8x8_SetPin(u8x8, U8X8_PIN_D4, d4);
  1522. u8x8_SetPin(u8x8, U8X8_PIN_D5, d5);
  1523. u8x8_SetPin(u8x8, U8X8_PIN_D6, d6);
  1524. u8x8_SetPin(u8x8, U8X8_PIN_D7, d7);
  1525. u8x8_SetPin(u8x8, U8X8_PIN_E, enable);
  1526. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1527. u8x8_SetPin(u8x8, U8X8_PIN_CS, cs0);
  1528. u8x8_SetPin(u8x8, U8X8_PIN_CS1, cs1);
  1529. u8x8_SetPin(u8x8, U8X8_PIN_CS2, cs2);
  1530. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1531. }
  1532. void u8x8_SetPin_SED1520(u8x8_t *u8x8, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t dc, uint8_t e1, uint8_t e2, uint8_t reset)
  1533. {
  1534. u8x8_SetPin(u8x8, U8X8_PIN_D0, d0);
  1535. u8x8_SetPin(u8x8, U8X8_PIN_D1, d1);
  1536. u8x8_SetPin(u8x8, U8X8_PIN_D2, d2);
  1537. u8x8_SetPin(u8x8, U8X8_PIN_D3, d3);
  1538. u8x8_SetPin(u8x8, U8X8_PIN_D4, d4);
  1539. u8x8_SetPin(u8x8, U8X8_PIN_D5, d5);
  1540. u8x8_SetPin(u8x8, U8X8_PIN_D6, d6);
  1541. u8x8_SetPin(u8x8, U8X8_PIN_D7, d7);
  1542. u8x8_SetPin(u8x8, U8X8_PIN_E, e1);
  1543. u8x8_SetPin(u8x8, U8X8_PIN_CS, e2);
  1544. u8x8_SetPin(u8x8, U8X8_PIN_DC, dc);
  1545. u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
  1546. }
  1547. #endif // U8X8_USE_PINS