AS5048A Magnetic Encoder and STM32

This past week I began to write the controller firmware - mainly playing with the AS5048A angular position sensor from AMS. Here is the datasheet

This sensor will be used to get the rotor position and then compute the resulting electrical angle for the Clarke/Park transforms and also it will be used to find rotor speed for the out loop. 

I will be using an STM32F446 board to run the loop as it has a high clock speed. The AS5048A sends its measurements over SPI. Below is the read package sent from the sensor:


We get 14 bits of position data - aka 16384 ticks per revolution, or a resolution of 0.0219 degrees.

I wrote this read()function that sends the SPI command package and reads the resulting response:


uint16_t read(uint16_t registerAddress) {

uint16_t command = 0x4000; // PAR=0 R/W=R


uint8_t RxBuff[2];

uint8_t TxBuff[2];


command = command | registerAddress;

command |= (uint16_t) (spiCalcEvenParity(command) << 0xF);


TxBuff[0] = command >> 8;

TxBuff[1] = command & 0xFF;


// Send 16-bit SPI command

HAL_GPIO_WritePin(SPI3_SEL_GPIO_Port, SPI3_SEL_Pin, 0);

HAL_SPI_Transmit(&hspi3, (uint8_t *)&TxBuff[0], 1, 100);

HAL_SPI_Transmit(&hspi3, (uint8_t *)&TxBuff[1], 1, 100);

HAL_GPIO_WritePin(SPI3_SEL_GPIO_Port, SPI3_SEL_Pin, 1);


// Receive 16-bit SPI response

HAL_GPIO_WritePin(SPI3_SEL_GPIO_Port, SPI3_SEL_Pin, 0);

HAL_SPI_Receive(&hspi3, (uint8_t *)&RxBuff[0], 1, 100);

HAL_SPI_Receive(&hspi3, (uint8_t *)&RxBuff[1], 1, 100);

HAL_GPIO_WritePin(SPI3_SEL_GPIO_Port, SPI3_SEL_Pin, 1);


return ((RxBuff[0] << 8) | RxBuff[1]) & ~0xC000;

}

It pretty much takes the desired register to be read on the AS5048A (for angle it is 0x3FFF), computes the parity, and uses the built in STM32 HAL methods to transmit and receive the data. The timing/order took me a bit to figure out since STM32 is configured by default to send 8 bit words while the sensor uses 16 bits - as you can see I ended up splitting it into 2 single byte transmits/receives

I plan on posting the code on GitHub later. 


Comments

Popular posts from this blog

Pseudo-FOC Controller - Simulations and Inital

Revised CNC Mill Design