Functions

# spi_lib.c File Reference

This file contains SPI lib. More...

`#include "config.h"`
`#include "spi_lib.h"`
Include dependency graph for spi_lib.c:

Go to the source code of this file.

## Functions
bit | spi_init_master (U8 mode, U8 rate, Bool b_msb_first)
| This function configures the SPI controller in master.
bit | spi_init_slave (U8 mode, U8 rate, Bool b_msb_first)
| This function configures the SPI controller in slave.
char | spi_putchar (char ch)
| This function sends a byte on the SPI.
bit | spi_test_hit (void)
| This function checks if a bytes has been received on the SPI.
char | spi_getchar (void)
| This function reads a byte on the SPI.
void | spi_transmit_master (char cData)
| SPI Make the transmission possible.
char | spi_rw (char tx)
| This function sends a byte on SPI interface.

## Detailed Description

This file contains SPI lib.

- Compiler: IAR EWAVR and GNU GCC for AVR
- Supported devices: ATmega32U4

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file spi_lib.c.

## Function Documentation

bit spi_init_master | ( | U8 | mode,
|  | U8 | rate,
|  | Bool | b_msb_first
| ) |  |

This function configures the SPI controller in master.

Parameters:

  mode | SPI_MODE_0 to SPI_MODE_3
  rate | SPI_RATE_0 to SPI_RATE_6
  b_msb_first | TRUE if the MSB of the data word is transmitted first

Returns:
  FALSE in case of error
  TRUE, initialization OK

Definition at line 62 of file spi_lib.c.

References Spi_enable, Spi_init_bus, Spi_select_master, Spi_set_lsbfirst, Spi_set_mode, Spi_set_msbfirst, Spi_set_rate, and TRUE.

    {
       Spi_select_master();
       Spi_set_mode(mode);
       Spi_set_rate(rate);
       if( b_msb_first )
       {
          Spi_set_msbfirst();
       }else{
          Spi_set_lsbfirst();
       }
       Spi_init_bus();
       Spi_enable();
       return TRUE;
    }

bit spi_init_slave | ( | U8 | mode,
|  | U8 | rate,
|  | Bool | b_msb_first
| ) |  |

This function configures the SPI controller in slave.

Parameters:

  mode | SPI_MODE_0 to SPI_MODE_3
  rate | SPI_RATE_0 to SPI_RATE_6
  b_msb_first | TRUE if the MSB of the data word is transmitted first

Returns:
  FALSE in case of error
  TRUE, initialization OK

Definition at line 88 of file spi_lib.c.

References Spi_enable, Spi_init_bus, Spi_select_slave, Spi_set_lsbfirst, Spi_set_mode, Spi_set_msbfirst, Spi_set_rate, and TRUE.

    {
       Spi_select_slave();
       Spi_set_mode(mode);
       Spi_set_rate(rate);
       if( b_msb_first )
       {
          Spi_set_msbfirst();
       }else{
          Spi_set_lsbfirst();
       }
       Spi_init_bus();
       Spi_enable();
       return TRUE;
    }

char spi_putchar | ( | char | ch ) |

This function sends a byte on the SPI.

Parameters:

  uc_wr_byte | character to send on the SPI

Returns:
  character sent

Definition at line 111 of file spi_lib.c.

References Spi_tx_ready, and Spi_write_data.

    {
       Spi_write_data(ch);
       while(!Spi_tx_ready());
       return ch;
    }

bit spi_test_hit | ( | void | ) |

This function checks if a bytes has been received on the SPI.

Returns:
  TRUE if byte received

Definition at line 123 of file spi_lib.c.

References Spi_rx_ready.

    {
       return Spi_rx_ready();
    }

char spi_getchar | ( | void | ) |

This function reads a byte on the SPI.

Returns:
  character read

Definition at line 133 of file spi_lib.c.

References Spi_read_data, and Spi_rx_ready.

    {
       register char c;
       while(!Spi_rx_ready());
       c = Spi_read_data();
       return c;
    }

void spi_transmit_master | ( | char | cData ) |

SPI Make the transmission possible.

Parameters:

  cData | the data byte to send

Definition at line 146 of file spi_lib.c.

References Spi_wait_eot, and Spi_write_data.

    {
       // Wait for transmission complete
       Spi_wait_eot();
       // Start new transmission
       Spi_write_data(cData);
    }

char spi_rw | ( | char | tx ) |

This function sends a byte on SPI interface.

Parameters:

  tx | the data byte to send

Returns:
  the received byte

Definition at line 161 of file spi_lib.c.

References Spi_read_data, and Spi_write_data.

    {
       Spi_write_data(tx);
       return Spi_read_data();
    }