Home | History | Annotate | Download | only in stm32_flash
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <stdint.h>
     19 #include <unistd.h>
     20 #include <sys/ioctl.h>
     21 #include <linux/i2c-dev.h>
     22 
     23 #include "stm32_bl.h"
     24 #include "i2c.h"
     25 
     26 uint8_t i2c_write_data(handle_t *handle, uint8_t *buffer, int length)
     27 {
     28     i2c_handle_t *i2c_handle = (i2c_handle_t *)handle;
     29 
     30     buffer[length] = checksum(handle, buffer, length);
     31 
     32     if (write(i2c_handle->fd, buffer, length+1) == (length+1))
     33         return CMD_ACK;
     34     else
     35         return CMD_NACK;
     36 }
     37 
     38 uint8_t i2c_write_cmd(handle_t *handle, uint8_t cmd)
     39 {
     40     uint8_t buffer[sizeof(uint8_t)+1] =
     41     {
     42         cmd
     43     };
     44 
     45     return handle->write_data(handle, buffer, sizeof(uint8_t));
     46 }
     47 
     48 uint8_t i2c_read_data(handle_t *handle, uint8_t *data, int length)
     49 {
     50     i2c_handle_t *i2c_handle = (i2c_handle_t *)handle;
     51 
     52     if (read(i2c_handle->fd, data, length) == length)
     53         return CMD_ACK;
     54     else
     55         return CMD_NACK;
     56 }
     57 
     58 uint8_t i2c_read_ack(handle_t *handle)
     59 {
     60     uint8_t buffer;
     61 
     62     if (handle->read_data(handle, &buffer, sizeof(uint8_t)) == CMD_ACK)
     63         return buffer;
     64     else
     65         return CMD_NACK;
     66 }
     67 
     68 int i2c_init(handle_t *handle)
     69 {
     70     i2c_handle_t *i2c_handle = (i2c_handle_t *)handle;
     71 
     72     handle->cmd_erase = CMD_ERASE_NS;
     73     handle->cmd_read_memory = CMD_READ_MEMORY;
     74     handle->cmd_write_memory = CMD_WRITE_MEMORY_NS;
     75 
     76     handle->no_extra_sync = 0;
     77 
     78     handle->write_data = i2c_write_data;
     79     handle->write_cmd = i2c_write_cmd;
     80     handle->read_data = i2c_read_data;
     81     handle->read_ack = i2c_read_ack;
     82 
     83     if (ioctl(i2c_handle->fd, I2C_SLAVE, i2c_handle->addr) < 0) {
     84         perror("Error setting slave addr");
     85         return -1;
     86     } else {
     87         return 0;
     88     }
     89 }
     90