libnile
Loading...
Searching...
No Matches
eeprom.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_EEPROM_H_
24#define NILE_MCU_EEPROM_H_
25
26#include "../mcu.h"
27
37
44 int16_t result;
45 bool retval = false;
46 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(NILE_MCU_NATIVE_CMD_EEPROM_MODE, mode), NULL, 0)) < 0) return result;
47 if ((result = nile_mcu_native_recv_cmd(&retval, 1)) <= 0) return result;
48 return retval;
49}
50
51static inline int16_t nile_mcu_native_eeprom_erase_sync(void) {
52 int16_t result;
53 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(NILE_MCU_NATIVE_CMD_EEPROM_ERASE, 0), NULL, 0)) < 0) return result;
54 return nile_mcu_native_recv_cmd(NULL, 0);
55}
56
63static inline int16_t nile_mcu_native_eeprom_read_sync(void __far* buffer, uint16_t offset, uint16_t length) {
64 int16_t result;
65 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(NILE_MCU_NATIVE_CMD_EEPROM_READ, length), &offset, 2)) < 0) return result;
66 return nile_mcu_native_recv_cmd(buffer, length << 1);
67}
68
75static inline int16_t nile_mcu_native_eeprom_write_sync(const void __wf_cram* buffer, uint16_t offset, uint16_t length) {
76 int16_t result;
77 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(NILE_MCU_NATIVE_CMD_EEPROM_WRITE, length), &offset, 2)) < 0) return result;
78 if (!nile_spi_tx_async_block(buffer, length << 1)) return NILE_MCU_NATIVE_ERROR_SPI;
79 if ((result = nile_mcu_native_recv_cmd(NULL, 0)) < 0) return result;
80 return 0;
81}
82
89static inline int16_t nile_mcu_native_eeprom_write_async_start(const void __wf_cram* buffer, uint16_t offset, uint16_t length) {
90 int16_t result;
91 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(NILE_MCU_NATIVE_CMD_EEPROM_WRITE, length), &offset, 2)) < 0) return result;
92 if (!nile_spi_tx_async_block(buffer, length << 1)) return NILE_MCU_NATIVE_ERROR_SPI;
94}
95#define nile_mcu_native_eeprom_write_async_finish nile_mcu_native_recv_cmd_response_none
96
102static inline int16_t nile_mcu_native_eeprom_get_mode_sync(void) {
103 int16_t result;
104 uint8_t mode;
105 if ((result = nile_mcu_native_send_cmd(NILE_MCU_NATIVE_CMD(NILE_MCU_NATIVE_CMD_EEPROM_GET_MODE, 0), NULL, 0)) < 0) return result;
106 if ((result = nile_mcu_native_recv_cmd(&mode, 1)) <= 0) return result;
107 return mode;
108}
109
110#endif /* NILE_MCU_EEPROM_H_ */
static int16_t nile_mcu_native_eeprom_write_async_start(const void __wf_cram *buffer, uint16_t offset, uint16_t length)
Definition eeprom.h:89
static int16_t nile_mcu_native_eeprom_read_sync(void __far *buffer, uint16_t offset, uint16_t length)
Definition eeprom.h:63
nile_mcu_eeprom_mode_t
Definition eeprom.h:28
@ NILE_MCU_EEPROM_MODE_NONE
Definition eeprom.h:29
@ NILE_MCU_EEPROM_MODE_M93LC76
Definition eeprom.h:34
@ NILE_MCU_EEPROM_MODE_M93LC46
Definition eeprom.h:31
@ NILE_MCU_EEPROM_MODE_M93LC86
Definition eeprom.h:35
@ NILE_MCU_EEPROM_MODE_M93LC66
Definition eeprom.h:33
@ NILE_MCU_EEPROM_MODE_M93LC56
Definition eeprom.h:32
@ NILE_MCU_EEPROM_MODE_M93LC06
Definition eeprom.h:30
static int16_t nile_mcu_native_eeprom_write_sync(const void __wf_cram *buffer, uint16_t offset, uint16_t length)
Definition eeprom.h:75
static int16_t nile_mcu_native_eeprom_set_mode_sync(nile_mcu_eeprom_mode_t mode)
Set EEPROM emulation mode.
Definition eeprom.h:43
static int16_t nile_mcu_native_eeprom_erase_sync(void)
Definition eeprom.h:51
static int16_t nile_mcu_native_eeprom_get_mode_sync(void)
Get EEPROM emulation mode.
Definition eeprom.h:102
int16_t nile_mcu_native_recv_cmd(void __far *buffer, uint16_t buflen)
Receive the response of a "native protocol" MCU command synchronously.
#define NILE_MCU_NATIVE_CMD(cmd, arg)
Definition mcu.h:56
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_send_cmd(uint16_t cmd, const void __far *buffer, int buflen)
Send a "native protocol" MCU command asynchronously.
Definition mcu.h:164
#define NILE_MCU_NATIVE_ERROR_SPI
SPI communication error.
Definition mcu.h:148
@ NILE_MCU_NATIVE_CMD_EEPROM_READ
Definition protocol.h:41
@ NILE_MCU_NATIVE_CMD_EEPROM_ERASE
Definition protocol.h:40
@ NILE_MCU_NATIVE_CMD_EEPROM_WRITE
Definition protocol.h:42
@ NILE_MCU_NATIVE_CMD_EEPROM_GET_MODE
Definition protocol.h:44
@ NILE_MCU_NATIVE_CMD_EEPROM_MODE
Definition protocol.h:39
bool nile_spi_tx_async_block(const void __far *buf, uint16_t size)
Asynchronously transfer block of data over the SPI interface.