diff --git a/platform/mcu/STM32F4xx/drivers/SPI2.c b/platform/mcu/STM32F4xx/drivers/SPI2.c new file mode 100644 index 00000000..9deaf7ff --- /dev/null +++ b/platform/mcu/STM32F4xx/drivers/SPI2.c @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, see * + ***************************************************************************/ + +#include "SPI2.h" +#include +#include + +pthread_mutex_t mutex; + +void spi2_init() +{ + RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; + __DSB(); + + SPI2->CR1 = SPI_CR1_SSM /* Software managment of nCS */ + | SPI_CR1_SSI /* Force internal nCS */ + | SPI_CR1_BR_2 /* Fclock: 42MHz/32 = 1.3MHz */ + | SPI_CR1_MSTR /* Master mode */ + | SPI_CR1_SPE; /* Enable peripheral */ + + pthread_mutex_init(&mutex, NULL); +} + +void spi2_terminate() +{ + RCC->APB1ENR &= ~RCC_APB1ENR_SPI2EN; + __DSB(); + + pthread_mutex_destroy(&mutex); +} + +uint8_t spi2_sendRecv(const uint8_t val) +{ + SPI2->DR = val; + while((SPI2->SR & SPI_SR_RXNE) == 0) ; + return SPI2->DR; +} + +bool spi2_lockDevice() +{ + if(pthread_mutex_trylock(&mutex) == 0) + { + return true; + } + + return false; +} + +void spi2_lockDeviceBlocking() +{ + pthread_mutex_lock(&mutex); +} + +void spi2_releaseDevice() +{ + pthread_mutex_unlock(&mutex); +} diff --git a/platform/mcu/STM32F4xx/drivers/SPI2.h b/platform/mcu/STM32F4xx/drivers/SPI2.h new file mode 100644 index 00000000..2b2008f5 --- /dev/null +++ b/platform/mcu/STM32F4xx/drivers/SPI2.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, see * + ***************************************************************************/ + +#ifndef SPI2_H +#define SPI2_H + +#include +#include +#include + +/** + * Initialise SPI2 peripheral with a bus clock frequency of ~1.3MHz. + * NOTE: this driver does not configure the SPI GPIOs, which have to be put in + * alternate mode by application code. + */ +void spi2_init(); + +/** + * Shut down SPI2 peripheral. + * NOTE: is left to application code to change the operating mode of the SPI + * GPIOs + */ +void spi2_terminate(); + +/** + * Exchange one byte over the SPI bus. + * @param val: transmitted byte. + * @return incoming byte from slave device. + */ +uint8_t spi2_sendRecv(const uint8_t val); + +/** + * Acquire exclusive ownership on the SPI peripheral by locking an internal + * mutex. This function is nonblocking and returs true if mutex has been + * successfully locked by the caller. + * @return true if device has been locked. + */ +bool spi2_lockDevice(); + +/** + * Acquire exclusive ownership on the SPI peripheral by locking an internal + * mutex. In case mutex is already locked, this function blocks the execution + * flow until it becomes free again. + */ +void spi2_lockDeviceBlocking(); + +/** + * Release exclusive ownership on the SPI peripheral. + */ +void spi2_releaseDevice(); + +#endif /* SPI2_H */