Compare commits
49 Commits
master
...
redesign_i
@ -0,0 +1 @@
|
||||
|
@ -0,0 +1,301 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file i2c.c
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 18.07.2022
|
||||
* @version 1.0
|
||||
**************************************************************************************************
|
||||
* @brief I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
|
||||
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
|
||||
* This will also not have a I3C support for the forseable futrue. This code is generated for the stm32f4xK6 series of mcu for stm
|
||||
*
|
||||
* **Detailed Description :**
|
||||
*
|
||||
* I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
|
||||
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
|
||||
* This will also not have a I3C support for the forseable futrue.
|
||||
*
|
||||
* @todo
|
||||
* - 18.07.2021 : Implement the i2c.c
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#include "i2c.h"
|
||||
|
||||
#define I2C_BASE ((I2C_TypeDef*)i2cBase_Addr_List[i2cHardware->channelNo])
|
||||
|
||||
uint8_t temp8 = 0;
|
||||
uint16_t temp16 = 0;
|
||||
|
||||
void i2cEnableHardware(i2c_t *i2cHardware)
|
||||
{
|
||||
i2cHardwareReset(i2cHardware);
|
||||
|
||||
// Enables the i2c bus
|
||||
RCC->APB1ENR |= (1 << i2cBus_En_bitPos[i2cHardware->channelNo]);
|
||||
|
||||
pinInit(pinA9);
|
||||
pinInit(pinA10);
|
||||
pinConfig(pinA9, alternate, openDrain, pullUp, fast);// I2C SCL
|
||||
pinConfig(pinA10, alternate, openDrain, pullUp, fast);// I2C CSA
|
||||
pinSetAlternate(pinA9, 4);
|
||||
pinSetAlternate(pinA10, 4);
|
||||
}
|
||||
|
||||
void i2cHardwareReset(i2c_t *i2cHardware)
|
||||
{
|
||||
// Resete the I2C hardware using the reset registers,
|
||||
RCC->APB1RSTR |= (1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
|
||||
RCC->APB1RSTR &= ~(1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
|
||||
}
|
||||
|
||||
|
||||
void i2cEnablePeriferal(i2c_t *i2cHardware)
|
||||
{
|
||||
I2C_BASE->CR1 |= I2C_CR1_PE;
|
||||
}
|
||||
|
||||
void i2cDisablePeriferal(i2c_t *i2cHardware)
|
||||
{
|
||||
I2C_BASE->CR1 &= ~I2C_CR1_PE;
|
||||
}
|
||||
|
||||
|
||||
void i2cSetMode(i2c_t *i2cHardware, i2cMode_t mode)
|
||||
{
|
||||
//This device will set himself automatically to master mode
|
||||
if(i2cHardware->mode == i2cModeMaster)
|
||||
{
|
||||
//Automatic end mode (master mode) disabled Enablede as default
|
||||
I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND;
|
||||
//I2C_BASE->CR2 |= I2C_CR2_AUTOEND;
|
||||
}
|
||||
else if(i2cHardware->mode == i2cModeSlave)
|
||||
{
|
||||
// to be written.
|
||||
}
|
||||
}
|
||||
|
||||
void i2cSetAddress(i2c_t *i2cHardware, uint16_t mainAddress, uint16_t secondAddress)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void i2cSetAddressLenght(i2c_t *i2cHardware, i2cAddressSize_t size)
|
||||
{
|
||||
if(i2cHardware->mode == i2cModeMaster)
|
||||
{
|
||||
if(i2cHardware->addressSize == i2cAddressSizeTenBits)
|
||||
{
|
||||
I2C_BASE->CR2 |= I2C_CR2_ADD10; // 10 Bit addressing
|
||||
I2C_BASE->CR2 &= ~I2C_CR2_HEAD10R; // 7 Bit header read turned on DEFAULT
|
||||
}
|
||||
else
|
||||
{
|
||||
I2C_BASE->CR2 &= ~I2C_CR2_ADD10; // 7 Bit addressing DEFAULT
|
||||
I2C_BASE->CR2 |= I2C_CR2_HEAD10R; // 7 Bit header read turned off DEFAULT
|
||||
}
|
||||
}
|
||||
else if(i2cHardware->mode == i2cModeSlave)
|
||||
{
|
||||
// to be written.
|
||||
}
|
||||
}
|
||||
|
||||
void i2cSetClockStretch(i2c_t *i2cHardware, i2cClockStretching_t stretching)
|
||||
{
|
||||
if(i2cHardware->stretching == i2cClockStretchingEnable)
|
||||
{
|
||||
I2C_BASE->CR1 &=~ I2C_CR1_NOSTRETCH;
|
||||
}
|
||||
else if(i2cHardware->stretching == i2cClockStretchingDisable)
|
||||
{
|
||||
I2C_BASE->CR1 |= I2C_CR1_NOSTRETCH;
|
||||
}
|
||||
}
|
||||
|
||||
void i2cSetSpeed(i2c_t *i2cHardware, i2cSpeed_t speed)
|
||||
{
|
||||
switch(speed)
|
||||
{
|
||||
case i2cSpeedStandart:
|
||||
I2C_BASE->TIMINGR = 0x2000090E;
|
||||
break;
|
||||
|
||||
case i2cSpeedFast:
|
||||
break;
|
||||
|
||||
case i2cSpeedFastPlus:
|
||||
break;
|
||||
|
||||
case i2cSpeedHightSpeed:
|
||||
break;
|
||||
|
||||
case i2cSpeedUltraFast:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void i2cConfigureFilters(i2c_t *i2cHardware)
|
||||
{
|
||||
//Anlalog filter is on
|
||||
I2C_BASE->CR1 &= ~I2C_CR1_ANFOFF;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
I2C Hardware functions
|
||||
***************************************************************************************************/
|
||||
|
||||
void i2cGenerateStart(i2c_t *i2cHardware)
|
||||
{
|
||||
I2C_BASE->CR2 &=~ I2C_CR2_STOP;
|
||||
I2C_BASE->CR2 |= I2C_CR2_START;
|
||||
//Wait until the start condition in generated.
|
||||
while(!(I2C_BASE->ISR & (I2C_ISR_BUSY)));
|
||||
i2cHardware->hardwareState = i2cHwStartGenerated;
|
||||
// This device places the salve address automaticalyy in the output buffer before sending the star condition
|
||||
i2cHardware->hardwareState = i2cHwOutputBufferFull;
|
||||
}
|
||||
|
||||
void i2cGenerateStop(i2c_t *i2cHardware)
|
||||
{
|
||||
// Send stop command
|
||||
I2C_BASE->CR2 |= I2C_CR2_STOP;
|
||||
I2C_BASE->CR2 &=~ I2C_CR2_START;
|
||||
i2cHardware->hardwareState = i2cHwStopGenerated;
|
||||
}
|
||||
|
||||
void i2cIsPeriferalReady(i2c_t *i2cHardware)
|
||||
{
|
||||
if((I2C_BASE->ISR & (I2C_ISR_BUSY))!=I2C_ISR_BUSY)
|
||||
{
|
||||
i2cHardware->periferalState = i2cPerifReady;
|
||||
}
|
||||
}
|
||||
|
||||
void i2cSetTransferCounter(i2c_t *i2cHardware, uint8_t count)
|
||||
{
|
||||
I2C_BASE->CR2 &= ~(0xFF << I2C_CR2_NBYTES_Pos);
|
||||
I2C_BASE->CR2 |= (count << I2C_CR2_NBYTES_Pos);
|
||||
}
|
||||
|
||||
void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress)
|
||||
{
|
||||
// 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
|
||||
|
||||
i2cGenerateStart(i2cHardware);
|
||||
|
||||
// Wait until the data in the ouput buffer is put to the i2c BUS
|
||||
while((i2cHardware->hardwareState =! i2cHwOutputBufferEmpty))
|
||||
{
|
||||
i2cIsOutputBufferEmpty(i2cHardware);
|
||||
}
|
||||
}
|
||||
|
||||
void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t *registerAddress)
|
||||
{
|
||||
i2cSendData(i2cHardware,registerAddress);
|
||||
}
|
||||
|
||||
void i2cInitiateWriteCommand(i2c_t *i2cHardware)
|
||||
{
|
||||
I2C_BASE->CR2 &= ~I2C_CR2_RD_WRN;
|
||||
}
|
||||
|
||||
void i2cInitiateReadCommand(i2c_t *i2cHardware)
|
||||
{
|
||||
I2C_BASE->CR2 |= I2C_CR2_RD_WRN;
|
||||
}
|
||||
|
||||
void i2cSendData(i2c_t *i2cHardware, uint8_t *registerAddress)
|
||||
{
|
||||
while((i2cHardware->hardwareState =! i2cHwOutputBufferEmpty))
|
||||
{
|
||||
i2cIsOutputBufferEmpty(i2cHardware);
|
||||
}
|
||||
i2cSetOutputRegister(i2cHardware,registerAddress);
|
||||
while((i2cHardware->hardwareState =! i2cHwOutputBufferEmpty))
|
||||
{
|
||||
i2cIsOutputBufferEmpty(i2cHardware);
|
||||
}
|
||||
}
|
||||
|
||||
void i2cIsOutputBufferEmpty(i2c_t *i2cHardware)
|
||||
{
|
||||
if((i2cHardware->periferalState) == i2cPerifTransmitting)
|
||||
{
|
||||
if((I2C_BASE->ISR & (I2C_ISR_TXIS)) == I2C_ISR_TXIS)
|
||||
{
|
||||
i2cHardware->hardwareState = i2cHwOutputBufferEmpty;
|
||||
}
|
||||
else
|
||||
{
|
||||
i2cHardware->hardwareState = i2cHwOutputBufferFull;
|
||||
}
|
||||
}
|
||||
else if((i2cHardware->periferalState) == i2cPerifRecieving)
|
||||
{
|
||||
if((I2C_BASE->ISR & (I2C_ISR_TXE)) == I2C_ISR_TXE)
|
||||
{
|
||||
i2cHardware->hardwareState = i2cHwOutputBufferEmpty;
|
||||
}
|
||||
else
|
||||
{
|
||||
i2cHardware->hardwareState = i2cHwOutputBufferFull;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void i2cIsInputBufferEmpty(i2c_t *i2cHardware)
|
||||
{
|
||||
if((I2C_BASE->ISR & (I2C_ISR_RXNE)) == I2C_ISR_RXNE)
|
||||
{
|
||||
i2cHardware->hardwareState = i2cHwInputBufferFull;
|
||||
}
|
||||
else
|
||||
{
|
||||
i2cHardware->hardwareState = i2cHwInputBufferEmpty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void i2cReadInputRegister(i2c_t *i2cHardware, uint8_t *data)
|
||||
{
|
||||
*data = I2C_BASE->RXDR;
|
||||
}
|
||||
|
||||
void i2cSetOutputRegister(i2c_t *i2cHardware, uint8_t *data)
|
||||
{
|
||||
I2C_BASE->TXDR = *data;
|
||||
i2cHardware->hardwareState = i2cHwOutputBufferFull;
|
||||
}
|
||||
|
||||
void i2cIsTransferComplete(i2c_t *i2cHardware)
|
||||
{
|
||||
if((I2C_BASE->ISR & (I2C_ISR_TC)) == I2C_ISR_TC)
|
||||
{
|
||||
i2cHardware->periferalState = i2cPerifTransferComplete;
|
||||
}
|
||||
else
|
||||
{
|
||||
i2cHardware->periferalState = i2cPerifTransferOngoign;
|
||||
}
|
||||
}
|
||||
|
||||
void i2cGenerateNack(i2c_t *i2cHardware)
|
||||
{
|
||||
if(i2cHardware->mode == i2cModeMaster)
|
||||
{
|
||||
//IN master mode this ic geretes NACK's otomatically
|
||||
i2cHardware->hardwareState = i2cHwSentNack;
|
||||
}
|
||||
else if(i2cHardware->mode == i2cModeSlave)
|
||||
{
|
||||
// to be written.
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
#include "spi.h"
|
||||
#include "hardwareDescription.h"
|
||||
|
||||
#define SPI_BASE ((SPI_TypeDef *)spiBase_Addr_List[spi_hw_ch])
|
||||
|
||||
void spiReset(spiCH_t spi_hw_ch)
|
||||
{
|
||||
if(spiBus_No[spi_hw_ch] == 1) {
|
||||
RCC->APB1RSTR |= (1 << spiBus_Rst_bitPos[spi_hw_ch]);
|
||||
RCC->APB1RSTR &= ~(1 << spiBus_Rst_bitPos[spi_hw_ch]);
|
||||
return;
|
||||
}
|
||||
|
||||
RCC->APB2RSTR |= (1 << spiBus_Rst_bitPos[spi_hw_ch]);
|
||||
RCC->APB2RSTR &= ~(1 << spiBus_Rst_bitPos[spi_hw_ch]);
|
||||
}
|
||||
|
||||
void spiEnableBus(spiCH_t spi_hw_ch)
|
||||
{
|
||||
if(spiBus_No[spi_hw_ch] == 1) {
|
||||
RCC->APB1ENR |= (1 << spiBus_En_bitPos[spi_hw_ch]);
|
||||
return;
|
||||
}
|
||||
|
||||
RCC->APB2ENR |= (1 << spiBus_En_bitPos[spi_hw_ch]);
|
||||
}
|
||||
|
||||
void spiEnable(spiCH_t spi_hw_ch)
|
||||
{
|
||||
SPI_BASE->CR1 |= SPI_CR1_SPE;
|
||||
}
|
||||
|
||||
void spiDissable(spiCH_t spi_hw_ch)
|
||||
{
|
||||
// TODO: implement p.768 procedure for dissabling
|
||||
//while(SPI_BASE->SR
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_SPE;
|
||||
}
|
||||
|
||||
void spiSetMode(spiCH_t spi_hw_ch, spi_mode_t mode)
|
||||
{
|
||||
SPI_BASE->CR1 &= ~(mode << SPI_CR1_MSTR_Pos);
|
||||
SPI_BASE->CR1 |= mode << SPI_CR1_MSTR_Pos;
|
||||
|
||||
// TODO: find out if this is the correct place to set the SSOE bit
|
||||
SPI_BASE->CR2 &= ~SPI_CR2_SSOE;
|
||||
if(mode == SPI_MASTER) {
|
||||
SPI_BASE->CR2 |= SPI_CR2_SSOE;
|
||||
}
|
||||
}
|
||||
|
||||
void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol)
|
||||
{
|
||||
// reset
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_CPOL;
|
||||
// set
|
||||
SPI_BASE->CR1 |= clkPol << SPI_CR1_CPOL_Pos;
|
||||
}
|
||||
|
||||
void spiSetPhase(spiCH_t spi_hw_ch, spi_phase_t phase)
|
||||
{
|
||||
// reset
|
||||
SPI_BASE->CR1 &= ~(phase << SPI_CR1_CPHA_Pos);
|
||||
// set
|
||||
SPI_BASE->CR1 |= phase << SPI_CR1_CPHA_Pos;
|
||||
}
|
||||
|
||||
void spiSetBitFrameLength(spiCH_t spi_hw_ch, spi_framel_t framel)
|
||||
{
|
||||
|
||||
SPI_BASE->CR2 &= ~(SPI_CR2_FRXTH | SPI_CR2_DS);
|
||||
|
||||
// using p.974 as example
|
||||
if(framel == SPI_FRAME_LENGTH_8BIT) {
|
||||
// set FIFO reception threshold to 8 bit
|
||||
SPI_BASE->CR2 |= SPI_CR2_FRXTH;
|
||||
// set transfer lwnght to 8 bit
|
||||
SPI_BASE->CR2 |= SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2;
|
||||
return;
|
||||
}
|
||||
SPI_BASE->CR2 |= SPI_CR2_DS;
|
||||
}
|
||||
|
||||
void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef)
|
||||
{
|
||||
// reset
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_LSBFIRST;
|
||||
// set
|
||||
SPI_BASE->CR1 |= framef << SPI_CR1_LSBFIRST_Pos;
|
||||
}
|
||||
|
||||
spi_framef_t spiGetFrameFormat(spiCH_t spi_hw_ch)
|
||||
{
|
||||
return (spi_framef_t)(SPI_BASE->CR1 & SPI_CR1_LSBFIRST) >> SPI_CR1_LSBFIRST_Pos;
|
||||
}
|
||||
|
||||
void spiSetClockPrescaler(spiCH_t spi_hw_ch, uint32_t clkDiv)
|
||||
{
|
||||
// reset
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_BR;
|
||||
// set
|
||||
SPI_BASE->CR1 |= (clkDiv << SPI_CR1_BR_Pos) & SPI_CR1_BR;
|
||||
}
|
||||
|
||||
void spiSetComMode(spiCH_t spi_hw_ch, spi_comMode_t comMode)
|
||||
{
|
||||
// reset
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_RXONLY;
|
||||
// set
|
||||
SPI_BASE->CR1 |= comMode << SPI_CR1_RXONLY_Pos;
|
||||
}
|
||||
|
||||
void spiSetSoftwareSlaveManagement(spiCH_t spi_hw_ch, uint8_t logic)
|
||||
{
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_SSM;
|
||||
|
||||
if(logic){
|
||||
SPI_BASE->CR1 |= SPI_CR1_SSM;
|
||||
}
|
||||
}
|
||||
|
||||
void spiSetInternalSlaveSelect(spiCH_t spi_hw_ch, uint8_t logic)
|
||||
{
|
||||
SPI_BASE->CR1 &= ~SPI_CR1_SSI;
|
||||
|
||||
if(logic) {
|
||||
SPI_BASE->CR1 |= SPI_CR1_SSI;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t spiTrx8BitPolling(spiCH_t spi_hw_ch, uint8_t tx_data)
|
||||
{
|
||||
// wait for BSY bit to Reset -> This will indicate that SPI is not busy in communication
|
||||
while (SPI_BASE->SR & SPI_SR_BSY);
|
||||
|
||||
// from example code p.975 f
|
||||
// this masking must be done. otherwise 16bits frame will be used
|
||||
*(uint8_t*)&(SPI_BASE->DR) = tx_data;
|
||||
|
||||
// Wait for RXNE to set -> This will indicate that the Rx buffer is not empty
|
||||
while (!(SPI_BASE->SR &SPI_SR_RXNE));
|
||||
|
||||
|
||||
// TODO: test read!
|
||||
return *(uint8_t*)&(SPI_BASE->DR);
|
||||
}
|
||||
|
@ -0,0 +1,2 @@
|
||||
add_subdirectory(max7219)
|
||||
add_subdirectory(max31865)
|
@ -0,0 +1,5 @@
|
||||
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)
|
@ -0,0 +1,5 @@
|
||||
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,124 @@
|
||||
#include "max7219.h"
|
||||
|
||||
void max7219_init(
|
||||
max7219_t *display,
|
||||
spi_ch_t *spi_ch)
|
||||
{
|
||||
display->spiCH = spi_ch;
|
||||
}
|
||||
|
||||
void max7219_testDisplay(
|
||||
max7219_t *display,
|
||||
uint8_t logic)
|
||||
{
|
||||
spiWriteReg(display->spiCH,0x0F, (logic) ? 0x01 : 0x00);
|
||||
}
|
||||
|
||||
void max7219_shutdownDiaply(
|
||||
max7219_t *display,
|
||||
uint8_t logic)
|
||||
{
|
||||
spiWriteReg(display->spiCH,0x0C, (logic) ? 0x00 : 0x01);
|
||||
}
|
||||
|
||||
void max7219_setDecodeMode(
|
||||
max7219_t *display,
|
||||
max7219_decodeMode_t dmode)
|
||||
{
|
||||
spiWriteReg(display->spiCH,0x09,(uint8_t) dmode);
|
||||
}
|
||||
|
||||
void max7219_setIntensity(
|
||||
max7219_t *display,
|
||||
uint8_t intensity)
|
||||
{
|
||||
spiWriteReg(display->spiCH, 0x0A, intensity & 0x0F);
|
||||
}
|
||||
|
||||
void max7219_setScanLimit(
|
||||
max7219_t *display,
|
||||
max7219_scanLimit_t slimit)
|
||||
{
|
||||
spiWriteReg(display->spiCH, 0x0B, ((uint8_t) slimit) & 0x0F);
|
||||
}
|
||||
|
||||
void max7219_setAllLEDsOff(
|
||||
max7219_t *display)
|
||||
{
|
||||
uint8_t i;
|
||||
for(i = 0; i < 9; i++) {
|
||||
spiWriteReg(display->spiCH, i, 0x00);
|
||||
}
|
||||
}
|
||||
|
||||
void max7219_ledMatrixSetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col)
|
||||
{
|
||||
|
||||
//uint8_t val = 0xAE; // Unused variable warning commented by KeY
|
||||
|
||||
row = (row & 0x07) + 1;
|
||||
col = 1 << (col & 0x07);
|
||||
|
||||
spiWriteReg(display->spiCH, row,col);
|
||||
}
|
||||
|
||||
void max7219_rawWrite(
|
||||
max7219_t *display,
|
||||
uint8_t reg,
|
||||
uint8_t data)
|
||||
{
|
||||
spiWriteReg(display->spiCH, reg, data);
|
||||
}
|
||||
|
||||
void max7219_printLedMatrix(
|
||||
max7219_t *display,
|
||||
uint8_t matrix[])
|
||||
{
|
||||
uint8_t i = 0;
|
||||
|
||||
for(i = 0; i < 8; i ++) {
|
||||
spiWriteReg(display->spiCH, i+1, matrix[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void max7219_ledMatrixUnsetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col)
|
||||
{
|
||||
row = (row & 0x07) + 1;
|
||||
col = 1 << (col & 0x07);
|
||||
// TODO: find out how to turn off LED
|
||||
spiWriteReg(display->spiCH, row,col+1);
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
// daysichained matrix
|
||||
typedef struct{
|
||||
spi_ch_t *spiCH;
|
||||
uint8_t nDevices;
|
||||
}max7219_dm_t;
|
||||
|
||||
|
||||
void max7219_dm_write(
|
||||
max7219_dm_t *matrix,
|
||||
uint8_t **data)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t j = 0;
|
||||
// TODO: Test it out
|
||||
for(i = 0; i < 8; i++) {
|
||||
pinWrite(matrix->spiCH->pin, 0);
|
||||
for(j = 0; j < matrix->nDevices; j++) {
|
||||
spiTrx8BitPolling(matrix->spiCH->spi, i+1); // reg
|
||||
spiTrx8BitPolling(matrix->spiCH->spi, data[j][i]);
|
||||
}
|
||||
pinWrite(matrix->spiCH->pin, 1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -0,0 +1,111 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file max7219.h
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 25.08.2022
|
||||
* @version 1.0
|
||||
**************************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* **Detailed Description :**
|
||||
*
|
||||
* @todo
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _MAX7219_H_
|
||||
#define _MAX7219_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "spi.h"
|
||||
#include "pin.h"
|
||||
|
||||
typedef enum{
|
||||
NO_DECODE_DIGIT_7_TO_0 = 0x00,
|
||||
CODE_B_DECODE_ONLY_DIGIT_0 = 0x01,
|
||||
CODE_B_DECODE_ONLY_DIGIT_3_TO_0 = 0x0F,
|
||||
CODE_B_DECOD_DIGIT_7_TO_0 = 0xFF
|
||||
}max7219_decodeMode_t;
|
||||
|
||||
typedef enum{
|
||||
DISPLAY_DIGIT_0 = 0x00,
|
||||
DISPLAY_DIGIT_1_TO_0 = 0x01,
|
||||
DSIPLAX_DIGIT_2_TO_0 = 0x02,
|
||||
DSIPLAX_DIGIT_3_TO_0 = 0x03,
|
||||
DSIPLAX_DIGIT_4_TO_0 = 0x04,
|
||||
DSIPLAX_DIGIT_5_TO_0 = 0x05,
|
||||
DSIPLAX_DIGIT_6_TO_0 = 0x06,
|
||||
DSIPLAX_DIGIT_7_TO_0 = 0x07
|
||||
}max7219_scanLimit_t;
|
||||
|
||||
typedef struct{
|
||||
spi_ch_t *spiCH;
|
||||
}max7219_t;
|
||||
|
||||
void max7219_init(
|
||||
max7219_t *display,
|
||||
spi_ch_t *spi_ch);
|
||||
|
||||
void max7219_testDisplay(
|
||||
max7219_t *display,
|
||||
uint8_t logic);
|
||||
|
||||
void max7219_shutdownDiaply(
|
||||
max7219_t *display,
|
||||
uint8_t logic);
|
||||
|
||||
void max7219_setDecodeMode(
|
||||
max7219_t *display,
|
||||
max7219_decodeMode_t dmode);
|
||||
|
||||
void max7219_setIntensity(
|
||||
max7219_t *display,
|
||||
uint8_t intensity);
|
||||
|
||||
void max7219_setScanLimit(
|
||||
max7219_t *display,
|
||||
max7219_scanLimit_t slimit);
|
||||
|
||||
void max7219_setAllLEDsOff(
|
||||
max7219_t *display);
|
||||
|
||||
void max7219_ledMatrixSetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col);
|
||||
|
||||
void max7219_rawWrite(
|
||||
max7219_t *display,
|
||||
uint8_t reg,
|
||||
uint8_t data);
|
||||
|
||||
void max7219_printLedMatrix(
|
||||
max7219_t *display,
|
||||
uint8_t matrix[]);
|
||||
|
||||
void max7219_ledMatrixUnsetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col);
|
||||
#if 0
|
||||
// daysichained matrix
|
||||
typedef struct{
|
||||
spi_ch_t *spiCH;
|
||||
uint8_t nDevices;
|
||||
}max7219_dm_t;
|
||||
|
||||
|
||||
void max7219_dm_write(
|
||||
max7219_dm_t *matrix,
|
||||
uint8_t **data);
|
||||
#endif
|
||||
|
||||
#ifdef _cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _MAX7219_H_
|
@ -0,0 +1,126 @@
|
||||
#include "max7219.h"
|
||||
|
||||
void max7219_init(
|
||||
max7219_t *display,
|
||||
spi_ch_t *spi_ch)
|
||||
{
|
||||
display->spiCH = spi_ch;
|
||||
}
|
||||
|
||||
void h
|
||||
|
||||
void max7219_testDisplay(
|
||||
max7219_t *display,
|
||||
uint8_t logic)
|
||||
{
|
||||
spiWriteReg(display->spiCH,0x0F, (logic) ? 0x01 : 0x00);
|
||||
}
|
||||
|
||||
void max7219_shutdownDiaply(
|
||||
max7219_t *display,
|
||||
uint8_t logic)
|
||||
{
|
||||
spiWriteReg(display->spiCH,0x0C, (logic) ? 0x00 : 0x01);
|
||||
}
|
||||
|
||||
void max7219_setDecodeMode(
|
||||
max7219_t *display,
|
||||
max7219_decodeMode_t dmode)
|
||||
{
|
||||
spiWriteReg(display->spiCH,0x09,(uint8_t) dmode);
|
||||
}
|
||||
|
||||
void max7219_setIntensity(
|
||||
max7219_t *display,
|
||||
uint8_t intensity)
|
||||
{
|
||||
spiWriteReg(display->spiCH, 0x0A, intensity & 0x0F);
|
||||
}
|
||||
|
||||
void max7219_setScanLimit(
|
||||
max7219_t *display,
|
||||
max7219_scanLimit_t slimit)
|
||||
{
|
||||
spiWriteReg(display->spiCH, 0x0B, ((uint8_t) slimit) & 0x0F);
|
||||
}
|
||||
|
||||
void max7219_setAllLEDsOff(
|
||||
max7219_t *display)
|
||||
{
|
||||
uint8_t i;
|
||||
for(i = 0; i < 9; i++) {
|
||||
spiWriteReg(display->spiCH, i, 0x00);
|
||||
}
|
||||
}
|
||||
|
||||
void max7219_ledMatrixSetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col)
|
||||
{
|
||||
|
||||
uint8_t val = 0xAE;
|
||||
|
||||
row = (row & 0x07) + 1;
|
||||
col = 1 << (col & 0x07);
|
||||
|
||||
spiWriteReg(display->spiCH, row,col);
|
||||
}
|
||||
|
||||
void max7219_rawWrite(
|
||||
max7219_t *display,
|
||||
uint8_t reg,
|
||||
uint8_t data)
|
||||
{
|
||||
spiWriteReg(display->spiCH, reg, data);
|
||||
}
|
||||
|
||||
void max7219_printLedMatrix(
|
||||
max7219_t *display,
|
||||
uint8_t matrix[])
|
||||
{
|
||||
uint8_t i = 0;
|
||||
|
||||
for(i = 0; i < 8; i ++) {
|
||||
spiWriteReg(display->spiCH, i+1, matrix[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void max7219_ledMatrixUnsetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col)
|
||||
{
|
||||
row = (row & 0x07) + 1;
|
||||
col = 1 << (col & 0x07);
|
||||
// TODO: find out how to turn off LED
|
||||
spiWriteReg(display->spiCH, row,col+1);
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
// daysichained matrix
|
||||
typedef struct{
|
||||
spi_ch_t *spiCH;
|
||||
uint8_t nDevices;
|
||||
}max7219_dm_t;
|
||||
|
||||
|
||||
void max7219_dm_write(
|
||||
max7219_dm_t *matrix,
|
||||
uint8_t **data)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t j = 0;
|
||||
// TODO: Test it out
|
||||
for(i = 0; i < 8; i++) {
|
||||
pinWrite(matrix->spiCH->pin, 0);
|
||||
for(j = 0; j < matrix->nDevices; j++) {
|
||||
spiTrx8BitPolling(matrix->spiCH->spi, i+1); // reg
|
||||
spiTrx8BitPolling(matrix->spiCH->spi, data[j][i]);
|
||||
}
|
||||
pinWrite(matrix->spiCH->pin, 1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -0,0 +1,160 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file max7219.h
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 25.08.2022
|
||||
* @version 1.0
|
||||
**************************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* **Detailed Description :**
|
||||
*
|
||||
* @todo
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _MAX7219_H_
|
||||
#define _MAX7219_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "spi.h"
|
||||
#include "pin.h"
|
||||
|
||||
typedef enum{
|
||||
NO_DECODE_DIGIT_7_TO_0 = 0x00,
|
||||
CODE_B_DECODE_ONLY_DIGIT_0 = 0x01,
|
||||
CODE_B_DECODE_ONLY_DIGIT_3_TO_0 = 0x0F,
|
||||
CODE_B_DECOD_DIGIT_7_TO_0 = 0xFF
|
||||
}max7219_decodeMode_t;
|
||||
|
||||
typedef enum{
|
||||
DISPLAY_DIGIT_0 = 0x00,
|
||||
DISPLAY_DIGIT_1_TO_0 = 0x01,
|
||||
DSIPLAX_DIGIT_2_TO_0 = 0x02,
|
||||
DSIPLAX_DIGIT_3_TO_0 = 0x03,
|
||||
DSIPLAX_DIGIT_4_TO_0 = 0x04,
|
||||
DSIPLAX_DIGIT_5_TO_0 = 0x05,
|
||||
DSIPLAX_DIGIT_6_TO_0 = 0x06,
|
||||
DSIPLAX_DIGIT_7_TO_0 = 0x07
|
||||
}max7219_scanLimit_t;
|
||||
|
||||
/**
|
||||
* @brief max7219 matrix class
|
||||
*
|
||||
* The matrix can be ccomposed of only one ore many devices daysichained
|
||||
* together.
|
||||
* @var *spiCH Pointer to spi channel object
|
||||
* @var nDevicesChained Ammount of devices hoocked up together
|
||||
* @var brightness brightness of the matrix
|
||||
* @var buf Array of matrix content. Each n to n + 7 (n can be 0, 8, 16, etc) represents the
|
||||
* content of one individual max7219 device.
|
||||
*/
|
||||
typedef struct{
|
||||
spi_ch_t *spiCH;
|
||||
uint8_t nDevicesChained;
|
||||
uint8_t brightness;
|
||||
uint8_t buf[];
|
||||
}max7219_mx_t;
|
||||
|
||||
void max7219_t_mx_init(
|
||||
max7219_mx_t *matrix,
|
||||
spi_ch_t *spiChannel,
|
||||
uint8_t *pBuf,
|
||||
uint8_t bufLen);
|
||||
|
||||
void max7219_mx_setPixel(
|
||||
max7219_mx_t *matrix,
|
||||
uint8_t row,
|
||||
uint8_t col,
|
||||
uint8_t logic);
|
||||
|
||||
void max7219_mx_print(
|
||||
max7219_mx_t *matrix,
|
||||
char *text,
|
||||
uint8_t len);
|
||||
|
||||
void max7219_mx_show(
|
||||
max7219_mx_t *matrix);
|
||||
|
||||
void max7219_mx_shutdown(
|
||||
max7219_mx_t *matrix);
|
||||
|
||||
void max7219_mx_test(
|
||||
max7219_mx_t *matrix);
|
||||
|
||||
void max7219_mx_mapMatrix(
|
||||
max7219_mx_t *matrix,
|
||||
uint8_t *map);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
|
||||
void max7219_init(
|
||||
max7219_t *display,
|
||||
spi_ch_t *spi_ch);
|
||||
|
||||
void max7219_testDisplay(
|
||||
max7219_t *display,
|
||||
uint8_t logic);
|
||||
|
||||
void max7219_shutdownDiaply(
|
||||
max7219_t *display,
|
||||
uint8_t logic);
|
||||
|
||||
void max7219_setDecodeMode(
|
||||
max7219_t *display,
|
||||
max7219_decodeMode_t dmode);
|
||||
|
||||
void max7219_setIntensity(
|
||||
max7219_t *display,
|
||||
uint8_t intensity);
|
||||
|
||||
void max7219_setScanLimit(
|
||||
max7219_t *display,
|
||||
max7219_scanLimit_t slimit);
|
||||
|
||||
void max7219_setAllLEDsOff(
|
||||
max7219_t *display);
|
||||
|
||||
void max7219_ledMatrixSetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col);
|
||||
|
||||
void max7219_rawWrite(
|
||||
max7219_t *display,
|
||||
uint8_t reg,
|
||||
uint8_t data);
|
||||
|
||||
void max7219_printLedMatrix(
|
||||
max7219_t *display,
|
||||
uint8_t matrix[]);
|
||||
|
||||
void max7219_ledMatrixUnsetLED(
|
||||
max7219_t *display,
|
||||
uint8_t row,
|
||||
uint8_t col);
|
||||
#if 0
|
||||
// daysichained matrix
|
||||
typedef struct{
|
||||
spi_ch_t *spiCH;
|
||||
uint8_t nDevices;
|
||||
}max7219_dm_t;
|
||||
|
||||
|
||||
void max7219_dm_write(
|
||||
max7219_dm_t *matrix,
|
||||
uint8_t **data);
|
||||
#endif
|
||||
*/
|
||||
#ifdef _cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _MAX7219_H_
|
@ -0,0 +1,45 @@
|
||||
add_library(SPI spi.c ${CSL_SOURCES}/imp_spi.c)
|
||||
target_compile_options(SPI PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(SPI PRIVATE ${C_DEFS})
|
||||
target_include_directories(SPI PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::spi ALIAS SPI)
|
||||
|
||||
add_library(DELAY ${CSL_SOURCES}/imp_delay.c)
|
||||
target_compile_options(DELAY PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(DELAY PRIVATE ${C_DEFS})
|
||||
target_include_directories(DELAY PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::delay ALIAS DELAY)
|
||||
|
||||
add_library(PIN ${CSL_SOURCES}/imp_pin.c)
|
||||
target_compile_options(PIN PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(PIN PRIVATE ${C_DEFS})
|
||||
target_include_directories(PIN PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::pin ALIAS PIN)
|
||||
|
||||
add_library(USART ${CSL_SOURCES}/imp_usart.c)
|
||||
target_compile_options(USART PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(USART PRIVATE ${C_DEFS})
|
||||
target_include_directories(USART PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::usart ALIAS USART)
|
||||
|
||||
# TODO: this point needs to be discussed. How should it work and what should it do?
|
||||
add_library(INIT ${CSL_SOURCES}/deviceSetup.c)
|
||||
target_compile_options(INIT PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(INIT PRIVATE ${C_DEFS})
|
||||
target_include_directories(INIT PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::init ALIAS INIT)
|
||||
|
||||
add_library(I2C i2c.c ${CSL_SOURCES}/imp_i2c.c)
|
||||
target_compile_options(I2C PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(I2C PRIVATE ${C_DEFS})
|
||||
target_include_directories(I2C PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::i2c ALIAS I2C)
|
||||
|
||||
add_library(TIMER ${CSL_SOURCES}/imp_timer.c)
|
||||
target_compile_options(TIMER PRIVATE ${C_FLAGS})
|
||||
target_compile_definitions(TIMER PRIVATE ${C_DEFS})
|
||||
target_include_directories(TIMER PUBLIC ${PERIFERALS_DIR} ${CSL_INCLUDES})
|
||||
add_library(sub::timer ALIAS TIMER)
|
||||
|
||||
|
||||
|
@ -0,0 +1,183 @@
|
||||
#include "i2c.h"
|
||||
|
||||
//uint32_t i2cCR1;
|
||||
//uint32_t i2cCR2;
|
||||
//uint32_t i2cISR;
|
||||
|
||||
void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object */
|
||||
i2cCh_t channelNo, /*!< The harware channel to be used */
|
||||
i2cMode_t mode, /*!< Master, Slave or Multymaster Modes */
|
||||
uint16_t mainAddress, /*!< First and Main address of the device */
|
||||
uint16_t secondAddress, /*!< Second address if dual addresse mode is configured */
|
||||
i2cAddressCount_t addressCount, /*!< Single or multiple */
|
||||
i2cAddressSize_t addressSize, /*!< 10 or 7 bit address size */
|
||||
i2cSpeed_t speed, /*!< Bus Speed */
|
||||
i2cOpperationMode_t opperationMode, /*!< Blocking or non blocking polling, Int or DMA */
|
||||
i2cClockStretching_t stretching, /*!< Clock Stretching enable onyl in slave mode */
|
||||
i2cWakeUpTypes_t wakeOn) /*!< Wake up condition */
|
||||
{
|
||||
i2cHardware->channelNo = channelNo;
|
||||
i2cHardware->mode = mode;
|
||||
i2cHardware->mainAddress = mainAddress;
|
||||
i2cHardware->secondAddress = secondAddress;
|
||||
i2cHardware->addressCount = addressCount;
|
||||
i2cHardware->addressSize = addressSize;
|
||||
i2cHardware->speed = speed;
|
||||
i2cHardware->opperationMode = opperationMode;
|
||||
i2cHardware->stretching = stretching;
|
||||
i2cHardware->wakeOn = wakeOn;
|
||||
i2cHardware->hardwareState = i2cHwIdle;
|
||||
i2cHardware->periferalState = i2cPerifNotInitialized;
|
||||
|
||||
i2cEnableHardware(i2cHardware);
|
||||
|
||||
i2cDisablePeriferal(i2cHardware);
|
||||
|
||||
i2cConfigureFilters(i2cHardware);
|
||||
|
||||
i2cSetClockStretch(i2cHardware, stretching);
|
||||
|
||||
i2cSetSpeed(i2cHardware, speed);
|
||||
|
||||
i2cSetMode(i2cHardware, opperationMode);
|
||||
|
||||
i2cSetAddressLenght(i2cHardware, addressSize);
|
||||
|
||||
i2cSetAddress(i2cHardware, mainAddress, secondAddress);
|
||||
|
||||
i2cEnablePeriferal(i2cHardware);
|
||||
|
||||
i2cHardware->periferalState = i2cPerifNotInitialized;
|
||||
}
|
||||
|
||||
|
||||
void i2cDeInit(i2c_t *i2cHardware)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void i2cRead(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght)
|
||||
{
|
||||
switch(i2cHardware->mode)
|
||||
{
|
||||
case i2cModeMaster:
|
||||
i2cMasterRecieve(i2cHardware, slaveAddress, registerAddress, data);
|
||||
break;
|
||||
case i2cModeSlave:
|
||||
break;
|
||||
case i2cModeMultyMaster:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// this function still doesn't implment 10 bit oopeartion TODO
|
||||
void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
|
||||
{
|
||||
switch(i2cHardware->opperationMode)
|
||||
{
|
||||
case i2cOpperationPolling :
|
||||
i2cMasterRecievePolling(i2cHardware, slaveAddress, registerAddress, data);
|
||||
break;
|
||||
case i2cOpperationInt:
|
||||
break;
|
||||
case i2cOpperationDma:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void i2cWrite(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght)
|
||||
{
|
||||
switch(i2cHardware->mode)
|
||||
{
|
||||
case i2cModeMaster:
|
||||
i2cMasterSend(i2cHardware, slaveAddress, registerAddress, data);
|
||||
break;
|
||||
case i2cModeSlave:
|
||||
break;
|
||||
case i2cModeMultyMaster:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void i2cMasterSend(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
|
||||
{
|
||||
switch(i2cHardware->opperationMode)
|
||||
{
|
||||
case i2cOpperationPolling :
|
||||
i2cMasterSendPolling(i2cHardware, slaveAddress, registerAddress, data);
|
||||
break;
|
||||
case i2cOpperationInt:
|
||||
break;
|
||||
case i2cOpperationDma:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void i2cMasterSendPolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
|
||||
{
|
||||
while(i2cHardware->periferalState != i2cPerifReady)
|
||||
{
|
||||
i2cIsPeriferalReady(i2cHardware);
|
||||
}
|
||||
|
||||
i2cHardware->periferalState = i2cPerifTransmitting;
|
||||
|
||||
i2cInitiateWriteCommand(i2cHardware);
|
||||
i2cSetTransferCounter(i2cHardware,2);
|
||||
i2cSendSlaveAddress(i2cHardware, slaveAddress);
|
||||
i2cSendRegisterAddress(i2cHardware,registerAddress);
|
||||
|
||||
i2cSendData(i2cHardware,data);
|
||||
|
||||
while(i2cHardware->periferalState != i2cPerifTransferComplete)
|
||||
{
|
||||
i2cIsTransferComplete(i2cHardware);
|
||||
}
|
||||
|
||||
i2cGenerateStop(i2cHardware);
|
||||
|
||||
i2cHardware->periferalState = i2cPerifReady;
|
||||
i2cHardware->hardwareState = i2cHwIdle;
|
||||
}
|
||||
|
||||
|
||||
void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
|
||||
{
|
||||
while(i2cHardware->periferalState != i2cPerifReady)
|
||||
{
|
||||
i2cIsPeriferalReady(i2cHardware);
|
||||
}
|
||||
|
||||
i2cHardware->periferalState = i2cPerifTransmitting;
|
||||
|
||||
i2cSetTransferCounter(i2cHardware,1);
|
||||
i2cInitiateWriteCommand(i2cHardware);
|
||||
i2cSendSlaveAddress(i2cHardware, slaveAddress);
|
||||
i2cSendRegisterAddress(i2cHardware,registerAddress);
|
||||
|
||||
while(i2cHardware->periferalState != i2cPerifTransferComplete)
|
||||
{
|
||||
i2cIsTransferComplete(i2cHardware);
|
||||
}
|
||||
|
||||
i2cInitiateReadCommand(i2cHardware);
|
||||
i2cSendSlaveAddress(i2cHardware, slaveAddress);
|
||||
|
||||
i2cHardware->periferalState = i2cPerifRecieving;
|
||||
|
||||
while(i2cHardware->hardwareState != i2cHwInputBufferFull)
|
||||
{
|
||||
i2cIsInputBufferEmpty(i2cHardware);
|
||||
}
|
||||
|
||||
i2cGenerateNack(i2cHardware);
|
||||
i2cGenerateStop(i2cHardware);
|
||||
|
||||
i2cReadInputRegister(i2cHardware, data);
|
||||
|
||||
i2cHardware->periferalState = i2cPerifReady;
|
||||
i2cHardware->hardwareState = i2cHwIdle;
|
||||
}
|
||||
|
@ -0,0 +1,552 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file i2c.h
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 18.07.2022
|
||||
* @version 1.01
|
||||
**************************************************************************************************
|
||||
* @brief I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
|
||||
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
|
||||
* This will also not have a I3C support for the forseable futrue.
|
||||
*
|
||||
* **Detailed Description :**
|
||||
*
|
||||
* I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
|
||||
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
|
||||
* This will also not have a I3C support for the forseable futrue.
|
||||
*
|
||||
* @todo
|
||||
* - 26.07.2021 : Implement the DMA
|
||||
* - 26.07.2021 : Implement the Interrupt
|
||||
* - 26.07.2021 : Implement the Sleep/WakeUp
|
||||
* - 26.07.2021 : Implement the Slave opperation
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _I2C_H_
|
||||
#define _I2C_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "hardwareDescription.h"
|
||||
#include "pin.h"
|
||||
|
||||
|
||||
|
||||
/*! Enum of possible I2C opperation modes */
|
||||
typedef enum{
|
||||
i2cModeMaster, /*!< Master mode : In Master mode, the I2C interface initiates
|
||||
a data transfer and generates the clock signal. **DEFAULT** */
|
||||
i2cModeMultyMaster, /*!< Multimaster Mode : In case if more than one master are present
|
||||
in one I2C bus. In such case each device needs to be able to
|
||||
cooperate knowing that another device could talking
|
||||
therefore the bus is busy (Arbirtation).
|
||||
For More information : https://www.i2c-bus.org/multimaster/ */
|
||||
i2cModeSlave /*!< Slave mode : A slave device has to always be ready to detect and
|
||||
process a start condition and to recognize its address */
|
||||
}i2cMode_t;
|
||||
|
||||
/*! Enum of possible I2C speeds */
|
||||
typedef enum{
|
||||
i2cSpeedStandart, /*!< SM 100 kbits/s Standart i2c Speed **DEFAULT** */
|
||||
i2cSpeedFast, /*!< FM 400 kbits/s */
|
||||
i2cSpeedFastPlus, /*!< FM+ 1 Mbits/s */
|
||||
i2cSpeedHightSpeed, /*!< HS 3.4 Mbits/s */
|
||||
i2cSpeedUltraFast /*!< UFM 5 Mbits/s */
|
||||
}i2cSpeed_t;
|
||||
|
||||
/*! Enum of possible I2C Adress sizes */
|
||||
typedef enum{
|
||||
i2cAddressSizeSevenBits, /*!< 7 Bits address size **DEFAULT** */
|
||||
i2cAddressSizeTenBits /*!< 10 Bits address size */
|
||||
}i2cAddressSize_t;
|
||||
|
||||
/*! Enum of possible I2C Address count */
|
||||
typedef enum{
|
||||
i2cAddressCountSingle, /*!< Only one address for communication **DEFAULT** */
|
||||
i2cAddressCountDual /*!< Dual addresses for one device respondng to two addresses */
|
||||
}i2cAddressCount_t;
|
||||
|
||||
/*! Enum for clock strechning activation. Can only be implmented as Slave
|
||||
* for more information : https://www.i2c-bus.org/clock-stretching/ */
|
||||
typedef enum{
|
||||
i2cClockStretchingDisable, /*!< We assume that the master and slave have compatible
|
||||
Clock frequencies **DEFAULT** */
|
||||
i2cClockStretchingEnable /*!< In situations where an I2C slave is not able to co-operate
|
||||
with the clock speed given by the master and needs to slow down.
|
||||
This is done by a mechanism referred to as clock stretching. */
|
||||
}i2cClockStretching_t;
|
||||
|
||||
/*! Enum for diffenrent wake up methodes wehnin sleep mode */
|
||||
typedef enum{
|
||||
i2cWakeUpDisabled, /*!< No wake up is possible this is the default mode also means that
|
||||
the sleep function is not implmentes **DEFAULT** */
|
||||
i2cWakeUpOnAddressMatch /*!< Wakes up on address match, this can be harware dependent */
|
||||
}i2cWakeUpTypes_t;
|
||||
|
||||
/*! Enum operation mode of the i2c channel */
|
||||
typedef enum{
|
||||
i2cOpperationPolling, /*!< Polling mode Blocking, i2c communication states are constantly
|
||||
checked on polling mode so it can be stoped
|
||||
when interrupts occures **DEFAULT** */
|
||||
i2cOpperationInt, /*!< Interrut Mode Non-blocking, i2c communications state changes
|
||||
generates an interrupt and can be handeled this way */
|
||||
i2cOpperationDma /*!< DMA Mode Non-blocking, I2C communication satets are managed
|
||||
via Inteerupts and with Direct Memory Access */
|
||||
}i2cOpperationMode_t;
|
||||
|
||||
/*! typedef for the i2c states*/
|
||||
typedef enum
|
||||
{
|
||||
i2cHwIdle, /*!< Hardware is in Idle Mode **DEFAULT** */
|
||||
i2cHwStartGenerated, /*!< Generated the star condition */
|
||||
i2cHwOutputBufferFull, /*!< The output buffer of the I2C Periferal is Full */
|
||||
i2cHwOutputBufferEmpty, /*!< The output buffer of the I2C Periferal is Empty */
|
||||
i2cHwInputBufferFull, /*!< The input buffer of the I2C Periferal Is Full */
|
||||
i2cHwInputBufferEmpty, /*!< The input buffer of the I2C Periferal Is Empty */
|
||||
i2cHwSentRead, /*!< Sent read request */
|
||||
i2cHwSentWrite, /*!< Sent write request */
|
||||
i2cHwGotAck, /*!< Recieved ACK */
|
||||
i2cHwGotNack, /*!< Recieved NACK */
|
||||
i2cHwSentAck, /*!< Sent ACK */
|
||||
i2cHwSentNack, /*!< Sent NACK */
|
||||
i2cHwStopGenerated, /*!< Generated the star condition */
|
||||
i2cHwError /*!< Error */
|
||||
} i2cHardwareState_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
i2cPerifNotInitialized, /*!< Default Peripheral is not yet Initialized **DEFAULT** */
|
||||
i2cPerifInitialized, /*!< I2C CHannle is initilized but not neceserly ready */
|
||||
i2cPerifSlaveAddressSent, /*!< The Salve Address Was Sent to the bus */
|
||||
i2cPerifTransferComplete, /*!< A full communication process is complete (wire or Read) */
|
||||
i2cPerifTransferOngoign, /*!< A full communication process is in progress (wire or Read) */
|
||||
i2cPerifReady, /*!< Peripheral Initialized and ready for use */
|
||||
i2cPerifSlaveNotFound, /*!< Desired Slave was not able to be found */
|
||||
i2cPerifTransmitting, /*!< Data Transmission process is ongoing */
|
||||
i2cPerifRecieving, /*!< Data Reception process is ongoing */
|
||||
i2cPerifListening, /*!< Address Listen Mode is ongoing */
|
||||
i2cPerifListeningAndTransmitting, /*!< Address Listen Mode and ongoing Data Transmission */
|
||||
i2cPerifListeningAndRecieveing, /*!< Address Listen Mode and ongoing Data Reception */
|
||||
i2cPerifAbort, /*!< Abort user request ongoing */
|
||||
i2cPerifTimeout, /*!< Timeout state */
|
||||
i2cPerifError /*!< Error */
|
||||
} i2cPeriferalState_t;
|
||||
|
||||
/*! Struture a an i2c channel with all the required propereties*/
|
||||
typedef struct i2c_t
|
||||
{
|
||||
i2cCh_t channelNo; /*!< The harware channel to be used */
|
||||
i2cMode_t mode; /*!< Master, Slave or Multymaster Modes */
|
||||
uint16_t mainAddress; /*!< First and Main address of the device */
|
||||
uint16_t secondAddress; /*!< Second address if dual addresse mode is configured */
|
||||
i2cAddressCount_t addressCount; /*!< Single or multiple */
|
||||
i2cAddressSize_t addressSize; /*!< 10 or 7 bit address size */
|
||||
i2cSpeed_t speed; /*!< Bus Speed */
|
||||
i2cOpperationMode_t opperationMode; /*!< Blocking or non blocking polling, Int or DMA */
|
||||
i2cClockStretching_t stretching; /*!< Clock Stretching enable onyl in slave mode */
|
||||
i2cWakeUpTypes_t wakeOn; /*!< Define on which type of action the i2c channel should
|
||||
wake up. Only if de prefiral goes to sleep */
|
||||
i2cHardwareState_t hardwareState; /*!< The current sitate of the I2C Bus */
|
||||
i2cPeriferalState_t periferalState; /*!< The current sitate of the I2C Bus */
|
||||
}i2c_t;
|
||||
|
||||
/***************************************************************************************************
|
||||
I2C Configuration functions
|
||||
***************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Initilize the I2C Hardware
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object */
|
||||
i2cCh_t channelNo, /*!< The harware channel to be used */
|
||||
i2cMode_t mode, /*!< Master, Slave or Multymaster Modes */
|
||||
uint16_t mainAddress, /*!< First and Main address of the device */
|
||||
uint16_t secondAddress, /*!< Second address if dual addresse mode is configured */
|
||||
i2cAddressCount_t addressCount, /*!< Single or multiple */
|
||||
i2cAddressSize_t addressSize, /*!< 10 or 7 bit address size */
|
||||
i2cSpeed_t speed, /*!< Bus Speed */
|
||||
i2cOpperationMode_t opperationMode, /*!< Blocking or non blocking polling, Int or DMA */
|
||||
i2cClockStretching_t stretching, /*!< Clock Stretching enable onyl in slave mode */
|
||||
i2cWakeUpTypes_t wakeOn /*!< Wake up condition */
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief De-Initilize the I2C Hardware
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cDeInit(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Set the i2c channle to the gievn mode
|
||||
* @param Channel is the i2c hardware channel
|
||||
* @param mode The mode for i2c : Master Slave or Multymaster
|
||||
*/
|
||||
void i2cSetMode(i2c_t *i2cHardware, i2cMode_t mode);
|
||||
|
||||
/**
|
||||
* @brief Set the i2c channles mode as Master Slave or Multymaster
|
||||
* @param i2cHardware is the i2c hardware channel
|
||||
* @param addressOne The forst address for the device
|
||||
* @param addressTwo The second address for the device only if dual address mode is not defined
|
||||
*/
|
||||
void i2cSetAddress(i2c_t *i2cHardware, uint16_t mainAddress, uint16_t secondAddress);
|
||||
|
||||
/**
|
||||
* @brief Set the i2c Address Lenght, 7 bit or 8 bit, Master or slave doesn't make any difference
|
||||
* @param i2cHardware is the i2c hardware channel
|
||||
* @param size Is the Adress isze to be used 7 Bit or 10 Bit
|
||||
*/
|
||||
void i2cSetAddressLenght(i2c_t *i2cHardware, i2cAddressSize_t size);
|
||||
|
||||
/**
|
||||
* @brief Stets the Communication speed
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param speed the different awailable speeds
|
||||
*/
|
||||
void i2cSetSpeed(i2c_t *i2cHardware, i2cSpeed_t speed);
|
||||
|
||||
/**
|
||||
* @brief Initiates the opperation mode for the selected i2c Channel
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param opperation define if the i2c channel will opperate on pollin, Interrupt, or DMA modes
|
||||
*/
|
||||
void i2cSetOpperation(i2c_t *i2cHardware, i2cOpperationMode_t opperation);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Ebales or disables clock stretching functionalities
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param The value to be written in the register. This will heavily depend on the hardware
|
||||
*/
|
||||
void i2cSetClockStretch(i2c_t *i2cHardware, i2cClockStretching_t stretching);
|
||||
|
||||
/**
|
||||
* @brief Set the wakeup mode
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param wake the desider wakeup mode for now i have found only one mode.
|
||||
*/
|
||||
void i2cSetWakeup(i2c_t *i2cHardware, i2cWakeUpTypes_t wake);
|
||||
|
||||
/**
|
||||
* @brief Configures Hardware implmente filters if there are any.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cConfigureFilters(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Set the timeout to close the i2c wait time if a communication fails.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param timeout the desider timeout duration in ticks.
|
||||
*/
|
||||
void i2cSetTimeout(i2c_t *i2cHardware, uint8_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Resets the i2c Periferal to it's inital state.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cPeriferalReset(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Resets the i2c Harware and register to it's factory deflauts.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cHardwareReset(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Enables I2C Hardware BUS & Clock & Pins
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cEnableHardware(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Enables I2C Periferal core.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cEnablePeriferal(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Disables I2C Periferal core.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cDisablePeriferal(i2c_t *i2cHardware);
|
||||
|
||||
/**************************************************************************************************
|
||||
I2C Communication functions independen of opperating mode
|
||||
***************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Writes the given amount of data to the selected device. This function will
|
||||
* automaticaly choose between i2cMasterSend(); or i2cSlaveSend();
|
||||
* Depending if the I2C channel was initiated as slave or master.
|
||||
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data to be written
|
||||
* @param dataLenght is the total quantity of 8 bit data to be written.
|
||||
*/
|
||||
void i2cWrite(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght);
|
||||
|
||||
/**
|
||||
* @brief Reads the given amount of data to the selected device. This function will
|
||||
* automaticaly choose between i2cMasterRecieve(); or i2cSlaveRecieve();
|
||||
* Depending if the I2C channel was initiated as slave or master.
|
||||
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data which has been red
|
||||
* @paran dataLenght is the total quantity of 8 bit data to be red
|
||||
*/
|
||||
void i2cRead(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght);
|
||||
|
||||
/**
|
||||
* @brief Recieve a Single Byte as master from from the given devices register.
|
||||
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data whic has been red
|
||||
*/
|
||||
void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
/**
|
||||
* @brief Sends a Single Byte as master to the given devices register.
|
||||
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the send command will be sent
|
||||
* @param registerAddress is the regiter to be written to the device
|
||||
* @param data is the data to be sent
|
||||
*/
|
||||
void i2cMasterSend(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief Recieve a Single Byte as slave from given devices register.
|
||||
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data whic has been red
|
||||
*/
|
||||
void i2cSlaveRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief Recieve a Single Byte as slave from the given devices register.
|
||||
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data whic has been red
|
||||
*/
|
||||
void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
|
||||
/**************************************************************************************************
|
||||
I2C Hardware functions
|
||||
***************************************************************************************************/
|
||||
/**
|
||||
* @brief Checks if the device is ready for any type of communication
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cIsPeriferalReady(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Generates a Start condition.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cGenerateStart(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Generates a Start condition.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cGenerateStop(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Generates a NACK condition.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cGenerateNack(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Generates a ACK condition.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cGenerateAck(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Initiates the communication protocl by sending the slave address on the bus and waits
|
||||
* for an ACK (aknowledge)
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress The address of the slave to be communicated
|
||||
*/
|
||||
void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress);
|
||||
|
||||
/**
|
||||
* @brief Sende the register adrres with which we want to communicate.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param registerAddress The register address of the slave device that we are communication with
|
||||
*/
|
||||
void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t *registerAddress);
|
||||
|
||||
/**
|
||||
* @brief Send the register that we want to read or write.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param registerAddress the register that need to be accessed
|
||||
*/
|
||||
void i2cSendData(i2c_t *i2cHardware, uint8_t *registerAddress);
|
||||
|
||||
/**
|
||||
* @brief Initiates a Write command with the previously set slave address.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cInitiateWriteCommand(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Initiates a read command with the previously set slave address.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cInitiateReadCommand(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Is the output buffer empty. This allso meas that the data that was in the ouput buffer
|
||||
* is sent to the i2c BUS.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cIsOutputBufferEmpty(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Is the output buffer empty. This allso meas that the data that was in the ouput buffer
|
||||
* is sent to the i2c BUS.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cIsInputBufferEmpty(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief reads the I2C input buffer
|
||||
* is sent to the i2c BUS.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param data pointer to the data that need to be read and returned
|
||||
*/
|
||||
void i2cReadInputRegister(i2c_t *i2cHardware, uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief writes to the I2C output buffer
|
||||
* is sent to the i2c BUS.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param data pointer to the data that need to be read and returned
|
||||
*/
|
||||
void i2cSetOutputRegister(i2c_t *i2cHardware, uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief Checks is transfer is complete
|
||||
* is sent to the i2c BUS.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param data pointer to the data that need to be read and returned
|
||||
*/
|
||||
void i2cIsTransferComplete(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Defines the amount of transfers to be made. Address exchange and start conditon does not count
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param count amount o data to be transfered.
|
||||
*/
|
||||
void i2cSetTransferCounter(i2c_t *i2cHardware, uint8_t count);
|
||||
/**************************************************************************************************
|
||||
I2C Communication functions Polling / Blocking Mode
|
||||
***************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Recieve a Single Byte in polling mode as master from from the given devices register.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data whic has been red
|
||||
*/
|
||||
void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
/**
|
||||
* @brief Sends a Single Byte in polling mode as master to the given devices register.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the send command will be sent
|
||||
* @param registerAddress is the regiter to be written to the device
|
||||
* @param data is the data to be sent
|
||||
*/
|
||||
void i2cMasterSendPolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief Recieve a Single Byte in polling mide as slave from the given devices register.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data whic has been red
|
||||
*/
|
||||
void i2cSlaveRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief Recieve a Single Byte in polling mode as slave from the given devices register.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
* @param registerAddress is the regiter to be red from the device
|
||||
* @param data is the data whic has been red
|
||||
*/
|
||||
void i2cSlaveSendPolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data);
|
||||
|
||||
/**************************************************************************************************
|
||||
I2C Arbitration Functions for Multymaster mode and slaves clock stretching
|
||||
***************************************************************************************************/
|
||||
void i2cClockSynchronise(); // I2C Standart : Clock Syncronization
|
||||
void i2cAbortTransmit(); // I2c Standart : Stop Communication for multimaster mode
|
||||
void i2cArbitration(); // I2C Standart : Arbitration for multimaster mode to define the right master.
|
||||
void i2cSoftReset(); // I2C Standart : Software reset not supported by all hardware.
|
||||
void i2cBusClear(); // I2C Standart : in case if SCL is stuck
|
||||
|
||||
|
||||
/**************************************************************************************************
|
||||
I2C Extra functions that are not esential for the main functionality
|
||||
***************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief This function will scan every for possible addresses to discover devices on the bus.
|
||||
* And write them to the Devices list given to him. This function will not discover more
|
||||
* devices than what he is told.
|
||||
* @param i2cHardware 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 i2cDiscoverDevices(i2c_t *i2cHardware, uint16_t *devices, uint8_t deviceCount);
|
||||
|
||||
/**
|
||||
* @brief This function will try to communicate with a device with every speed
|
||||
* allowed by his hardware to find out the maximum communication sppeed possible.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to which the read command will be sent
|
||||
*/
|
||||
i2cSpeed_t i2cTestDeviceSpeed(i2c_t *i2cHardware, uint16_t *slaveAddress);
|
||||
|
||||
/**
|
||||
* @brief This function will read the device info register (if awailable) as follows :
|
||||
* I2c Standart : 3 Bytes (24 bits) | 12 Bits : Manufacturer info | 9 Bits: Part Identification | 3 Bits DIE Rev.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
* @param slaveAddress is the address of the device to be red
|
||||
*/
|
||||
uint32_t i2cReadDeviceInfo(i2c_t *i2cHardware, uint16_t *slaveAddress);
|
||||
|
||||
/**
|
||||
* @brief The selected i2c channel is put on sleep.
|
||||
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
|
||||
*/
|
||||
void i2cSleep(i2c_t *i2cHardware);
|
||||
|
||||
/**
|
||||
* @brief Error handling.
|
||||
* @param error The error no generated.
|
||||
*/
|
||||
void i2cThrowError(int16_t error); // Defined by me : Private error function for error handling
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _I2C_H_
|
@ -0,0 +1,11 @@
|
||||
#ifndef _INTERRUPT_H_
|
||||
#define _INTERRUPT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
@ -0,0 +1,219 @@
|
||||
#include "spi.h"
|
||||
|
||||
// generic implementation of spi channel class
|
||||
|
||||
void spiInitMaster(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_clkPol_t clockPolarity,
|
||||
spi_phase_t phase,
|
||||
spi_framef_t frameFormat,
|
||||
spi_comMode_t comMode,
|
||||
uint32_t prescaler,
|
||||
pinNo_t clkPin,
|
||||
uint16_t altFuncClkPin,
|
||||
pinNo_t MOSIPin,
|
||||
uint16_t altFuncMOSIPin,
|
||||
pinNo_t MISOPin,
|
||||
uint16_t altFuncMISOPin)
|
||||
{
|
||||
// GPIO setup
|
||||
pinInit(clkPin);
|
||||
pinInit(MOSIPin);
|
||||
pinInit(MISOPin);
|
||||
pinConfig(clkPin, alternate, pushPull, output, veryFast);
|
||||
pinConfig(MISOPin, alternate, floating, input , veryFast);
|
||||
pinConfig(MOSIPin, alternate, pushPull, output, veryFast);
|
||||
pinSetAlternate(clkPin, altFuncClkPin);
|
||||
pinSetAlternate(MOSIPin, altFuncMOSIPin);
|
||||
pinSetAlternate(MISOPin, altFuncMISOPin);
|
||||
|
||||
// SPI setup
|
||||
spiEnableBus(spi_hw_ch);
|
||||
spiSetPolarity(spi_hw_ch,clockPolarity);
|
||||
spiSetPhase(spi_hw_ch,phase);
|
||||
spiSetMode(spi_hw_ch, SPI_MASTER);
|
||||
spiSetClockPrescaler(spi_hw_ch, prescaler);
|
||||
spiSetFrameFormat(spi_hw_ch,frameFormat);
|
||||
spiSetSoftwareSlaveManagement(spi_hw_ch,1);
|
||||
spiSetInternalSlaveSelect(spi_hw_ch,0);
|
||||
spiSetComMode(spi_hw_ch, comMode);
|
||||
spiSetClockPrescaler(spi_hw_ch, prescaler);
|
||||
spiSetBitFrameLength(spi_hw_ch, SPI_FRAME_LENGTH_8BIT);
|
||||
}
|
||||
|
||||
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin)
|
||||
{
|
||||
ch->pin = chipselectPin;
|
||||
ch->spi = spi_hw_ch;
|
||||
pinWrite(chipselectPin, 0);
|
||||
}
|
||||
|
||||
uint8_t spiReadReg(spi_ch_t *spi_ch, uint8_t reg_address) {
|
||||
uint8_t buf;
|
||||
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of target register
|
||||
spiTrx8BitPolling(spi_ch->spi, reg_address);
|
||||
|
||||
// read from target register
|
||||
buf = spiTrx8BitPolling(spi_ch->spi,0x00);
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void spiAutoReadBlock(spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
uint8_t* buffer,
|
||||
uint8_t buf_len) {
|
||||
uint8_t i = 0;
|
||||
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of starting register
|
||||
spiTrx8BitPolling(spi_ch->spi, start_address);
|
||||
|
||||
// read block from device
|
||||
for(;i < buf_len;i++) {
|
||||
buffer[i] = spiTrx8BitPolling(spi_ch->spi, 0x00);
|
||||
}
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
void spiWriteReg(spi_ch_t *spi_ch,
|
||||
uint8_t reg_address,
|
||||
uint8_t data) {
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of target register
|
||||
spiTrx8BitPolling(spi_ch->spi, reg_address);
|
||||
|
||||
// write to target register
|
||||
spiTrx8BitPolling(spi_ch->spi, data);
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
void spiWriteBlock(spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
const uint8_t *data,
|
||||
uint8_t data_len) {
|
||||
uint8_t i = 0;
|
||||
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of starting register
|
||||
spiTrx8BitPolling(spi_ch->spi, start_address);
|
||||
|
||||
// read block from device
|
||||
for(;i < data_len;i++) {
|
||||
spiTrx8BitPolling(spi_ch->spi, data[i]);
|
||||
}
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
void spiWrite8bit(spi_ch_t *spi_ch,
|
||||
uint8_t bits)
|
||||
{
|
||||
pinWrite(spi_ch->pin,0);
|
||||
spiTrx8BitPolling(spi_ch->spi,bits);
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
uint8_t spiReadWrite8bit(spi_ch_t *spi_ch,
|
||||
uint8_t bits)
|
||||
{
|
||||
uint8_t buf;
|
||||
pinWrite(spi_ch->pin,0);
|
||||
buf = spiTrx8BitPolling(spi_ch->spi,bits);
|
||||
pinWrite(spi_ch->pin,1);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void spiWrite16bit(spi_ch_t *spi_ch,
|
||||
uint16_t bits)
|
||||
{
|
||||
pinWrite(spi_ch->pin,0);
|
||||
if(spiGetFrameFormat(spi_ch->spi) == SPI_MSB_FIRST) {
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
} else {
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8));
|
||||
}
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
uint16_t spiReadWrite16bit(spi_ch_t *spi_ch,
|
||||
uint16_t bits)
|
||||
{
|
||||
uint16_t buf;
|
||||
|
||||
pinWrite(spi_ch->pin,0);
|
||||
if(spiGetFrameFormat(spi_ch->spi) == SPI_LSB_FIRST) {
|
||||
buf = spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8)) << 8;
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
} else {
|
||||
buf = spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8)) << 8;
|
||||
}
|
||||
pinWrite(spi_ch->pin,1);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void spiWrite32bit(spi_ch_t *spi_ch,
|
||||
uint32_t bits)
|
||||
{
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
if(spiGetFrameFormat(spi_ch->spi) == SPI_LSB_FIRST) {
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 24));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 16));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
} else {
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 16));
|
||||
spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 24));
|
||||
}
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
uint32_t spiReadWrite32bit(spi_ch_t *spi_ch,
|
||||
uint8_t bits)
|
||||
{
|
||||
uint32_t buf;
|
||||
|
||||
pinWrite(spi_ch->pin,0);
|
||||
if(spiGetFrameFormat(spi_ch->spi) == SPI_LSB_FIRST) {
|
||||
buf = spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 24)) << 24;
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 16)) << 16;
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8)) << 8;
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
} else {
|
||||
buf = spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits));
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 8)) >> 8;
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 16)) >> 16;
|
||||
buf |= spiTrx8BitPolling(spi_ch->spi,(uint8_t)(bits >> 24)) >> 24;
|
||||
}
|
||||
pinWrite(spi_ch->pin,1);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,390 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file spi.h
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 12.03.2022
|
||||
* @version 1.0
|
||||
**************************************************************************************************
|
||||
* @brief This is the genral interface for spi.
|
||||
*
|
||||
* **Detailed Description :**
|
||||
* This the spi interface and belongs to the interface layer.
|
||||
*
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _SPI_H_
|
||||
#define _SPI_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "hardwareDescription.h"
|
||||
#include "pin.h"
|
||||
|
||||
// TODO: when everything worksmove this into imp.spi.c
|
||||
//#include "hardwareDescription.h"
|
||||
//#define SPI_BASE ((SPI_TypeDef *)spiBase_Addr_List[spi_hw_ch])
|
||||
|
||||
/*! Enum of possible States*/
|
||||
typedef enum{
|
||||
SPI_SLAVE,
|
||||
SPI_MASTER
|
||||
} spi_mode_t;
|
||||
|
||||
/* Enum of clock polarities*/
|
||||
typedef enum{
|
||||
SPI_NONINVERTED,
|
||||
SPI_INVERTED
|
||||
}spi_clkPol_t;
|
||||
|
||||
/*! Enum of phases*/
|
||||
typedef enum{
|
||||
SPI_CAPTURE_ON_FIRST_CLK_TRANSITION,
|
||||
SPI_CAPTURE_ON_SECCOND_CLK_TRANSITION
|
||||
} spi_phase_t;
|
||||
|
||||
/*! Enum of frame formats*/
|
||||
typedef enum{
|
||||
SPI_MSB_FIRST,
|
||||
SPI_LSB_FIRST
|
||||
}spi_framef_t;
|
||||
|
||||
/*! Enum of frame lenghts*/
|
||||
typedef enum{
|
||||
SPI_FRAME_LENGTH_8BIT,
|
||||
SPI_FRAME_LENGTH_16BIT
|
||||
}spi_framel_t;
|
||||
|
||||
/*! Enum of communication mode*/
|
||||
typedef enum{
|
||||
SPI_DOUPLEX,
|
||||
SPI_SIMPLEX
|
||||
}spi_comMode_t;
|
||||
|
||||
/*! \brief SPI cannel class
|
||||
* This class cpntains the pin and spi channel number
|
||||
* select.
|
||||
* */
|
||||
typedef struct{
|
||||
pinNo_t pin; /*!< pin number */
|
||||
spiCH_t spi; /*!< spi hardware channel number */
|
||||
}spi_ch_t;
|
||||
|
||||
typedef uint8_t (*readReg_t) (uint8_t);
|
||||
|
||||
// generic code
|
||||
|
||||
/** TODO: setup init with all attributes
|
||||
* TODO: setup auto pin set alternate function
|
||||
* @brief This is the spi hardware channel class
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
*/
|
||||
void spiInitMaster(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_clkPol_t clockPolarity,
|
||||
spi_phase_t phase,
|
||||
spi_framef_t frameFormat,
|
||||
spi_comMode_t comMode,
|
||||
uint32_t prescaler,
|
||||
pinNo_t clkPin,
|
||||
uint16_t altFuncClkPin,
|
||||
pinNo_t MOSIPin,
|
||||
uint16_t altFuncMOSIPin,
|
||||
pinNo_t MISOPin,
|
||||
uint16_t altFuncMISOPin);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Set up SPI channel
|
||||
* Set up a SPI channel by passig a hardware SPI channel and a chipselect pin.
|
||||
* The chipselect pin will be set to high (chipselect is lowactive).
|
||||
* \param *ch pointer to SPI channel
|
||||
* \param spi_hw_ch SPI hardware channel
|
||||
* \param chipslectPin designated pin for chipslect
|
||||
*/
|
||||
void spiSetupCH(
|
||||
spi_ch_t *ch,
|
||||
spiCH_t spi_hw_ch,
|
||||
pinNo_t chipselectPin);
|
||||
|
||||
/**
|
||||
* \brief Read register
|
||||
* Read one byte from a one register with one byte address.
|
||||
* \param *spi_ch spi pointer to spi channel object
|
||||
* \param reg_address register address
|
||||
* \return register content
|
||||
*/
|
||||
uint8_t spiReadReg(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t reg_address);
|
||||
|
||||
/**
|
||||
* \brief Read Block
|
||||
* Read a block of data starting at a given start address.
|
||||
* This function makes use of the auto register increment of the device to be read from.
|
||||
* The address will be sent once and then data is read.
|
||||
* \param *spi_ch pointer to spi cannel object
|
||||
* \param start_address start address to the first register
|
||||
* \param *buffer pointer to the buffer in which the read content is written into
|
||||
* \param buf_len length of buffer
|
||||
*/
|
||||
void spiAutoReadBlock(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
uint8_t* buffer,
|
||||
uint8_t buf_len);
|
||||
|
||||
/**
|
||||
* \brief Write register
|
||||
* Write one byte to one register with one byte address.
|
||||
* \param *spi_ch pointer to spi channel object
|
||||
* \param reg_address register address
|
||||
* \param data data byte to be written into register
|
||||
*
|
||||
*/
|
||||
void spiWriteReg(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t reg_address,
|
||||
uint8_t data);
|
||||
/**
|
||||
* \brief Write data block
|
||||
* Write a block of data starting at a given start address.
|
||||
* This function makes use of the auto register increment of the device to be written to. The
|
||||
* address will be sent once an then data is written.
|
||||
* \param *spi_ch pointer to spi channel object
|
||||
* \param start_address start address of the first reister
|
||||
* \param *data pointer to data to be written
|
||||
* \param data_len length of data to be written
|
||||
*/
|
||||
void spiWriteBlock(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
const uint8_t *data,
|
||||
uint8_t data_len);
|
||||
|
||||
|
||||
/**
|
||||
* \brief write 8 bits
|
||||
* \param bits 8 bits
|
||||
*/
|
||||
void spiWrite8bit(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t bits);
|
||||
|
||||
/**
|
||||
* \brief read and write simultainously 8 bits
|
||||
* \param bits 8 bits
|
||||
* \return 8 bits
|
||||
*/
|
||||
uint8_t spiReadWrite8bit(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t bits);
|
||||
|
||||
/**
|
||||
* \brief write 16 bits
|
||||
* \param bits 16 bits
|
||||
*/
|
||||
void spiWrite16bit(
|
||||
spi_ch_t *spi_ch,
|
||||
uint16_t bits);
|
||||
|
||||
/**
|
||||
* \brief read and write simultainously 16 bits
|
||||
* \param bits 16 bits
|
||||
* \return 16 bits
|
||||
*/
|
||||
uint16_t spiReadWrite16bit(
|
||||
spi_ch_t *spi_ch,
|
||||
uint16_t bits);
|
||||
|
||||
/**
|
||||
* \brief write 32 bits
|
||||
* \param bits 32 bits
|
||||
*/
|
||||
void spiWrite32bit(
|
||||
spi_ch_t *spi_ch,
|
||||
uint32_t bits);
|
||||
|
||||
/**
|
||||
* \brief read and write simultainously 32 bits
|
||||
* \param bits 32 bits
|
||||
* \return 32 bits
|
||||
*/
|
||||
uint32_t spiReadWrite32bit(
|
||||
spi_ch_t *spi_ch,
|
||||
uint8_t bits);
|
||||
|
||||
// implementation
|
||||
|
||||
/**
|
||||
* @brief SPI hardware peripheral reset
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
*/
|
||||
void spiReset(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Enable Bus for SPI
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
*/
|
||||
void spiEnableBus(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Enable SPI hardware channel
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
*/
|
||||
void spiEnable(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Dissable SPI hardware channel
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
*/
|
||||
void spiDissable(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Set SPI operation mode (MASTER or SLAVE)
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param mode
|
||||
*/
|
||||
void spiSetMode(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_mode_t mode);
|
||||
|
||||
/**
|
||||
* @brief Set SPI clock polarity
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param clkPol Clock polarity
|
||||
*/
|
||||
void spiSetPolarity(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_clkPol_t clkPol);
|
||||
|
||||
/**
|
||||
* @breif Get SPI polarity
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @return polarity
|
||||
*/
|
||||
spi_clkPol_t spiGetPolarity(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Set SPI clock phase
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param phase
|
||||
*/
|
||||
void spiSetPhase(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_phase_t phase);
|
||||
|
||||
/**
|
||||
* @brief Get SPI clock phase
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @return phase
|
||||
*/
|
||||
spi_phase_t spiGetPhase(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Set frame length
|
||||
* one can choose between 4/8/16 bits. For devices that not support a given frame length an error
|
||||
* will be generated.
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param framel Frame length
|
||||
*/
|
||||
void spiSetBitFrameLength(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_framel_t framel);
|
||||
|
||||
/**
|
||||
* @brief Set SPI frame format
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param framef Frame format
|
||||
*/
|
||||
void spiSetFrameFormat(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_framef_t framef);
|
||||
/**
|
||||
* @brief Get SPI frame format
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @return Frame format
|
||||
*/
|
||||
spi_framef_t spiGetFrameFormat(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
/**
|
||||
* @brief Set Clock Prescaler
|
||||
* This is dependent on your target device. Please enter in the correct value.
|
||||
* The entered Value will be masked with the maximal number of bits (truncated)
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param clkDiv
|
||||
*/
|
||||
void spiSetClockPrescaler(
|
||||
spiCH_t spi_hw_ch,
|
||||
uint32_t clkDiv);
|
||||
|
||||
/**
|
||||
* @brief Set communication Mode
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param comMode
|
||||
*/
|
||||
void spiSetComMode(
|
||||
spiCH_t spi_hw_ch,
|
||||
spi_comMode_t comMode);
|
||||
|
||||
/**
|
||||
* @brief Get Clock Prescaler
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @return prescaler
|
||||
*/
|
||||
uint32_t spiGetClockPrescaler(
|
||||
spiCH_t spi_hw_ch);
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* @brief Set software slave management
|
||||
* @param logic 1 = enabled, 0 = dissabled
|
||||
*/
|
||||
void spiSetSoftwareSlaveManagement(spiCH_t spi_hw_ch, uint8_t logic);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Enable software slave management
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param logic Slave management done by software... 1 = enables / 0 = dissabled
|
||||
*/
|
||||
void spiSetSoftwareSlaveManagement(
|
||||
spiCH_t spi_hw_ch,
|
||||
uint8_t logic);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Enable internal slave select
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param logic 1 = enable / 0 = dissable
|
||||
*/
|
||||
void spiSetInternalSlaveSelect(
|
||||
spiCH_t spi_hw_ch,
|
||||
uint8_t logic);
|
||||
|
||||
/*!
|
||||
* @brief Transmits and receives one byte of data in polling mode
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param tx_data 'tx_data' The byte to be transmitted"
|
||||
* @return The received data
|
||||
*/
|
||||
uint8_t spiTrx8BitPolling(
|
||||
spiCH_t spi_hw_ch,
|
||||
uint8_t tx_data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _SPI_H_
|
||||
|
@ -0,0 +1,191 @@
|
||||
#include "main.h"
|
||||
#include "delay.h"
|
||||
#include "deviceSetup.h"
|
||||
#include "usart.h"
|
||||
#include "ascii.h"
|
||||
#include "timer.h"
|
||||
#include "i2c.h"
|
||||
|
||||
void timer_test(timerNo_t timer, pinNo_t pin)
|
||||
{
|
||||
|
||||
timerInitCounter(timer, 4000, 999, downCounting);
|
||||
timerSart(timer);
|
||||
|
||||
for(int i = 0; i < 10 ; i++)
|
||||
{
|
||||
while(!timerGetUpdateInterrupt(timer));
|
||||
timerClearUpdateInterrupt(timer);
|
||||
pinToggle(pin);
|
||||
}
|
||||
pinWrite(pin,0);
|
||||
}
|
||||
|
||||
void timer_capture_compare_test(timerNo_t timer)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
timerInitCounter(timer, 100, 99, downCounting);
|
||||
// We use pin PA3 (Arduino header A2)
|
||||
//timerInitOutputCompare(timer, toggle, 4, pinA3, 2,0, 300);
|
||||
timerInitOutputCompare(timer, pwm_normal, 2, pinB3, 2,0, 900);
|
||||
timerSart(timer);
|
||||
|
||||
while(1){
|
||||
delayMs(200);
|
||||
timerSetCounterCompareValue(timer, 2, i);
|
||||
i += 10;
|
||||
if(i>99) i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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|");
|
||||
print_Usart(usart2, "\n\r");
|
||||
print_Usart(usart2, "Bits |");
|
||||
for(i=0; i < 32; i++)
|
||||
{
|
||||
pt = (toPrint >> i) & 1;
|
||||
usartSendChar(usart2, ' ');
|
||||
usartSendChar(usart2, pt + 0x30);
|
||||
usartSendChar(usart2, '|');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
print_Usart(usart2, "Bit Pos |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|");
|
||||
print_Usart(usart2, "\n\r");
|
||||
print_Usart(usart2, "Bits |");
|
||||
for(i=0; i < 32; i++)
|
||||
{
|
||||
pt = (toPrint >> (31-i)) & 1;
|
||||
usartSendChar(usart2, ' ');
|
||||
usartSendChar(usart2, pt + 0x30);
|
||||
usartSendChar(usart2, '|');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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|");
|
||||
print_Usart(usart2, "\n\r");
|
||||
print_Usart(usart2, "Bits |");
|
||||
for(i=0; i < 8; i++)
|
||||
{
|
||||
pt = (toPrint >> i) & 1;
|
||||
usartSendChar(usart2, ' ');
|
||||
usartSendChar(usart2, pt + 0x30);
|
||||
usartSendChar(usart2, '|');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
print_Usart(usart2, "Bit Pos | 7| 6| 5| 4| 3| 2| 1| 0|");
|
||||
print_Usart(usart2, "\n\r");
|
||||
print_Usart(usart2, "Bits |");
|
||||
for(i=0; i < 8; i++)
|
||||
{
|
||||
pt = (toPrint >> (7-i)) & 1;
|
||||
usartSendChar(usart2, ' ');
|
||||
usartSendChar(usart2, pt + 0x30);
|
||||
usartSendChar(usart2, '|');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
uint8_t i = 0;
|
||||
uint16_t slaveAddress = 0xC0;
|
||||
uint8_t registerToRead = 0x00;
|
||||
uint8_t i2cRecieved = 0;
|
||||
uint8_t i2cToWrite = 0xFF;
|
||||
uint8_t i2cDataLenght = 1;
|
||||
i2c_t i2c_1;
|
||||
|
||||
// making array with all available timers
|
||||
//timerNo_t timers[MAX_TIMER_CHANNEL_COUNT] = {timer_1, timer_2, timer_3, timer_14, timer_16, timer_17};
|
||||
|
||||
delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library
|
||||
|
||||
pinConfig(pinB3, output, pushPull, def_res, def_speed);
|
||||
pinConfig(pinA0, input, def_stage, pullDown, def_speed);
|
||||
|
||||
setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main.
|
||||
|
||||
usartInit( usart2,
|
||||
pinA2,
|
||||
pinA15,
|
||||
115200,
|
||||
eight,
|
||||
NO_PARITY_CTRL,
|
||||
noFlowControl);
|
||||
|
||||
//clears screen and send the wellcome messgae
|
||||
print_Usart(usart2, ASCII_clear);
|
||||
print_Usart(usart2, "Hello to our KED project\n\r");
|
||||
|
||||
|
||||
//blinks 10 times to indicate the sicsessfull init if the device
|
||||
for(i = 0 ; i < 2 ; i++) {
|
||||
delayMs(100);
|
||||
pinToggle(pinB3);
|
||||
delayMs(100);
|
||||
}
|
||||
|
||||
pinWrite(pinB3,0);
|
||||
|
||||
|
||||
print_Usart(usart2, "\n\r");
|
||||
|
||||
i2cInit(&i2c_1, I2C_CH_1, i2cModeMaster, 0x00,0x00, i2cAddressCountSingle, i2cAddressSizeSevenBits, i2cSpeedStandart, i2cOpperationPolling, i2cClockStretchingDisable, i2cWakeUpDisabled);
|
||||
/*
|
||||
i2cRead(&i2c_1, &slaveAddress, ®isterToRead, &i2cRecieved, &i2cDataLenght);
|
||||
printBinary8(i2cRecieved,0);
|
||||
|
||||
print_Usart(usart2, "\n\r");
|
||||
|
||||
registerToRead += 1;
|
||||
|
||||
i2cRead(&i2c_1, &slaveAddress, ®isterToRead, &i2cRecieved, &i2cDataLengh);
|
||||
printBinary8(i2cRecieved,0);
|
||||
*/
|
||||
|
||||
registerToRead = 1;
|
||||
i2cToWrite = 0xFF;
|
||||
print_Usart(usart2, "\n\r");
|
||||
//i2cWrite(&i2c_1, &slaveAddress, ®isterToRead, &i2cToWrite, &i2cDataLenght);
|
||||
printBinary32(i2cCR2,0);
|
||||
|
||||
print_Usart(usart2, "\n\r");
|
||||
i2cRead(&i2c_1, &slaveAddress, ®isterToRead, &i2cRecieved,&i2cDataLenght);
|
||||
|
||||
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);
|
||||
pinToggle(pinB3);
|
||||
delayMs(100);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MAIN_H */
|
@ -0,0 +1,244 @@
|
||||
#include "main.h"
|
||||
#include "delay.h"
|
||||
#include "deviceSetup.h"
|
||||
#include "usart.h"
|
||||
#include "ascii.h"
|
||||
#include "timer.h"
|
||||
#include "spi.h"
|
||||
#include "max7219.h"
|
||||
|
||||
unsigned char A[] = {0b00000000,0b00111100,0b01100110,0b01100110,0b01111110,0b01100110,0b01100110,0b01100110};
|
||||
unsigned char B[] = {0b01111000,0b01001000,0b01001000,0b01110000,0b01001000,0b01000100,0b01000100,0b01111100};
|
||||
unsigned char C[] = {0b00000000,0b00011110,0b00100000,0b01000000,0b01000000,0b01000000,0b00100000,0b00011110};
|
||||
unsigned char D[] = {0b00000000,0b00111000,0b00100100,0b00100010,0b00100010,0b00100100,0b00111000,0b00000000};
|
||||
unsigned char E[] = {0b00000000,0b00111100,0b00100000,0b00111000,0b00100000,0b00100000,0b00111100,0b00000000};
|
||||
unsigned char F[] = {0b00000000,0b00111100,0b00100000,0b00111000,0b00100000,0b00100000,0b00100000,0b00000000};
|
||||
unsigned char G[] = {0b00000000,0b00111110,0b00100000,0b00100000,0b00101110,0b00100010,0b00111110,0b00000000};
|
||||
unsigned char H[] = {0b00000000,0b00100100,0b00100100,0b00111100,0b00100100,0b00100100,0b00100100,0b00000000};
|
||||
unsigned char I[] = {0b00000000,0b00111000,0b00010000,0b00010000,0b00010000,0b00010000,0b00111000,0b00000000};
|
||||
unsigned char J[] = {0b00000000,0b00011100,0b00001000,0b00001000,0b00001000,0b00101000,0b00111000,0b00000000};
|
||||
unsigned char K[] = {0b00000000,0b00100100,0b00101000,0b00110000,0b00101000,0b00100100,0b00100100,0b00000000};
|
||||
unsigned char L[] = {0b00000000,0b00100000,0b00100000,0b00100000,0b00100000,0b00100000,0b00111100,0b00000000};
|
||||
unsigned char M[] = {0b00000000,0b00000000,0b01000100,0b10101010,0b10010010,0b10000010,0b10000010,0b00000000};
|
||||
unsigned char N[] = {0b00000000,0b00100010,0b00110010,0b00101010,0b00100110,0b00100010,0b00000000,0b00000000};
|
||||
unsigned char O[] = {0b00000000,0b00111100,0b01000010,0b01000010,0b01000010,0b01000010,0b00111100,0b00000000};
|
||||
unsigned char P[] = {0b00000000,0b00111000,0b00100100,0b00100100,0b00111000,0b00100000,0b00100000,0b00000000};
|
||||
unsigned char Q[] = {0b00000000,0b00111100,0b01000010,0b01000010,0b01000010,0b01000110,0b00111110,0b00000001};
|
||||
unsigned char R[] = {0b00000000,0b00111000,0b00100100,0b00100100,0b00111000,0b00100100,0b00100100,0b00000000};
|
||||
unsigned char S[] = {0b00000000,0b00111100,0b00100000,0b00111100,0b00000100,0b00000100,0b00111100,0b00000000};
|
||||
unsigned char T[] = {0b00000000,0b01111100,0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,0b00000000};
|
||||
unsigned char U[] = {0b00000000,0b01000010,0b01000010,0b01000010,0b01000010,0b00100100,0b00011000,0b00000000};
|
||||
unsigned char V[] = {0b00000000,0b00100010,0b00100010,0b00100010,0b00010100,0b00010100,0b00001000,0b00000000};
|
||||
unsigned char W[] = {0b00000000,0b10000010,0b10010010,0b01010100,0b01010100,0b00101000,0b00000000,0b00000000};
|
||||
unsigned char X[] = {0b00000000,0b01000010,0b00100100,0b00011000,0b00011000,0b00100100,0b01000010,0b00000000};
|
||||
unsigned char Y[] = {0b00000000,0b01000100,0b00101000,0b00010000,0b00010000,0b00010000,0b00010000,0b00000000};
|
||||
unsigned char Z[] = {0b00000000,0b00111100,0b00000100,0b00001000,0b00010000,0b00100000,0b00111100,0b00000000};
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t j = 0;
|
||||
|
||||
uint8_t f[] = {
|
||||
0x00,
|
||||
0x7E,
|
||||
0x40,
|
||||
0x40,
|
||||
0x3E,
|
||||
0x40,
|
||||
0x40,
|
||||
0x40
|
||||
};
|
||||
|
||||
uint8_t matrix[] = {
|
||||
0xAA,
|
||||
0xFF,
|
||||
0xAA,
|
||||
0xFF,
|
||||
0xAA,
|
||||
0xFF,
|
||||
0xAA,
|
||||
0xFF};
|
||||
|
||||
uint8_t matrix_1[] = {
|
||||
0x11,
|
||||
0x11,
|
||||
0x11,
|
||||
0x11,
|
||||
0x11,
|
||||
0x11,
|
||||
0x11,
|
||||
0x11};
|
||||
spi_ch_t spi_test_channel;
|
||||
max7219_t ledMatrix;
|
||||
|
||||
// making array with all available timers
|
||||
delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library
|
||||
|
||||
pinConfig(pinB3, output, pushPull, output, def_speed);
|
||||
pinConfig(pinA0, input, def_stage, pullDown, def_speed);
|
||||
|
||||
setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main.
|
||||
|
||||
|
||||
usartInit( usart2,
|
||||
pinA2,
|
||||
pinA15,
|
||||
115200,
|
||||
eight,
|
||||
NO_PARITY_CTRL,
|
||||
noFlowControl);
|
||||
|
||||
//clears screen and send the wellcome messgae
|
||||
print_Usart(usart2, ASCII_clear);
|
||||
print_Usart(usart2, "HEllooo to our KED project\n\r");
|
||||
|
||||
|
||||
for(i = 0 ; i < 5 ; i++) {
|
||||
delayMs(100);
|
||||
pinToggle(pinB3);
|
||||
delayMs(100);
|
||||
}
|
||||
|
||||
pinWrite(pinB3,0);
|
||||
|
||||
pinInit(pinA3);
|
||||
|
||||
//pinConfig(pinA6, alternate, floating, pullDown, veryFast);
|
||||
pinConfig(pinA3, output, pushPull, output, veryFast);
|
||||
|
||||
// spi hardware channel setup
|
||||
spiInitMaster(SPI_CH_1,
|
||||
SPI_NONINVERTED,
|
||||
SPI_CAPTURE_ON_FIRST_CLK_TRANSITION,
|
||||
SPI_MSB_FIRST,
|
||||
SPI_DOUPLEX,
|
||||
7,
|
||||
pinA5,
|
||||
0,
|
||||
pinA7,
|
||||
0,
|
||||
pinA6,
|
||||
0);
|
||||
|
||||
spiSetupCH(&spi_test_channel, SPI_CH_1, pinA3);
|
||||
|
||||
// LED Matrix object setup
|
||||
//ledMatrix.spiCH = &spi_test_channel;
|
||||
max7219_init(&ledMatrix, &spi_test_channel);
|
||||
|
||||
spiEnable(SPI_CH_1);
|
||||
/*
|
||||
* MAX7219 hoockup for this example
|
||||
*
|
||||
* Signal pin nucleo
|
||||
* --------------------------
|
||||
* MOSI pinA7 A6
|
||||
* MISO pinA6 A5
|
||||
* CLK pinA5 A4
|
||||
* CS pinA4 A3
|
||||
*/
|
||||
|
||||
|
||||
for(i = 0 ; i < 10 ; i++) {
|
||||
delayMs(50);
|
||||
pinToggle(pinB3);
|
||||
delayMs(50);
|
||||
}
|
||||
|
||||
max7219_testDisplay(&ledMatrix,1);
|
||||
delayMs(500);
|
||||
max7219_testDisplay(&ledMatrix,0);
|
||||
max7219_shutdownDiaply(&ledMatrix,0);
|
||||
max7219_setDecodeMode(&ledMatrix, NO_DECODE_DIGIT_7_TO_0);
|
||||
max7219_setScanLimit(&ledMatrix, DSIPLAX_DIGIT_7_TO_0);
|
||||
max7219_setIntensity(&ledMatrix,0x01);
|
||||
|
||||
max7219_ledMatrixSetLED(&ledMatrix,0,0);
|
||||
delayMs(1000);
|
||||
max7219_setAllLEDsOff(&ledMatrix);
|
||||
/*
|
||||
for(i=0; i < 8; i++) {
|
||||
max7219_ledMatrixSetLED(&ledMatrix,i,i);
|
||||
delayMs(100);
|
||||
}
|
||||
*/
|
||||
|
||||
while(1) {
|
||||
max7219_printLedMatrix(&ledMatrix, F);
|
||||
//max7219_ledMatrixSetLED(&ledMatrix, 0, 0);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, A);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, T);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, I);
|
||||
delayMs(300);
|
||||
max7219_setAllLEDsOff(&ledMatrix);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, G);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, E);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, N);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, T);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, I);
|
||||
delayMs(300);
|
||||
max7219_setAllLEDsOff(&ledMatrix);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, E);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, D);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, W);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, I);
|
||||
delayMs(300);
|
||||
max7219_printLedMatrix(&ledMatrix, N);
|
||||
delayMs(300);
|
||||
max7219_setAllLEDsOff(&ledMatrix);
|
||||
delayMs(300);
|
||||
}
|
||||
|
||||
while(1) {
|
||||
for(i=0; i < 0x8; i++) {
|
||||
max7219_ledMatrixSetLED(&ledMatrix,i,i+j);
|
||||
delayMs(100);
|
||||
}
|
||||
//delayMs(10);
|
||||
j++;
|
||||
if(j > 7) j =0;
|
||||
}
|
||||
|
||||
/*
|
||||
while(1){
|
||||
pinToggle(pinB3);
|
||||
dispTest(&ledMatrix);
|
||||
//max7219_test_display(&spi_test_channel);
|
||||
}
|
||||
*/
|
||||
for(i = 0 ; i < 100 ; i++) {
|
||||
pinWrite(pinB3, 1);
|
||||
//pinWrite(pinA4,1);
|
||||
delayMs(100);
|
||||
pinWrite(pinB3, 0);
|
||||
//pinWrite(pinA4,0);
|
||||
delayMs(900);
|
||||
}
|
||||
|
||||
|
||||
//timer_capture_compare_test(timer_2);
|
||||
//print_Usart(usart2, "All is working fine \r\n");
|
||||
|
||||
while(1)
|
||||
{
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MAIN_H */
|
@ -1,90 +0,0 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file i2c.h
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 18.07.2022
|
||||
* @version 1.0
|
||||
**************************************************************************************************
|
||||
* @brief I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
|
||||
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
|
||||
* This will also not have a I3C support for the forseable futrue.
|
||||
*
|
||||
* **Detailed Description :**
|
||||
*
|
||||
* I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
|
||||
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
|
||||
* This will also not have a I3C support for the forseable futrue.
|
||||
*
|
||||
* @todo
|
||||
* - 18.07.2021 : Implement the i2c.c.
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _I2C_H_
|
||||
#define _I2C_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define I2C_STATE_RESET 1 // Not Initialized
|
||||
#define I2C_STATE_READY 2 // Ready
|
||||
#define I2C_STATE_TX 4 // Transmitting
|
||||
#define I2C_STATE_RX 5 // Receiving
|
||||
#define I2C_STATE_LISTEN 6 // Listening
|
||||
#define I2C_STATE_ABORT 7 // Aborted by user
|
||||
#define I2C_STATE_TIMEOUT 8 // Timeout
|
||||
#define I2C_STATE_ERROR 9 // Error happened
|
||||
|
||||
#define I2C_SPEED_STANDART 1 // Sm 100 kbits/s This mode will be choosen for the constructor.
|
||||
#define I2C_SPEED_FAST 2 // Fm 400 kbits/s
|
||||
#define I2C_SPEED_FAST_PLUS 3 // Fm+ 1 Mbits/s
|
||||
#define I2C_SPEED_HIGH_SPEED 4 // Hs 3.4 Mbits/s
|
||||
#define I2C_SPEED_ULTRA_FAST 5 // UFm 5 Mbits/s
|
||||
|
||||
#define I2C_ADDRESS_7B 1 // 7 bits addressing mode
|
||||
#define I2C_ADDRESS_10B 2 // 10 bits addressing mode
|
||||
|
||||
#define I2C_MODE_MASTER 1 // Single Master Mode
|
||||
#define I2C_MODE_SLAVE 2 // Slave Mode
|
||||
#define I2C_MODE_MULTI_MASTER 3 // Multy Master Mode
|
||||
|
||||
/* Creator from the CPP Version PLease keep it to define the main init sequence.*/
|
||||
//ll_i2c(uint16_t address, uint8_t channel, uint8_t mode, uint8_t adressMode); // Creat i2c abject witha agiven channel address & mode speed is by default the slowest.
|
||||
|
||||
void i2cRead(uint8_t *reg, uint8_t *buffer, uint8_t ®Lenght, uint8_t &bufferLenght); // Defined by me : Read a given number of bytes
|
||||
void i2cWrite(uint8_t *reg, uint8_t *data, uint8_t ®Lenght, uint8_t &dataLenght); // Defined by me : Send a given number of bytes
|
||||
|
||||
uint8_t i2cTestDeviceSpeed(); // Defined by me : Cycle trough different modes until device cnat't answer fast enought
|
||||
uint8_t i2cDiscoverDevices(); // Defined by me : Scan the awailable address range on standart mode to find devices
|
||||
|
||||
void i2cInitChannelAsMaster(); // Hardware Specific : Initilise the hardware channel in master mode
|
||||
void i2cInitChannelAsSlave(); // Hardware Specific : Initilise the hardware channel in slavic mode (@life of boris)
|
||||
void i2cFreeChannel(); // Hardware Specific : Free the hardware channel for othe recousrces
|
||||
|
||||
void i2cClockSynchronise();// I2C Standart : Clock Syncronization
|
||||
void i2cReadDeviceInfo(); // I2c Standart : 3 Bytes (24 bits) | 12 Bits : Manufacturer info | 9 Bits: Part Identification | 3 Bits DIE Rev.
|
||||
void i2cAbortTransmit(); // I2c Standart : Stop Communication for multimaster mode
|
||||
void i2cClockStretch(); // I2C Standart : Optional For Pausing Communication because treatement takes longer than the communication
|
||||
void i2cArbitration(); // I2C Standart : Arbitration for multimaster mode to define the right master.
|
||||
void i2cSoftReset(); // I2C Standart : Software reset not supported by all hardware.
|
||||
void i2cBusClear(); // I2C Standart : in case if SCL is stuck
|
||||
|
||||
void i2cSetSpeed(uint8_t speed); // I2C Standart
|
||||
void i2cSetAddress(uint16_t &address); // I2C Standart
|
||||
void i2cSetAddressMode(); // I2C Standart
|
||||
|
||||
void i2cSetTimeout(uint8_t &timeout); // Hardware specific
|
||||
void i2cSetInterrupt(); // Hardware Specific
|
||||
void i2cSetDma(); // Hardware specific
|
||||
|
||||
void i2cThrowError(int16_t error); // Defined by me : Private error function for error handling
|
||||
void i2cPointReg(uint8_t *reg); // Defined by me : Points to the register to be red from
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _I2C_H_
|
@ -1,45 +0,0 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @file spi.h
|
||||
* @author Kerem Yollu & Edwin Koch
|
||||
* @date 12.03.2022
|
||||
* @version 1.0
|
||||
**************************************************************************************************
|
||||
* @brief This is the genral interface for spi.
|
||||
*
|
||||
* **Detailed Description :**
|
||||
* This the spi interface and belongs to the interface layer.
|
||||
*
|
||||
**************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _SPI_H_
|
||||
#define _SPI_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hardwareDescription.h"
|
||||
|
||||
/** This is the spi hardware channel class*/
|
||||
|
||||
void spi_init(spiCH_t spi_hw_ch);
|
||||
|
||||
|
||||
/*!
|
||||
* @brief Transmits and receives on byte of data
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param tx_data 'tx_data' The byte to be transmitted"
|
||||
* @return The received data
|
||||
*/
|
||||
uint8_t spi_trx(spiCH_t spi_hw_ch, uint8_t tx_data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _SPI_H_
|
||||
|
@ -1,25 +0,0 @@
|
||||
#ifndef _SPI_HPP_
|
||||
#define _SPI_HPP_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
template <typename Derived>
|
||||
struct SPI
|
||||
{
|
||||
uint8_t trx_u8(const uint8_t& data)
|
||||
{
|
||||
return static_cast<Derived*>(this)->trx_u8Impl(data);
|
||||
}
|
||||
|
||||
void tx(const uint8_t& data)
|
||||
{
|
||||
static_cast<Derived*>(this)->txImpl(data);
|
||||
}
|
||||
|
||||
uint8_t rx()
|
||||
{
|
||||
return static_cast<Derived*>(this)->rxImpl();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _SPI_HPP_
|
@ -1,82 +0,0 @@
|
||||
|
||||
|
||||
#ifndef _SPI_CH_H
|
||||
#define _SPI_CH_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "pin.h"
|
||||
#include "spi.h"
|
||||
#include <stdint.h>
|
||||
|
||||
// https://stackoverflow.com/questions/17052443/c-function-inside-struct
|
||||
typedef uint8_t (*readReg_t) (uint8_t);
|
||||
|
||||
|
||||
/*! \brief SPI cannel class
|
||||
* This class cpntains the pin and spi channel number
|
||||
* select.
|
||||
* */
|
||||
typedef struct{
|
||||
pinNo_t pin, /*!< pin number */
|
||||
spiCH_t spi /*!< spi hardware channel number */
|
||||
}spi_ch_t;
|
||||
|
||||
/**
|
||||
* \brief Read register
|
||||
* Read one byte from a one register with one byte address.
|
||||
* \param *spi_ch spi pointer to spi channel object
|
||||
* \param reg_address register address
|
||||
* \return register content
|
||||
*/
|
||||
uint8_t spiCH_readReg(spi_ch_t *spi_ch,
|
||||
uint8_t reg_address);
|
||||
|
||||
/**
|
||||
* \brief Read Block
|
||||
* Read a block of data starting at a given start address.
|
||||
* This function makes use of the auto register increment of the device to be read from.
|
||||
* The address will be sent once and then data is read.
|
||||
* \param *spi_ch pointer to spi cannel object
|
||||
* \param start_address start address to the first register
|
||||
* \param *buffer pointer to the buffer in which the read content is written into
|
||||
* \param buf_len length of buffer
|
||||
*/
|
||||
void spiCH_autoReadBlock(spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
uint8_t* buffer,
|
||||
uint8_t buf_len);
|
||||
|
||||
/**
|
||||
* \brief Write register
|
||||
* Write one byte to one register with one byte address.
|
||||
* \param *spi_ch pointer to spi channel object
|
||||
* \param reg_address register address
|
||||
* \param data data byte to be written into register
|
||||
*
|
||||
*/
|
||||
void spiCH_writeReg(spi_ch_t *spi_ch,
|
||||
uint8_t reg_address,
|
||||
uint8_t data);
|
||||
/**
|
||||
* \brief Write data block
|
||||
* Write a block of data starting at a given start address.
|
||||
* This function makes use of the auto register increment of the device to be written to. The
|
||||
* address will be sent once an then data is written.
|
||||
* \param *spi_ch pointer to spi channel object
|
||||
* \param start_address start address of the first reister
|
||||
* \param *data pointer to data to be written
|
||||
* \param data_len length of data to be written
|
||||
*/
|
||||
void spiCH_writeBlock(spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
const uint8_t *data,
|
||||
uint8_t data_len);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //_SPI_CH_H
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue