Hello,
I try to write a function for a very small bootloader. Which should send commands to a display.
However the last statement "LPSPI0->TDR = (uint32_t)(0x20<<8);" cause a hard fault and I dont know why. Can anyone help me? The MCU is a K32L2A31A.
__attribute__ ((naked, section(".after_vectors.reset")))
void showBLMInfo(void){
//Connect CMP1 with TPM2
TRGMUX_TPM2 = TRGMUX_TRGCFG_SEL0(0b100111);
//Comparator for regulated Chargepump
PCC_CMP1 = PCC_CLKCFG_CGC_MASK; //Clock enable
//Caution Vin2 as reference is VDD, datasheet is wrong here!
CMP1->DACCR = CMP_DACCR_DACEN_MASK | CMP_DACCR_VRSEL_MASK | CMP_DACCR_VOSEL(0x2E);
CMP1->MUXCR = CMP_MUXCR_MSEL(7) | CMP_MUXCR_PSEL(0);
CMP1->CR1 = CMP_CR1_EN_MASK;
//Generate PWM for Chargepump
PCC_TPM2 = PCC_CLKCFG_CGC_MASK | PCC_CLKCFG_PCS(2); //Clock enable, Fast IRC
TPM2->CONF = TPM_CONF_CPOT_MASK | TPM_CONF_TRGSEL(0) | TPM_CONF_DBGMODE_MASK;
TPM2->CNT = 0;
TPM2->MOD = 10;
TPM2->CONTROLS[1].CnSC = TPM_CnSC_ELSB_MASK | TPM_CnSC_MSB_MASK;
TPM2->CONTROLS[1].CnV = 5;
//Enable Chargepump
TPM2->SC = TPM_SC_CMOD(1) | TPM_SC_PS(2);
//GPIO
//GPIO_SPI0_CS_DISP_GPIO->PCOR = GPIO_SPI0_CS_DISP_GPIO_PIN_MASK;
//EN_U_BAT, EN_DISP
PCC_PORTE = PCC_CLKCFG_CGC_MASK; //Clock enable PORT E
PCC_PORTD = PCC_CLKCFG_CGC_MASK; //Clock enable PORT D
PORTE->PCR[1] = PORT_PCR_MUX(1); //EN_U_BAT
PORTE->PCR[25] = PORT_PCR_MUX(2); //EN_DISP
PORTD->PCR[0] = PORT_PCR_MUX(2); //CS
PORTD->PCR[1] = PORT_PCR_MUX(2); //SCK
PORTD->PCR[2] = PORT_PCR_MUX(2); //MOSI
GPIOE->PDDR = (1U << 25U) | (1U << 1U);
GPIOE->PSOR = (1U << 25U) | (1U << 1U);
//1Hz Refresh Clock
//RTC->CR |= RTC_CR_CPE(1);
PCC_LPSPI0 = PCC_CLKCFG_CGC_MASK | PCC_CLKCFG_PCS(2); //Clock enable
LPSPI0->CR = LPSPI_CR_DBGEN_MASK | LPSPI_CR_MEN_MASK;
LPSPI0->CFGR1 = LPSPI_CFGR1_MASTER_MASK | LPSPI_CFGR1_PCSPOL(1) | LPSPI_CFGR1_AUTOPCS_MASK;
LPSPI0->CCR = LPSPI_CCR_PCSSCK(11)|LPSPI_CCR_SCKPCS(2)|LPSPI_CCR_DBT(4);
//Send Display clear command
LPSPI0->TCR = LPSPI_TCR_FRAMESZ(15) | LPSPI_TCR_PRESCALE(4) | LPSPI_TCR_TXMSK_MASK | LPSPI_TCR_CONT_MASK; //16bit Transfer
LPSPI0->TDR = (uint32_t)(0x20<<8);
}
Thanks a lot in advance.
Solved! Go to Solution.
Unless the function is an interrupt it should not be declared as naked.
Unless the function is an interrupt it should not be declared as naked.
Hello,
thanks a lot this was the solution. Just leave naked
Best regards
Hello,
I found out the issue is not writing to "LPSPI0->TDR". The issue is the return of the function. Why a return from a function cause a hard fault in startup code? The call stack looks good before the hardfault cause.
I hope someone knows what is going on here.
Thanks a lot and best regards