Advertisement
Guest User

mikro

a guest
Feb 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.07 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* USER CODE END Header */
  40.  
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "main.h"
  43.  
  44. /* Private includes ----------------------------------------------------------*/
  45. /* USER CODE BEGIN Includes */
  46. #include <math.h>
  47. #include "stm32f1xx.h"
  48. #include "stm32f1xx_hal.h"
  49. #include "sd_hal_mpu6050.h"
  50.  
  51. #include <stdarg.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54.  
  55.  
  56. /* USER CODE END Includes */
  57.  
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* USER CODE BEGIN PTD */
  60.  
  61. /* USER CODE END PTD */
  62.  
  63. /* Private define ------------------------------------------------------------*/
  64. /* USER CODE BEGIN PD */
  65.  
  66. /* USER CODE END PD */
  67.  
  68. /* Private macro -------------------------------------------------------------*/
  69. /* USER CODE BEGIN PM */
  70.  
  71. /* USER CODE END PM */
  72.  
  73. /* Private variables ---------------------------------------------------------*/
  74. I2C_HandleTypeDef hi2c1;
  75. I2C_HandleTypeDef hi2c2;
  76.  
  77. UART_HandleTypeDef huart2;
  78.  
  79. /* USER CODE BEGIN PV */
  80. SD_MPU6050 mpu1;
  81.  
  82. /* USER CODE END PV */
  83.  
  84. /* Private function prototypes -----------------------------------------------*/
  85. void SystemClock_Config(void);
  86. static void MX_GPIO_Init(void);
  87. static void MX_USART2_UART_Init(void);
  88. static void MX_I2C2_Init(void);
  89. static void MX_I2C1_Init(void);
  90. /* USER CODE BEGIN PFP */
  91.  
  92. /* USER CODE END PFP */
  93.  
  94. /* Private user code ---------------------------------------------------------*/
  95. /* USER CODE BEGIN 0 */
  96. #define Bufor_size 200
  97. #define pocz_ramka '#'
  98. #define kon_ramka '*'
  99. #define ESC '!'
  100. #define ESC_START '('
  101. #define ESC_STOP ')'
  102. uint8_t czy_pocz=0, czy_specjal=0, czy_kon=0;
  103. char BUF_RX[Bufor_size];
  104. char BUF_TX[Bufor_size];
  105. char com[Bufor_size];
  106. char bf[Bufor_size];
  107. volatile uint8_t BusyRX = 0;
  108. volatile uint8_t EmptyRX = 0;
  109. volatile uint8_t BusyTX = 0;
  110. volatile uint8_t EmptyTX = 0;
  111.  
  112. volatile uint8_t Busytab = 0;
  113. volatile uint8_t Emptytab = 0;
  114. int x,y;
  115.  
  116. uint16_t wielkosc_ram;
  117.  
  118. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart){
  119.  
  120. if(huart==&huart2){
  121. if(EmptyTX!=BusyTX){
  122. uint8_t tmp=BUF_TX[BusyTX];
  123. BusyTX++;
  124. if(BusyTX>=Bufor_size){
  125. BusyTX=0;
  126. }
  127.  
  128. HAL_UART_Transmit_IT(&huart2,&tmp,1);
  129. }
  130. }
  131. }
  132. void send (char* format,...){
  133. char tmp_rs[128];
  134. int i;
  135. uint8_t idx;
  136. va_list arglist; //zmienna typu do przechowywania informacji o zmiennych argumentach
  137. va_start(arglist,format); //Inicjalizuj zmienną listę argumentów
  138. vsprintf(tmp_rs,format,arglist); //Zapisuj sformatowane dane z arglist na string
  139. va_end(arglist); //Zakończ przy użyciu zmiennej listy argumentów
  140. idx=EmptyTX;
  141. for(i=0;i<strlen(tmp_rs);i++){
  142. BUF_TX[idx]=tmp_rs[i];
  143. idx++;
  144. if(idx>=Bufor_size){idx=0;}
  145. }
  146. __disable_irq(); //wyłączenie przerwań
  147. if((EmptyTX==BusyTX) && (__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TXE)==SET)){
  148. EmptyTX=idx;
  149. uint8_t tmp=BUF_TX[BusyTX];
  150. BusyTX++;
  151. if(BusyTX>=Bufor_size){BusyTX=0;}
  152. HAL_UART_Transmit_IT(&huart2,&tmp,1);
  153. }else{
  154. EmptyTX=idx;
  155. }
  156. __enable_irq(); //włączenie przerwań
  157. }
  158.  
  159. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
  160.  
  161. if(huart==&huart2){
  162. EmptyRX++;
  163. if(EmptyRX>=Bufor_size){EmptyRX=0;}
  164.  
  165. HAL_UART_Receive_IT(&huart2,&BUF_RX[EmptyRX],1);
  166. }
  167. }
  168. /*unsigned char DigToHex(unsigned char d)
  169. // Konwersja liczb 0..F do znakow '0'..'F'
  170. {
  171. char zn;
  172. // if( (d<10)) return('0' + d);
  173. if((d>=0) && (d<10)) zn = ('0' + d);
  174. if((d>=10) && (d<=15)) zn = ('A' + d - 10);
  175. return zn;
  176. }
  177.  
  178. void CharToHex(char c, char x[3])
  179. //Convert char c to hex reprezentation x
  180. {
  181. unsigned char xh,xl;
  182. xl = (unsigned char)c & 0x0f;
  183. xh = ((unsigned char)c & 0xf0) >> 4;
  184. x[0] = DigToHex(xh); x[1] = DigToHex(xl); x[2] = 0;
  185. }
  186.  
  187. unsigned char HexToDig(char c)
  188. // Konwersja znakow '0'..'F' do liczb 0..F
  189. {
  190. char znak;
  191. if((c>='0') && (c<='9')) znak = (c - '0');
  192. if((c>='A') && (c<='F')) znak = (c - 'A' + 10);
  193. return znak;
  194. }
  195.  
  196. char HexToChar(char x[3])
  197. // Convert hex string x to char
  198. {
  199. unsigned char xh,xl,c;
  200. xh = HexToDig(x[0]);
  201. xl = HexToDig(x[1]);
  202. c = ((xh << 4) + xl);
  203. return (char) c;
  204. }
  205.  
  206.  
  207. unsigned char lrcgen(char *buf, int size)
  208. // Wyznaczanie LCR dla bufora buf i size znakow
  209. {
  210. unsigned char sum = 0;
  211. int i;
  212. for(i=0;i<size;i++) sum = sum + buf[i];
  213. return(~sum + 1);
  214. }
  215. void SendFrm( int Adr, int Command, int Size, char *Data)
  216. Send Command to port dev, unit Adr, Size char of Data
  217. Uzyta zmienna globalna FrameBuf
  218. {
  219. unsigned char lrc;
  220. int i,cnt;
  221. char tmp[3];
  222.  
  223.  
  224. Computing of LCR ----------------
  225. FrameBuf[0] = (char)(Adr & 0x00FF);
  226. FrameBuf[1] = (char)(Command & 0x00FF);
  227. if( Size > 0) for(i=0;i<Size;i++) FrameBuf[i+2] = Data[i];
  228. lrc = lrcgen(FrameBuf,Size + 2);
  229. Budowa ramki -------------------------------------------------
  230. FrameBuf[0] = '#';
  231. Address -----------------------------------------
  232. CharToHex((char) Adr,tmp);
  233. FrameBuf[1] = tmp[0]; FrameBuf[2] = tmp[1];
  234. Command -----------------------------------------
  235. CharToHex((char)Command,tmp);
  236. FrameBuf[3] = tmp[0]; FrameBuf[4] = tmp[1];
  237. cnt = 5;
  238. if( Size > 0 ) { Data ---------------------------
  239. for(i=0;i<Size;i++) {
  240. CharToHex(Data[i],tmp);
  241. FrameBuf[cnt] = tmp[0]; FrameBuf[cnt+1] = tmp[1];
  242. cnt++; cnt++;
  243. }
  244. }
  245. LRC ---------------------------------------
  246. CharToHex((char) lrc,tmp);
  247. FrameBuf[cnt] = tmp[0]; FrameBuf[cnt+1] = tmp[1]; cnt= cnt+2;
  248. End of Frame Marks ------------------------
  249. FrameBuf[cnt] = CR; cnt++;
  250. FrameBuf[cnt] = LF; cnt++;
  251. FrameBuf[cnt+1] = '\0'; <-- usunac
  252. Ramka gotowa --> wysylac
  253. printf("\nWyslano: ");
  254. for(i=0;i<cnt;i++) {
  255. write(&FrameBuf[i],1);
  256. printf("%c",FrameBuf[i]);
  257. }
  258. send("wyslano: %d znakow\n",cnt);
  259. }*/
  260. uint8_t USART_getchar(){
  261. uint tmp;
  262. if(EmptyRX!=BusyRX){
  263. tmp=BUF_RX[BusyRX];
  264. BusyRX++;
  265. if(BusyRX == Bufor_size)BusyRX=0;
  266. return tmp;
  267.  
  268. }else{
  269. return 0;
  270. }
  271.  
  272. }
  273.  
  274. /*void atoh(char *ascii_ptr, char* hex_ptr,int len)
  275. {
  276. int i;
  277.  
  278. for(i = 0; i < (len / 2); i++)
  279. {
  280. if((*(ascii_ptr+(2*i)) <= '9'))
  281. *(hex_ptr+i) = ((*(ascii_ptr+(2*i)) - '0') * 16 );
  282. else
  283. *(hex_ptr+i) = (((*(ascii_ptr+(2*i)) - 'A') + 10) << 4);
  284. (hex_ptr+i) = ((ascii_ptr+(2*i)) <= '9') ? (((ascii_ptr+(2*i)) - '0') * 16 ) : ((((ascii_ptr+(2*i)) - 'A') + 10) << 4);
  285. (hex_ptr+i) |= ((ascii_ptr+(2*i)+1) <= '9') ? ((ascii_ptr+(2*i)+1) - '0') : ((ascii_ptr+(2*i)+1) - 'A' + 10);
  286.  
  287. }
  288.  
  289.  
  290. }*/
  291.  
  292. /*void atoh2(char ascii_ptr, char hex_ptr,int len)
  293. {
  294. int i;
  295.  
  296. for(i = 0; i < (len / 2); i++)
  297. {
  298. if((ascii_ptr) <= '9')
  299. (hex_ptr) = ((ascii_ptr - '0') * 16 );
  300. else
  301. (hex_ptr+i) = ((((ascii_ptr+(2*i)) - 'A') + 10) << 4);
  302. (hex_ptr+i) = ((ascii_ptr+(2*i)) <= '9') ? (((ascii_ptr+(2*i)) - '0') * 16 ) : ((((ascii_ptr+(2*i)) - 'A') + 10) << 4);
  303. (hex_ptr+i) |= ((ascii_ptr+(2*i)+1) <= '9') ? ((ascii_ptr+(2*i)+1) - '0') : ((ascii_ptr+(2*i)+1) - 'A' + 10);
  304.  
  305. // }
  306.  
  307.  
  308. }*/
  309. /*char kodow(char com[]){
  310. int idx=1;
  311. char tmp[Bufor_size];
  312. int len = strlen(com);
  313. atoh(&com,&tmp,len);
  314. send("kodoww %c",com);
  315.  
  316. return tmp;
  317. }*/
  318. /*void comma(void){
  319. uint16_t idx = 0;
  320. while(EmptyRX!=BusyRX)
  321. com[idx]=USART_getchar();
  322. send("GETCHE %c",com[idx]);
  323. com[idx++]=USART_getchar();
  324. send("GETCHE1 %c",com[idx]);
  325. if(com[idx]==pocz_ramka){
  326. czy_pocz =1;
  327. idx=0;
  328. com[idx]=0;
  329. }else if(com[idx] == ESC){
  330. czy_specjal =1;
  331. com[idx]=USART_getchar();}
  332. else if(com[idx] == ESC_START && czy_specjal == 1){
  333. com[idx]=pocz_ramka;
  334. idx++;
  335. }
  336. send("kod %c",com);
  337. for(int i =0 ; i<strlen(com);i++){
  338. kodow(com);
  339. send("kod %x",kodow(com));}
  340. }*/
  341. int Frame_Size(uint8_t Busy, uint8_t Empty){
  342. wielkosc_ram= Empty>=Busy?(Empty-Busy):(Empty-Busy+Bufor_size);
  343. return wielkosc_ram;
  344. }
  345.  
  346. void poczatek(char com[],uint16_t wielkosc,uint8_t pocz){
  347. for(int i=pocz;i<wielkosc;i++){
  348. //send("main %c",com[i]);
  349. if(com[i]==pocz_ramka){
  350. send("Mamy poczatek %d",i);
  351. czy_pocz=1;
  352. Busytab=i;break;
  353. }
  354. else {czy_pocz=0;}
  355. }
  356.  
  357. }
  358. void koniec(char com[],uint16_t wielkosc,uint8_t pocz){
  359. for(int i=pocz;i<wielkosc;i++){
  360. //send("main %c",com[i]);
  361. if(com[i]==kon_ramka){
  362. send("Mamy koniec %d",i);
  363. czy_kon=1;
  364. Emptytab=i;break;
  365. }
  366. else {czy_kon=0;}
  367. }
  368. }
  369.  
  370. void GetCommand(uint8_t EmptyRX, uint8_t BusyRX) {
  371. uint16_t idx = 0;
  372.  
  373. send("ramka");
  374. BusyRX++;
  375. if (BusyRX >= Bufor_size) {
  376. BusyRX = 0;
  377. }
  378. while (BusyRX != EmptyRX) {
  379.  
  380. com[idx] = USART_getchar(); //BUF_RX[BusyRX];
  381. BusyRX++;
  382. idx++;
  383. if (BusyRX >= Bufor_size) {
  384. BusyRX = 0;
  385. }
  386. if (idx >= Bufor_size) {
  387. break;
  388. }
  389. }
  390. for(int i =0;i<=(Emptytab-Busytab);i++){
  391. send("NASZA RAMKA %c\n\r",com[i]);
  392. }
  393. }
  394. void Find_Frame(){
  395. if(BusyRX!=EmptyRX)
  396. {Frame_Size(BusyRX,EmptyRX);
  397. poczatek(BUF_RX,EmptyRX,BusyRX);
  398. koniec(BUF_RX,EmptyRX,BusyRX);
  399. if(czy_pocz == 1 && czy_kon == 1){
  400. GetCommand(Emptytab,Busytab);
  401. }
  402. else {send("Brak ramki");}
  403.  
  404. }
  405.  
  406. }
  407. void sprawdzenie(){
  408. if(strlen(com)==16){
  409. send("ramka ok\n\r");
  410. }
  411. else {send("zla ramka\n\r");}
  412.  
  413. /*for(int i = 0; i < 6; i++){
  414. if(com[i] >='0' && com[i]<='9')send("ramka ok1\n\r");
  415.  
  416. else send("zla ramka1\n\r");
  417. }*/
  418. /*if(com[9]==':')send("ramka ok2\n\r");
  419.  
  420. else send("zla ramka2\n\r");*/
  421. }
  422. /*
  423. void read_st1(){
  424. if(BusyRX!=EmptyRX)
  425. {
  426.  
  427. //buf_pomoc=BusyRX;
  428. wielkosc=((EmptyRX-BusyRX)+256)%256;
  429. if(find_start==256) buf_pomoc=BusyRX;
  430. if(BUF_RX[buf_pomoc]=='*') // wyszukanie pierwszego znaku startu
  431. {
  432. send("*\n");
  433. BusyRX=buf_pomoc;
  434. find_start=buf_pomoc;
  435. if((buf_pomoc+1)%256!=EmptyRX) buf_pomoc=(buf_pomoc+1)%256;
  436. }
  437. else if(find_start!=256 && BUF_RX[buf_pomoc]=='/') //znalezienie znaku stopu
  438. {
  439. send("/\n");
  440. find_stop=buf_pomoc;
  441. pomoc_wielkosc=((find_stop-find_start)+257)%256;
  442. if(pomoc_wielkosc>=6 && pomoc_wielkosc-6<=128) // ramka o odpowiedniej minimalnej maksymalnej dlugosci
  443. {
  444. if(BUF_RX[find_start+1]>=48 && BUF_RX[find_start+1]<=57 && // czy 1 znak jest cyfra
  445. BUF_RX[find_start+2]>=48 && BUF_RX[find_start+2]<=57 && // czy 2 znak jest cyfra
  446. BUF_RX[find_start+3]>=48 && BUF_RX[find_start+3]<=57 && BUF_RX[find_start+4]==':') // czy 3 znak jest cyfra i czy 4 znak jest :
  447. {
  448. if(pomoc_wielkosc-6==(BUF_RX[find_start+1]-48)*100+(BUF_RX[find_start+2]-48)*10+(BUF_RX[find_start+3]-48))
  449. { //jesli dlugosc zadeklarowana dlugosc bedzie rowna dlugosci ramki
  450. find_start=(find_start+5)%256;
  451. while(find_start!=find_stop) //kopiowanie do drugiego bufora
  452. {
  453. bufor_komend[drugi_stop]=BUF_RX[find_start];
  454. find_start=(find_start+1)%256;
  455. drugi_stop=(drugi_stop+1)%256;
  456. }
  457. send("Ramka ok\n");
  458. BusyRX=(find_stop+1)%256;
  459. find_start=256;
  460. find_stop=256;
  461.  
  462. //ll
  463. }
  464. else // bledna budowa ramki - inna zadeklarowana dlugosc
  465. {
  466. send("Bledna ramka dl\n");
  467. BusyRX=find_stop;
  468. find_start=256;
  469. find_stop=256;
  470. }
  471. }
  472. else // bledna budowa ramki - dlugosc ramki nie zapisana jako cyfry
  473. {
  474. send("Bledna ramka cy\n");
  475. BusyRX=(find_stop+1)%256;
  476. find_start=256;
  477. find_stop=256;
  478. }
  479. }
  480. else // zbyt krótka lub zbyt dluga ramka
  481. {
  482. send("Bledna ramka krdl\n");
  483. BusyRX=(find_stop+1)%256;
  484. find_start=256;
  485. find_stop=256;
  486. }
  487. }
  488. else if(find_start!=256) // znalezienie znaku w srodku ramki
  489. {
  490. send("znak w\n");
  491. if((buf_pomoc+1)%256!=EmptyRX) buf_pomoc=(buf_pomoc+1)%256;
  492. }
  493. else // znalezienie znaku poza ramka
  494. { send("znak poza\n");
  495. buf_pomoc=(buf_pomoc+1)%256;
  496. BusyRX=buf_pomoc;
  497. }
  498. }
  499. }*/
  500.  
  501.  
  502. /* USER CODE END 0 */
  503.  
  504. /**
  505. * @brief The application entry point.
  506. * @retval int
  507. */
  508. int main(void)
  509. {
  510. /* USER CODE BEGIN 1 */
  511. SD_MPU6050_Result result ;
  512. char mpu_ok[14] = {"MPU DZIALA\n\r"};
  513. char mpu_not[16] = {"MPU NIE DZIALA\n"};
  514. /* USER CODE END 1 */
  515.  
  516. /* MCU Configuration--------------------------------------------------------*/
  517.  
  518. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  519. HAL_Init();
  520.  
  521. /* USER CODE BEGIN Init */
  522.  
  523. /* USER CODE END Init */
  524.  
  525. /* Configure the system clock */
  526. SystemClock_Config();
  527.  
  528. /* USER CODE BEGIN SysInit */
  529.  
  530. /* USER CODE END SysInit */
  531.  
  532. /* Initialize all configured peripherals */
  533. MX_GPIO_Init();
  534. MX_USART2_UART_Init();
  535. MX_I2C2_Init();
  536. MX_I2C1_Init();
  537. /* USER CODE BEGIN 2 */
  538.  
  539. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
  540. /*Ustawienie parametrów akcelerometru*/
  541. result = SD_MPU6050_Init(&hi2c2,&mpu1,SD_MPU6050_Device_0,SD_MPU6050_Accelerometer_2G);
  542. HAL_Delay(500);
  543. /*Sprawdzenie czy moduł działa*/
  544. if(result == SD_MPU6050_Result_Ok)
  545. {
  546. send(mpu_ok);
  547. }
  548. else
  549. {
  550. send(mpu_not);
  551. }
  552.  
  553.  
  554. /* USER CODE END 2 */
  555.  
  556. /* Infinite loop */
  557. /* USER CODE BEGIN WHILE */
  558. while (1)
  559. {
  560.  
  561. // HAL_Delay(500);
  562. /* USER CODE END WHILE */
  563.  
  564. /* USER CODE BEGIN 3 */
  565. SD_MPU6050_ReadAccelerometer(&hi2c2,&mpu1);
  566. int16_t a_x = mpu1.Accelerometer_X;
  567. int16_t a_y = mpu1.Accelerometer_Y;
  568. int16_t a_z = mpu1.Accelerometer_Z;
  569.  
  570.  
  571.  
  572. /*uint8_t idx;
  573. while(BusyRX!=EmptyRX){
  574. bf[idx]= USART_getchar();
  575. send("ramka %c",bf[idx]);
  576. if(bf[idx] == pocz_ramka)
  577. {
  578. czy_pocz = 1;
  579. send("pocz");
  580. idx=0;
  581. bf[idx]=0;
  582. send("bufor %c",bf[idx]);
  583. }else if(bf[idx]==ESC){
  584. czy_specjal=1;
  585. bf[idx]=USART_getchar();
  586. }else if(bf[idx] == ESC_START && czy_specjal == 1){
  587. bf[idx]=pocz_ramka;
  588. idx++;}
  589. */
  590. HAL_UART_Receive_IT(&huart2,&BUF_RX[EmptyRX],1);
  591.  
  592. if(BusyRX != EmptyRX)
  593. {
  594. send("BusyRX %d",BusyRX);
  595. send("EmptyRX %d",EmptyRX);
  596. send("Pobranie");
  597. Find_Frame();
  598.  
  599. //if(czy_pocz==1 && czy_kon==1){
  600. // send("sprawdz");
  601. //sprawdzenie();
  602. //}
  603.  
  604. BusyRX=Emptytab;
  605. if (BusyRX >= Bufor_size) {
  606. BusyRX = 0;
  607. }
  608. //GetCommand(Emptytab,Busytab);
  609. //else send("niewiem");
  610. //send("BusyRX po ++ %d",BusyRX);
  611. x=Busytab;
  612. poczatek(BUF_RX,EmptyRX,BusyRX);
  613. y=Busytab;
  614. if(x == y){
  615. BusyRX=EmptyRX;
  616. send("kon");
  617. // BUF_RX=0;
  618. // BusyRX=0;
  619. // EmptyRX=0;
  620.  
  621.  
  622. }
  623. }
  624.  
  625. // send("x = %d\n\ry = %d\n\rz = %d\n\r",a_x,a_y,a_z);
  626. memset(com, 0, sizeof(com));
  627. }
  628.  
  629. /* USER CODE END 3 */
  630. }
  631.  
  632. /**
  633. * @brief System Clock Configuration
  634. * @retval None
  635. */
  636. void SystemClock_Config(void)
  637. {
  638. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  639. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  640.  
  641. /**Initializes the CPU, AHB and APB busses clocks
  642. */
  643. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  644. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  645. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  646. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  647. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  648. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  649. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  650. {
  651. Error_Handler();
  652. }
  653. /**Initializes the CPU, AHB and APB busses clocks
  654. */
  655. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  656. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  657. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  658. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  659. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  660. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  661.  
  662. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  663. {
  664. Error_Handler();
  665. }
  666. }
  667.  
  668. /**
  669. * @brief I2C1 Initialization Function
  670. * @param None
  671. * @retval None
  672. */
  673. static void MX_I2C1_Init(void)
  674. {
  675.  
  676. /* USER CODE BEGIN I2C1_Init 0 */
  677.  
  678. /* USER CODE END I2C1_Init 0 */
  679.  
  680. /* USER CODE BEGIN I2C1_Init 1 */
  681.  
  682. /* USER CODE END I2C1_Init 1 */
  683. hi2c1.Instance = I2C1;
  684. hi2c1.Init.ClockSpeed = 100000;
  685. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  686. hi2c1.Init.OwnAddress1 = 0;
  687. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  688. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  689. hi2c1.Init.OwnAddress2 = 0;
  690. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  691. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  692. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  693. {
  694. Error_Handler();
  695. }
  696. /* USER CODE BEGIN I2C1_Init 2 */
  697.  
  698. /* USER CODE END I2C1_Init 2 */
  699.  
  700. }
  701.  
  702. /**
  703. * @brief I2C2 Initialization Function
  704. * @param None
  705. * @retval None
  706. */
  707. static void MX_I2C2_Init(void)
  708. {
  709.  
  710. /* USER CODE BEGIN I2C2_Init 0 */
  711.  
  712. /* USER CODE END I2C2_Init 0 */
  713.  
  714. /* USER CODE BEGIN I2C2_Init 1 */
  715.  
  716. /* USER CODE END I2C2_Init 1 */
  717. hi2c2.Instance = I2C2;
  718. hi2c2.Init.ClockSpeed = 100000;
  719. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  720. hi2c2.Init.OwnAddress1 = 0;
  721. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  722. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  723. hi2c2.Init.OwnAddress2 = 0;
  724. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  725. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  726. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  727. {
  728. Error_Handler();
  729. }
  730. /* USER CODE BEGIN I2C2_Init 2 */
  731.  
  732. /* USER CODE END I2C2_Init 2 */
  733.  
  734. }
  735.  
  736. /**
  737. * @brief USART2 Initialization Function
  738. * @param None
  739. * @retval None
  740. */
  741. static void MX_USART2_UART_Init(void)
  742. {
  743.  
  744. /* USER CODE BEGIN USART2_Init 0 */
  745.  
  746. /* USER CODE END USART2_Init 0 */
  747.  
  748. /* USER CODE BEGIN USART2_Init 1 */
  749.  
  750. /* USER CODE END USART2_Init 1 */
  751. huart2.Instance = USART2;
  752. huart2.Init.BaudRate = 115200;
  753. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  754. huart2.Init.StopBits = UART_STOPBITS_1;
  755. huart2.Init.Parity = UART_PARITY_NONE;
  756. huart2.Init.Mode = UART_MODE_TX_RX;
  757. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  758. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  759. if (HAL_UART_Init(&huart2) != HAL_OK)
  760. {
  761. Error_Handler();
  762. }
  763. /* USER CODE BEGIN USART2_Init 2 */
  764.  
  765. /* USER CODE END USART2_Init 2 */
  766.  
  767. }
  768.  
  769. /**
  770. * @brief GPIO Initialization Function
  771. * @param None
  772. * @retval None
  773. */
  774. static void MX_GPIO_Init(void)
  775. {
  776. GPIO_InitTypeDef GPIO_InitStruct = {0};
  777.  
  778. /* GPIO Ports Clock Enable */
  779. __HAL_RCC_GPIOC_CLK_ENABLE();
  780. __HAL_RCC_GPIOD_CLK_ENABLE();
  781. __HAL_RCC_GPIOA_CLK_ENABLE();
  782. __HAL_RCC_GPIOB_CLK_ENABLE();
  783.  
  784. /*Configure GPIO pin Output Level */
  785. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  786.  
  787. /*Configure GPIO pin : B1_Pin */
  788. GPIO_InitStruct.Pin = B1_Pin;
  789. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  790. GPIO_InitStruct.Pull = GPIO_NOPULL;
  791. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  792.  
  793. /*Configure GPIO pin : LD2_Pin */
  794. GPIO_InitStruct.Pin = LD2_Pin;
  795. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  796. GPIO_InitStruct.Pull = GPIO_NOPULL;
  797. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  798. HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  799.  
  800. /* EXTI interrupt init*/
  801. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  802. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  803.  
  804. }
  805.  
  806. /* USER CODE BEGIN 4 */
  807.  
  808. /* USER CODE END 4 */
  809.  
  810. /**
  811. * @brief This function is executed in case of error occurrence.
  812. * @retval None
  813. */
  814. void Error_Handler(void)
  815. {
  816. /* USER CODE BEGIN Error_Handler_Debug */
  817. /* User can add his own implementation to report the HAL error return state */
  818.  
  819. /* USER CODE END Error_Handler_Debug */
  820. }
  821.  
  822. #ifdef USE_FULL_ASSERT
  823. /**
  824. * @brief Reports the name of the source file and the source line number
  825. * where the assert_param error has occurred.
  826. * @param file: pointer to the source file name
  827. * @param line: assert_param error line source number
  828. * @retval None
  829. */
  830. void assert_failed(uint8_t *file, uint32_t line)
  831. {
  832. /* USER CODE BEGIN 6 */
  833. /* User can add his own implementation to report the file name and line number,
  834. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  835. /* USER CODE END 6 */
  836. }
  837. #endif /* USE_FULL_ASSERT */
  838.  
  839. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement