1 /* 2 * Copyright (c) 2006-2023, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2022-11-26 zhaohaisheng copy from sch and do some change 9 */ 10 11 #ifndef __DRV_SPI_H__ 12 #define __DRV_SPI_H__ 13 14 #include <rtthread.h> 15 #include <rtdevice.h> 16 #include <rthw.h> 17 18 #include "ch32v30x_rcc.h" 19 #include "ch32v30x_gpio.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif /* ifdef __cplusplus */ 24 25 struct ch32_hw_spi_cs 26 { 27 GPIO_TypeDef* GPIOx; 28 rt_uint16_t GPIO_Pin; 29 }; 30 31 struct ch32_spi_config 32 { 33 SPI_TypeDef *Instance; 34 char *bus_name; 35 IRQn_Type irq_type; 36 37 }; 38 39 struct ch32_spi_device 40 { 41 rt_uint32_t pin; 42 char *bus_name; 43 char *device_name; 44 }; 45 46 typedef struct __SPI_HandleTypeDef 47 { 48 49 SPI_TypeDef *Instance; /*!< SPI registers base address */ 50 51 SPI_InitTypeDef Init; /*!< SPI communication parameters */ 52 53 rt_uint8_t *pTxBuffPtr; /*!< Pointer to SPI Tx transfer Buffer */ 54 55 rt_uint16_t TxXferSize; /*!< SPI Tx Transfer size */ 56 57 volatile rt_uint16_t TxXferCount; /*!< SPI Tx Transfer Counter */ 58 59 rt_uint8_t *pRxBuffPtr; /*!< Pointer to SPI Rx transfer Buffer */ 60 61 rt_uint16_t RxXferSize; /*!< SPI Rx Transfer size */ 62 63 volatile rt_uint16_t RxXferCount; /*!< SPI Rx Transfer Counter */ 64 65 66 } SPI_HandleTypeDef; 67 68 /* ch32 spi dirver class */ 69 struct ch32_spi 70 { 71 SPI_HandleTypeDef handle; 72 73 struct ch32_spi_config *config; 74 struct rt_spi_configuration *cfg; 75 struct rt_spi_bus spi_bus; 76 77 78 }; 79 80 rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef* cs_gpiox, rt_uint16_t cs_gpio_pin); 81 82 #ifdef __cplusplus 83 } 84 #endif /* ifdef __cplusplus */ 85 86 #endif /*__DRV_SPI_H__ */ 87