State structure moved to separate file. Cmd poll function moved to separate file.
authorMartin Prudek <prudemar@fel.cvut.cz>
Sun, 10 May 2015 19:24:45 +0000 (21:24 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Sun, 10 May 2015 19:24:45 +0000 (21:24 +0200)
pmsm-control/test_sw/Makefile
pmsm-control/test_sw/cmd_proc.c [new file with mode: 0644]
pmsm-control/test_sw/cmd_proc.h [new file with mode: 0644]
pmsm-control/test_sw/main_pmsm.c
pmsm-control/test_sw/misc.h
pmsm-control/test_sw/pmsm_state.h [new file with mode: 0644]

index 10b8442..91f533a 100644 (file)
@@ -49,5 +49,5 @@ blikej: howto_gpio.o rpi_hw.o
 spi: rp_spi.o
        gcc -o spi rp_spi.c
 #pro rpi
-pmsm: main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o
-       gcc -o pmsm_controll main_pmsm.o rp_spi.o rpi_hw.o  misc.o pxmc_sin_fixtab.o -lpthread -lrt
+pmsm: main_pmsm.o rp_spi.o rpi_hw.o misc.o pxmc_sin_fixtab.o cmd_proc.o
+       gcc -o pmsm_controll main_pmsm.o rp_spi.o rpi_hw.o  misc.o pxmc_sin_fixtab.o cmd_proc.o -lpthread -lrt
diff --git a/pmsm-control/test_sw/cmd_proc.c b/pmsm-control/test_sw/cmd_proc.c
new file mode 100644 (file)
index 0000000..72260b5
--- /dev/null
@@ -0,0 +1,74 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "cmd_proc.h"
+
+
+/**
+ * \brief
+ * Commands detection.
+ */
+void poll_cmd(struct rpi_state* state){
+       unsigned int tmp;
+       /*
+        * Note:
+        * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
+        * k preukladani hodnot na promenne test. Dost divne.
+        */
+       while (1){
+               scanf("%u",&tmp);
+               printf("volba=%u\n",tmp);
+               switch (tmp){
+               case 1:
+                       scanf("%u",&tmp);
+                       sem_wait(&state->thd_par_sem);
+                       state->pwm1=tmp&0xFFF;
+                       sem_post(&state->thd_par_sem);
+                       break;
+               case 2:
+                       scanf("%u",&tmp);
+                       sem_wait(&state->thd_par_sem);
+                       state->pwm2=tmp&0xFFF;
+                       sem_post(&state->thd_par_sem);
+                       break;
+               case 3:
+                       scanf("%u",&tmp);
+                       sem_wait(&state->thd_par_sem);
+                       state->pwm3=tmp&0xFFF;
+                       sem_post(&state->thd_par_sem);
+                       break;
+               case 4:
+                       scanf("%u",&tmp);
+                       sem_wait(&state->thd_par_sem);
+                       state->test=tmp&0xFF;
+                       sem_post(&state->thd_par_sem);
+                       break;
+               case 5:
+                       sem_wait(&state->thd_par_sem);
+                       state->commutate=!state->commutate;
+                       /* switch off pwms at the end of commutation */
+                       state->pwm1&=state->commutate*0xFFFF;
+                       state->pwm2&=state->commutate*0xFFFF;
+                       state->pwm3&=state->commutate*0xFFFF;
+                       sem_post(&state->thd_par_sem);
+                       break;
+               case 6:
+                       scanf("%d",&tmp);
+                       sem_wait(&state->thd_par_sem);
+                       state->duty=tmp;
+                       sem_post(&state->thd_par_sem);
+                       break;
+               case 7:
+                       scanf("%d",&tmp);
+                       sem_wait(&state->thd_par_sem);
+                       state->desired_pos=tmp;
+                       sem_post(&state->thd_par_sem);
+                       break;
+
+               default:
+                       break;
+               }
+
+       }
+       return ;
+}
diff --git a/pmsm-control/test_sw/cmd_proc.h b/pmsm-control/test_sw/cmd_proc.h
new file mode 100644 (file)
index 0000000..52d6627
--- /dev/null
@@ -0,0 +1,3 @@
+#include "pmsm_state.h"
+
+void poll_cmd(struct rpi_state* state);
index 8706dc9..20e6417 100644 (file)
@@ -21,8 +21,8 @@
 #include "rp_spi.h"    /*spi*/
 #include "misc.h"      /*structure for priorities*/
 #include "pxmc_sin_fixed.h"    /*to test sin commutation */
-
-
+#include "pmsm_state.h"
+#include "cmd_proc.h"
 
 #define PRUM_PROUD     2061
 #define PRUM_SOUC      6183
 #define NSEC_PER_SEC    (1000000000) /* The number of nsecs per sec. */
 
 
-
 struct rpi_in data;
-struct rpi_state{
-       uint8_t test;
-       uint16_t pwm1, pwm2, pwm3;
-       uint16_t t_pwm1, t_pwm2, t_pwm3;
-       char commutate;
-       int duty;                       /* duty cycle of pwm */
-       uint16_t index_dist;            /* distance to index position */
-       unsigned char index_ok;
-       uint32_t tf_count;              /*number of transfer*/
-       int desired_pos;                /* desired position */
-}rps;
 
 
 /**
@@ -80,7 +68,7 @@ void appl_stop(){
        spi_disable();
        clk_disable();
        /*muzeme zavrit semafor*/
-       sem_destroy(&thd_par_sem);
+       sem_destroy(&rps.thd_par_sem);
         printf("\nprogram bezpecne ukoncen\n");
 }
 
@@ -110,10 +98,10 @@ void printData(){
        float cur0, cur1, cur2;
        int i;
        /* copy the data */
-       sem_wait(&thd_par_sem);
+       sem_wait(&rps.thd_par_sem);
        data_p = data;
        s=rps;
-       sem_post(&thd_par_sem);
+       sem_post(&rps.thd_par_sem);
 
        if (data_p.adc_m_count){
                cur0=data_p.ch0/data_p.adc_m_count;
@@ -481,7 +469,7 @@ void * read_data(void* param){
                while(1){
                        /* wait until next shot */
                        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
-                       sem_wait(&thd_par_sem);         /*---take semaphore---*/
+                       sem_wait(&rps.thd_par_sem);             /*---take semaphore---*/
                        prepare_tx(tx);                 /*save the data to send*/
                        data = spi_read(tx);            /*exchange data*/
                        /*subtract initiate postion */
@@ -503,7 +491,7 @@ void * read_data(void* param){
                        }else if(!rps.index_ok && rps.commutate){
                                simple_hall_commutator(rps.duty);
                        }
-                       sem_post(&thd_par_sem);         /*--post semaphore---*/
+                       sem_post(&rps.thd_par_sem);             /*--post semaphore---*/
 
                        /* calculate next shot */
                        t.tv_nsec += interval;
@@ -517,74 +505,7 @@ void * read_data(void* param){
 }
 
 
-/**
- * \brief
- * Commands detection.
- */
-void poll_cmd(){
-       unsigned int tmp;
-       /*
-        * Note:
-        * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
-        * k preukladani hodnot na promenne test. Dost divne.
-        */
-       while (1){
-               scanf("%u",&tmp);
-               printf("volba=%u\n",tmp);
-               switch (tmp){
-               case 1:
-                       scanf("%u",&tmp);
-                       sem_wait(&thd_par_sem);
-                       rps.pwm1=tmp&0xFFF;
-                       sem_post(&thd_par_sem);
-                       break;
-               case 2:
-                       scanf("%u",&tmp);
-                       sem_wait(&thd_par_sem);
-                       rps.pwm2=tmp&0xFFF;
-                       sem_post(&thd_par_sem);
-                       break;
-               case 3:
-                       scanf("%u",&tmp);
-                       sem_wait(&thd_par_sem);
-                       rps.pwm3=tmp&0xFFF;
-                       sem_post(&thd_par_sem);
-                       break;
-               case 4:
-                       scanf("%u",&tmp);
-                       sem_wait(&thd_par_sem);
-                       rps.test=tmp&0xFF;
-                       sem_post(&thd_par_sem);
-                       break;
-               case 5:
-                       sem_wait(&thd_par_sem);
-                       rps.commutate=!rps.commutate;
-                       /* switch off pwms at the end of commutation */
-                       rps.pwm1&=rps.commutate*0xFFFF;
-                       rps.pwm2&=rps.commutate*0xFFFF;
-                       rps.pwm3&=rps.commutate*0xFFFF;
-                       sem_post(&thd_par_sem);
-                       break;
-               case 6:
-                       scanf("%d",&tmp);
-                       sem_wait(&thd_par_sem);
-                       rps.duty=tmp;
-                       sem_post(&thd_par_sem);
-                       break;
-               case 7:
-                       scanf("%d",&tmp);
-                       sem_wait(&thd_par_sem);
-                       rps.desired_pos=tmp;
-                       sem_post(&thd_par_sem);
-                       break;
-
-               default:
-                       break;
-               }
 
-       }
-       return ;
-}
 /**
  * \brief Main function.
  */
@@ -595,7 +516,7 @@ int main(){
        spi_init();             /* iniicializace spi*/
 
        /*semafor pro detekci zpracovani parametru vlaken*/
-       sem_init(&thd_par_sem,THREAD_SHARED,INIT_VALUE);
+       sem_init(&rps.thd_par_sem,THREAD_SHARED,INIT_VALUE);
        setup_environment();
 
        base_thread_id=pthread_self();
@@ -607,7 +528,7 @@ int main(){
        create_rt_task(&base_thread_id,PRIOR_LOW,pos_monitor,NULL);
 
        /*wait for commands*/
-       poll_cmd();
+       poll_cmd(&rps);
 
        return 0;
 }
index 7a131c0..b7783e4 100644 (file)
 #define NULL (void*)0
 #endif /*NULL*/
 
-#include <semaphore.h>
-
-sem_t thd_par_sem; ///< semafor pro detekci zpracovani parametru noveho vlakna
-
 /**
  * \brief
  * Setup initial environment.
diff --git a/pmsm-control/test_sw/pmsm_state.h b/pmsm-control/test_sw/pmsm_state.h
new file mode 100644 (file)
index 0000000..5f63acf
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef PMSM_STATE
+#define PMSM_STATE
+
+#include <stdint.h>
+#include <semaphore.h>
+
+
+struct rpi_state{
+       sem_t thd_par_sem;
+       uint8_t test;
+       uint16_t pwm1, pwm2, pwm3;
+       uint16_t t_pwm1, t_pwm2, t_pwm3;
+       char commutate;
+       int duty;                       /* duty cycle of pwm */
+       uint16_t index_dist;            /* distance to index position */
+       unsigned char index_ok;
+       uint32_t tf_count;              /*number of transfer*/
+       int desired_pos;                /* desired position */
+}rps;
+
+#endif /*PMSM_STATE*/