Commit 715edc5e authored by TMRh20's avatar TMRh20

Add mutex locking to SPI functions for RPi

per #172 #173

- Add mutex locking to SPI for RPi interrupt usage
- Leave rfInterrupts() rfNoInterrupts() functions for testing purposes
parent a381194b
...@@ -168,3 +168,11 @@ int attachInterrupt (int pin, int mode, void (*function)(void)) ...@@ -168,3 +168,11 @@ int attachInterrupt (int pin, int mode, void (*function)(void))
return 0 ; return 0 ;
} }
void rfNoInterrupts(){
pthread_mutex_lock (&pinMutex) ;
}
void rfInterrupts(){
pthread_mutex_unlock (&pinMutex) ;
}
...@@ -52,6 +52,8 @@ extern int piHiPri (const int pri); ...@@ -52,6 +52,8 @@ extern int piHiPri (const int pri);
********************************************************************************* *********************************************************************************
*/ */
extern int attachInterrupt (int pin, int mode, void (*function)(void)); extern int attachInterrupt (int pin, int mode, void (*function)(void));
extern void rfNoInterrupts();
extern void rfInterrupts();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
\ No newline at end of file
...@@ -8,12 +8,13 @@ SPI::SPI() { ...@@ -8,12 +8,13 @@ SPI::SPI() {
void SPI::begin( int busNo ) { void SPI::begin( int busNo ) {
rfNoInterrupts();
if (!bcm2835_init()){ if (!bcm2835_init()){
return; return;
} }
bcm2835_spi_begin(); bcm2835_spi_begin();
rfInterrupts();
} }
...@@ -22,19 +23,27 @@ void SPI::end() { ...@@ -22,19 +23,27 @@ void SPI::end() {
} }
void SPI::setBitOrder(uint8_t bit_order) { void SPI::setBitOrder(uint8_t bit_order) {
rfNoInterrupts();
bcm2835_spi_setBitOrder(bit_order); bcm2835_spi_setBitOrder(bit_order);
rfInterrupts();
} }
void SPI::setDataMode(uint8_t data_mode) { void SPI::setDataMode(uint8_t data_mode) {
rfNoInterrupts();
bcm2835_spi_setDataMode(data_mode); bcm2835_spi_setDataMode(data_mode);
rfInterrupts();
} }
void SPI::setClockDivider(uint16_t spi_speed) { void SPI::setClockDivider(uint16_t spi_speed) {
rfNoInterrupts();
bcm2835_spi_setClockDivider(spi_speed); bcm2835_spi_setClockDivider(spi_speed);
rfInterrupts();
} }
void SPI::chipSelect(int csn_pin){ void SPI::chipSelect(int csn_pin){
rfNoInterrupts();
bcm2835_spi_chipSelect(csn_pin); bcm2835_spi_chipSelect(csn_pin);
rfInterrupts();
} }
SPI::~SPI() { SPI::~SPI() {
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <stdio.h> #include <stdio.h>
#include "bcm2835.h" #include "bcm2835.h"
#include "interrupt.h"
class SPI { class SPI {
public: public:
...@@ -34,16 +35,23 @@ public: ...@@ -34,16 +35,23 @@ public:
uint8_t SPI::transfer(uint8_t _data) { uint8_t SPI::transfer(uint8_t _data) {
return bcm2835_spi_transfer(_data); rfNoInterrupts();
uint8_t data = bcm2835_spi_transfer(_data);
rfInterrupts();
return data;
} }
void SPI::transfernb(char* tbuf, char* rbuf, uint32_t len){ void SPI::transfernb(char* tbuf, char* rbuf, uint32_t len){
rfNoInterrupts();
bcm2835_spi_transfernb( tbuf, rbuf, len); bcm2835_spi_transfernb( tbuf, rbuf, len);
rfInterrupts();
} }
void SPI::transfern(char* buf, uint32_t len) void SPI::transfern(char* buf, uint32_t len)
{ {
rfNoInterrupts();
transfernb(buf, buf, len); transfernb(buf, buf, len);
rfInterrupts();
} }
/** /**
* \endcond * \endcond
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment