sysfs_software_spi.c (6451B)
1 /***************************************************************************** 2 * | File : sysfs_software_spi.h 3 * | Author : Waveshare team 4 * | Function : Read and write /sys/class/gpio, software spi 5 * | Info : 6 *---------------- 7 * | This version: V1.0 8 * | Date : 2019-06-05 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 # Read_data OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 # THE SOFTWARE. 29 # 30 ******************************************************************************/ 31 #include "sysfs_software_spi.h" 32 33 SOFTWARE_SPI software_spi; 34 35 /****************************************************************************** 36 function: 37 parameter: 38 Info: 39 ******************************************************************************/ 40 void SYSFS_software_spi_begin(void) 41 { 42 // gpio 43 software_spi.SCLK_PIN = SPI0_SCK; 44 software_spi.MOSI_PIN = SPI0_MOSI; 45 software_spi.MISO_PIN = SPI0_MISO; 46 47 //software spi configure 48 software_spi.Mode = SOFTWARE_SPI_Mode0; 49 software_spi.Type = SOFTWARE_SPI_Master; 50 software_spi.Delay = SOFTWARE_SPI_CLOCK_DIV2; 51 software_spi.Order = SOFTWARE_SPI_MSBFIRST; // MSBFIRST 52 53 SYSFS_GPIO_Export(software_spi.SCLK_PIN); 54 SYSFS_GPIO_Export(software_spi.MOSI_PIN); 55 SYSFS_GPIO_Export(software_spi.MISO_PIN); 56 57 SYSFS_GPIO_Direction(software_spi.SCLK_PIN, OUT); 58 SYSFS_GPIO_Direction(software_spi.MOSI_PIN, OUT); 59 SYSFS_GPIO_Direction(software_spi.MISO_PIN, IN); 60 } 61 62 void SYSFS_software_spi_end(void) 63 { 64 SYSFS_GPIO_Write(software_spi.SCLK_PIN, LOW); 65 SYSFS_GPIO_Write(software_spi.MOSI_PIN, LOW); 66 67 SYSFS_GPIO_Unexport(software_spi.SCLK_PIN); 68 SYSFS_GPIO_Unexport(software_spi.MOSI_PIN); 69 } 70 71 void SYSFS_software_spi_setBitOrder(uint8_t order) 72 { 73 software_spi.Order = order & 1; 74 } 75 76 void SYSFS_software_spi_setDataMode(uint8_t mode) 77 { 78 if(mode > 4) { 79 SYSFS_SOFTWARE_SPI_Debug("MODE must be 0-3\r\n"); 80 return; 81 } 82 software_spi.Mode = mode; 83 84 switch (software_spi.Mode) { 85 case SOFTWARE_SPI_Mode0: 86 software_spi.CPOL = 0; 87 software_spi.CPHA = 0; 88 break; 89 case SOFTWARE_SPI_Mode1: 90 software_spi.CPOL = 0; 91 software_spi.CPHA = 1; 92 break; 93 case SOFTWARE_SPI_Mode2: 94 software_spi.CPOL = 1; 95 software_spi.CPHA = 0; 96 break; 97 case SOFTWARE_SPI_Mode3: 98 software_spi.CPOL = 1; 99 software_spi.CPHA = 1; 100 break; 101 } 102 } 103 104 void SYSFS_software_spi_setClockDivider(uint8_t div) 105 { 106 if(div > 8) { 107 SYSFS_SOFTWARE_SPI_Debug("div must be 0-7\r\n"); 108 return; 109 } 110 switch (div) { 111 case SOFTWARE_SPI_CLOCK_DIV2: 112 software_spi.Delay = 2; 113 break; 114 case SOFTWARE_SPI_CLOCK_DIV4: 115 software_spi.Delay = 4; 116 break; 117 case SOFTWARE_SPI_CLOCK_DIV8: 118 software_spi.Delay = 8; 119 break; 120 case SOFTWARE_SPI_CLOCK_DIV16: 121 software_spi.Delay = 16; 122 break; 123 case SOFTWARE_SPI_CLOCK_DIV32: 124 software_spi.Delay = 32; 125 break; 126 case SOFTWARE_SPI_CLOCK_DIV64: 127 software_spi.Delay = 64; 128 break; 129 case SOFTWARE_SPI_CLOCK_DIV128: 130 software_spi.Delay = 128; 131 break; 132 default: 133 software_spi.Delay = 128; 134 break; 135 } 136 } 137 138 /****************************************************************************** 139 function: SPI Mode 0 140 parameter: 141 Info: 142 ******************************************************************************/ 143 uint8_t SYSFS_software_spi_transfer(uint8_t value) 144 { 145 // printf("value = %d\r\n", value); 146 uint8_t Read_data; 147 if (software_spi.Order == SOFTWARE_SPI_LSBFIRST) { 148 uint8_t temp = 149 ((value & 0x01) << 7) | 150 ((value & 0x02) << 5) | 151 ((value & 0x04) << 3) | 152 ((value & 0x08) << 1) | 153 ((value & 0x10) >> 1) | 154 ((value & 0x20) >> 3) | 155 ((value & 0x40) >> 5) | 156 ((value & 0x80) >> 7); 157 value = temp; 158 } 159 160 uint8_t delay = software_spi.Delay >> 1; 161 for(int j=delay; j > 0; j--); 162 163 // printf("value = %d\r\n", value); 164 uint8_t Read_miso = 0; 165 166 SYSFS_GPIO_Write(software_spi.SCLK_PIN, 0); 167 for (uint8_t bit = 0; bit < 8; bit++) { 168 SYSFS_GPIO_Write(software_spi.SCLK_PIN, 0); 169 // for(int j=delay; j > 0; j--);// DELAY 170 171 if (software_spi.CPHA) { 172 Read_miso = SYSFS_GPIO_Read(software_spi.MISO_PIN); 173 if (software_spi.Order == SOFTWARE_SPI_LSBFIRST) { 174 Read_data <<= 1; 175 Read_data |= Read_miso; 176 } else { 177 Read_data >>= 1; 178 Read_data |= Read_miso << 7; 179 } 180 } else { 181 SYSFS_GPIO_Write(software_spi.MOSI_PIN, ((value<<bit) & 0x80) ? HIGH : LOW); 182 } 183 184 // for(int j=delay; j > 0; j--);// DELAY 185 SYSFS_GPIO_Write(software_spi.SCLK_PIN, 1); 186 // for(int j=delay; j > 0; j--);// DELAY 187 188 if (software_spi.CPHA) { 189 SYSFS_GPIO_Write(software_spi.MOSI_PIN, ((value<<bit) & 0x80) ? HIGH : LOW); 190 } else { 191 Read_miso = SYSFS_GPIO_Read(software_spi.MISO_PIN); 192 if (software_spi.Order == SOFTWARE_SPI_LSBFIRST) { 193 Read_data <<= 1; 194 Read_data |= Read_miso; 195 } else { 196 Read_data >>= 1; 197 Read_data |= Read_miso << 7; 198 } 199 } 200 201 // for(int j=delay; j > 0; j--);// DELAY 202 } 203 return Read_data; 204 }