u8g_com_arduino_hw_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. u8g_com_arduino_hw_spi.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. SPI Clock Cycle Type
  27. SSD1351 50ns 20 MHz
  28. SSD1322 300ns 3.3 MHz
  29. SSD1327 300ns
  30. SSD1306 300ns
  31. ST7565 400ns 2.5 MHz
  32. ST7920 400ns
  33. Arduino DUE
  34. PA25 MISO
  35. PA26 MOSI 75
  36. PA27 SCLK 76
  37. typedef struct {
  38. WoReg SPI_CR; (Spi Offset: 0x00) Control Register
  39. RwReg SPI_MR; (Spi Offset: 0x04) Mode Register
  40. RoReg SPI_RDR; (Spi Offset: 0x08) Receive Data Register
  41. WoReg SPI_TDR; (Spi Offset: 0x0C) Transmit Data Register
  42. RoReg SPI_SR; (Spi Offset: 0x10) Status Register
  43. WoReg SPI_IER; (Spi Offset: 0x14) Interrupt Enable Register
  44. WoReg SPI_IDR; (Spi Offset: 0x18) Interrupt Disable Register
  45. RoReg SPI_IMR; (Spi Offset: 0x1C) Interrupt Mask Register
  46. RoReg Reserved1[4];
  47. RwReg SPI_CSR[4]; (Spi Offset: 0x30) Chip Select Register
  48. RoReg Reserved2[41];
  49. RwReg SPI_WPMR; (Spi Offset: 0xE4) Write Protection Control Register
  50. RoReg SPI_WPSR; (Spi Offset: 0xE8) Write Protection Status Register
  51. } Spi;
  52. Power Management Controller (PMC)
  53. arduino-1.5.2/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/instance/instance_pmc.h
  54. - enable PIO
  55. REG_PMC_PCER0 = 1UL << ID_PIOA
  56. - enable SPI
  57. REG_PMC_PCER0 = 1UL << ID_SPI0
  58. - enable PIOA and SPI0
  59. REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
  60. Parallel Input/Output Controller (PIO)
  61. arduino-1.5.2/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/instance/instance_pioa.h
  62. - enable special function of the pin: disable PIO on A26 and A27:
  63. REG_PIOA_PDR = 0x0c000000
  64. PIOA->PIO_PDR = 0x0c000000
  65. SPI
  66. SPI0->SPI_CR = SPI_CR_SPIDIS
  67. SPI0->SPI_CR = SPI_CR_SWRST ;
  68. SPI0->SPI_CR = SPI_CR_SWRST ;
  69. SPI0->SPI_CR = SPI_CR_SPIEN
  70. Bit 0: Master Mode = 1 (active)
  71. Bit 1: Peripheral Select = 0 (fixed)
  72. Bit 2: Chip Select Decode Mode = 1 (4 to 16)
  73. Bit 4: Mode Fault Detection = 1 (disabled)
  74. Bit 5: Wait Data Read = 0 (disabled)
  75. Bit 7: Loop Back Mode = 0 (disabled)
  76. Bit 16-19: Peripheral Chip Select = 0 (chip select 0)
  77. SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PCSDEC | SPI_MR_MODFDIS
  78. Bit 0: Clock Polarity = 0
  79. Bit 1: Clock Phase = 0
  80. Bit 4-7: Bits = 0 (8 Bit)
  81. Bit 8-15: SCBR = 1
  82. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(x) Serial Baud Rate
  83. SCBR / 84000000 > 50 / 1000000000
  84. SCBR / 84 > 5 / 100
  85. SCBR > 50 *84 / 1000 --> SCBR=5
  86. SCBR > 300*84 / 1000 --> SCBR=26
  87. SCBR > 400*84 / 1000 --> SCBR=34
  88. Arduino Due test code:
  89. REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
  90. REG_PIOA_PDR = 0x0c000000;
  91. SPI0->SPI_CR = SPI_CR_SPIDIS;
  92. SPI0->SPI_CR = SPI_CR_SWRST;
  93. SPI0->SPI_CR = SPI_CR_SWRST;
  94. SPI0->SPI_CR = SPI_CR_SPIEN;
  95. SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PCSDEC | SPI_MR_MODFDIS;
  96. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(30);
  97. for(;;)
  98. {
  99. while( (SPI0->SPI_SR & SPI_SR_TDRE) == 0 )
  100. ;
  101. SPI0->SPI_TDR = 0x050;
  102. }
  103. */
  104. #include "u8g.h"
  105. #if defined(ARDUINO)
  106. #if defined(__AVR__)
  107. #define U8G_ARDUINO_ATMEGA_HW_SPI
  108. /* remove the definition for attiny */
  109. #if __AVR_ARCH__ == 2
  110. #undef U8G_ARDUINO_ATMEGA_HW_SPI
  111. #endif
  112. #if __AVR_ARCH__ == 25
  113. #undef U8G_ARDUINO_ATMEGA_HW_SPI
  114. #endif
  115. #endif
  116. #if defined(U8G_ARDUINO_ATMEGA_HW_SPI)
  117. #include <avr/interrupt.h>
  118. #include <avr/io.h>
  119. #if ARDUINO < 100
  120. #include <WProgram.h>
  121. /* fixed pins */
  122. #if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) // Sanguino.cc board
  123. #define PIN_SCK 7
  124. #define PIN_MISO 6
  125. #define PIN_MOSI 5
  126. #define PIN_CS 4
  127. #else // Arduino Board
  128. #define PIN_SCK 13
  129. #define PIN_MISO 12
  130. #define PIN_MOSI 11
  131. #define PIN_CS 10
  132. #endif // (__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
  133. #else
  134. #include <Arduino.h>
  135. /* use Arduino pin definitions */
  136. #define PIN_SCK SCK
  137. #define PIN_MISO MISO
  138. #define PIN_MOSI MOSI
  139. #define PIN_CS SS
  140. #endif
  141. //static uint8_t u8g_spi_out(uint8_t data) U8G_NOINLINE;
  142. static uint8_t u8g_spi_out(uint8_t data)
  143. {
  144. /* unsigned char x = 100; */
  145. /* send data */
  146. SPDR = data;
  147. /* wait for transmission */
  148. while (!(SPSR & (1<<SPIF)))
  149. ;
  150. /* clear the SPIF flag by reading SPDR */
  151. return SPDR;
  152. }
  153. uint8_t u8g_com_arduino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  154. {
  155. switch(msg)
  156. {
  157. case U8G_COM_MSG_STOP:
  158. break;
  159. case U8G_COM_MSG_INIT:
  160. u8g_com_arduino_assign_pin_output_high(u8g);
  161. pinMode(PIN_SCK, OUTPUT);
  162. digitalWrite(PIN_SCK, LOW);
  163. pinMode(PIN_MOSI, OUTPUT);
  164. digitalWrite(PIN_MOSI, LOW);
  165. /* pinMode(PIN_MISO, INPUT); */
  166. pinMode(PIN_CS, OUTPUT); /* system chip select for the atmega board */
  167. digitalWrite(PIN_CS, HIGH);
  168. /*
  169. SPR1 SPR0
  170. 0 0 fclk/4
  171. 0 1 fclk/16
  172. 1 0 fclk/64
  173. 1 1 fclk/128
  174. */
  175. SPCR = 0;
  176. SPCR = (1<<SPE) | (1<<MSTR)|(0<<SPR1)|(0<<SPR0)|(0<<CPOL)|(0<<CPHA);
  177. #ifdef U8G_HW_SPI_2X
  178. SPSR = (1 << SPI2X); /* double speed, issue 89 */
  179. #else
  180. if ( arg_val <= U8G_SPI_CLK_CYCLE_50NS )
  181. {
  182. SPSR = (1 << SPI2X); /* double speed, issue 89 */
  183. }
  184. #endif
  185. break;
  186. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  187. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  188. break;
  189. case U8G_COM_MSG_CHIP_SELECT:
  190. if ( arg_val == 0 )
  191. {
  192. /* disable */
  193. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  194. }
  195. else
  196. {
  197. /* enable */
  198. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  199. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  200. }
  201. break;
  202. case U8G_COM_MSG_RESET:
  203. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  204. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  205. break;
  206. case U8G_COM_MSG_WRITE_BYTE:
  207. u8g_spi_out(arg_val);
  208. break;
  209. case U8G_COM_MSG_WRITE_SEQ:
  210. {
  211. register uint8_t *ptr = arg_ptr;
  212. while( arg_val > 0 )
  213. {
  214. u8g_spi_out(*ptr++);
  215. arg_val--;
  216. }
  217. }
  218. break;
  219. case U8G_COM_MSG_WRITE_SEQ_P:
  220. {
  221. register uint8_t *ptr = arg_ptr;
  222. while( arg_val > 0 )
  223. {
  224. u8g_spi_out(u8g_pgm_read(ptr));
  225. ptr++;
  226. arg_val--;
  227. }
  228. }
  229. break;
  230. }
  231. return 1;
  232. }
  233. /* #elif defined(__18CXX) || defined(__PIC32MX) */
  234. #elif defined(__SAM3X8E__) // Arduino Due, maybe we should better check for __SAM3X8E__
  235. #include <Arduino.h>
  236. /* use Arduino pin definitions */
  237. #define PIN_SCK SCK
  238. #define PIN_MISO MISO
  239. #define PIN_MOSI MOSI
  240. #define PIN_CS SS
  241. static uint8_t u8g_spi_out(uint8_t data)
  242. {
  243. /* wait until tx register is empty */
  244. while( (SPI0->SPI_SR & SPI_SR_TDRE) == 0 )
  245. ;
  246. /* send data */
  247. SPI0->SPI_TDR = (uint32_t)data;
  248. return data;
  249. }
  250. uint8_t u8g_com_arduino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  251. {
  252. switch(msg)
  253. {
  254. case U8G_COM_MSG_STOP:
  255. break;
  256. case U8G_COM_MSG_INIT:
  257. u8g_com_arduino_assign_pin_output_high(u8g);
  258. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  259. /* Arduino Due specific code */
  260. /* enable PIOA and SPI0 */
  261. REG_PMC_PCER0 = (1UL << ID_PIOA) | (1UL << ID_SPI0);
  262. /* disable PIO on A26 and A27 */
  263. REG_PIOA_PDR = 0x0c000000;
  264. /* reset SPI0 (from sam lib) */
  265. SPI0->SPI_CR = SPI_CR_SPIDIS;
  266. SPI0->SPI_CR = SPI_CR_SWRST;
  267. SPI0->SPI_CR = SPI_CR_SWRST;
  268. SPI0->SPI_CR = SPI_CR_SPIEN;
  269. u8g_MicroDelay();
  270. /* master mode, no fault detection, chip select 0 */
  271. SPI0->SPI_MR = SPI_MR_MSTR | SPI_MR_PCSDEC | SPI_MR_MODFDIS;
  272. /* Polarity, Phase, 8 Bit data transfer, baud rate */
  273. /* x * 1000 / 84 --> clock cycle in ns
  274. 5 * 1000 / 84 = 58 ns
  275. SCBR > 50 *84 / 1000 --> SCBR=5
  276. SCBR > 300*84 / 1000 --> SCBR=26
  277. SCBR > 400*84 / 1000 --> SCBR=34
  278. */
  279. if ( arg_val <= U8G_SPI_CLK_CYCLE_50NS )
  280. {
  281. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(5) | 1;
  282. }
  283. else if ( arg_val <= U8G_SPI_CLK_CYCLE_300NS )
  284. {
  285. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(26) | 1;
  286. }
  287. else if ( arg_val <= U8G_SPI_CLK_CYCLE_400NS )
  288. {
  289. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(34) | 1;
  290. }
  291. else
  292. {
  293. SPI0->SPI_CSR[0] = SPI_CSR_SCBR(84) | 1;
  294. }
  295. u8g_MicroDelay();
  296. break;
  297. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  298. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  299. u8g_MicroDelay();
  300. break;
  301. case U8G_COM_MSG_CHIP_SELECT:
  302. if ( arg_val == 0 )
  303. {
  304. /* disable */
  305. u8g_MicroDelay(); /* this delay is required to avoid that the display is switched off too early --> DOGS102 with DUE */
  306. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  307. u8g_MicroDelay();
  308. }
  309. else
  310. {
  311. /* enable */
  312. //u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  313. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  314. u8g_MicroDelay();
  315. }
  316. break;
  317. case U8G_COM_MSG_RESET:
  318. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  319. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  320. break;
  321. case U8G_COM_MSG_WRITE_BYTE:
  322. u8g_spi_out(arg_val);
  323. u8g_MicroDelay();
  324. break;
  325. case U8G_COM_MSG_WRITE_SEQ:
  326. {
  327. register uint8_t *ptr = arg_ptr;
  328. while( arg_val > 0 )
  329. {
  330. u8g_spi_out(*ptr++);
  331. arg_val--;
  332. }
  333. }
  334. break;
  335. case U8G_COM_MSG_WRITE_SEQ_P:
  336. {
  337. register uint8_t *ptr = arg_ptr;
  338. while( arg_val > 0 )
  339. {
  340. u8g_spi_out(u8g_pgm_read(ptr));
  341. ptr++;
  342. arg_val--;
  343. }
  344. }
  345. break;
  346. }
  347. return 1;
  348. }
  349. #else /* U8G_ARDUINO_ATMEGA_HW_SPI */
  350. #endif /* U8G_ARDUINO_ATMEGA_HW_SPI */
  351. #else /* ARDUINO */
  352. uint8_t u8g_com_arduino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  353. {
  354. return 1;
  355. }
  356. #endif /* ARDUINO */