1 //*****************************************************************************
2 //
3 //! @file am_devices_cooper.h
4 //!
5 //! @brief An implementation of the Apollo inteface to Cooper using the IOM.
6 //!
7 //! @addtogroup cooper Cooper BLE Device Driver
8 //! @ingroup devices
9 //! @{
10 //
11 //*****************************************************************************
12 
13 //*****************************************************************************
14 //
15 // Copyright (c) 2023, Ambiq Micro, Inc.
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 // POSSIBILITY OF SUCH DAMAGE.
43 //
44 // This is part of revision release_sdk_4_4_0-3c5977e664 of the AmbiqSuite Development Package.
45 //
46 //*****************************************************************************
47 
48 #ifndef AM_DEVICES_COOPER_H
49 #define AM_DEVICES_COOPER_H
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55 
56 #include "am_util.h"
57 #include <stdlib.h>
58 
59 //*****************************************************************************
60 //
61 //! Type definitions.
62 //
63 //*****************************************************************************
64 
65 #define UINT16_TO_BYTE0(n)        ((uint8_t) (n))
66 #define UINT16_TO_BYTE1(n)        ((uint8_t) ((n) >> 8))
67 #define UINT32_TO_BYTE0(n)        ((uint8_t) (n))
68 #define UINT32_TO_BYTE1(n)        ((uint8_t) ((n) >> 8))
69 #define UINT32_TO_BYTE2(n)        ((uint8_t) ((n) >> 16))
70 #define UINT32_TO_BYTE3(n)        ((uint8_t) ((n) >> 24))
71 
72 //
73 //!
74 //
75 typedef enum
76 {
77     AM_DEVICES_COOPER_STATUS_SUCCESS,
78     AM_DEVICES_COOPER_STATUS_ERROR,
79     //
80     //! This error occurs when an HCI read or write function is called while
81     //! another HCI communication function is already in progress.
82     //
83     AM_DEVICES_COOPER_STATUS_BUS_BUSY,
84 
85     //
86     //! This error means that the MCU tried to execute an HCI write, but can not
87     //! get the space available (0xA868).
88     //! This might mean that the controller has not finished the wakeup process,
89     //! the MCU need to wait until the IRQ is asserted.
90     //
91     AM_DEVICES_COOPER_STATUS_CONTROLLER_NOT_READY,
92 
93     //
94     //! Wrong length of data been read back (==0 or > 260)
95     //
96     AM_DEVICES_COOPER_STATUS_WRONG_DATA_LENGTH,
97 
98     //
99     //! We are expecting an HCI response to a packet we just sent, but the BLE
100     //! core isn't asserting BLEIRQ. Its software may have crashed, and it may
101     //! need to restart.
102     //
103     AM_DEVICES_COOPER_STATUS_NO_RESPONSE,
104 
105     //
106     //! The transaction ends up in error and cannot finish the HCI packet R/W
107     //
108     AM_DEVICES_COOPER_STATUS_PACKET_INCOMPLETE,
109 
110     //
111     //! The CQ does not complete transaction before times out
112     //
113     AM_DEVICES_COOPER_STATUS_TIMEOUT,
114     AM_DEVICES_COOPER_STATUS_INVALID_OPERATION,
115 } am_devices_cooper_status_t;
116 
117 //
118 //! Definition of the tag associated to each parameters
119 //
120 enum PARAM_ID
121 {
122     //! Local Bd Address
123     PARAM_ID_BD_ADDRESS                 = 0x01,
124     //! Device Name
125     PARAM_ID_DEVICE_NAME                = 0x02,
126     //! 32K source
127     PARAM_ID_32K_CLK_SOURCE             = 0x03,
128     //! Radio Drift
129     PARAM_ID_LPCLK_DRIFT                = 0x07,
130     //! Radio Jitter
131     PARAM_ID_LPCLK_JITTER               = 0x08,
132     //! External wake-up time
133     PARAM_ID_EXT_WAKEUP_TIME            = 0x0D,
134     //! Oscillator wake-up time
135     PARAM_ID_OSC_WAKEUP_TIME            = 0x0E,
136     //! Radio wake-up time
137     PARAM_ID_RM_WAKEUP_TIME             = 0x0F,
138     //! UART baudrate
139     PARAM_ID_UART_BAUDRATE              = 0x10,
140     //! Enable sleep mode
141     PARAM_ID_SLEEP_ENABLE               = 0x11,
142     //! Enable External Wakeup
143     PARAM_ID_EXT_WAKEUP_ENABLE          = 0x12,
144     //! SP Private Key 192
145     PARAM_ID_SP_PRIVATE_KEY_P192        = 0x13,
146     //! SP Public Key 192
147     PARAM_ID_SP_PUBLIC_KEY_P192         = 0x14,
148 
149     //! Activity Move Configuration (enables/disables activity move for BLE connections and BT (e)SCO links)
150     PARAM_ID_ACTIVITY_MOVE_CONFIG       = 0x15,
151 
152     //! Enable/disable scanning for extended advertising PDUs
153     PARAM_ID_SCAN_EXT_ADV               = 0x16,
154 
155     //! Duration of the schedule reservation for long activities such as scan, inquiry, page, HDC advertising
156     PARAM_ID_SCHED_SCAN_DUR             = 0x17,
157 
158     //! Programming delay, margin for programming the baseband in advance of each activity (in half-slots)
159     PARAM_ID_PROG_DELAY                 = 0x18,
160 
161     //! Enable/disable channel assessment for BT and/or BLE
162     PARAM_ID_CH_ASS_EN                  = 0x19,
163 
164     //! Synchronous links configuration
165     PARAM_ID_SYNC_CONFIG                = 0x2C,
166     //! PCM Settings
167     PARAM_ID_PCM_SETTINGS               = 0x2D,
168     //! Sleep algorithm duration
169     PARAM_ID_SLEEP_ALGO_DUR             = 0x2E,
170     //! Tracer configuration
171     PARAM_ID_TRACER_CONFIG              = 0x2F,
172 
173     //! Diagport configuration
174     PARAM_ID_DIAG_BT_HW                 = 0x30,
175     //! Diagport configuration
176     PARAM_ID_DIAG_BLE_HW                = 0x31,
177     //! Diagport configuration
178     PARAM_ID_DIAG_SW                    = 0x32,
179     //! Diagport configuration
180     PARAM_ID_DIAG_DM_HW                 = 0x33,
181     //! Diagport configuration
182     PARAM_ID_DIAG_PLF                   = 0x34,
183 
184     //! IDC selection (for audio demo)
185     PARAM_ID_IDCSEL_PLF                 = 0x37,
186 
187     //! RSSI threshold tags
188     PARAM_ID_RSSI_HIGH_THR              = 0x3A,
189    //! RSSI threshold tags
190     PARAM_ID_RSSI_LOW_THR               = 0x3B,
191    //! RSSI threshold tags
192     PARAM_ID_RSSI_INTERF_THR            = 0x3C,
193 
194     //! RF BTIPT
195     PARAM_ID_RF_BTIPT_VERSION          = 0x3E,
196    //! RF BTIPT
197    PARAM_ID_RF_BTIPT_XO_SETTING       = 0x3F,
198    //! RF BTIPT
199    PARAM_ID_RF_BTIPT_GAIN_SETTING     = 0x40,
200 
201 
202     PARAM_ID_BT_LINK_KEY_FIRST          = 0x60,
203     PARAM_ID_BT_LINK_KEY_LAST           = 0x67,
204 
205     PARAM_ID_BLE_LINK_KEY_FIRST         = 0x70,
206     PARAM_ID_BLE_LINK_KEY_LAST          = 0x7F,
207     //! SC Private Key (Low Energy)
208     PARAM_ID_LE_PRIVATE_KEY_P256        = 0x80,
209     //! SC Public Key (Low Energy)
210     PARAM_ID_LE_PUBLIC_KEY_P256         = 0x81,
211     //! SC Debug: Used Fixed Private Key from NVDS (Low Energy)
212     PARAM_ID_LE_DBG_FIXED_P256_KEY      = 0x82,
213     //! SP Private Key (classic BT)
214     PARAM_ID_SP_PRIVATE_KEY_P256        = 0x83,
215     //! SP Public Key (classic BT)
216     PARAM_ID_SP_PUBLIC_KEY_P256         = 0x84,
217 
218     //! LE Coded PHY 500 Kbps selection
219     PARAM_ID_LE_CODED_PHY_500           = 0x85,
220 
221     //! Application specific
222     PARAM_ID_APP_SPECIFIC_FIRST         = 0x90,
223    //! Application specific
224    PARAM_ID_APP_SPECIFIC_LAST          = 0xAF,
225 
226     //! Mesh NVDS values
227     PARAM_ID_MESH_SPECIFIC_FIRST        = 0xB0,
228    //! Mesh NVDS values
229    PARAM_ID_MESH_SPECIFIC_LAST         = 0xF0,
230 };
231 
232 //NVDS_MAGIC_NUMBER
233 #define NVDS_PARAMETER_MAGIC_NUMBER     0x4e, 0x56, 0x44, 0x53
234 //! Local Bd Address
235 #define NVDS_PARAMETER_BD_ADDRESS       PARAM_ID_BD_ADDRESS, 0x06, 0x06, 0x22, 0x44, 0x66, 0x88, 0x48, 0x59
236 //! device name
237 #define NVDS_PARAMETER_DEVICE_NAME      PARAM_ID_DEVICE_NAME, 0x06, 0x06, 0x43, 0x6F, 0x6F, 0x70, 0x65, 0x72
238 #define NVDS_PARAMETER_EXT_32K_CLK_SOURCE PARAM_ID_32K_CLK_SOURCE, 0x06, 0x01, 0x01
239 
240 #define HCI_VSC_CMD_HEADER_LENGTH                    4
241 
242 //*****************************************************************************
243 //
244 //! @brief Vendor Specific commands.
245 //!
246 //! @note Lengths are reported as "4 + <parameter length>". Each vendor-specific
247 //! header is 4 bytes long. This definition allows the macro version of the
248 //! length to be used in all BLE APIs.
249 //
250 //*****************************************************************************
251 typedef enum
252 {
253     HCI_VSC_RD_MEM_CMD_OPCODE                      = 0xFC01,
254     HCI_VSC_WR_MEM_CMD_OPCODE                      = 0xFC02,
255 
256     HCI_VSC_ID_FLASH_CMD_OPCODE                    = 0xFC05,
257     HCI_VSC_ER_FLASH_CMD_OPCODE                    = 0xFC06,
258     HCI_VSC_WR_FLASH_CMD_OPCODE                    = 0xFC07,
259     HCI_VSC_RD_FLASH_CMD_OPCODE                    = 0xFC08,
260 
261     HCI_VSC_PLF_RESET_CMD_OPCODE                   = 0xFC11,
262 
263     HCI_VSC_REG_RD_CMD_OPCODE                      = 0xFC39,
264     HCI_VSC_REG_WR_CMD_OPCODE                      = 0xFC3A,
265 
266     //! Ambiq Vendor Specific Command set Tx power level
267     HCI_VSC_SET_TX_POWER_LEVEL_CFG_CMD_OPCODE      = 0xFC70,
268     //! Ambiq Vendor Specific Command  start transmitter test
269     HCI_VSC_START_TRANS_TEST_CFG_CMD_OPCODE        = 0xFC71,
270     //! Ambiq Vendor Specific Command end transmitter test
271     HCI_VSC_END_TRANS_TEST_CFG_CMD_OPCODE          = 0xFC72,
272     //! Ambiq Vendor Specific Command set debug log bitmap
273     HCI_VSC_SET_LOG_BITMAP_CFG_CMD_OPCODE          = 0xFC73,
274     //! Ambiq Vendor Specific Command update bd address
275     HCI_VSC_SET_BD_ADDR_CFG_CMD_OPCODE             = 0xFC74,
276     //! Ambiq Vendor Specific Command update FW
277     HCI_VSC_UPDATE_FW_CFG_CMD_OPCODE               = 0xFC75,
278     //! Ambiq Vendor Specific Command get device ID
279     HCI_VSC_GET_DEVICE_ID_CFG_CMD_OPCODE           = 0xFC76,
280     //! Ambiq Vendor Specific Command set NVDS parameters
281     HCI_VSC_UPDATE_NVDS_CFG_CMD_OPCODE             = 0xFC77,
282     //! Ambiq Vendor Specific Command set link layer features
283     HCI_VSC_UPDATE_LL_FEATURE_CFG_CMD_OPCODE       = 0xFC78,
284     //! Ambiq Vendor Specific Command get RSSI in DTM mode
285     HCI_VSC_GET_DTM_RSSI_CMD_OPCODE                = 0xFC79,
286     //! Ambiq Vendor Specific Command configure specified event mask
287     HCI_VSC_CFG_EVT_MASK_CMD_OPCODE                = 0xFC7A,
288 
289     // SBL use only
290     //! Ambiq Vendor Specific Command store info0 trim values to RAM
291     HCI_VSC_STORE_INFO0_TRIM_CMD_OPCODE            = 0xFCC0,
292     //! Ambiq Vendor Specific Command flash info0 trim values to cooper
293     HCI_VSC_FLASH_INFO0_TRIM_CMD_OPCODE            = 0xFCC1,
294     //! Ambiq Vendor Specific Command trigger Apollo4B to enter sleep
295     HCI_VSC_ENTER_SLEEP_CMD_OPCODE                 = 0xFCC2,
296 }vsc_opcode;
297 
298 #define HCI_VSC_RD_MEM_CMD_LENGTH                      6
299 #define HCI_VSC_WR_MEM_CMD_LENGTH                      134
300 #define HCI_VSC_ID_FLASH_CMD_LENGTH                    0
301 #define HCI_VSC_ER_FLASH_CMD_LENGTH                    9
302 #define HCI_VSC_WR_FLASH_CMD_LENGTH                    134
303 #define HCI_VSC_RD_FLASH_CMD_LENGTH                    6
304 #define HCI_VSC_PLF_RESET_CMD_LENGTH                   1
305 #define HCI_VSC_REG_RD_CMD_LENGTH                      4
306 #define HCI_VSC_REG_WR_CMD_LENGTH                      8
307 #define HCI_VSC_SET_TX_POWER_LEVEL_CFG_CMD_LENGTH      1
308 #define HCI_VSC_START_TRANS_TEST_CFG_CMD_LENGTH        4
309 #define HCI_VSC_END_TRANS_TEST_CFG_CMD_LENGTH          0
310 #define HCI_VSC_SET_LOG_BITMAP_CFG_CMD_LENGTH          4
311 #define HCI_VSC_SET_BD_ADDR_CFG_CMD_LENGTH             6
312 #define HCI_VSC_UPDATE_FW_CFG_CMD_LENGTH               4
313 #define HCI_VSC_GET_DEVICE_ID_CFG_CMD_LENGTH           0
314 #define HCI_VSC_UPDATE_NVDS_CFG_CMD_LENGTH             240
315 #define HCI_VSC_STORE_INFO0_TRIM_CMD_LENGTH            6
316 #define HCI_VSC_FLASH_INFO0_TRIM_CMD_LENGTH            0
317 #define HCI_VSC_ENTER_SLEEP_CMD_LENGTH                 0
318 #define HCI_VSC_GET_DTM_RSSI_CMD_LENGTH                0
319 #define HCI_VSC_CFG_EVT_MASK_CMD_LENGTH                4
320 
321 #define HCI_VSC_CMD_LENGTH(n)                          (HCI_VSC_CMD_HEADER_LENGTH + n)
322 #define HCI_VSC_UPDATE_NVDS_CFG_CMD_OFFSET             (HCI_VSC_CMD_HEADER_LENGTH + 4) //!< NVDS_PARAMETER_MAGIC_NUMBER
323 
324 #define HCI_VSC_CMD(CMD, ...)                          {AM_DEVICES_COOPER_CMD, UINT16_TO_BYTE0(CMD##_CMD_OPCODE), UINT16_TO_BYTE1(CMD##_CMD_OPCODE), CMD##_CMD_LENGTH, ##__VA_ARGS__}
325 #define HCI_RAW_CMD(OPCODE, LEN, ...)                  {AM_DEVICES_COOPER_CMD, UINT16_TO_BYTE0(OPCODE), UINT16_TO_BYTE1(OPCODE), LEN, ##__VA_ARGS__}
326 
327 #ifndef LPCLK_DRIFT_VALUE
328 //! Radio Drift
329 #define LPCLK_DRIFT_VALUE               500 //!<  PPM
330 #endif
331 #define NVDS_PARAMETER_LPCLK_DRIFT      PARAM_ID_LPCLK_DRIFT, 0x06, 0x2, UINT16_TO_BYTE0(LPCLK_DRIFT_VALUE), UINT16_TO_BYTE1(LPCLK_DRIFT_VALUE)
332 
333 #ifndef EXT_WAKEUP_TIME_VALUE
334 //! External wake-up time
335 #define EXT_WAKEUP_TIME_VALUE           1000 //!<  microsecond
336 #endif
337 #define NVDS_PARAMETER_EXT_WAKEUP_TIME  PARAM_ID_EXT_WAKEUP_TIME, 0x06, 0x02, UINT16_TO_BYTE0(EXT_WAKEUP_TIME_VALUE), UINT16_TO_BYTE1(EXT_WAKEUP_TIME_VALUE)
338 #ifndef OSC_WAKEUP_TIME_VALUE
339 //! Oscillator wake-up time
340 #define OSC_WAKEUP_TIME_VALUE           1000 //!<  microsecond
341 #endif
342 #define NVDS_PARAMETER_OSC_WAKEUP_TIME  PARAM_ID_OSC_WAKEUP_TIME, 0x06, 0x02, UINT16_TO_BYTE0(OSC_WAKEUP_TIME_VALUE), UINT16_TO_BYTE1(OSC_WAKEUP_TIME_VALUE)
343 //! Radio wake-up time
344 #define NVDS_PARAMETER_RM_WAKEUP_TIME   PARAM_ID_RM_WAKEUP_TIME, 0x06, 0x02, 0x1E, 0x00
345 //! set UART_BAUDRATE
346 #define NVDS_PARAMETER_UART_BAUDRATE    PARAM_ID_UART_BAUDRATE, 0x06, 0x04, 0x00, 0x10, 0x0E, 0x00
347 //! sleep algorithm enabled
348 #define NVDS_PARAMETER_SLEEP_ENABLE     PARAM_ID_SLEEP_ENABLE, 0x06, 0x01, 0x01
349 //! sleep algorithm disabled
350 #define NVDS_PARAMETER_SLEEP_DISABLE    PARAM_ID_SLEEP_ENABLE, 0x06, 0x01, 0x00
351 //! external wake-up support
352 #define NVDS_PARAMETER_EXT_WAKEUP_ENABLE PARAM_ID_EXT_WAKEUP_ENABLE, 0x06, 0x01, 0x01
353 //! Activity Move Configuration
354 #define NVDS_PARAMETER_ACTIVITY_MOVE_CONFIG PARAM_ID_ACTIVITY_MOVE_CONFIG, 0x06, 0x01, 0x01
355 //! Enable/disable scanning for extended advertising PDUs
356 #define NVDS_PARAMETER_SCAN_EXT_ADV      PARAM_ID_SCAN_EXT_ADV, 0x06, 0x01, 0x01
357 //! default scan duration
358 #define NVDS_PARAMETER_SCHED_SCAN_DUR    PARAM_ID_SCHED_SCAN_DUR, 0x06, 0x02, 0xE4, 0x57
359 //! Programming delay
360 #define NVDS_PARAMETER_PROG_DELAY        PARAM_ID_PROG_DELAY, 0x06, 0x01, 0x1
361 //! channel assessment for BT and/or BLE
362 #define NVDS_PARAMETER_CH_ASS_EN         PARAM_ID_CH_ASS_EN, 0x06, 0x01, 0x1
363 //! sleep algorithm duration
364 #define NVDS_PARAMETER_SLEEP_ALGO_DUR   PARAM_ID_SLEEP_ALGO_DUR, 0x06, 0x02, 0xF4, 0x01
365 //! debug trace config
366 #define NVDS_PARAMETER_TRACE_CONFIG     PARAM_ID_TRACER_CONFIG, 0x06, 0x04, 0x00, 0x00, 0x00, 0x00
367 //! diagnostic port
368 #define NVDS_PARAMETER_DIAG_BLE_HW      PARAM_ID_DIAG_BLE_HW, 0x06, 0x04, 0x00, 0x00, 0x00, 0x00
369 //! SW diags configuration
370 #define NVDS_PARAMETER_DIAG_SW          PARAM_ID_DIAG_SW, 0x06, 0x04, 0xFF, 0xFF, 0xFF, 0xFF
371 //! diagport configuration
372 #define NVDS_PARAMETER_DIAG_DM_HW       PARAM_ID_DIAG_DM_HW, 0x06, 0x04, 0x00, 0x00, 0x00, 0x00
373 //! SC Private Key (Low Energy)
374 #define NVDS_PARAMETER_LE_PRIVATE_KEY_P256   PARAM_ID_LE_PRIVATE_KEY_P256, 0x06, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
375                                                                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
376                                                                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377 //! SC Debug: Used Fixed Private Key from NVDS (Low Energy)
378 #define NVDS_PARAMETER_LE_DBG_FIXED_P256_KEY  PARAM_ID_LE_DBG_FIXED_P256_KEY, 0x06, 0x01, 0x00
379 //! LE Coded PHY 500 Kbps selection
380 #define NVDS_PARAMETER_LE_CODED_PHY_500   PARAM_ID_LE_CODED_PHY_500, 0x06, 0x01, 0x00
381 
382 
383 #define AM_DEVICES_COOPER_SBL_UPDATE_STATE_INIT                     0x00
384 #define AM_DEVICES_COOPER_SBL_UPDATE_STATE_HELLO                    0x01
385 #define AM_DEVICES_COOPER_SBL_UPDATE_STATE_UPDATE                   0x02
386 #define AM_DEVICES_COOPER_SBL_UPDATE_STATE_DATA                     0x03
387 #define AM_DEVICES_COOPER_SBL_UPDATE_STATE_IMAGE_OK                 0x04
388 
389 #define AM_DEVICES_COOPER_SBL_UPDATE_IMAGE_TYPE_FW                  0x00
390 #define AM_DEVICES_COOPER_SBL_UPDATE_IMAGE_TYPE_INFO_0              0x01
391 #define AM_DEVICES_COOPER_SBL_UPDATE_IMAGE_TYPE_INFO_1              0x02
392 #define AM_DEVICES_COOPER_SBL_UPDATE_IMAGE_TYPE_NONE                0x03
393 
394 #define AM_DEVICES_COOPER_SBL_UPADTE_MAX_SPI_PKT_SIZE               0x200
395 #define AM_DEVICES_COOPER_SBL_UPADTE_INVALID_PSI_PKT_SIZE           0xFFFFFFFF
396 #define AM_DEVICES_COOPER_SBL_UPADTE_IMAGE_HDR_SIZE                 0x40
397 
398 #define AM_DEVICES_COOPER_SBL_MAX_COMM_ERR_COUNT                    0x03
399 
400 #define AM_DEVICES_COOPER_SBL_ACK_RESP_SUCCESS                      0x00
401 #define AM_DEVICES_COOPER_SBL_ACK_RESP_FAIL                         0x01
402 #define AM_DEVICES_COOPER_SBL_ACK_RESP_BAD_HDR                      0x02
403 #define AM_DEVICES_COOPER_SBL_ACK_RESP_BAD_CRC                      0x03
404 #define AM_DEVICES_COOPER_SBL_ACK_RESP_VERSION_INVALID              0x04
405 #define AM_DEVICES_COOPER_SBL_ACK_RESP_MSG_TOO_BIG                  0x05
406 #define AM_DEVICES_COOPER_SBL_ACK_RESP_UNKNOWN_MSG                  0x06
407 #define AM_DEVICES_COOPER_SBL_ACK_RESP_INVALID_ADDRESS              0x07
408 #define AM_DEVICES_COOPER_SBL_ACK_RESP_INVALID_OPERATION            0x08
409 #define AM_DEVICES_COOPER_SBL_ACK_RESP_INVALID_PARAM                0x09
410 #define AM_DEVICES_COOPER_SBL_ACK_RESP_INVALID_DATA_LEN             0x0A
411 #define AM_DEVICES_COOPER_SBL_ACK_RESP_SEQ                          0x0B
412 #define AM_DEVICES_COOPER_SBL_ACK_RESP_TOO_BIG_DATA                 0x0C
413 #define AM_DEVICES_COOPER_SBL_ACK_RESP_BAD_IMAGE                    0x0D
414 #define AM_DEVICES_COOPER_SBL_ACK_RESP_FLASH_WRITE_FAILED           0x0E
415 #define AM_DEVICES_COOPER_SBL_ACK_RESP_INVALID_DEVICE_ID            0x0F
416 #define AM_DEVICES_COOPER_SBL_ACK_RESP_BAD_KEY                      0x10
417 
418 #define AM_DEVICES_COOPER_SBL_STAT_RESP_SUCCESS                     0x00
419 #define AM_DEVICES_COOPER_SBL_STAT_RESP_FAIL                        0x01
420 #define AM_DEVICES_COOPER_SBL_STAT_RESP_BAD_HDR                     0x02
421 #define AM_DEVICES_COOPER_SBL_STAT_RESP_BAD_CRC                     0x03
422 #define AM_DEVICES_COOPER_SBL_STAT_RESP_STAT_BAD_SBL                0x04
423 #define AM_DEVICES_COOPER_SBL_STAT_RESP_BAD_IMAGE                   0x05
424 #define AM_DEVICES_COOPER_SBL_STAT_RESP_UNKNOWN_MSG                 0x06
425 #define AM_DEVICES_COOPER_SBL_STAT_RESP_FW_UPDATE_REQ               0x07
426 #define AM_DEVICES_COOPER_SBL_STAT_RESP_INFO0_UPDATE_REQ            0x08
427 #define AM_DEVICES_COOPER_SBL_STAT_RESP_INFO1_UPDATE_REQ            0x09
428 
429 #define AM_DEVICES_COOPER_SBL_STATUS_INIT                           0x00000000
430 #define AM_DEVICES_COOPER_SBL_STATUS_OK                             0xA5A5A5A5
431 #define AM_DEVICES_COOPER_SBL_STATUS_FAIL                           0xA1A1A1A1
432 #define AM_DEVICES_COOPER_SBL_STATUS_CRC_FAIL                       0xA2A2A2A2
433 #define AM_DEVICES_COOPER_SBL_STATUS_UPDATE_FW                      0x4598F231
434 #define AM_DEVICES_COOPER_SBL_STATUS_UPDATE_INFO_0                  0x8730DA5B
435 #define AM_DEVICES_COOPER_SBL_STATUS_UPDATE_INFO_1                  0x09FA3725
436 #define AM_DEVICES_COOPER_SBL_STATUS_IN_PROGRESS                    0xA78BD32C
437 #define AM_DEVICES_COOPER_SBL_STATUS_UPDATE_IMAGE_FAIL              0xA3A3A3A3
438 
439 #define AM_DEVICES_COOPER_SBL_DEFAULT_FW_VERSION                    0xFFFFFFFF
440 
441 #define AM_DEVICES_COOPER_SBL_STAT_VER_ROLL_BACK_EN                 0xFFFFFFFF
442 #define AM_DEVICES_COOPER_SBL_STAT_VER_ROLL_BACK_DBL                0xFFFFFF00
443 
444 #define AM_DEVICES_COOPER_SBL_FW_IMAGE_ADDRESS                      0x10030000
445 #define AM_DEVICES_COOPER_SBL_FW_IMAGE_SIZE                         0x22890
446 #define AM_DEVICES_COOPER_SBL_FW_IMAGE_SIZE_MAX                     0x30000
447 
448 #define AM_DEVICES_COOPER_SBL_INFO0_PATCH_ADDRESS                   AM_DEVICES_COOPER_SBL_FW_IMAGE_ADDRESS +  AM_DEVICES_COOPER_SBL_FW_IMAGE_SIZE_MAX
449 #define AM_DEVICES_COOPER_SBL_INFO0_PATCH_SIZE                      0x180
450 #define AM_DEVICES_COOPER_SBL_INFO0_PATCH_SIZE_MAX                  0x200
451 
452 #define AM_DEVICES_COOPER_SBL_INFO1_PATCH_ADDRESS                   AM_DEVICES_COOPER_SBL_INFO0_PATCH_ADDRESS +  AM_DEVICES_COOPER_SBL_INFO0_PATCH_SIZE_MAX
453 #define AM_DEVICES_COOPER_SBL_INFO1_PATCH_SIZE                      0x80
454 #define AM_DEVICES_COOPER_SBL_INFO1_PATCH_SIZE_MAX                  0x100
455 
456 #define AM_DEVICES_COOPER_STATE_STARTUP                             0x00000000
457 #define AM_DEVICES_COOPER_STATE_INITIALIZED                         0x00000001
458 #define AM_DEVICES_COOPER_STATE_INITIALIZE_FAIL                     0x00000002
459 
460 //! Signatures for the image downloads
461 #define COOPER_INFO0_UPDATE_SIGN   0xB35D18C9
462 //! Signatures for the image downloads
463 #define COOPER_INFO1_UPDATE_SIGN   0x38B75A0D
464 //! Signatures for the image downloads
465 #define COOPER_FW_UPDATE_SIGN      0xC593876A
466 
467 //
468 //!
469 //
470 typedef struct
471 {
472     uint8_t*    pImageAddress;
473     uint32_t    imageSize;
474     uint32_t    imageType;
475     uint32_t    version;
476 } am_devices_cooper_sbl_update_data_t;
477 
478 
479 //
480 //!
481 //
482 typedef struct
483 {
484     uint32_t    ui32SblUpdateState;
485     uint8_t*    pImageBuf;
486     uint32_t    ui32ImageSize;
487     uint8_t*    pDataBuf;
488     uint32_t    ui32DataSize;
489     uint32_t    ui32PacketNumber;
490     uint32_t    ui32TotalPackets;
491     uint32_t    ui32ImageType;
492     uint32_t    ui32ErrorCounter;
493     uint32_t*   pWorkBuf;
494 
495     uint32_t    ui32CooperFWImageVersion;
496     uint32_t    ui32CooperSblStatus;
497     uint32_t    ui32CooperVerRollBackConfig; //Version 2
498     uint32_t    ui32copperChipIdWord0;  //Version 2
499     uint32_t    ui32copperChipIdWord1;  //Version 2
500 
501     uint32_t    ui32RxPacketLength;
502     bool        bRxCrcCheckPass;
503 } am_devices_cooper_sbl_update_state_t;
504 
505 //
506 //! Message types
507 //
508 typedef enum
509 {
510     AM_SBL_HOST_MSG_HELLO = 0,
511     AM_SBL_HOST_MSG_STATUS,
512     AM_SBL_HOST_MSG_UPDATE_STATUS,
513     AM_SBL_HOST_MSG_UPDATE,
514     AM_SBL_HOST_MSG_FW_CONTINUE,
515     AM_SBL_HOST_MSG_NACK,
516     AM_SBL_HOST_MSG_RESET,
517     AM_SBL_HOST_MSG_ACK,
518     AM_SBL_HOST_MSG_DATA
519 } AM_SBL_HOST_MSG_E;
520 
521 //////////////////SBL Messages To and From Host////////////////////////
522 
523 //
524 //! Message header
525 //
526 typedef struct
527 {
528     uint32_t    msgCrc;
529     uint16_t    msgType;
530     uint16_t    msgLength;
531 } am_sbl_host_msg_hdr_t;
532 
533 //
534 //! Hello Message
535 //
536 typedef struct
537 {
538     am_sbl_host_msg_hdr_t    msgHdr;
539 } am_sbl_host_msg_hello_t;
540 
541 //
542 //! Status message
543 //
544 typedef struct
545 {
546     am_sbl_host_msg_hdr_t   msgHdr;
547     uint32_t                versionNumber;
548     uint32_t                maxImageSize;
549     uint32_t                bootStatus;
550     uint32_t                verRollBackStatus;  //!< Version 2
551     uint32_t                copperChipIdWord0;  //!< Version 2
552     uint32_t                copperChipIdWord1;  //!< Version 2
553 } am_sbl_host_msg_status_t;
554 
555 //
556 //! Update Message
557 //
558 typedef struct
559 {
560     am_sbl_host_msg_hdr_t   msgHdr;
561     uint32_t                imageSize;
562     uint32_t                maxPacketSize;
563     uint32_t                NumPackets;
564     uint32_t                versionNumber;
565 } am_sbl_host_msg_update_t;
566 
567 //
568 //! Data Message
569 //
570 typedef struct
571 {
572     am_sbl_host_msg_hdr_t   msgHdr;
573     uint32_t                packetNumber;
574     uint8_t                 data[];
575 } am_sbl_host_msg_data_t;
576 
577 //
578 //! FW continue Message
579 //
580 typedef struct
581 {
582     am_sbl_host_msg_hdr_t   msgHdr;
583     uint32_t                reserved;
584 } am_sbl_host_msg_fw_continue_t;
585 //
586 //! Reset Message
587 //
588 typedef struct
589 {
590     am_sbl_host_msg_hdr_t   msgHdr;
591     uint32_t                reserved;
592 } am_sbl_host_msg_reset_t;
593 
594 //
595 //! Ack /Nack Message
596 //
597 typedef struct
598 {
599     am_sbl_host_msg_hdr_t   msgHdr;
600     uint32_t                srcMsgType;
601     uint32_t                status;
602     uint32_t                nextPacketNum;  //!< only valid for data messages ack
603     uint32_t                reserved[3];    //!< Version 2
604 } am_sbl_host_msg_ack_nack_t;
605 
606 
607 #define   AM_DEVICES_COOPER_SBL_MAX_INFO_0_PATCH_VALUES         64
608 #define   AM_DEVICES_COOPER_SBL_BIT_MASK_PER_WORD               16
609 #define   AM_DEVICES_COOPER_SBL_INFO_0_REPLACE_TRIM        0x00000003
610 //
611 //! INFO 0 patch for Rev A only
612 //
613 typedef struct
614 {
615     uint32_t    magicNumNSize;            //!<  0x0A500180
616     uint32_t    rsaSize;                  //!<  0x00000000
617     uint32_t    loadHdrReserved[14];      //!<  All Zeros
618     uint32_t    cmacHash[4];              //!<  All Zeros
619     uint32_t    authKeyNHashDataSize;     //!<  All Zeros
620     uint32_t    xBlock;                   //!<  0x00000000
621     uint32_t    keyDervData[4];           //!<  All Zeros
622 
623     uint32_t    bitMaskWord[4];
624     uint32_t    reserved_0;
625     uint32_t    trimDataWords[AM_DEVICES_COOPER_SBL_MAX_INFO_0_PATCH_VALUES];
626     uint32_t    reserved_1;
627 
628 } am_sbl_info0_patch_blob_t;
629 
630 //
631 //! INFO 0 patch data
632 //
633 typedef struct
634 {
635     uint32_t    wordOffset;
636     uint32_t    value;
637 } am_sbl_info0_patch_data_t;
638 
639 //
640 //! BLE controller function callback
641 //
642 typedef struct
643 {
644     /**
645      *************************************************************************************
646      * @brief Starts a data transmission via IOM
647      *
648      * @param[in]  data        Pointer to the TX buffer
649      * @param[in]  size        Size of the transmission
650      * @return                 Status of data transmission, 0 is success.
651      *************************************************************************************
652      */
653     int (*write)(uint8_t *data, uint16_t len);
654 
655     /**
656      *************************************************************************************
657      * @brief Reset the BLE controller via RESET GPIO.
658      *
659      *************************************************************************************
660      */
661     void (*reset)(void);
662 } am_devices_cooper_callback_t;
663 
664 //
665 //! NVDS data which needs to set to BLE controller
666 //
667 extern uint8_t am_devices_cooper_nvds[];
668 
669 //*****************************************************************************
670 //
671 //! @brief Update Image.
672 //!
673 //! @return 0 is success
674 //
675 //*****************************************************************************
676 uint32_t am_devices_cooper_update_image(void);
677 
678 //*****************************************************************************
679 //
680 //! @brief Initialize the Image Update state machine
681 //
682 //*****************************************************************************
683 void am_devices_cooper_image_update_init(void);
684 
685 //*****************************************************************************
686 //
687 //! @brief Get cooper firmware image from local binary
688 //!
689 //! @param pFwImage
690 //!
691 //! @return true if pFwImage isn't null
692 //
693 //*****************************************************************************
694 bool am_devices_cooper_get_FwImage(am_devices_cooper_sbl_update_data_t *pFwImage );
695 
696 //*****************************************************************************
697 //
698 //! @brief Get cooper info1 image from local binary
699 //!
700 //! @param pInfo1Image
701 //!
702 //! @return true if pInfo1Image isn't null
703 //
704 //*****************************************************************************
705 bool am_devices_cooper_get_info1_patch(am_devices_cooper_sbl_update_data_t *pInfo1Image);
706 
707 //*****************************************************************************
708 //
709 //! @brief Get cooper info0 image from local binary
710 //!
711 //! @param pInfo0Image
712 //!
713 //! @return true if  pInfo0Image isn't null
714 //
715 //*****************************************************************************
716 bool am_devices_cooper_get_info0_patch(am_devices_cooper_sbl_update_data_t *pInfo0Image);
717 
718 //*****************************************************************************
719 //
720 //! @brief Initialize the BLE controller driver.
721 //!
722 //! @param cb pointer of BLE Controller callback
723 //
724 //*****************************************************************************
725 uint32_t am_devices_cooper_init(am_devices_cooper_callback_t *cb);
726 
727 //*****************************************************************************
728 //
729 //! @brief Reset BLE Controller.
730 //!
731 //! This function asserts reset pin to reset the BLE controller
732 //
733 //*****************************************************************************
734 extern void am_devices_cooper_reset(void);
735 
736 //*****************************************************************************
737 //
738 //! @brief Reset the BLE controller and check if there's request to update
739 //!
740 //! @return status from am_devices_cooper_status_t
741 //
742 //*****************************************************************************
743 extern uint32_t am_devices_cooper_reset_with_sbl_check(void);
744 
745 //*****************************************************************************
746 //
747 //! @brief Process the received packet from BLE Controller in SBL handshake stage.
748 //!
749 //! @param pBuf pointer of received packet
750 //! @param len  length of received packet
751 //
752 //*****************************************************************************
753 void am_devices_cooper_handshake_recv(uint8_t* pBuf, uint16_t len);
754 
755 //*****************************************************************************
756 //
757 //! @brief Get the Cooper initialization state
758 //!
759 //! @return Cooper current initialization state
760 //
761 //*****************************************************************************
762 uint32_t am_devices_cooper_get_initialize_state(void);
763 
764 //*****************************************************************************
765 //
766 //! @brief Set the Cooper initialization state
767 //!
768 //! @param state  Cooper current initialization state
769 //
770 //*****************************************************************************
771 void am_devices_cooper_set_initialize_state(uint32_t state);
772 
773 #ifdef __cplusplus
774 }
775 #endif
776 
777 #endif // AM_DEVICES_COOPER_H
778 
779 //*****************************************************************************
780 //
781 // End Doxygen group.
782 //! @}
783 //
784 //*****************************************************************************
785