LTlib LaurTec Library  4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
module_SPI.h File Reference
#include "LTlib.h"

Go to the source code of this file.

Macros

#define SPI_SS_ENABLED   0b00000100
 
#define SPI_SS_DISABLED   0b00000101
 
#define SPI1_SS_PIN   SPI_SS_DISABLED
 
#define SPI2_SS_PIN   SPI_SS_DISABLED
 
#define SPI_MASTER_DEVICE   0x01
 
#define SPI_SLAVE_DEVICE   0x02
 
#define SPI_MODE_0   0x00
 
#define SPI_MODE_1   0x01
 
#define SPI_MODE_2   0x02
 
#define SPI_MODE_3   0x03
 
#define SPI_TIMER_OUT_CLK   0b00000011
 
#define SPI_CLK_OSC_64   0b00000010
 
#define SPI_CLK_OSC_16   0b00000001
 
#define SPI_CLK_OSC_4   0b00000000
 
#define SPI1_check_data_ready()   (SSPSTATbits.BF)
 
#define SPI2_check_data_ready()   (SSP2STATbits.BF)
 
#define SPI1_check_bus_collision()   (SSPCON1bits.WCOL)
 
#define SPI2_check_bus_collision()   (SSP2CON1bits.WCOL)
 
#define SPI1_check_buffer_overflow()   (SSPCON1bits.SSPOV)
 
#define SPI2_check_buffer_overflow()   (SSP2CON1bits.SSPOV)
 
#define SPI1_is_TX_over()   (PIR1bits.SSPIF)
 
#define SPI2_is_TX_over()   (PIR3bits.SSP2IF)
 

Functions

void SPI1_open (unsigned char device_type)
 
void SPI2_open (unsigned char device_type)
 
void SPI1_close (void)
 
void SPI2_close (void)
 
void SPI1_baudrate (unsigned char baudrate)
 
void SPI2_baudrate (unsigned char baudrate)
 
void SPI1_mode (unsigned char communication_mode)
 
void SPI2_mode (unsigned char communication_mode)
 
unsigned char SPI1_write_byte (unsigned char byte_to_send)
 
unsigned char SPI2_write_byte (unsigned char byte_to_send)
 
unsigned char SPI1_read_byte (void)
 
unsigned char SPI2_read_byte (void)
 

Macro Definition Documentation

#define SPI1_check_buffer_overflow ( )    (SSPCON1bits.SSPOV)

This macro checks if any buffer overflow occured.

Parameters
void
Returns
Bit WCOL 0: No overflow. 1: Buffer Overflow dedected.

Definition at line 215 of file module_SPI.h.

#define SPI1_check_bus_collision ( )    (SSPCON1bits.WCOL)

This macro checks if any communication .

Parameters
void
Returns
Bit WCOL 0: No communication collision. 1: Communication collision dedected.

Definition at line 202 of file module_SPI.h.

#define SPI1_check_data_ready ( )    (SSPSTATbits.BF)

This macro checks if the data is ready. It can be used for TX mode only.

Parameters
void
Returns
Bit BF 0: Trasmission in progress 0: Trasmission completed (buffer full)

Definition at line 189 of file module_SPI.h.

#define SPI1_is_TX_over ( )    (PIR1bits.SSPIF)

This macro checks TX transition is over. It check the interrupt flag as reference.

Parameters
void
Returns
Bit Interrupt Flag 0: Communication in progress 1: Communication completed

Definition at line 228 of file module_SPI.h.

#define SPI1_SS_PIN   SPI_SS_DISABLED

Definition at line 64 of file module_SPI.h.

#define SPI2_check_buffer_overflow ( )    (SSP2CON1bits.SSPOV)

Definition at line 216 of file module_SPI.h.

#define SPI2_check_bus_collision ( )    (SSP2CON1bits.WCOL)

Definition at line 203 of file module_SPI.h.

#define SPI2_check_data_ready ( )    (SSP2STATbits.BF)

Definition at line 190 of file module_SPI.h.

#define SPI2_is_TX_over ( )    (PIR3bits.SSP2IF)

Definition at line 229 of file module_SPI.h.

#define SPI2_SS_PIN   SPI_SS_DISABLED

Definition at line 65 of file module_SPI.h.

#define SPI_CLK_OSC_16   0b00000001

Definition at line 82 of file module_SPI.h.

#define SPI_CLK_OSC_4   0b00000000

Definition at line 83 of file module_SPI.h.

#define SPI_CLK_OSC_64   0b00000010

Definition at line 81 of file module_SPI.h.

#define SPI_MASTER_DEVICE   0x01

Definition at line 72 of file module_SPI.h.

#define SPI_MODE_0   0x00

Definition at line 75 of file module_SPI.h.

#define SPI_MODE_1   0x01

Definition at line 76 of file module_SPI.h.

#define SPI_MODE_2   0x02

Definition at line 77 of file module_SPI.h.

#define SPI_MODE_3   0x03

Definition at line 78 of file module_SPI.h.

#define SPI_SLAVE_DEVICE   0x02

Definition at line 73 of file module_SPI.h.

#define SPI_SS_DISABLED   0b00000101

Definition at line 63 of file module_SPI.h.

#define SPI_SS_ENABLED   0b00000100

Definition at line 62 of file module_SPI.h.

#define SPI_TIMER_OUT_CLK   0b00000011

Definition at line 80 of file module_SPI.h.

Function Documentation

void SPI1_baudrate ( unsigned char  baudrate)

This function sets the communication baudrate. It should be called before SPI_open function.

Parameters
baudrateBaudrate [SPI_CLK_OSC_64, SPI_CLK_OSC_16, SPI_CLK_OSC_4, SPI_TIMER_OUT_CLK]
Returns
void
Warning
This function should be called before SPI_open and for Master device Only.

Definition at line 151 of file module_SPI.c.

void SPI1_close ( void  )

This function close the SPI module.

Parameters
void
Returns
void

Definition at line 134 of file module_SPI.c.

void SPI1_mode ( unsigned char  communication_mode)

This function sets the communication mode. It should be called before SPI_open function.

Parameters
communication_modeOperational mode [SPI_MODE_0, SPI_MODE_1, SPI_MODE_2, SPI_MODE_3]
Returns
void
Warning
This function should be called before SPI_open for both MAster and Slave devices.

Definition at line 168 of file module_SPI.c.

References SPI_MODE_0, SPI_MODE_1, SPI_MODE_2, and SPI_MODE_3.

void SPI1_open ( unsigned char  device_type)

This function opens the SPI module for communication. In particular it sets the device type either to Master or Slave.

Parameters
device_typeDevice Type [SPI_MASTER_DEVICE, SPI_SLAVE_DEVICE]
Returns
void
Warning
This function should be called only after having set the module with the right baudrate and mode.

Definition at line 52 of file module_SPI.c.

References PIN_AS_INPUT, PIN_AS_OUTPUT, SPI1_SS_PIN, SPI_MASTER_DEVICE, and SPI_SLAVE_DEVICE.

unsigned char SPI1_read_byte ( void  )

This function reads a single byte from the bus

Parameters
void
Returns
byte Value read from the input buffer.
Warning
The function is a bloching one. It waits for 8 bits to be sent out.

Definition at line 286 of file module_SPI.c.

References SPI1_is_TX_over.

unsigned char SPI1_write_byte ( unsigned char  byte_to_send)

This function writes a single byte on the bus.

Parameters
byte_to_sendByte to send [0..255]
Returns
error Communication error 0: The byte has been properly written -1: Communication error

Definition at line 228 of file module_SPI.c.

References SPI1_is_TX_over.

void SPI2_baudrate ( unsigned char  baudrate)
void SPI2_close ( void  )
void SPI2_mode ( unsigned char  communication_mode)
void SPI2_open ( unsigned char  device_type)
unsigned char SPI2_read_byte ( void  )
unsigned char SPI2_write_byte ( unsigned char  byte_to_send)