prva verzija, poslednji bajt je sporan

Committer:
bosko001
Date:
Tue Jul 28 17:17:51 2020 +0200
Revision:
3:3b0754ed31e9
Parent:
1:dd44d6b64300
funkcionalan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 0:c06c791ca345 1 #include "MiWiApp.h"
bosko001 0:c06c791ca345 2
bosko001 0:c06c791ca345 3
bosko001 0:c06c791ca345 4 #include "mbed.h"
bosko001 0:c06c791ca345 5
bosko001 1:dd44d6b64300 6 #define MiWiADDRESS 0
bosko001 1:dd44d6b64300 7
bosko001 0:c06c791ca345 8
bosko001 0:c06c791ca345 9 const char _MIWI_DEV_CFG[ 32 ] =
bosko001 0:c06c791ca345 10 {
bosko001 0:c06c791ca345 11 0x20 | 0x10 | 0, //0x08 //0x06,1
bosko001 0:c06c791ca345 12 0x80 | 0x04 | 0x00,
bosko001 0:c06c791ca345 13 0x04,
bosko001 0:c06c791ca345 14 0x07,
bosko001 0:c06c791ca345 15 0x0C,
bosko001 0:c06c791ca345 16 0xC0 | 0x02,
bosko001 0:c06c791ca345 17 0x77,
bosko001 0:c06c791ca345 18 95, //0x64,
bosko001 0:c06c791ca345 19 1, //0x32,
bosko001 0:c06c791ca345 20 0x74,
bosko001 0:c06c791ca345 21 0x62,
bosko001 0:c06c791ca345 22 0x32,
bosko001 0:c06c791ca345 23 0x20 | 0x18,
bosko001 0:c06c791ca345 24 /*0xC0*/0x0 | 0x08, //irq0=PLREADY irq1=CRCOK TXDONE
bosko001 0:c06c791ca345 25 0x20 | 0x09, // TXDONE RSSI en, PLL en
bosko001 0:c06c791ca345 26 0x30, //0x00, //// RSSI threshold = 50
bosko001 0:c06c791ca345 27 0xA0 | 0x03,
bosko001 0:c06c791ca345 28 0x08 | 0x30,
bosko001 0:c06c791ca345 29 0x20 | 0x18,
bosko001 0:c06c791ca345 30 0x7, //0x07, //reserved read only
bosko001 0:c06c791ca345 31 0, //rssi read only
bosko001 0:c06c791ca345 32 0, // OOK config reg
bosko001 0:c06c791ca345 33 0x69,
bosko001 0:c06c791ca345 34 0x81,
bosko001 0:c06c791ca345 35 0x7E,
bosko001 0:c06c791ca345 36 0x96,
bosko001 0:c06c791ca345 37 0x78, //Tx polfv = 200KHz Tx pow = 1dbm ///0xF0,
bosko001 0:c06c791ca345 38 0, //0x80,
bosko001 0:c06c791ca345 39 64,
bosko001 0:c06c791ca345 40 0x00,
bosko001 0:c06c791ca345 41 0x80 | 0x60 | 0x08, // variable packet, 4 byte preamble, CRC en
bosko001 0:c06c791ca345 42 0x00
bosko001 0:c06c791ca345 43 };
bosko001 0:c06c791ca345 44
bosko001 0:c06c791ca345 45
bosko001 0:c06c791ca345 46
bosko001 0:c06c791ca345 47 SPI spi( PTD2, PTD3, PTD1 );
bosko001 0:c06c791ca345 48 DigitalOut csdata( PTD0,1 );
bosko001 0:c06c791ca345 49 DigitalOut csconfig( PTB2,1 );
bosko001 0:c06c791ca345 50 DigitalOut MiWiReset( PTB11, 1 );
bosko001 0:c06c791ca345 51
bosko001 0:c06c791ca345 52 DigitalIn pin_irq0( PTB9 );
bosko001 0:c06c791ca345 53 DigitalIn pin_irq1( PTC2 );
bosko001 0:c06c791ca345 54
bosko001 0:c06c791ca345 55
bosko001 1:dd44d6b64300 56
bosko001 1:dd44d6b64300 57 Mutex m;
bosko001 1:dd44d6b64300 58
bosko001 1:dd44d6b64300 59 char readMiWiReg( char reg )
bosko001 1:dd44d6b64300 60 {
bosko001 1:dd44d6b64300 61 char c;
bosko001 1:dd44d6b64300 62 m.lock();
bosko001 1:dd44d6b64300 63 csconfig=0;
bosko001 1:dd44d6b64300 64 spi.write( (reg)*2 + 0x40 );
bosko001 1:dd44d6b64300 65 c = spi.write( 0 );
bosko001 1:dd44d6b64300 66 csconfig = 1;
bosko001 1:dd44d6b64300 67 m.unlock();
bosko001 1:dd44d6b64300 68 return c;
bosko001 1:dd44d6b64300 69 }
bosko001 1:dd44d6b64300 70 void writeMiWiReg( char reg, char data )
bosko001 1:dd44d6b64300 71 {
bosko001 1:dd44d6b64300 72 char c;
bosko001 1:dd44d6b64300 73 m.lock();
bosko001 1:dd44d6b64300 74 csconfig=0;
bosko001 1:dd44d6b64300 75 spi.write( (reg)*2 /*& ~0x40*/ );
bosko001 1:dd44d6b64300 76 spi.write( data );
bosko001 1:dd44d6b64300 77 csconfig = 1;
bosko001 1:dd44d6b64300 78 m.unlock();
bosko001 1:dd44d6b64300 79 }
bosko001 1:dd44d6b64300 80
bosko001 1:dd44d6b64300 81 void writeMiWiData( char data )
bosko001 1:dd44d6b64300 82 {
bosko001 1:dd44d6b64300 83 char c;
bosko001 1:dd44d6b64300 84 m.lock();
bosko001 1:dd44d6b64300 85 csdata=0;
bosko001 1:dd44d6b64300 86 spi.write( data );
bosko001 1:dd44d6b64300 87 csdata = 1;
bosko001 1:dd44d6b64300 88 m.unlock();
bosko001 1:dd44d6b64300 89 }
bosko001 1:dd44d6b64300 90 char readMiWiData( void )
bosko001 1:dd44d6b64300 91 {
bosko001 1:dd44d6b64300 92 char c;
bosko001 1:dd44d6b64300 93 m.lock();
bosko001 1:dd44d6b64300 94 csdata=0;
bosko001 1:dd44d6b64300 95 c = spi.write( 0x55 );
bosko001 1:dd44d6b64300 96 csdata = 1;
bosko001 1:dd44d6b64300 97 m.unlock();
bosko001 1:dd44d6b64300 98 return c;
bosko001 1:dd44d6b64300 99 }
bosko001 1:dd44d6b64300 100
bosko001 1:dd44d6b64300 101
bosko001 1:dd44d6b64300 102
bosko001 1:dd44d6b64300 103
bosko001 1:dd44d6b64300 104
bosko001 0:c06c791ca345 105 void inic_MiWi( void )
bosko001 0:c06c791ca345 106 {
bosko001 0:c06c791ca345 107
bosko001 0:c06c791ca345 108 spi.format( 8, 0);
bosko001 0:c06c791ca345 109 spi.frequency(1000000);
bosko001 0:c06c791ca345 110
bosko001 0:c06c791ca345 111 resetMiWi( );
bosko001 0:c06c791ca345 112
bosko001 0:c06c791ca345 113 for(int i = 0; i<0x20; i++ ) writeMiWiReg(i, _MIWI_DEV_CFG[i]);
bosko001 0:c06c791ca345 114 print_regs( );
bosko001 0:c06c791ca345 115
bosko001 0:c06c791ca345 116 lockPllMiWi( );
bosko001 0:c06c791ca345 117 printf("FTPRIREG = %x\n\r", readMiWiReg( 0xe));
bosko001 0:c06c791ca345 118
bosko001 0:c06c791ca345 119 }
bosko001 0:c06c791ca345 120
bosko001 0:c06c791ca345 121 void resetMiWi( void )
bosko001 0:c06c791ca345 122 {
bosko001 0:c06c791ca345 123 MiWiReset = 1;
bosko001 1:dd44d6b64300 124 wait( 0.2 );
bosko001 0:c06c791ca345 125 MiWiReset = 0;
bosko001 1:dd44d6b64300 126 wait( 0.1 );
bosko001 0:c06c791ca345 127 }
bosko001 0:c06c791ca345 128
bosko001 0:c06c791ca345 129 void set_mode( char mode )
bosko001 0:c06c791ca345 130 {
bosko001 0:c06c791ca345 131 union U_GCONFIG u_gc;
bosko001 0:c06c791ca345 132 u_gc.bajt = readMiWiReg( 0 ); // read GCONFIG
bosko001 0:c06c791ca345 133 u_gc.s_gc.CMOD = mode;
bosko001 0:c06c791ca345 134 writeMiWiReg( 0, u_gc.bajt ); // write GCONFIG
bosko001 0:c06c791ca345 135 printf("gconfig %x\n\r", readMiWiReg(0) );
bosko001 0:c06c791ca345 136 }
bosko001 0:c06c791ca345 137 char get_mode( void )
bosko001 0:c06c791ca345 138 {
bosko001 0:c06c791ca345 139 union U_GCONFIG u_gc;
bosko001 0:c06c791ca345 140 char c;
bosko001 0:c06c791ca345 141 u_gc.bajt = readMiWiReg( 0 ); // read GCONFIG
bosko001 0:c06c791ca345 142 c = u_gc.s_gc.CMOD;
bosko001 0:c06c791ca345 143 return c;
bosko001 0:c06c791ca345 144 }
bosko001 0:c06c791ca345 145
bosko001 0:c06c791ca345 146
bosko001 0:c06c791ca345 147 void print_regs( void )
bosko001 0:c06c791ca345 148 {
bosko001 0:c06c791ca345 149
bosko001 0:c06c791ca345 150 char c;
bosko001 0:c06c791ca345 151 for(int i = 0; i<0x20; i++ )
bosko001 0:c06c791ca345 152 {
bosko001 0:c06c791ca345 153 c = readMiWiReg(i);
bosko001 0:c06c791ca345 154 printf("%2x ", c );
bosko001 0:c06c791ca345 155 }
bosko001 0:c06c791ca345 156 printf(" irq0=%d irq1=%d\n\r", pin_irq0.read(), pin_irq1.read() ); fflush(stdout);
bosko001 0:c06c791ca345 157 }
bosko001 0:c06c791ca345 158
bosko001 0:c06c791ca345 159 void txMiWi( char *niz )
bosko001 0:c06c791ca345 160 {
bosko001 0:c06c791ca345 161 char old_mode = get_mode();
bosko001 0:c06c791ca345 162 set_mode(1);
bosko001 0:c06c791ca345 163 for(int i=0; i<niz[0]; i++ )
bosko001 0:c06c791ca345 164 {
bosko001 0:c06c791ca345 165 writeMiWiData( niz[i] ); // write FIFO
bosko001 0:c06c791ca345 166 }
bosko001 0:c06c791ca345 167 set_mode( 4 ); // predaja
bosko001 0:c06c791ca345 168 while( !(readMiWiReg( 0x0E ) & 0x20) ); // TXDONE ?
bosko001 0:c06c791ca345 169 set_mode( old_mode );
bosko001 0:c06c791ca345 170 // wait(0.5);
bosko001 1:dd44d6b64300 171 // printf("txdone = %x\n\r", readMiWiReg( 0x0E ));
bosko001 1:dd44d6b64300 172 // printf("mode = %x\n\r", old_mode);
bosko001 1:dd44d6b64300 173 // printf("config = %x\n\r", readMiWiReg( 0 ));
bosko001 0:c06c791ca345 174 // // standby
bosko001 0:c06c791ca345 175 }
bosko001 0:c06c791ca345 176
bosko001 1:dd44d6b64300 177 char sendbuff[64];
bosko001 1:dd44d6b64300 178 void sendMiWi( char *buff, int len)
bosko001 0:c06c791ca345 179 {
bosko001 1:dd44d6b64300 180 sendbuff[0] = len+2; sendbuff[1]=MiWiADDRESS;
bosko001 1:dd44d6b64300 181
bosko001 1:dd44d6b64300 182 memcpy((void*)&sendbuff[2], (void*)buff, len>62 ? 62:len );
bosko001 1:dd44d6b64300 183 txMiWi( sendbuff );
bosko001 1:dd44d6b64300 184
bosko001 0:c06c791ca345 185 }
bosko001 0:c06c791ca345 186
bosko001 0:c06c791ca345 187 bool lockPllMiWi( )
bosko001 0:c06c791ca345 188 {
bosko001 0:c06c791ca345 189 char old_mode;
bosko001 0:c06c791ca345 190
bosko001 0:c06c791ca345 191 union U_GCONFIG u;
bosko001 0:c06c791ca345 192 u.bajt = readMiWiReg( 0 );
bosko001 0:c06c791ca345 193 old_mode = u.s_gc.CMOD;
bosko001 0:c06c791ca345 194
bosko001 0:c06c791ca345 195 set_mode(2);
bosko001 0:c06c791ca345 196 u.bajt = readMiWiReg( 0 );
bosko001 0:c06c791ca345 197 int i;
bosko001 0:c06c791ca345 198 for( i =0; i<4 ; i++)
bosko001 0:c06c791ca345 199 {
bosko001 0:c06c791ca345 200 u.s_gc.VCOT = i;
bosko001 0:c06c791ca345 201 writeMiWiReg( 0, u.bajt );
bosko001 0:c06c791ca345 202 wait(0.001 );
bosko001 0:c06c791ca345 203
bosko001 0:c06c791ca345 204 printf("cofig[%d] = %x\n\r",i ,readMiWiReg( 0 ));
bosko001 0:c06c791ca345 205
bosko001 0:c06c791ca345 206 if( readMiWiReg( 0xe ) & 2 ) break;
bosko001 0:c06c791ca345 207 }
bosko001 0:c06c791ca345 208 set_mode( old_mode );
bosko001 0:c06c791ca345 209 if( readMiWiReg( 0xe ) & 2 ) return true;
bosko001 0:c06c791ca345 210 return false;
bosko001 0:c06c791ca345 211
bosko001 0:c06c791ca345 212 }
bosko001 0:c06c791ca345 213