David Giles
/
CM-MBED_RSEDP_Test_Suite_LPC1768_V3
This is a complete listing of the RS-EDP software for the mbed module to support the RS-EDP platform.
SourceFiles/RSEDP_BB_24LC32_Serial_EEPROM.cpp@0:5b7639d1f2c4, 2010-11-19 (annotated)
- Committer:
- DavidGilesHitex
- Date:
- Fri Nov 19 09:49:16 2010 +0000
- Revision:
- 0:5b7639d1f2c4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DavidGilesHitex | 0:5b7639d1f2c4 | 1 | /* Serial EEPROM I2C Type 24LC32 C Source Code Driver File */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 2 | /* ******************************************************* */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 3 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 4 | /* Version 1.00 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 5 | /* Start Date: 25/2/2008 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 6 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 7 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 8 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 9 | /* Include Files Here */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 10 | #include "mbed.h" /* mbed header file */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 11 | #include "misra_types.h" /* MISRA Types header file */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 12 | #include "defines.h" |
DavidGilesHitex | 0:5b7639d1f2c4 | 13 | #include "mbed_Port_Structure.h" /* Port structure for MBED Module */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 14 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 15 | #include "RSEDP_CNTRL_I2C.h" /* Control I2C Driver */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 16 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 17 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 18 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 19 | /* Function Prototypes Here */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 20 | sint32_t RSEDP_BB_setup_24LC32(uint8_t Slave_Address); /* Configure the I2C EEPROM */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 21 | sint32_t RSEDP_BB_eeprom_store_byte(uint8_t Slave_Address, sint8_t payload, uint16_t eeaddress); /* Write one byte to one memory location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 22 | sint32_t RSEDP_BB_eeprom_store_bytes(uint8_t Slave_Address, sint8_t *payload, uint8_t qty, uint16_t eeaddress); /* Called by main to write multiple bytes to the serial EEPROM */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 23 | sint32_t RSEDP_BB_eeprom_read_byte(uint8_t Slave_Address, sint8_t *read_value, uint16_t eeaddress); /* Read one byte from one location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 24 | sint32_t RSEDP_BB_eeprom_read_bytes(uint8_t Slave_Address, sint8_t *payload, uint16_t qty, uint16_t eeaddress); /* Read several byte from one location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 25 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 26 | static void delay_small(void); /* Local static 1us delay routine */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 27 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 28 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 29 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 30 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 31 | /* Setup the serial interface to the EEPROM */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 32 | sint32_t RSEDP_BB_setup_24LC32(uint8_t Slave_Address) /* Configure the 24LC32 */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 33 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 34 | sint32_t Ack_Status = 0; |
DavidGilesHitex | 0:5b7639d1f2c4 | 35 | /* Assume the I2C bus is already configured. This is normally done on mbed_Port_Structure.c */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 36 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 37 | Ack_Status = CNTRL_I2C_Master_Mode_Transmit(Slave_Address, 0, 1); /* Ping the device */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 38 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 39 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 40 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 41 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 42 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 43 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 44 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 45 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 46 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 47 | /* Store one byte at one address location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 48 | sint32_t RSEDP_BB_eeprom_store_byte(uint8_t Slave_Address, sint8_t payload, uint16_t eeaddress) /* Write one byte to one memory location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 49 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 50 | sint32_t Ack_Status = 0; |
DavidGilesHitex | 0:5b7639d1f2c4 | 51 | sint8_t tx_array[3]; /* two byte array used to hold transmission packet */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 52 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 53 | eeaddress = (eeaddress & 0x0fff); /* Ensure address is not more than 12 bit */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 54 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 55 | tx_array[0] = (uint8_t) (eeaddress >> 8); /* Extract high part */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 56 | tx_array[1] = (uint8_t) eeaddress; /* Extract Low address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 57 | tx_array[2] = payload; /* second member of array is the value to be stored */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 58 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 59 | Ack_Status = CNTRL_I2C_Master_Mode_Transmit(Slave_Address, tx_array, 3); /* Send a data packet to a slave, record acknowledge bit */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 60 | delay_small(); |
DavidGilesHitex | 0:5b7639d1f2c4 | 61 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 62 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 63 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 64 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 65 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 66 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 67 | /* Write upto 32 bytes into the EEPROM all at once */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 68 | sint32_t RSEDP_BB_eeprom_store_bytes(uint8_t Slave_Address, sint8_t *payload, uint8_t qty, uint16_t eeaddress) /* Called by main to write multiple bytes to the serial EEPROM */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 69 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 70 | sint32_t Ack_Status = 0; |
DavidGilesHitex | 0:5b7639d1f2c4 | 71 | uint8_t n=0; /* Scratch pad variable */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 72 | sint8_t tx_array[34]; /* transmission array */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 73 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 74 | eeaddress = (eeaddress & 0x0fff); /* Cap range to 12 bit address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 75 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 76 | tx_array[0]=(uint8_t) (eeaddress >> 8); /* Extract high address byte */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 77 | tx_array[1]=(uint8_t) eeaddress; /* Extract Low address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 78 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 79 | for (n = 2; n < (qty + 2); n++) /* Copy the transmission data into a local array */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 80 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 81 | tx_array[n] = *payload; |
DavidGilesHitex | 0:5b7639d1f2c4 | 82 | payload++; |
DavidGilesHitex | 0:5b7639d1f2c4 | 83 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 84 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 85 | Ack_Status = CNTRL_I2C_Master_Mode_Transmit(Slave_Address, tx_array, (qty+2)); /* Send data out on I2C bus */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 86 | delay_small(); /* EEPROM Busy period - self timed */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 87 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 88 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 89 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 90 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 91 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 92 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 93 | /* Read One Byte From An Address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 94 | sint32_t RSEDP_BB_eeprom_read_byte(uint8_t Slave_Address, sint8_t *payload, uint16_t eeaddress) /* Read one byte from one location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 95 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 96 | sint32_t Ack_Status = 0; |
DavidGilesHitex | 0:5b7639d1f2c4 | 97 | sint8_t tx_array[3]; |
DavidGilesHitex | 0:5b7639d1f2c4 | 98 | sint8_t rx_array = 0; /* Variable used to receive data from slave */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 99 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 100 | eeaddress = (eeaddress & 0x0fff); /* Cap address to 12 bit */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 101 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 102 | tx_array[0] = (uint8_t) (eeaddress >> 8); /* Extract high order byte */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 103 | tx_array[1] = (uint8_t) eeaddress; /* Extract low address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 104 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 105 | Ack_Status = CNTRL_I2C_Master_Mode_Transmit(Slave_Address, tx_array, 2); /* Set the EEPROM address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 106 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 107 | // delay_small(); /* EEPROM is busy writing */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 108 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 109 | if (Ack_Status == ACK) |
DavidGilesHitex | 0:5b7639d1f2c4 | 110 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 111 | Ack_Status = CNTRL_I2C_Master_Mode_Receive(Slave_Address, &rx_array,1); /* Read single byte address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 112 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 113 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 114 | *payload = rx_array; /* Load value from EEPROM into receive pointer */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 115 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 116 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 117 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 118 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 119 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 120 | /* Read several byte from the memory location */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 121 | sint32_t RSEDP_BB_eeprom_read_bytes(uint8_t Slave_Address, sint8_t *payload, uint16_t qty, uint16_t eeaddress) |
DavidGilesHitex | 0:5b7639d1f2c4 | 122 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 123 | sint32_t Ack_Status = 0; |
DavidGilesHitex | 0:5b7639d1f2c4 | 124 | sint8_t tx_array[3]; |
DavidGilesHitex | 0:5b7639d1f2c4 | 125 | uint8_t address_low = 0; /* EEPROM high address byte */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 126 | uint8_t address_high = 0; /* EEPROM low address byte */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 127 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 128 | eeaddress = (eeaddress & 0x0fff); /* Cap to 12 bit address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 129 | address_high = (uint8_t) (eeaddress >> 8); /* Extract high order address byte */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 130 | address_low = (uint8_t) eeaddress; /* Extract Low address byte */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 131 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 132 | tx_array[0] = address_high; /* first member of array is the EEPROM high address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 133 | tx_array[1] = address_low; /* Low byte next */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 134 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 135 | Ack_Status = CNTRL_I2C_Master_Mode_Transmit(Slave_Address, tx_array, 2); /* Set the EEPROM address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 136 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 137 | delay_small(); /* Write time delay busy period */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 138 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 139 | if (Ack_Status == ACK) |
DavidGilesHitex | 0:5b7639d1f2c4 | 140 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 141 | Ack_Status = CNTRL_I2C_Master_Mode_Receive(Slave_Address, payload, qty); /* Read address */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 142 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 143 | return Ack_Status; |
DavidGilesHitex | 0:5b7639d1f2c4 | 144 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 145 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 146 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 147 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 148 | /* Small 5ms delay approx */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 149 | /* Note: Change this delay to suite the write time/busy period of the EEPROM */ |
DavidGilesHitex | 0:5b7639d1f2c4 | 150 | static void delay_small(void) |
DavidGilesHitex | 0:5b7639d1f2c4 | 151 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 152 | uint32_t nnnn = 0; |
DavidGilesHitex | 0:5b7639d1f2c4 | 153 | |
DavidGilesHitex | 0:5b7639d1f2c4 | 154 | for(nnnn = 0; nnnn < 0x6000; nnnn++) |
DavidGilesHitex | 0:5b7639d1f2c4 | 155 | { |
DavidGilesHitex | 0:5b7639d1f2c4 | 156 | ; |
DavidGilesHitex | 0:5b7639d1f2c4 | 157 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 158 | } |
DavidGilesHitex | 0:5b7639d1f2c4 | 159 |