| 1 | /***************************************************************************//** |
| 2 | * @file Communication.c |
| 3 | * @brief Implementation of the Communication Driver for RL78G14 processor. |
| 4 | * @author DBogdan ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2012(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | ******************************************************************************** |
| 40 | * SVN Revision: $WCREV$ |
| 41 | *******************************************************************************/ |
| 42 | |
| 43 | /******************************************************************************/ |
| 44 | /***************************** Include Files **********************************/ |
| 45 | /******************************************************************************/ |
| 46 | #include "Communication.h" // Communication definitions |
| 47 | #include "RDKRL78G14.h" // RDKRL78G14 definitions |
| 48 | |
| 49 | char IICA0_Flag; |
| 50 | |
| 51 | /******************************************************************************/ |
| 52 | /************************ Functions Definitions *******************************/ |
| 53 | /******************************************************************************/ |
| 54 | |
| 55 | /***************************************************************************//** |
| 56 | * @brief I2C interrupt service routine. |
| 57 | * |
| 58 | * @return None. |
| 59 | *******************************************************************************/ |
| 60 | #pragma vector = INTIICA0_vect |
| 61 | __interrupt static void IICA0_Interrupt(void) |
| 62 | { |
| 63 | IICA0_Flag = 1; |
| 64 | } |
| 65 | |
| 66 | /***************************************************************************//** |
| 67 | * @brief Initializes the SPI communication peripheral. |
| 68 | * |
| 69 | * @param lsbFirst - Transfer format (0 or 1). |
| 70 | * Example: 0x0 - MSB first. |
| 71 | * 0x1 - LSB first. |
| 72 | * @param clockFreq - SPI clock frequency (Hz). |
| 73 | * Example: 1000 - SPI clock frequency is 1 kHz. |
| 74 | * @param clockPol - SPI clock polarity (0 or 1). |
| 75 | * Example: 0x0 - Idle state for clock is a low level; active |
| 76 | * state is a high level; |
| 77 | * 0x1 - Idle state for clock is a high level; active |
| 78 | * state is a low level. |
| 79 | * @param clockEdg - SPI clock edge (0 or 1). |
| 80 | * Example: 0x0 - Serial output data changes on transition |
| 81 | * from idle clock state to active clock state; |
| 82 | * 0x1 - Serial output data changes on transition |
| 83 | * from active clock state to idle clock state. |
| 84 | * |
| 85 | * @return status - Result of the initialization procedure. |
| 86 | * Example: 0 - if initialization was successful; |
| 87 | * -1 - if initialization was unsuccessful. |
| 88 | *******************************************************************************/ |
| 89 | char SPI_Init(char lsbFirst, |
| 90 | long clockFreq, |
| 91 | char clockPol, |
| 92 | char clockEdg) |
| 93 | { |
| 94 | long mckFreq = 32000000; |
| 95 | char sdrValue = 0; |
| 96 | char delay = 0; |
| 97 | |
| 98 | /* Configure the CS pins. */ |
| 99 | PMOD1_CS_OUT; |
| 100 | PMOD1_CS_HIGH; |
| 101 | PMOD2_CS_OUT; |
| 102 | PMOD2_CS_HIGH; |
| 103 | ST7579_CS_OUT; |
| 104 | ST7579_CS_HIGH; |
| 105 | |
| 106 | /* Enable input clock supply. */ |
| 107 | SAU1EN = 1; |
| 108 | |
| 109 | /* After setting the SAUmEN bit to 1, be sure to set serial clock select |
| 110 | register m (SPSm) after 4 or more fCLK clocks have elapsed. */ |
| 111 | NOP; |
| 112 | NOP; |
| 113 | NOP; |
| 114 | NOP; |
| 115 | |
| 116 | /* Select the fCLK as input clock. */ |
| 117 | SPS1 = 0x0000; |
| 118 | |
| 119 | /* Select the CSI operation mode. */ |
| 120 | SMR11 = 0x0020; |
| 121 | |
| 122 | clockPol = 1 - clockPol; |
| 123 | SCR11 = (clockEdg << 13) | |
| 124 | (clockPol << 12) | |
| 125 | 0xC000 | // Operation mode: Transmission/reception. |
| 126 | 0x0007; // 8-bit data length. |
| 127 | |
| 128 | /* clockFreq = mckFreq / (sdrValue * 2 + 2) */ |
| 129 | sdrValue = mckFreq / (2 * clockFreq) - 1; |
| 130 | SDR11 = sdrValue << 9; |
| 131 | |
| 132 | /* Set the clock and data initial level. */ |
| 133 | clockPol = 1 - clockPol; |
| 134 | SO1 &= ~0x0202; |
| 135 | SO1 |= (clockPol << 9) | |
| 136 | (clockPol << 1); |
| 137 | |
| 138 | /* Enable output for serial communication operation. */ |
| 139 | SOE1 |= 0x0002; |
| 140 | |
| 141 | /* Configure the MISO pin as input. */ |
| 142 | PM7 |= 0x02; |
| 143 | |
| 144 | /* Configure SCLK and MOSI pins as output. */ |
| 145 | P7 |= 0x05; |
| 146 | PM7 &= ~0x05; |
| 147 | |
| 148 | /* Wait for the changes to take place. */ |
| 149 | for(delay = 0; delay < 50; delay++) |
| 150 | { |
| 151 | NOP; |
| 152 | } |
| 153 | |
| 154 | /* Set the SEmn bit to 1 and enter the communication wait status */ |
| 155 | SS1 |= 0x0002; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /***************************************************************************//** |
| 161 | * @brief Writes data to SPI. |
| 162 | * |
| 163 | * @param slaveDeviceId - The ID of the selected slave device. |
| 164 | * @param data - Data represents the write buffer. |
| 165 | * @param bytesNumber - Number of bytes to write. |
| 166 | * |
| 167 | * @return Number of written bytes. |
| 168 | *******************************************************************************/ |
| 169 | char SPI_Write(char slaveDeviceId, |
| 170 | unsigned char* data, |
| 171 | char bytesNumber) |
| 172 | { |
| 173 | char byte = 0; |
| 174 | unsigned char read = 0; |
| 175 | unsigned short originalSCR = 0; |
| 176 | unsigned short originalSO1 = 0; |
| 177 | |
| 178 | if(slaveDeviceId == 1) |
| 179 | { |
| 180 | PMOD1_CS_LOW; |
| 181 | } |
| 182 | if(slaveDeviceId == 2) |
| 183 | { |
| 184 | PMOD2_CS_LOW; |
| 185 | } |
| 186 | if(slaveDeviceId == 3) |
| 187 | { |
| 188 | ST1 |= 0x0002; |
| 189 | originalSO1 = SO1; |
| 190 | originalSCR = SCR11; |
| 191 | SO1 &= ~0x0202; |
| 192 | SCR11 &= ~0x3000; |
| 193 | SS1 |= 0x0002; |
| 194 | ST7579_CS_LOW; |
| 195 | } |
| 196 | for(byte = 0; byte < bytesNumber; byte++) |
| 197 | { |
| 198 | SIO21 = data[byte]; |
| 199 | NOP; |
| 200 | while(SSR11 & 0x0040); |
| 201 | read = SIO21; |
| 202 | } |
| 203 | if(slaveDeviceId == 1) |
| 204 | { |
| 205 | PMOD1_CS_HIGH; |
| 206 | } |
| 207 | if(slaveDeviceId == 2) |
| 208 | { |
| 209 | PMOD2_CS_HIGH; |
| 210 | } |
| 211 | if(slaveDeviceId == 3) |
| 212 | { |
| 213 | ST7579_CS_HIGH; |
| 214 | ST1 |= 0x0002; |
| 215 | SO1 = originalSO1; |
| 216 | SCR11 = originalSCR; |
| 217 | SS1 |= 0x0002; |
| 218 | } |
| 219 | |
| 220 | return bytesNumber; |
| 221 | } |
| 222 | |
| 223 | /***************************************************************************//** |
| 224 | * @brief Reads data from SPI. |
| 225 | * |
| 226 | * @param slaveDeviceId - The ID of the selected slave device. |
| 227 | * @param data - Data represents the write buffer as an input parameter |
| 228 | * and the read buffer as an output parameter. |
| 229 | * @param bytesNumber - Number of bytes to read. |
| 230 | * |
| 231 | * @return Number of read bytes. |
| 232 | *******************************************************************************/ |
| 233 | char SPI_Read(char slaveDeviceId, |
| 234 | unsigned char* data, |
| 235 | char bytesNumber) |
| 236 | { |
| 237 | char byte = 0; |
| 238 | unsigned short originalSCR = 0; |
| 239 | unsigned short originalSO1 = 0; |
| 240 | |
| 241 | if(slaveDeviceId == 1) |
| 242 | { |
| 243 | PMOD1_CS_LOW; |
| 244 | } |
| 245 | if(slaveDeviceId == 2) |
| 246 | { |
| 247 | PMOD2_CS_LOW; |
| 248 | } |
| 249 | if(slaveDeviceId == 3) |
| 250 | { |
| 251 | ST1 |= 0x0002; |
| 252 | originalSO1 = SO1; |
| 253 | originalSCR = SCR11; |
| 254 | SO1 &= ~0x0202; |
| 255 | SCR11 &= ~0x3000; |
| 256 | SS1 |= 0x0002; |
| 257 | ST7579_CS_LOW; |
| 258 | } |
| 259 | for(byte = 0; byte < bytesNumber; byte++) |
| 260 | { |
| 261 | SIO21 = data[byte]; |
| 262 | NOP; |
| 263 | while(SSR11 & 0x0040); |
| 264 | data[byte] = SIO21; |
| 265 | } |
| 266 | if(slaveDeviceId == 1) |
| 267 | { |
| 268 | PMOD1_CS_HIGH; |
| 269 | } |
| 270 | if(slaveDeviceId == 2) |
| 271 | { |
| 272 | PMOD2_CS_HIGH; |
| 273 | } |
| 274 | if(slaveDeviceId == 3) |
| 275 | { |
| 276 | ST7579_CS_HIGH; |
| 277 | ST1 |= 0x0002; |
| 278 | SO1 = originalSO1; |
| 279 | SCR11 = originalSCR; |
| 280 | SS1 |= 0x0002; |
| 281 | } |
| 282 | |
| 283 | return bytesNumber; |
| 284 | } |
| 285 | |
| 286 | /***************************************************************************//** |
| 287 | * @brief Initializes the I2C communication peripheral. |
| 288 | * |
| 289 | * @param clockFreq - I2C clock frequency (Hz). |
| 290 | * Example: 100000 - SPI clock frequency is 100 kHz. |
| 291 | * @return status - Result of the initialization procedure. |
| 292 | * Example: 0 - if initialization was successful; |
| 293 | * -1 - if initialization was unsuccessful. |
| 294 | *******************************************************************************/ |
| 295 | char I2C_Init(long clockFreq) |
| 296 | { |
| 297 | long fckFreq = 32000000; |
| 298 | unsigned char wlValue = 0; |
| 299 | unsigned char whValue = 0; |
| 300 | |
| 301 | /* Enable interrupts */ |
| 302 | EI; |
| 303 | |
| 304 | /* Enable input clock supply. */ |
| 305 | IICA0EN = 1; |
| 306 | |
| 307 | /* Set the fast mode plus operation. */ |
| 308 | SMC0 = 1; |
| 309 | |
| 310 | /* Set transfer rate. */ |
| 311 | wlValue = (unsigned char)((0.5 * fckFreq) / clockFreq); |
| 312 | whValue = (unsigned char)(wlValue - (fckFreq / (10 * clockFreq))); |
| 313 | IICWL0 = wlValue; |
| 314 | IICWH0 = whValue; |
| 315 | |
| 316 | STCEN0 = 1; // After operation is enabled, enable generation of a start |
| 317 | // condition without detecting a stop condition. |
| 318 | WTIM0 = 1; // Interrupt request is generated at the ninth clock’s |
| 319 | // falling edge. |
| 320 | |
| 321 | /* Enable I2C operation. */ |
| 322 | IICE0 = 1; |
| 323 | |
| 324 | /* Configure SCLA0 and SDAA0 pins as digital output. */ |
| 325 | P6 &= ~0x03; |
| 326 | PM6 &= ~0x03; |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /***************************************************************************//** |
| 332 | * @brief Writes data to a slave device. |
| 333 | * |
| 334 | * @param slaveAddress - Adress of the slave device. |
| 335 | * @param dataBuffer - Pointer to a buffer storing the transmission data. |
| 336 | * @param bytesNumber - Number of bytes to write. |
| 337 | * @param stopBit - Stop condition control. |
| 338 | * Example: 0 - A stop condition will not be sent; |
| 339 | * 1 - A stop condition will be sent. |
| 340 | * |
| 341 | * @return status - Number of read bytes or 0xFF if the slave address was |
| 342 | * not acknowledged by the device. |
| 343 | *******************************************************************************/ |
| 344 | char I2C_Write(char slaveAddress, |
| 345 | unsigned char* dataBuffer, |
| 346 | char bytesNumber, |
| 347 | char stopBit) |
| 348 | { |
| 349 | char byte = 0; |
| 350 | char status = 0; |
| 351 | |
| 352 | IICAMK0 = 1; // Interrupt servicing disabled. |
| 353 | STT0 = 1; // Generate a start condition. |
| 354 | IICAMK0 = 0; // Interrupt servicing enabled. |
| 355 | |
| 356 | /* Send the first byte. */ |
| 357 | IICA0_Flag = 0; |
| 358 | IICA0 = (slaveAddress << 1); |
| 359 | while(IICA0_Flag == 0); |
| 360 | |
| 361 | if(ACKD0) // Acknowledge was detected. |
| 362 | { |
| 363 | for(byte = 0; byte < bytesNumber; byte++) |
| 364 | { |
| 365 | IICA0_Flag = 0; |
| 366 | IICA0 = *dataBuffer; |
| 367 | while(IICA0_Flag == 0); |
| 368 | dataBuffer++; |
| 369 | } |
| 370 | status = bytesNumber; |
| 371 | } |
| 372 | else // Acknowledge was not detected. |
| 373 | { |
| 374 | status = 0xFF; |
| 375 | } |
| 376 | if(stopBit) |
| 377 | { |
| 378 | SPT0 = 1; // Generate a stop condition. |
| 379 | while(IICBSY0); // Wait until the I2C bus status flag is cleared. |
| 380 | } |
| 381 | |
| 382 | return status; |
| 383 | } |
| 384 | |
| 385 | /***************************************************************************//** |
| 386 | * @brief Reads data from a slave device. |
| 387 | * |
| 388 | * @param slaveAddress - Adress of the slave device. |
| 389 | * @param dataBuffer - Pointer to a buffer that will store the received data. |
| 390 | * @param bytesNumber - Number of bytes to read. |
| 391 | * @param stopBit - Stop condition control. |
| 392 | * Example: 0 - A stop condition will not be sent; |
| 393 | * 1 - A stop condition will be sent. |
| 394 | * |
| 395 | * @return status - Number of read bytes or 0xFF if the slave address was |
| 396 | * not acknowledged by the device. |
| 397 | *******************************************************************************/ |
| 398 | char I2C_Read(char slaveAddress, |
| 399 | unsigned char* dataBuffer, |
| 400 | char bytesNumber, |
| 401 | char stopBit) |
| 402 | { |
| 403 | char byte = 0; |
| 404 | char status = 0; |
| 405 | |
| 406 | IICAMK0 = 1; // Interrupt servicing disabled. |
| 407 | STT0 = 1; // Generate a start condition. |
| 408 | IICAMK0 = 0; // Interrupt servicing enabled. |
| 409 | |
| 410 | /* Send the first byte. */ |
| 411 | IICA0_Flag = 0; |
| 412 | IICA0 = (slaveAddress << 1) + 1; |
| 413 | while(IICA0_Flag == 0); |
| 414 | |
| 415 | if(ACKD0) // Acknowledge was detected. |
| 416 | { |
| 417 | ACKE0 = 1; // Enable acknowledgment. |
| 418 | for(byte = 0; byte < bytesNumber; byte++) |
| 419 | { |
| 420 | if(byte == (bytesNumber - 1)) |
| 421 | { |
| 422 | ACKE0 = 0U; // Disable acknowledgment. |
| 423 | } |
| 424 | WREL0 = 1U; // Cancel wait. |
| 425 | IICA0_Flag = 0; |
| 426 | while(IICA0_Flag == 0); |
| 427 | *dataBuffer = IICA0; |
| 428 | dataBuffer++; |
| 429 | } |
| 430 | status = bytesNumber; |
| 431 | } |
| 432 | else // Acknowledge was not detected. |
| 433 | { |
| 434 | status = 0xFF; |
| 435 | } |
| 436 | if(stopBit) |
| 437 | { |
| 438 | SPT0 = 1; // Generate a stop condition. |
| 439 | while(IICBSY0); // Wait until the I2C bus status flag is cleared. |
| 440 | } |
| 441 | |
| 442 | return status; |
| 443 | } |