| 38 | |
| 39 | |
| 40 | static rt_err_t configure(struct rt_spi_device *device, |
| 41 | struct rt_spi_configuration *configuration) |
| 42 | { |
| 43 | struct rt_spi_bus *spi_bus = NULL; |
| 44 | struct ls1c_spi *ls1c_spi = NULL; |
| 45 | unsigned char SPIx = 0; |
| 46 | void *spi_base = NULL; |
| 47 | unsigned char cpol = 0; |
| 48 | unsigned char cpha = 0; |
| 49 | unsigned char val = 0; |
| 50 | |
| 51 | RT_ASSERT(NULL != device); |
| 52 | RT_ASSERT(NULL != configuration); |
| 53 | |
| 54 | spi_bus = device->bus; |
| 55 | ls1c_spi = (struct ls1c_spi *)spi_bus->parent.user_data; |
| 56 | SPIx = ls1c_spi->SPIx; |
| 57 | spi_base = ls1c_spi_get_base(SPIx); |
| 58 | |
| 59 | { |
| 60 | // 使能SPI控制器,master模式,关闭中断 |
| 61 | reg_write_8(0x53, spi_base + LS1C_SPI_SPCR_OFFSET); |
| 62 | |
| 63 | // 清空状态寄存器 |
| 64 | reg_write_8(0xc0, spi_base + LS1C_SPI_SPSR_OFFSET); |
| 65 | |
| 66 | // 1字节产生中断,采样(读)与发送(写)时机同时 |
| 67 | reg_write_8(0x03, spi_base + LS1C_SPI_SPER_OFFSET); |
| 68 | |
| 69 | // 关闭SPI flash |
| 70 | val = reg_read_8(spi_base + LS1C_SPI_SFC_PARAM_OFFSET); |
| 71 | val &= 0xfe; |
| 72 | reg_write_8(val, spi_base + LS1C_SPI_SFC_PARAM_OFFSET); |
| 73 | |
| 74 | // spi flash时序控制寄存器 |
| 75 | reg_write_8(0x05, spi_base + LS1C_SPI_SFC_TIMING_OFFSET); |
| 76 | } |
| 77 | |
| 78 | // baudrate |
| 79 | ls1c_spi_set_clock(spi_base, configuration->max_hz); |
| 80 | |
| 81 | // 设置通信模式(时钟极性和相位) |
| 82 | if (configuration->mode & RT_SPI_CPOL) // cpol |
| 83 | { |
| 84 | cpol = SPI_CPOL_1; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | cpol = SPI_CPOL_0; |
| 89 | } |
| 90 | if (configuration->mode & RT_SPI_CPHA) // cpha |
| 91 | { |
| 92 | cpha = SPI_CPHA_1; |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | cpha = SPI_CPHA_0; |
| 97 | } |
nothing calls this directly
no test coverage detected