Advertisement
Guest User

MSP430 I2C Library

a guest
Jan 18th, 2015
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <msp430.h>
  3. #include "i2c.h"
  4.  
  5. volatile uint8_t *i2c_out_buffer;
  6. volatile uint16_t i2c_out_buffer_size;
  7. volatile uint8_t *i2c_in_buffer;
  8. volatile uint16_t i2c_in_buffer_size;
  9.  
  10. void i2c_init() {
  11.     P4SEL |= BIT1 | BIT2;
  12.     UCB1CTL1 = UCSWRST;
  13.     UCB1CTL0 = UCMST | UCSYNC | UCMODE_3;
  14.     UCB1CTL1 |= UCSSEL__SMCLK;
  15.     UCB1BR0 = 50; // 400 kHz
  16.     UCB1BR1 = 0;
  17.     UCB1CTL1 &= ~UCSWRST;
  18.     UCB1IE = UCNACKIE | UCTXIE | UCRXIE;
  19. }
  20.  
  21. int i2c_busy() {
  22.     return (UCB1STAT & UCBBUSY);
  23. }
  24.  
  25. void i2c_request(uint8_t addr, uint8_t *send_buffer, uint16_t send_buffer_size, uint8_t *recv_buffer, uint16_t recv_buffer_size, int wait) {
  26.     while (i2c_busy());
  27.     i2c_out_buffer = send_buffer;
  28.     i2c_out_buffer_size = send_buffer_size;
  29.     i2c_in_buffer = recv_buffer;
  30.     i2c_in_buffer_size = recv_buffer_size;
  31.     UCB1I2CSA = addr;
  32.     i2c_nack = 0;
  33.     if (send_buffer_size) {
  34.         UCB1CTL1 |= UCTR;
  35.     } else {
  36.         UCB1CTL1 &= ~UCTR;
  37.         if (i2c_in_buffer_size == 1) {
  38.             UCB1CTL1 |= UCTXNACK;
  39.         }
  40.     }
  41.     UCB1CTL1 |= UCTXSTT;
  42.     if (wait) {
  43.         while (i2c_out_buffer_size || i2c_in_buffer_size);
  44.     }
  45. }
  46.  
  47. void __attribute__((interrupt(USCI_B1_VECTOR))) USCI_B1_ISR() {
  48.     switch (UCB1IV) {
  49.         case 2: // Arbitration lost
  50.             break;
  51.         case 4: // No ACK
  52.             UCB1CTL1 |= UCTXSTP;
  53.             i2c_nack = 1;
  54.             break;
  55.         case 6: // Start received
  56.             break;
  57.         case 8: // Stop received
  58.             break;
  59.         case 12: // Data sent
  60.             if (i2c_out_buffer_size) {
  61.                 UCB1TXBUF = *i2c_out_buffer;
  62.                 i2c_out_buffer++;
  63.                 i2c_out_buffer_size--;
  64.             } else {
  65.                 if (i2c_in_buffer_size) {
  66.                     UCB1CTL1 &= ~UCTR;
  67.                     UCB1CTL1 |= UCTXSTT;
  68.                     if (i2c_in_buffer_size == 1) {
  69.                         UCB1CTL1 |= UCTXNACK;
  70.                     }
  71.                 } else {
  72.                     UCB1CTL1 |= UCTXSTP;
  73.                 }
  74.             }
  75.             break;
  76.         case 10: // Data received
  77.             *i2c_in_buffer = UCB1RXBUF;
  78.             i2c_in_buffer++;
  79.             i2c_in_buffer_size--;
  80.             if (i2c_in_buffer_size == 1) {
  81.                 UCB1CTL1 |= UCTXNACK;
  82.             } else if (i2c_in_buffer_size == 0) {
  83.                 UCB1CTL1 |= UCTXSTP;
  84.             }
  85.             break;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement