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#include "mcu/protocol.h"
29
30#define NILE_MCU_BOOT_ACK 0x79
31#define NILE_MCU_BOOT_NACK 0x1F
32#define NILE_MCU_BOOT_START 0x5A
33#define NILE_MCU_BOOT_GET 0x00
34#define NILE_MCU_BOOT_GET_VERSION 0x01
35#define NILE_MCU_BOOT_GET_ID 0x02
36#define NILE_MCU_BOOT_READ_MEMORY 0x11
37#define NILE_MCU_BOOT_JUMP 0x21
38#define NILE_MCU_BOOT_WRITE_MEMORY 0x31
39#define NILE_MCU_BOOT_ERASE_MEMORY 0x44
40#define NILE_MCU_BOOT_SPECIAL 0x50
41#define NILE_MCU_BOOT_EXT_SPECIAL 0x51
42#define NILE_MCU_BOOT_WRITE_LOCK 0x63
43#define NILE_MCU_BOOT_WRITE_UNLOCK 0x73
44#define NILE_MCU_BOOT_READ_LOCK 0x82
45#define NILE_MCU_BOOT_READ_UNLOCK 0x92
46#define NILE_MCU_BOOT_GET_CRC 0xA1
47
48#define NILE_MCU_BOOT_ERASE_ALL_SECTORS 0xFFFF
49
50#define NILE_MCU_BOOT_FLAG_SIZE 0x01
51#define NILE_MCU_BOOT_FLAG_CHECKSUM 0x02
52
53#define NILE_MCU_FLASH_START 0x08000000
54#define NILE_MCU_FLASH_PAGE_SIZE 2048
55
56#define NILE_MCU_NATIVE_CMD(cmd, arg) (((cmd) & 0x7F) | ((arg) << 7))
57
63#define NILE_MCU_NATIVE_RESET_TIME_US 40000
64
71#define NILE_MCU_NATIVE_MODESWITCH_TIME_US 2500
72
73#ifndef __ASSEMBLER__
74#include <stdbool.h>
75#include <stddef.h>
76#include <stdint.h>
77
85bool nile_mcu_reset(bool to_bootloader);
86
88bool nile_mcu_boot_send_cmd(uint8_t cmd);
89bool nile_mcu_boot_send_data(const void __far *buffer, uint16_t len, uint8_t flags);
90uint16_t nile_mcu_boot_recv_data(void __far* buffer, uint16_t buflen, uint8_t flags);
91
97
102uint16_t nile_mcu_boot_get_id(void);
103
111bool nile_mcu_boot_read_memory(uint32_t address, void __far* buffer, uint16_t buflen);
112
118bool nile_mcu_boot_jump(uint32_t address);
119
127bool nile_mcu_boot_write_memory(uint32_t address, const void __far* buffer, uint16_t buflen);
128
136bool nile_mcu_boot_erase_memory(uint16_t sector_address, uint16_t sector_count);
137
144
148#define NILE_MCU_NATIVE_ERROR_SPI -1
149
153#define NILE_MCU_NATIVE_ERROR_MCU -2
154
164static inline int16_t nile_mcu_native_send_cmd(uint16_t cmd, const void __far* buffer, int buflen) {
165 int16_t __nile_mcu_native_send_cmd_async(uint16_t cmd, int buflen, const void __far* buffer);
166 return __nile_mcu_native_send_cmd_async(cmd, buflen, buffer);
167}
168// int16_t nile_mcu_native_send_cmd(uint16_t cmd, const void __wf_cram* buffer, int buflen);
169
179int16_t nile_mcu_native_recv_cmd(void __far* buffer, uint16_t buflen);
180
187int16_t nile_mcu_native_recv_cmd_start(uint16_t resplen);
188
196int16_t nile_mcu_native_recv_cmd_finish(void __far* buffer, uint16_t buflen);
197
198static inline int16_t nile_mcu_native_recv_cmd_response_none(void) {
199 return nile_mcu_native_recv_cmd_finish(NULL, 0);
200}
201
202static inline int16_t nile_mcu_native_recv_cmd_response_uint8(void) {
203 int16_t result;
204 uint8_t bytes;
205 if ((result = nile_mcu_native_recv_cmd_finish(&bytes, 1)) < 1) return result;
206 return bytes;
207}
208
209static inline int16_t nile_mcu_native_recv_cmd_response_int16(void) {
210 int16_t result, bytes = 0;
211 if ((result = nile_mcu_native_recv_cmd_finish(&bytes, 2)) < 1) return result;
212 return bytes;
213}
214
215#endif /* __ASSEMBLER__ */
216
217#endif /* NILE_MCU_H_ */
static int16_t nile_mcu_native_recv_cmd_response_none(void)
Definition mcu.h:198
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...
int16_t nile_mcu_native_recv_cmd(void __far *buffer, uint16_t buflen)
Receive the response of a "native protocol" MCU command synchronously.
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:202
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:209
int16_t nile_mcu_native_recv_cmd_start(uint16_t resplen)
Start receiving the response of a "native protocol" MCU command asynchronously.
bool nile_mcu_boot_jump(uint32_t address)
Request that the MCU branch to a specific address in memory.
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:141
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:164
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:48
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.