8 years, 10 months ago.

AS5048A SPI Mag Rotary Position Sensor - FRDM - K64F

Hello,

I'm having problems working with the SPI bus on the FRDM - K64F.

I've been trying to use the AS5048A SPI magnetic angle sensor with the following library:

Import libraryAS5048

AS5048A SPI - Magnetic rotary encoder library

Pins: CS -> PTD0 SCLK -> PTD1 MISO -> PTD3 MOSI -> PTD2

So far the circuit has been unresponsive to the library. However, I can use some low level code which I converted from arduino to elicit some response from the circuit.

The code below responds to a magnetic field rather erratically. The values change, but not in a manner I can make sense of.

Ideally I would like to work with the library. Is there something I've missed?

include the mbed library with this snippet

 "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
//MOSI, MISO, SCLK
SPI spi(PTD2, PTD3, PTD1);
//CS/SS
DigitalOut cs(PTD0);

int main()
{

    unsigned int result1 = 0;
    unsigned int result2 = 0;
    unsigned int result = 0;

    spi.format(8, 1);
    spi.frequency(1000000);


    while(1) {

        cs.write(0);
        wait_us(1);

        result1 = spi.write(0x0b00000000);
        result1 &= 0x0b00111111;
        result1 = result1 << 8;
        result2 = spi.write(0x0b00000000);
        result = result1 | result2;
               
        cs.write(1);
        wait_ms(200);
    }
}

1 Answer

6 years, 5 months ago.

Hello, I'd like to ask you some question :

It always show me the error: [mbed assertation failed: _id, file: .\mbed-os\rtos\Mutex.cpp, line 49] when I try to use the SPI API on the K64. My code is provided by the mbed official website, and exactly the same as your codes below. Did you meet that before? How to slove it?

SPI example

#include "mbed.h"

#define PIN_MOSI PTD2
#define PIN_MISO PTD3
#define PIN_SCLK PTD1
#define PIN_CS   PTD0

SPI spi(PIN_MOSI, PIN_MISO, PIN_SCLK); // mosi, miso, sclk
DigitalOut cs(PIN_CS);

int main() {
	// Chip must be deselected
	cs = 1;

	// Setup the spi for 8 bit data, high steady state clock,
	// second edge capture, with a 1MHz clock rate
	spi.format(8, 3);
	spi.frequency(1000000);

	// Select the device by seting chip select low
	cs = 0;

	// Send 0x8f, the command to read the WHOAMI register
	spi.write(0x8F);

	// Send a dummy byte to receive the contents of the WHOAMI register
	int whoami = spi.write(0x00);
	printf("WHOAMI register = 0x%X\n", whoami);

	// Deselect the device
	cs = 1;
}

Good luck. Thanks for your kind help.

And I'm sure the error caused by the fuction (lock()) as shown as below:

SPI.cpp

void SPI::format(int bits, int mode) {
    lock();
    _bits = bits;
    _mode = mode;
    // If changing format while you are the owner than just
    // update format, but if owner is changed than even frequency should be
    // updated which is done by acquire.
    if (_owner == this) {
        spi_format(&_spi, _bits, _mode, 0);
    } else {
        _acquire();
    }
    unlock();
}
posted by Dick Jor 03 Nov 2017