u8g_state.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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__)
  57. #define U8G_ATMEGA_HW_SPI
  58. /* remove the definition for attiny */
  59. #if __AVR_ARCH__ == 2
  60. #undef U8G_ATMEGA_HW_SPI
  61. #endif
  62. #if __AVR_ARCH__ == 25
  63. #undef U8G_ATMEGA_HW_SPI
  64. #endif
  65. #endif
  66. #if defined(U8G_ATMEGA_HW_SPI)
  67. #include <avr/interrupt.h>
  68. static uint8_t u8g_state_avr_spi_memory[2];
  69. void u8g_backup_spi(uint8_t msg)
  70. {
  71. if ( U8G_STATE_MSG_IS_BACKUP(msg) )
  72. {
  73. u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)] = SPCR;
  74. }
  75. else
  76. {
  77. uint8_t tmp = SREG;
  78. cli();
  79. SPCR = 0;
  80. SPCR = u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)];
  81. SREG = tmp;
  82. }
  83. }
  84. #elif defined (U8G_RASPBERRY_PI)
  85. #include <stdio.h>
  86. void u8g_backup_spi(uint8_t msg) {
  87. printf("u8g_backup_spi %d\r\n",msg);
  88. }
  89. #elif defined(ARDUINO) && defined(__SAM3X8E__) // Arduino Due, maybe we should better check for __SAM3X8E__
  90. #include "sam.h"
  91. struct sam_backup_struct
  92. {
  93. uint32_t mr;
  94. uint32_t sr;
  95. uint32_t csr[4];
  96. } sam_backup[2];
  97. void u8g_backup_spi(uint8_t msg)
  98. {
  99. uint8_t idx = U8G_STATE_MSG_GET_IDX(msg);
  100. if ( U8G_STATE_MSG_IS_BACKUP(msg) )
  101. {
  102. sam_backup[idx].mr = SPI0->SPI_MR;
  103. sam_backup[idx].sr = SPI0->SPI_SR;
  104. sam_backup[idx].csr[0] = SPI0->SPI_CSR[0];
  105. sam_backup[idx].csr[1] = SPI0->SPI_CSR[1];
  106. sam_backup[idx].csr[2] = SPI0->SPI_CSR[2];
  107. sam_backup[idx].csr[3] = SPI0->SPI_CSR[3];
  108. }
  109. else
  110. {
  111. SPI0->SPI_MR = sam_backup[idx].mr;
  112. SPI0->SPI_CSR[0] = sam_backup[idx].csr[0];
  113. SPI0->SPI_CSR[1] = sam_backup[idx].csr[1];
  114. SPI0->SPI_CSR[2] = sam_backup[idx].csr[2];
  115. SPI0->SPI_CSR[3] = sam_backup[idx].csr[3];
  116. }
  117. }
  118. #else
  119. void u8g_backup_spi(uint8_t msg)
  120. {
  121. }
  122. #endif