spi.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. This file defines the interface to the lpc22xx SPI module.
  3. Copyright (C) 2006 Embedded Artists AB (www.embeddedartists.com)
  4. This file may be included in software not adhering to the GPL.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #ifndef SPI_H
  18. #define SPI_H
  19. #include <config.h>
  20. #include <common.h>
  21. #include <asm/errno.h>
  22. #include <asm/arch/hardware.h>
  23. #define SPIF 0x80
  24. #define spi_lock() disable_interrupts();
  25. #define spi_unlock() enable_interrupts();
  26. extern unsigned long spi_flags;
  27. extern unsigned char spi_idle;
  28. int spi_init(void);
  29. static inline unsigned char spi_read(void)
  30. {
  31. unsigned char b;
  32. PUT8(S0SPDR, spi_idle);
  33. while (!(GET8(S0SPSR) & SPIF));
  34. b = GET8(S0SPDR);
  35. return b;
  36. }
  37. static inline void spi_write(unsigned char b)
  38. {
  39. PUT8(S0SPDR, b);
  40. while (!(GET8(S0SPSR) & SPIF));
  41. GET8(S0SPDR); /* this will clear the SPIF bit */
  42. }
  43. static inline void spi_set_clock(unsigned char clk_value)
  44. {
  45. PUT8(S0SPCCR, clk_value);
  46. }
  47. static inline void spi_set_cfg(unsigned char phase,
  48. unsigned char polarity,
  49. unsigned char lsbf)
  50. {
  51. unsigned char v = 0x20; /* master bit set */
  52. if (phase)
  53. v |= 0x08; /* set phase bit */
  54. if (polarity) {
  55. v |= 0x10; /* set polarity bit */
  56. spi_idle = 0xFF;
  57. } else {
  58. spi_idle = 0x00;
  59. }
  60. if (lsbf)
  61. v |= 0x40; /* set lsbf bit */
  62. PUT8(S0SPCR, v);
  63. }
  64. #endif /* SPI_H */