2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2018-02-15 04:47:04 +00:00
|
|
|
#ifndef MICROPY_INCLUDED_STM32_SPI_H
|
|
|
|
#define MICROPY_INCLUDED_STM32_SPI_H
|
2014-05-03 23:27:38 +01:00
|
|
|
|
stm32/spi: Add implementation of low-level SPI protocol.
Can be used, for example, to configure external SPI flash using a hardware
SPI interface (code to be put in a board's bdev.c file):
STATIC const spi_proto_cfg_t hard_spi_bus = {
.spi = &spi_obj[5],
.baudrate = 10000000,
.polarity = 0,
.phase = 0,
.bits = 8,
.firstbit = SPI_FIRSTBIT_MSB,
};
STATIC mp_spiflash_cache_t spi_bdev_cache;
const mp_spiflash_config_t spiflash_config = {
.bus_kind = MP_SPIFLASH_BUS_SPI,
.bus.u_spi.cs = pin_A0,
.bus.u_spi.data = (void*)&hard_spi_bus,
.bus.u_spi.proto = &spi_proto,
.cache = &spi_bdev_cache,
};
spi_bdev_t spi_bdev;
2018-08-14 13:05:20 +01:00
|
|
|
#include "drivers/bus/spi.h"
|
2018-02-05 02:44:31 +00:00
|
|
|
#include "dma.h"
|
|
|
|
|
|
|
|
typedef struct _spi_t {
|
|
|
|
SPI_HandleTypeDef *spi;
|
|
|
|
const dma_descr_t *tx_dma_descr;
|
|
|
|
const dma_descr_t *rx_dma_descr;
|
|
|
|
} spi_t;
|
|
|
|
|
stm32/spi: Add implementation of low-level SPI protocol.
Can be used, for example, to configure external SPI flash using a hardware
SPI interface (code to be put in a board's bdev.c file):
STATIC const spi_proto_cfg_t hard_spi_bus = {
.spi = &spi_obj[5],
.baudrate = 10000000,
.polarity = 0,
.phase = 0,
.bits = 8,
.firstbit = SPI_FIRSTBIT_MSB,
};
STATIC mp_spiflash_cache_t spi_bdev_cache;
const mp_spiflash_config_t spiflash_config = {
.bus_kind = MP_SPIFLASH_BUS_SPI,
.bus.u_spi.cs = pin_A0,
.bus.u_spi.data = (void*)&hard_spi_bus,
.bus.u_spi.proto = &spi_proto,
.cache = &spi_bdev_cache,
};
spi_bdev_t spi_bdev;
2018-08-14 13:05:20 +01:00
|
|
|
typedef struct _spi_proto_cfg_t {
|
|
|
|
const spi_t *spi;
|
|
|
|
uint32_t baudrate;
|
|
|
|
uint8_t polarity;
|
|
|
|
uint8_t phase;
|
|
|
|
uint8_t bits;
|
|
|
|
uint8_t firstbit;
|
|
|
|
} spi_proto_cfg_t;
|
|
|
|
|
2018-08-14 08:11:07 +01:00
|
|
|
typedef struct _pyb_spi_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
const spi_t *spi;
|
|
|
|
} pyb_spi_obj_t;
|
|
|
|
|
|
|
|
typedef struct _machine_hard_spi_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
const spi_t *spi;
|
|
|
|
} machine_hard_spi_obj_t;
|
|
|
|
|
2018-02-05 02:44:31 +00:00
|
|
|
extern const spi_t spi_obj[6];
|
|
|
|
|
stm32/spi: Add implementation of low-level SPI protocol.
Can be used, for example, to configure external SPI flash using a hardware
SPI interface (code to be put in a board's bdev.c file):
STATIC const spi_proto_cfg_t hard_spi_bus = {
.spi = &spi_obj[5],
.baudrate = 10000000,
.polarity = 0,
.phase = 0,
.bits = 8,
.firstbit = SPI_FIRSTBIT_MSB,
};
STATIC mp_spiflash_cache_t spi_bdev_cache;
const mp_spiflash_config_t spiflash_config = {
.bus_kind = MP_SPIFLASH_BUS_SPI,
.bus.u_spi.cs = pin_A0,
.bus.u_spi.data = (void*)&hard_spi_bus,
.bus.u_spi.proto = &spi_proto,
.cache = &spi_bdev_cache,
};
spi_bdev_t spi_bdev;
2018-08-14 13:05:20 +01:00
|
|
|
extern const mp_spi_proto_t spi_proto;
|
2014-04-20 00:16:30 +01:00
|
|
|
extern const mp_obj_type_t pyb_spi_type;
|
2022-10-06 18:48:07 +01:00
|
|
|
extern const mp_obj_type_t machine_spi_type;
|
2014-04-20 00:16:30 +01:00
|
|
|
|
2018-08-14 08:11:07 +01:00
|
|
|
// A transfer of "len" bytes should take len*8*1000/baudrate milliseconds.
|
|
|
|
// To simplify the calculation we assume the baudrate is never less than 8kHz
|
|
|
|
// and use that value for the baudrate in the formula, plus a small constant.
|
|
|
|
#define SPI_TRANSFER_TIMEOUT(len) ((len) + 100)
|
|
|
|
|
2014-04-20 00:16:30 +01:00
|
|
|
void spi_init0(void);
|
2023-03-09 00:30:37 +00:00
|
|
|
int spi_init(const spi_t *spi, bool enable_nss_pin);
|
2018-08-14 08:11:07 +01:00
|
|
|
void spi_deinit(const spi_t *spi_obj);
|
|
|
|
int spi_find_index(mp_obj_t id);
|
|
|
|
void spi_set_params(const spi_t *spi_obj, uint32_t prescale, int32_t baudrate,
|
|
|
|
int32_t polarity, int32_t phase, int32_t bits, int32_t firstbit);
|
|
|
|
void spi_transfer(const spi_t *self, size_t len, const uint8_t *src, uint8_t *dest, uint32_t timeout);
|
|
|
|
void spi_print(const mp_print_t *print, const spi_t *spi_obj, bool legacy);
|
2018-02-05 02:44:31 +00:00
|
|
|
const spi_t *spi_from_mp_obj(mp_obj_t o);
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 22:14:58 +01:00
|
|
|
|
2018-02-15 04:47:04 +00:00
|
|
|
#endif // MICROPY_INCLUDED_STM32_SPI_H
|