From b4d468f4be4d8e91bbd775b5b37f0ae431ae6648 Mon Sep 17 00:00:00 2001 From: Martin Prudek Date: Wed, 20 May 2015 18:52:56 +0200 Subject: [PATCH] All SPI stuff moved to rp_spi.c --- pmsm-control/test_sw/main_pmsm.c | 81 +++----------------- pmsm-control/test_sw/rp_spi.c | 127 +++++++++++++++++++++++-------- pmsm-control/test_sw/rp_spi.h | 4 +- 3 files changed, 108 insertions(+), 104 deletions(-) diff --git a/pmsm-control/test_sw/main_pmsm.c b/pmsm-control/test_sw/main_pmsm.c index a9c6329..f4ce30b 100644 --- a/pmsm-control/test_sw/main_pmsm.c +++ b/pmsm-control/test_sw/main_pmsm.c @@ -139,71 +139,8 @@ void makeLog(){ */ } -/* - * \brief - * Pripravi psi buffer - */ -void prepare_tx(uint8_t * tx){ - - /*Data format: - * tx[4] - bity 95 downto 88 - bits that are sent first - * tx[5] - bity 87 downto 80 - * tx[6] - bity 79 downto 72 - * tx[7] - bity 71 downto 64 - * tx[8] - bity 63 downto 56 - * tx[9] - bity 55 downto 48 - * tx[10] - bity 47 downto 40 - * tx[11] - bity 39 downto 32 - * tx[12] - bity 31 downto 24 - * tx[13] - bity 23 downto 16 - * tx[14] - bity 15 downto 8 - * tx[15] - bity 7 downto 0 - * - * bit 95 - ADC reset - * bit 94 - enable PWM1 - * bit 93 - enable PWM2 - * bit 92 - enable PWM3 - * bit 91 - shutdown1 - * bit 90 - shutdown2 - * bit 89 - shutdown3 - * . - * . - * Unused - * . - * . - * bits 47 .. 32 - match PWM1 - * bits 31 .. 16 - match PWM2 - * bits 15 .. 0 - match PWM3 - */ - - - uint16_t tmp; - - /* keep the 11-bit cap*/ - - if (rps.pwm1>2047) rps.pwm1=2047; - if (rps.pwm2>2047) rps.pwm2=2047; - if (rps.pwm3>2047) rps.pwm3=2047; - - tx[0]=rps.test; /*bit 94 - enable PWM1*/ - - /*now we have to switch the bytes due to endianess */ - /* ARMv6 & ARMv7 instructions are little endian */ - /*pwm1*/ - tx[10]=((uint8_t*)&rps.pwm1)[1]; /*MSB*/ - tx[11]=((uint8_t*)&rps.pwm1)[0]; /*LSB*/ - - /*pwm2*/ - tx[12]=((uint8_t*)&rps.pwm2)[1]; /*MSB*/ - tx[13]=((uint8_t*)&rps.pwm2)[0]; /*LSB*/ - - /*pwm3*/ - tx[14]=((uint8_t*)&rps.pwm3)[1]; /*MSB*/ - tx[15]=((uint8_t*)&rps.pwm3)[0]; /*LSB*/ -} - /** * \brief Signal handler pro Ctrl+C @@ -216,8 +153,7 @@ void appl_stop(){ rps.pwm1=0; rps.pwm2=0; rps.pwm3=0; - prepare_tx(tx); /*save the data to send*/ - data=spi_read(tx); + spi_read(&rps); spi_disable(); clk_disable(); @@ -334,6 +270,8 @@ void compSpeed(){ rps.speed=(int32_t)spd; } + + /* * \brief * Feedback loop. @@ -342,12 +280,16 @@ void compSpeed(){ void * read_data(void* param){ int i; struct rpi_in pocatek; + struct rpi_state poc={ + .spi_dat=&pocatek, + .test=0, + .pwm1=0, .pwm1=0, .pwm3=0 + }; struct timespec t; int interval = 1000000; /* 1ms ~ 1kHz*/ - uint8_t tx[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ; char first=1; uint16_t last_index; /*we have index up-to date*/ - pocatek = spi_read(tx); + spi_read(&poc); /*pocatecni informace*/ clock_gettime(CLOCK_MONOTONIC ,&t); /* start after one second */ t.tv_sec++; @@ -358,11 +300,10 @@ void * read_data(void* param){ /*old positions*/ rps.old_pos[rps.tf_count%OLD_POS_NUM]=rps.spi_dat->pozice; - prepare_tx(tx); /*save the data to send*/ - data = spi_read(tx); /*exchange data*/ + spi_read(&rps); /*exchange data*/ /*subtract initiate postion */ rps.tf_count++; - substractOffset(&data,&pocatek); + substractOffset(&data,poc.spi_dat); compSpeed(); /*spocita rychlost*/ if (!rps.index_ok){ diff --git a/pmsm-control/test_sw/rp_spi.c b/pmsm-control/test_sw/rp_spi.c index 6a51a31..d02c1ab 100644 --- a/pmsm-control/test_sw/rp_spi.c +++ b/pmsm-control/test_sw/rp_spi.c @@ -14,6 +14,7 @@ #include #include "rp_spi.h" /*struct rpi_in */ +#include "pmsm_state.h" //#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #define ARRAY_SIZE 16 @@ -65,7 +66,7 @@ void transfer() } puts(""); } -struct rpi_in spi_read(uint8_t * tx) +void spi_read(struct rpi_state* this) { uint8_t *uint8_p; uint16_t tmp; @@ -73,7 +74,8 @@ struct rpi_in spi_read(uint8_t * tx) struct rpi_in in; int ret; - uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */ + uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu */ + uint8_t tx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu */ struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx, .rx_buf = (unsigned long)rx, @@ -85,14 +87,75 @@ struct rpi_in spi_read(uint8_t * tx) memset(&in,0,sizeof(in)); + /*Data format: + * tx[4] - bity 95 downto 88 - bits that are sent first + * tx[5] - bity 87 downto 80 + * tx[6] - bity 79 downto 72 + * tx[7] - bity 71 downto 64 + * tx[8] - bity 63 downto 56 + * tx[9] - bity 55 downto 48 + * tx[10] - bity 47 downto 40 + * tx[11] - bity 39 downto 32 + * tx[12] - bity 31 downto 24 + * tx[13] - bity 23 downto 16 + * tx[14] - bity 15 downto 8 + * tx[15] - bity 7 downto 0 + * + * bit 95 - ADC reset + * bit 94 - enable PWM1 + * bit 93 - enable PWM2 + * bit 92 - enable PWM3 + * bit 91 - shutdown1 + * bit 90 - shutdown2 + * bit 89 - shutdown3 + * . + * . + * Unused + * . + * . + * bits 47 .. 32 - match PWM1 + * bits 31 .. 16 - match PWM2 + * bits 15 .. 0 - match PWM3 + */ + + + + /* keep the 11-bit cap*/ + + if (this->pwm1>2047) this->pwm1=2047; + if (this->pwm2>2047) this->pwm2=2047; + if (this->pwm3>2047) this->pwm3=2047; + + tx[0]=this->test; /*bit 94 - enable PWM1*/ + + /*now we have to switch the bytes due to endianess */ + /* ARMv6 & ARMv7 instructions are little endian */ + /*pwm1*/ + tx[10]=((uint8_t*)&this->pwm1)[1]; /*MSB*/ + tx[11]=((uint8_t*)&this->pwm1)[0]; /*LSB*/ + + /*pwm2*/ + tx[12]=((uint8_t*)&this->pwm2)[1]; /*MSB*/ + tx[13]=((uint8_t*)&this->pwm2)[0]; /*LSB*/ + + /*pwm3*/ + tx[14]=((uint8_t*)&this->pwm3)[1]; /*MSB*/ + tx[15]=((uint8_t*)&this->pwm3)[0]; /*LSB*/ + + /*----------------------------------------*/ + ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); - /*vypisovani prichozich dat */ - if (ret < 1) + + if (ret < 1){ pabort("can't send spi message"); + } + + /*-----------------------------------------*/ + for (ret = 0; ret < ARRAY_SIZE; ret++) { - in.debug_rx[ret]=rx[ret]; + this->spi_dat->debug_rx[ret]=rx[ret]; } /*/ @@ -117,7 +180,7 @@ struct rpi_in spi_read(uint8_t * tx) */ /*uprava endianity pozice*/ - uint8_p=(uint8_t*)&in.pozice_raw; + uint8_p=(uint8_t*)&this->spi_dat->pozice_raw; /* x86 je Little-Endian */ uint8_p[0]=rx[3]; /* LSB */ uint8_p[1]=rx[2]; @@ -129,19 +192,19 @@ struct rpi_in spi_read(uint8_t * tx) * hal2 - bit94 * hal3 - bit93 */ - in.hal1=!!(0x80 & rx[4]); - in.hal2=!!(0x40 & rx[4]); - in.hal3=!!(0x20 & rx[4]); + this->spi_dat->hal1=!!(0x80 & rx[4]); + this->spi_dat->hal2=!!(0x40 & rx[4]); + this->spi_dat->hal3=!!(0x20 & rx[4]); /* index position * bits 92 downto 81 * 92..88 in rx[4] last 5 bits (from left) * 87..81 in rx[5] first 7 bits (from left) */ - in.index_position=0x1F & rx[4]; - in.index_position<<=8; - in.index_position|=0xFE & rx[5]; - in.index_position>>=1; + this->spi_dat->index_position=0x1F & rx[4]; + this->spi_dat->index_position<<=8; + this->spi_dat->index_position|=0xFE & rx[5]; + this->spi_dat->index_position>>=1; /* current measurments count * bits 80 downto 72 @@ -149,9 +212,9 @@ struct rpi_in spi_read(uint8_t * tx) * bits 79..72 in rx[6] */ - in.adc_m_count=0x01 & rx[5]; - in.adc_m_count<<=8; - in.adc_m_count|=rx[6]; + this->spi_dat->adc_m_count=0x01 & rx[5]; + this->spi_dat->adc_m_count<<=8; + this->spi_dat->adc_m_count|=rx[6]; /** currents @@ -169,26 +232,24 @@ struct rpi_in spi_read(uint8_t * tx) * 7..0 in rx[15] - all byte */ - in.ch2=rx[7]; - in.ch2<<=8; - in.ch2|=rx[8]; - in.ch2<<=8; - in.ch2|=rx[9]; - - in.ch0=rx[10]; - in.ch0<<=8; - in.ch0|=rx[11]; - in.ch0<<=8; - in.ch0|=rx[12]; + this->spi_dat->ch2=rx[7]; + this->spi_dat->ch2<<=8; + this->spi_dat->ch2|=rx[8]; + this->spi_dat->ch2<<=8; + this->spi_dat->ch2|=rx[9]; - in.ch1=rx[13]; - in.ch1<<=8; - in.ch1|=rx[14]; - in.ch1<<=8; - in.ch1|=rx[15]; + this->spi_dat->ch0=rx[10]; + this->spi_dat->ch0<<=8; + this->spi_dat->ch0|=rx[11]; + this->spi_dat->ch0<<=8; + this->spi_dat->ch0|=rx[12]; + this->spi_dat->ch1=rx[13]; + this->spi_dat->ch1<<=8; + this->spi_dat->ch1|=rx[14]; + this->spi_dat->ch1<<=8; + this->spi_dat->ch1|=rx[15]; - return in; } diff --git a/pmsm-control/test_sw/rp_spi.h b/pmsm-control/test_sw/rp_spi.h index 1be10b9..4b1cfa6 100644 --- a/pmsm-control/test_sw/rp_spi.h +++ b/pmsm-control/test_sw/rp_spi.h @@ -20,6 +20,8 @@ struct rpi_in{ uint16_t index_position; /* raw position of irc_i */ }; +struct rpi_state; + /** * \brief Testovaci funkce. */ @@ -35,6 +37,6 @@ int spi_init(void); */ void spi_disable(void); -struct rpi_in spi_read(uint8_t *); +void spi_read(struct rpi_state*); #endif /*RP_SPI*/ -- 2.39.2