fw_spi.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ___FW_SPI_H___
  15. #define ___FW_SPI_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. /**
  19. * STC8H1K08(TSSOP20) STC8H3K32S2(TSSOP20)
  20. * #1 #4 #1 #3 #4
  21. * SPI SS -> 1 16 19 7 16(P35)
  22. * SPI MOSI -> 2 15 15(P34)
  23. * SPI MISO -> 3 14 14(P33)
  24. * SPI CLK -> 4 13 13(P32)
  25. *
  26. * The ports for STC8G1K08-8Pin, STC8G1K08A are different:
  27. * SS MO MI SCLK
  28. * 00 - P5.5 P5.4 P3.3 P3.2, 01/10/11 - n/a
  29. */
  30. typedef enum
  31. {
  32. // SS MOSI MISO SCLK
  33. SPI_AlterPort_P12P54_P13_P14_P15 = 0x00,
  34. SPI_AlterPort_P22_P23_P24_P25 = 0x01,
  35. SPI_AlterPort_P54_P40_P41_P43 = 0x02,
  36. SPI_AlterPort_P35_P34_P33_P32 = 0x03,
  37. SPI_AlterPort_8G1K08_8Pin = 0x00,
  38. } SPI_AlterPort_t;
  39. typedef enum
  40. {
  41. SPI_ClockPreScaler_4 = 0x00,
  42. SPI_ClockPreScaler_8 = 0x01,
  43. SPI_ClockPreScaler_16 = 0x02,
  44. SPI_ClockPreScaler_32or2 = 0x03,
  45. } SPI_ClockPreScaler_t;
  46. typedef enum
  47. {
  48. SPI_ClockPhase_LeadingEdge = 0x00, // Bits are sampled on the leading edge of SCLK
  49. SPI_ClockPhase_TrailingEdge = 0x01, // Bits are sampled on the trailing edge of SCLK
  50. } SPI_ClockPhase_t;
  51. typedef enum
  52. {
  53. SPI_DataOrder_MSB = 0x00, // High bits first
  54. SPI_DataOrder_LSB = 0x01, // Low bits first
  55. } SPI_DataOrder_t;
  56. #define SPI_RxTxFinished() (SPSTAT & 0x80)
  57. #define SPI_ClearInterrupt() SFR_SET(SPSTAT, 7)
  58. #define SPI_ClearWriteConflictInterrupt() SFR_SET(SPSTAT, 6)
  59. #define SPI_ClearInterrupts() (SPSTAT |= 0xC0)
  60. #define SPI_IgnoreSlaveSelect(__STATE__) SFR_ASSIGN(SPCTL, 7, __STATE__)
  61. #define SPI_SetEnabled(__STATE__) SFR_ASSIGN(SPCTL, 6, __STATE__)
  62. #define SPI_SetDataOrder(__ORDER__) SFR_ASSIGN(SPCTL, 5, __ORDER__)
  63. #define SPI_SetMasterMode(__STATE__) SFR_ASSIGN(SPCTL, 4, __STATE__)
  64. /**
  65. * Clock Polarity, CPOL
  66. * 0: clock line idles low
  67. * 1: clock line idles high
  68. */
  69. #define SPI_SetClockPolarity(__STATE__) SFR_ASSIGN(SPCTL, 3, __STATE__)
  70. /**
  71. * Clock Phase (CPHA)
  72. * 0: bits are sampled on the leading clock edge
  73. * 1: bits are sampled on the trailing clock edge
  74. */
  75. #define SPI_SetClockPhase(__PHASE__) SFR_ASSIGN(SPCTL, 2, __PHASE__)
  76. /**
  77. * SPI Clock
  78. */
  79. #define SPI_SetClockPrescaler(__PRE_SCALER__) (SPCTL = SPCTL & ~0x03 | ((__PRE_SCALER__) << 0))
  80. /**
  81. * Alternative ports
  82. */
  83. #define SPI_SetPort(__ALTER_PORT__) (P_SW1 = P_SW1 & ~(0x03 << 2) | ((__ALTER_PORT__) << 2))
  84. uint8_t SPI_TxRx(uint8_t dat);
  85. void SPI_TxRxBytes(uint8_t *pBuf, uint8_t len);
  86. #endif