365894126 发表于 2021-10-2 10:30:41

csr867x i2c和uart参考代码(来源文档)

I2C
#include <message.h>
#include <stdlib.h>
#include <i2c.h>
#include <panic.h>
#define MESSAGE_TX_DATA 0x4000
#define MESSAGE_RX_DATA 0x4001
#define I2C_DEVICE_ADDRESS 0x22
#define I2C_A_DEVICE_REGISTER 0x10
typedef struct
{
    TaskData task;
} I2CTaskData;
I2CTaskData theI2CTaskData;
uint16 i2c_register_data;
static void I2CMessageHandler(Task pTask, MessageId pId, Message pMessage);
static void i2c_example_init(void);
static void i2c_write_16bit_device_register(uint8 device_address, uint8 device_register, uint16 data);
static void i2c_read_16bit_device_register(uint8 device_address, uint8 device_register, uint16 *data);
int main(void)
{
    i2c_example_init();

    /* Start the message scheduler loop */
    MessageLoop();

    /* Never get here...*/
    return 0;
}
void i2c_example_init(void)
{
    /* Assign task message handler */
    theI2CTaskData.task.handler = I2CMessageHandler;
    /* Send MESSAGE_TX_DATA to the task */
    MessageSendLater(&theI2CTaskData.task, MESSAGE_TX_DATA, 0, 1000);
    /* Send MESSAGE_RX_DATA to the task */
    MessageSendLater(&theI2CTaskData.task, MESSAGE_RX_DATA, 0, 2000);
}
void i2c_write_16bit_device_register(uint8 device_address, uint8 device_register, uint16 data)
{
    uint8 i2c_data;
    i2c_data = device_register;
    i2c_data = (data >> 8) & 0xFF;
    i2c_data = data & 0xFF;
    /*
I2cTransfer(uint16 address, const uint8 *tx, uint16 tx_len, uint8 *rx, uint16 rx_len)
If tx_len is non-zero and rx_len is zero then the sequence reduces to:
- Start condition (S).
- Write slave address and direction byte (address | 0).
- Write tx_len data bytes from the buffer at tx.
- Stop condition (P).
*/
    PanicZero(I2cTransfer(device_address, i2c_data, 3, NULL, 0));
}
void i2c_read_16bit_device_register(uint8 device_address, uint8 device_register, uint16 *data)
{
    uint8 i2c_data;
    uint8 i2c_rx_data;
    i2c_data = device_register;
    /*
I2cTransfer(uint16 address, const uint8 *tx, uint16 tx_len, uint8 *rx, uint16 rx_len)
If tx_len is non-zero and rx_len is non-zero then the sequence is:
- Start condition (S).
- Write slave address and direction byte (address | 0).
- Write tx_len data bytes from the buffer at tx.
- Repeated start condition (Sr).
- Write slave address and direction byte (address | 1).
- Read rx_len bytes into the buffer at rx, acknowledging all but the final byte.
- Stop condition (P).
*/
    PanicZero(I2cTransfer(device_address, i2c_data, 1, i2c_rx_data, 2));
    *data = (i2c_rx_data << 8) + i2c_rx_data;
}
void I2CMessageHandler(Task pTask, MessageId pId, Message pMessage)
{
    switch (pId)
    {
    case MESSAGE_TX_DATA:
      i2c_write_16bit_device_register(I2C_DEVICE_ADDRESS, I2C_A_DEVICE_REGISTER,
                                        0x1234);
      /* Send MESSAGE_TX_DATA to the task */
      MessageSendLater(&theI2CTaskData.task, MESSAGE_TX_DATA, 0, 1000);
      break;
    case MESSAGE_RX_DATA:
      i2c_read_16bit_device_register(I2C_DEVICE_ADDRESS, I2C_A_DEVICE_REGISTER,
                                       &i2c_register_data);
      /* Send MESSAGE_RX_DATA to the task */
      MessageSendLater(&theI2CTaskData.task, MESSAGE_RX_DATA, 0, 2000);
      break;
    default:
      break;
    }
}

365894126 发表于 2021-10-2 10:31:42

UART (managed connection)
#include <stream.h>
#include <sink.h>
#include <source.h>
#include <string.h>
#include <panic.h>
#include <message.h>
#include <app/uart/uart_if.h>
typedef struct
{
    TaskData task;
    Sink uart_sink;
    Source uart_source;
} UARTStreamTaskData;
UARTStreamTaskData theUARTStreamTask;
static void UARTStreamMessageHandler(Task pTask, MessageId pId, Message pMessage);
static void uart_data_stream_rx_data(Source src);
static void uart_data_stream_tx_data(const uint8 *data, uint16 length);
static void uart_data_stream_init(void);
int main(void)
{
    uart_data_stream_init();

    /* Start the message scheduler loop */
    MessageLoop();

    /* Never get here...*/
    return 0;
}
void uart_data_stream_init(void)
{
    /* Assign task message handler */
    theUARTStreamTask.task.handler = UARTStreamMessageHandler;
    /* Configure uart settings */
    StreamUartConfigure(VM_UART_RATE_38K4, VM_UART_STOP_ONE, VM_UART_PARITY_NONE);
    /* Get the sink for the uart */
    theUARTStreamTask.uart_sink = StreamUartSink();
    PanicNull(theUARTStreamTask.uart_sink);
    /* Get the source for the uart */
    theUARTStreamTask.uart_source = StreamUartSource();
    PanicNull(theUARTStreamTask.uart_source);
    /* Register uart source with task */
    MessageSinkTask(StreamSinkFromSource(theUARTStreamTask.uart_source),
                  &theUARTStreamTask.task);
}
void uart_data_stream_tx_data(const uint8 *data, uint16 length)
{
    uint16 offset = 0;
    uint8 *dest = NULL;
    /* Claim space in the sink, getting the offset to it */
    offset = SinkClaim(theUARTStreamTask.uart_sink, length);
    if (offset == 0xFFFF)
      Panic();
    /* Map the sink into memory space */
    dest = SinkMap(theUARTStreamTask.uart_sink);
    PanicNull(dest);
    /* Copy data into the claimed space */
    memcpy(dest + offset, data, length);
    /* Flush the data out to the uart */
    PanicZero(SinkFlush(theUARTStreamTask.uart_sink, length));
}
void uart_data_stream_rx_data(Source src)
{
    uint16 length = 0;
    const uint8 *data = NULL;
    /* Get the number of bytes in the specified source before the next packetboundary */
    if (!(length = SourceBoundary(src)))
      return;
    /* Maps the specified source into the address map */
    data = SourceMap(src);
    PanicNull((void *)data);
    /* Transmit the received data */
    uart_data_stream_tx_data(data, length);
    /* Discards the specified amount of bytes from the front of the specifiedsource */
    SourceDrop(src, length);
}
void UARTStreamMessageHandler(Task pTask, MessageId pId, Message pMessage)
{
    switch (pId)
    {
    case MESSAGE_MORE_DATA:
      uart_data_stream_rx_data(((MessageMoreData *)pMessage)->source);
      break;
    default:
      break;
    }
}


365894126 发表于 2021-10-2 10:32:15

UART (Direct connection)
#include <stream.h>
#include <sink.h>
#include <source.h>
#include <string.h>
#include <panic.h>
#include <message.h>
#include <stdlib.h>
#include <app/uart/uart_if.h>
#define MESSAGE_SEND_STRING 0x4000
typedef struct
{
    TaskData task;
    Sink uart_sink;
    Source region_source;
    const uint8 *region_data;
} UARTAndRegionStreamTaskData;
UARTAndRegionStreamTaskData theUARTAndRegionStreamTask;
const uint8 example_text[] = "Hello World";
static void UARTAndRegionStreamMessageHandler(Task pTask, MessageId pId, Message pMessage);
static void uart_and_region_data_stream_tx_data(const uint8 *data, uint16 length);
static void uart_and_region_data_stream_init(void);
int main(void)
{
    uart_and_region_data_stream_init();

    /* Start the message scheduler loop */
    MessageLoop();

    /* Never get here...*/
    return 0;
}
void uart_and_region_data_stream_init(void)
{
    /* Assign task message handler */
    theUARTAndRegionStreamTask.task.handler = UARTAndRegionStreamMessageHandler;
    /* Configure uart settings */
    StreamUartConfigure(VM_UART_RATE_38K4, VM_UART_STOP_ONE, VM_UART_PARITY_NONE);
    /* Get the sink for the uart */
    theUARTAndRegionStreamTask.uart_sink = StreamUartSink();
    PanicNull(theUARTAndRegionStreamTask.uart_sink);
    /* Send MESSAGE_SEND_STRING to the task */
    MessageSendLater(&theUARTAndRegionStreamTask.task, MESSAGE_SEND_STRING, 0, 1000);
}
void uart_and_region_data_stream_tx_data(const uint8 *data, uint16 length)
{
    if (!length)
      return;
    /* Allocate memory for the region */
    theUARTAndRegionStreamTask.region_data = malloc(length);
    /* Region of memory to be treated as a source */
    theUARTAndRegionStreamTask.region_source =
      StreamRegionSource(theUARTAndRegionStreamTask.region_data, length);
    PanicNull(theUARTAndRegionStreamTask.region_source);
    /* Register region source with task */
    MessageSinkTask(StreamSinkFromSource(theUARTAndRegionStreamTask.region_source),
                  &theUARTAndRegionStreamTask.task);
    /* Establishes an automatic connection between the region source and sink */
    PanicZero(StreamConnect(theUARTAndRegionStreamTask.region_source,
                            theUARTAndRegionStreamTask.uart_sink));
    /* Copy data into the region */
    memcpy((uint8 *)theUARTAndRegionStreamTask.region_data, data, length);
}
void UARTAndRegionStreamMessageHandler(Task pTask, MessageId pId, Message pMessage)
{
    switch (pId)
    {
    case MESSAGE_SOURCE_EMPTY:
      /* Free the memory allocated for the region data */
      /* The source created using StreamRegionSource() only existswhile the data is being read */
      /* However, the memory block being treated as a sourceis not freed by the stream subsystem */
      if (theUARTAndRegionStreamTask.region_data)
      {
            free((uint8 *)theUARTAndRegionStreamTask.region_data);
      }
      /* Send MESSAGE_SEND_STRING to the task */
      MessageSendLater(&theUARTAndRegionStreamTask.task, MESSAGE_SEND_STRING, 0, 1000);
      break;
    case MESSAGE_SEND_STRING:
      uart_and_region_data_stream_tx_data(example_text, sizeof(example_text));
      break;
    default:
      break;
    }
}

365894126 发表于 2021-10-2 10:33:09

来源文档:
http://www.52bluetooth.com/forum.php?mod=viewthread&tid=5353

213465982 发表于 2021-10-4 15:55:20

支持分享。

深圳市 发表于 2023-11-27 10:00:00

学习

asfjin72 发表于 2025-4-4 14:54:47

thank you
页: [1]
查看完整版本: csr867x i2c和uart参考代码(来源文档)