Hey Jeremy,
I am confused because I do't succeed to make my SPI connection working with DMA...
You gave me so many advice, but I steel have problems.
here my code :
The main.c
int main(void){
volatile int it = 0;
dma_init();
for(it=0;it <100000;it++);
SPI_init();
camera_send_cmd(CR,0,0);
for(it=0;it <100000;it++);
camera_send_cmd(WU,0,0);
TERMINAL_PRINTF("START TEST\n\r");
while(1) {
test_RT();
for(it=0;it <100000;it++);
}
return 0;
}
The test_RT fonction:
extern uint8_t spi_rdata[3]={0xf};
#define RT 0b11000011
void test_RT(){
volatile int i=0;
uint8_t read[3]={RT,0x00,0x00};
TERMINAL_PRINTF("TEST_RT - new_dma_data=%u\n\r",new_dma_data);
SPI_write(read,3);
for(i=0;i<1000000;i++);
if(new_dma_data){
TERMINAL_PRINTF("SB=%u\n\r",spi_rdata[0]);
TERMINAL_PRINTF("Threshold=%u\n\r",spi_rdata[1]);
TERMINAL_PRINTF("Zero=%u\n\r",spi_rdata[2]);
TERMINAL_PRINTF("----------------\n\r");
}
}
I will give you me 2 init fonction :
volatile uint8_t new_dma_data=0;
void DMA_IRQHandler(void){
static uint8_t nb_int = 0;
nb_int =;
if(nb_int%3==0){
if (((DMA_DSR_BCR0 & DMA_DSR_BCR_DONE_MASK) == DMA_DSR_BCR_DONE_MASK)
| ((DMA_DSR_BCR0 & DMA_DSR_BCR_BES_MASK) == DMA_DSR_BCR_BES_MASK)
| ((DMA_DSR_BCR0 & DMA_DSR_BCR_BED_MASK) == DMA_DSR_BCR_BED_MASK)
| ((DMA_DSR_BCR0 & DMA_DSR_BCR_CE_MASK) == DMA_DSR_BCR_CE_MASK))
DMA_DSR_BCR0 |= DMA_DSR_BCR_DONE_MASK;
DMA_SAR0 = (uint32_t) &SPI0_D;
DMA_DAR0 = (uint32_t) &spi_rdata;
DMA_DSR_BCR0 |= DMA_DSR_BCR_BCR(3);
DMA_DCR0 |= DMA_DCR_ERQ_MASK;
new_dma_data++;
}
TERMINAL_PRINTF("IRQ_DMA\n");
}
void dma_init(void){
new_dma_data=0;
SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
DMAMUX0_CHCFG0 = 0x00;
if (((DMA_DSR_BCR0 & DMA_DSR_BCR_DONE_MASK) == DMA_DSR_BCR_DONE_MASK)
| ((DMA_DSR_BCR0 & DMA_DSR_BCR_BES_MASK) == DMA_DSR_BCR_BES_MASK)
| ((DMA_DSR_BCR0 & DMA_DSR_BCR_BED_MASK) == DMA_DSR_BCR_BED_MASK)
| ((DMA_DSR_BCR0 & DMA_DSR_BCR_CE_MASK) == DMA_DSR_BCR_CE_MASK))
DMA_DSR_BCR0 |= DMA_DSR_BCR_DONE_MASK;
DMA_SAR0 = (uint32_t) &SPI0_D;
DMA_DAR0 = (uint32_t) &spi_rdata;
DMA_DSR_BCR0 = DMA_DSR_BCR_BCR(3);
DMA_DCR0 &= ~(DMA_DCR_SSIZE_MASK | DMA_DCR_DSIZE_MASK);
DMA_DCR0 |= DMA_DCR_DSIZE(1);
DMA_DCR0 |= DMA_DCR_SSIZE(1);
DMA_DCR0 |= DMA_DCR_DINC_MASK;
DMA_DCR0 |= DMA_DCR_DMOD(1);
DMA_DCR0 |= DMA_DCR_CS_MASK;
DMA_DCR0 |= DMA_DCR_EINT_MASK;
DMAMUX0_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(16);
DMA_DCR0 |= DMA_DCR_ERQ_MASK;
enable_irq(INT_DMA0 - 16);
}
and for the spi :
uint8_t spi_rdata[3]={0xFF};
void SPI_init(void) {
SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK;
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK;
PORTD_PCR0 = PORT_PCR_MUX(2);
PORTC_PCR5 = PORT_PCR_MUX(2);
PORTC_PCR6 = PORT_PCR_MUX(2);
PORTC_PCR7 = PORT_PCR_MUX(2);
SPI0_C1 &= ~SPI_C1_SPE_MASK;
SPI0_C1 = SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPHA_MASK | SPI_C1_CPOL_MASK;
SPI0_C2 = SPI_C2_MODFEN_MASK;
SPI0_C2 |= SPI_C2_RXDMAE_MASK;
SPI0_BR = (SPI_BR_SPPR(0b000) | SPI_BR_SPR(0b0011));
SPI0_C1 |= SPI_C1_SPE_MASK;
}
void SPI_write(uint8_t* p, uint8_t size) {
uint8_t i=0;
for (i=0; i<size; i++) {
while(!(SPI_S_SPTEF_MASK & SPI_status));
SPI0_D = p[i];
}
}
sorry for the french comment in the code ^^
I give you lot of work, but your help would be very very very useful because I really donc understand what is the problem.
I use an oscilloscope to see the signal. All signal are right the MOSI, the MISO also.
BUT :
-> if the interrupt of the DMA is disable (DMA_DCR_EINT_MASK not used), all works fine. But I receive 0 in spi_rdata[0], 0 in spi_rdata[1], 0 in spi_rdata[2]. I initialise spi_rdata[] with 0xFF, and in the printf I can see that there are 0 in spi_rdata[i]. But the problem is that on MISO I don't have 0 ... So Why ? Really don't understand this point.
-> if I enable the interrupt in the DMA init : the spi_write() is blocking. The program block in the spi_write fonction I donc know why ...
REALLY thanks in advance !