annotate Small_CPU/Src/spi.c @ 138:cc9c18075e00 FlipDisplay

Removed no longer supported scooter code
author Ideenmodellierer
date Sat, 23 Feb 2019 21:10:51 +0100
parents 6ae8ba5683d6
children 466c8d9c5e43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /**
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
2 ******************************************************************************
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
3 * @file spi.c
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
4 * @author heinrichs weikamp gmbh
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
5 * @version V0.0.1
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
6 * @date 16-Sept-2014
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
7 * @brief Source code for spi control
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
8 *
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
9 @verbatim
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
10 ==============================================================================
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
11 ##### How to use #####
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
12 ==============================================================================
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
13 @endverbatim
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
14 ******************************************************************************
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
15 * @attention
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
16 *
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
17 * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
18 *
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
19 ******************************************************************************
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
20 */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 #include "spi.h"
120
6347a86caa18 Cleanup warning and disable printf calls
Ideenmodellierer
parents: 104
diff changeset
24 #include "dma.h"
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 //#include "gpio.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 /* USER CODE BEGIN 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 #include "scheduler.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29
120
6347a86caa18 Cleanup warning and disable printf calls
Ideenmodellierer
parents: 104
diff changeset
30 #ifdef DEBUG_GPIO
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 extern void GPIO_new_DEBUG_LOW(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 extern void GPIO_new_DEBUG_HIGH(void);
120
6347a86caa18 Cleanup warning and disable printf calls
Ideenmodellierer
parents: 104
diff changeset
33 #endif
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
35 uint8_t data_error = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
36 uint32_t data_error_time = 0;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 static void SPI_Error_Handler(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 /* USER CODE END 0 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 static uint8_t SPI_check_header_and_footer_ok(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 SPI_HandleTypeDef hspi1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 SPI_HandleTypeDef hspi3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 DMA_HandleTypeDef hdma_tx;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 DMA_HandleTypeDef hdma_rx;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 // SPI3 init function
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
51 void MX_SPI3_Init(void) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
52 hspi3.Instance = SPI3;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
53 hspi3.Init.Mode = SPI_MODE_MASTER;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
54 hspi3.Init.Direction = SPI_DIRECTION_2LINES;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
55 hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
56 hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
57 hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
58 hspi3.Init.NSS = SPI_NSS_SOFT;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
59 hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
60 hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
61 hspi3.Init.TIMode = SPI_TIMODE_DISABLED;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
62 hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
63 hspi3.Init.CRCPolynomial = 7;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
64 HAL_SPI_Init(&hspi3);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
67 void MX_SPI3_DeInit(void) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
68 HAL_SPI_DeInit(&hspi3);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
71 uint8_t SPI3_ButtonAdjust(uint8_t *arrayInput, uint8_t *arrayOutput) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 HAL_StatusTypeDef status;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 uint8_t answer[10];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 uint8_t rework[10];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 rework[0] = 0xFF;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
77 for (int i = 0; i < 3; i++) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 // limiter
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
79 if (arrayInput[i] == 0xFF)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 arrayInput[i] = 0xFE;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
81 if (arrayInput[i] >= 15) {
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
82 // copy - ausl�se-schwelle
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
83 rework[i + 1] = arrayInput[i];
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 // wieder-scharf-schalte-schwelle
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
85 rework[i + 3 + 1] = arrayInput[i] - 10;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
86 } else if (arrayInput[i] >= 10) {
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
87 // copy - ausl�se-schwelle
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
88 rework[i + 1] = arrayInput[i];
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 // wieder-scharf-schalte-schwelle
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
90 rework[i + 3 + 1] = arrayInput[i] - 5;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
91 } else {
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
92 // copy - ausl�se-schwelle
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
93 rework[i + 1] = 7;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 // wieder-scharf-schalte-schwelle
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
95 rework[i + 3 + 1] = 6;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 status = HAL_OK; /* = 0 */
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
100 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
101 for (int i = 0; i < 7; i++) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
102 HAL_Delay(10);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
103 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
63
cd298de33783 Replaced wait loop with delay fct
Ideenmodellierer
parents: 38
diff changeset
104 HAL_Delay(10);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
105 status += HAL_SPI_TransmitReceive(&hspi3, &rework[i], &answer[i], 1,
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
106 20);
63
cd298de33783 Replaced wait loop with delay fct
Ideenmodellierer
parents: 38
diff changeset
107 HAL_Delay(10);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
108 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 }
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
110
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
111 if (status == HAL_OK) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
112 for (int i = 0; i < 3; i++) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
113 arrayOutput[i] = answer[i + 2]; // first not, return of 0xFF not
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
114 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 return 1;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
116 } else
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
117
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 // SPI5 init function
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
122 void MX_SPI1_Init(void) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
123 hspi1.Instance = SPI1;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
124 hspi1.Init.Mode = SPI_MODE_SLAVE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
125 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
126 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
127 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
128 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
129 hspi1.Init.NSS = SPI_NSS_HARD_INPUT; //SPI_NSS_SOFT;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 90
diff changeset
130 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
131 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
132 hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
133 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //_DISABLED; _ENABLED;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
134 hspi1.Init.CRCPolynomial = 7;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
135 HAL_SPI_Init(&hspi1);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
138 void MX_SPI_DeInit(void) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
139 HAL_SPI_DeInit(&hspi1);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
142 void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
144 GPIO_InitTypeDef GPIO_InitStruct;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
146 if (hspi->Instance == SPI1) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
147 // Peripheral clock enable
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
148 __SPI1_CLK_ENABLE();
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
149 __GPIOA_CLK_ENABLE();
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 //SPI1 GPIO Configuration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 //PA4 ------> SPI1_CS
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 //PA5 ------> SPI1_SCK
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 //PA6 ------> SPI1_MISO
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 //PA7 ------> SPI1_MOSI
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
155
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
156 GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 // GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
158 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
159 GPIO_InitStruct.Pull = GPIO_PULLUP;
124
4b355396557a Change GPIO speed for SPI communication as recommended by STM32 Errata
Ideenmodellierer
parents: 120
diff changeset
160 GPIO_InitStruct.Speed = GPIO_SPEED_FAST; /* Decision is based on errata which recommends FAST for GPIO at 90Mhz */
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
161 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
162 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 //##-3- Configure the DMA streams ##########################################
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 // Configure the DMA handler for Transmission process
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
166 hdma_tx.Instance = DMA2_Stream3;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
167 hdma_tx.Init.Channel = DMA_CHANNEL_3;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
168 hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
169 hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
170 hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
172 hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
173 hdma_tx.Init.Mode = DMA_NORMAL;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
174 hdma_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
175 hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
176 hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
177 hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
178 hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
179
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
180 HAL_DMA_Init(&hdma_tx);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
181
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 // Associate the initialized DMA handle to the the SPI handle
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183 __HAL_LINKDMA(hspi, hdmatx, hdma_tx);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
184
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 // Configure the DMA handler for Transmission process
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
186 hdma_rx.Instance = DMA2_Stream0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
187 hdma_rx.Init.Channel = DMA_CHANNEL_3;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
188 hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
189 hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
190 hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191 hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
192 hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
193 hdma_rx.Init.Mode = DMA_NORMAL;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
194 hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
195 hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
196 hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
197 hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
198 hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
200 HAL_DMA_Init(&hdma_rx);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
201
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
202 // Associate the initialized DMA handle to the the SPI handle
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
203 __HAL_LINKDMA(hspi, hdmarx, hdma_rx);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
204
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
205 //##-4- Configure the NVIC for DMA #########################################
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
206 //NVIC configuration for DMA transfer complete interrupt (SPI3_RX)
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
207 HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 1, 0);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
208 HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
209
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
210 // NVIC configuration for DMA transfer complete interrupt (SPI1_TX)
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
211 HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 1, 1);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
212 HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
213 } else if (hspi->Instance == SPI3) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
214 __GPIOC_CLK_ENABLE();
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
215 __SPI3_CLK_ENABLE();
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
216
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
217 //SPI1 GPIO Configuration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
218 //PC10 ------> SPI3_SCK
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
219 //PC11 ------> SPI3_MISO
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
220 //PC12 ------> SPI3_MOSI
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
221 //PA15 ------> SPI3_NSS (official)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
222 //PC9 ------> SPI3_NSS (hw)
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
223
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
224 GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
225 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
226 GPIO_InitStruct.Pull = GPIO_PULLUP;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
227 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
228 GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
229 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
230
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
231 GPIO_InitStruct.Pin = GPIO_PIN_9;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
232 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
233 GPIO_InitStruct.Pull = GPIO_PULLUP;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
234 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
235 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
236
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
237 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
238 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
239 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
240
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
241 void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
242 if (hspi->Instance == SPI1) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
243 __SPI1_FORCE_RESET();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
244 __SPI1_RELEASE_RESET();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
245
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
246 //SPI1 GPIO Configuration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
247 //PA5 ------> SPI1_SCK
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
248 //PA6 ------> SPI1_MISO
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
249 //PA7 ------> SPI1_MOSI
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
250
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
251 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
252
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
253 HAL_DMA_DeInit(&hdma_tx);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
254 HAL_DMA_DeInit(&hdma_rx);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
255
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
256 HAL_NVIC_DisableIRQ(DMA2_Stream3_IRQn);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
257 HAL_NVIC_DisableIRQ(DMA2_Stream0_IRQn);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
258 } else if (hspi->Instance == SPI3) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
259 __SPI3_FORCE_RESET();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
260 __SPI3_RELEASE_RESET();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
261
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
262 //SPI1 GPIO Configuration
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
263 //PC10 ------> SPI3_SCK
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
264 //PC11 ------> SPI3_MISO
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
265 //PC12 ------> SPI3_MOSI
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
266 //PA15 ------> SPI3_NSS (official)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
267 //PC9 ------> SPI3_NSS (hw)
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
268 HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
269 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
270 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
271
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
272 void SPI_synchronize_with_Master(void) {
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
273 GPIO_InitTypeDef GPIO_InitStruct;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
274 //
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
275 #if 0
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
276 __GPIOA_CLK_ENABLE();
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
277 /**SPI1 GPIO Configuration
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
278 PA5 ------> SPI1_SCK
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
279 */
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
280 GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
281 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
282 GPIO_InitStruct.Pull = GPIO_PULLUP;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
283 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
284 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
285 //
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
286 HAL_Delay(10);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
287 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
288 HAL_Delay(10);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
289 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
290 HAL_Delay(20);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
291 #endif
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
292 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
293
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
294 void SPI_Start_single_TxRx_with_Master(void) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
295 uint8_t * pOutput;
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
296 HAL_StatusTypeDef retval;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
297
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
298 if (global.dataSendToSlave.getDeviceDataNow) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
299 global.dataSendToSlave.getDeviceDataNow = 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
300 pOutput = (uint8_t*) &(global.deviceDataSendToMaster);
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
301 } else {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
302 pOutput = (uint8_t*) &(global.dataSendToMaster);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
303 }
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
304 retval = HAL_SPI_TransmitReceive_DMA(&hspi1, pOutput,(uint8_t*) &(global.dataSendToSlave), EXCHANGE_BUFFERSIZE);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
305 if ( retval!= HAL_OK) {
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
306 SPI_Error_Handler();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
307 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
308 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
309
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
310 void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
311 /* restart SPI */
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
312 if (hspi == &hspi1)
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
313 {
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
314 global.check_sync_not_running = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
315 /* stop data exchange? */
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
316 if (global.mode == MODE_SHUTDOWN) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
317 global.mode = MODE_SLEEP;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
318 global.dataSendToSlavePending = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
319 global.dataSendToSlaveIsValid = 1;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
320 global.dataSendToSlaveIsNotValidCount = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
321 return;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
322 }
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
323
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
324 /* data consistent? */
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
325 if (SPI_check_header_and_footer_ok()) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
326 // GPIO_new_DEBUG_HIGH(); //For debug.
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
327 global.dataSendToSlaveIsValid = 1;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
328 global.dataSendToSlaveIsNotValidCount = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
329 } else {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
330 // GPIO_new_DEBUG_LOW(); //For debug.
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
331 global.dataSendToSlaveIsValid = 0;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
332 global.dataSendToSlaveIsNotValidCount++;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
333 HAL_SPI_Abort_IT(&hspi1);
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
334 global.dataSendToSlaveIsNotValidCount = 1;
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
335 }
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
336 }
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
337 global.dataSendToMaster.power_on_reset = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
338 global.deviceDataSendToMaster.power_on_reset = 0;
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
339
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 90
diff changeset
340 //TODO:REMOVE
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
341 // if ( !global.dataSendToSlaveStopEval ) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
342 // scheduleSpecial_Evaluate_DataSendToSlave();
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
343 // }
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
344 scheduleSpecial_Evaluate_DataSendToSlave();
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
345
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
346 SPI_Start_single_TxRx_with_Master(); //Send data always.
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
347 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
348
136
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
349
6ae8ba5683d6 Introduces abort of communication in case of a out of sync DMA transfer
Ideenmodellierer
parents: 124
diff changeset
350
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
351 static uint8_t SPI_check_header_and_footer_ok(void) {
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
352 if (global.dataSendToSlave.header.checkCode[0] != 0xBB)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
353 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
354 if (global.dataSendToSlave.header.checkCode[1] != 0x01)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
355 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
356 if (global.dataSendToSlave.header.checkCode[2] != 0x01)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
357 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
358 if (global.dataSendToSlave.header.checkCode[3] != 0xBB)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
359 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
360 if (global.dataSendToSlave.footer.checkCode[0] != 0xF4)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
361 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
362 if (global.dataSendToSlave.footer.checkCode[1] != 0xF3)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
363 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
364 if (global.dataSendToSlave.footer.checkCode[2] != 0xF2)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
365 return 0;
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
366 if (global.dataSendToSlave.footer.checkCode[3] != 0xF1)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
367 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
368
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
369 return 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
370 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
371
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
372 static void SPI_Error_Handler(void) {
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
373 //The device is locks. Hard to recover.
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
374 // while(1)
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
375 // {
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 63
diff changeset
376 // }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
377 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
378
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
379 /**
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
380 * @}
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
381 */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
382
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
383 /**
89
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
384 * @}
ff7775cc34c4 temp! full cyclic SPI
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
385 */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
386
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
387 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/