Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deviec init/read/write error message #588

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/err.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
#define ESTGFULL -142 /* Storage full */
#define ESTGNOKEY -143 /* Key not found */
#define ESTGSIZE -144 /* Data too long */
#define EDEVINIT -145 /* Device Init/Deinit Error */
#define EDEVREAD -146 /* Device Read Error */
#define EDEVWRITE -147 /* Device Write Error */

extern const char* errmsg[];

Expand Down
3 changes: 3 additions & 0 deletions src/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ const char* errmsg[] = {
"Storage full",
"Key not found",
"Data too long",
"Device Init/Deinit Error",
"Device Read Error",
"Device Write Error",
};

jerry_value_t create_system_error(const int errno) {
Expand Down
26 changes: 13 additions & 13 deletions targets/rp2/src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int km_i2c_setup_master(uint8_t bus, uint32_t speed, km_i2c_pins_t pins) {
i2c_inst_t *i2c = __get_i2c_no(bus);
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_NONE) ||
(__check_i2c_pins(bus, pins) == false)) {
return ENOPHRPL;
return EDEVINIT;
}
__i2c_status[bus].mode = KM_I2C_MASTER;
if (speed > I2C_MAX_CLOCK) {
Expand All @@ -129,7 +129,7 @@ int km_i2c_setup_slave(uint8_t bus, uint8_t address, km_i2c_pins_t pins) {
i2c_inst_t *i2c = __get_i2c_no(bus);
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_NONE) ||
(__check_i2c_pins(bus, pins) == false)) {
return ENOPHRPL;
return EDEVINIT;
}
__i2c_status[bus].mode = KM_I2C_SLAVE;
return 0;
Expand All @@ -142,7 +142,7 @@ int km_i2c_mem_write_master(uint8_t bus, uint8_t address, uint16_t mem_addr,
int ret;
uint8_t __memaddr[2];
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_MASTER)) {
return ENOPHRPL;
return EDEVWRITE;
}
if (mem_addr_size == 16) { // 16 bit mem address
__memaddr[0] = ((mem_addr >> 8) & 0xFF);
Expand All @@ -156,7 +156,7 @@ int km_i2c_mem_write_master(uint8_t bus, uint8_t address, uint16_t mem_addr,
ret = i2c_write_timeout_us(i2c, address, buf, len, false, timeout * 1000);
}
if (ret < 0) {
return -1;
return EDEVWRITE;
}
return ret;
}
Expand All @@ -168,7 +168,7 @@ int km_i2c_mem_read_master(uint8_t bus, uint8_t address, uint16_t mem_addr,
int ret;
uint8_t __memaddr[2];
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_MASTER)) {
return ENOPHRPL;
return EDEVREAD;
}
if (mem_addr_size == 16) { // 16 bit mem address
__memaddr[0] = ((mem_addr >> 8) & 0xFF);
Expand All @@ -182,7 +182,7 @@ int km_i2c_mem_read_master(uint8_t bus, uint8_t address, uint16_t mem_addr,
ret = i2c_read_timeout_us(i2c, address, buf, len, false, timeout * 1000);
}
if (ret < 0) {
return -1;
return EDEVREAD;
}
return ret;
}
Expand All @@ -192,11 +192,11 @@ int km_i2c_write_master(uint8_t bus, uint8_t address, uint8_t *buf, size_t len,
i2c_inst_t *i2c = __get_i2c_no(bus);
int ret;
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_MASTER)) {
return ENOPHRPL;
return EDEVWRITE;
}
ret = i2c_write_timeout_us(i2c, address, buf, len, false, timeout * 1000);
if (ret < 0) {
return -1;
return EDEVWRITE;
}
return ret;
}
Expand All @@ -205,7 +205,7 @@ int km_i2c_write_slave(uint8_t bus, uint8_t *buf, size_t len,
uint32_t timeout) {
i2c_inst_t *i2c = __get_i2c_no(bus);
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_SLAVE)) {
return ENOPHRPL;
return EDEVWRITE;
}
return 0;
}
Expand All @@ -215,27 +215,27 @@ int km_i2c_read_master(uint8_t bus, uint8_t address, uint8_t *buf, size_t len,
i2c_inst_t *i2c = __get_i2c_no(bus);
int ret;
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_MASTER)) {
return ENOPHRPL;
return EDEVREAD;
}
ret = i2c_read_timeout_us(i2c, address, buf, len, false, timeout * 1000);
if (ret < 0) {
return -1;
return EDEVREAD;
}
return ret;
}

int km_i2c_read_slave(uint8_t bus, uint8_t *buf, size_t len, uint32_t timeout) {
i2c_inst_t *i2c = __get_i2c_no(bus);
if ((i2c == NULL) || (__i2c_status[bus].mode != KM_I2C_SLAVE)) {
return ENOPHRPL;
return EDEVREAD;
}
return 0;
}

int km_i2c_close(uint8_t bus) {
i2c_inst_t *i2c = __get_i2c_no(bus);
if ((i2c == NULL) || (__i2c_status[bus].mode == KM_I2C_NONE)) {
return ENOPHRPL;
return EDEVINIT;
}
i2c_deinit(i2c);
__i2c_status[bus].mode = KM_I2C_NONE;
Expand Down
12 changes: 6 additions & 6 deletions targets/rp2/src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int km_spi_setup(uint8_t bus, km_spi_mode_t mode, uint32_t baudrate,
spi_inst_t *spi = __get_spi_no(bus);
if ((spi == NULL) || (__spi_status[bus].enabled) ||
(__check_spi_pins(bus, pins) == false)) {
return ENOPHRPL;
return EDEVINIT;
}
spi_cpol_t pol = SPI_CPOL_0;
spi_cpha_t pha = SPI_CPHA_0;
Expand Down Expand Up @@ -167,7 +167,7 @@ int km_spi_sendrecv(uint8_t bus, uint8_t *tx_buf, uint8_t *rx_buf, size_t len,
uint32_t timeout) {
spi_inst_t *spi = __get_spi_no(bus);
if ((spi == NULL) || (__spi_status[bus].enabled == false)) {
return ENOPHRPL;
return EDEVREAD;
}
(void)timeout; // timeout is not supported.
return spi_write_read_blocking(spi, tx_buf, rx_buf, len);
Expand All @@ -176,7 +176,7 @@ int km_spi_sendrecv(uint8_t bus, uint8_t *tx_buf, uint8_t *rx_buf, size_t len,
int km_spi_send(uint8_t bus, uint8_t *buf, size_t len, uint32_t timeout) {
spi_inst_t *spi = __get_spi_no(bus);
if ((spi == NULL) || (__spi_status[bus].enabled == false)) {
return ENOPHRPL;
return EDEVWRITE;
}
(void)timeout; // timeout is not supported.
return spi_write_blocking(spi, buf, len);
Expand All @@ -186,7 +186,7 @@ int km_spi_recv(uint8_t bus, uint8_t send_byte, uint8_t *buf, size_t len,
uint32_t timeout) {
spi_inst_t *spi = __get_spi_no(bus);
if ((spi == NULL) || (__spi_status[bus].enabled == false)) {
return ENOPHRPL;
return EDEVREAD;
}
(void)timeout; // timeout is not supported.
return spi_read_blocking(spi, send_byte, buf, len);
Expand All @@ -195,7 +195,7 @@ int km_spi_recv(uint8_t bus, uint8_t send_byte, uint8_t *buf, size_t len,
int km_set_spi_baudrate(uint8_t bus, uint32_t baudrate) {
spi_inst_t *spi = __get_spi_no(bus);
if ((spi == NULL) || (__spi_status[bus].enabled == false)) {
return ENOPHRPL;
return ENODEV;
}
spi_set_baudrate(spi, baudrate);
return 0;
Expand All @@ -204,7 +204,7 @@ int km_set_spi_baudrate(uint8_t bus, uint32_t baudrate) {
int km_spi_close(uint8_t bus) {
spi_inst_t *spi = __get_spi_no(bus);
if ((spi == NULL) || (__spi_status[bus].enabled == false)) {
return ENOPHRPL;
return EDEVINIT;
}
spi_deinit(spi);
__spi_status[bus].enabled = false;
Expand Down
10 changes: 5 additions & 5 deletions targets/rp2/src/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int km_uart_setup(uint8_t port, uint32_t baudrate, uint8_t bits,
if ((uart == NULL) || (__uart_status[port].enabled) || (bits < 5) ||
(bits > 8) ||
(__check_uart_pins(port, pins) == false)) { // Can't support 9 bit
return ENOPHRPL;
return EDEVINIT;
}
uart_init(uart, baudrate);
if ((flow & KM_UART_FLOW_RTS) && (pins.rts >= 0)) {
Expand All @@ -168,7 +168,7 @@ int km_uart_setup(uint8_t port, uint32_t baudrate, uint8_t bits,
uart_set_format(uart, bits, stop, pt);
__read_buffer[port] = (uint8_t *)malloc(buffer_size);
if (__read_buffer[port] == NULL) {
return ENOPHRPL;
return EDEVINIT;
} else {
ringbuffer_init(&__uart_rx_ringbuffer[port], __read_buffer[port],
buffer_size);
Expand All @@ -195,7 +195,7 @@ int km_uart_setup(uint8_t port, uint32_t baudrate, uint8_t bits,
int km_uart_write(uint8_t port, uint8_t *buf, size_t len) {
uart_inst_t *uart = __get_uart_no(port);
if ((uart == NULL) || (__uart_status[port].enabled == false)) {
return ENOPHRPL;
return EDEVWRITE;
}
uart_write_blocking(uart, (const uint8_t *)buf, len);
return len;
Expand All @@ -213,7 +213,7 @@ uint32_t km_uart_available(uint8_t port) {
uint32_t km_uart_read(uint8_t port, uint8_t *buf, size_t len) {
uart_inst_t *uart = __get_uart_no(port);
if ((uart == NULL) || (__uart_status[port].enabled == false)) {
return ENOPHRPL;
return EDEVREAD;
}
uint32_t n = ringbuffer_length(&__uart_rx_ringbuffer[port]);
if (n > len) {
Expand All @@ -226,7 +226,7 @@ uint32_t km_uart_read(uint8_t port, uint8_t *buf, size_t len) {
int km_uart_close(uint8_t port) {
uart_inst_t *uart = __get_uart_no(port);
if ((uart == NULL) || (__uart_status[port].enabled == false)) {
return ENOPHRPL;
return EDEVINIT;
}
if (__read_buffer[port]) {
free(__read_buffer[port]);
Expand Down