Driver for STM32F4 SPI2, with locking mechanism for concurrent access
This commit is contained in:
parent
e157fc361d
commit
10e9ac9057
|
|
@ -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 <http://www.gnu.org/licenses/> *
|
||||
***************************************************************************/
|
||||
|
||||
#include "SPI2.h"
|
||||
#include <pthread.h>
|
||||
#include <stm32f4xx.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -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 <http://www.gnu.org/licenses/> *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SPI2_H
|
||||
#define SPI2_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
Loading…
Reference in New Issue