In this exmple we write/read one single byte to both eeproms one complete operation at a time for each memory. For each eeprom we declare its corresponding channel, job and sequence, notice we set two different external device each of them corresponding to one eeprom, with its respective chip selects, but both using the same LPSPI peripheral.
Mcu
Mcu:
McuClockSettingConfig: "McuClockSettingConfig_0"
...
McuPeripheralClockConfig: "McuPeripheralClockConfig_7" # enable SPI clock with FIRC clock
Peripheral Clock under MCU control: true
Mcu Peripheral Name: LPSPI0
Enable peripheral: true
Peripheral clock selection: FIRC
Peripheral Clock Frequency: 2.4E7
...
Port
Port:
...
PortContainer: "PortContainer_0"
General:
PortNumberOfPortPins: 5
PortPin: "EEPROM_MOSI" # port as SPI MOSI
PortPin Pcr: 36
PortPin Mode: LPSPI0_SOUT
...
PortPin: "EEPROM_MISO"
PortPin Pcr: 35 # port as SPI MISO
PortPin Mode: LPSPI0_SIN
...
PortPin: "EEPROM_CLK"
PortPin Pcr: 34 # port as SPI Clock
PortPin Mode: LPSPI0_SCK
...
PortPin: "EEPROM_CS"
PortPin Pcr: 37 # port as SPI Chip select
PortPin Mode: LPSPI0_PCS0
...
PortPin: "EEPROM_CS1"
PortPin Pcr: 32 # port as scond SPI Chip select
PortPin Mode: LPSPI0_PCS1
...
Spi
Spi:
General:
Post Build Varian Used: true
Config Variant: VarianPostBuild
...
SpiGeneral:
SpiDevErrorDetect: false
SpiChannelBuffersAllowed: 1 # only use external buffer
SpiLevelDelivered: 0
SpiPhyUnit: "SpiPhyUnit_0"
General:
SpiPhyUnitMapping: LPSPI_0 # set the LPSPI module to use
SpiPhyUnitMode: SPI_MASTER # master mode
SpiPhyUnitSync: true # only synchronus comunication
SpiPinConfiguration: 0
SpiPhyUnitClockRef: /Mcu/Mcu/McuModuleConfiguration/McuClockSettingConfig_0/McuClockReferencePoint_0
...
SpiExternalDevice: "SpiExternalDevice_0"
General:
SpiBaudrate: 100000
SpiCsIdentifier: PCS0
SpiCsPolarity: LOW
SpiCsSelection: CS_VIA_PERIPHERAL_ENGINE
SpiDataShiftEdge: TRAILING
SpiEnableCs: true
SpiHwUnit: CSIB0
SpiShiftClockIdleLevel: LOW
...
SpiExternalDevice: "SpiExternalDevice_1"
General:
SpiBaudrate: 100000
SpiCsIdentifier: PCS1
SpiCsPolarity: LOW
SpiCsSelection: CS_VIA_PERIPHERAL_ENGINE
SpiDataShiftEdge: TRAILING
SpiEnableCs: true
SpiHwUnit: CSIB0
SpiShiftClockIdleLevel: LOW
...
SpiChannel: "SpiChannel_0"
General:
SpiChannelType: EB
SpiDataWidth: 8
SpiTransferStart: MSB
...
SpiChannel: "SpiChannel_1"
General:
SpiChannelType: EB
SpiDataWidth: 8
SpiTransferStart: MSB
...
SpiJob: "SpiJob_0"
General:
SpiDeviceAssignment: /Spi/Spi/SpiDriver/SpiExternalDevice_0
...
SpiChannelList:
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_0
...
SpiJob: "SpiJob_1"
General:
SpiDeviceAssignment: /Spi/Spi/SpiDriver/SpiExternalDevice_1
...
SpiChannelList:
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_1
...
SpiSequence: "SpiSequence_0"
General:
...
SpiJobAssignment:
Index 0: /Spi/Spi/SpiDriver/SpiJob_0
SpiSequence: "SpiSequence_1"
General:
...
SpiJobAssignment:
Index 0: /Spi/Spi/SpiDriver/SpiJob_1
main.c
#include "Mcu.h"
#include "Port.h"
#include "Dio.h"
#include "Platform.h"
#include "Osif.h"
#include "Spi.h"
#include "SEGGER_RTT.h"
void EcuM_Init( void );
/*this is dummy delay function prepare just for this example, in a real application
no delay shall be used*/
void Delay( uint32 ms )
{
uint32 Timeout = OsIf_MicrosToTicks( ms * 1000u, OSIF_COUNTER_SYSTEM );
uint32 SeedTick = OsIf_GetCounter( OSIF_COUNTER_SYSTEM );
uint32 ElapsedTime = 0u;
do
{
ElapsedTime += OsIf_GetElapsed( &SeedTick, OSIF_COUNTER_SYSTEM );
}while( ElapsedTime < Timeout );
}
int main( void )
{
Spi_DataBufferType TxBuffer[4];
Spi_DataBufferType RxBuffer[4];
EcuM_Init();
SEGGER_RTT_Init();
/*Enable writing instructions in the eeprom memory by sending a 0x06*/
TxBuffer[0] = 0x06; /*WREN Instruction value*/
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_0, TxBuffer, NULL, 1 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_0 );
/*Send to write the value 0x35 in the address 0 of the memory*/
TxBuffer[0] = 0x02; /*Write instruction */
TxBuffer[1] = 0x00; /*16 bit - address */
TxBuffer[2] = 0x00; /*16 bit - address */
TxBuffer[3] = 0x37; /*Data byte */
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_0, TxBuffer, NULL, 4 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_0 );
/*Wait for the data to be recorded in memory, it is not the best way to
do it, so it's just for demonstration purposes*/
Delay( 5 );
/*Read a single byte from direccion 0x00 of eeprom memory, we need to transmit first
the read instruction plus the address to read*/
TxBuffer[0] = 0x03; /*Read instruction */
TxBuffer[1] = 0x00; /*16 bit - address */
TxBuffer[2] = 0x00; /*16 bit - address */
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_0, TxBuffer, RxBuffer, 4 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_0 );
/*Display data read it*/
SEGGER_RTT_printf(0, "Data read from eeprom 1: 0x%X\n", RxBuffer[3] );
/*Enable writing instructions in the eeprom memory by sending a 0x06*/
TxBuffer[0] = 0x06; /*WREN Instruction value*/
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_1, TxBuffer, NULL, 1 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_1 );
/*Send to write the value 0x35 in the address 0 of the memory*/
TxBuffer[0] = 0x02; /*Write instruction */
TxBuffer[1] = 0x00; /*16 bit - address */
TxBuffer[2] = 0x00; /*16 bit - address */
TxBuffer[3] = 0x38; /*Data byte */
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_1, TxBuffer, NULL, 4 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_1 );
/*Wait for the data to be recorded in memory, it is not the best way to
do it, so it's just for demonstration purposes*/
Delay( 5 );
/*Read a single byte from direccion 0x00 of eeprom memory, we need to transmit first
the read instruction plus the address to read*/
TxBuffer[0] = 0x03; /*Read instruction */
TxBuffer[1] = 0x00; /*16 bit - address */
TxBuffer[2] = 0x00; /*16 bit - address */
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_1, TxBuffer, RxBuffer, 4 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_1 );
/*Display data read it*/
SEGGER_RTT_printf(0, "Data read from eeprom 2: 0x%X\n", RxBuffer[3] );
while( 1u )
{
Delay( 10u );
}
return 0u;
}
void EcuM_Init( void )
{
Mcu_Init( &Mcu_Config );
Mcu_InitClock( McuClockSettingConfig_0 );
Mcu_SetMode( McuModeSettingConf_0 );
/*Init the internal tick reference Systick Timer*/
OsIf_Init( NULL_PTR );
/*enable and setup interrupts*/
Platform_Init( NULL_PTR );
/*Apply all the Pin Port microcontroller configuration*/
Port_Init( &Port_Config );
/*init the LPSPI0 with the paramters set in Tresos*/
Spi_Init( &Spi_Config );
}