/*************************************************************************** * Copyright (C) 2006 - 2020 by Olaf Rempel * * razzor AT kopf MINUS tisch DOT de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; version 2 of the License, * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include "target.h" #include "twi_master.h" #include #if (USE_TWI_SUPPORT) #define TWI_SLA_W(addr) (addr << 1) #define TWI_SLA_R(addr) ((addr << 1) | 0x01) /* *********************************************************************** * twi_start * *********************************************************************** */ static uint8_t twi_start(void) { TWCR = (1< 0); result = twi_master_rx(p_data++, ack); if (result != TWI_SUCCESS) { break; } } return result; } /* twi_master_rx_buf */ /* *********************************************************************** * twi_generic * *********************************************************************** */ uint8_t twi_generic(uint8_t twi_addr, const uint8_t * p_wr_data, uint16_t write_size, uint8_t * p_rd_data, uint16_t read_size) { uint8_t result = TWI_ERROR; if (write_size > 0) { result = twi_master_start(TWI_SLA_W(twi_addr)); if (result == TWI_SUCCESS) { result = twi_master_tx_buf(p_wr_data, write_size); } } if ((read_size > 0) && (result == TWI_SUCCESS) ) { result = twi_master_start(TWI_SLA_R(twi_addr)); if (result == TWI_SUCCESS) { result = twi_master_rx_buf(p_rd_data, read_size); } } twi_stop(); return result; } /* twi_generic */ /* *********************************************************************** * twi_switch_application * *********************************************************************** */ uint8_t twi_switch_application(uint8_t twi_addr, uint8_t app) { uint8_t cmd[2] = { CMD_SWITCH_APPLICATION, app }; return twi_generic(twi_addr, cmd, sizeof(cmd), NULL, 0); } /* twi_switch_application */ /* *********************************************************************** * twi_read_version * *********************************************************************** */ uint8_t twi_read_version(uint8_t twi_addr, char * p_version, uint8_t version_length) { uint8_t cmd[1] = { CMD_READ_VERSION }; return twi_generic(twi_addr, cmd, sizeof(cmd), (uint8_t *)p_version, version_length); } /* twi_read_version */ /* *********************************************************************** * twi_read_chipinfo * *********************************************************************** */ uint8_t twi_read_chipinfo(uint8_t twi_addr, twi_chipinfo_t * p_chipinfo) { uint8_t cmd[4] = { CMD_READ_MEMORY, MEMTYPE_CHIPINFO, 0x00, 0x00 }; return twi_generic(twi_addr, cmd, sizeof(cmd), (uint8_t *)p_chipinfo, sizeof(twi_chipinfo_t)); } /* twi_read_chipinfo */ /* *********************************************************************** * twi_read_memory * *********************************************************************** */ uint8_t twi_read_memory(uint8_t twi_addr, uint8_t memory_type, uint16_t memory_addr, uint8_t * p_data, uint16_t data_length) { uint8_t cmd[4] = { CMD_READ_MEMORY, memory_type, (memory_addr >> 8) & 0xFF, (memory_addr & 0xFF) }; return twi_generic(twi_addr, cmd, sizeof(cmd), p_data, data_length); } /* twi_read_memory */ /* *********************************************************************** * twi_write_memory * *********************************************************************** */ uint8_t twi_write_memory(uint8_t twi_addr, uint8_t memory_type, uint16_t memory_addr, const uint8_t * p_data, uint16_t data_length) { uint8_t cmd[4] = { CMD_WRITE_MEMORY, memory_type, (memory_addr >> 8) & 0xFF, (memory_addr & 0xFF) }; uint8_t result; result = twi_master_start(TWI_SLA_W(twi_addr)); if (result == TWI_SUCCESS) { result = twi_master_tx_buf(cmd, sizeof(cmd)); } if (result == TWI_SUCCESS) { result = twi_master_tx_buf(p_data, data_length); } twi_stop(); return result; } /* twi_read_memory */ /* *********************************************************************** * twi_init * *********************************************************************** */ void twi_init(uint8_t enable) { if (enable) { TWBR = ((F_CPU / 100000) -16) / 2; TWCR = (1<