IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: Jaký parametr zadat při volání funkce

V předchozím kvízu, Online test znalostí C++, jsme si ověřili nabyté zkušenosti z kurzu.

Aktivity
Avatar
Caster
Člen
Avatar
Caster:1.3.2020 2:20

Pokouším se vyzkoušet načtení hodnoty z GPS chipu (u-blox ZOE-M8Q) pomocí jednočipového procesoru ATtiny202 a sběrnice I2C. Při překladu mi to hází chybu:

warning: implicit declaration of function 'I2C0_read_han­dler'

Zkusil jsem: main.c

#include <atmel_start.h>
#include <i2c_types.h>
#include <i2c_simple_master.h>
#include <utils/atomic.h>

#define slave_adr 0x42
#define slave_reg_adr 0xff

int main(void) {
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();

    /** Structure passed into read_handler to describe the actions to be performed by the handler */
    struct transfer_descriptor_t *I2C_0;
    uint8_t read_data[2];


    /** This callback is called when the initial write of the pointer register has finished.
    This callback controls the second phase of the I2C transaction, the read of the
    targeted register after a REPEATED START.
     */
    I2C_0_read_handler(&I2C_0_buf_t);

i2c_master_exam­ple.c

/** Structure passed into read_handler to describe the actions to be performed by the handler */
typedef struct {
        uint8_t *data;
        uint8_t  size;
} transfer_descriptor_t;

/** This callback is called when the initial write of the pointer register has finished.
    This callback controls the second phase of the I2C transaction, the read of the
    targeted register after a REPEATED START.
*/
i2c_operations_t I2C_0_read_handler(void *d)
{
        transfer_descriptor_t *desc = (transfer_descriptor_t *)d;
        I2C_0_set_buffer((void *)desc->data, desc->size);
        // Set callback to terminate transfer and send STOP after read is complete
        I2C_0_set_data_complete_callback(i2c_cb_return_stop, NULL);
        return i2c_restart_read; // Send REPEATED START before read
}

Chci docílit: Co zadat jako parametr funkce I2C0_read_han­dler(), případně zda jsem správně zadal I2C0_read_han­dler(&I2C0_buf_t); i když to nehází chybu.

 
Odpovědět
1.3.2020 2:20
Avatar
Ondřej Šrytr:1.3.2020 10:19

Podle varování 'implicit declaration of function ' to vypadá, že funkci I2C0_read_han­dler() jsi deklaroval později, než je main(). Takže řešením by mělo být před funkci main() napsat:

i2c_operations_t I2C_0_read_handler(void *d);
 
Nahoru Odpovědět
1.3.2020 10:19
Avatar
Caster
Člen
Avatar
Odpovídá na Ondřej Šrytr
Caster:1.3.2020 13:12

Díky, nějak jsem se do toho zamotal. Když s tím člověk nedělá každý den, tak je to fakt na hlavu. Po 5ti hodinách jsem pak konečně přišel, jak na to. Překlad v pohodě, jen to vyzkouším, zda to něco dělá.

Main.c

#include <atmel_start.h>
#include <i2c_types.h>
#include <i2c_simple_master.h>
#include <utils/atomic.h>
#include <i2c_master_example.h>

#define slave_adr 0x42
#define slave_reg_adr 0xff

uint8_t write_data [8] = {0xB5, 0x6, 0xF0, 0x01, 0x00, 0x00, 0xF1, 0xC3};
uint8_t read_data[1];

/** Structure passed into read_handler to describe the actions to be performed by the handler */
typedef struct {
    uint8_t *data;
    uint8_t size;
} transfer_descriptor_t;

/** This callback is called when the initial write of the pointer register has finished.
    This callback controls the second phase of the I2C transaction, the read of the
    targeted register after a REPEATED START.
 */
i2c_operations_t read_handler(void *d) {
    transfer_descriptor_t *desc = (transfer_descriptor_t *) d;
    I2C_0_set_buffer((void *) desc->data, desc->size);
    // Set callback to terminate transfer and send STOP after read is complete
    I2C_0_set_data_complete_callback(i2c_cb_return_stop, NULL);
    return i2c_restart_read; // Send REPEATED START before read
}

/** Performs the following transfer sequence:
1. Send SLA+W, Data1
2. Send RepeatedStart, SLA+R, Read Data1, Read Data2
3. Send Stop

This transfer sequence is typically done to first write to the slave the address in
the slave to read from, thereafter to read N bytes from this address.
 */
void do_transfer(uint8_t adr, uint8_t *data, uint8_t size) {
    transfer_descriptor_t d = {data, size};
    while (!I2C_0_open(slave_adr))
        ; // sit here until we get the bus..
    // This callback specifies what to do after the first write operation has completed
    // The parameters to the callback are bundled together in the aggregate data type d.
    I2C_0_set_data_complete_callback((void *) read_handler, &d);
    // If we get an address NACK, then try again by sending SLA+W
    I2C_0_set_address_nack_callback((void *) i2c_cb_restart_write, NULL);
    // Transmit one byte
    I2C_0_set_buffer((void *) &adr, 1);
    // Start a Write operation
    I2C_0_master_operation(false);
    while (I2C_BUSY == I2C_0_close())
        ; // sit here until the entire chained operation has finished
}

int main(void) {
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();

    //    asm("sei");

    do_transfer(slave_reg_adr, read_data, 1);

    asm("nop");

    /* Replace with your application code */
    while (1) {
    }
}
Akceptované řešení
+5 Zkušeností
Řešení problému
 
Nahoru Odpovědět
1.3.2020 13:12
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 3 zpráv z 3.