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.
Zobrazeno 3 zpráv z 3.
V předchozím kvízu, Online test znalostí C++, jsme si ověřili nabyté zkušenosti z kurzu.
Podle varování 'implicit declaration of function ' to vypadá, že funkci I2C0_read_handler() 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);
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) {
}
}
Zobrazeno 3 zpráv z 3.