Sensorian  1.0
C API Reference Guide Library
SPI.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
28 #include "SPI.h"
29 #include <stdio.h>
30 
35 void SPI_Initialize(void)
36 { if (!bcm2835_init())
37  {
38  printf("BCM libray error.\n"); //Should be run with the sudo cmd
39  }
40  bcm2835_spi_begin(); //Configure SPI pins
41  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //Configure bit order
42  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //Set clock polarity and phase CPOL=0, CPHA=0
43  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_8); //SPI baud rate at 244 Khz
44  bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); //Control CE0 in software
45  printf("SPI initialized...\n");
46 }
47 
53 unsigned char SPI_Write(unsigned char data)
54 {
55  return bcm2835_spi_transfer(data);
56 }
57 
62 unsigned char SPI_Read(void)
63 {
64  unsigned char data = bcm2835_spi_transfer(0xff);
65  return data;
66 }
67 
74 void SPI_Write_Array(char* buff, unsigned int length)
75 {
76  bcm2835_spi_writenb(buff,length);
77 }
78 
86 void SPI_Read_Array(char* sArray, char* rArray, char length)
87 {
88  bcm2835_spi_transfernb(sArray,rArray,length);
89 }
90 
95 void SPI_Close(void)
96 {
97  bcm2835_spi_end();
98 }
unsigned char SPI_Write(unsigned char data)
Writes a byte of data to the SPI bus.
Definition: SPI.c:53
unsigned char SPI_Read(void)
Reads a byte of data from the SPI bus.
Definition: SPI.c:62
void SPI_Initialize(void)
Initializes the SPI peripheral.
Definition: SPI.c:35
void SPI_Write_Array(char *buff, unsigned int length)
Writes an array of bytes to the SPI bus.
Definition: SPI.c:74
void SPI_Close(void)
Close SPI bus, should not be called if another SPI device is being used.
Definition: SPI.c:95
void SPI_Read_Array(char *sArray, char *rArray, char length)
Reads an array of data from the SPI bus.
Definition: SPI.c:86
SPI library header.