u8g_com_arduino_attiny85_hw_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. u8g_arduino_ATtiny85_std_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. */
  27. // Uses code from tinySPI Written by Nick Gammon
  28. // March 2013
  29. // ATMEL ATTINY45 / ARDUINO pin mappings
  30. //
  31. // +-\/-+
  32. // RESET Ain0 (D 5) PB5 1| |8 Vcc
  33. // CLK1 Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1 SCK / USCK / SCL
  34. // CLK0 Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1 MISO / DO
  35. // GND 4| |5 PB0 (D 0) pwm0 MOSI / DI / SDA
  36. // +----+
  37. #include "u8g.h"
  38. #if defined(ARDUINO) && defined(__AVR_ATtiny85__)
  39. #if ARDUINO < 100
  40. #include <WProgram.h>
  41. #else
  42. #include <Arduino.h>
  43. #endif
  44. const byte DI = 0; // D0, pin 5 Data In
  45. const byte DO = 1; // D1, pin 6 Data Out (this is *not* MOSI)
  46. const byte USCK = 2; // D2, pin 7 Universal Serial Interface clock
  47. uint8_t u8g_arduino_ATtiny85_spi_out(uint8_t val)
  48. {
  49. USIDR = val; // byte to output
  50. USISR = _BV (USIOIF); // clear Counter Overflow Interrupt Flag, set count to zero
  51. do
  52. {
  53. USICR = _BV (USIWM0) // 3-wire mode
  54. | _BV (USICS1) | _BV (USICLK) // Software clock strobe
  55. | _BV (USITC); // Toggle Clock Port Pin
  56. }
  57. while ((USISR & _BV (USIOIF)) == 0); // until Counter Overflow Interrupt Flag set
  58. return USIDR; // return read data
  59. }
  60. uint8_t u8g_com_arduino_ATtiny85_std_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  61. {
  62. switch(msg)
  63. {
  64. case U8G_COM_MSG_INIT:
  65. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH); // ensure SS stays high until needed
  66. pinMode (USCK, OUTPUT);
  67. pinMode (DO, OUTPUT);
  68. pinMode (u8g->pin_list[U8G_PI_CS], OUTPUT);
  69. pinMode (u8g->pin_list[U8G_PI_A0], OUTPUT);
  70. USICR = _BV (USIWM0); // 3-wire mode
  71. u8g_MicroDelay();
  72. break;
  73. case U8G_COM_MSG_STOP:
  74. break;
  75. case U8G_COM_MSG_RESET:
  76. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  77. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  78. break;
  79. case U8G_COM_MSG_CHIP_SELECT:
  80. if ( arg_val == 0 )
  81. {
  82. /* disable */
  83. u8g_MicroDelay();
  84. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  85. u8g_MicroDelay();
  86. }
  87. else
  88. {
  89. /* enable */
  90. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  91. u8g_MicroDelay();
  92. }
  93. break;
  94. case U8G_COM_MSG_WRITE_BYTE:
  95. u8g_arduino_ATtiny85_spi_out(arg_val);
  96. u8g_MicroDelay();
  97. break;
  98. case U8G_COM_MSG_WRITE_SEQ:
  99. {
  100. register uint8_t *ptr = arg_ptr;
  101. while( arg_val > 0 )
  102. {
  103. u8g_arduino_ATtiny85_spi_out(*ptr++);
  104. arg_val--;
  105. }
  106. }
  107. break;
  108. case U8G_COM_MSG_WRITE_SEQ_P:
  109. {
  110. register uint8_t *ptr = arg_ptr;
  111. while( arg_val > 0 )
  112. {
  113. u8g_arduino_ATtiny85_spi_out(u8g_pgm_read(ptr));
  114. ptr++;
  115. arg_val--;
  116. }
  117. }
  118. break;
  119. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  120. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  121. u8g_MicroDelay();
  122. break;
  123. }
  124. return 1;
  125. }
  126. #else /* ARDUINO */
  127. uint8_t u8g_com_arduino_ATtiny85_std_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  128. {
  129. return 1;
  130. }
  131. #endif /* ARDUINO */