bosko lekovic
/
IO_board_K22
K22 processor
main.cpp@10:5de7b6e77c4a, 2021-03-29 (annotated)
- Committer:
- Bosko Lekovic
- Date:
- Mon Mar 29 21:53:19 2021 +0200
- Revision:
- 10:5de7b6e77c4a
- Parent:
- 9:a4360a7713f5
- Child:
- 13:7c4ec32f6a54
text
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 0:c06c791ca345 | 1 | /* mbed Microcontroller Library |
bosko001 | 0:c06c791ca345 | 2 | * Copyright (c) 2019 ARM Limited |
bosko001 | 0:c06c791ca345 | 3 | * SPDX-License-Identifier: Apache-2.0 |
bosko001 | 0:c06c791ca345 | 4 | */ |
bosko001 | 0:c06c791ca345 | 5 | |
bosko001 | 0:c06c791ca345 | 6 | #include "mbed.h" |
bosko001 | 0:c06c791ca345 | 7 | #include "platform/mbed_thread.h" |
bosko001 | 6:12b2660d7dff | 8 | #include <vector> |
bosko001 | 0:c06c791ca345 | 9 | |
bosko001 | 6:12b2660d7dff | 10 | #include "Pinovi.h" |
bosko001 | 6:12b2660d7dff | 11 | // #include "IOspiLed.h" |
bosko001 | 6:12b2660d7dff | 12 | #include "ledControl.h" |
bosko001 | 6:12b2660d7dff | 13 | |
bosko001 | 6:12b2660d7dff | 14 | |
bosko001 | 6:12b2660d7dff | 15 | // #include "IOgpiospi.h" |
bosko001 | 6:12b2660d7dff | 16 | // #include "IOspitelegram.h" |
bosko001 | 6:12b2660d7dff | 17 | #include "IOspiprotokol.h" |
bosko001 | 6:12b2660d7dff | 18 | |
bosko001 | 6:12b2660d7dff | 19 | #include "ZeroCrossing.h" |
bosko001 | 6:12b2660d7dff | 20 | #include "IOzcControl.h" |
bosko001 | 6:12b2660d7dff | 21 | |
bosko001 | 6:12b2660d7dff | 22 | |
bosko001 | 6:12b2660d7dff | 23 | extern vector< S_logTelegram> s_log; |
bosko001 | 0:c06c791ca345 | 24 | |
bosko001 | 8:5a96bfb6b6f9 | 25 | |
bosko001 | 8:5a96bfb6b6f9 | 26 | |
bosko001 | 0:c06c791ca345 | 27 | // Blinking rate in milliseconds |
bosko001 | 0:c06c791ca345 | 28 | #define BLINKING_RATE_MS 200 |
bosko001 | 0:c06c791ca345 | 29 | |
bosko001 | 6:12b2660d7dff | 30 | DigitalOut ioTest(IO_TEST,0); |
bosko001 | 0:c06c791ca345 | 31 | |
bosko001 | 6:12b2660d7dff | 32 | DigitalOut testPin(PTE24, 0 ); |
bosko001 | 6:12b2660d7dff | 33 | |
bosko001 | 6:12b2660d7dff | 34 | |
bosko001 | 6:12b2660d7dff | 35 | char verzija = 5; |
bosko001 | 0:c06c791ca345 | 36 | |
bosko001 | 0:c06c791ca345 | 37 | |
bosko001 | 0:c06c791ca345 | 38 | DigitalIn b2(SW2); |
bosko001 | 0:c06c791ca345 | 39 | |
bosko001 | 6:12b2660d7dff | 40 | DigitalOut ledB(LED3,1); |
bosko001 | 0:c06c791ca345 | 41 | |
bosko001 | 0:c06c791ca345 | 42 | |
bosko001 | 3:71278c5b7c67 | 43 | |
bosko001 | 0:c06c791ca345 | 44 | |
bosko001 | 6:12b2660d7dff | 45 | class C_data c_data; |
bosko001 | 6:12b2660d7dff | 46 | |
bosko001 | 6:12b2660d7dff | 47 | |
bosko001 | 6:12b2660d7dff | 48 | C_ledControl c_ledControl(&c_data);//( &c_ledLight); |
bosko001 | 6:12b2660d7dff | 49 | |
bosko001 | 6:12b2660d7dff | 50 | C_spiProtokol c_spiProtokol( &c_data, &c_ledControl ); |
bosko001 | 6:12b2660d7dff | 51 | |
bosko001 | 6:12b2660d7dff | 52 | C_zeroCrossing c_zeroCrossing( &c_data); |
bosko001 | 6:12b2660d7dff | 53 | C_zeroCrossing& rc_zc = c_zeroCrossing; |
bosko001 | 6:12b2660d7dff | 54 | |
bosko001 | 6:12b2660d7dff | 55 | C_zcControl c_zcControl( &c_zeroCrossing, &c_spiProtokol); |
bosko001 | 6:12b2660d7dff | 56 | |
bosko001 | 6:12b2660d7dff | 57 | //DigitalOut pola( PTB20, 0); // test zero crossing ivica |
bosko001 | 6:12b2660d7dff | 58 | |
bosko001 | 6:12b2660d7dff | 59 | |
bosko001 | 6:12b2660d7dff | 60 | Ticker test_RasaLed; |
bosko001 | 6:12b2660d7dff | 61 | DigitalOut RasaLed(PTE0,1); |
bosko001 | 6:12b2660d7dff | 62 | |
bosko001 | 6:12b2660d7dff | 63 | long t; int tz; |
bosko001 | 6:12b2660d7dff | 64 | int v1, v2, v3, v4,v5,v6,f=0; |
bosko001 | 6:12b2660d7dff | 65 | int time_delay = 0, time_flag=0; |
bosko001 | 0:c06c791ca345 | 66 | |
bosko001 | 0:c06c791ca345 | 67 | int main() |
bosko001 | 0:c06c791ca345 | 68 | { |
bosko001 | 6:12b2660d7dff | 69 | |
bosko001 | 0:c06c791ca345 | 70 | // Initialise the digital pin LED1 as an output |
bosko001 | 6:12b2660d7dff | 71 | |
bosko001 | 4:fce988d27938 | 72 | printf("\n\n\r ____________ POCETAK IOboard_______________\n\r"); |
bosko001 | 0:c06c791ca345 | 73 | |
bosko001 | 6:12b2660d7dff | 74 | s_log.resize(1000); |
bosko001 | 6:12b2660d7dff | 75 | s_log.clear(); |
bosko001 | 6:12b2660d7dff | 76 | |
bosko001 | 6:12b2660d7dff | 77 | printf( " capacity %d max_size %d size %d\n\r", s_log.capacity(), s_log.max_size(), s_log.size()); |
bosko001 | 6:12b2660d7dff | 78 | test_RasaLed.attach_us([=]{ RasaLed = !RasaLed;}, 500000 ); |
bosko001 | 6:12b2660d7dff | 79 | |
bosko001 | 6:12b2660d7dff | 80 | /* postavljanje funkcija za komande i indikaciju sinhrono sa zero crossing-om */ |
bosko001 | 6:12b2660d7dff | 81 | |
bosko001 | 6:12b2660d7dff | 82 | |
bosko001 | 6:12b2660d7dff | 83 | c_zeroCrossing.set_ontimeONfun( []() |
bosko001 | 6:12b2660d7dff | 84 | { |
bosko001 | 6:12b2660d7dff | 85 | // testPin =! testPin; |
bosko001 | 6:12b2660d7dff | 86 | rc_zc.set_portOut( c_data.get_command() );// |
bosko001 | 6:12b2660d7dff | 87 | if( c_data.get_newcommand_flag() == true) { time_delay = 1; t = (long)Kernel::get_ms_count(); }// vreme = time_delay * 20ms |
bosko001 | 6:12b2660d7dff | 88 | else if(time_delay) { time_delay--; if( time_delay == 0 ) time_flag = 1; } |
bosko001 | 6:12b2660d7dff | 89 | else { c_data.set_messagge(); c_data.set_realIndication();} |
bosko001 | 6:12b2660d7dff | 90 | } |
bosko001 | 6:12b2660d7dff | 91 | ); |
bosko001 | 6:12b2660d7dff | 92 | c_zeroCrossing.set_ontimeOFFfun( [](){ } ); |
bosko001 | 6:12b2660d7dff | 93 | c_zeroCrossing.set_postponeONfun( []() // minimum mreze - rad sa indikacijama ispravnosti sijalica |
bosko001 | 6:12b2660d7dff | 94 | { |
bosko001 | 6:12b2660d7dff | 95 | if(time_delay == 0 && (c_data.check_newcommand_flag()== false) ) |
bosko001 | 6:12b2660d7dff | 96 | { |
bosko001 | 6:12b2660d7dff | 97 | char p1 = rc_zc.get_portInMin(); // dva ocitavanja porta |
bosko001 | 6:12b2660d7dff | 98 | wait_us(10); |
bosko001 | 6:12b2660d7dff | 99 | char p2 = rc_zc.get_portInMin(); |
bosko001 | 6:12b2660d7dff | 100 | p1 &= p2; |
Bosko Lekovic |
10:5de7b6e77c4a | 101 | c_data.set_pMin( p1 ); // upis vrednosti minimuma mreze u C_data |
Bosko Lekovic |
10:5de7b6e77c4a | 102 | c_data.set_indication(); // proracun i upis indikacije u C_data |
Bosko Lekovic |
10:5de7b6e77c4a | 103 | c_data.set_corectness(); // proracun i upis ispravnosti sijalice |
Bosko Lekovic |
10:5de7b6e77c4a | 104 | c_data.set_realIndication(); // izracun aktuelne indikacije - spremna za slanje na LEDove |
bosko001 | 4:fce988d27938 | 105 | |
bosko001 | 6:12b2660d7dff | 106 | // ledB =!ledB; |
bosko001 | 6:12b2660d7dff | 107 | // static int i=0; |
bosko001 | 6:12b2660d7dff | 108 | // if(i++ > 100) { i = 0; f = 1; |
bosko001 | 6:12b2660d7dff | 109 | // v1 = c_data.pMin; |
bosko001 | 6:12b2660d7dff | 110 | // v2 = c_data.get_command(); |
bosko001 | 6:12b2660d7dff | 111 | // v3 = c_data.get_indication(); |
bosko001 | 6:12b2660d7dff | 112 | // } |
bosko001 | 6:12b2660d7dff | 113 | } |
bosko001 | 6:12b2660d7dff | 114 | else c_data.clear_pMin(); |
bosko001 | 6:12b2660d7dff | 115 | } |
bosko001 | 6:12b2660d7dff | 116 | ); |
bosko001 | 6:12b2660d7dff | 117 | c_zeroCrossing.set_postponeOFFfun( []() // maximum mreze - rad sa neispravnim naponom - misem |
bosko001 | 6:12b2660d7dff | 118 | { |
bosko001 | 6:12b2660d7dff | 119 | static int i=0, j=0; |
bosko001 | 6:12b2660d7dff | 120 | if(time_delay == 0 && (c_data.check_newcommand_flag()== false)) |
bosko001 | 6:12b2660d7dff | 121 | { |
bosko001 | 6:12b2660d7dff | 122 | ledB =!ledB; |
bosko001 | 6:12b2660d7dff | 123 | char p1 = rc_zc.get_portInMax(); // dva ocitavanja porta |
bosko001 | 6:12b2660d7dff | 124 | wait_us(10); |
bosko001 | 6:12b2660d7dff | 125 | char p2 = rc_zc.get_portInMax(); |
bosko001 | 6:12b2660d7dff | 126 | p1 |= p2; |
Bosko Lekovic |
10:5de7b6e77c4a | 127 | v1 = c_data.set_pMax( v4 = p1 ); // upis vrednosti maksimuma mreze |
Bosko Lekovic |
10:5de7b6e77c4a | 128 | c_data.set_faultVoltage(); // izracun i upis napona misa |
bosko001 | 6:12b2660d7dff | 129 | v2 = c_data.get_command(); |
bosko001 | 6:12b2660d7dff | 130 | v3 = c_data.get_faultVoltage(); |
bosko001 | 6:12b2660d7dff | 131 | |
bosko001 | 6:12b2660d7dff | 132 | if(time_flag) |
bosko001 | 6:12b2660d7dff | 133 | { |
bosko001 | 6:12b2660d7dff | 134 | testPin =! testPin; |
bosko001 | 6:12b2660d7dff | 135 | f = 1; time_flag--; |
bosko001 | 6:12b2660d7dff | 136 | v1 = c_data.pMax; |
bosko001 | 6:12b2660d7dff | 137 | v6 = (long)Kernel::get_ms_count()-t; |
bosko001 | 6:12b2660d7dff | 138 | } |
bosko001 | 6:12b2660d7dff | 139 | } |
bosko001 | 6:12b2660d7dff | 140 | else c_data.clear_pMax(); |
bosko001 | 6:12b2660d7dff | 141 | |
bosko001 | 6:12b2660d7dff | 142 | } ); |
bosko001 | 6:12b2660d7dff | 143 | |
bosko001 | 0:c06c791ca345 | 144 | |
bosko001 | 0:c06c791ca345 | 145 | int b = b2; |
bosko001 | 0:c06c791ca345 | 146 | |
bosko001 | 6:12b2660d7dff | 147 | /* konzolu napraciti kao klasu koja nasledjuje C_thread klasu */ |
bosko001 | 6:12b2660d7dff | 148 | extern void consolafun( void ); |
bosko001 | 6:12b2660d7dff | 149 | Thread th_consola; |
bosko001 | 6:12b2660d7dff | 150 | th_consola.start( consolafun); |
bosko001 | 6:12b2660d7dff | 151 | |
bosko001 | 6:12b2660d7dff | 152 | /* cekanje da se procita prva ispravna adresa sa dip switch-a */ |
bosko001 | 6:12b2660d7dff | 153 | do { ThisThread::sleep_for(10ms);;c_spiProtokol.inic_address( ); } |
bosko001 | 6:12b2660d7dff | 154 | while ( c_data.get_address() == 0 ); |
bosko001 | 6:12b2660d7dff | 155 | printf(" adresa je = %d\n\r", c_data.get_address()); |
bosko001 | 6:12b2660d7dff | 156 | |
bosko001 | 6:12b2660d7dff | 157 | // postavljanje sopstvene adrese i prikaz na ledovima |
bosko001 | 6:12b2660d7dff | 158 | c_spiProtokol.set_myAddress(c_data.get_address()); |
bosko001 | 6:12b2660d7dff | 159 | c_ledControl.set_colorTemplate_Green(); |
bosko001 | 6:12b2660d7dff | 160 | c_ledControl.set_ledView(adresa); |
bosko001 | 6:12b2660d7dff | 161 | |
bosko001 | 4:fce988d27938 | 162 | |
bosko001 | 0:c06c791ca345 | 163 | while (true) |
bosko001 | 0:c06c791ca345 | 164 | { |
bosko001 | 0:c06c791ca345 | 165 | |
bosko001 | 6:12b2660d7dff | 166 | /* if(f==1) |
bosko001 | 6:12b2660d7dff | 167 | { |
bosko001 | 6:12b2660d7dff | 168 | f=0; |
bosko001 | 6:12b2660d7dff | 169 | printf( "det = %x %x %x %x %d vreme = %d %d\n\r", v1, v2, v3, v4, v5, v6, t); |
bosko001 | 6:12b2660d7dff | 170 | } |
bosko001 | 6:12b2660d7dff | 171 | */ |
bosko001 | 4:fce988d27938 | 172 | |
bosko001 | 0:c06c791ca345 | 173 | if( b!= b2) |
bosko001 | 0:c06c791ca345 | 174 | { |
bosko001 | 0:c06c791ca345 | 175 | b = b2; |
bosko001 | 6:12b2660d7dff | 176 | if( b ) { ledB = 1;} |
bosko001 | 6:12b2660d7dff | 177 | else { ledB = 0; //printf( " bitCount = %d, byteCount = %d\n\r", c_spiProtokol.get_bitCount(), c_spiProtokol.get_byteCount()); |
bosko001 | 6:12b2660d7dff | 178 | |
bosko001 | 6:12b2660d7dff | 179 | printf( " zerocrossing timer on = %d off = %d \n\r", c_zeroCrossing.get_zcTimeON(), c_zeroCrossing.get_zcTimeOFF()); |
bosko001 | 6:12b2660d7dff | 180 | // printf(" port in %x\n\r", rc_zc.s_portinMin.get_port() ); |
bosko001 | 6:12b2660d7dff | 181 | printf(" adresa je = %d\n\r", c_data.get_address()); |
bosko001 | 6:12b2660d7dff | 182 | } |
bosko001 | 0:c06c791ca345 | 183 | } |
bosko001 | 6:12b2660d7dff | 184 | |
bosko001 | 6:12b2660d7dff | 185 | c_spiProtokol.spi_monitor(); |
bosko001 | 0:c06c791ca345 | 186 | |
bosko001 | 0:c06c791ca345 | 187 | |
bosko001 | 0:c06c791ca345 | 188 | thread_sleep_for(BLINKING_RATE_MS); |
bosko001 | 0:c06c791ca345 | 189 | } |
bosko001 | 0:c06c791ca345 | 190 | } |
bosko001 | 0:c06c791ca345 | 191 | |
bosko001 | 0:c06c791ca345 | 192 | |
bosko001 | 0:c06c791ca345 | 193 | |
bosko001 | 0:c06c791ca345 | 194 | void consolafun( void ) |
bosko001 | 0:c06c791ca345 | 195 | { |
bosko001 | 0:c06c791ca345 | 196 | while( true ) |
bosko001 | 0:c06c791ca345 | 197 | { |
bosko001 | 6:12b2660d7dff | 198 | |
bosko001 | 0:c06c791ca345 | 199 | char uc = getchar( ); |
bosko001 | 0:c06c791ca345 | 200 | switch( (unsigned char) uc ) |
bosko001 | 0:c06c791ca345 | 201 | { |
bosko001 | 0:c06c791ca345 | 202 | case 'r': case 'R': |
bosko001 | 0:c06c791ca345 | 203 | |
bosko001 | 0:c06c791ca345 | 204 | printf("\n\rcitanje registra, unesi broj:\n\r"); |
bosko001 | 0:c06c791ca345 | 205 | int r; |
bosko001 | 0:c06c791ca345 | 206 | scanf("%x", &r); |
bosko001 | 1:1052d6227111 | 207 | // printf( "\n\rregistar %x ima vrednost %x\n\r", r, readMiWiReg(r)); |
bosko001 | 0:c06c791ca345 | 208 | |
bosko001 | 0:c06c791ca345 | 209 | break; |
bosko001 | 0:c06c791ca345 | 210 | case 'w': case 'W': |
bosko001 | 0:c06c791ca345 | 211 | { |
bosko001 | 0:c06c791ca345 | 212 | printf("\n\rupis registra, unesi broj registra:"); |
bosko001 | 0:c06c791ca345 | 213 | int r; |
bosko001 | 0:c06c791ca345 | 214 | scanf("%x", &r); |
bosko001 | 0:c06c791ca345 | 215 | printf( " %x", r ); |
bosko001 | 0:c06c791ca345 | 216 | printf("\n\runesi vrednost registra:"); |
bosko001 | 0:c06c791ca345 | 217 | int v; |
bosko001 | 0:c06c791ca345 | 218 | scanf("%x", &v); |
bosko001 | 0:c06c791ca345 | 219 | printf( " %x", v ); |
bosko001 | 1:1052d6227111 | 220 | // printf( "\n\rregistar %x ima vrednost %x\n\r", r, readMiWiReg(r)); |
bosko001 | 0:c06c791ca345 | 221 | } |
bosko001 | 0:c06c791ca345 | 222 | break; |
bosko001 | 4:fce988d27938 | 223 | case 's': case 'S': |
bosko001 | 6:12b2660d7dff | 224 | // set_shiftspi_signal( ); |
bosko001 | 4:fce988d27938 | 225 | break; |
bosko001 | 4:fce988d27938 | 226 | |
bosko001 | 6:12b2660d7dff | 227 | case 'c': |
bosko001 | 6:12b2660d7dff | 228 | s_log.clear(); |
bosko001 | 6:12b2660d7dff | 229 | //break; |
bosko001 | 6:12b2660d7dff | 230 | case 'l': |
bosko001 | 6:12b2660d7dff | 231 | printf( " capacity %d max_size %d size %d\n\r", s_log.capacity(), s_log.max_size(), s_log.size()); |
bosko001 | 6:12b2660d7dff | 232 | break; |
bosko001 | 6:12b2660d7dff | 233 | case 'p': |
bosko001 | 6:12b2660d7dff | 234 | int i=1; |
bosko001 | 6:12b2660d7dff | 235 | if(s_log.size()) |
bosko001 | 6:12b2660d7dff | 236 | { |
bosko001 | 6:12b2660d7dff | 237 | for( auto it = s_log.end()-1; it != s_log.begin(); it--) |
bosko001 | 6:12b2660d7dff | 238 | printf( "%3d %s\n\r", i++, (*it).poruka); |
bosko001 | 6:12b2660d7dff | 239 | auto it = s_log.begin(); |
bosko001 | 6:12b2660d7dff | 240 | printf( "%3d %s\n\r", i++, (*it).poruka ); |
bosko001 | 6:12b2660d7dff | 241 | } |
bosko001 | 6:12b2660d7dff | 242 | break; |
bosko001 | 0:c06c791ca345 | 243 | } |
bosko001 | 3:71278c5b7c67 | 244 | ThisThread::yield(); |
bosko001 | 3:71278c5b7c67 | 245 | thread_sleep_for(100); |
bosko001 | 0:c06c791ca345 | 246 | } |
bosko001 | 0:c06c791ca345 | 247 | } |