libnile
Loading...
Searching...
No Matches
mcu.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023, 2024, 2025 Adrian "asie" Siekierka
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 *
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 *
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22
23#ifndef NILE_MCU_H_
24#define NILE_MCU_H_
25
26#include <wonderful.h>
27#include "hardware.h"
28
29#define NILE_MCU_BOOT_ACK 0x79
30#define NILE_MCU_BOOT_NACK 0x1F
31#define NILE_MCU_BOOT_START 0x5A
32#define NILE_MCU_BOOT_GET 0x00
33#define NILE_MCU_BOOT_GET_VERSION 0x01
34#define NILE_MCU_BOOT_GET_ID 0x02
35#define NILE_MCU_BOOT_READ_MEMORY 0x11
36#define NILE_MCU_BOOT_JUMP 0x21
37#define NILE_MCU_BOOT_WRITE_MEMORY 0x31
38#define NILE_MCU_BOOT_ERASE_MEMORY 0x44
39#define NILE_MCU_BOOT_SPECIAL 0x50
40#define NILE_MCU_BOOT_EXT_SPECIAL 0x51
41#define NILE_MCU_BOOT_WRITE_LOCK 0x63
42#define NILE_MCU_BOOT_WRITE_UNLOCK 0x73
43#define NILE_MCU_BOOT_READ_LOCK 0x82
44#define NILE_MCU_BOOT_READ_UNLOCK 0x92
45#define NILE_MCU_BOOT_GET_CRC 0xA1
46
47#define NILE_MCU_BOOT_ERASE_ALL_SECTORS 0xFFFF
48
49#define NILE_MCU_BOOT_FLAG_SIZE 0x01
50#define NILE_MCU_BOOT_FLAG_CHECKSUM 0x02
51
52#define NILE_MCU_FLASH_START 0x08000000
53#define NILE_MCU_FLASH_PAGE_SIZE 2048
54
55#define NILE_MCU_NATIVE_CMD(cmd, arg) (((cmd) & 0x7F) | ((arg) << 7))
56
62#define NILE_MCU_NATIVE_RESET_TIME_US 40000
63
70#define NILE_MCU_NATIVE_MODESWITCH_TIME_US 2500
71
72#ifndef __ASSEMBLER__
73#include <stdbool.h>
74#include <stddef.h>
75#include <stdint.h>
76
84bool nile_mcu_reset(bool to_bootloader);
85
87bool nile_mcu_boot_send_cmd(uint8_t cmd);
88bool nile_mcu_boot_send_data(const void __far *buffer, uint16_t len, uint8_t flags);
89uint16_t nile_mcu_boot_recv_data(void __far* buffer, uint16_t buflen, uint8_t flags);
90
96
101uint16_t nile_mcu_boot_get_id(void);
102
110bool nile_mcu_boot_read_memory(uint32_t address, void __far* buffer, uint16_t buflen);
111
117bool nile_mcu_boot_jump(uint32_t address);
118
126bool nile_mcu_boot_write_memory(uint32_t address, const void __far* buffer, uint16_t buflen);
127
135bool nile_mcu_boot_erase_memory(uint16_t sector_address, uint16_t sector_count);
136
143
147#define NILE_MCU_NATIVE_ERROR_SPI -1
148
152#define NILE_MCU_NATIVE_ERROR_MCU -2
153
163static inline int16_t nile_mcu_native_send_cmd(uint16_t cmd, const void __far* buffer, int buflen) {
164 int16_t __nile_mcu_native_send_cmd_async(uint16_t cmd, int buflen, const void __far* buffer);
165 return __nile_mcu_native_send_cmd_async(cmd, buflen, buffer);
166}
167// int16_t nile_mcu_native_send_cmd(uint16_t cmd, const void __wf_cram* buffer, int buflen);
168
178int16_t nile_mcu_native_recv_cmd(void __far* buffer, uint16_t buflen);
179
186int16_t nile_mcu_native_recv_cmd_start(uint16_t resplen);
187
195int16_t nile_mcu_native_recv_cmd_finish(void __far* buffer, uint16_t buflen);
196
197static inline int16_t nile_mcu_native_recv_cmd_response_none(void) {
198 return nile_mcu_native_recv_cmd_finish(NULL, 0);
199}
200
201static inline int16_t nile_mcu_native_recv_cmd_response_uint8(void) {
202 int16_t result;
203 uint8_t bytes;
204 if ((result = nile_mcu_native_recv_cmd_finish(&bytes, 1)) < 1) return result;
205 return bytes;
206}
207
208static inline int16_t nile_mcu_native_recv_cmd_response_int16(void) {
209 int16_t result, bytes = 0;
210 if ((result = nile_mcu_native_recv_cmd_finish(&bytes, 2)) < 1) return result;
211 return bytes;
212}
213
221
225static inline int16_t nile_mcu_native_mcu_switch_mode(uint8_t mode) {
226 return nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(0x01, mode), NULL, 0);
227}
228
234static inline int16_t nile_mcu_native_mcu_spi_set_speed_sync(uint8_t speed) {
235 int16_t result;
236 uint8_t op_result;
237 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(0x02, speed), NULL, 0)) < 0) return result;
238 if ((result = nile_mcu_native_recv_cmd(&op_result, 1)) < 1) return result;
239 return op_result;
240}
241
242static inline int16_t nile_mcu_native_mcu_get_uuid_sync(void __far* buffer, uint16_t buflen) {
243 int16_t result;
244 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(0x03, 0), NULL, 0)) < 0) return result;
245 return nile_mcu_native_recv_cmd(buffer, buflen);
246}
247
248typedef struct {
249 uint16_t major;
250 uint16_t minor;
252
253static inline int16_t nile_mcu_native_mcu_get_version_sync(void __far* buffer, uint16_t buflen) {
254 int16_t result;
255 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(0x0F, 0), NULL, 0)) < 0) return result;
256 return nile_mcu_native_recv_cmd(buffer, buflen);
257}
258
259#endif /* __ASSEMBLER__ */
260
261#endif /* NILE_MCU_H_ */
static int16_t nile_mcu_native_recv_cmd_response_none(void)
Definition mcu.h:197
static int16_t nile_mcu_native_mcu_get_version_sync(void __far *buffer, uint16_t buflen)
Definition mcu.h:253
uint8_t nile_mcu_boot_get_version(void)
Get the version of the SPI protocol used. More information is available in the Application Note AN428...
static int16_t nile_mcu_native_mcu_spi_set_speed_sync(uint8_t speed)
Tell the MCU to operate at a specific SPI speed.
Definition mcu.h:234
int16_t nile_mcu_native_recv_cmd(void __far *buffer, uint16_t buflen)
Receive the response of a "native protocol" MCU command synchronously.
static int16_t nile_mcu_native_mcu_get_uuid_sync(void __far *buffer, uint16_t buflen)
Definition mcu.h:242
uint16_t nile_mcu_boot_get_id(void)
Get the chip ID. More information is available in the Application Note AN4286.
bool nile_mcu_boot_send_data(const void __far *buffer, uint16_t len, uint8_t flags)
static int16_t nile_mcu_native_recv_cmd_response_uint8(void)
Definition mcu.h:201
#define NILE_MCU_NATIVE_CMD(cmd, arg)
Definition mcu.h:55
bool nile_mcu_boot_read_memory(uint32_t address, void __far *buffer, uint16_t buflen)
Request bytes from the MCU's address space.
bool nile_mcu_reset(bool to_bootloader)
Reset the MCU.
static int16_t nile_mcu_native_recv_cmd_response_int16(void)
Definition mcu.h:208
int16_t nile_mcu_native_recv_cmd_start(uint16_t resplen)
Start receiving the response of a "native protocol" MCU command asynchronously.
static int16_t nile_mcu_native_mcu_switch_mode(uint8_t mode)
Switch the mode in which the MCU is operating.
Definition mcu.h:225
bool nile_mcu_boot_jump(uint32_t address)
Request that the MCU branch to a specific address in memory.
nile_mcu_native_mode_t
Definition mcu.h:214
@ NILE_MCU_NATIVE_MODE_STANDBY
MCU standby mode - will not respond to further SPI messages until reset.
Definition mcu.h:219
@ NILE_MCU_NATIVE_MODE_RTC
RTC emulation mode.
Definition mcu.h:217
@ NILE_MCU_NATIVE_MODE_CDC
CDC output-only mode.
Definition mcu.h:218
@ NILE_MCU_NATIVE_MODE_EEPROM
EEPROM emulation mode.
Definition mcu.h:216
@ NILE_MCU_NATIVE_MODE_CMD
Native command mode.
Definition mcu.h:215
bool nile_mcu_boot_send_cmd(uint8_t cmd)
static bool nile_mcu_boot_erase_all_memory(void)
Erase all of the MCU's flash memory.
Definition mcu.h:140
bool nile_mcu_boot_write_memory(uint32_t address, const void __far *buffer, uint16_t buflen)
Write bytes to the MCU's address space (RAM or flash memory).
static int16_t nile_mcu_native_send_cmd(uint16_t cmd, const void __far *buffer, int buflen)
Send a "native protocol" MCU command asynchronously.
Definition mcu.h:163
uint16_t nile_mcu_boot_recv_data(void __far *buffer, uint16_t buflen, uint8_t flags)
bool nile_mcu_boot_erase_memory(uint16_t sector_address, uint16_t sector_count)
Erase pages of the MCU's flash memory.
#define NILE_MCU_BOOT_ERASE_ALL_SECTORS
Definition mcu.h:47
bool nile_mcu_boot_wait_ack(void)
int16_t nile_mcu_native_recv_cmd_finish(void __far *buffer, uint16_t buflen)
Finish receiving the response of a "native protocol" MCU command.