u8g_com_raspberrypi_hw_spi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. u8g_com_raspberrypi_hw_spi.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2012, 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. Assumes, that
  27. MOSI is at PORTB, Pin 3
  28. and
  29. SCK is at PORTB, Pin 5
  30. Update for ATOMIC operation done (01 Jun 2013)
  31. U8G_ATOMIC_OR(ptr, val)
  32. U8G_ATOMIC_AND(ptr, val)
  33. U8G_ATOMIC_START()
  34. U8G_ATOMIC_END()
  35. */
  36. #include "u8g.h"
  37. #if defined(U8G_RASPBERRY_PI)
  38. #include <wiringPiSPI.h>
  39. #include <wiringPi.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. uint8_t u8g_com_raspberrypi_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  44. {
  45. switch(msg)
  46. {
  47. case U8G_COM_MSG_STOP:
  48. break;
  49. case U8G_COM_MSG_INIT:
  50. // check wiringPi setup
  51. if (wiringPiSetup() == -1)
  52. {
  53. printf("wiringPi-Error\n");
  54. exit(1);
  55. }
  56. if (wiringPiSPISetup (0, 100000) < 0)
  57. {
  58. printf ("Unable to open SPI device 0: %s\n", strerror (errno)) ;
  59. exit (1) ;
  60. }
  61. u8g_SetPIOutput(u8g, U8G_PI_RESET);
  62. u8g_SetPIOutput(u8g, U8G_PI_A0);
  63. break;
  64. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  65. u8g_SetPILevel(u8g, U8G_PI_A0, arg_val);
  66. break;
  67. case U8G_COM_MSG_CHIP_SELECT:
  68. /* Done by the SPI hardware */
  69. break;
  70. case U8G_COM_MSG_RESET:
  71. u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
  72. break;
  73. case U8G_COM_MSG_WRITE_BYTE:
  74. wiringPiSPIDataRW (0, &arg_val, 1) ;
  75. break;
  76. case U8G_COM_MSG_WRITE_SEQ:
  77. wiringPiSPIDataRW (0, arg_ptr, arg_val);
  78. break;
  79. case U8G_COM_MSG_WRITE_SEQ_P:
  80. wiringPiSPIDataRW (0, arg_ptr, arg_val);
  81. break;
  82. }
  83. return 1;
  84. }
  85. #else
  86. uint8_t u8g_com_raspberrypi_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  87. {
  88. return 1;
  89. }
  90. #endif