K22 processor

Committer:
bosko001
Date:
Sun Mar 21 18:20:15 2021 +0100
Revision:
7:4aa3dac73b66
Child:
12:ea2256d6c725
novi pocetak

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 7:4aa3dac73b66 1 #ifndef SLAVESPI_H
bosko001 7:4aa3dac73b66 2 #define SLAVESPI_H
bosko001 7:4aa3dac73b66 3
bosko001 7:4aa3dac73b66 4
bosko001 7:4aa3dac73b66 5 #include "mbed.h"
bosko001 7:4aa3dac73b66 6 #include "Pinovi.h"
bosko001 7:4aa3dac73b66 7 #include "ClassThreadAbstract.h"
bosko001 7:4aa3dac73b66 8
bosko001 7:4aa3dac73b66 9 //typedef void (*pf_pc_c)(char* tx, char rx);
bosko001 7:4aa3dac73b66 10
bosko001 7:4aa3dac73b66 11 //static DigitalOut ledG(LED2,1);
bosko001 7:4aa3dac73b66 12 //static DigitalOut ledC(LED1,1);
bosko001 7:4aa3dac73b66 13
bosko001 7:4aa3dac73b66 14 static DigitalOut _CS( PTB18,1);
bosko001 7:4aa3dac73b66 15
bosko001 7:4aa3dac73b66 16
bosko001 7:4aa3dac73b66 17 typedef void (*pfv)(void);
bosko001 7:4aa3dac73b66 18
bosko001 7:4aa3dac73b66 19 #define _SPI_BASE SPI1_BASE
bosko001 7:4aa3dac73b66 20 #define _SPI_IRQn SPI1_IRQn
bosko001 7:4aa3dac73b66 21
bosko001 7:4aa3dac73b66 22
bosko001 7:4aa3dac73b66 23 static char readData, writeData=0x7e;
bosko001 7:4aa3dac73b66 24 static volatile char _transferFlag;// = false;
bosko001 7:4aa3dac73b66 25
bosko001 7:4aa3dac73b66 26 static EventFlags ef;
bosko001 7:4aa3dac73b66 27
bosko001 7:4aa3dac73b66 28
bosko001 7:4aa3dac73b66 29 class C_slaveSpi: public SPISlave
bosko001 7:4aa3dac73b66 30 {
bosko001 7:4aa3dac73b66 31
bosko001 7:4aa3dac73b66 32 public:
bosko001 7:4aa3dac73b66 33
bosko001 7:4aa3dac73b66 34
bosko001 7:4aa3dac73b66 35 static int spiReadRegister( int ar ){ return *(int*)( _SPI_BASE + ar);}
bosko001 7:4aa3dac73b66 36 static void spiWriteRegister( int ar, int d ){ *(int*)( _SPI_BASE + ar) = d;}
bosko001 7:4aa3dac73b66 37
bosko001 7:4aa3dac73b66 38 static void inic_slaveSpi( void )
bosko001 7:4aa3dac73b66 39 {
bosko001 7:4aa3dac73b66 40 NVIC_DisableIRQ( _SPI_IRQn );
bosko001 7:4aa3dac73b66 41 _CS = 1;
bosko001 7:4aa3dac73b66 42 wait_us(10);
bosko001 7:4aa3dac73b66 43 _CS = 0;
bosko001 7:4aa3dac73b66 44 spiWriteRegister(0, spiReadRegister(0) | 0x3c00); /* 0x00000C00 = ciscenje rx i tx buffer-a*/
bosko001 7:4aa3dac73b66 45 spiWriteRegister(0x2c, spiReadRegister(0x2c) | 0x80000000); /* TCF clear */
bosko001 7:4aa3dac73b66 46 spiWriteRegister(0x30, spiReadRegister(0x30) | 0x80000000); /* transfer interrupt enable */
bosko001 7:4aa3dac73b66 47
bosko001 7:4aa3dac73b66 48 spiWriteRegister(0x34, writeData); /* data for transmit */
bosko001 7:4aa3dac73b66 49 }
bosko001 7:4aa3dac73b66 50
bosko001 7:4aa3dac73b66 51 static void ff(void )
bosko001 7:4aa3dac73b66 52 {
bosko001 7:4aa3dac73b66 53 if( ( spiReadRegister(0x2c)) & 0x80000000 )
bosko001 7:4aa3dac73b66 54 {
bosko001 7:4aa3dac73b66 55 spiWriteRegister(0x2c, spiReadRegister(0x2c) | 0x80000000);
bosko001 7:4aa3dac73b66 56 readData = spiReadRegister(0x38); /* data register read */
bosko001 7:4aa3dac73b66 57 spiWriteRegister(0x34, writeData); /* data for transmit */
bosko001 7:4aa3dac73b66 58
bosko001 7:4aa3dac73b66 59
bosko001 7:4aa3dac73b66 60 _transferFlag = 1;
bosko001 7:4aa3dac73b66 61 ef.set(1);
bosko001 7:4aa3dac73b66 62 }
bosko001 7:4aa3dac73b66 63
bosko001 7:4aa3dac73b66 64 // else { ledG =! ledG; inic_slaveSpi( ); set_slaveSpiInterrupt(ff);}
bosko001 7:4aa3dac73b66 65 // ledC =! ledC;
bosko001 7:4aa3dac73b66 66 // spiByteTransfer( &writeData, readData );
bosko001 7:4aa3dac73b66 67 }
bosko001 7:4aa3dac73b66 68
bosko001 7:4aa3dac73b66 69 static void set_slaveSpiInterrupt( pfv p_fun )
bosko001 7:4aa3dac73b66 70 {
bosko001 7:4aa3dac73b66 71 NVIC_SetVector( _SPI_IRQn, (int)p_fun ); // varijanta sa static ff()
bosko001 7:4aa3dac73b66 72 NVIC_EnableIRQ( _SPI_IRQn );
bosko001 7:4aa3dac73b66 73
bosko001 7:4aa3dac73b66 74 }
bosko001 7:4aa3dac73b66 75
bosko001 7:4aa3dac73b66 76 // class C_slaveSpi: SPISlave, C_thread
bosko001 7:4aa3dac73b66 77 // {
bosko001 7:4aa3dac73b66 78 private:
bosko001 7:4aa3dac73b66 79
bosko001 7:4aa3dac73b66 80 char bitCount = 0;
bosko001 7:4aa3dac73b66 81 Thread *p_t;
bosko001 7:4aa3dac73b66 82
bosko001 7:4aa3dac73b66 83
bosko001 7:4aa3dac73b66 84 void (*pfv) (void);
bosko001 7:4aa3dac73b66 85 void spiClockCount_fun(void);
bosko001 7:4aa3dac73b66 86
bosko001 7:4aa3dac73b66 87 Callback<void(void)> cbfun;
bosko001 7:4aa3dac73b66 88
bosko001 7:4aa3dac73b66 89 public:
bosko001 7:4aa3dac73b66 90
bosko001 7:4aa3dac73b66 91 C_slaveSpi( ): SPISlave(Mosi, Miso, SpiClock, SpiSel )
bosko001 7:4aa3dac73b66 92 {
bosko001 7:4aa3dac73b66 93 // transferFlag = false;
bosko001 7:4aa3dac73b66 94
bosko001 7:4aa3dac73b66 95 inic_slaveSpi( );
bosko001 7:4aa3dac73b66 96 format(8,3);
bosko001 7:4aa3dac73b66 97 set_slaveSpiInterrupt( ff );
bosko001 7:4aa3dac73b66 98
bosko001 7:4aa3dac73b66 99 p_t = new Thread( osPriorityAboveNormal, 1000 );
bosko001 7:4aa3dac73b66 100 p_t->start( [this](){thread_fun();} );
bosko001 7:4aa3dac73b66 101
bosko001 7:4aa3dac73b66 102 // cbfun = &(this->clear_bitCount);
bosko001 7:4aa3dac73b66 103 }
bosko001 7:4aa3dac73b66 104 // void attach( pf_pc_c fun) { transfer_fun = fun;}
bosko001 7:4aa3dac73b66 105 virtual void spiByteTransfer( char * tx, char rx) =0;
bosko001 7:4aa3dac73b66 106 void thread_fun( void )
bosko001 7:4aa3dac73b66 107 {
bosko001 7:4aa3dac73b66 108 extern volatile char _transferFlag;
bosko001 7:4aa3dac73b66 109 while( true )
bosko001 7:4aa3dac73b66 110 {
bosko001 7:4aa3dac73b66 111 // ledC =! ledC;
bosko001 7:4aa3dac73b66 112 // if( _transferFlag )
bosko001 7:4aa3dac73b66 113 while(ef.wait_any(1) )
bosko001 7:4aa3dac73b66 114 {
bosko001 7:4aa3dac73b66 115 // ledC =! ledC;
bosko001 7:4aa3dac73b66 116 spiByteTransfer(&writeData, readData);
bosko001 7:4aa3dac73b66 117 //printf(" %x %x \n\r",readData, writeData);
bosko001 7:4aa3dac73b66 118 _transferFlag = 0;
bosko001 7:4aa3dac73b66 119 }
bosko001 7:4aa3dac73b66 120 }
bosko001 7:4aa3dac73b66 121 }
bosko001 7:4aa3dac73b66 122
bosko001 7:4aa3dac73b66 123
bosko001 7:4aa3dac73b66 124
bosko001 7:4aa3dac73b66 125 char get_bitCount( void ) { return bitCount;}
bosko001 7:4aa3dac73b66 126 void clear_bitCount( void ) { /*ledG =! ledG;*/ bitCount = 0; inic_slaveSpi( ); set_slaveSpiInterrupt(ff);}
bosko001 7:4aa3dac73b66 127
bosko001 7:4aa3dac73b66 128 void spi_monitor( void ){};
bosko001 7:4aa3dac73b66 129
bosko001 7:4aa3dac73b66 130 };
bosko001 7:4aa3dac73b66 131
bosko001 7:4aa3dac73b66 132
bosko001 7:4aa3dac73b66 133
bosko001 7:4aa3dac73b66 134 #endif