Bus error when writing to SPI registers, am I missing something?

Hey guys,

I’m at wit’s end on this problem (its 3:30am here) and I’m ready to give up the head banging approach and ask for some help.

What I’d like to do is trigger DMA to send two buffers out both SPI ports simultaneously.

But I’m trying to make baby steps and I got hung up pretty early. My test code that I can’t get working is simply designed to send a word out of spi0 via its register. The problem is, as soon as I try to touch the SPI registers, I get a bus error. I googled around and it looks like I wasn’t enabling clocks like I should. So I enabled CM_PER_L4LS_CLKCTRL and CM_PER_SPI0_CLKCTRL in the CM_PER registers but that hasn’t helped my bus error issue. Am I doing the right thing to enable the clocks? Am I even enabling the right clocks?

`

#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include “mcspi.h”
#include “soc_AM335x.h”
#include “hw_cm_per.h”

int main()
{
static volatile uint32_t * spi0_base_addr;
static volatile uint32_t * clocks_base_addr;

printf(“Opening raw memory…\n”);
int memfd = open("/dev/mem", O_RDWR | O_SYNC);
if(memfd < 0) {
printf(“Error opening memory!\n”);
return -1;
}

printf(“Mapping registers to user space…\n”);
spi0_base_addr = (uint32_t *)mmap(0,getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, memfd, SOC_SPI_0_REGS);
clocks_base_addr = (uint32_t *)mmap(0,getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, memfd, SOC_CM_PER_REGS);

if((int32_t) spi0_base_addr < 0 || (int32_t) clocks_base_addr < 0) {
printf(“mmap failed: %s\n”, strerror(errno));
return -1;
}

//Turn on clocks
clocks_base_addr[CM_PER_L4LS_CLKCTRL] |= CM_PER_L4LS_CLKCTRL_MODULEMODE_ENABLE;
clocks_base_addr[CM_PER_SPI0_CLKCTRL] |= CM_PER_SPI0_CLKCTRL_MODULEMODE_ENABLE;

//Wait to make sure there isn’t a race condition
sleep(1);
//Check the SPI_GCLK status bit, should be non-zero
printf(“CLock status: %08x\n”, clocks_base_addr[CM_PER_L4LS_CLKSTCTRL] & (CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_SPI_GCLK_ACT << CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_SPI_GCLK_SHIFT));

//Read back the two clock registers that were enabled before
printf(“clock registers %08x %08x \nWriting spi register…\n”, clocks_base_addr[CM_PER_L4LS_CLKCTRL], clocks_base_addr[CM_PER_SPI0_CLKCTRL]);

//Soft reset of the SPI peripheral
spi0_base_addr[MCSPI_SYSCONFIG] |= MCSPI_SYSCONFIG_SOFTRESET;

//McSPIReset((unsigned int)spi0_base_addr);

spi0_base_addr[MCSPI_TX(0)] = 1337;

//McSPITransmitData((unsigned int)spi0_base_addr, 1337, MCSPI_CHANNEL_1);
//McSPIClkConfig(spi0_base_addr, 24000000, 24000000, MCSPI_CHANNEL_0, MCSPI_CLK_MODE_0);

printf(“Done.\n”);

return 0;
}

`

Thanks in advance for any help,
-JonRW

I forgot to paste the output, it may be helpful:

`

root@beaglebone:~/early_testing/dspi# ./mem
Opening raw memory…
Mapping registers to user space…
CLock status: 00000000
clock registers 00000000 00040002
Writing spi register…
Bus error

`