找回密码
 立即注册

使用微信账号登录

只需一步,快速开始

查看: 1538|回复: 5

[CSR8系列] csr867x i2c和uart参考代码(来源文档)

[复制链接]
连续签到天数:1天
签到总天数:75天
签到总奖励:2866金币
发表于 2021-10-2 10:30:41 | 显示全部楼层 |阅读模式

马上注册,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册  

×
I2C
  1. #include <message.h>
  2. #include <stdlib.h>
  3. #include <i2c.h>
  4. #include <panic.h>
  5. #define MESSAGE_TX_DATA 0x4000
  6. #define MESSAGE_RX_DATA 0x4001
  7. #define I2C_DEVICE_ADDRESS 0x22
  8. #define I2C_A_DEVICE_REGISTER 0x10
  9. typedef struct
  10. {
  11.     TaskData task;
  12. } I2CTaskData;
  13. I2CTaskData theI2CTaskData;
  14. uint16 i2c_register_data;
  15. static void I2CMessageHandler(Task pTask, MessageId pId, Message pMessage);
  16. static void i2c_example_init(void);
  17. static void i2c_write_16bit_device_register(uint8 device_address, uint8 device_register, uint16 data);
  18. static void i2c_read_16bit_device_register(uint8 device_address, uint8 device_register, uint16 *data);
  19. int main(void)
  20. {
  21.     i2c_example_init();

  22.     /* Start the message scheduler loop */
  23.     MessageLoop();

  24.     /* Never get here...*/
  25.     return 0;
  26. }
  27. void i2c_example_init(void)
  28. {
  29.     /* Assign task message handler */
  30.     theI2CTaskData.task.handler = I2CMessageHandler;
  31.     /* Send MESSAGE_TX_DATA to the task */
  32.     MessageSendLater(&theI2CTaskData.task, MESSAGE_TX_DATA, 0, 1000);
  33.     /* Send MESSAGE_RX_DATA to the task */
  34.     MessageSendLater(&theI2CTaskData.task, MESSAGE_RX_DATA, 0, 2000);
  35. }
  36. void i2c_write_16bit_device_register(uint8 device_address, uint8 device_register, uint16 data)
  37. {
  38.     uint8 i2c_data[3];
  39.     i2c_data[0] = device_register;
  40.     i2c_data[1] = (data >> 8) & 0xFF;
  41.     i2c_data[2] = data & 0xFF;
  42.     /*
  43. I2cTransfer(uint16 address, const uint8 *tx, uint16 tx_len, uint8 *rx, uint16 rx_len)
  44. If tx_len is non-zero and rx_len is zero then the sequence reduces to:
  45. - Start condition (S).
  46. - Write slave address and direction byte (address | 0).
  47. - Write tx_len data bytes from the buffer at tx.
  48. - Stop condition (P).
  49. */
  50.     PanicZero(I2cTransfer(device_address, i2c_data, 3, NULL, 0));
  51. }
  52. void i2c_read_16bit_device_register(uint8 device_address, uint8 device_register, uint16 *data)
  53. {
  54.     uint8 i2c_data[1];
  55.     uint8 i2c_rx_data[2];
  56.     i2c_data[0] = device_register;
  57.     /*
  58. I2cTransfer(uint16 address, const uint8 *tx, uint16 tx_len, uint8 *rx, uint16 rx_len)
  59. If tx_len is non-zero and rx_len is non-zero then the sequence is:
  60. - Start condition (S).
  61. - Write slave address and direction byte (address | 0).
  62. - Write tx_len data bytes from the buffer at tx.
  63. - Repeated start condition (Sr).
  64. - Write slave address and direction byte (address | 1).
  65. - Read rx_len bytes into the buffer at rx, acknowledging all but the final byte.
  66. - Stop condition (P).
  67. */
  68.     PanicZero(I2cTransfer(device_address, i2c_data, 1, i2c_rx_data, 2));
  69.     *data = (i2c_rx_data[0] << 8) + i2c_rx_data[1];
  70. }
  71. void I2CMessageHandler(Task pTask, MessageId pId, Message pMessage)
  72. {
  73.     switch (pId)
  74.     {
  75.     case MESSAGE_TX_DATA:
  76.         i2c_write_16bit_device_register(I2C_DEVICE_ADDRESS, I2C_A_DEVICE_REGISTER,
  77.                                         0x1234);
  78.         /* Send MESSAGE_TX_DATA to the task */
  79.         MessageSendLater(&theI2CTaskData.task, MESSAGE_TX_DATA, 0, 1000);
  80.         break;
  81.     case MESSAGE_RX_DATA:
  82.         i2c_read_16bit_device_register(I2C_DEVICE_ADDRESS, I2C_A_DEVICE_REGISTER,
  83.                                        &i2c_register_data);
  84.         /* Send MESSAGE_RX_DATA to the task */
  85.         MessageSendLater(&theI2CTaskData.task, MESSAGE_RX_DATA, 0, 2000);
  86.         break;
  87.     default:
  88.         break;
  89.     }
  90. }
复制代码


楼主热帖
积分商城 - 让您的金币更有价值!||官方Q群 - 让您的沟通更加及时!
连续签到天数:1天
签到总天数:75天
签到总奖励:2866金币
 楼主| 发表于 2021-10-2 10:31:42 | 显示全部楼层
UART (managed connection)
  1. #include <stream.h>
  2. #include <sink.h>
  3. #include <source.h>
  4. #include <string.h>
  5. #include <panic.h>
  6. #include <message.h>
  7. #include <app/uart/uart_if.h>
  8. typedef struct
  9. {
  10.     TaskData task;
  11.     Sink uart_sink;
  12.     Source uart_source;
  13. } UARTStreamTaskData;
  14. UARTStreamTaskData theUARTStreamTask;
  15. static void UARTStreamMessageHandler(Task pTask, MessageId pId, Message pMessage);
  16. static void uart_data_stream_rx_data(Source src);
  17. static void uart_data_stream_tx_data(const uint8 *data, uint16 length);
  18. static void uart_data_stream_init(void);
  19. int main(void)
  20. {
  21.     uart_data_stream_init();

  22.     /* Start the message scheduler loop */
  23.     MessageLoop();

  24.     /* Never get here...*/
  25.     return 0;
  26. }
  27. void uart_data_stream_init(void)
  28. {
  29.     /* Assign task message handler */
  30.     theUARTStreamTask.task.handler = UARTStreamMessageHandler;
  31.     /* Configure uart settings */
  32.     StreamUartConfigure(VM_UART_RATE_38K4, VM_UART_STOP_ONE, VM_UART_PARITY_NONE);
  33.     /* Get the sink for the uart */
  34.     theUARTStreamTask.uart_sink = StreamUartSink();
  35.     PanicNull(theUARTStreamTask.uart_sink);
  36.     /* Get the source for the uart */
  37.     theUARTStreamTask.uart_source = StreamUartSource();
  38.     PanicNull(theUARTStreamTask.uart_source);
  39.     /* Register uart source with task */
  40.     MessageSinkTask(StreamSinkFromSource(theUARTStreamTask.uart_source),
  41.                     &theUARTStreamTask.task);
  42. }
  43. void uart_data_stream_tx_data(const uint8 *data, uint16 length)
  44. {
  45.     uint16 offset = 0;
  46.     uint8 *dest = NULL;
  47.     /* Claim space in the sink, getting the offset to it */
  48.     offset = SinkClaim(theUARTStreamTask.uart_sink, length);
  49.     if (offset == 0xFFFF)
  50.         Panic();
  51.     /* Map the sink into memory space */
  52.     dest = SinkMap(theUARTStreamTask.uart_sink);
  53.     PanicNull(dest);
  54.     /* Copy data into the claimed space */
  55.     memcpy(dest + offset, data, length);
  56.     /* Flush the data out to the uart */
  57.     PanicZero(SinkFlush(theUARTStreamTask.uart_sink, length));
  58. }
  59. void uart_data_stream_rx_data(Source src)
  60. {
  61.     uint16 length = 0;
  62.     const uint8 *data = NULL;
  63.     /* Get the number of bytes in the specified source before the next packet  boundary */
  64.     if (!(length = SourceBoundary(src)))
  65.         return;
  66.     /* Maps the specified source into the address map */
  67.     data = SourceMap(src);
  68.     PanicNull((void *)data);
  69.     /* Transmit the received data */
  70.     uart_data_stream_tx_data(data, length);
  71.     /* Discards the specified amount of bytes from the front of the specified  source */
  72.     SourceDrop(src, length);
  73. }
  74. void UARTStreamMessageHandler(Task pTask, MessageId pId, Message pMessage)
  75. {
  76.     switch (pId)
  77.     {
  78.     case MESSAGE_MORE_DATA:
  79.         uart_data_stream_rx_data(((MessageMoreData *)pMessage)->source);
  80.         break;
  81.     default:
  82.         break;
  83.     }
  84. }
复制代码


积分商城 - 让您的金币更有价值!||官方Q群 - 让您的沟通更加及时!
回复 支持 反对

使用道具 举报

连续签到天数:1天
签到总天数:75天
签到总奖励:2866金币
 楼主| 发表于 2021-10-2 10:32:15 | 显示全部楼层
UART (Direct connection)
  1. #include <stream.h>
  2. #include <sink.h>
  3. #include <source.h>
  4. #include <string.h>
  5. #include <panic.h>
  6. #include <message.h>
  7. #include <stdlib.h>
  8. #include <app/uart/uart_if.h>
  9. #define MESSAGE_SEND_STRING 0x4000
  10. typedef struct
  11. {
  12.     TaskData task;
  13.     Sink uart_sink;
  14.     Source region_source;
  15.     const uint8 *region_data;
  16. } UARTAndRegionStreamTaskData;
  17. UARTAndRegionStreamTaskData theUARTAndRegionStreamTask;
  18. const uint8 example_text[] = "Hello World";
  19. static void UARTAndRegionStreamMessageHandler(Task pTask, MessageId pId, Message pMessage);
  20. static void uart_and_region_data_stream_tx_data(const uint8 *data, uint16 length);
  21. static void uart_and_region_data_stream_init(void);
  22. int main(void)
  23. {
  24.     uart_and_region_data_stream_init();

  25.     /* Start the message scheduler loop */
  26.     MessageLoop();

  27.     /* Never get here...*/
  28.     return 0;
  29. }
  30. void uart_and_region_data_stream_init(void)
  31. {
  32.     /* Assign task message handler */
  33.     theUARTAndRegionStreamTask.task.handler = UARTAndRegionStreamMessageHandler;
  34.     /* Configure uart settings */
  35.     StreamUartConfigure(VM_UART_RATE_38K4, VM_UART_STOP_ONE, VM_UART_PARITY_NONE);
  36.     /* Get the sink for the uart */
  37.     theUARTAndRegionStreamTask.uart_sink = StreamUartSink();
  38.     PanicNull(theUARTAndRegionStreamTask.uart_sink);
  39.     /* Send MESSAGE_SEND_STRING to the task */
  40.     MessageSendLater(&theUARTAndRegionStreamTask.task, MESSAGE_SEND_STRING, 0, 1000);
  41. }
  42. void uart_and_region_data_stream_tx_data(const uint8 *data, uint16 length)
  43. {
  44.     if (!length)
  45.         return;
  46.     /* Allocate memory for the region */
  47.     theUARTAndRegionStreamTask.region_data = malloc(length);
  48.     /* Region of memory to be treated as a source */
  49.     theUARTAndRegionStreamTask.region_source =
  50.         StreamRegionSource(theUARTAndRegionStreamTask.region_data, length);
  51.     PanicNull(theUARTAndRegionStreamTask.region_source);
  52.     /* Register region source with task */
  53.     MessageSinkTask(StreamSinkFromSource(theUARTAndRegionStreamTask.region_source),
  54.                     &theUARTAndRegionStreamTask.task);
  55.     /* Establishes an automatic connection between the region source and sink */
  56.     PanicZero(StreamConnect(theUARTAndRegionStreamTask.region_source,
  57.                             theUARTAndRegionStreamTask.uart_sink));
  58.     /* Copy data into the region */
  59.     memcpy((uint8 *)theUARTAndRegionStreamTask.region_data, data, length);
  60. }
  61. void UARTAndRegionStreamMessageHandler(Task pTask, MessageId pId, Message pMessage)
  62. {
  63.     switch (pId)
  64.     {
  65.     case MESSAGE_SOURCE_EMPTY:
  66.         /* Free the memory allocated for the region data */
  67.         /* The source created using StreamRegionSource() only exists  while the data is being read */
  68.         /* However, the memory block being treated as a source  is not freed by the stream subsystem */
  69.         if (theUARTAndRegionStreamTask.region_data)
  70.         {
  71.             free((uint8 *)theUARTAndRegionStreamTask.region_data);
  72.         }
  73.         /* Send MESSAGE_SEND_STRING to the task */
  74.         MessageSendLater(&theUARTAndRegionStreamTask.task, MESSAGE_SEND_STRING, 0, 1000);
  75.         break;
  76.     case MESSAGE_SEND_STRING:
  77.         uart_and_region_data_stream_tx_data(example_text, sizeof(example_text));
  78.         break;
  79.     default:
  80.         break;
  81.     }
  82. }
复制代码


积分商城 - 让您的金币更有价值!||官方Q群 - 让您的沟通更加及时!
回复 支持 反对

使用道具 举报

连续签到天数:1天
签到总天数:75天
签到总奖励:2866金币
 楼主| 发表于 2021-10-2 10:33:09 | 显示全部楼层
积分商城 - 让您的金币更有价值!||官方Q群 - 让您的沟通更加及时!
回复 支持 反对

使用道具 举报

连续签到天数:1天
签到总天数:55天
签到总奖励:1981金币
发表于 2021-10-4 15:55:20 | 显示全部楼层
支持分享。
积分商城 - 让您的金币更有价值!||官方Q群 - 让您的沟通更加及时!
回复 支持 反对

使用道具 举报

连续签到天数:1天
签到总天数:64天
签到总奖励:2015金币
发表于 2023-11-27 10:00:00 | 显示全部楼层
学习
积分商城 - 让您的金币更有价值!||官方Q群 - 让您的沟通更加及时!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册  

本版积分规则

小黑屋|手机版|我爱蓝牙网 - 52Bluetooth

GMT+8, 2024-3-29 08:25 , Processed in 0.161118 second(s), 19 queries , Gzip On, MemCached On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表