The example is almost identical to the second one, we write four bytes then we read them, the difference is the way we set the channels, assigning one for eeprom commands, one for Address in 16 bits long, and the channels for the data, we use jobs to set the channels in the desire order and then we create sequences for writing and reading, please notice the use of NULL pointer when is no necessary to specific buffer for Tx or Rx
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 # SPI configuration at 100KHz
SpiCsIdentifier: PCS0 # Control pin CS 0
SpiCsPolarity: LOW
SpiCsSelection: CS_VIA_PERIPHERAL_ENGINE # CS under peripheral control
SpiDataShiftEdge: TRAILING # sampling on falling edge
SpiEnableCs: true
SpiHwUnit: CSIB0
SpiShiftClockIdleLevel: LOW # clock idle in Low level
...
SpiExternalDevice: "SpiExternalDevice_1"
General:
SpiBaudrate: 100000 # SPI configuration at 100KHz
SpiCsIdentifier: PCS1 # Control pin CS 1
SpiCsPolarity: LOW
SpiCsSelection: CS_VIA_PERIPHERAL_ENGINE # CS under peripheral control
SpiDataShiftEdge: TRAILING # sampling on falling edge
SpiEnableCs: true
SpiHwUnit: CSIB0
SpiShiftClockIdleLevel: LOW # clock idle in Low level
...
SpiChannel: "SpiChannel_Wren"
General:
SpiChannelType: EB
SpiDataWidth: 8
SpiTransferStart: MSB
...
SpiChannel: "SpiChannel_Cmd"
General:
SpiChannelType: EB
SpiDataWidth: 8
SpiTransferStart: MSB
...
SpiChannel: "SpiChannel_Addr"
General:
SpiChannelType: EB
SpiDataWidth: 16
SpiTransferStart: MSB
...
SpiChannel: "SpiChannel_Data"
General:
SpiChannelType: EB
SpiDataWidth: 8
SpiTransferStart: MSB
...
SpiJob: "SpiJob_Wren"
General:
SpiDeviceAssignment: /Spi/Spi/SpiDriver/SpiExternalDevice_0
...
SpiChannelList:
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_Wren
...
SpiJob: "SpiJob_Data"
General:
SpiDeviceAssignment: /Spi/Spi/SpiDriver/SpiExternalDevice_0
...
SpiChannelList:
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_Cmd
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_Addr
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_Data
...
SpiSequence: "SpiSequence_Write"
General:
...
SpiJobAssignment:
Index 0: /Spi/Spi/SpiDriver/SpiJob_Wren
Index 1: /Spi/Spi/SpiDriver/SpiJob_Data
SpiSequence: "SpiSequence_Read"
General:
...
SpiJobAssignment:
Index 0: /Spi/Spi/SpiDriver/SpiJob_Data
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] = { 0x00, 0x01, 0x02, 0x03 };
Spi_DataBufferType RxBuffer[4];
uint8 Wren, Cmd;
uint16 Address;
EcuM_Init();
SEGGER_RTT_Init();
/*Enable writing instructions in the eeprom memories by sending a 0x06
and send it in the first Job*/
Wren = 0x06;
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Wren, &Wren, NULL, 1 );
/*Set up the buffer for the Write command + Memoery Address and four bytes of data
in the second job*/
Cmd = 0x02; /*WREN Instruction value*/
Address = 0x00A0; /*Memory address to write data*/
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Cmd, &Cmd, NULL, 1 );
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Addr, (Spi_DataBufferType*)&Address, NULL, 1 );
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Data, TxBuffer, NULL, 4 );
/*Send the data to the eeprom memory, two jobs*/
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_Write );
/*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 );
/*Set up the buffer with the read command + address to read and the buffer to store
the read data in one job*/
Cmd = 0x03; /*READ Instruction value*/
Address = 0x00A0; /*Memory address to read data*/
TxBuffer[0] = 0x33;
TxBuffer[3] = 0x33;
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Cmd, &Cmd, NULL, 1 );
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Addr, (Spi_DataBufferType*)&Address, NULL, 1 );
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_Data, NULL, RxBuffer, 4 );
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_Read );
SEGGER_RTT_printf(0, "Data read from eeprom 1: %x\n", RxBuffer[0] );
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 );
}