Skip to content

Commit

Permalink
added SPI driver
Browse files Browse the repository at this point in the history
  • Loading branch information
1brahimmohamed committed Nov 4, 2022
1 parent f639654 commit 70fc043
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 0 deletions.
115 changes: 115 additions & 0 deletions SPI/.cproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="de.innot.avreclipse.configuration.app.debug.566707699">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="de.innot.avreclipse.configuration.app.debug.566707699" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="de.innot.avreclipse.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=de.innot.avreclipse.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" description="" id="de.innot.avreclipse.configuration.app.debug.566707699" name="Debug" parent="de.innot.avreclipse.configuration.app.debug">
<folderInfo id="de.innot.avreclipse.configuration.app.debug.566707699." name="/" resourcePath="">
<toolChain id="de.innot.avreclipse.toolchain.winavr.app.debug.1438525170" name="AVR-GCC Toolchain" superClass="de.innot.avreclipse.toolchain.winavr.app.debug">
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.debug.798276505" name="Generate HEX file for Flash memory" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.debug.1473223599" name="Generate HEX file for EEPROM" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.debug.1590509243" name="Generate Extended Listing (Source + generated Assembler)" superClass="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.size.app.debug.1595054978" name="Print Size" superClass="de.innot.avreclipse.toolchain.options.toolchain.size.app.debug"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.debug.983461129" name="AVRDude" superClass="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.debug"/>
<targetPlatform id="de.innot.avreclipse.targetplatform.winavr.app.debug.367660973" name="AVR Cross-Target" superClass="de.innot.avreclipse.targetplatform.winavr.app.debug"/>
<builder buildPath="${workspace_loc:/SPI}/Debug" id="de.innot.avreclipse.target.builder.winavr.app.debug.1555420057" managedBuildOn="true" name="AVR GNU Make Builder.Debug" superClass="de.innot.avreclipse.target.builder.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.assembler.winavr.app.debug.2039558393" name="AVR Assembler" superClass="de.innot.avreclipse.tool.assembler.winavr.app.debug">
<option id="de.innot.avreclipse.assembler.option.debug.level.2076308231" superClass="de.innot.avreclipse.assembler.option.debug.level"/>
</tool>
<tool id="de.innot.avreclipse.tool.compiler.winavr.app.debug.1645439178" name="AVR Compiler" superClass="de.innot.avreclipse.tool.compiler.winavr.app.debug">
<option id="de.innot.avreclipse.compiler.option.debug.level.339703045" superClass="de.innot.avreclipse.compiler.option.debug.level"/>
<option id="de.innot.avreclipse.compiler.option.optimize.1354317821" superClass="de.innot.avreclipse.compiler.option.optimize"/>
</tool>
<tool id="de.innot.avreclipse.tool.cppcompiler.app.debug.1679923102" name="AVR C++ Compiler" superClass="de.innot.avreclipse.tool.cppcompiler.app.debug">
<option id="de.innot.avreclipse.cppcompiler.option.debug.level.1424436331" superClass="de.innot.avreclipse.cppcompiler.option.debug.level"/>
<option id="de.innot.avreclipse.cppcompiler.option.optimize.1316358032" superClass="de.innot.avreclipse.cppcompiler.option.optimize"/>
</tool>
<tool id="de.innot.avreclipse.tool.linker.winavr.app.debug.712978806" name="AVR C Linker" superClass="de.innot.avreclipse.tool.linker.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.cpplinker.app.debug.286955952" name="AVR C++ Linker" superClass="de.innot.avreclipse.tool.cpplinker.app.debug"/>
<tool id="de.innot.avreclipse.tool.archiver.winavr.base.1151230965" name="AVR Archiver" superClass="de.innot.avreclipse.tool.archiver.winavr.base"/>
<tool id="de.innot.avreclipse.tool.objdump.winavr.app.debug.545762386" name="AVR Create Extended Listing" superClass="de.innot.avreclipse.tool.objdump.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.objcopy.flash.winavr.app.debug.1712922577" name="AVR Create Flash image" superClass="de.innot.avreclipse.tool.objcopy.flash.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.debug.957309705" name="AVR Create EEPROM image" superClass="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.size.winavr.app.debug.58197367" name="Print Size" superClass="de.innot.avreclipse.tool.size.winavr.app.debug"/>
<tool id="de.innot.avreclipse.tool.avrdude.app.debug.296251861" name="AVRDude" superClass="de.innot.avreclipse.tool.avrdude.app.debug"/>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
<cconfiguration id="de.innot.avreclipse.configuration.app.release.291407534">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="de.innot.avreclipse.configuration.app.release.291407534" moduleId="org.eclipse.cdt.core.settings" name="Release">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="de.innot.avreclipse.buildArtefactType.app" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=de.innot.avreclipse.buildArtefactType.app,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" description="" id="de.innot.avreclipse.configuration.app.release.291407534" name="Release" parent="de.innot.avreclipse.configuration.app.release">
<folderInfo id="de.innot.avreclipse.configuration.app.release.291407534." name="/" resourcePath="">
<toolChain id="de.innot.avreclipse.toolchain.winavr.app.release.672667939" name="AVR-GCC Toolchain" superClass="de.innot.avreclipse.toolchain.winavr.app.release">
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release.570309470" name="Generate HEX file for Flash memory" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.flash.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release.1139741824" name="Generate HEX file for EEPROM" superClass="de.innot.avreclipse.toolchain.options.toolchain.objcopy.eeprom.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release.997462703" name="Generate Extended Listing (Source + generated Assembler)" superClass="de.innot.avreclipse.toolchain.options.toolchain.objdump.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.size.app.release.2118827835" name="Print Size" superClass="de.innot.avreclipse.toolchain.options.toolchain.size.app.release"/>
<option id="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release.657230952" name="AVRDude" superClass="de.innot.avreclipse.toolchain.options.toolchain.avrdude.app.release"/>
<targetPlatform id="de.innot.avreclipse.targetplatform.winavr.app.release.1892761489" name="AVR Cross-Target" superClass="de.innot.avreclipse.targetplatform.winavr.app.release"/>
<builder buildPath="${workspace_loc:/SPI}/Release" id="de.innot.avreclipse.target.builder.winavr.app.release.227203609" managedBuildOn="true" name="AVR GNU Make Builder.Release" superClass="de.innot.avreclipse.target.builder.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.assembler.winavr.app.release.55141916" name="AVR Assembler" superClass="de.innot.avreclipse.tool.assembler.winavr.app.release">
<option id="de.innot.avreclipse.assembler.option.debug.level.1429885572" superClass="de.innot.avreclipse.assembler.option.debug.level" value="de.innot.avreclipse.assembler.option.debug.level.none" valueType="enumerated"/>
<inputType id="de.innot.avreclipse.tool.assembler.input.176649827" superClass="de.innot.avreclipse.tool.assembler.input"/>
</tool>
<tool id="de.innot.avreclipse.tool.compiler.winavr.app.release.442141070" name="AVR Compiler" superClass="de.innot.avreclipse.tool.compiler.winavr.app.release">
<option id="de.innot.avreclipse.compiler.option.debug.level.1992900101" superClass="de.innot.avreclipse.compiler.option.debug.level" value="de.innot.avreclipse.compiler.option.debug.level.none" valueType="enumerated"/>
<option id="de.innot.avreclipse.compiler.option.optimize.315637616" superClass="de.innot.avreclipse.compiler.option.optimize" value="de.innot.avreclipse.compiler.optimize.size" valueType="enumerated"/>
<inputType id="de.innot.avreclipse.compiler.winavr.input.247670045" name="C Source Files" superClass="de.innot.avreclipse.compiler.winavr.input"/>
</tool>
<tool id="de.innot.avreclipse.tool.cppcompiler.app.release.2107583471" name="AVR C++ Compiler" superClass="de.innot.avreclipse.tool.cppcompiler.app.release">
<option id="de.innot.avreclipse.cppcompiler.option.debug.level.1415822359" superClass="de.innot.avreclipse.cppcompiler.option.debug.level" value="de.innot.avreclipse.cppcompiler.option.debug.level.none" valueType="enumerated"/>
<option id="de.innot.avreclipse.cppcompiler.option.optimize.649915337" superClass="de.innot.avreclipse.cppcompiler.option.optimize" value="de.innot.avreclipse.cppcompiler.optimize.size" valueType="enumerated"/>
</tool>
<tool id="de.innot.avreclipse.tool.linker.winavr.app.release.1718064280" name="AVR C Linker" superClass="de.innot.avreclipse.tool.linker.winavr.app.release">
<inputType id="de.innot.avreclipse.tool.linker.input.1785180922" name="OBJ Files" superClass="de.innot.avreclipse.tool.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
<tool id="de.innot.avreclipse.tool.cpplinker.app.release.2013452126" name="AVR C++ Linker" superClass="de.innot.avreclipse.tool.cpplinker.app.release"/>
<tool id="de.innot.avreclipse.tool.archiver.winavr.base.2026147507" name="AVR Archiver" superClass="de.innot.avreclipse.tool.archiver.winavr.base"/>
<tool id="de.innot.avreclipse.tool.objdump.winavr.app.release.636775976" name="AVR Create Extended Listing" superClass="de.innot.avreclipse.tool.objdump.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release.1373154016" name="AVR Create Flash image" superClass="de.innot.avreclipse.tool.objcopy.flash.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release.306238869" name="AVR Create EEPROM image" superClass="de.innot.avreclipse.tool.objcopy.eeprom.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.size.winavr.app.release.400419689" name="Print Size" superClass="de.innot.avreclipse.tool.size.winavr.app.release"/>
<tool id="de.innot.avreclipse.tool.avrdude.app.release.1069026028" name="AVRDude" superClass="de.innot.avreclipse.tool.avrdude.app.release"/>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="SPI.de.innot.avreclipse.project.winavr.elf_2.1.0.342383303" name="AVR Cross Target Application" projectType="de.innot.avreclipse.project.winavr.elf_2.1.0"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="de.innot.avreclipse.configuration.app.release.291407534;de.innot.avreclipse.configuration.app.release.291407534.;de.innot.avreclipse.tool.compiler.winavr.app.release.442141070;de.innot.avreclipse.compiler.winavr.input.247670045">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="de.innot.avreclipse.core.AVRGCCManagedMakePerProjectProfileC"/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
</cproject>
27 changes: 27 additions & 0 deletions SPI/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SPI</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>de.innot.avreclipse.core.avrnature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions SPI/.settings/de.innot.avreclipse.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
avrtarget/ClockFrequency=1000000
avrtarget/ExtRAMSize=0
avrtarget/ExtendedRAM=false
avrtarget/MCUType=atmega32
avrtarget/UseEEPROM=false
avrtarget/UseExtendedRAMforHeap=true
eclipse.preferences.version=1
2 changes: 2 additions & 0 deletions SPI/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
File renamed without changes.
File renamed without changes.
File renamed without changes.
144 changes: 144 additions & 0 deletions SPI/src/spi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/******************************************************************************
*
* Module: SPI
* File Name: spi.c
* Description: Source file for the SPI AVR driver
* Author: Ibrahim Mohamed
*
*******************************************************************************/

#include "spi.h"
#include "gpio.h" /* To Setup the SPI pins direction */
#include "common_macros.h" /* To use the macros like SET_BIT */
#include "avr/io.h" /* To use the SPI Registers */

/*******************************************************************************
* Functions Definitions *
*******************************************************************************/

/*
* Description :
* Initialize the SPI device as Master.
*/
void SPI_initMaster(void)
{
/******** Configure SPI Master Pins *********
* SS(PB4) --> Output
* MOSI(PB5) --> Output
* MISO(PB6) --> Input
* SCK(PB7) --> Output
********************************************/
GPIO_setupPinDirection(PORTB_ID,PIN4_ID,PIN_OUTPUT);
GPIO_setupPinDirection(PORTB_ID,PIN5_ID,PIN_OUTPUT);
GPIO_setupPinDirection(PORTB_ID,PIN6_ID,PIN_INPUT);
GPIO_setupPinDirection(PORTB_ID,PIN7_ID,PIN_OUTPUT);

/************************** SPCR Description **************************
* SPIE = 0 Disable SPI Interrupt
* SPE = 1 Enable SPI Driver
* DORD = 0 Transmit the MSB first
* MSTR = 1 Enable Master
* CPOL = 0 SCK is low when idle
* CPHA = 0 Sample Data with the raising edge
* SPR1:0 = 00 Choose SPI clock = Fosc/4
***********************************************************************/
SPCR = (1<<SPE) | (1<<MSTR);

/* Clear the SPI2X bit in SPSR to Choose SPI clock = Fosc/4 */
SPSR &= ~(1<<SPI2X);
}

/*
* Description :
* Initialize the SPI device as Slave.
*/
void SPI_initSlave(void)
{
/******** Configure SPI Slave Pins *********
* SS(PB4) --> Input
* MOSI(PB5) --> Input
* MISO(PB6) --> Output
* SCK(PB7) --> Input
********************************************/
GPIO_setupPinDirection(PORTB_ID,PIN4_ID,PIN_INPUT);
GPIO_setupPinDirection(PORTB_ID,PIN5_ID,PIN_INPUT);
GPIO_setupPinDirection(PORTB_ID,PIN6_ID,PIN_OUTPUT);
GPIO_setupPinDirection(PORTB_ID,PIN7_ID,PIN_INPUT);

/************************** SPCR Description **************************
* SPIE = 0 Disable SPI Interrupt
* SPE = 1 Enable SPI Driver
* DORD = 0 Transmit the MSB first
* MSTR = 0 Disable Master
* CPOL = 0 SCK is low when idle
* CPHA = 0 Sample Data with the raising edge
* SPR1:0 = 00 Choose SPI clock = Fosc/4
***********************************************************************/
SPCR = (1<<SPE);

/* Clear the SPI2X bit in SPSR to Choose SPI clock = Fosc/4 */
SPSR &= ~(1<<SPI2X);
}

/*
* Description :
* Send the required data through SPI to the other SPI device.
* In the same time data will be received from the other device.
*/
uint8 SPI_sendReceiveByte(uint8 data)
{
/* Initiate the communication and send data by SPI */
SPDR = data;

/* Wait until SPI interrupt flag SPIF = 1 (data has been sent/received correctly) */
while(BIT_IS_CLEAR(SPSR,SPIF)){}

/*
* Note: SPIF flag is cleared by first reading SPSR (with SPIF set) which is done in the previous step.
* and then accessing SPDR like the below line.
*/
return SPDR;
}

/*
* Description :
* Send the required string through SPI to the other SPI device.
*/
void SPI_sendString(const uint8 *str)
{
uint8 i = 0;
uint8 received_data = 0;

/* Send the whole string */
while(str[i] != '\0')
{
/*
* received_data contains the received data from the other device.
* It is a dummy data variable as we just need to send the string to other device.
*/
received_data = SPI_sendReceiveByte(str[i]);
i++;
}
}

/*
* Description :
* Receive the required string until the '#' symbol through SPI from the other SPI device.
*/
void SPI_receiveString(uint8 *str)
{
uint8 i = 0;

/* Receive the first byte */
str[i] = SPI_sendReceiveByte(SPI_DEFAULT_DATA_VALUE);

/* Receive the whole string until the '#' */
while(str[i] != '#')
{
i++;
str[i] = SPI_sendReceiveByte(SPI_DEFAULT_DATA_VALUE);
}

/* After receiving the whole string plus the '#', replace the '#' with '\0' */
str[i] = '\0';
}
Loading

0 comments on commit 70fc043

Please sign in to comment.