u8g_com_arduino_hw_usart_spi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. u8g_com_arduino_hw_usart_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. */
  34. #include "u8g.h"
  35. #if defined(ARDUINO)
  36. #if defined(__AVR_ATmega32U4__ )
  37. #include <avr/interrupt.h>
  38. #include <avr/io.h>
  39. #if ARDUINO < 100
  40. #include <WProgram.h>
  41. #else
  42. #include <Arduino.h>
  43. #endif
  44. static uint8_t u8g_usart_spi_out(uint8_t data)
  45. {
  46. /* send data */
  47. UDR1 = data;
  48. /* wait for empty transmit buffer */
  49. while(!(UCSR1A & (1 << UDRE1)));
  50. return UDR1;
  51. }
  52. uint8_t u8g_com_arduino_hw_usart_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  53. {
  54. switch(msg)
  55. {
  56. case U8G_COM_MSG_STOP:
  57. break;
  58. case U8G_COM_MSG_INIT:
  59. /* SCK is already an output as we overwrite TXLED */
  60. u8g_com_arduino_assign_pin_output_high(u8g);
  61. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  62. // Init interface at 2MHz
  63. UBRR1 = 0x00;
  64. UCSR1C = (1 << UMSEL11) | (1 << UMSEL10);
  65. UCSR1B = (1 << TXEN1);
  66. UBRR1 = 3;
  67. break;
  68. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  69. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  70. break;
  71. case U8G_COM_MSG_CHIP_SELECT:
  72. if ( arg_val == 0 )
  73. {
  74. /* disable */
  75. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  76. }
  77. else
  78. {
  79. /* enable */
  80. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  81. }
  82. break;
  83. case U8G_COM_MSG_RESET:
  84. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  85. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  86. break;
  87. case U8G_COM_MSG_WRITE_BYTE:
  88. u8g_usart_spi_out(arg_val);
  89. break;
  90. case U8G_COM_MSG_WRITE_SEQ:
  91. {
  92. register uint8_t *ptr = arg_ptr;
  93. while( arg_val > 0 )
  94. {
  95. u8g_usart_spi_out(*ptr++);
  96. arg_val--;
  97. }
  98. }
  99. break;
  100. case U8G_COM_MSG_WRITE_SEQ_P:
  101. {
  102. register uint8_t *ptr = arg_ptr;
  103. while( arg_val > 0 )
  104. {
  105. u8g_usart_spi_out(u8g_pgm_read(ptr));
  106. ptr++;
  107. arg_val--;
  108. }
  109. }
  110. break;
  111. }
  112. return 1;
  113. }
  114. /* #elif defined(__18CXX) || defined(__PIC32MX) */
  115. /* #elif defined(__arm__) // Arduino Due, maybe we should better check for __SAM3X8E__ */
  116. #else /* __AVR_ATmega32U4__ */
  117. #endif /* __AVR_ATmega32U4__ */
  118. #else /* ARDUINO */
  119. uint8_t u8g_com_arduino_hw_usart_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  120. {
  121. return 1;
  122. }
  123. #endif /* ARDUINO */