summaryrefslogtreecommitdiff
path: root/bsp/apollo2/board/spi.c
blob: f59610975fe8e865a5524259859211a690ca3b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-12-04     Haley        the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "am_mcu_apollo.h"
#include "spi.h"

/* SPI0 */
#define AM_SPI0_IOM_INST    0

#define SPI0_GPIO_SCK       5
#define SPI0_GPIO_CFG_SCK   AM_HAL_PIN_5_M0SCK
#define SPI0_GPIO_MISO      6
#define SPI0_GPIO_CFG_MISO  AM_HAL_PIN_6_M0MISO
#define SPI0_GPIO_MOSI      7
#define SPI0_GPIO_CFG_MOSI  AM_HAL_PIN_7_M0MOSI

/* SPI1 */
#define AM_SPI1_IOM_INST    1

static am_hal_iom_config_t g_sIOMConfig =
{
    AM_HAL_IOM_SPIMODE, // ui32InterfaceMode
    AM_HAL_IOM_400KHZ, // ui32ClockFrequency
    0, // bSPHA
    0, // bSPOL
    80, // ui8WriteThreshold
    80, // ui8ReadThreshold
};

/* AM spi driver */
struct am_spi_bus
{
    struct rt_spi_bus parent;
    rt_uint32_t u32Module;
};

//connect am drv to rt drv.
static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration)
{
    struct am_spi_bus * am_spi_bus = (struct am_spi_bus *)device->bus;
    rt_uint32_t max_hz = configuration->max_hz;

    if(max_hz >= 24000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_24MHZ;
    }
    else if(max_hz >= 16000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_16MHZ;
    }
    else if(max_hz >= 12000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_12MHZ;
    }
    else if(max_hz >= 8000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_8MHZ;
    }
    else if(max_hz >= 6000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_6MHZ;
    }
    else if(max_hz >= 4000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_4MHZ;
    }
    else if(max_hz >= 3000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_3MHZ;
    }
    else if(max_hz >= 2000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_2MHZ;
    }
    else if(max_hz >= 1500000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_1_5MHZ;
    }
    else if(max_hz >= 1000000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_1MHZ;
    }
    else if(max_hz >= 750000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_750KHZ;
    }
    else if(max_hz >= 500000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_500KHZ;
    }
    else if(max_hz >= 400000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_400KHZ;
    }
    else if(max_hz >= 375000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_375KHZ;
    }
    else if(max_hz >= 250000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_250KHZ;
    }
    else if(max_hz >= 100000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_100KHZ;
    }
    else if(max_hz >= 50000)
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_50KHZ;
    }
    else
    {
        g_sIOMConfig.ui32ClockFrequency = AM_HAL_IOM_10KHZ;
    }

    /* CPOL */
    if(configuration->mode & RT_SPI_CPOL)
    {
        g_sIOMConfig.bSPOL = 1;
    }
    else
    {
        g_sIOMConfig.bSPOL = 0;
    }

    /* CPHA */
    if(configuration->mode & RT_SPI_CPHA)
    {
        g_sIOMConfig.bSPHA= 1;
    }
    else
    {
        g_sIOMConfig.bSPHA= 0;
    }

    /* init SPI */
    am_hal_iom_disable(am_spi_bus->u32Module);
    am_hal_iom_config(am_spi_bus->u32Module, &g_sIOMConfig);
    am_hal_iom_enable(am_spi_bus->u32Module);

    return RT_EOK;
};

static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message* message)
{
    struct am_spi_bus * am_spi_bus = (struct am_spi_bus *)device->bus;
    //struct rt_spi_configuration * config = &device->config;
    struct am_spi_cs * am_spi_cs = device->parent.user_data;
    rt_uint32_t * send_ptr = (rt_uint32_t *)message->send_buf;
    rt_uint32_t * recv_ptr = message->recv_buf;
    rt_uint32_t u32BytesRemaining = message->length;
    rt_uint32_t u32TransferSize = 0;

    /* take CS */
    if (message->cs_take)
    {
        am_hal_gpio_out_bit_clear(am_spi_cs->chip_select);
    }

    // 读数据
    if (recv_ptr != RT_NULL)
    {
        while (u32BytesRemaining)
        {
            /* Set the transfer size to either 64, or the number of remaining
               bytes, whichever is smaller */
            if (u32BytesRemaining > 64)
            {
                u32TransferSize = 64;
                am_hal_gpio_pin_config(SPI0_GPIO_MOSI, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_PULL6K);
                am_hal_gpio_out_bit_set(SPI0_GPIO_MOSI);
                am_hal_iom_spi_read(am_spi_bus->u32Module, am_spi_cs->chip_select,
                                   (uint32_t *)recv_ptr, u32TransferSize, AM_HAL_IOM_RAW);
                am_hal_gpio_pin_config(SPI0_GPIO_MOSI, SPI0_GPIO_CFG_MOSI | AM_HAL_GPIO_PULL6K);
            }
            else
            {
                u32TransferSize = u32BytesRemaining;
                {
                    am_hal_gpio_pin_config(SPI0_GPIO_MOSI, AM_HAL_GPIO_OUTPUT | AM_HAL_GPIO_PULL6K);
                    am_hal_gpio_out_bit_set(SPI0_GPIO_MOSI);
                    am_hal_iom_spi_read(am_spi_bus->u32Module, am_spi_cs->chip_select,
                                   (uint32_t *)recv_ptr, u32TransferSize, AM_HAL_IOM_RAW);
                    am_hal_gpio_pin_config(SPI0_GPIO_MOSI, SPI0_GPIO_CFG_MOSI | AM_HAL_GPIO_PULL6K);
                }
            }

            u32BytesRemaining -= u32TransferSize;
            recv_ptr = (rt_uint32_t *)((rt_uint32_t)recv_ptr + u32TransferSize);
        }
    }

    // 写数据
    else
    {
        while (u32BytesRemaining)
        {
            /* Set the transfer size to either 32, or the number of remaining
               bytes, whichever is smaller */
            if (u32BytesRemaining > 64)
            {
                u32TransferSize = 64;
                am_hal_iom_spi_write(am_spi_bus->u32Module, am_spi_cs->chip_select,
                                    (uint32_t *)send_ptr, u32TransferSize, AM_HAL_IOM_RAW);

            }
            else
            {
                u32TransferSize = u32BytesRemaining;
                {
                    am_hal_iom_spi_write(am_spi_bus->u32Module, am_spi_cs->chip_select,
                              (uint32_t *)send_ptr, u32TransferSize, AM_HAL_IOM_RAW);
                }
            }

            u32BytesRemaining -= u32TransferSize;
            send_ptr = (rt_uint32_t *)((rt_uint32_t)send_ptr + u32TransferSize);
        }
    }

    /* release CS */
    if(message->cs_release)
    {
        am_hal_gpio_out_bit_set(am_spi_cs->chip_select);
    }

    return message->length;
}

static const struct rt_spi_ops am_spi_ops =
{
    configure,
    xfer
};

#ifdef RT_USING_SPI0
static struct am_spi_bus am_spi_bus_0 =
{
    {0},
    AM_SPI0_IOM_INST
};
#endif /* #ifdef RT_USING_SPI0 */

#ifdef RT_USING_SPI1
static struct am_spi_bus am_spi_bus_1 =
{
    {0},
    AM_SPI1_IOM_INST
};
#endif /* #ifdef RT_USING_SPI1 */

int yr_hw_spi_init(void)
{
    struct am_spi_bus* am_spi;

#ifdef RT_USING_SPI0
    /* init spi gpio */
    am_hal_gpio_pin_config(SPI0_GPIO_SCK, SPI0_GPIO_CFG_SCK);
    am_hal_gpio_pin_config(SPI0_GPIO_MISO, SPI0_GPIO_CFG_MISO | AM_HAL_GPIO_PULL6K);
    am_hal_gpio_pin_config(SPI0_GPIO_MOSI, SPI0_GPIO_CFG_MOSI | AM_HAL_GPIO_PULL6K);

    /* Initialize IOM 0 in SPI mode at 100KHz */
    am_hal_iom_pwrctrl_enable(AM_SPI0_IOM_INST);
    am_hal_iom_config(AM_SPI0_IOM_INST, &g_sIOMConfig);
    am_hal_iom_enable(AM_SPI0_IOM_INST);

    //init spi bus device
    am_spi = &am_spi_bus_0;
    rt_spi_bus_register(&am_spi->parent, "spi0", &am_spi_ops);
#endif

    //rt_kprintf("spi init!\n");

    return 0;
}
#ifdef RT_USING_COMPONENTS_INIT
INIT_BOARD_EXPORT(yr_hw_spi_init);
#endif

/*@}*/