Hi,

I was trying to port the note-c
<https://github.com/blues/note-zephyr/tree/main> library to communicate
with Blues wireless notecard over I2C.
For that I created a package in the core repository and pasted all the
sources and headers. They are compelling when I build the code.
Then I wrote some MyNewt I2C API specific code. I took some inspiration
from the code of Zephyr-RTOS port. The code compiles fine but when I try to
communicate with notecard I get the following error.
The unhandled interrupt is from os_fault.c. But I do not understand why I
am getting this error.
I am using nRF52840-DK for my examples.

Please see the attached code for more information.

000065 [ERROR] notecard not responding
000130 [ERROR] notecard not responding
000195 [ERROR] notecard not responding
000260 [ERROR] notecard not responding
000325 [ERROR] notecard not responding
000390 [ERROR] notecard not responding
000455 [ERROR] notecard not responding
000520 [ERROR] notecard not responding
000585 [ERROR] notecard not responding
000650 [ERROR] notecard not responding
000773 Unhandled interrupt (3), exception sp 0x20001370
000773  r0:0x00000000  r1:0x00017948  r2:0x00017763  r3:0x20000258
000773  r4:0x77630000  r5:0x00300001  r6:0x12b8dead  r7:0xbeef2000
000773  r8:0x12c0dead  r9:0x01062000 r10:0x12400a80 r11:0x77602000
000773 r12:0xa0000000  lr:0x00012505  pc:0x1a000000 psr:0x61000000
000773 ICSR:0x00421803 HFSR:0x40000000 CFSR:0x00000100
000773 BFAR:0xe000ed38 MMFAR:0xe000ed34

Regards,
Ujjval
#include "note_c_hooks.h"
#include "bsp/bsp.h"

#include "hal/hal_i2c.h"

#include "os/os_time.h"

#include <console/console.h>

static const size_t REQUEST_HEADER_SIZE = 2;

const uint8_t i2c_num = 0;
bool i2c_initialized = false;


uint32_t platform_millis(void) {
    return (uint32_t)(os_get_uptime_usec()/1000);
}

void platform_delay(uint32_t ms) {
    os_time_delay((OS_TICKS_PER_SEC/1000) * ms);
}

const char *note_i2c_receive(uint16_t device_address_, uint8_t *buffer_, uint16_t size_, uint32_t *available_) {

     //const int request_length = size_ + REQUEST_HEADER_SIZE;
     //request_length
    uint8_t size_buf[2];
    size_buf[0] = 0;
    size_buf[1] = (uint8_t)size_;

    for(int i=0; i<=sizeof(size_buf); i++)
    {
        struct hal_i2c_master_data data = {
            .address = device_address_,
            .len = sizeof(size_buf[i]),
            .buffer = &size_buf[i]
        };
        uint8_t write_result = hal_i2c_master_write(i2c_num, &data, OS_TICKS_PER_SEC / 10, 0);

        if(write_result != 0)
        {
            return "i2c: unable to initiate read from the notecard\n";
        }
    }
    // Read from the Notecard and copy the response bytes into the response buffer
    const int request_length = size_ + REQUEST_HEADER_SIZE;
    uint8_t read_buf[256];

    for(int i=0; i<=request_length; i++){
        
        struct hal_i2c_master_data data = {
            .address = device_address_,
            .len = sizeof(read_buf[i]),
            .buffer = &read_buf[i]
        };
        
        if(i == sizeof(read_buf)){
            uint8_t read_result = hal_i2c_master_read(i2c_num, &data, OS_TICKS_PER_SEC / 10, 1);

            if (read_result != 0) {
                return "i2c: Unable to receive data from the Notecard.\n";
                }
            else 
            {
                *available_ = (uint32_t)read_buf[0];
                uint8_t bytes_to_read = read_buf[1];
                for (size_t i = 0; i < bytes_to_read; i++) 
                {
                    buffer_[i] = read_buf[i + 2];
                }
                return NULL;
            }
        }

        else
        {
            uint8_t read_result = hal_i2c_master_read(i2c_num, &data, OS_TICKS_PER_SEC / 10, 0);
            if (read_result != 0) 
            {
                return "i2c: Unable to receive data from the Notecard.\n";
            }
            else {
                *available_ = (uint32_t)read_buf[0];
                uint8_t bytes_to_read = read_buf[1];
                for (size_t i = 0; i < bytes_to_read; i++) 
                {
                    buffer_[i] = read_buf[i + 2];
                }
                return NULL;
                }
        }
    }
     return NULL;

}

bool note_i2c_reset(uint16_t device_address_){

    (void)device_address_;

    if(i2c_initialized){
        return true;
    }
    if(hal_i2c_enable(i2c_num) != 0){
        console_printf("i2c: Device not ready.\n");
        return false;
    }

    console_printf("i2c: Device is ready.\n");

    i2c_initialized = true;

    return true;
}

const char *note_i2c_transmit(uint16_t device_address_, uint8_t *buffer_, uint16_t size_) 
{

    uint8_t write_buf[size_ + 1];
    write_buf[0] = (uint8_t)size_;
    for (size_t i = 0; i < size_; i++) 
    {
        write_buf[i + 1] = buffer_[i];
    }
    
    for(int j=0; j<=sizeof(write_buf); j++)
    {
        struct hal_i2c_master_data data = 
        {
            .address = device_address_,
            .len = sizeof(write_buf[j]),
            .buffer = &write_buf[j]
        };

        if(j == sizeof(write_buf))
        {
            uint8_t write_result = hal_i2c_master_write(i2c_num, &data, OS_TICKS_PER_SEC / 5, 1);
            
            if (write_result != 0) 
            {
                return "i2c: Unable to transmit data to the Notecard\n";
            } 
            else
            {
                return NULL;
            }            
        }
        else
        {
            uint8_t write_result = hal_i2c_master_write(i2c_num, &data, OS_TICKS_PER_SEC / 5, 0);

            if (write_result != 0) 
            {
                return "i2c: Unable to transmit data to the Notecard\n";
            } 
            else 
            {
                return NULL;
            }
        }
    }
    return NULL;
}

size_t note_log_print(const char *message_) {
    
    if(message_){
        console_printf("%s", message_);
        return 1;        
    }

    return 0;

}
#ifndef NOTE_C_HOOKS_H
#define NOTE_C_HOOKS_H

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>


void platform_delay(uint32_t ms);
uint32_t platform_millis(void);

const char *note_i2c_receive(uint16_t device_address_, uint8_t *buffer_, uint16_t size_, uint32_t *available_);
bool note_i2c_reset(uint16_t device_address_);
const char *note_i2c_transmit(uint16_t device_address_, uint8_t *buffer_, uint16_t size_);

size_t note_log_print(const char *message_);

#endif // NOTE_C_HOOKS_H

Reply via email to