i2c is working beter and added a check device funtion who checks is a device is on the bu or not. Driver header in cmake were not correctly included, i have fixed that issue too

master
Kerem Yollu 2 years ago
parent 0e22b6b379
commit 4030ed9611

@ -93,39 +93,44 @@ add_subdirectory(peripherals)
set(STARTUP_UCODE sub::startup)
####################################################################################################
# Peripehral definitions
# Driver definitions
####################################################################################################
message("${BoldYellow}")
message("${BoldBlue}")
message("+-------------------------------+")
message("Cheking Periferals")
message("Cheking Drivers")
message("+-------------------------------+")
foreach(X IN LISTS PERIPHERALS_LIST)
addPeripheral("${X}" "NEW_PERIPHERAL")
list(APPEND PERIPHERAL_LIBS ${NEW_PERIPHERAL})
foreach(X IN LISTS DRIVERS_LIST)
addDriver("${X}" "NEW_DRIVER")
list(APPEND DRIVER_LIBS ${NEW_DRIVER})
endforeach()
foreach(X IN LISTS DRIVERS_LIST)
addDriverHeader("${X}" "NEW_DRIVER_HEADER")
list(APPEND DRIVER_HEADERS_LIST ${NEW_DRIVER_HEADER})
endforeach()
message("+-------------------------------+")
message("${ColourReset}")
####################################################################################################
# Driver definitions
# Peripehral definitions
####################################################################################################
message("${BoldBlue}")
message("${BoldYellow}")
message("+-------------------------------+")
message("Cheking Drivers")
message("Cheking Periferals")
message("+-------------------------------+")
foreach(X IN LISTS DRIVERS_LIST)
addDriver("${X}" "NEW_DRIVER")
list(APPEND DRIVER_LIBS ${NEW_DRIVER})
foreach(X IN LISTS PERIPHERALS_LIST)
addPeripheral("${X}" "NEW_PERIPHERAL")
list(APPEND PERIPHERAL_LIBS ${NEW_PERIPHERAL})
endforeach()
message("+-------------------------------+")
message("${ColourReset}")
####################################################################################################
# User's project definitions
####################################################################################################
@ -164,7 +169,7 @@ message("${ColourReset}")
####################################################################################################
# Stick All the libraries together, only for code redability futher down.
####################################################################################################
foreach(X IN LISTS STARTUP_UCODE PROJECT_LIBS PERIPHERAL_LIBS DRIVER_LIBS )
foreach(X IN LISTS STARTUP_UCODE PROJECT_LIBS PERIPHERAL_LIBS DRIVER_LIBS )
list(APPEND GENERATED_LIBRARIES ${X})
endforeach()
@ -218,7 +223,7 @@ add_executable(${EXECUTABLE} ${PROJECT_DIR}/main.c)
target_compile_options(${EXECUTABLE} PRIVATE ${MAIN_FLAGS})
target_compile_definitions(${EXECUTABLE} PRIVATE ${MAIN_DEFS})
target_include_directories(${EXECUTABLE} PUBLIC ${PROJECT_HEADERS_DIR} ${PROJECT_SOURCES_DIR}
${MAIN_INCLUDES})
${MAIN_INCLUDES} ${DRIVER_HEADERS_LIST})
####################################################################################################
# LINKING EXECUTEABLE

@ -103,17 +103,38 @@ function(addDriver alias _currentDriver)
if(EXISTS ${DRIVERS_DIR}/${alias}/${alias}.${PL})
message(" |-> Source File : ${DRIVERS_DIR}/${alias}/${alias}.${PL} = FOUND")
if(EXISTS ${DRIVERS_DIR}/${alias}/${alias}.h)
message(" |-> Header File : ${DRIVERS_DIR}/${alias}/${alias}.h = FOUND")
#add_subdirectory(${DRIVERS_DIR}/${alias})
add_library(${alias}_submodule ${DRIVERS_DIR}/${alias}/${alias}.${PL})
target_compile_options(${alias}_submodule PRIVATE ${C_FLAGS})
target_compile_definitions(${alias}_submodule PRIVATE ${C_DEFS})
target_include_directories(${alias}_submodule PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
add_library(sub::${alias} ALIAS ${alias}_submodule)
set(${_currentPripheral} sub::${alias} PARENT_SCOPE)
set(${_currentDriver} sub::${alias} PARENT_SCOPE)
else ()
message("${BoldRed}")
message(FATAL_ERROR "\nThe Included ${alias} Driver's Header file was NOT found ### COMPILATION ABORTED ###\n")
message("${ColourReset}")
endif()
else ()
message("${BoldRed}")
message(FATAL_ERROR "\nThe Included ${alias} Driver's Source file was NOT found ### COMPILATION ABORTED ###\n")
message("${ColourReset}")
endif()
else ()
message("${BoldRed}")
message(FATAL_ERROR "\nThe Included ${alias} Driver was NOT found ### COMPILATION ABORTED ###\n")
message("${ColourReset}")
endif()
endfunction()
function(addDriverHeader alias _currentDriverHeader)
if(EXISTS ${DRIVERS_DIR}/${alias} ) # Checks if the desired peripheral is implemented for the desired CSL.
message(" --> Driver directory : ${DRIVERS_DIR}/${alias} = FOUND")
if(EXISTS ${DRIVERS_DIR}/${alias}/${alias}.${PL})
message(" |-> Source File : ${DRIVERS_DIR}/${alias}/${alias}.${PL} = FOUND")
if(EXISTS ${DRIVERS_DIR}/${alias}/${alias}.h)
message(" |-> Header File : ${DRIVERS_DIR}/${alias}/${alias}.h = FOUND")
set(${_currentDriverHeader} ${DRIVERS_DIR}/${alias} PARENT_SCOPE)
else ()
message("${BoldRed}")

@ -1,10 +1,9 @@
#include "i2c.h"
#include "usart.h"
#include "delay.h"
#define I2C_BASE ((I2C_TypeDef*)i2cBase_Addr_List[i2c_dev->channelNo])
uint8_t temp8 = 0;
uint16_t temp16 = 0;
#define I2C_BASE ((I2C_TypeDef*)i2cBase_Addr_List[i2c_dev->channelNo])
void i2c_hardware_enable(i2c_t *i2c_dev)
{
@ -54,9 +53,7 @@ void i2c_set_mode(i2c_t *i2c_dev, i2c_mode_t mode)
//This device will set himself automatically to master mode
if(i2c_dev->mode == i2c_mode_master)
{
//Automatic end mode (master mode) disabled Enablede as default
I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND;
//I2C_BASE->CR2 |= I2C_CR2_AUTOEND;
}
else if(i2c_dev->mode == i2c_mode_slave)
{
@ -131,8 +128,8 @@ void i2c_set_filter(i2c_t *i2c_dev, uint8_t enable)
}
else
{
//Anlalog filter is on
I2C_BASE->CR2 |= I2C_CR2_START;
//Anlalog filter is off
I2C_BASE->CR1 |= I2C_CR1_ANFOFF;
}
}
@ -164,7 +161,12 @@ void i2c_send_stop(i2c_t *i2c_dev)
uint8_t i2c_is_perif_ready(i2c_t *i2c_dev)
{
return ((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY);
if((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY)
{
i2c_dev->periferalState = i2c_perif_ready;
return 1;
}
return 0;
}
void i2c_set_transfer_counter(i2c_t *i2c_dev, uint8_t count)
@ -191,8 +193,11 @@ void i2c_init_read_command(i2c_t *i2c_dev)
}
void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
uint8_t i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
{
//Automatic end mode (master mode) disabled Enablede as default
I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND;
// On This chip this shoudl be done before the start condition
i2c_init_write_command(i2c_dev);
@ -202,16 +207,10 @@ void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress)
// This device places the salve address automaticaly in the output buffer before sending the star condition
i2c_send_start(i2c_dev);
while(i2c_is_output_buffer_full(i2c_dev));
if(i2c_check_nack(i2c_dev))
{
i2c_dev->hardwareState = i2c_hw_got_nack;
i2c_throw_error(i2c_dev,i2c_dev->hardwareState);
}
i2c_dev->hardwareState = i2c_hw_got_ack;
return 1;
}
void i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress)
uint8_t i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress)
{
// On This chip this shoudl be done before the start condition
i2c_init_read_command(i2c_dev);
@ -220,14 +219,8 @@ void i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress)
I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode
// This device places the salve address automaticaly in the output buffer before sending the star condition
i2c_send_start(i2c_dev);
while(i2c_is_output_buffer_full(i2c_dev));
if(i2c_check_nack(i2c_dev))
{
i2c_dev->hardwareState = i2c_hw_got_nack;
i2c_throw_error(i2c_dev,i2c_dev->hardwareState);
}
i2c_dev->hardwareState = i2c_hw_got_ack;
while(i2c_is_output_buffer_full(i2c_dev));
return 1;
}
uint8_t i2c_is_output_buffer_full(i2c_t *i2c_dev)
@ -331,61 +324,10 @@ void i2c_send_nack(i2c_t *i2c_dev)
}
}
/*
void i2cSendSlaveAddressRead(i2c_t *i2c_dev, uint16_t *slaveAddress)
{
I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND;
// The Slave addrress is automatically place on the output buffer (must be done before the start condition)
I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode
// On This chip this shoudl be done before the start condition
i2cInitiateReadCommand(i2c_dev);
i2cGenerateStart(i2c_dev);
// This device places the salve address automaticaly in the output buffer before sending the star condition
i2c_dev->hardwareState = i2cHwOutputBufferFull;
while((i2c_dev->hardwareState =! i2cHwOutputBufferEmpty))
{
i2cIsOutputBufferEmpty(i2c_dev);
}
}
void i2cSetOutputRegister(i2c_t *i2c_dev, uint8_t *data)
{
i2c_dev->hardwareState = i2cHwOutputBufferFull;
}
void i2cIsTransferComplete(i2c_t *i2c_dev)
{
if((I2C_BASE->ISR & (I2C_ISR_TC)) == I2C_ISR_TC)
{
i2c_dev->periferalState = i2cPerifTransferComplete;
}
else
{
i2c_dev->periferalState = i2cPerifTransferOngoign;
}
}
}
*/
uint8_t i2c_check_nack(i2c_t *i2c_dev)
{
if((I2C_BASE->ISR & (I2C_ISR_NACKF)))
{
return 1;
}
return 0;
delayMs(2);
return(I2C_BASE->ISR & I2C_ISR_NACKF);
}
void i2c_set_address(i2c_t *i2c_dev, uint16_t address){}

@ -1 +1 @@
set(DRIVERS_LIST max7219 max31865)
set(DRIVERS_LIST pf8574 max7219 max31865)

@ -1,5 +0,0 @@
add_library(MAX31865 max31865.c)
target_compile_options(MAX31865 PRIVATE ${C_FLAGS})
target_compile_definitions(MAX31865 PRIVATE ${C_DEFS})
target_include_directories(MAX31865 PUBLIC . ${PERIFERALS_DIR} ${CSL_INCLUDES})
add_library(sub::max31865 ALIAS MAX31865)

@ -1,5 +0,0 @@
add_library(MAX7219 max7219.c)
target_compile_options(MAX7219 PRIVATE ${C_FLAGS})
target_compile_definitions(MAX7219 PRIVATE ${C_DEFS})
target_include_directories(MAX7219 PUBLIC . ${PERIFERALS_DIR} ${CSL_INCLUDES})
add_library(sub::max7219 ALIAS MAX7219)

@ -0,0 +1,123 @@
//////////////////////////////////////////////////////////
// Created by : Kerem Yollu
// Project : Multiprise conectée
// Nom : lcd.c
// Header : lcd.h
//_________________________Info_________________________
//
// Libraire pour le control d'un ecran lcd stadard (HD44780)
// controlée par un expandeur de port PCF8574 en I2C.
//
//////////////////////////////////////////////////////////
#include "pf8574.h"
// Fonction pour initialiser l'écran vide en mode 4 bits
uint16_t lcd_init(i2c_t *i2c_dev)
{
lcd_write(i2c_dev,0x03,CMD_MODE); // Mise en mode 4 bit avec 4 essai conssecutif
lcd_write(i2c_dev,0x03,CMD_MODE);
lcd_write(i2c_dev,0x03,CMD_MODE);
lcd_write(i2c_dev,0x02,CMD_MODE);
lcd_write(i2c_dev,LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE ,CMD_MODE);
lcd_write(i2c_dev,LCD_DISPLAYCONTROL | LCD_DISPLAYON ,CMD_MODE);
lcd_write(i2c_dev,LCD_CLEARDISPLAY ,CMD_MODE);
lcd_write(i2c_dev,LCD_ENTRYMODESET | LCD_ENTRYLEFT ,CMD_MODE);
return 1;
}
// Fonction qui vas afficher une pharse sur la ligne et colone qui lui est indiquée
// Attention la fonction est capable de calculer la liongeure d0ubn phrase mais il ne
// faut pas dépasser la limite d'une ligne totale qui est de 20 charactères max
//
// LCD 20x4
// -------------------------------------------
// ||# # # # # # # # # # # # # # # # # # # #||
// ||# # # # # # # # # # # # # # # # # # # #||
// ||# # # # # # # # # # # # # # # # # # # #||
// ||# # # # # # # # # # # # # # # # # # # #||
// -------------------------------------------
void lcd_display_string(i2c_t *i2c_dev, uint8_t line, uint8_t pos, uint8_t* charvalue, uint8_t lenght)
{
uint8_t setPosition = 0;
uint16_t i, S_length = 0;
S_length = lenght;
if (S_length > TOTAL_CHAR_CAP)
{
}
else
{
if(line == 1) // Selection de la ligne d'écriture
{
setPosition = pos;
}
else if(line ==2)
{
setPosition = 0x40 + pos;
}
else if(line ==3)
{
setPosition = 0x14 + pos;
}
else if(line ==4)
{
setPosition = 0x54 + pos;
}
else
{
setPosition = -1;
}
if(setPosition >= 0)
{
lcd_write(i2c_dev, LCD_SETDDRAMADDR + setPosition, CMD_MODE);
for(i = 0; i < lenght; i++ )
{
lcd_write(i2c_dev,charvalue[i],RS);
}
}
else
{
}
}
}
// Cette fonction nous permet d'envoyer un information de 8 bits sous format
// de 2x4 bites. Celà est necessaire du au fonctionnement de l'expendeur de port PCF8574
// qui est branché sur l'écran de facon a ce qu'il communiquer en 4 bits.
void lcd_write(i2c_t *i2c_dev, uint8_t cmd, uint8_t mode)
{
lcd_write_4bits(i2c_dev, mode | (cmd & 0xF0));
lcd_write_4bits(i2c_dev, mode | ((cmd << 4) & 0xF0));
}
// Fonction nous permettant d'nevoyer 4 bits dinformation sur le PC8574 ainsi que
// le rétroéclairage.
void lcd_write_4bits(i2c_t *i2c_dev,uint8_t data)
{
uint16_t address = LCD_ADDRS;
uint8_t i2c_data = 0;
i2c_set_transfer_counter(i2c_dev,3);
i2c_send_address_for_write(i2c_dev, &address);
i2c_data = data | LCD_BACKLIGHT;
i2c_send_data(i2c_dev, &i2c_data);
// Toggle LCD enbale pin.
i2c_data = data | EN |LCD_BACKLIGHT;
i2c_send_data(i2c_dev, &i2c_data);
delayMs(100);
i2c_data = ((data & ~EN) | LCD_BACKLIGHT);
i2c_send_data(i2c_dev, &i2c_data);
while(!i2c_is_transfer_complete(i2c_dev));
i2c_send_stop(i2c_dev);
delayMs(500);
}

@ -0,0 +1,94 @@
/**
**************************************************************************************************
* @file max7219.h
* @author Kerem Yollu & Edwin Koch
* @date 25.08.2022
* @version 1.0
**************************************************************************************************
* @brief
*
* **Detailed Description :**
*
* @todo
**************************************************************************************************
*/
#ifndef _PF8574_H_
#define _PF8574_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "i2c.h"
#include "delay.h"
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags pour mode d'ecriture
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags pour ecran on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags pour display/decalage curseurr
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags pour function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x1DOTS 0x04
#define LCD_5x8DOTS 0x00
//flags pour le rétroeclairage
#define LCD_BACKLIGHT 0x08
#define LCD_NOBACKLIGHT 0x00
//Pins de gestion de donées.
#define EN 0x04 // Enable bit
#define RW 0x02 // Read/Write bit
#define RS 0x01 // Register select bit
//DIfferents mode enre commande est ecriture
#define CMD_MODE 0x00
#define CHAR_MODE 0x01
//adresse I2C du controlleur pour LCD
#define LCD_ADDRS 0x27
//Nombre max de uint8_t sur une ligne
#define TOTAL_CHAR_CAP 20
uint16_t lcd_init(i2c_t *i2c_dev);
void lcd_write_char(i2c_t *i2c_dev, uint8_t charvalue);
void lcd_display_string(i2c_t *i2c_dev, uint8_t line, uint8_t pos, uint8_t* charvalue, uint8_t lenght);
void lcd_write(i2c_t *i2c_dev, uint8_t cmd, uint8_t mode);
void lcd_write_4bits(i2c_t *i2c_de, uint8_t data);
#ifdef __cplusplus
}
#endif
#endif /* MAIN_H */

@ -43,7 +43,6 @@ void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
{
int i;
char pt;
print_Usart(usart2, "\n\r");
if(lsbFirst)
{
print_Usart(usart2, "Bit Pos | 0| 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|");
@ -77,7 +76,6 @@ void printBinary8(uint8_t toPrint, uint8_t lsbFirst)
{
int i;
char pt;
print_Usart(usart2, "\n\r");
if(lsbFirst)
{
print_Usart(usart2, "Bit Pos | 0| 1| 2| 3| 4| 5| 6| 7|");
@ -113,7 +111,7 @@ int main(int argc, char *argv[])
uint16_t slaveAddress = 0xC0;
uint8_t registerToRead = 0x00;
uint8_t i2cRecieved = 0;
uint8_t i2cToWrite = 0xFF;
uint8_t i2cData = 0xFF;
uint8_t i2cDataLenght = 1;
i2c_t i2c_1;
@ -148,32 +146,37 @@ int main(int argc, char *argv[])
print_Usart(usart2, "\n\r");
i2cInit(&i2c_1, I2C_CH_1, i2cModeMaster, 0x00,0x00, i2cAddressCountSingle, i2cAddressSizeSevenBits, i2cSpeedStandart, i2cOpperationPolling, i2cClockStretchingDisable, i2cWakeUpDisabled);
/*
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved, &i2cDataLenght);
printBinary8(i2cRecieved,0);
i2c_init(&i2c_1, I2C_CH_1, i2c_mode_master, 0x00,0x00, i2c_address_count_single, i2c_address_size_7b, i2c_clk_speed_standart, i2c_clk_stretching_disable, i2c_wake_disabled);
slaveAddress = 0x40;
registerToRead = 0x02;
registerToRead = 0x06;
i2cData = 0xAA;
print_Usart(usart2, "Register Address To Write \n\r");
printBinary8(registerToRead,0);
print_Usart(usart2, "\n\r");
registerToRead += 1;
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved, &i2cDataLengh);
printBinary8(i2cRecieved,0);
*/
registerToRead = 1;
i2cToWrite = 0xFF;
print_Usart(usart2, "\n\r");
//i2cWrite(&i2c_1, &slaveAddress, &registerToRead, &i2cToWrite, &i2cDataLenght);
print_Usart(usart2, "Data To Sent\n\r");
printBinary8(i2cData,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,&i2cDataLenght);
i2c_check_device(&i2c_1, &slaveAddress);
i2c_write(&i2c_1, &slaveAddress, &registerToRead, &i2cData, &i2cDataLenght);
i2c_read(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,&i2cDataLenght);
print_Usart(usart2, "Data Recieved\n\r");
printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "All is working fine\n\r");
while(1)
{
delayMs(100);

@ -5,9 +5,7 @@
extern "C" {
#endif
#include <stdint.h>
#ifdef __cplusplus

@ -38,7 +38,7 @@ void i2c_init( i2c_t *i2c_dev, /*!< Pointer to I2C hardware Object */
i2c_set_address(i2c_dev, mainAddress);
i2c_set_address_second(i2c_dev, mainAddress);
i2c_periferal_enable(i2c_dev);
print_Usart(usart2, "I2C -> periferal enabled\n\r");
print_Usart(usart2, "\n\rI2C -> periferal enabled\n\r");
}
void i2c_read(i2c_t *i2c_dev, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght)
@ -55,7 +55,7 @@ void i2c_read(i2c_t *i2c_dev, uint16_t *slaveAddress, uint8_t *registerAddress,
i2c_send_reg_address(i2c_dev, registerAddress);
while(!i2c_is_transfer_complete(i2c_dev));
i2c_set_transfer_counter(i2c_dev,1);
//i2c_set_transfer_counter(i2c_dev,2);
i2c_send_address_for_read(i2c_dev, slaveAddress);
i2c_get_input_register(i2c_dev, data);
while(!i2c_is_transfer_complete(i2c_dev));
@ -63,19 +63,13 @@ void i2c_read(i2c_t *i2c_dev, uint16_t *slaveAddress, uint8_t *registerAddress,
i2c_send_stop(i2c_dev);
while(!i2c_is_perif_ready(i2c_dev));
print_Usart(usart2, "I2C Ready afer read\n\r");
i2c_dev->periferalState = i2c_perif_ready;
}
void i2c_write(i2c_t *i2c_dev, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght)
{
i2c_dev->periferalState = i2c_perif_write;
i2c_set_transfer_counter(i2c_dev,2);
print_Usart(usart2, "I2C Transfer Counter: ");
usartSendChar(usart2, i2c_get_transfer_counter(i2c_dev) + 0x30);
print_Usart(usart2, "\n\r");
i2c_send_address_for_write(i2c_dev, slaveAddress);
i2c_send_reg_address(i2c_dev, registerAddress);
i2c_send_data(i2c_dev, data);
@ -83,18 +77,44 @@ void i2c_write(i2c_t *i2c_dev, uint16_t *slaveAddress, uint8_t *registerAddress,
i2c_send_stop(i2c_dev);
while(!i2c_is_perif_ready(i2c_dev));
print_Usart(usart2, "I2C Ready afer write\n\r");
i2c_dev->periferalState = i2c_perif_ready;
}
void i2c_throw_error(i2c_t *i2c_dev, int16_t error)
uint8_t i2c_check_device(i2c_t *i2c_dev, uint16_t *device)
{
uint8_t found = 0;
i2c_dev->periferalState = i2c_perif_write;
i2c_set_transfer_counter(i2c_dev,0);
i2c_send_address_for_write(i2c_dev, device);
if(i2c_check_nack(i2c_dev))
{
i2c_dev->hardwareState = i2c_hw_got_nack;
i2c_throw_error(i2c_dev,1);
return 0;
}
i2c_dev->hardwareState = i2c_hw_got_ack;
return 1;
}
uint8_t i2c_discover_devices(i2c_t *i2c_dev)
{
uint16_t i = 0;
char pt;
for(i = 0x03; i <= 0x77; i++)
{
i2c_check_device(i2c_dev,&i);
}
}
void i2c_throw_error(i2c_t *i2c_dev, uint8_t error)
{
print_Usart(usart2, "\n\r");
print_Usart(usart2, "I2C : Error");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "I2C Error Detected : ");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "I2C Error No: ");
usartSendChar(usart2, error + 0x30);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
if(error == 1)
{
print_Usart(usart2, " -> Device not Found ");
print_Usart(usart2, "\n\r");
}
}

@ -122,6 +122,7 @@ typedef enum
i2c_perif_listening, /*!< Address Listen Mode is ongoing */
i2c_perif_listening_and_tx, /*!< Address Listen Mode and ongoing Data Transmission */
i2c_perif_listening_and_rx, /*!< Address Listen Mode and ongoing Data Reception */
i2c_perif_discovery, /*!< Discovery mode will cancel some error in order to be able to discover the devices on the bus */
i2c_perif_timeout, /*!< Timeout state */
i2c_perif_Error /*!< Error */
} i2c_perif_state_t;
@ -381,7 +382,7 @@ void i2c_send_ack(i2c_t *i2c_dev);
* @param i2c_dev is the beforehand declared i2c channel with his opperation modes
* @param slaveAddress The address of the slave to be communicated
*/
void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress);
uint8_t i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress);
/**
* @brief Initiates the communication by sending the slave address on the bus followed by a READ
@ -389,7 +390,7 @@ void i2c_send_address_for_write(i2c_t *i2c_dev, uint16_t *slaveAddress);
* @param i2c_dev is the beforehand declared i2c channel with his opperation modes
* @param slaveAddress The address of the slave to be communicated
*/
void i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress);
uint8_t i2c_send_address_for_read(i2c_t *i2c_dev, uint16_t *slaveAddress);
/**
* @brief Sende the register adrres with which we want to communicate.
@ -489,10 +490,15 @@ void i2c_arbitration_clear_bus(); // I2C Standart : in case if SCL is stuck
* And write them to the Devices list given to him. This function will not discover more
* devices than what he is told.
* @param i2c_dev is the beforehand declared i2c channel with his opperation modes
* @param devices list where the discevered devices will be written
* @param deviceCount Max number of devices to be discovered.
*/
void i2c_discover_devices(i2c_t *i2c_dev, uint16_t *devices, uint8_t deviceCount);
uint8_t i2c_discover_devices(i2c_t *i2c_dev);
/**
* @brief This function will scan the given device to see if he responds
* @param i2c_dev is the beforehand declared i2c channel with his opperation modes
* @param address is the address to be chekced
*/
uint8_t i2c_check_device(i2c_t *i2c_dev, uint16_t *device);
/**
* @brief This function will try to communicate with a device with every speed
@ -520,7 +526,7 @@ void i2c_sleep(i2c_t *i2c_dev);
* @brief Error handling.
* @param error The error no generated.
*/
void i2c_throw_error(i2c_t *i2c_dev, int16_t error);
void i2c_throw_error(i2c_t *i2c_dev, uint8_t error);
uint8_t i2c_is_txis(i2c_t *i2c_dev);

Loading…
Cancel
Save