-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPI_Generic.h
80 lines (61 loc) · 2.57 KB
/
SPI_Generic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* SPI Generic
* Nathan Reichenberger
*
* Generic SPI class. Acts as slave and/or master
*
*/
#ifndef __SPI_GENERIC_H__
#define __SPI_GENERIC_H__
#include <Arduino.h>
#include "stdint.h"
/* Defines */
#ifndef CNT_OF_ARRAY
#define CNT_OF_ARRAY( x ) ( sizeof( x )/sizeof( x[0] ) )
#endif
/* Global Variables */
/* function types */
typedef void (*slave_send_data_func_t)( void * buffer, uint8_t buffer_len );
typedef void (*slave_recv_data_func_t)( void * buffer, uint8_t buffer_len );
/* Common Types */
class SPI_Generic_Master_t
{
public:
/* constructors */
SPI_Generic_Master_t( void );
~SPI_Generic_Master_t( void );
/* functions */
void begin( void );
void end( void );
bool transfer_buffer_r( uint8_t * buffer_in, uint8_t transfer_len );
bool transfer_buffer_w( uint8_t * buffer_out, uint8_t transfer_len );
bool transfer_buffer_rw( uint8_t * buffer_in_out, uint8_t transfer_len );
private:
/* functions */
/* friend functions */
/* variables */
};
class SPI_Generic_Slave_t
{
public:
/* constructors */
SPI_Generic_Slave_t( uint8_t data_len, uint8_t ss_int_pin, slave_send_data_func_t send_data_func, slave_recv_data_func_t recv_data_func );
~SPI_Generic_Slave_t( void );
/* functions */
private:
/* functions */
void settup_interrupts( void ); /* Configure Interrupts */
void release_interrupts( void ); /* Release Interrupts */
/* freinds */
friend void spi_ss_irq( void ); /* Interrupt for Slave select pin */
friend void spi_data_transfer_irq( void ); /* Master sent a command to the slave */
/* variables */
uint8_t ss_pin; /* Interrupt pin used for Slave Select, Should be tied to SS -1 to disable */
char * buff; /* Buffer used to send and recieve data [Header][UserData]*/
uint8_t buff_len; /* Full length of the buffer */
uint8_t buff_send_index; /* Index used to step through buffer */
uint8_t buff_recv_index; /* Index used to step through buffer */
slave_send_data_func_t send_data_f; /* Callback used to get the data to be sent */
slave_recv_data_func_t recv_data_f; /* Callback used to provide the application with the received data ( Data in buffer when transaction finished ) */
};
#endif