spi.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file spi.h
  30. * @brief Contains all functions support for spi driver
  31. * @version 0.0
  32. * @date 18. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef _SPI_H_
  40. #define _SPI_H_
  41. #include "types.h"
  42. #include "gpio.h"
  43. #include "ap_cp.h"
  44. #include "gpio.h"
  45. #include "common.h"
  46. #define SPI_MASTER_MODE 1 //define master mode,1:master mode,0:salve mode
  47. #define SPI_USE_TIMEOUT 1
  48. #define SPI_OP_TIMEOUT 100 //100ms for an Byte operation
  49. #if(SPI_USE_TIMEOUT == 1)
  50. #define SPI_INIT_TOUT(to) int to = hal_systick()
  51. #define SPI_CHECK_TOUT(to, timeout,loginfo) {if(hal_ms_intv(to) > timeout){LOG(loginfo);return PPlus_ERR_TIMEOUT;}}
  52. #else
  53. #define SPI_INIT_TOUT(to)
  54. #define SPI_CHECK_TOUT(to, timeout) false
  55. #endif
  56. #define ENABLE_SPI Ssix->SSIEN = 1
  57. #define DISABLE_SPI Ssix->SSIEN = 0
  58. #define NUMBER_DATA_RX_FIFO Ssix->RXFLR
  59. #define NUMBER_DATA_TX_FIFO Ssix->TXFLR
  60. #define SPI_BUSY 0x1
  61. #define TX_FIFO_NOT_FULL 0x2
  62. #define TX_FIFO_EMPTY 0x4
  63. #define RX_FIFO_NOT_EMPTY 0x8
  64. typedef enum{
  65. SPI_MODE0=0, //SCPOL=0,SCPH=0
  66. SPI_MODE1, //SCPOL=0,SCPH=1
  67. SPI_MODE2, //SCPOL=1,SCPH=0
  68. SPI_MODE3, //SCPOL=1,SCPH=1
  69. }SPI_SCMOD_e;
  70. typedef enum{
  71. SPI_TRXD=0, //Transmit & Receive
  72. SPI_TXD, //Transmit Only
  73. SPI_RXD, //Receive Only
  74. SPI_EEPROM, //EEPROM Read
  75. }SPI_TMOD_e;
  76. typedef enum{
  77. SPI0=0, //use spi 0
  78. SPI1, //use spi 1
  79. }SPI_INDEX_e;
  80. typedef enum{
  81. SPI_TX_COMPLETED = 1,
  82. SPI_RX_COMPLETED,
  83. SPI_TX_REQ_S, //slave tx
  84. SPI_RX_DATA_S, //slave rx
  85. } SPI_EVT_e;
  86. typedef struct _spi_evt_t{
  87. uint8_t id;
  88. SPI_EVT_e evt;
  89. uint8_t* data;
  90. uint8_t len;
  91. }spi_evt_t;
  92. typedef void (*spi_hdl_t)(spi_evt_t* pevt);
  93. typedef struct _spi_Cfg_t{
  94. GPIO_Pin_e sclk_pin;
  95. GPIO_Pin_e ssn_pin;
  96. GPIO_Pin_e MOSI;
  97. GPIO_Pin_e MISO;
  98. uint32_t baudrate;
  99. SPI_TMOD_e spi_tmod;
  100. SPI_SCMOD_e spi_scmod;
  101. bool int_mode;
  102. bool force_cs;
  103. spi_hdl_t evt_handler;
  104. }spi_Cfg_t;
  105. typedef enum{
  106. TRANSMIT_FIFO_EMPTY = 0x01,
  107. TRANSMIT_FIFO_OVERFLOW = 0x02,
  108. RECEIVE_FIFO_UNDERFLOW = 0x04,
  109. RECEIVE_FIFO_OVERFLOW = 0x08,
  110. RECEIVE_FIFO_FULL = 0x10,
  111. }SPI_INT_STATUS_e;
  112. typedef struct _hal_spi_t{
  113. SPI_INDEX_e spi_index;
  114. }hal_spi_t;
  115. typedef struct{
  116. bool busy;
  117. uint16_t xmit_len;
  118. uint16_t buf_len; //tx buffer and rx buffer size should same
  119. uint8_t* tx_buf;
  120. uint8_t* rx_buf;
  121. uint16_t tx_offset;
  122. uint16_t rx_offset;
  123. }spi_xmit_t;
  124. void __attribute__((weak)) hal_SPI0_IRQHandler(void);
  125. void __attribute__((weak)) hal_SPI1_IRQHandler(void);
  126. int hal_spis_clear_rx(hal_spi_t* spi_ptr);
  127. uint32_t hal_spis_rx_len(hal_spi_t* spi_ptr);
  128. int hal_spis_read_rxn(hal_spi_t* spi_ptr, uint8_t* pbuf, uint16_t len);
  129. int hal_spi_bus_init(hal_spi_t* spi_ptr,spi_Cfg_t cfg);
  130. int hal_spis_bus_init(hal_spi_t* spi_ptr,spi_Cfg_t cfg);
  131. int hal_spi_bus_deinit(hal_spi_t* spi_ptr);
  132. int hal_spi_init(SPI_INDEX_e channel);
  133. int hal_spi_transmit(hal_spi_t* spi_ptr,uint8_t* tx_buf,uint8_t* rx_buf,uint16_t len);
  134. int hal_spi_set_tx_buffer(hal_spi_t* spi_ptr,uint8_t* tx_buf,uint16_t len);
  135. int hal_spi_set_int_mode(hal_spi_t* spi_ptr,bool en);
  136. int hal_spi_set_force_cs(hal_spi_t* spi_ptr,bool en);
  137. bool hal_spi_get_transmit_bus_state(hal_spi_t* spi_ptr);
  138. int hal_spi_TxComplete(hal_spi_t* spi_ptr);
  139. int hal_spi_send_byte(hal_spi_t* spi_ptr,uint8_t data);
  140. #endif