The following example just show how to transmit data to one of the eeprom in the board, one single byte will be written and the read it again to be displayed using printf statement. Notice we only configure one single channel, job and sequence for all the operations.
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 buffers
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"
#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: 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 );
}