MSPM0G3507应用之ADC数据通过I2C接口进行传输

[复制链接]
 楼主| xyz549040622 发表于 2023-12-24 23:01 | 显示全部楼层 |阅读模式

msedge_8YB3nDK1gW.png
msedge_fnrAJR7gBo.png
例程位于官方SDK下面,如下图所示:


msedge_gDNYSQcX0L.png

 楼主| xyz549040622 发表于 2023-12-24 23:01 | 显示全部楼层
  1. /*
  2. * Copyright (c) 2021, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * *  Redistributions of source code must retain the above copyright
  10. *    notice, this list of conditions and the following disclaimer.
  11. *
  12. * *  Redistributions in binary form must reproduce the above copyright
  13. *    notice, this list of conditions and the following disclaimer in the
  14. *    documentation and/or other materials provided with the distribution.
  15. *
  16. * *  Neither the name of Texas Instruments Incorporated nor the names of
  17. *    its contributors may be used to endorse or promote products derived
  18. *    from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */

  32. #include "ti_msp_dl_config.h"

  33. /* Maximum size of TX packet */
  34. #define I2C_TX_MAX_PACKET_SIZE (16)

  35. /* Maximum size of RX packet */
  36. #define I2C_RX_MAX_PACKET_SIZE (16)

  37. /* Commands for the ADC sampling */
  38. #define SAMPLE_ADC (0x01)
  39. #define SEND_RESULTS (0x02)
  40. #define SAMPLE_SEND_RESULTS (0x03)

  41. /* Data sent to Controller in response to Read transfer, default set to 0xAA */
  42. uint8_t gTxPacket[I2C_TX_MAX_PACKET_SIZE] = {0xAA};

  43. /* Counters for TX length and bytes sent */
  44. uint32_t gTxLen, gTxCount;

  45. /* Data received from Controller during a Write transfer */
  46. uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];

  47. /* Counters for TX length and bytes sent */
  48. uint32_t gRxLen, gRxCount, gCommandCount;

  49. /* Boolean to know when a stop command was issued */
  50. bool gStopReceived = false;

  51. /* ADC variables:
  52. * gADCResult is the adc result taken from the Memory Register
  53. * NumberOfADCSamples is the count of total ADC samples to keep track for the gTxResult buffer
  54. */

  55. uint16_t gADCResult;
  56. uint8_t gNumberOfADCSamples = 0;
  57. bool gCheckADC = false;

  58. /* Process Command will take the command from the I2C Rx and parse which command is to be issued */
  59. void processCommand(uint8_t cmd);

  60. /* ADC Process will start the ADC and separate the ADC result into 8 bits to place into the Tx Packet buffer. */
  61. void ADCProcess();

  62. int main(void)
  63. {
  64.     SYSCFG_DL_init();

  65.     /* Set LED to indicate start of transfer */
  66.     DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);

  67.    /* Reset variables for the Tx, these are used in the I2C ISR */
  68.     gTxCount = 0;
  69.     gTxLen   = 0;
  70.     DL_I2C_enableInterrupt(I2C_INST, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER);

  71.     /* Initialize variables to receive data inside RX ISR */
  72.     gRxCount = 0;
  73.     gRxLen   = I2C_RX_MAX_PACKET_SIZE;

  74.     NVIC_EnableIRQ(I2C_INST_INT_IRQN);
  75.     NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);


  76.     while (1)
  77.     {
  78.         /* Sleep until the stop command is given from the I2C Controller */
  79.         while(gStopReceived == false)
  80.             __WFE();
  81.         gStopReceived = false;

  82.         /* Process given commands */
  83.         for(uint16_t i = 0; i < gCommandCount; i++){
  84.             processCommand(gRxPacket[i]);
  85.         }
  86.         gCommandCount = 0;
  87.     }
  88. }

  89. /* Process the command given */
  90. void processCommand(uint8_t cmd){

  91.     switch(cmd){
  92.         /* Sample the ADC */
  93.         case SAMPLE_ADC:
  94.             ADCProcess();
  95.             break;
  96.         /* Add 2 to the Tx Length, this equates to one more full result added to the TX transmission */
  97.         case SEND_RESULTS:
  98.             gTxLen += 2;
  99.             break;
  100.         /* Sample the ADC and increment the Tx Length. */
  101.         case SAMPLE_SEND_RESULTS:
  102.             ADCProcess();
  103.             gTxLen += 2;
  104.             break;
  105.         default:
  106.             break;
  107.     }
  108. }

  109. /* Start the ADC process of sampling and storing into the TxPacket Buffer */
  110. void ADCProcess(){

  111.     /* Start the ADC Sampling */

  112.     DL_ADC12_startConversion(ADC12_0_INST);

  113.     /* Sleep until the ADC sampling and conversion is finished, then reset the boolean */
  114.     while(gCheckADC == false){
  115.         __WFI();
  116.     }
  117.     gCheckADC = false;

  118.     /* Get the ADC result and place the upper byte and lower byte into the TxPacket, upper byte first */
  119.     gADCResult = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0);
  120.     gTxPacket[gNumberOfADCSamples*2] = (uint8_t)((gADCResult >> 8) & 0xFF);
  121.     gTxPacket[gNumberOfADCSamples*2 + 1] = (uint8_t)(gADCResult & 0xFF);

  122.     /* Make sure the number of ADC samples does not go over the TxPacket size, if it does next sample will overwrite */
  123.     gNumberOfADCSamples++;
  124.     if(gNumberOfADCSamples*2 + 1 > I2C_TX_MAX_PACKET_SIZE)
  125.     {
  126.         gNumberOfADCSamples = 0;
  127.     }
  128.     DL_ADC12_enableConversions(ADC12_0_INST);

  129. }


  130. void ADC12_0_INST_IRQHandler(void) {
  131.   switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) {
  132.   case DL_ADC12_IIDX_MEM0_RESULT_LOADED:
  133.     gCheckADC = true;
  134.     break;
  135.   default:
  136.     break;
  137.   }
  138. }


  139. void I2C_INST_IRQHandler(void)
  140. {
  141.     static bool commandReceived = false;

  142.     switch (DL_I2C_getPendingInterrupt(I2C_INST)) {
  143.         case DL_I2C_IIDX_TARGET_START:
  144.             /* Initialize RX or TX after Start condition is received */
  145.             gTxCount = 0;
  146.             gRxCount = 0;
  147.             commandReceived   = false;
  148.             /* Flush TX FIFO to refill it */
  149.             DL_I2C_flushTargetTXFIFO(I2C_INST);
  150.             break;
  151.         case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  152.             /* Store received commands in buffer */
  153.             commandReceived = true;
  154.             while (DL_I2C_isTargetRXFIFOEmpty(I2C_INST) != true) {
  155.                 if (gRxCount < gRxLen) {
  156.                     gRxPacket[gRxCount++] = DL_I2C_receiveTargetData(I2C_INST);
  157.                 } else {
  158.                     /* Prevent overflow and just ignore data */
  159.                     DL_I2C_receiveTargetData(I2C_INST);
  160.                 }
  161.             }
  162.             break;
  163.         case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  164.             /* Fill TX FIFO with ADC Results */
  165.             if (gTxCount < gTxLen) {
  166.                 gTxCount += DL_I2C_fillTargetTXFIFO(
  167.                     I2C_INST, &gTxPacket[gTxCount], (gTxLen - gTxCount));
  168.             } else {
  169.                 /*
  170.                  * Fill FIFO with 0xAA if more data is requested than
  171.                  * expected gTxLen
  172.                  */
  173.                 while (DL_I2C_transmitTargetDataCheck(I2C_INST, 0xAA) != false)
  174.                     ;
  175.             }
  176.             break;
  177.         case DL_I2C_IIDX_TARGET_STOP:
  178.             /* If commands were received, set the command count to RxCount and reset fifo */
  179.             if (commandReceived == true) {
  180.                 gCommandCount = gRxCount;
  181.                 commandReceived = false;
  182.                 DL_I2C_flushTargetTXFIFO(I2C_INST);
  183.             }

  184.             /* Set Stop Received to true to allow the main loop to proceed */
  185.             gStopReceived = true;
  186.             /* Toggle LED to indicate successful RX or TX */
  187.             DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
  188.             break;
  189.         default:
  190.             break;
  191.     }
  192. }


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

本版积分规则

个人签名:qq群: 嵌入式系统arm初学者 224636155←← +→→点击-->小 i 精品课全集,21ic公开课~~←←→→点击-->小 i 精品课全集,给你全方位的技能策划~~←←

2841

主题

19330

帖子

110

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:qq群: 嵌入式系统arm初学者 224636155←← +→→点击-->小 i 精品课全集,21ic公开课~~←←→→点击-->小 i 精品课全集,给你全方位的技能策划~~←←

2841

主题

19330

帖子

110

粉丝
快速回复 在线客服 返回列表 返回顶部