PICO I2C FW

Dependencies:   USBDevice

Committer:
cyberjoey
Date:
Fri Feb 08 21:18:10 2019 +0000
Revision:
23:9bcf097cbfc2
Parent:
22:ee0d36d534cd
Child:
24:5ee9d36089f3
edited for simplicity

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:60a522ae2e35 1 #include "mbed.h"
cyberjoey 9:36ba3626aab7 2 #include "USBSerial.h"
cyberjoey 9:36ba3626aab7 3
cyberjoey 9:36ba3626aab7 4 #define BS 8 // ASCII Back Space
cyberjoey 9:36ba3626aab7 5 #define CR 13 // ASCII Carriage Return
cyberjoey 9:36ba3626aab7 6
cyberjoey 23:9bcf097cbfc2 7 // Virtual serial port over USB TODO NEW VID PID NEEDED!!
cyberjoey 15:61dce4bef44f 8 USBSerial pc(0x0B6A, 0x0042, 0x0001, false);
cyberjoey 9:36ba3626aab7 9
cyberjoey 9:36ba3626aab7 10 // I2C setup
cyberjoey 23:9bcf097cbfc2 11 I2C i2c(P1_6, P1_7); // P1_6 -> I2C1_SDA, P1_7-> I2C1_SCL
cyberjoey 9:36ba3626aab7 12
cyberjoey 9:36ba3626aab7 13 //Timer setup
cyberjoey 9:36ba3626aab7 14 Ticker timer_1; // timer for blinking led heartbeat
cyberjoey 9:36ba3626aab7 15 bool led_toggle_flag = false;
cyberjoey 9:36ba3626aab7 16
cyberjoey 9:36ba3626aab7 17 //LED blink setup
switches 1:90313362ec11 18 DigitalOut rLED(LED1);
switches 1:90313362ec11 19 DigitalOut gLED(LED2);
cyberjoey 9:36ba3626aab7 20 DigitalOut bLED(LED3);
switches 0:60a522ae2e35 21
cyberjoey 23:9bcf097cbfc2 22 //InterruptIn button(SW1);
cyberjoey 23:9bcf097cbfc2 23
cyberjoey 23:9bcf097cbfc2 24 void LED_blink_callback() { // LED Heart beat
cyberjoey 9:36ba3626aab7 25
cyberjoey 9:36ba3626aab7 26 led_toggle_flag = !led_toggle_flag;
cyberjoey 23:9bcf097cbfc2 27
cyberjoey 23:9bcf097cbfc2 28 if (led_toggle_flag)
cyberjoey 9:36ba3626aab7 29 {
cyberjoey 23:9bcf097cbfc2 30 //toggle teal leds
cyberjoey 9:36ba3626aab7 31 gLED = LED_ON;
cyberjoey 23:9bcf097cbfc2 32 bLED = LED_ON;
cyberjoey 9:36ba3626aab7 33 }
cyberjoey 9:36ba3626aab7 34 else
cyberjoey 9:36ba3626aab7 35 {
cyberjoey 23:9bcf097cbfc2 36 rLED = LED_OFF;
cyberjoey 23:9bcf097cbfc2 37 gLED = LED_OFF;
cyberjoey 23:9bcf097cbfc2 38 bLED = LED_OFF;
cyberjoey 9:36ba3626aab7 39 }
cyberjoey 23:9bcf097cbfc2 40
cyberjoey 9:36ba3626aab7 41 }
cyberjoey 9:36ba3626aab7 42
cyberjoey 9:36ba3626aab7 43 // *****************************************************************************
cyberjoey 22:ee0d36d534cd 44 // I2C_write_register(char, char, char) writes single byte to OT07
cyberjoey 17:401a5bb8d403 45 // char I2C address
cyberjoey 17:401a5bb8d403 46 // char OT07 register address
cyberjoey 17:401a5bb8d403 47 // char data byte to be writen
cyberjoey 17:401a5bb8d403 48 // returns 0 on success ACK, 1 on NACK
cyberjoey 17:401a5bb8d403 49 // *****************************************************************************
cyberjoey 17:401a5bb8d403 50
cyberjoey 23:9bcf097cbfc2 51 int I2C_write_register(char I2C_add, char reg_add, char byte) {
cyberjoey 17:401a5bb8d403 52 char data[2];
cyberjoey 17:401a5bb8d403 53 int error;
cyberjoey 17:401a5bb8d403 54 data[0] = reg_add;
cyberjoey 17:401a5bb8d403 55 data[1] = byte;
cyberjoey 23:9bcf097cbfc2 56 error = i2c.write(I2C_add, data, 2);
cyberjoey 23:9bcf097cbfc2 57 return error;
cyberjoey 23:9bcf097cbfc2 58
cyberjoey 17:401a5bb8d403 59 }
cyberjoey 17:401a5bb8d403 60
cyberjoey 17:401a5bb8d403 61 /// ****************************************************************************
cyberjoey 22:ee0d36d534cd 62 // I2C_write_register(char, char, char *, int) writes multiple bytes to OT07
cyberjoey 17:401a5bb8d403 63 // char I2C address
cyberjoey 17:401a5bb8d403 64 // char OT07 register address
cyberjoey 17:401a5bb8d403 65 // char * data vector of bytes to be written
cyberjoey 17:401a5bb8d403 66 // int number of bytes to write
cyberjoey 17:401a5bb8d403 67 // returns 0 on success ACK, 1 on NACK
cyberjoey 17:401a5bb8d403 68 // *****************************************************************************
cyberjoey 17:401a5bb8d403 69
cyberjoey 23:9bcf097cbfc2 70 int I2C_write_register(char I2C_add, char reg_add, char *bytes, int n) {
cyberjoey 23:9bcf097cbfc2 71 int i;
cyberjoey 17:401a5bb8d403 72 //set start address
cyberjoey 17:401a5bb8d403 73 char data[16];
cyberjoey 23:9bcf097cbfc2 74 int error;
cyberjoey 23:9bcf097cbfc2 75 data[0] = reg_add;
cyberjoey 23:9bcf097cbfc2 76 for (i = 1; i <= n; i++) {
cyberjoey 23:9bcf097cbfc2 77 data[i] = bytes[i - 1];
cyberjoey 17:401a5bb8d403 78 }
cyberjoey 23:9bcf097cbfc2 79 error = i2c.write(I2C_add, data, n + 1); // send n bytes of data
cyberjoey 23:9bcf097cbfc2 80
cyberjoey 23:9bcf097cbfc2 81 return error;
cyberjoey 17:401a5bb8d403 82 }
cyberjoey 17:401a5bb8d403 83
cyberjoey 17:401a5bb8d403 84 // *****************************************************************************
cyberjoey 22:ee0d36d534cd 85 // I2C_read_register(char, char, char *, int) writes single byte to OT07
cyberjoey 17:401a5bb8d403 86 // char I2C address
cyberjoey 17:401a5bb8d403 87 // char OT07 register address
cyberjoey 17:401a5bb8d403 88 // char * data vector for read bytes to be stored in
cyberjoey 17:401a5bb8d403 89 // int number of bytes to read
cyberjoey 17:401a5bb8d403 90 // returns 0 on success, 1 on fail
cyberjoey 17:401a5bb8d403 91 // *****************************************************************************
cyberjoey 17:401a5bb8d403 92
cyberjoey 23:9bcf097cbfc2 93 int I2C_read_register(char I2C_add, char reg_add, char *bytes, int n) {
cyberjoey 17:401a5bb8d403 94 int error;
cyberjoey 23:9bcf097cbfc2 95 error = i2c.write(I2C_add, &reg_add, 1, 1);
cyberjoey 23:9bcf097cbfc2 96 if (error)return error;
cyberjoey 23:9bcf097cbfc2 97 error = i2c.read(I2C_add, bytes, n);
cyberjoey 17:401a5bb8d403 98 //if(DEBUG)db.printf("rr e[%d]\r\n",error);
cyberjoey 23:9bcf097cbfc2 99 return error;
cyberjoey 23:9bcf097cbfc2 100 }
cyberjoey 23:9bcf097cbfc2 101
cyberjoey 23:9bcf097cbfc2 102
cyberjoey 23:9bcf097cbfc2 103 /// ****************************************************************************
cyberjoey 23:9bcf097cbfc2 104 // process_command(char *) writes multiple bytes to OT07
cyberjoey 23:9bcf097cbfc2 105 // char * receive buffer from virtual serial port
cyberjoey 23:9bcf097cbfc2 106 // returns 0 on processed command, 1 on invalid command
cyberjoey 23:9bcf097cbfc2 107 // *****************************************************************************
cyberjoey 23:9bcf097cbfc2 108 int process_command(char rx_buff[128])
cyberjoey 23:9bcf097cbfc2 109 {
cyberjoey 23:9bcf097cbfc2 110 char data[130];
cyberjoey 23:9bcf097cbfc2 111 int i;
cyberjoey 23:9bcf097cbfc2 112 int num_bytes;
cyberjoey 23:9bcf097cbfc2 113 char c;
cyberjoey 23:9bcf097cbfc2 114 int arg1 = 0;
cyberjoey 23:9bcf097cbfc2 115 int arg2 = 0;
cyberjoey 23:9bcf097cbfc2 116 int arg3 = 0;
cyberjoey 23:9bcf097cbfc2 117 int n = sscanf(rx_buff, " %c %x %x %x", &c, &arg1, &arg2, &arg3);
cyberjoey 23:9bcf097cbfc2 118 //process input
cyberjoey 23:9bcf097cbfc2 119 if (n > 0) {//got input so process it
cyberjoey 23:9bcf097cbfc2 120 switch (c) {
cyberjoey 23:9bcf097cbfc2 121 case 'r':
cyberjoey 23:9bcf097cbfc2 122 case 'R':
cyberjoey 23:9bcf097cbfc2 123 //read register "r slave.add radd.start radd.end"
cyberjoey 23:9bcf097cbfc2 124 if (n == 3) { //read single register from selected device
cyberjoey 23:9bcf097cbfc2 125 int error = I2C_read_register(arg1, arg2, data, 1);
cyberjoey 23:9bcf097cbfc2 126 if (error)
cyberjoey 23:9bcf097cbfc2 127 pc.printf("nack\r\n");
cyberjoey 23:9bcf097cbfc2 128 else
cyberjoey 23:9bcf097cbfc2 129 pc.printf("%02X\r\n", data[0]);
cyberjoey 23:9bcf097cbfc2 130 }
cyberjoey 23:9bcf097cbfc2 131 if (n == 4) { //read a range of regesters from selected device
cyberjoey 23:9bcf097cbfc2 132 num_bytes = arg3 - arg2 + 1;// calculate number of bytes to read
cyberjoey 23:9bcf097cbfc2 133 if (num_bytes < 1) num_bytes = 1;// if arg2 <= arg 1 just read arg1 address.
cyberjoey 23:9bcf097cbfc2 134 I2C_read_register(arg1, arg2, data, num_bytes);
cyberjoey 23:9bcf097cbfc2 135 for (i = 0; i<num_bytes; i++) {
cyberjoey 23:9bcf097cbfc2 136 pc.printf("%02X\r\n", data[i]);
cyberjoey 23:9bcf097cbfc2 137 }
cyberjoey 23:9bcf097cbfc2 138 }
cyberjoey 23:9bcf097cbfc2 139 break;
cyberjoey 23:9bcf097cbfc2 140 case 'w':
cyberjoey 23:9bcf097cbfc2 141 case 'W': //write register "w device w.addr data"
cyberjoey 23:9bcf097cbfc2 142 int error = I2C_write_register(arg1, arg2, arg3);
cyberjoey 23:9bcf097cbfc2 143 if (error)
cyberjoey 23:9bcf097cbfc2 144 pc.printf("nack\r\n");
cyberjoey 23:9bcf097cbfc2 145 else
cyberjoey 23:9bcf097cbfc2 146 pc.printf("ack\r\n");
cyberjoey 23:9bcf097cbfc2 147 break;
cyberjoey 23:9bcf097cbfc2 148 default:
cyberjoey 23:9bcf097cbfc2 149 return 1;
cyberjoey 23:9bcf097cbfc2 150 }//end switch(c)
cyberjoey 23:9bcf097cbfc2 151 }
cyberjoey 23:9bcf097cbfc2 152 return 0;
cyberjoey 17:401a5bb8d403 153 }
cyberjoey 17:401a5bb8d403 154
cyberjoey 9:36ba3626aab7 155 //******************************************************************************
cyberjoey 9:36ba3626aab7 156 // main()
cyberjoey 9:36ba3626aab7 157 //******************************************************************************
cyberjoey 9:36ba3626aab7 158
switches 0:60a522ae2e35 159 int main()
switches 0:60a522ae2e35 160 {
cyberjoey 10:148da21c297e 161
cyberjoey 9:36ba3626aab7 162 // i/o variables
cyberjoey 9:36ba3626aab7 163 char rx_buff[128]; // comport input buffer
cyberjoey 9:36ba3626aab7 164 int rx_index; // rx_buffer pointer
cyberjoey 22:ee0d36d534cd 165
cyberjoey 9:36ba3626aab7 166 //************* init ticker timer callbacks ****************
cyberjoey 23:9bcf097cbfc2 167 timer_1.attach(&LED_blink_callback, 0.5); //start ticker, once per sec.
cyberjoey 23:9bcf097cbfc2 168
cyberjoey 12:aa9fff0aec91 169 i2c.frequency(400000); //set I2C clock to 400kHz
cyberjoey 23:9bcf097cbfc2 170
cyberjoey 9:36ba3626aab7 171 rLED = LED_OFF;
switches 6:96d8d823e292 172 gLED = LED_ON;
cyberjoey 9:36ba3626aab7 173 bLED = LED_ON;
cyberjoey 23:9bcf097cbfc2 174
cyberjoey 9:36ba3626aab7 175 rx_index = 0; //character buffer index for input from PC
cyberjoey 23:9bcf097cbfc2 176
cyberjoey 23:9bcf097cbfc2 177 while (1) { // start main loop, check for input, process command repeat
cyberjoey 23:9bcf097cbfc2 178
cyberjoey 23:9bcf097cbfc2 179 //test if PC sent some charaters
cyberjoey 23:9bcf097cbfc2 180 while (pc.readable()) { //characters in buffer, get them
cyberjoey 9:36ba3626aab7 181 rx_buff[rx_index] = pc.getc();
cyberjoey 23:9bcf097cbfc2 182
cyberjoey 23:9bcf097cbfc2 183 if (rx_buff[rx_index] == CR) {
cyberjoey 9:36ba3626aab7 184 rx_buff[++rx_index] = 0;
cyberjoey 9:36ba3626aab7 185 rx_index = -1; // because i++ at end of while give i=0 on next loop
cyberjoey 9:36ba3626aab7 186
cyberjoey 23:9bcf097cbfc2 187 process_command(rx_buff);//if carriage return received, process command
cyberjoey 23:9bcf097cbfc2 188
cyberjoey 9:36ba3626aab7 189 }//end if(CR)
cyberjoey 23:9bcf097cbfc2 190 if (rx_buff[rx_index] == BS) {//backspace received, back up buffer pointer
cyberjoey 23:9bcf097cbfc2 191 if (rx_index>0)rx_index--;//remove last char from buffer if not at start.
cyberjoey 23:9bcf097cbfc2 192 }
cyberjoey 23:9bcf097cbfc2 193 else rx_index++;
cyberjoey 9:36ba3626aab7 194 }//end while(pc.redable())
cyberjoey 9:36ba3626aab7 195 }//end while(1)
switches 0:60a522ae2e35 196 }