Skip to content

Commit

Permalink
Wait for DMA channel end, variable fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gerh committed Feb 5, 2021
1 parent 6e0e6dc commit 298397d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion MDK-ARM/FreeJoy.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@

<Group>
<GroupName>USB-FS-Device_Driver</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
Expand Down
2 changes: 1 addition & 1 deletion MDK-ARM/FreeJoy.uvprojx
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
</ArmAdsMisc>
<Cads>
<interw>1</interw>
<Optim>1</Optim>
<Optim>4</Optim>
<oTime>0</oTime>
<SplitLS>0</SplitLS>
<OneElfS>1</OneElfS>
Expand Down
42 changes: 29 additions & 13 deletions application/Src/as5048a.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@

#include "as5048a.h"

uint8_t gtmp_buf[2];

void reset_err_flag(sensor_t * sensor){
uint16_t tmp;
// CS low
pin_config[sensor->source].port->ODR &= ~pin_config[sensor->source].pin;
gtmp_buf[0] = 0x40;
gtmp_buf[1] = 0x01;

SPI_HalfDuplex_Transmit(&gtmp_buf[0], 2, AS5048A_SPI_MODE);

do{
tmp = DMA_GetCurrDataCounter(DMA1_Channel3);
} while(tmp!=0);

// CS high
pin_config[sensor->source].port->ODR |= pin_config[sensor->source].pin;

}
/**
* @brief AS5048A get measured data
* @param data: variable for storing data
Expand All @@ -33,27 +52,26 @@
int AS5048A_GetData(uint16_t * data, sensor_t * sensor, uint8_t channel)
{
int ret = 0;
uint8_t tmph,tmpl;
uint16_t tmp;
// wait till the DMA channel has finished
do{
tmp = DMA_GetCurrDataCounter(DMA1_Channel2);
} while(tmp!=0);
tmph = sensor->data[0];
tmpl = sensor->data[1];
tmp = (tmph << 8) | tmpl;
tmp = sensor->data[0];
tmp = (tmp << 8) | sensor->data[1];
*data = tmp & 0x3FFF;
// check error bit
if((tmph&0x40)!=0){
// need to do: reset err flag with 0x41 command
// return -1;
if((tmp&0x4000)!=0){
// reset err flag with 0x4001 command
reset_err_flag(sensor);
ret = -1;
}
// check parity
tmp ^= tmp >> 8;
tmp ^= tmp >> 4;
tmp ^= tmp >> 2;
tmp ^= tmp >> 1;
if((tmp & 1)==1) ret = -1;
if((tmp & 1)==1) ret = -2;
return ret;
}

Expand All @@ -65,17 +83,16 @@ int AS5048A_GetData(uint16_t * data, sensor_t * sensor, uint8_t channel)
*/
void AS5048A_StartDMA(sensor_t * sensor)
{
uint8_t tmp_buf[2];

sensor->rx_complete = 0;
sensor->tx_complete = 1;

tmp_buf[0] = 0x3F; // Read Meas. command: 0x3FFF
tmp_buf[1] = 0xFF; //
gtmp_buf[0] = 0x3F; // Read Meas. command: 0x3FFF
gtmp_buf[1] = 0xFF; //

// CS low
pin_config[sensor->source].port->ODR &= ~pin_config[sensor->source].pin;
SPI_FullDuplex_TransmitReceive(tmp_buf, sensor->data, 2, AS5048A_SPI_MODE);
SPI_FullDuplex_TransmitReceive(gtmp_buf, sensor->data, 2, AS5048A_SPI_MODE);
}

void AS5048A_StopDMA(sensor_t * sensor)
Expand All @@ -86,7 +103,6 @@ void AS5048A_StopDMA(sensor_t * sensor)
sensor->rx_complete = 1;
sensor->tx_complete = 1;

SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Tx);
}


Expand Down

0 comments on commit 298397d

Please sign in to comment.