AnsweredAssumed Answered

write the data received by the UART into the EEPROM

Question asked by 灵旺 俞 on Nov 14, 2019
Latest reply on Nov 14, 2019 by 灵旺 俞

Hello, I need to write the data received by the UART into the EEPROM. I don't understand how to write the instructions. Can you help me? Thank you.

/*
* main implementation: use this 'C' sample to create your own application
*
*/


#include "S32K144.h" /* include peripheral declarations S32K144 */
#include "EEEPROM_io_control.h"

#define USER_DATA_SIZE 32u
#define DISABLE_INTERRUPTS() __asm volatile ("cpsid i" : : : "memory");
#include "s32_core_cm4.h"
void SOSC_init_8MHz(void)
{
SCG->SOSCDIV=0x00000101; //SOSCDIV1 & SOSCDIV2 =1: 分频/1
SCG->SOSCCFG=0x00000024; //Range=2: 选择晶体振荡器的中频范围 (SOSC 1MHz-8MHz)
// HGO=0: 控制晶体振荡器的工作功率模式 --低功率模式
// EREFS=1: 外部参考选择OSC内部晶体振荡器
while(SCG->SOSCCSR & SCG_SOSCCSR_LK_MASK); //等待SOSCCSR解锁 寄存器解锁后才可写入
SCG->SOSCCSR=0x00000001; // LK=0: SOSCCSR可以写
// SOSCCM=0: 系统OSC时钟监视器被禁用
// SOSCEN=1: 启用系统OSC
while(!(SCG->SOSCCSR & SCG_SOSCCSR_SOSCVLD_MASK)); //等待系统OSC成功启用,输出时钟有效
}


void SPLL_init_160MHz(void)
{
while(SCG->SPLLCSR & SCG_SPLLCSR_LK_MASK); //等待SPLLCSR寄存器解锁 寄存器解锁后才可写入
SCG->SPLLCSR = 0x00000000; // LK=0: SPLLCSR可以写入
// SPLLEN=0: SPLL禁用
SCG->SPLLDIV = 0x00000302; // SPLLDIV1 分频/2; SPLLDIV2 分频/4
SCG->SPLLCFG = 0x00180000; // PREDIV=0: 锁相环参考时钟分频因子
// MULT=24: SPLL时钟频率的乘法因子
// SPLL_CLK = 8MHz / 1 * 40 / 2 = 160 MHz SPLL_CLK = (VCO_CLK)/2 VCO_CLK = SPLL_SOURCE/(PREDIV+1)*(MULT+16)
while(SCG->SPLLCSR & SCG_SPLLCSR_LK_MASK); //等待SPLLCSR寄存器解锁 寄存器解锁后才可写入
SCG->SPLLCSR = 0x00000001; // LK=0: SPLLCSR可以写入
// SPLLCM=0: SPLL时钟监视器被禁用
// SPLLEN=1: 开启SPLL
while(!(SCG->SPLLCSR & SCG_SPLLCSR_SPLLVLD_MASK)); //等待SPLL成功启用,输出时钟有效
}

void NormalRUNmode_40MHz (void)
{
SCG->RCCR=SCG_RCCR_SCS(6) // SPLL做为系统时钟源
|SCG_RCCR_DIVCORE(0b11) // DIVCORE=3, 分频/4: Core clock = 160/4 MHz = 40 MHz
|SCG_RCCR_DIVBUS(0b11) // DIVBUS=3, 分频/4: bus clock = 160/4 MHz = 40 MHz
|SCG_RCCR_DIVSLOW(0b111); // DIVSLOW=7, 分频/8: SCG slow, flash clock= 160/8 MHz = 20MHZ
while (((SCG->CSR & SCG_CSR_SCS_MASK) >> SCG_CSR_SCS_SHIFT ) != 6) {}//等待系统时钟源成功选择SPLL
}
void UART2_NVIC_init_IRQs(void)
{
S32_NVIC->ICPR[1] = 1 << (35 % 32); /* IRQ48-UART2: clr any pending IRQ*/
S32_NVIC->ISER[(uint32_t)(LPUART2_RxTx_IRQn) >> 5U] = (uint32_t)(1UL << ((uint32_t)(LPUART2_RxTx_IRQn) & (uint32_t)0x1FU));
S32_NVIC->IP[35] = 0x7; /* IRQ48-UART2: priority 7 of 0-15*/
}
void UART2_PORT_init (void)
{
PCC->PCCn[PCC_PORTA_INDEX ] |= PCC_PCCn_CGC_MASK; //使能PTA端口时钟
PORTA->PCR[8] |= PORT_PCR_MUX(2); // Port A8: MUX = ALT2,UART2 RX PTA8复用为RX
PORTA->PCR[9] |= PORT_PCR_MUX(2); // Port A9: MUX = ALT2,UART2 TX PTA9复用为TX
}
//波特率: 9600 , 1停止位, 8个数据位, 无奇偶校验
void UART2_init(void)
{
PCC->PCCn[PCC_LPUART2_INDEX] &= ~PCC_PCCn_CGC_MASK; //禁LPUART2时钟
PCC->PCCn[PCC_LPUART2_INDEX] |= PCC_PCCn_PCS(0b001) //选择时钟 Clock Src= 1 (SOSCDIV2_CLK)
| PCC_PCCn_CGC_MASK; //使能LPUART2时钟

LPUART2->BAUD = 0x0F000034; //配置波特率为9600,1停止位
// SBR=52 (0x34): 波特率模数除数 = 8M/9600/16 = ~52
// OSR=15: 采样率 = 15+1=16
// SBNS=0: 1个停止位
// BOTHEDGE=0: 接收端使用波特率时钟上升沿采样输入数据
// M10=0: 接收机和发射机使用7位到9位的数据字符
// RESYNCDIS=0: 支持在接收数据字期间重新同步
// LBKDIE, RXEDGIE=0: 禁用LIN中断,RX收入边缘中断

LPUART2->CTRL=0x000C0000 | (1<<21);
// RE=1,TE=1: 使能接收及发送
// PE=0: 无奇偶校验
// M7,M,R8T9,R9T8=0: 8个数据位
// DOZEEN=0: 睡眠模式下LPUART正常启用
// ORIE,NEIE,FEIE,PEIE,TIE,TCIE,ILIE,MA1IE,MA2IE=0,RIE=1: 接收中断
// TXINV=0: 传输数据不是反向的
// IDLCFG=0: 1个空闲字符
// ILT=0: 空闲字符位计数在开始位之后开始
}
/*********************************************************************
* 函数原型:void UART2_transmit_char(char send)
* 功 能:UART2发送一个BYTE的数据
* 输入参数:send--需要发送的字符数据
* 返回参数:无
*
* 其他说明:
*********************************************************************/
void UART2_transmit_char(char send)
{ /* Function to Transmit single Char */
while((LPUART2->STAT & LPUART_STAT_TDRE_MASK)>>LPUART_STAT_TDRE_SHIFT==0);
/* Wait for transmit buffer to be empty */
LPUART2->DATA=send; /* Send data */
}

void UART2_transmit_string(char data_string[],int len)
{
uint32_t i=0;
for(i=0;i<len;i++)
{
UART2_transmit_char(data_string[i]);
}
UART2_transmit_char('\n'); /* New line */
UART2_transmit_char('\r'); /* Return */
}

void _printf(char data_string[])
{
UART2_transmit_string(data_string,strlen(data_string));

UART2_transmit_char('\n'); /* New line */
UART2_transmit_char('\r'); /* Return */
}
typedef struct
{
float p1;
float p2;
float p3;
float p4;
char ch;
double d1;
double d2;
unsigned long runtime_sec;
int i1;
char user_data[USER_DATA_SIZE];
} eeerom_data_t;

__attribute__ ((section(".eeeprom"))) eeerom_data_t eeerom_data;

 

void WDOG_disable (void)
{
WDOG->CNT=0xD928C520; /*Unlock watchdog*/
WDOG->TOVAL=0x0000FFFF; /*Maximum timeout value*/
WDOG->CS = 0x00002100; /*Disable watchdog*/
}


int main(void)
{
WDOG_disable();

SOSC_init_8MHz(); /* Initialize system oscilator for 8 MHz xtal */
SPLL_init_160MHz(); /* Initialize SPLL to 160 MHz with 8 MHz SOSC */
NormalRUNmode_40MHz();/* Init clocks: 80 MHz sysclk & core, 40 MHz bus, 20 MHz flash */

//DISABLE_INTERRUPTS();

UART2_init(); //配置UART2 波特率9600 1个停止数 无校验位
UART2_NVIC_init_IRQs(); //配置中断优先级
UART2_PORT_init (); //配置GPIO复用为TX、RX

//_printf("just do it!");

EEE_Init();

//EEE_Write_int32(123456,&eeerom_data.i1);
//EEE_Write_ByteArray(char* source,char* target,unsigned int size)
//EEE_Write_Byte(rev,&eeerom_data.ch);
while(1)
{

}


/* to avoid the warning message for GHS and IAR: statement is unreachable*/
//#if defined (__ghs__)
//#pragma ghs nowarning 111
//#endif
//#if defined (__ICCARM__)
//#pragma diag_suppress=Pe111
//#endif
return 0;
}

void LPUART2_RxTx_IRQHandler(void)
{
char rev;
if (LPUART2->STAT & 0x00200000)
{
rev = LPUART2->DATA;
//EEE_Write_int32(123456,&eeerom_data.i1);
//EEE_Write_Byte(rev,&eeerom_data.ch);
//UART2_transmit_char(eeerom_data.i1);
if((LPUART2->STAT & LPUART_STAT_TDRE_MASK)>>LPUART_STAT_TDRE_SHIFT!=0) //判断发射是否空闲
{
LPUART2->DATA = rev;
}
}

}

Outcomes