The following example just show how to send 10 bytes using the SPI configured as Master using the LPSPI0 module, even thou the bus is connected to an eeprom (on Modular-S1) we do not write anything because we are only sending dummy data at 100KHz to be visualize using a logic analyzer or an oscilloscope
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: 4
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
...
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
...
SpiChannel: "SpiChannel_0"
General:
SpiChannelType: EB
SpiDataWidth: 8
SpiTransferStart: MSB
...
SpiJob: "SpiJob_0"
General:
SpiDeviceAssignment: /Spi/Spi/SpiDriver/SpiExternalDevice_0
...
SpiChannelList:
SpiChannelAssignment: /Spi/Spi/SpiDriver/SpiChannel_0
...
SpiSequence: "SpiSequence_0"
General:
...
SpiJobAssignment:
Index 0: /Spi/Spi/SpiDriver/SpiJob_0
main.c
#include "Mcu.h"
#include "Port.h"
#include "Dio.h"
#include "Platform.h"
#include "Osif.h"
#include "Spi.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 )
{
static Spi_DataBufferType TxMasterBuffer[10] = {11,12,13,14,15,16,17,18,19,20};
EcuM_Init();
/* Copy transmitted data to internal buffer */
Spi_SetupEB( SpiConf_SpiChannel_SpiChannel_0, TxMasterBuffer, NULL, 10 );
while( 1u )
{
/* This sequence of master transferring 10 frames of 8 bits using synchronous method*/
Spi_SyncTransmit( SpiConf_SpiSequence_SpiSequence_0 );
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 );
}