Skip to content

Pico-sdk library test #15

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

Open
lavara123 opened this issue May 20, 2025 · 0 comments
Open

Pico-sdk library test #15

lavara123 opened this issue May 20, 2025 · 0 comments

Comments

@lavara123
Copy link

lavara123 commented May 20, 2025

Header

#ifndef _NAU7802_H_
#define _NAU7802_H_

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

// I2C adresi
#define NAU7802_I2C_ADDRESS 0x2A

// NAU7802 kayıt adresleri ve sabitleri (orijinal .h dosyasından alınarak sadeleştirilecek)
#define NAU7802_PU_CTRL        0x00
#define NAU7802_CTRL1          0x01
#define NAU7802_CTRL2          0x02
#define NAU7802_ADC            0x12
#define NAU7802_REV_ID         0x1F

// Diğer sabitler ve maskeler de buraya eklenebilir

// NAU7802 yapı tanımı
typedef struct {
    uint8_t i2c_address;
    int32_t zero_offset;
    float calibration_factor;
    bool is_initialized;
    // Donanım platformuna özel I2C handler/ID, örneğin Raspberry Pi Pico için i2c_inst_t* i2c
    void* i2c_handle;
} NAU7802_t;

// Temel işlev prototipleri
bool nau7802_begin(NAU7802_t* dev, void* i2c_handle);
bool nau7802_available(NAU7802_t* dev);
int32_t nau7802_getReading(NAU7802_t* dev);
bool nau7802_calibrate(NAU7802_t* dev);
bool nau7802_setGain(NAU7802_t* dev, uint8_t gain);
bool nau7802_setLDO(NAU7802_t* dev, uint8_t ldo);
bool nau7802_setSampleRate(NAU7802_t* dev, uint8_t rate);
float nau7802_getWeight(NAU7802_t* dev, bool allowNegative);
void nau7802_setZeroOffset(NAU7802_t* dev, int32_t offset);
void nau7802_setCalibrationFactor(NAU7802_t* dev, float factor);

#endif // _NAU7802_H_

Source

/*
  SparkFun Qwiic Scale NAU7802 Library - C Port
  Converted from original C++ Arduino version
  

*/

#include "nau7802_c.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"

#define NAU7802_DEVICE_ADDRESS 0x2A

// I2C instance and address
static i2c_inst_t *i2c_port = i2c0;
static uint8_t device_address = NAU7802_DEVICE_ADDRESS;

// Internal config
static bool conversion_ready = false;

// Helper delay
static void delay_ms(uint32_t ms) {
    sleep_ms(ms);
}

// Low-level I2C register write
bool nau7802_write_register(uint8_t reg_address, uint8_t value) {
    uint8_t buffer[2] = {reg_address, value};
    int result = i2c_write_blocking(i2c_port, device_address, buffer, 2, false);
    return result == 2;
}

// Low-level I2C register read
bool nau7802_read_register(uint8_t reg_address, uint8_t *value) {
    int result = i2c_write_blocking(i2c_port, device_address, &reg_address, 1, true);
    if (result != 1) return false;
    result = i2c_read_blocking(i2c_port, device_address, value, 1, false);
    return result == 1;
}

// Bit mask utility
bool nau7802_set_bit(uint8_t reg, uint8_t bit_mask) {
    uint8_t value;
    if (!nau7802_read_register(reg, &value)) return false;
    value |= bit_mask;
    return nau7802_write_register(reg, value);
}

bool nau7802_clear_bit(uint8_t reg, uint8_t bit_mask) {
    uint8_t value;
    if (!nau7802_read_register(reg, &value)) return false;
    value &= ~bit_mask;
    return nau7802_write_register(reg, value);
}

bool nau7802_get_bit(uint8_t reg, uint8_t bit_mask) {
    uint8_t value;
    if (!nau7802_read_register(reg, &value)) return false;
    return (value & bit_mask) != 0;
}

bool nau7802_reset(void) {
    if (!nau7802_set_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_RR)) return false;
    delay_ms(1);
    return nau7802_clear_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_RR);
}

bool nau7802_power_up(void) {
    if (!nau7802_set_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_AVDDS)) return false;
    if (!nau7802_set_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_PUD)) return false;
    if (!nau7802_set_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_PUA)) return false;
    for (int i = 0; i < 100; i++) {
        bool ready;
        if (nau7802_get_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_PUR) &&
            nau7802_get_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_DRDY)) {
            return true;
        }
        delay_ms(1);
    }
    return false;
}

bool nau7802_begin(i2c_inst_t *i2c, uint8_t address) {
    i2c_port = i2c;
    device_address = address;

    if (!nau7802_reset()) return false;
    if (!nau7802_power_up()) return false;

    return true;
}

bool nau7802_calibrate_afe(void) {
    if (!nau7802_set_bit(NAU7802_CTRL2, NAU7802_CTRL2_CALMOD_INTERNAL)) return false;
    if (!nau7802_set_bit(NAU7802_CTRL2, NAU7802_CTRL2_CALS)) return false;
    for (int i = 0; i < 100; i++) {
        bool cal_done = !nau7802_get_bit(NAU7802_CTRL2, NAU7802_CTRL2_CALS);
        if (cal_done) return true;
        delay_ms(1);
    }
    return false;
}

bool nau7802_available(void) {
    return nau7802_get_bit(NAU7802_PU_CTRL, NAU7802_PU_CTRL_DRDY);
}

bool nau7802_read_adc(int32_t *result) {
    uint8_t buffer[3];
    uint8_t reg = NAU7802_ADCO_B2;
    if (i2c_write_blocking(i2c_port, device_address, &reg, 1, true) != 1) return false;
    if (i2c_read_blocking(i2c_port, device_address, buffer, 3, false) != 3) return false;

    int32_t value = ((int32_t)buffer[0] << 16) | ((int32_t)buffer[1] << 8) | buffer[2];
    if (value & 0x800000) value |= 0xFF000000; // Sign extend

    *result = value;
    return true;
}

Can you check and correct it and publish it for raspberry pi pico c language?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant