libnile
Loading...
Searching...
No Matches
mcu.h File Reference
#include <wonderful.h>
#include "hardware.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define NILE_MCU_BOOT_ACK   0x79
#define NILE_MCU_BOOT_NACK   0x1F
#define NILE_MCU_BOOT_START   0x5A
#define NILE_MCU_BOOT_GET   0x00
#define NILE_MCU_BOOT_GET_VERSION   0x01
#define NILE_MCU_BOOT_GET_ID   0x02
#define NILE_MCU_BOOT_READ_MEMORY   0x11
#define NILE_MCU_BOOT_JUMP   0x21
#define NILE_MCU_BOOT_WRITE_MEMORY   0x31
#define NILE_MCU_BOOT_ERASE_MEMORY   0x44
#define NILE_MCU_BOOT_SPECIAL   0x50
#define NILE_MCU_BOOT_EXT_SPECIAL   0x51
#define NILE_MCU_BOOT_WRITE_LOCK   0x63
#define NILE_MCU_BOOT_WRITE_UNLOCK   0x73
#define NILE_MCU_BOOT_READ_LOCK   0x82
#define NILE_MCU_BOOT_READ_UNLOCK   0x92
#define NILE_MCU_BOOT_GET_CRC   0xA1
#define NILE_MCU_BOOT_ERASE_ALL_SECTORS   0xFFFF
#define NILE_MCU_BOOT_FLAG_SIZE   0x01
#define NILE_MCU_BOOT_FLAG_CHECKSUM   0x02
#define NILE_MCU_FLASH_START   0x08000000
#define NILE_MCU_FLASH_PAGE_SIZE   2048
#define NILE_MCU_NATIVE_CMD(cmd, arg)
#define NILE_MCU_NATIVE_ERROR_SPI   -1
 SPI communication error.
#define NILE_MCU_NATIVE_ERROR_MCU   -2
 MCU communication error.
#define nile_mcu_native_cdc_write_async_finish   nile_mcu_native_recv_cmd_response_int16
#define nile_mcu_native_cdc_available_async_finish   nile_mcu_native_recv_cmd_response_int16

Enumerations

enum  nile_mcu_native_mode_t {
  NILE_MCU_NATIVE_MODE_CMD = 0x00 , NILE_MCU_NATIVE_MODE_EEPROM = 0x01 , NILE_MCU_NATIVE_MODE_RTC = 0x02 , NILE_MCU_NATIVE_MODE_CDC = 0x03 ,
  NILE_MCU_NATIVE_MODE_STANDBY = 0xFF
}

Functions

bool nile_mcu_reset (bool to_bootloader)
 Reset the MCU.
bool nile_mcu_boot_wait_ack (void)
bool nile_mcu_boot_send_cmd (uint8_t cmd)
bool nile_mcu_boot_send_data (const void __far *buffer, uint16_t len, uint8_t flags)
uint16_t nile_mcu_boot_recv_data (void __far *buffer, uint16_t buflen, uint8_t flags)
uint8_t nile_mcu_boot_get_version (void)
 Get the version of the SPI protocol used. More information is available in the Application Note AN4286.
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_read_memory (uint32_t address, void __far *buffer, uint16_t buflen)
 Request bytes from the MCU's address space.
bool nile_mcu_boot_jump (uint32_t address)
 Request that the MCU branch to a specific address in memory.
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).
bool nile_mcu_boot_erase_memory (uint16_t sector_address, uint16_t sector_count)
 Erase pages of the MCU's flash memory.
static bool nile_mcu_boot_erase_all_memory (void)
 Erase all of the MCU's 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.
int16_t nile_mcu_native_recv_cmd (void __far *buffer, uint16_t buflen)
 Receive the response of a "native protocol" MCU command synchronously.
int16_t nile_mcu_native_recv_cmd_start (uint16_t resplen)
 Start receiving the response of a "native protocol" MCU command asynchronously.
int16_t nile_mcu_native_recv_cmd_finish (void __far *buffer, uint16_t buflen)
 Finish receiving the response of a "native protocol" MCU command.
static int16_t nile_mcu_native_recv_cmd_response_uint8 (void)
static int16_t nile_mcu_native_recv_cmd_response_int16 (void)
static int16_t nile_mcu_native_mcu_switch_mode (uint8_t mode)
 Switch the mode in which the MCU is operating.
static int16_t nile_mcu_native_mcu_spi_set_speed_sync (uint8_t speed)
 Tell the MCU to operate at a specific SPI speed.
static int16_t nile_mcu_native_mcu_get_uuid_sync (void __far *buffer, uint16_t buflen)
static int16_t nile_mcu_native_cdc_read_sync (void __far *buffer, uint16_t buflen)
static int16_t nile_mcu_native_cdc_write_sync (const void __wf_cram *buffer, uint16_t buflen)
static int16_t nile_mcu_native_cdc_write_async_start (const void __wf_cram *buffer, uint16_t buflen)
static int16_t nile_mcu_native_cdc_available_sync (void)
static int16_t nile_mcu_native_cdc_available_async_start (const void __wf_cram *buffer, uint16_t buflen)
static int16_t nile_mcu_native_cdc_clear_sync (void)

Macro Definition Documentation

◆ NILE_MCU_BOOT_ACK

#define NILE_MCU_BOOT_ACK   0x79

Definition at line 29 of file mcu.h.

◆ NILE_MCU_BOOT_ERASE_ALL_SECTORS

#define NILE_MCU_BOOT_ERASE_ALL_SECTORS   0xFFFF

Definition at line 47 of file mcu.h.

◆ NILE_MCU_BOOT_ERASE_MEMORY

#define NILE_MCU_BOOT_ERASE_MEMORY   0x44

Definition at line 38 of file mcu.h.

◆ NILE_MCU_BOOT_EXT_SPECIAL

#define NILE_MCU_BOOT_EXT_SPECIAL   0x51

Definition at line 40 of file mcu.h.

◆ NILE_MCU_BOOT_FLAG_CHECKSUM

#define NILE_MCU_BOOT_FLAG_CHECKSUM   0x02

Definition at line 50 of file mcu.h.

◆ NILE_MCU_BOOT_FLAG_SIZE

#define NILE_MCU_BOOT_FLAG_SIZE   0x01

Definition at line 49 of file mcu.h.

◆ NILE_MCU_BOOT_GET

#define NILE_MCU_BOOT_GET   0x00

Definition at line 32 of file mcu.h.

◆ NILE_MCU_BOOT_GET_CRC

#define NILE_MCU_BOOT_GET_CRC   0xA1

Definition at line 45 of file mcu.h.

◆ NILE_MCU_BOOT_GET_ID

#define NILE_MCU_BOOT_GET_ID   0x02

Definition at line 34 of file mcu.h.

◆ NILE_MCU_BOOT_GET_VERSION

#define NILE_MCU_BOOT_GET_VERSION   0x01

Definition at line 33 of file mcu.h.

◆ NILE_MCU_BOOT_JUMP

#define NILE_MCU_BOOT_JUMP   0x21

Definition at line 36 of file mcu.h.

◆ NILE_MCU_BOOT_NACK

#define NILE_MCU_BOOT_NACK   0x1F

Definition at line 30 of file mcu.h.

◆ NILE_MCU_BOOT_READ_LOCK

#define NILE_MCU_BOOT_READ_LOCK   0x82

Definition at line 43 of file mcu.h.

◆ NILE_MCU_BOOT_READ_MEMORY

#define NILE_MCU_BOOT_READ_MEMORY   0x11

Definition at line 35 of file mcu.h.

◆ NILE_MCU_BOOT_READ_UNLOCK

#define NILE_MCU_BOOT_READ_UNLOCK   0x92

Definition at line 44 of file mcu.h.

◆ NILE_MCU_BOOT_SPECIAL

#define NILE_MCU_BOOT_SPECIAL   0x50

Definition at line 39 of file mcu.h.

◆ NILE_MCU_BOOT_START

#define NILE_MCU_BOOT_START   0x5A

Definition at line 31 of file mcu.h.

◆ NILE_MCU_BOOT_WRITE_LOCK

#define NILE_MCU_BOOT_WRITE_LOCK   0x63

Definition at line 41 of file mcu.h.

◆ NILE_MCU_BOOT_WRITE_MEMORY

#define NILE_MCU_BOOT_WRITE_MEMORY   0x31

Definition at line 37 of file mcu.h.

◆ NILE_MCU_BOOT_WRITE_UNLOCK

#define NILE_MCU_BOOT_WRITE_UNLOCK   0x73

Definition at line 42 of file mcu.h.

◆ NILE_MCU_FLASH_PAGE_SIZE

#define NILE_MCU_FLASH_PAGE_SIZE   2048

Definition at line 53 of file mcu.h.

◆ NILE_MCU_FLASH_START

#define NILE_MCU_FLASH_START   0x08000000

Definition at line 52 of file mcu.h.

◆ nile_mcu_native_cdc_available_async_finish

#define nile_mcu_native_cdc_available_async_finish   nile_mcu_native_recv_cmd_response_int16

Definition at line 260 of file mcu.h.

◆ nile_mcu_native_cdc_write_async_finish

#define nile_mcu_native_cdc_write_async_finish   nile_mcu_native_recv_cmd_response_int16

Definition at line 246 of file mcu.h.

◆ NILE_MCU_NATIVE_CMD

#define NILE_MCU_NATIVE_CMD ( cmd,
arg )
Value:
(((cmd) & 0x7F) | ((arg) << 7))

Definition at line 55 of file mcu.h.

◆ NILE_MCU_NATIVE_ERROR_MCU

#define NILE_MCU_NATIVE_ERROR_MCU   -2

MCU communication error.

Definition at line 137 of file mcu.h.

◆ NILE_MCU_NATIVE_ERROR_SPI

#define NILE_MCU_NATIVE_ERROR_SPI   -1

SPI communication error.

Definition at line 132 of file mcu.h.

Enumeration Type Documentation

◆ nile_mcu_native_mode_t

Enumerator
NILE_MCU_NATIVE_MODE_CMD 

Native command mode.

NILE_MCU_NATIVE_MODE_EEPROM 

EEPROM emulation mode.

NILE_MCU_NATIVE_MODE_RTC 

RTC emulation mode.

NILE_MCU_NATIVE_MODE_CDC 

CDC output-only mode.

NILE_MCU_NATIVE_MODE_STANDBY 

MCU standby mode - will not respond to further SPI messages until reset.

Definition at line 194 of file mcu.h.

Function Documentation

◆ nile_mcu_boot_erase_all_memory()

bool nile_mcu_boot_erase_all_memory ( void )
inlinestatic

Erase all of the MCU's flash memory.

Definition at line 125 of file mcu.h.

◆ nile_mcu_boot_erase_memory()

bool nile_mcu_boot_erase_memory ( uint16_t sector_address,
uint16_t sector_count )

Erase pages of the MCU's flash memory.

Parameters
sector_addressThe starting page to erase.
sector_countThe number of pages to erase.
See also
NILE_MCU_FLASH_PAGE_SIZE

◆ nile_mcu_boot_get_id()

uint16_t nile_mcu_boot_get_id ( void )

Get the chip ID. More information is available in the Application Note AN4286.

◆ nile_mcu_boot_get_version()

uint8_t nile_mcu_boot_get_version ( void )

Get the version of the SPI protocol used. More information is available in the Application Note AN4286.

◆ nile_mcu_boot_jump()

bool nile_mcu_boot_jump ( uint32_t address)

Request that the MCU branch to a specific address in memory.

Parameters
addressAddress to branch to.

◆ nile_mcu_boot_read_memory()

bool nile_mcu_boot_read_memory ( uint32_t address,
void __far * buffer,
uint16_t buflen )

Request bytes from the MCU's address space.

Parameters
addressMCU address to read data from.
bufferBuffer to read data to.
buflenAmount of data to read, in bytes (1 - 256).

◆ nile_mcu_boot_recv_data()

uint16_t nile_mcu_boot_recv_data ( void __far * buffer,
uint16_t buflen,
uint8_t flags )

◆ nile_mcu_boot_send_cmd()

bool nile_mcu_boot_send_cmd ( uint8_t cmd)

◆ nile_mcu_boot_send_data()

bool nile_mcu_boot_send_data ( const void __far * buffer,
uint16_t len,
uint8_t flags )

◆ nile_mcu_boot_wait_ack()

bool nile_mcu_boot_wait_ack ( void )

◆ nile_mcu_boot_write_memory()

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).

Parameters
addressMCU address to write data to.
bufferBuffer to write data from.
buflenAmount of data to write, in bytes (1 - 256).

◆ nile_mcu_native_cdc_available_async_start()

int16_t nile_mcu_native_cdc_available_async_start ( const void __wf_cram * buffer,
uint16_t buflen )
inlinestatic

Definition at line 255 of file mcu.h.

◆ nile_mcu_native_cdc_available_sync()

int16_t nile_mcu_native_cdc_available_sync ( void )
inlinestatic

Definition at line 248 of file mcu.h.

◆ nile_mcu_native_cdc_clear_sync()

int16_t nile_mcu_native_cdc_clear_sync ( void )
inlinestatic

Definition at line 262 of file mcu.h.

◆ nile_mcu_native_cdc_read_sync()

int16_t nile_mcu_native_cdc_read_sync ( void __far * buffer,
uint16_t buflen )
inlinestatic

Definition at line 228 of file mcu.h.

◆ nile_mcu_native_cdc_write_async_start()

int16_t nile_mcu_native_cdc_write_async_start ( const void __wf_cram * buffer,
uint16_t buflen )
inlinestatic

Definition at line 241 of file mcu.h.

◆ nile_mcu_native_cdc_write_sync()

int16_t nile_mcu_native_cdc_write_sync ( const void __wf_cram * buffer,
uint16_t buflen )
inlinestatic

Definition at line 234 of file mcu.h.

◆ nile_mcu_native_mcu_get_uuid_sync()

int16_t nile_mcu_native_mcu_get_uuid_sync ( void __far * buffer,
uint16_t buflen )
inlinestatic

Definition at line 222 of file mcu.h.

◆ nile_mcu_native_mcu_spi_set_speed_sync()

int16_t nile_mcu_native_mcu_spi_set_speed_sync ( uint8_t speed)
inlinestatic

Tell the MCU to operate at a specific SPI speed.

This does not actually change the speed used by the cartridge by itself!

Definition at line 214 of file mcu.h.

◆ nile_mcu_native_mcu_switch_mode()

int16_t nile_mcu_native_mcu_switch_mode ( uint8_t mode)
inlinestatic

Switch the mode in which the MCU is operating.

Definition at line 205 of file mcu.h.

◆ nile_mcu_native_recv_cmd()

int16_t nile_mcu_native_recv_cmd ( void __far * buffer,
uint16_t buflen )

Receive the response of a "native protocol" MCU command synchronously.

If the response size exceeds the size of the buffer, the remaining bytes are consumed and skipped.

Parameters
bufferBuffer to receive response to.
buflenThe size of the buffer.
Returns
int16_t The number of bytes received.

◆ nile_mcu_native_recv_cmd_finish()

int16_t nile_mcu_native_recv_cmd_finish ( void __far * buffer,
uint16_t buflen )

Finish receiving the response of a "native protocol" MCU command.

Parameters
bufferBuffer to copy response to.
buflenThe size of the buffer.
Returns
int16_t The number of bytes received.

◆ nile_mcu_native_recv_cmd_response_int16()

int16_t nile_mcu_native_recv_cmd_response_int16 ( void )
inlinestatic

Definition at line 188 of file mcu.h.

◆ nile_mcu_native_recv_cmd_response_uint8()

int16_t nile_mcu_native_recv_cmd_response_uint8 ( void )
inlinestatic

Definition at line 182 of file mcu.h.

◆ nile_mcu_native_recv_cmd_start()

int16_t nile_mcu_native_recv_cmd_start ( uint16_t resplen)

Start receiving the response of a "native protocol" MCU command asynchronously.

Parameters
resplenThe maximum size of the response.
Returns
int16_t 0 on success, or error code on failure.

◆ nile_mcu_native_send_cmd()

int16_t nile_mcu_native_send_cmd ( uint16_t cmd,
const void __far * buffer,
int buflen )
inlinestatic

Send a "native protocol" MCU command asynchronously.

Parameters
cmdCommand.
bufferOptional parameter buffer.
buflenSize of the parameter buffer (0 - 512 bytes).
Returns
int16_t 0 on success, or error code on failure.
See also
NILE_MCU_NATIVE_CMD

Definition at line 148 of file mcu.h.

◆ nile_mcu_reset()

bool nile_mcu_reset ( bool to_bootloader)

Reset the MCU.

Parameters
to_bootloaderIf true, the MCU is restarted into bootloader mode, allowing use of the nile_mcu_boot functions. If false, the MCU is restarted into the existing firmware flashed on it.
Returns
true Reset successful.
false Reset failed.