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-10-01     zhs          the first version which add from wch
9  */
10 
11 #include <board.h>
12 #include "drv_soft_spi.h"
13 
14 #ifdef BSP_USING_SOFT_SPI
15 
16 #define LOG_TAG             "drv.soft_spi"
17 #include <drv_log.h>
18 
19 static struct ch32_soft_spi_config soft_spi_config[] =
20 {
21 #ifdef BSP_USING_SOFT_SPI1
22     SOFT_SPI1_BUS_CONFIG,
23 #endif
24 #ifdef BSP_USING_SOFT_SPI2
25     SOFT_SPI2_BUS_CONFIG,
26 #endif
27 };
28 
29 static struct ch32_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])];
30 
31 /**
32   * Attach the spi device to soft SPI bus, this function must be used after initialization.
33   */
rt_hw_soft_spi_device_attach(const char * bus_name,const char * device_name,const char * pin_name)34 rt_err_t rt_hw_soft_spi_device_attach(const char *bus_name, const char *device_name, const char *pin_name)
35 {
36 
37     rt_err_t result;
38     struct rt_spi_device *spi_device;
39 
40     /* initialize the cs pin && select the slave*/
41     rt_base_t cs_pin = rt_pin_get(pin_name);
42     rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
43     rt_pin_write(cs_pin, PIN_HIGH);
44 
45     /* attach the device to soft spi bus*/
46     spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
47     RT_ASSERT(spi_device != RT_NULL);
48 
49     result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
50     return result;
51 }
52 
ch32_spi_gpio_init(struct ch32_soft_spi * spi)53 static void ch32_spi_gpio_init(struct ch32_soft_spi *spi)
54 {
55     struct ch32_soft_spi_config *cfg = (struct ch32_soft_spi_config *)spi->cfg;
56     rt_pin_mode(cfg->sck, PIN_MODE_OUTPUT);
57     rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
58     rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
59 
60     rt_pin_write(cfg->miso, PIN_HIGH);
61     rt_pin_write(cfg->sck, PIN_HIGH);
62     rt_pin_write(cfg->mosi, PIN_HIGH);
63 }
64 
ch32_tog_sclk(void * data)65 void ch32_tog_sclk(void *data)
66 {
67     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
68     if(rt_pin_read(cfg->sck) == PIN_HIGH)
69     {
70         rt_pin_write(cfg->sck, PIN_LOW);
71     }
72     else
73     {
74         rt_pin_write(cfg->sck, PIN_HIGH);
75     }
76 }
77 
ch32_set_sclk(void * data,rt_int32_t state)78 void ch32_set_sclk(void *data, rt_int32_t state)
79 {
80 
81     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
82     if (state)
83     {
84         rt_pin_write(cfg->sck, PIN_HIGH);
85     }
86     else
87     {
88         rt_pin_write(cfg->sck, PIN_LOW);
89     }
90 }
91 
ch32_set_mosi(void * data,rt_int32_t state)92 void ch32_set_mosi(void *data, rt_int32_t state)
93 {
94     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
95     if (state)
96     {
97         rt_pin_write(cfg->mosi, PIN_HIGH);
98     }
99     else
100     {
101         rt_pin_write(cfg->mosi, PIN_LOW);
102     }
103 }
104 
ch32_set_miso(void * data,rt_int32_t state)105 void ch32_set_miso(void *data, rt_int32_t state)
106 {
107     struct ch32_soft_spi_config* cfg = (struct ch3_soft_spi_config*)data;
108     if (state)
109     {
110         rt_pin_write(cfg->miso, PIN_HIGH);
111     }
112     else
113     {
114         rt_pin_write(cfg->miso, PIN_LOW);
115     }
116 }
117 
ch32_get_sclk(void * data)118 rt_int32_t ch32_get_sclk(void *data)
119 {
120     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
121     return rt_pin_read(cfg->sck);
122 }
123 
ch32_get_mosi(void * data)124 rt_int32_t ch32_get_mosi(void *data)
125 {
126     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
127     return rt_pin_read(cfg->mosi);
128 }
129 
ch32_get_miso(void * data)130 rt_int32_t ch32_get_miso(void *data)
131 {
132     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
133     return rt_pin_read(cfg->miso);
134 }
135 
ch32_dir_mosi(void * data,rt_int32_t state)136 void ch32_dir_mosi(void *data, rt_int32_t state)
137 {
138     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
139     if (state)
140     {
141         rt_pin_mode(cfg->mosi, PIN_MODE_INPUT);
142     }
143     else
144     {
145         rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
146     }
147 }
148 
ch32_dir_miso(void * data,rt_int32_t state)149 void ch32_dir_miso(void *data, rt_int32_t state)
150 {
151     struct ch32_soft_spi_config* cfg = (struct ch32_soft_spi_config*)data;
152     if (state)
153     {
154         rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
155     }
156     else
157     {
158         rt_pin_mode(cfg->miso, PIN_MODE_OUTPUT);
159     }
160 }
161 
ch32_udelay(rt_uint32_t us)162 static void ch32_udelay(rt_uint32_t us)
163 {
164     rt_uint32_t ticks;
165     rt_uint32_t told, tnow, tcnt = 0;
166     rt_uint32_t reload = SysTick->CMP;
167 
168     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
169     told = SysTick->CNT;
170     while (1)
171     {
172         tnow = SysTick->CNT;
173         if (tnow != told)
174         {
175             if (tnow > told)
176             {
177                 tcnt += tnow - told;
178             }
179             else
180             {
181                 tcnt += reload - told + tnow;
182             }
183             told = tnow;
184             if (tcnt >= ticks)
185             {
186                 break;
187             }
188         }
189     }
190 }
191 
ch32_pin_init(void)192 static void ch32_pin_init(void)
193 {
194     rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct ch32_soft_spi);
195 
196     for(rt_size_t i = 0; i < obj_num; i++)
197     {
198         ch32_spi_gpio_init(&spi_obj[i]);
199     }
200 }
201 
202 static struct rt_spi_bit_ops ch32_soft_spi_ops =
203 {
204     .data = RT_NULL,
205     .pin_init = ch32_pin_init,
206     .tog_sclk = ch32_tog_sclk,
207     .set_sclk = ch32_set_sclk,
208     .set_mosi = ch32_set_mosi,
209     .set_miso = ch32_set_miso,
210     .get_sclk = ch32_get_sclk,
211     .get_mosi = ch32_get_mosi,
212     .get_miso = ch32_get_miso,
213     .dir_mosi = ch32_dir_mosi,
214     .dir_miso = ch32_dir_miso,
215     .udelay = ch32_udelay,
216     .delay_us = 1,
217 };
218 
219 /* Soft SPI initialization function */
rt_soft_spi_init(void)220 int rt_soft_spi_init(void)
221 {
222     rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct ch32_soft_spi);
223     rt_err_t result;
224 
225     for (rt_size_t i = 0; i < obj_num; i++)
226     {
227         ch32_soft_spi_ops.data = (void *)&soft_spi_config[i];
228         spi_obj[i].spi.ops = &ch32_soft_spi_ops;
229         spi_obj[i].cfg = (void *)&soft_spi_config[i];
230         result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &ch32_soft_spi_ops);
231         RT_ASSERT(result == RT_EOK);
232     }
233 
234     return RT_EOK;
235 }
236 INIT_BOARD_EXPORT(rt_soft_spi_init);
237 
238 #endif /* BSP_USING_SOFT_SPI */
239