/* Reflow Oven Controller
*
* Copyright (C) 2021  Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __SPI_H__
#define __SPI_H__

#include <stm32/stm32f4xx.h>
#include <stdbool.h>

enum stm_spi_prescaler {
	SPI_PRSC_DIV2 = 0,
	SPI_PRSC_DIV4 = 1,
	SPI_PRSC_DIV8 = 2,
	SPI_PRSC_DIV16 = 3,
	SPI_PRSC_DIV32 = 4,
	SPI_PRSC_DIV64 = 5,
	SPI_PRSC_DIV128 = 6,
	SPI_PRSC_DIV256 = 7,
};

struct stm_spi_settings {
	void (*cs_activate)(void);
	void (*cs_deactivate)(void);
	bool cpol;
	bool cpha;
	bool master;
	bool msb_first;
	enum stm_spi_prescaler prescaler;
};

struct stm_spi_dev {
	uint32_t magic;
	SPI_TypeDef *spi_regs;
	struct stm_spi_settings settings;
};

typedef void * stm_spi_handle;

stm_spi_handle spi_init(struct stm_spi_dev *spi_dev_struct, SPI_TypeDef *spi_regs, const struct stm_spi_settings *settings);

void spi_deinit(stm_spi_handle handle);

int spi_transfer(stm_spi_handle handle, const uint8_t *tx, uint8_t *rx, uint32_t count, bool handle_cs);

#endif /* __SPI_H__ */
