This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Help! stm32f103c8 SPI1 issues

Hello, i am currently trying to develop a brushless motor controller. I wanted to be able to have position control so i implemented an AS5047P-TS_EK_AB magnetic encoder and am using SPI to get absolute position data from it. 

My problem is that when i try to send and receive data from it i get almost random or non-sensible data from it. The thing is that i got this project working with an arduino nano and it works perfectly. but  im missing the CAN bus functionality and debugging features from an stm32. 

Ive never really got the HAL libraries working and i dont prefer using the peripheral libraries either. so ive stuck with the direct register manipulation method (prefer that level of control anyway) and so far ive got all the registers set (i believe).

ive double triple checked every connection imaginable and i can confirm that every connection is solid. i even thought that the stlink v2 was interfering with the spi communication so i set up leds to see if i could debug it with out the st link. no luck.

in the code below, im trying to read the error register (0x4001) of the encoder to see if i could even correctly send data (because the angle data register is all ones 0xFFFF)

so since the connections are correct and the encoder works (because it works eminently when i use the arduino), my only other logical conclusion is that my code is not correct. 

so i wanted to know if there were any errors or problems that could be causing this issue in my code? (perhaps even some help using DMA if i can get it working)

im using SPI1 bus.

all of the includes are just for future code.

/**************************** Header file *******************************/
#include <stm32f10x.h>
#include "stm32f10x_conf.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_spi.h"

/*************************** user defined function **********************/
void GPIOInit(){

	GPIOA->CRL = 0xb8b3bbbb; //sets all portA to alt push pull except A6 for MISO
	GPIOA->CRH = 0x88844433; // led pins green on A8 and red on A9
}
void spi_Init(){
		//turn on the clocks 
	RCC->APB2ENR |= RCC_APB2ENR_SPI1EN | RCC_APB2ENR_IOPAEN;
	
		//configure spi
	SPI1->CR1 |= SPI_CR1_DFF | (0x7UL<<3) | SPI_CR1_MSTR | SPI_CR1_CPHA | SPI_CR1_SSM; 
	SPI1->CR2 |= SPI_CR2_SSOE;	
	GPIOA->BSRR |= GPIO_BSRR_BS4; // set ss high 
	SPI1->CR1 |= SPI_CR1_SPE; // spi enable
}

uint16_t SPIReadWrite (uint16_t data){ // recieve and send data 
	GPIOA->BSRR |= GPIO_BSRR_BR4; //ss set LOW
	SPI1->DR = data; //transmitting data
	while( (SPI1->SR & SPI_SR_TXE) != SPI_SR_TXE ); // waiting for data to transfer
	GPIOA->BSRR |= GPIO_BSRR_BS4; //ss set HIGH
	/*GPIOA->BSRR |= GPIO_BSRR_BR4; //ss set LOW
	SPI1->DR = 0x0000; //transmitting data
	while( (SPI1->SR & SPI_SR_TXE) != SPI_SR_TXE ); // waiting for data to transfer
	GPIOA->BSRR |= GPIO_BSRR_BS4; //ss set HIGH*/
	return SPI1->DR; // return variable
}
void PWM_Init()
{

/****configure clocks for PORTA & TIMER2********/

  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;           // enable port a clock
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;           // enable clock for timer 2

/*****  configure GPIOA mode *******************/

   /* Porta Pin 1 &2 &3 &4 as alternate function output Push pull 50 MHz */
	
	
    TIM2->ARR = 1000;                      // Set Auto reload value
    TIM2->PSC = 1;                         // Set Prescalar value

    /* Output Compare Mode, ENABLE Preload,PWM  mode:2*/
    //TIM2->CCMR1  = 0x00007800; 
		//TIM2->CCMR1 = 0x00006800; //pwm mode 1 for chan 1
		TIM2->CCMR1 = 0x00006868; //pwm mode 1 for chan 1 and 2
		TIM2->CCMR2 = 0x00006868; //pwm mode 1 for chan 3 and 4

    TIM2->EGR |= TIM_EGR_UG;       // ENABLE update generation

    /*CC2E : channel 2 enabled; polarity : active high*/         

    //TIM2->CCER = 0x00000010; //enable chan 2 
		TIM2->CCER = 0x00001111; // enable chan 1, 2, 3 and 4

    TIM2->CR1 |= TIM_CR1_ARPE;      // Auto preload ENABLE
    TIM2->CR1 |= TIM_CR1_CEN;       // ENABLE Timer counter     

}

/**************User declared variables************************/
int pwm = 200;
uint16_t dataReturn;
int compareData;
/**************Main Function**********************************/
int main()
 {  
		PWM_Init();                                // initialize PWM
    GPIOInit();																 // sets up GPIOs for SPI and PWM ***MUST BE RAN AFTER PWM FOR TIMERS****
		spi_Init();																 // initialize SPI
  
						TIM2->CCR1  = pwm;                 // SET duty cycle chan 1
						TIM2->CCR2  = pwm;                 // SET duty cycle chan 2
						TIM2->CCR3  = pwm;                 // SET duty cycle chan 3
						TIM2->CCR4  = pwm;                 // SET duty cycle chan 4
					
        while(1)
        {
           
						dataReturn = SPIReadWrite(0x4001);
						//dataReturn = dataReturn & 0x3FFF;
						//dataReturn = dataReturn / 10;
						//dataReturn = dataReturn / 4.55;
						compareData = dataReturn;
						if (compareData == 0){
						GPIOA->BSRR |= GPIO_BSRR_BS8; //set green led
						GPIOA->BSRR |= GPIO_BSRR_BR9; // reset red led
						}
						else {
						GPIOA->BSRR |= GPIO_BSRR_BS9; //set red led
						GPIOA->BSRR |= GPIO_BSRR_BR8; // reset green led
						}

        }
  }
 

Parents Reply Children
No data