Platform drivers for Mbed.

Dependents:   EVAL-CN0535-FMCZ EVAL-CN0535-FMCZ EVAL-AD568x-AD569x EVAL-AD7606 ... more

Revision:
11:a2dcf0ebb5b5
Parent:
8:70fc373a5f46
Child:
12:d85b77f4160c
--- a/src/spi.cpp	Wed Jun 17 14:54:14 2020 +0000
+++ b/src/spi.cpp	Mon Aug 03 17:21:20 2020 +0530
@@ -25,8 +25,6 @@
 /********************** Macros and Constants Definitions **********************/
 /******************************************************************************/
 
-#define SEND_BYTE                      8
-
 /******************************************************************************/
 /********************** Variables and User defined data types *****************/
 /******************************************************************************/
@@ -97,10 +95,9 @@
 		    SPI frequency is required, consult your device documentation.
 		  **/
 		spi->frequency(param->max_speed_hz);
-		spi->format(SEND_BYTE, param->mode);       // Stick to byte-multiples
+		spi->format(16, param->mode);              // 16-bit data write/read format
 		spi->set_default_write_value(0x00);        // code to write when reading back
 		ss->write(GPIO_HIGH);                      // set SS high
-
 		return SUCCESS;
 	}
 
@@ -152,17 +149,41 @@
 			   uint8_t *data,
 			   uint16_t bytes_number)
 {
-	mbed::SPI *spi; 	// pointer to new spi instance
-	DigitalOut *ss;  	// pointer to new SS instance
+	mbed::SPI *spi; 			// pointer to new spi instance
+	mbed::DigitalOut *ss;  		// pointer to new SS instance
+	uint8_t num_of_words;		// Number of words in SPI frame
+	uint16_t rw_data;			// SPI read data
+	uint8_t data_index = 0;		// Data index
 
 	if (desc) {
 		spi = (SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port);
 		ss = (DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select);
-
+		
+		spi->format(16, desc->mode);
+		
+		/* Get the total number of words (16-bit) and leftover bytes (8-bit) */
+		num_of_words = bytes_number / 2;
+		
 		ss->write(GPIO_LOW);
-
-		for (size_t byte = 0 ; byte < bytes_number ; byte++) {
-			data[byte] =  spi->write(data[byte]);
+		
+		while (num_of_words) {
+			/* Form a 16-bit data to be written */
+			rw_data = ((uint16_t)data[data_index + 1] | ((uint16_t)data[data_index] << 8));
+		
+			/* Transmit a 16-bit data over SPI */
+			rw_data =  (uint16_t)spi->write(rw_data);
+			
+			/* Extract the MSB and LSB from 16-bit read data */
+			data[data_index++] = (uint8_t)(rw_data >> 8);
+			data[data_index++] = (uint8_t)rw_data;
+			
+			num_of_words--;
+		}
+		
+		/* Send the odd/single byte */
+		if (bytes_number % 2) {
+			spi->format(8, desc->mode);
+			data[data_index] =  (uint8_t)spi->write(data[data_index]);
 		}
 
 		ss->write(GPIO_HIGH);