u8g_state.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. u8g_state.c
  3. backup and restore hardware state
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2011, 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. state callback: backup env U8G_STATE_MSG_BACKUP_ENV
  28. device callback: DEV_MSG_INIT
  29. state callback: backup u8g U8G_STATE_MSG_BACKUP_U8G
  30. state callback: restore env U8G_STATE_MSG_RESTORE_ENV
  31. state callback: backup env U8G_STATE_MSG_BACKUP_ENV
  32. state callback: retore u8g U8G_STATE_MSG_RESTORE_U8G
  33. DEV_MSG_PAGE_FIRST or DEV_MSG_PAGE_NEXT
  34. state callback: restore env U8G_STATE_MSG_RESTORE_ENV
  35. */
  36. #include <stddef.h>
  37. #include "u8g.h"
  38. void u8g_state_dummy_cb(uint8_t msg)
  39. {
  40. /* the dummy procedure does nothing */
  41. }
  42. void u8g_SetHardwareBackup(u8g_t *u8g, u8g_state_cb backup_cb)
  43. {
  44. u8g->state_cb = backup_cb;
  45. /* in most cases the init message was already sent, so this will backup the */
  46. /* current u8g state */
  47. backup_cb(U8G_STATE_MSG_BACKUP_U8G);
  48. }
  49. /*===============================================================*/
  50. /* register variable for restoring interrupt state */
  51. #if defined(__AVR__)
  52. uint8_t global_SREG_backup;
  53. #endif
  54. /*===============================================================*/
  55. /* AVR */
  56. #if defined(__AVR_XMEGA__)
  57. #elif defined(__AVR__)
  58. #define U8G_ATMEGA_HW_SPI
  59. /* remove the definition for attiny */
  60. #if __AVR_ARCH__ == 2
  61. #undef U8G_ATMEGA_HW_SPI
  62. #endif
  63. #if __AVR_ARCH__ == 25
  64. #undef U8G_ATMEGA_HW_SPI
  65. #endif
  66. #endif
  67. #if defined(U8G_ATMEGA_HW_SPI)
  68. #include <avr/interrupt.h>
  69. static uint8_t u8g_state_avr_spi_memory[2];
  70. void u8g_backup_spi(uint8_t msg)
  71. {
  72. if ( U8G_STATE_MSG_IS_BACKUP(msg) )
  73. {
  74. u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)] = SPCR;
  75. }
  76. else
  77. {
  78. uint8_t tmp = SREG;
  79. cli();
  80. SPCR = 0;
  81. SPCR = u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)];
  82. SREG = tmp;
  83. }
  84. }
  85. #elif defined (U8G_RASPBERRY_PI)
  86. #include <stdio.h>
  87. void u8g_backup_spi(uint8_t msg) {
  88. printf("u8g_backup_spi %d\r\n",msg);
  89. }
  90. #elif defined(ARDUINO) && defined(__SAM3X8E__) // Arduino Due, maybe we should better check for __SAM3X8E__
  91. #include "sam.h"
  92. struct sam_backup_struct
  93. {
  94. uint32_t mr;
  95. uint32_t sr;
  96. uint32_t csr[4];
  97. } sam_backup[2];
  98. void u8g_backup_spi(uint8_t msg)
  99. {
  100. uint8_t idx = U8G_STATE_MSG_GET_IDX(msg);
  101. if ( U8G_STATE_MSG_IS_BACKUP(msg) )
  102. {
  103. sam_backup[idx].mr = SPI0->SPI_MR;
  104. sam_backup[idx].sr = SPI0->SPI_SR;
  105. sam_backup[idx].csr[0] = SPI0->SPI_CSR[0];
  106. sam_backup[idx].csr[1] = SPI0->SPI_CSR[1];
  107. sam_backup[idx].csr[2] = SPI0->SPI_CSR[2];
  108. sam_backup[idx].csr[3] = SPI0->SPI_CSR[3];
  109. }
  110. else
  111. {
  112. SPI0->SPI_MR = sam_backup[idx].mr;
  113. SPI0->SPI_CSR[0] = sam_backup[idx].csr[0];
  114. SPI0->SPI_CSR[1] = sam_backup[idx].csr[1];
  115. SPI0->SPI_CSR[2] = sam_backup[idx].csr[2];
  116. SPI0->SPI_CSR[3] = sam_backup[idx].csr[3];
  117. }
  118. }
  119. #else
  120. void u8g_backup_spi(uint8_t msg)
  121. {
  122. }
  123. #endif