1 /*
2  * Copyright (c) 2006-2024 RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-07-18     Rbb666       first version
9  * 2023-03-30     Rbb666       update spi driver
10  * 2025-04-27     Hydevcode    update spi driver
11  */
12 
13 #include <drv_spi.h>
14 
15 #ifdef RT_USING_SPI
16 
17 /*#define DRV_DEBUG*/
18 #define DBG_TAG              "drv.spi"
19 #ifdef DRV_DEBUG
20     #define DBG_LVL               DBG_LOG
21 #else
22     #define DBG_LVL               DBG_INFO
23 #endif /* DRV_DEBUG */
24 #include <rtdbg.h>
25 
26 #ifdef BSP_USING_SPI0
27     static struct rt_spi_bus spi_bus0;
28 #endif
29 #ifdef BSP_USING_SPI3
30     static struct rt_spi_bus spi_bus3;
31 #endif
32 #ifdef BSP_USING_SPI5
33     static struct rt_spi_bus spi_bus5;
34 #endif
35 #ifdef BSP_USING_SPI6
36     static struct rt_spi_bus spi_bus6;
37 #endif
38 
39 static struct ifx_spi_handle spi_bus_obj[] =
40 {
41 #if defined(BSP_USING_SPI0)
42     {
43         .bus_name = "spi0",
44         .sck_pin = GET_PIN(0, 4),
45         .miso_pin = GET_PIN(0, 3),
46         .mosi_pin = GET_PIN(0, 2),
47     },
48 #endif
49 #if defined(BSP_USING_SPI3)
50     {
51         .bus_name = "spi3",
52         .sck_pin = GET_PIN(6, 2),
53         .miso_pin = GET_PIN(6, 1),
54         .mosi_pin = GET_PIN(6, 0),
55     },
56 #endif
57 #if defined(BSP_USING_SPI5)
58     {
59         .bus_name = "spi5",
60         .sck_pin = GET_PIN(7, 2),
61         .miso_pin = GET_PIN(7, 0),
62         .mosi_pin = GET_PIN(7, 1),
63     },
64 #endif
65 #if defined(BSP_USING_SPI6)
66     {
67         .bus_name = "spi6",
68         .sck_pin = GET_PIN(12, 2),
69         .miso_pin = GET_PIN(12, 1),
70         .mosi_pin = GET_PIN(12, 0),
71     },
72 #endif
73 };
74 
75 static struct ifx_spi spi_config[sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0])] =
76 {0};
77 
78 /* private rt-thread spi ops function */
79 static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
80 static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message);
81 
82 static struct rt_spi_ops ifx_spi_ops =
83 {
84     .configure = spi_configure,
85     .xfer = spixfer,
86 };
87 
spi_interrupt_callback(void * arg,cyhal_spi_event_t event)88 static void spi_interrupt_callback(void *arg, cyhal_spi_event_t event)
89 {
90     struct ifx_spi *spi_drv = (struct ifx_spi *)arg;
91 
92     rt_interrupt_enter();
93 
94     if ((event & CYHAL_SPI_IRQ_DONE) != 0u)
95     {
96         /* Transmission is complete. Handle Event */
97         rt_completion_done(&spi_drv->cpt);
98     }
99 
100     rt_interrupt_leave();
101 }
102 
ifx_spi_init(struct ifx_spi * spi_device)103 static void ifx_spi_init(struct ifx_spi *spi_device)
104 {
105     RT_ASSERT(spi_device != RT_NULL);
106 
107     rt_err_t result = RT_EOK;
108 
109     static uint8_t init_flag = 1;
110 
111     if (init_flag)
112     {
113         result = cyhal_spi_init(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->mosi_pin, spi_device->spi_handle_t->miso_pin,
114                                 spi_device->spi_handle_t->sck_pin, NC, NULL, spi_device->spi_handle_t->spi_obj->data_bits,
115                                 spi_device->spi_handle_t->spi_obj->mode, false);
116         if (result != RT_EOK)
117         {
118             LOG_E("spi%s init fail", spi_device->spi_handle_t->bus_name);
119             return;
120         }
121 
122         result = cyhal_spi_set_frequency(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->freq);
123         if (result == CYHAL_SPI_RSLT_CLOCK_ERROR)
124         {
125             LOG_E("%s set frequency fail", spi_device->spi_handle_t->bus_name);
126             return;
127         }
128         LOG_I("[%s] freq:[%d]HZ\n", spi_device->spi_handle_t->bus_name, spi_device->spi_handle_t->freq);
129 
130         /* Register a callback function to be called when the interrupt fires */
131         cyhal_spi_register_callback(spi_device->spi_handle_t->spi_obj, spi_interrupt_callback, spi_device);
132 
133         /* Enable the events that will trigger the call back function */
134         cyhal_spi_enable_event(spi_device->spi_handle_t->spi_obj, CYHAL_SPI_IRQ_DONE, 4, true);
135     }
136 
137     init_flag = 0;
138 }
139 
spi_configure(struct rt_spi_device * device,struct rt_spi_configuration * configuration)140 static rt_err_t spi_configure(struct rt_spi_device *device,
141                               struct rt_spi_configuration *configuration)
142 {
143     RT_ASSERT(device != RT_NULL);
144     RT_ASSERT(configuration != RT_NULL);
145 
146     struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);
147 
148     /* data_width */
149     if (configuration->data_width <= 8)
150     {
151         spi_device->spi_handle_t->spi_obj->data_bits = 8;
152     }
153     else if (configuration->data_width <= 16)
154     {
155         spi_device->spi_handle_t->spi_obj->data_bits = 16;
156     }
157     else
158     {
159         return -RT_EIO;
160     }
161 
162     uint32_t max_hz;
163     max_hz = configuration->max_hz;
164     spi_device->spi_handle_t->freq = max_hz;
165 
166     /* MSB or LSB */
167     switch (configuration->mode & RT_SPI_MODE_3)
168     {
169     case RT_SPI_MODE_0:
170         spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_00_MSB;
171         break;
172 
173     case RT_SPI_MODE_1:
174         spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_01_MSB;
175         break;
176 
177     case RT_SPI_MODE_2:
178         spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_10_MSB;
179         break;
180 
181     case RT_SPI_MODE_3:
182         spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_11_MSB;
183         break;
184     }
185 
186     ifx_spi_init(spi_device);
187 
188     return RT_EOK;
189 }
190 
spixfer(struct rt_spi_device * device,struct rt_spi_message * message)191 static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
192 {
193     RT_ASSERT(device != NULL);
194     RT_ASSERT(message != NULL);
195 
196     struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);
197 
198     /* take CS */
199     if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
200     {
201         if (device->config.mode & RT_SPI_CS_HIGH)
202         {
203             cyhal_gpio_write(device->cs_pin, PIN_HIGH);
204         }
205         else
206         {
207             cyhal_gpio_write(device->cs_pin, PIN_LOW);
208         }
209         LOG_D("spi take cs\n");
210     }
211 
212     int result = RT_EOK;
213 
214     if (message->length > 0)
215     {
216         if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
217         {
218             /**< receive message */
219             result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, RT_NULL, 0x00, message->recv_buf, message->length, 0x00);
220         }
221         else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
222         {
223             /**< send message */
224             result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, RT_NULL, 0x00, 0x00);
225         }
226         else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
227         {
228             /**< send and receive message */
229             result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, message->recv_buf, message->length, 0x00);
230         }
231 
232         /* blocking the thread,and the other tasks can run */
233         rt_completion_wait(&spi_device->cpt, RT_WAITING_FOREVER);
234     }
235 
236     if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
237     {
238         if (device->config.mode & RT_SPI_CS_HIGH)
239             cyhal_gpio_write(device->cs_pin, PIN_LOW);
240         else
241             cyhal_gpio_write(device->cs_pin, PIN_HIGH);
242     }
243 
244     return message->length;
245 }
246 
247 /**
248   * Attach the spi device to SPI bus, this function must be used after initialization.
249   */
rt_hw_spi_device_attach(const char * bus_name,const char * device_name,rt_base_t cs_pin)250 rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
251 {
252     RT_ASSERT(bus_name != RT_NULL);
253     RT_ASSERT(device_name != RT_NULL);
254 
255     rt_err_t result;
256     struct rt_spi_device *spi_device;
257 
258     /* attach the device to spi bus*/
259     spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
260     RT_ASSERT(spi_device != RT_NULL);
261 
262     result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
263     if (result != RT_EOK)
264     {
265         LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
266     }
267 
268     RT_ASSERT(result == RT_EOK);
269 
270     LOG_D("%s attach to %s done", device_name, bus_name);
271 
272     return result;
273 }
274 
rt_hw_spi_init(void)275 int rt_hw_spi_init(void)
276 {
277     int result = RT_EOK;
278 
279     for (int spi_index = 0; spi_index < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); spi_index++)
280     {
281         spi_bus_obj[spi_index].spi_obj = rt_malloc(sizeof(cyhal_spi_t));
282         RT_ASSERT(spi_bus_obj[spi_index].spi_obj != RT_NULL);
283 
284         spi_config[spi_index].spi_handle_t = &spi_bus_obj[spi_index];
285 
286         rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].spi_bus, spi_bus_obj[spi_index].bus_name, &ifx_spi_ops);
287         if (RT_EOK != err)
288         {
289             LOG_E("%s bus register failed.", spi_config[spi_index].spi_handle_t->bus_name);
290             return -RT_ERROR;
291         }
292 
293         LOG_D("MOSI PIN:[%d], MISO PIN[%d], CLK PIN[%d]\n",
294               spi_bus_obj[spi_index].mosi_pin, spi_bus_obj[spi_index].miso_pin,
295               spi_bus_obj[spi_index].sck_pin);
296 
297         /* initialize completion object */
298         rt_completion_init(&spi_config[spi_index].cpt);
299     }
300 
301     return result;
302 }
303 INIT_BOARD_EXPORT(rt_hw_spi_init);
304 #endif /* RT_USING_SPI */
305