annotate Discovery/Src/externCPU2bootloader.c @ 217:ce05c801b002 Improve_IPC_Sync

Moved display refresh and button action function from tim callback to main loop The update of the display, especially for log book pages, may take some time causing problem because the execution of other functions (like SPI) was shifted => SPI errors.
author ideenmodellierer
date Sun, 31 Mar 2019 15:39:44 +0200
parents f64cf099a7f5
children 5ca177d2df5d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 * @file externCPU2bootloader.c Template
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 * @author heinrichs weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 * @version V0.0.1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 * @date 23-Oct-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 * @version V0.0.1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 * @since 23-Oct-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 * @brief Main Template to communicate with the second CPU in bootloader mode
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 * bootloader ROM build by ST and defined in AN4286
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 ##### How to use #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 * @attention
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 * <h2><center>&copy; COPYRIGHT(c) 2016 heinrichs weikamp</center></h2>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 #include "stm32f4xx_hal.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 #include "stdio.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 #include "ostc.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 #include "settings.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 #include "externCPU2bootloader.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 #include "externLogbookFlash.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 #include "tComm.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 /* Exported variables --------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 /* Private types -------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #define BOOTLOADSPITIMEOUT 5000
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 /* Private variables ---------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 /* Private function prototypes -----------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 uint8_t boot_sync_frame(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 uint8_t boot_ack(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 uint8_t boot_get(uint8_t *RxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 uint8_t boot_get_id(uint8_t *RxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 uint8_t boot_get_version(uint8_t *RxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 //uint8_t boot_go(uint32_t address);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 uint8_t boot_write_memory(uint32_t address, uint8_t length_minus_1, uint8_t *data);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 //uint8_t boot_erase_memory(uint16_t data_frame, uint16_t *page_numbers);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 uint8_t boot_erase_memory(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 uint8_t boot_write_protect(uint8_t number_of_sectors_minus_one, uint8_t *sector_codes);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 uint8_t boot_write_unprotect(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 uint8_t boot_readout_protect(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 uint8_t boot_readout_unprotect(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 void Bootoader_send_command(uint8_t command);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 void Bootloader_spi_single(uint8_t TxByte);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 void Bootloader_spi(uint16_t lengthData, uint8_t *aTxBuffer, uint8_t *aRxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 void Bootloader_Error_Handler(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 /* Exported functions --------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 uint8_t extCPU2bootloader_start(uint8_t *version, uint16_t *chipID)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 // uint8_t aTxBuffer[256] = { 0 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 uint8_t aRxBuffer[256] = { 0 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 boot_sync_frame();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 boot_get_version(aRxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 *version = aRxBuffer[1];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 boot_get_id(aRxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 *chipID = ((uint16_t)aRxBuffer[2]) << 8;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 *chipID += (uint16_t)aRxBuffer[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 if((*chipID == 0x431) && (*version > 10) && (*version < 32))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 return 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87
53
f64cf099a7f5 Allow RTE binaries > 64k size
Ideenmodellierer
parents: 38
diff changeset
88 uint8_t extCPU2bootloader_internal(uint8_t* buffer, uint32_t length, char* display_text)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 uint8_t version = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 uint16_t chipID = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 // uint8_t ret;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 if(!extCPU2bootloader_start(&version,&chipID))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 if(!boot_erase_memory())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 HAL_Delay(100);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 uint16_t i=0;
53
f64cf099a7f5 Allow RTE binaries > 64k size
Ideenmodellierer
parents: 38
diff changeset
99 uint32_t lengthsave = length;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 uint8_t percent = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 while(length)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 percent = (100 * (i * 256)) /lengthsave;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 tComm_verlauf(percent);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 if(length > 256)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 if( !boot_write_memory(0x08000000 + (i * 256), 255, &buffer[i * 256]) )
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 return 0;;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 length -= 256;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 if(!boot_write_memory(0x08000000 + (i * 256), length - 1, &buffer[i * 256]))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 length = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 i++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 return 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125
53
f64cf099a7f5 Allow RTE binaries > 64k size
Ideenmodellierer
parents: 38
diff changeset
126 uint8_t extCPU2bootloader(uint8_t* buffer, uint32_t length, char* display_text)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 uint8_t result = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 MX_SmallCPU_Reset_To_Boot();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 result = extCPU2bootloader_internal(buffer,length,display_text);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 MX_SmallCPU_Reset_To_Standard();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 return result;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 /* Private functions --------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 uint8_t boot_sync_frame(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 return boot_ack();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 uint8_t boot_get(uint8_t *RxBuffer)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 Bootoader_send_command(0x00);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 Bootloader_spi(14, NULL, RxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 return boot_ack();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 uint8_t boot_get_version(uint8_t *RxBuffer)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 Bootoader_send_command(0x01);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 Bootloader_spi(3, NULL, RxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 return boot_ack();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 uint8_t boot_get_id(uint8_t *RxBuffer)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170 Bootoader_send_command(0x02);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173 Bootloader_spi(5, NULL, RxBuffer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 return boot_ack();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178 uint8_t boot_go(uint32_t address)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
179 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
180
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
181 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
184
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 uint8_t boot_write_memory(uint32_t address, uint8_t length_minus_1, uint8_t *data)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
186 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
187 uint8_t addressNew[4];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
188 uint8_t checksum = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
189 uint16_t length;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
190
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
192 Bootoader_send_command(0x31);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
193 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
194 return 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
195 HAL_Delay(5);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
196 addressNew[0] = (uint8_t)((address >> 24) & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
197 addressNew[1] = (uint8_t)((address >> 16) & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
198 addressNew[2] = (uint8_t)((address >> 8) & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199 addressNew[3] = (uint8_t)((address >> 0) & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
200 Bootloader_spi(4, addressNew, NULL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
201 checksum = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
202 checksum ^= addressNew[0];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
203 checksum ^= addressNew[1];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
204 checksum ^= addressNew[2];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
205 checksum ^= addressNew[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
206 Bootloader_spi_single(checksum);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
207 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
208 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
209 HAL_Delay(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
210 Bootloader_spi_single(length_minus_1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
211 length = ((uint16_t)length_minus_1) + 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
212 Bootloader_spi(length, data, NULL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
213 HAL_Delay(26);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
214 checksum = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
215 checksum ^= length_minus_1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
216 for(int i=0;i<length;i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
217 checksum ^= data[i];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
218 Bootloader_spi_single(checksum);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
219
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
220 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
221 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
222 HAL_Delay(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
223 return 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
224 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
225
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
226 //uint8_t boot_erase_memory(uint16_t data_frame, uint16_t *page_numbers)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
227 uint8_t boot_erase_memory(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
228 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
229 uint8_t special_erase_with_checksum[3] = {0xFF, 0xFF, 0x00};
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
230
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
231 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
232 Bootoader_send_command(0x44);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
233 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
234 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
235 Bootloader_spi(3, special_erase_with_checksum, NULL);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
236 HAL_Delay(11000); /* 5.5 to 11 seconds */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
237 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
238 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
239 return 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
240 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
241
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
242 /* write unprotect does reset the system !! */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
243 uint8_t boot_write_unprotect(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
244 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
245 Bootloader_spi_single(0x5a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
246 Bootoader_send_command(0x73);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
247 if(!boot_ack())
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
248 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
249 return boot_ack();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
250 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
251
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
252 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
253 uint8_t boot_write_protect(uint8_t number_of_sectors_minus_one, uint8_t *sector_codes)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
254 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
255
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
256 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
257
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
258 uint8_t boot_readout_protect(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
259 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
260
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
261 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
262
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
263 uint8_t boot_readout_unprotect(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
264 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
265
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
266 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
267 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
268
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
269 uint8_t boot_ack(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
270 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
271 uint8_t answer = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
272
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
273 Bootloader_spi_single(0x00);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
274 for(int i=0; i< 1000; i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
275 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
276 Bootloader_spi(1, NULL, &answer);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
277 if((answer == 0x79) || (answer == 0x1F))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
278 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
279 Bootloader_spi_single(0x79);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
280 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
281 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
282 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
283 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
284 if(answer == 0x79)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
285 return 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
286 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
287 return 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
288 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
289
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
290 void Bootoader_send_command(uint8_t command)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
291 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
292 uint8_t send[2];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
293 uint8_t receive[2];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
294
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
295 send[0] = command;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
296 send[1] = 0xFF ^ command;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
297 Bootloader_spi(2, send, receive);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
298 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
299
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
300 void Bootloader_spi_single(uint8_t TxByte)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
301 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
302 Bootloader_spi(1,&TxByte, 0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
303 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
304
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
305
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
306 void Bootloader_spi(uint16_t lengthData, uint8_t *aTxBuffer, uint8_t *aRxBuffer)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
307 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
308 uint8_t dummy[256] = { 0 };
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
309 uint8_t *tx_data;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
310 uint8_t *rx_data;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
311
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
312 tx_data = aTxBuffer;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
313 rx_data = aRxBuffer;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
314
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
315 if(aTxBuffer == NULL)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
316 tx_data = dummy;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
317 if(aRxBuffer == NULL)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
318 rx_data = dummy;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
319
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
320 //HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_RESET); // only for testing with Oscilloscope
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
321
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
322
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
323 HAL_SPI_TransmitReceive(&cpu2DmaSpi, (uint8_t *)tx_data, (uint8_t *)rx_data, (uint16_t)lengthData,1000);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
324 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
325 if(HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)tx_data, (uint8_t *)rx_data, (uint16_t)lengthData) != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
326 if(HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)tx_data, (uint8_t *)rx_data, (uint16_t)lengthData) != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
327 Bootloader_Error_Handler();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
328
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
329 while (HAL_SPI_GetState(&cpu2DmaSpi) != HAL_SPI_STATE_READY)// only for testing with Oscilloscope
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
330 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
331 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
332 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_SET); // only for testing with Oscilloscope
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
333 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
334 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
335
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
336
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
337 void Bootloader_Error_Handler(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
338 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
339 while(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
340 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
341
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
342
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
343