etisserant@0: /* etisserant@0: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@0: etisserant@0: Author: Christian Fortin (canfestival@canopencanada.ca) etisserant@0: etisserant@0: See COPYING file for copyrights details. etisserant@0: etisserant@0: This library is free software; you can redistribute it and/or etisserant@0: modify it under the terms of the GNU Lesser General Public etisserant@0: License as published by the Free Software Foundation; either etisserant@0: version 2.1 of the License, or (at your option) any later version. etisserant@0: etisserant@0: This library is distributed in the hope that it will be useful, etisserant@0: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: Lesser General Public License for more details. etisserant@0: etisserant@0: You should have received a copy of the GNU Lesser General Public etisserant@0: License along with this library; if not, write to the Free Software etisserant@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: */ etisserant@0: etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: #include "applicfg.h" etisserant@0: // #include "objdictdef.h" etisserant@0: etisserant@0: #include "lpc2138_pinout.h" etisserant@0: #include "lpc2138_defs.h" etisserant@0: #include "lpc2138.h" etisserant@0: etisserant@0: #define IAP_LOCATION 0x7ffffff1 etisserant@0: etisserant@0: // define a page of data of 256 bytes etisserant@0: // etisserant@0: short data_len; /* 0 to 256 bytes */ etisserant@0: unsigned int *data_page = NULL; etisserant@0: unsigned int data_addr; etisserant@0: etisserant@0: // local definitons etisserant@0: void ee_erase(unsigned int ,unsigned int[]); //function erases EEPROM etisserant@0: void ee_write_page(unsigned int); //function adds a record in EEPROM etisserant@0: void ee_read_page(unsigned int); //function reads the latest valid record in EEPROM etisserant@0: etisserant@0: typedef void (*IAP)(unsigned int [],unsigned int[]); etisserant@0: IAP iap_entry; etisserant@0: etisserant@0: etisserant@0: /***************************************************************************************/ etisserant@0: etisserant@0: void lpc2138_pinsel_set(int pin, LPC2138_PORT port, int size, int func) etisserant@0: { etisserant@0: int i; etisserant@0: etisserant@0: for (i = 0; i < size; pin++, i++) etisserant@0: { etisserant@0: /* 2 bits par broche. */ etisserant@0: int shift = (pin - ((pin < 16) ? 0 : 16)) << 1; etisserant@0: etisserant@0: REG32_ADDR pinsel = (port == 1) ? etisserant@0: (REG32_ADDR) P1_PINSEL2_ADDR : ((pin < 16) ? etisserant@0: (REG32_ADDR) P0_PINSEL0_ADDR : (REG32_ADDR) P0_PINSEL1_ADDR); etisserant@0: etisserant@0: *pinsel = (*pinsel & ~(BITMASK_2 << shift)) | (func << shift); etisserant@0: } etisserant@0: } etisserant@0: etisserant@0: void lpc2138_pinsel_clear() etisserant@0: { etisserant@0: P0_PINSEL0 = 0x00000000; etisserant@0: P0_PINSEL1 = 0x00000000; etisserant@0: P1_PINSEL2 = 0x00000000; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: int lpc2138_printf(void) etisserant@0: { etisserant@0: return lpc2138_fprintf(stdout); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: int lpc2138_printf_pins(void) etisserant@0: { etisserant@0: return lpc2138_fprintf_pins(stdout); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: int lpc2138_fprintf(FILE *stream) etisserant@0: { etisserant@0: return fprintf(stream, etisserant@0: "[p0=0x%08X,p0_iodir=0x%08X,p0_pinsel0=0x%08X,p0_pinsel1=0x%08X," \ etisserant@0: "p1=0x%08X,p1_iodir=0x%08X,p1_pinsel2=0x%08X]", etisserant@0: P0_IOPIN, P0_IODIR, P0_PINSEL0, P0_PINSEL1, etisserant@0: P1_IOPIN, P1_IODIR, P1_PINSEL2); etisserant@0: } etisserant@0: etisserant@0: int lpc2138_fprintf_pins(FILE *stream) etisserant@0: { etisserant@0: return fprintf(stream, etisserant@0: "[cs_s1d13706=0x%X," \ etisserant@0: "cs_sja1000=0x%X," \ etisserant@0: "wait=0x%X," \ etisserant@0: "bhe=0x%X," \ etisserant@0: "interrupt_sja1000=0x%X," \ etisserant@0: "redgreenled=0x%X," \ etisserant@0: "ale=0x%X," \ etisserant@0: "rd=0x%X," \ etisserant@0: "wr=0x%X," \ etisserant@0: "data=0x%X," \ etisserant@0: "addresses=0x%X]", etisserant@0: lpc2138_cs_s1d13706_get(), etisserant@0: lpc2138_cs_sja1000_get(), etisserant@0: lpc2138_wait_get(), etisserant@0: lpc2138_bhe_get(), etisserant@0: lpc2138_redgreenled_get(), etisserant@0: lpc2138_interrupt_sja1000_get(), etisserant@0: lpc2138_ale_get(), etisserant@0: lpc2138_rd_get(), etisserant@0: lpc2138_wr_get(), etisserant@0: lpc2138_data_get(), etisserant@0: lpc2138_addresses_get()); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: SJA1000 interface etisserant@0: */ etisserant@0: etisserant@0: unsigned char sja1000_read(unsigned char addr8) etisserant@0: { etisserant@0: unsigned char data; etisserant@0: etisserant@0: lpc2138_data_set_mode(LPC2138_MODE_OUTPUT); etisserant@0: lpc2138_ale_set(1); etisserant@0: lpc2138_data_set(addr8); etisserant@0: etisserant@0: lpc2138_ale_set(0); etisserant@0: lpc2138_data_set_mode(LPC2138_MODE_INPUT); etisserant@0: etisserant@0: lpc2138_cs_sja1000_set(0); etisserant@0: lpc2138_rd_set(0); etisserant@0: data = lpc2138_data_get(); etisserant@0: data = lpc2138_data_get(); etisserant@0: etisserant@0: lpc2138_rd_set(1); etisserant@0: etisserant@0: lpc2138_cs_sja1000_set(1); etisserant@0: lpc2138_data_set_mode(LPC2138_MODE_OUTPUT); etisserant@0: etisserant@0: return data; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void sja1000_write(unsigned char addr8, unsigned char data) etisserant@0: { etisserant@0: lpc2138_data_set_mode(LPC2138_MODE_OUTPUT); etisserant@0: etisserant@0: lpc2138_data_set(addr8); etisserant@0: etisserant@0: lpc2138_ale_set(1); etisserant@0: etisserant@0: lpc2138_ale_set(0); etisserant@0: lpc2138_cs_sja1000_set(0); etisserant@0: lpc2138_wr_set(0); etisserant@0: etisserant@0: lpc2138_data_set(data); etisserant@0: etisserant@0: lpc2138_wr_set(1); etisserant@0: lpc2138_cs_sja1000_set(1); etisserant@0: etisserant@0: } etisserant@0: etisserant@0: /* etisserant@0: FLASH interface etisserant@0: */ etisserant@0: etisserant@0: /************************************************************************/ etisserant@0: /* */ etisserant@0: /* function: */ etisserant@0: /* void ee_erase(unsigned int command_ee,unsigned int result_ee[]) */ etisserant@0: /* */ etisserant@0: /* type: void */ etisserant@0: /* */ etisserant@0: /* parameters: */ etisserant@0: /* command_ee - Not used. */ etisserant@0: /* result_ee[0] - Returns a response to the last IAP command used. */ etisserant@0: /* 0 - EEPROM successfully erased. */ etisserant@0: /* For all other response values, see microcontroller */ etisserant@0: /* User Manual, IAP Commands and Status Codes Summary. */ etisserant@0: /* result_ee[1] - Not used. */ etisserant@0: /* */ etisserant@0: /* version: 1.1 (01/27/2006) */ etisserant@0: /* */ etisserant@0: /* constants defined in LPC2k_ee.h used in this function: */ etisserant@0: /* EE_SEC_L - microcontroller's Flash sector where EEPROM begins */ etisserant@0: /* EE_SEC_H - microcontroller's Flash sector where EEPROM ends */ etisserant@0: /* EE_CCLK - microcontroller's system clock (cclk) */ etisserant@0: /* */ etisserant@0: /* description: */ etisserant@0: /* This function erases LPC2000 on-chip Flash sectors selected to act */ etisserant@0: /* as an EEPROM. All Flash sectors between EE_SEC_L abd EE_SEC_H */ etisserant@0: /* (including these sectors) will be erased using the In Application */ etisserant@0: /* Programming (IAP) routines (see User Manual for more details). */ etisserant@0: /* Also, this function disables all interrupts while erasing the */ etisserant@0: /* EEPROM. If this is not needed, three lines of the ee_erase */ etisserant@0: /* subroutine can simply be commented-out without affecting the */ etisserant@0: /* routine performance at all. */ etisserant@0: /* */ etisserant@0: /* revision history: */ etisserant@0: /* - Rev. 1.1 adds interrupt disable feature. */ etisserant@0: /* */ etisserant@0: /************************************************************************/ etisserant@0: void iat_flash_erase(unsigned int command_ee,unsigned int result_ee[]) etisserant@0: { etisserant@0: unsigned int command_iap[5]; etisserant@0: unsigned int result_iap[3]; etisserant@0: unsigned long int enabled_interrupts; etisserant@0: etisserant@0: enabled_interrupts = VICIntEnable; //disable all interrupts etisserant@0: VICIntEnClr = enabled_interrupts; etisserant@0: etisserant@0: command_iap[0]=50; // prepare sectors from EE_SEC_L to EE_SEC_H for erase etisserant@0: command_iap[1]=EE_SEC_L; etisserant@0: command_iap[2]=EE_SEC_H; etisserant@0: iap_entry=(IAP) IAP_LOCATION; etisserant@0: iap_entry(command_iap,result_iap); etisserant@0: etisserant@0: command_iap[0]=52; // erase sectors from EE_SEC_L to EE_SEC_H etisserant@0: command_iap[1]=EE_SEC_L; etisserant@0: command_iap[2]=EE_SEC_H; etisserant@0: command_iap[3]=EE_CCLK; etisserant@0: iap_entry=(IAP) IAP_LOCATION; etisserant@0: iap_entry(command_iap,result_iap); etisserant@0: etisserant@0: command_iap[0]=53; // blankcheck sectors from EE_SEC_L to EE_SEC_H etisserant@0: command_iap[1]=EE_SEC_L; etisserant@0: command_iap[2]=EE_SEC_H; etisserant@0: iap_entry=(IAP) IAP_LOCATION; etisserant@0: iap_entry(command_iap,result_iap); etisserant@0: etisserant@0: VICIntEnable = enabled_interrupts; //restore interrupt enable register etisserant@0: etisserant@0: result_ee[0]=result_iap[0]; etisserant@0: return; etisserant@0: } etisserant@0: etisserant@0: /************************************************************************/ etisserant@0: /* */ etisserant@0: /* function: */ etisserant@0: /* void ee_write(unsigned int command_ee,unsigned int result_ee[]) */ etisserant@0: /* */ etisserant@0: /* type: void */ etisserant@0: /* */ etisserant@0: /* parameters: */ etisserant@0: /* command_ee - An address of a content of ee_data type that has */ etisserant@0: /* to be programmed into EEPROM. */ etisserant@0: /* result_ee[0] - Returns a response to the last IAP command used. */ etisserant@0: /* 0 - data successfully programmed in EEPROM. */ etisserant@0: /* 501 - no space in EEPROM to program data. */ etisserant@0: /* For all other response values, see microcontroller */ etisserant@0: /* User Manual, IAP Commands and Status Codes Summary. */ etisserant@0: /* result_ee[1] - Not used. */ etisserant@0: /* */ etisserant@0: /* version: 1.1 (01/27/2006) */ etisserant@0: /* */ etisserant@0: /* constants defined in LPC2k_ee.h used in this function: */ etisserant@0: /* EE_BUFFER_SIZE - IAP buffer size; must be 256 or 512 */ etisserant@0: /* NO_SPACE_IN_EEPROM - EEPROM is full and no data can be programmed */ etisserant@0: /* EE_BUFFER_MASK - parameter used for interfacing with IAP */ etisserant@0: /* EE_REC_SIZE - ee_data structure size in bytes */ etisserant@0: /* EE_SEC_L - micro's Flash sector where EEPROM begins */ etisserant@0: /* EE_SEC_H - micro's Flash sector where EEPROM ends */ etisserant@0: /* EE_CCLK - micro's system clock (cclk) */ etisserant@0: /* */ etisserant@0: /* description: */ etisserant@0: /* This function writes a single structure of ee_data type into the */ etisserant@0: /* EEPROM using an In Application Programming (IAP) routines (see */ etisserant@0: /* User Manual for more details). command_ee contains an address of */ etisserant@0: /* this structure. EEPROM is scanned for the last (if any) record */ etisserant@0: /* identifier (EE_REC_ID), and a new record is added next to it. */ etisserant@0: /* Also, this function disables all interrupts while erasing the */ etisserant@0: /* EEPROM. If this is not needed, three lines of the ee_write */ etisserant@0: /* subroutine can simply be commented-out without affecting the */ etisserant@0: /* routine performance at all. */ etisserant@0: /* */ etisserant@0: /* revision history: */ etisserant@0: /* - Rev. 1.1 fixes a bug related to verifying a content written into */ etisserant@0: /* the EEPROM. 1.0 was reporting missmatch even when there were no */ etisserant@0: /* problems at all. */ etisserant@0: /* Rev. 1.1 adds interrupt disable feature. */ etisserant@0: /* */ etisserant@0: /************************************************************************/ etisserant@0: etisserant@0: void iat_flash_write_page(unsigned int addr) etisserant@0: { etisserant@0: unsigned long int enabled_interrupts; etisserant@0: // unsigned char ee_buffer[16]; etisserant@0: unsigned int command_iap[5], result_iap[3]; etisserant@0: etisserant@0: enabled_interrupts = VICIntEnable; //disable all interrupts etisserant@0: VICIntEnClr = enabled_interrupts; etisserant@0: etisserant@0: iap_entry = (IAP) IAP_LOCATION; etisserant@0: etisserant@0: // prepare sectors from EE_SEC_L to EE_SEC_H for erase etisserant@0: command_iap[0] = 50; etisserant@0: command_iap[1] = EE_SEC_L; etisserant@0: command_iap[2] = EE_SEC_H; etisserant@0: iap_entry(command_iap, result_iap); etisserant@0: etisserant@0: // copy RAM to flash/eeprom etisserant@0: command_iap[0] = 51; etisserant@0: command_iap[1] = (unsigned int) (addr & EE_START_MASK); // 256 kb boundary etisserant@0: command_iap[2] = (unsigned int) (data_page); // should be on a word boundary etisserant@0: command_iap[3] = 256; etisserant@0: command_iap[4] = EE_CCLK; etisserant@0: iap_entry(command_iap, result_iap); etisserant@0: etisserant@0: #if 0 etisserant@0: // compare RAM and flash/eeprom etisserant@0: command_iap[0] = 56; etisserant@0: command_iap[1] = (unsigned int) data; etisserant@0: command_iap[2] = addr; etisserant@0: command_iap[3] = dlen; etisserant@0: iap_entry(command_iap, result_iap); etisserant@0: #endif etisserant@0: etisserant@0: VICIntEnable = enabled_interrupts; //restore interrupt enable register etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /************************************************************************/ etisserant@0: /* */ etisserant@0: /* function: */ etisserant@0: /* void ee_read(unsigned int command_ee,unsigned int result_ee[]) */ etisserant@0: /* */ etisserant@0: /* type: void */ etisserant@0: /* */ etisserant@0: /* parameters: */ etisserant@0: /* command_ee - Not used. */ etisserant@0: /* result_ee[0] - Returns a response. */ etisserant@0: /* 0 - data successfully found in EEPROM. */ etisserant@0: /* 500 - no data/records available in EEPROM. */ etisserant@0: /* result_ee[1] - an address of the last record of ee_data type */ etisserant@0: /* in EEPROM. */ etisserant@0: /* */ etisserant@0: /* version: 1.1 (01/27/2006) */ etisserant@0: /* */ etisserant@0: /* constants defined in LPC2k_ee.h used in this function: */ etisserant@0: /* NO_RECORDS_AVAILABLE - EEPROM is empty/no records identifiable */ etisserant@0: /* with a record identifier (EE_REC_ID) found */ etisserant@0: /* EE_ADR_L - micro's Flash address from where EEPROM begins */ etisserant@0: /* EE_REC_SIZE - size (in bytes) of a ee_data structure */ etisserant@0: /* */ etisserant@0: /* description: */ etisserant@0: /* This function scans an EEPROM content looking for the last record */ etisserant@0: /* that can be identified with a record identifier (EE_REC_ID). When */ etisserant@0: /* such data is found, its address is passed as result_ee[1]. */ etisserant@0: /* */ etisserant@0: /* revision history: */ etisserant@0: /* - Rev. 1.0 had problems with accessing the last record in a fully */ etisserant@0: /* occupied EEPROM. Rev. 1.1 fixes this. */ etisserant@0: /* */ etisserant@0: /************************************************************************/ etisserant@0: void iat_flash_read_page(unsigned int addr) etisserant@0: { etisserant@0: memcpy(data_page, (unsigned int *)addr, sizeof(unsigned int)*64); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: