How can I use ESR1 and ECR?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I use ESR1 and ECR?

Jump to solution
2,741 Views
Oido
Contributor III

I wrote CAN FD and several functions, but I erased all of them except CAN FD.
I want to know how to use ECR and ESR1 registers.

The MCU I am using is S32K144W

Can I read the value with PEMICRO EXPRESSION?
CAN0->CTRL1 |= CAN_CTRL1_ERRMSK_MASK;
CAN0->ESR1=CAN_ESR1_ERRINT_MASK; Should I MASK the register directly like this?
I've looked up a lot of posts in the community, but I've seen it say ESR1 [ERRINT] like this, but I can't find how to use it.

I saw the data sheet that it can be recorded in freeze mode. But even looking at it, I have no idea how to use it. The code doesn't say freeze mode separately, but it's something I erased. I've already done it.

 

I just want to write a code that initializes CAN if I read the error count in ECR and get more than a few errors, but I'm wasting a lot of time. Please teach me.

#include "sdk_project_config.h"
#include <stdint.h>
#include <stdbool.h>
#include "core_cm4.h"
 
#define TX_MAILBOX  (1UL)
#define TX_MSG_ID   0x300
#define RX_MAILBOX  (0UL)
#define RX_MSG_ID   0x400
 
uint8_t TXD[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
flexcan_msgbuff_t recvBuff;
 
void FlexCANInit(void)
{
    FLEXCAN_DRV_Init(INST_FLEXCAN_CONFIG_1, &flexcanState0, &flexcanInitConfig0);
    flexcan_data_info_t dataInfo =
    {
            .data_length = 2U,
            .msg_id_type = FLEXCAN_MSG_ID_STD,
            .enable_brs  = true,
            .fd_enable   = true,
            .fd_padding  = 0U,
    };
    FLEXCAN_DRV_ConfigRxMb(INST_FLEXCAN_CONFIG_1, RX_MAILBOX, &dataInfo, 0x00);
    FLEXCAN_DRV_Receive(INST_FLEXCAN_CONFIG_1, RX_MAILBOX, &recvBuff);
    CAN0->CTRL1 |= CAN_CTRL1_ERRMSK_MASK;
    CAN0->ESR1=CAN_ESR1_ERRINT_MASK;
    //FLEXCAN_DRV_ConfigTxMb(INST_FLEXCAN_CONFIG_1,TX_MAILBOX,&dataInfo,0x300);
    //CAN0->FDCTRL |= CAN_FDCTRL_FDRATE(1)|CAN_FDCTRL_TDCEN_MASK|CAN_FDCTRL_TDCOFF(7)|CAN_FDCTRL_MBDSR0(3);//CAN_FDCTRL_TDCVAL_MASK|
}
 
void CAN0TX_Init(void) // 레지스터 방식 TX
{
#define MSG_BUF_SIZE 16
CAN0->IFLAG1 = 0x00000001;
CAN0->RAMn[ 0*MSG_BUF_SIZE + 0] = 0xCC4F0000; //CS설정 데이터시트 1712페이지
CAN0->RAMn[ 0*MSG_BUF_SIZE + 1] = 0x0C000000; //첫번째 워드는 ID를 설정함 원하는 ID값(HEX) *4 현재0x300
CAN0->RAMn[ 0*MSG_BUF_SIZE + 2] = 0xA5112233; // 두번째 데이터 워드는 데이터  한 워드당 4바이트
CAN0->RAMn[ 0*MSG_BUF_SIZE + 3] = 0x44556677; // 세번째 데이터 워드는 데이터
CAN0->RAMn[ 0*MSG_BUF_SIZE + 4] = 0x12341234; // 네번째 데이터 워드는 데이터
CAN0->RAMn[ 0*MSG_BUF_SIZE + 5] = 0x12498234; // 다섯번째 데이터 워드는 데이터
}
 
 
void BoardInit(void)
{
CLOCK_DRV_Init(&clockMan1_InitConfig0); //클럭설정 SPLL에 분주4로 시스템클럭 40Mhz
 
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0, g_pin_mux_InitConfigArr0); // 핀 입출력, 기능 설정(PWM ADC 등)
}
 
 
int main(void)
{
    BoardInit(); //핀 및 클럭
    FlexCANInit();//보더레이트 및
    while(1)
    {
    //SendCANData(TX_MAILBOX, TX_MSG_ID, TXD, 16UL); //계속 ID300으로 TX
    CAN0TX_Init ();
    }
}
-------------
Additionally, I tried a method using chatgpt, but it only popped up as zero and could not be read.
 

uint8_t getReceiveErrorCounter() {
// Freeze 모드로 진입하여 레지스터 읽기
CAN0->MCR |= CAN_MCR_HALT_MASK; // Freeze mode로 진입

// RXERRCNT 레지스터 읽기
uint8_t rxErrCounter = (CAN0->ECR & CAN_ECR_RXERRCNT_MASK) >> CAN_ECR_RXERRCNT_SHIFT;

// Freeze 모드 해제
CAN0->MCR &= ~CAN_MCR_HALT_MASK; // Freeze mode 해제

return rxErrCounter;
}

 

int main(){

uint8_t rxErrCounter = getReceiveErrorCounter();
}
0 Kudos
Reply
1 Solution
2,716 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@Oido

In this demo, if a CAN error occurs, flexcan0_ErrorCallback will be entered.

1.The FLEXCAN_DRV_GetErrorStatus returns the ESR1 register value. According to ERS1 to judge which error occured.

2.You can directly read the ECR register value to get the error count of TX and RX.

View solution in original post

0 Kudos
Reply
3 Replies
2,725 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@Oido

please refer to this article which posted by Petr.

https://community.nxp.com/t5/S32K-Knowledge-Base/Example-S32K144-FlexCAN-TX-RX-Error-ISR-test-S32DS2...

this demo show you how to use ECR and ESR registers.

0 Kudos
Reply
2,721 Views
Oido
Contributor III

First of all, thank you for your answer.

But I don't understand which part is the part where esr and ecr are used. Do you mean flexcan0_ErrorCallback part?

If that's right,

can0->CBT = CAN_CBT_BTF_MASK|CAN_CBT_EPRESDIV(1)|CAN_CBT_EPSEG2(7)|CAN_CBT_EPSEG1(29)|CAN_CBT_EPROPSEG(0)|CAN_CBT_ERJW(7);

Is there no way to declare a register this way?

 

Actually, I don't even know if it's an event. How can you find an error count over there

0 Kudos
Reply
2,717 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@Oido

In this demo, if a CAN error occurs, flexcan0_ErrorCallback will be entered.

1.The FLEXCAN_DRV_GetErrorStatus returns the ESR1 register value. According to ERS1 to judge which error occured.

2.You can directly read the ECR register value to get the error count of TX and RX.

0 Kudos
Reply