dev_hardware_SPI.h (3700B)
1 /***************************************************************************** 2 * | File : dev_hardware_SPI.h 3 * | Author : Waveshare team 4 * | Function : Read and write /dev/SPI, hardware SPI 5 * | Info : 6 *---------------- 7 * | This version: V1.0 8 * | Date : 2019-06-26 9 * | Info : Basic version 10 * 11 # 12 # Permission is hereby granted, free of charge, to any person obtaining a copy 13 # of this software and associated documnetation files (the "Software"), to deal 14 # in the Software without restriction, including without limitation the rights 15 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 # copies of the Software, and to permit persons to whom the Software is 17 # furished to do so, subject to the following conditions: 18 # 19 # The above copyright notice and this permission notice shall be included in 20 # all copies or substantial portions of the Software. 21 # 22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 # THE SOFTWARE. 29 # 30 ******************************************************************************/ 31 #ifndef __DEV_HARDWARE_SPI_ 32 #define __DEV_HARDWARE_SPI_ 33 34 #include <stdint.h> 35 36 #define DEV_HARDWARE_SPI_DEBUG 0 37 #if DEV_HARDWARE_SPI_DEBUG 38 #define DEV_HARDWARE_SPI_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__) 39 #else 40 #define DEV_HARDWARE_SPI_Debug(__info,...) 41 #endif 42 43 #define SPI_CPHA 0x01 44 #define SPI_CPOL 0x02 45 #define SPI_MODE_0 (0|0) 46 #define SPI_MODE_1 (0|SPI_CPHA) 47 #define SPI_MODE_2 (SPI_CPOL|0) 48 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 49 50 typedef enum{ 51 SPI_MODE0 = SPI_MODE_0, /*!< CPOL = 0, CPHA = 0 */ 52 SPI_MODE1 = SPI_MODE_1, /*!< CPOL = 0, CPHA = 1 */ 53 SPI_MODE2 = SPI_MODE_2, /*!< CPOL = 1, CPHA = 0 */ 54 SPI_MODE3 = SPI_MODE_3 /*!< CPOL = 1, CPHA = 1 */ 55 }SPIMode; 56 57 typedef enum{ 58 DISABLE = 0, 59 ENABLE = 1 60 }SPICSEN; 61 62 typedef enum{ 63 SPI_CS_Mode_LOW = 0, /*!< Chip Select 0 */ 64 SPI_CS_Mode_HIGH = 1, /*!< Chip Select 1 */ 65 SPI_CS_Mode_NONE = 3 /*!< No CS, control it yourself */ 66 }SPIChipSelect; 67 68 typedef enum 69 { 70 SPI_BIT_ORDER_LSBFIRST = 0, /*!< LSB First */ 71 SPI_BIT_ORDER_MSBFIRST = 1 /*!< MSB First */ 72 }SPIBitOrder; 73 74 typedef enum 75 { 76 SPI_3WIRE_Mode = 0, 77 SPI_4WIRE_Mode = 1 78 }BusMode; 79 80 81 /** 82 * Define SPI attribute 83 **/ 84 typedef struct SPIStruct { 85 //GPIO 86 uint16_t SCLK_PIN; 87 uint16_t MOSI_PIN; 88 uint16_t MISO_PIN; 89 90 uint16_t CS0_PIN; 91 uint16_t CS1_PIN; 92 93 94 uint32_t speed; 95 uint16_t mode; 96 uint16_t delay; 97 int fd; // 98 } HARDWARE_SPI; 99 100 101 102 103 void DEV_HARDWARE_SPI_begin(char *SPI_device); 104 void DEV_HARDWARE_SPI_beginSet(char *SPI_device, SPIMode mode, uint32_t speed); 105 void DEV_HARDWARE_SPI_end(void); 106 107 int DEV_HARDWARE_SPI_setSpeed(uint32_t speed); 108 109 uint8_t DEV_HARDWARE_SPI_TransferByte(uint8_t buf); 110 int DEV_HARDWARE_SPI_Transfer(uint8_t *buf, uint32_t len); 111 112 void DEV_HARDWARE_SPI_SetDataInterval(uint16_t us); 113 int DEV_HARDWARE_SPI_SetBusMode(BusMode mode); 114 int DEV_HARDWARE_SPI_SetBitOrder(SPIBitOrder Order); 115 int DEV_HARDWARE_SPI_ChipSelect(SPIChipSelect CS_Mode); 116 int DEV_HARDWARE_SPI_CSEN(SPICSEN EN); 117 int DEV_HARDWARE_SPI_Mode(SPIMode mode); 118 119 120 #endif