versione corretta

Dependents:   DISCO_L475VG_IOT01-Sensors-BSP

Committer:
group-Farnell24-IOT-Team
Date:
Tue Aug 21 08:34:28 2018 +0000
Revision:
0:766454e296c3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-Farnell24-IOT-Team 0:766454e296c3 1 /**
group-Farnell24-IOT-Team 0:766454e296c3 2 * @file BufferedSpi.cpp
group-Farnell24-IOT-Team 0:766454e296c3 3 * @brief Software Buffer - Extends mbed SPI functionallity
group-Farnell24-IOT-Team 0:766454e296c3 4 * @author Armelle Duboc
group-Farnell24-IOT-Team 0:766454e296c3 5 * @version 1.0
group-Farnell24-IOT-Team 0:766454e296c3 6 * @see
group-Farnell24-IOT-Team 0:766454e296c3 7 *
group-Farnell24-IOT-Team 0:766454e296c3 8 * Copyright (c) STMicroelectronics 2017
group-Farnell24-IOT-Team 0:766454e296c3 9 *
group-Farnell24-IOT-Team 0:766454e296c3 10 * Licensed under the Apache License, Version 2.0 (the "License");
group-Farnell24-IOT-Team 0:766454e296c3 11 * you may not use this file except in compliance with the License.
group-Farnell24-IOT-Team 0:766454e296c3 12 * You may obtain a copy of the License at
group-Farnell24-IOT-Team 0:766454e296c3 13 *
group-Farnell24-IOT-Team 0:766454e296c3 14 * http://www.apache.org/licenses/LICENSE-2.0
group-Farnell24-IOT-Team 0:766454e296c3 15 *
group-Farnell24-IOT-Team 0:766454e296c3 16 * Unless required by applicable law or agreed to in writing, software
group-Farnell24-IOT-Team 0:766454e296c3 17 * distributed under the License is distributed on an "AS IS" BASIS,
group-Farnell24-IOT-Team 0:766454e296c3 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-Farnell24-IOT-Team 0:766454e296c3 19 * See the License for the specific language governing permissions and
group-Farnell24-IOT-Team 0:766454e296c3 20 * limitations under the License.
group-Farnell24-IOT-Team 0:766454e296c3 21 */
group-Farnell24-IOT-Team 0:766454e296c3 22
group-Farnell24-IOT-Team 0:766454e296c3 23 #include "BufferedSpi.h"
group-Farnell24-IOT-Team 0:766454e296c3 24 #include <stdarg.h>
group-Farnell24-IOT-Team 0:766454e296c3 25 #include "mbed_debug.h"
group-Farnell24-IOT-Team 0:766454e296c3 26 #include "mbed_error.h"
group-Farnell24-IOT-Team 0:766454e296c3 27
group-Farnell24-IOT-Team 0:766454e296c3 28 // change to true to add few SPI debug lines
group-Farnell24-IOT-Team 0:766454e296c3 29 #define local_debug false
group-Farnell24-IOT-Team 0:766454e296c3 30
group-Farnell24-IOT-Team 0:766454e296c3 31 extern "C" int BufferedPrintfC(void *stream, int size, const char* format, va_list arg);
group-Farnell24-IOT-Team 0:766454e296c3 32
group-Farnell24-IOT-Team 0:766454e296c3 33 void BufferedSpi::DatareadyRising(void)
group-Farnell24-IOT-Team 0:766454e296c3 34 {
group-Farnell24-IOT-Team 0:766454e296c3 35 if (_cmddata_rdy_rising_event == 1) {
group-Farnell24-IOT-Team 0:766454e296c3 36 _cmddata_rdy_rising_event=0;
group-Farnell24-IOT-Team 0:766454e296c3 37 }
group-Farnell24-IOT-Team 0:766454e296c3 38 }
group-Farnell24-IOT-Team 0:766454e296c3 39
group-Farnell24-IOT-Team 0:766454e296c3 40 int BufferedSpi::wait_cmddata_rdy_high(void)
group-Farnell24-IOT-Team 0:766454e296c3 41 {
group-Farnell24-IOT-Team 0:766454e296c3 42 Timer timer;
group-Farnell24-IOT-Team 0:766454e296c3 43 timer.start();
group-Farnell24-IOT-Team 0:766454e296c3 44
group-Farnell24-IOT-Team 0:766454e296c3 45 /* wait for dataready = 1 */
group-Farnell24-IOT-Team 0:766454e296c3 46 while(dataready.read() == 0) {
group-Farnell24-IOT-Team 0:766454e296c3 47 if (timer.read_ms() > _timeout) {
group-Farnell24-IOT-Team 0:766454e296c3 48 debug_if(local_debug,"ERROR: SPI write timeout\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 49 return -1;
group-Farnell24-IOT-Team 0:766454e296c3 50 }
group-Farnell24-IOT-Team 0:766454e296c3 51 }
group-Farnell24-IOT-Team 0:766454e296c3 52
group-Farnell24-IOT-Team 0:766454e296c3 53 _cmddata_rdy_rising_event = 1;
group-Farnell24-IOT-Team 0:766454e296c3 54
group-Farnell24-IOT-Team 0:766454e296c3 55 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 56 }
group-Farnell24-IOT-Team 0:766454e296c3 57
group-Farnell24-IOT-Team 0:766454e296c3 58 int BufferedSpi::wait_cmddata_rdy_rising_event(void)
group-Farnell24-IOT-Team 0:766454e296c3 59 {
group-Farnell24-IOT-Team 0:766454e296c3 60 Timer timer;
group-Farnell24-IOT-Team 0:766454e296c3 61 timer.start();
group-Farnell24-IOT-Team 0:766454e296c3 62
group-Farnell24-IOT-Team 0:766454e296c3 63 while (_cmddata_rdy_rising_event == 1) {
group-Farnell24-IOT-Team 0:766454e296c3 64 if (timer.read_ms() > _timeout) {
group-Farnell24-IOT-Team 0:766454e296c3 65 _cmddata_rdy_rising_event = 0;
group-Farnell24-IOT-Team 0:766454e296c3 66 if (dataready.read() == 1) {
group-Farnell24-IOT-Team 0:766454e296c3 67 debug_if(local_debug,"ERROR: We missed rising event !! (timemout=%d)\r\n", _timeout);
group-Farnell24-IOT-Team 0:766454e296c3 68 }
group-Farnell24-IOT-Team 0:766454e296c3 69 debug_if(local_debug,"ERROR: SPI read timeout\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 70 return -1;
group-Farnell24-IOT-Team 0:766454e296c3 71 }
group-Farnell24-IOT-Team 0:766454e296c3 72 }
group-Farnell24-IOT-Team 0:766454e296c3 73
group-Farnell24-IOT-Team 0:766454e296c3 74 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 75 }
group-Farnell24-IOT-Team 0:766454e296c3 76
group-Farnell24-IOT-Team 0:766454e296c3 77 BufferedSpi::BufferedSpi(PinName mosi, PinName miso, PinName sclk, PinName _nss, PinName _datareadypin,
group-Farnell24-IOT-Team 0:766454e296c3 78 uint32_t buf_size, uint32_t tx_multiple, const char* name)
group-Farnell24-IOT-Team 0:766454e296c3 79 : SPI(mosi, miso, sclk, NC), nss(_nss), _txbuf((uint32_t)(tx_multiple*buf_size)), _rxbuf(buf_size), dataready(_datareadypin)
group-Farnell24-IOT-Team 0:766454e296c3 80 {
group-Farnell24-IOT-Team 0:766454e296c3 81 this->_buf_size = buf_size;
group-Farnell24-IOT-Team 0:766454e296c3 82 this->_tx_multiple = tx_multiple;
group-Farnell24-IOT-Team 0:766454e296c3 83 this->_sigio_event = 0;
group-Farnell24-IOT-Team 0:766454e296c3 84
group-Farnell24-IOT-Team 0:766454e296c3 85 _datareadyInt = new InterruptIn(_datareadypin);
group-Farnell24-IOT-Team 0:766454e296c3 86 _datareadyInt->rise(callback(this, &BufferedSpi::DatareadyRising));
group-Farnell24-IOT-Team 0:766454e296c3 87
group-Farnell24-IOT-Team 0:766454e296c3 88 _cmddata_rdy_rising_event = 1;
group-Farnell24-IOT-Team 0:766454e296c3 89
group-Farnell24-IOT-Team 0:766454e296c3 90 return;
group-Farnell24-IOT-Team 0:766454e296c3 91 }
group-Farnell24-IOT-Team 0:766454e296c3 92
group-Farnell24-IOT-Team 0:766454e296c3 93 BufferedSpi::~BufferedSpi(void)
group-Farnell24-IOT-Team 0:766454e296c3 94 {
group-Farnell24-IOT-Team 0:766454e296c3 95
group-Farnell24-IOT-Team 0:766454e296c3 96 return;
group-Farnell24-IOT-Team 0:766454e296c3 97 }
group-Farnell24-IOT-Team 0:766454e296c3 98
group-Farnell24-IOT-Team 0:766454e296c3 99 void BufferedSpi::frequency(int hz)
group-Farnell24-IOT-Team 0:766454e296c3 100 {
group-Farnell24-IOT-Team 0:766454e296c3 101 SPI::frequency(hz);
group-Farnell24-IOT-Team 0:766454e296c3 102 }
group-Farnell24-IOT-Team 0:766454e296c3 103
group-Farnell24-IOT-Team 0:766454e296c3 104 void BufferedSpi::format(int bits, int mode)
group-Farnell24-IOT-Team 0:766454e296c3 105 {
group-Farnell24-IOT-Team 0:766454e296c3 106 SPI::format(bits, mode);
group-Farnell24-IOT-Team 0:766454e296c3 107 }
group-Farnell24-IOT-Team 0:766454e296c3 108
group-Farnell24-IOT-Team 0:766454e296c3 109 void BufferedSpi::disable_nss()
group-Farnell24-IOT-Team 0:766454e296c3 110 {
group-Farnell24-IOT-Team 0:766454e296c3 111 nss = 1;
group-Farnell24-IOT-Team 0:766454e296c3 112 wait_us(15);
group-Farnell24-IOT-Team 0:766454e296c3 113 }
group-Farnell24-IOT-Team 0:766454e296c3 114
group-Farnell24-IOT-Team 0:766454e296c3 115 void BufferedSpi::enable_nss()
group-Farnell24-IOT-Team 0:766454e296c3 116 {
group-Farnell24-IOT-Team 0:766454e296c3 117 nss = 0;
group-Farnell24-IOT-Team 0:766454e296c3 118 wait_us(15);
group-Farnell24-IOT-Team 0:766454e296c3 119 }
group-Farnell24-IOT-Team 0:766454e296c3 120
group-Farnell24-IOT-Team 0:766454e296c3 121 int BufferedSpi::readable(void)
group-Farnell24-IOT-Team 0:766454e296c3 122 {
group-Farnell24-IOT-Team 0:766454e296c3 123 return _rxbuf.available(); // note: look if things are in the buffer
group-Farnell24-IOT-Team 0:766454e296c3 124 }
group-Farnell24-IOT-Team 0:766454e296c3 125
group-Farnell24-IOT-Team 0:766454e296c3 126 int BufferedSpi::writeable(void)
group-Farnell24-IOT-Team 0:766454e296c3 127 {
group-Farnell24-IOT-Team 0:766454e296c3 128 return 1; // buffer allows overwriting by design, always true
group-Farnell24-IOT-Team 0:766454e296c3 129 }
group-Farnell24-IOT-Team 0:766454e296c3 130
group-Farnell24-IOT-Team 0:766454e296c3 131 int BufferedSpi::getc(void)
group-Farnell24-IOT-Team 0:766454e296c3 132 {
group-Farnell24-IOT-Team 0:766454e296c3 133 if (_rxbuf.available())
group-Farnell24-IOT-Team 0:766454e296c3 134 return _rxbuf;
group-Farnell24-IOT-Team 0:766454e296c3 135 else return -1;
group-Farnell24-IOT-Team 0:766454e296c3 136 }
group-Farnell24-IOT-Team 0:766454e296c3 137
group-Farnell24-IOT-Team 0:766454e296c3 138 int BufferedSpi::putc(int c)
group-Farnell24-IOT-Team 0:766454e296c3 139 {
group-Farnell24-IOT-Team 0:766454e296c3 140 _txbuf = (char)c;
group-Farnell24-IOT-Team 0:766454e296c3 141
group-Farnell24-IOT-Team 0:766454e296c3 142 return c;
group-Farnell24-IOT-Team 0:766454e296c3 143 }
group-Farnell24-IOT-Team 0:766454e296c3 144
group-Farnell24-IOT-Team 0:766454e296c3 145 void BufferedSpi::flush_txbuf(void)
group-Farnell24-IOT-Team 0:766454e296c3 146 {
group-Farnell24-IOT-Team 0:766454e296c3 147 _txbuf.clear();
group-Farnell24-IOT-Team 0:766454e296c3 148 }
group-Farnell24-IOT-Team 0:766454e296c3 149
group-Farnell24-IOT-Team 0:766454e296c3 150 int BufferedSpi::puts(const char *s)
group-Farnell24-IOT-Team 0:766454e296c3 151 {
group-Farnell24-IOT-Team 0:766454e296c3 152 if (s != NULL) {
group-Farnell24-IOT-Team 0:766454e296c3 153 const char* ptr = s;
group-Farnell24-IOT-Team 0:766454e296c3 154
group-Farnell24-IOT-Team 0:766454e296c3 155 while(*(ptr) != 0) {
group-Farnell24-IOT-Team 0:766454e296c3 156 _txbuf = *(ptr++);
group-Farnell24-IOT-Team 0:766454e296c3 157 }
group-Farnell24-IOT-Team 0:766454e296c3 158 _txbuf = '\n'; // done per puts definition
group-Farnell24-IOT-Team 0:766454e296c3 159 BufferedSpi::txIrq(); // only write to hardware in one place
group-Farnell24-IOT-Team 0:766454e296c3 160 return (ptr - s) + 1;
group-Farnell24-IOT-Team 0:766454e296c3 161 }
group-Farnell24-IOT-Team 0:766454e296c3 162 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 163 }
group-Farnell24-IOT-Team 0:766454e296c3 164
group-Farnell24-IOT-Team 0:766454e296c3 165 extern "C" size_t BufferedSpiThunk(void *buf_spi, const void *s, size_t length)
group-Farnell24-IOT-Team 0:766454e296c3 166 {
group-Farnell24-IOT-Team 0:766454e296c3 167 BufferedSpi *buffered_spi = (BufferedSpi *)buf_spi;
group-Farnell24-IOT-Team 0:766454e296c3 168 return buffered_spi->buffwrite(s, length);
group-Farnell24-IOT-Team 0:766454e296c3 169 }
group-Farnell24-IOT-Team 0:766454e296c3 170
group-Farnell24-IOT-Team 0:766454e296c3 171 int BufferedSpi::printf(const char* format, ...)
group-Farnell24-IOT-Team 0:766454e296c3 172 {
group-Farnell24-IOT-Team 0:766454e296c3 173 va_list arg;
group-Farnell24-IOT-Team 0:766454e296c3 174 va_start(arg, format);
group-Farnell24-IOT-Team 0:766454e296c3 175 int r = BufferedPrintfC((void*)this, this->_buf_size, format, arg);
group-Farnell24-IOT-Team 0:766454e296c3 176 va_end(arg);
group-Farnell24-IOT-Team 0:766454e296c3 177 return r;
group-Farnell24-IOT-Team 0:766454e296c3 178 }
group-Farnell24-IOT-Team 0:766454e296c3 179
group-Farnell24-IOT-Team 0:766454e296c3 180 ssize_t BufferedSpi::buffwrite(const void *s, size_t length)
group-Farnell24-IOT-Team 0:766454e296c3 181 {
group-Farnell24-IOT-Team 0:766454e296c3 182 /* flush buffer from previous message */
group-Farnell24-IOT-Team 0:766454e296c3 183 this->flush_txbuf();
group-Farnell24-IOT-Team 0:766454e296c3 184
group-Farnell24-IOT-Team 0:766454e296c3 185 if (wait_cmddata_rdy_high() < 0) {
group-Farnell24-IOT-Team 0:766454e296c3 186 debug_if(local_debug, "BufferedSpi::buffwrite timeout (%d)\r\n", _timeout);
group-Farnell24-IOT-Team 0:766454e296c3 187 return -1;
group-Farnell24-IOT-Team 0:766454e296c3 188 }
group-Farnell24-IOT-Team 0:766454e296c3 189
group-Farnell24-IOT-Team 0:766454e296c3 190 this->enable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 191
group-Farnell24-IOT-Team 0:766454e296c3 192 if (s != NULL && length > 0) {
group-Farnell24-IOT-Team 0:766454e296c3 193 /* 1st fill _txbuf */
group-Farnell24-IOT-Team 0:766454e296c3 194 const char* ptr = (const char*)s;
group-Farnell24-IOT-Team 0:766454e296c3 195 const char* end = ptr + length;
group-Farnell24-IOT-Team 0:766454e296c3 196
group-Farnell24-IOT-Team 0:766454e296c3 197 while (ptr != end) {
group-Farnell24-IOT-Team 0:766454e296c3 198 _txbuf = *(ptr++);
group-Farnell24-IOT-Team 0:766454e296c3 199 }
group-Farnell24-IOT-Team 0:766454e296c3 200 if (length&1) { /* padding to send the last char */
group-Farnell24-IOT-Team 0:766454e296c3 201 _txbuf = '\n';
group-Farnell24-IOT-Team 0:766454e296c3 202 length++;
group-Farnell24-IOT-Team 0:766454e296c3 203 }
group-Farnell24-IOT-Team 0:766454e296c3 204
group-Farnell24-IOT-Team 0:766454e296c3 205 /* 2nd write in SPI */
group-Farnell24-IOT-Team 0:766454e296c3 206 BufferedSpi::txIrq(); // only write to hardware in one place
group-Farnell24-IOT-Team 0:766454e296c3 207
group-Farnell24-IOT-Team 0:766454e296c3 208 this->disable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 209 return ptr - (const char*)s;
group-Farnell24-IOT-Team 0:766454e296c3 210 }
group-Farnell24-IOT-Team 0:766454e296c3 211 this->disable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 212
group-Farnell24-IOT-Team 0:766454e296c3 213 return 0;
group-Farnell24-IOT-Team 0:766454e296c3 214 }
group-Farnell24-IOT-Team 0:766454e296c3 215
group-Farnell24-IOT-Team 0:766454e296c3 216 ssize_t BufferedSpi::buffsend(size_t length)
group-Farnell24-IOT-Team 0:766454e296c3 217 {
group-Farnell24-IOT-Team 0:766454e296c3 218 /* wait for dataready = 1 */
group-Farnell24-IOT-Team 0:766454e296c3 219 if (wait_cmddata_rdy_high() < 0) {
group-Farnell24-IOT-Team 0:766454e296c3 220 debug_if(local_debug, "BufferedSpi::buffsend timeout (%d)\r\n", _timeout);
group-Farnell24-IOT-Team 0:766454e296c3 221 return -1;
group-Farnell24-IOT-Team 0:766454e296c3 222 }
group-Farnell24-IOT-Team 0:766454e296c3 223
group-Farnell24-IOT-Team 0:766454e296c3 224 this->enable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 225
group-Farnell24-IOT-Team 0:766454e296c3 226 /* _txbuffer is already filled with data to send */
group-Farnell24-IOT-Team 0:766454e296c3 227 /* check if _txbuffer needs padding to send the last char */
group-Farnell24-IOT-Team 0:766454e296c3 228 if (length & 1) {
group-Farnell24-IOT-Team 0:766454e296c3 229 _txbuf = '\n';
group-Farnell24-IOT-Team 0:766454e296c3 230 length++;
group-Farnell24-IOT-Team 0:766454e296c3 231 }
group-Farnell24-IOT-Team 0:766454e296c3 232 BufferedSpi::txIrq(); // only write to hardware in one place
group-Farnell24-IOT-Team 0:766454e296c3 233
group-Farnell24-IOT-Team 0:766454e296c3 234 this->disable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 235
group-Farnell24-IOT-Team 0:766454e296c3 236 return length;
group-Farnell24-IOT-Team 0:766454e296c3 237 }
group-Farnell24-IOT-Team 0:766454e296c3 238
group-Farnell24-IOT-Team 0:766454e296c3 239 ssize_t BufferedSpi::read()
group-Farnell24-IOT-Team 0:766454e296c3 240 {
group-Farnell24-IOT-Team 0:766454e296c3 241 return this->read(0);
group-Farnell24-IOT-Team 0:766454e296c3 242 }
group-Farnell24-IOT-Team 0:766454e296c3 243
group-Farnell24-IOT-Team 0:766454e296c3 244 ssize_t BufferedSpi::read(uint32_t max)
group-Farnell24-IOT-Team 0:766454e296c3 245 {
group-Farnell24-IOT-Team 0:766454e296c3 246 uint32_t len = 0;
group-Farnell24-IOT-Team 0:766454e296c3 247 int tmp;
group-Farnell24-IOT-Team 0:766454e296c3 248
group-Farnell24-IOT-Team 0:766454e296c3 249 disable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 250
group-Farnell24-IOT-Team 0:766454e296c3 251 /* wait for data ready is up */
group-Farnell24-IOT-Team 0:766454e296c3 252 if(wait_cmddata_rdy_rising_event() != 0) {
group-Farnell24-IOT-Team 0:766454e296c3 253 debug_if(local_debug, "BufferedSpi::read timeout (%d)\r\n", _timeout);
group-Farnell24-IOT-Team 0:766454e296c3 254 return -1;
group-Farnell24-IOT-Team 0:766454e296c3 255 }
group-Farnell24-IOT-Team 0:766454e296c3 256
group-Farnell24-IOT-Team 0:766454e296c3 257 enable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 258 while (dataready.read() == 1 && (len < (_buf_size - 1))) {
group-Farnell24-IOT-Team 0:766454e296c3 259 tmp = SPI::write(0xAA); // dummy write to receive 2 bytes
group-Farnell24-IOT-Team 0:766454e296c3 260
group-Farnell24-IOT-Team 0:766454e296c3 261 if (!((len == 0) && (tmp == 0x0A0D))) {
group-Farnell24-IOT-Team 0:766454e296c3 262 /* do not take into account the 2 firts \r \n char in the buffer */
group-Farnell24-IOT-Team 0:766454e296c3 263 if ((max == 0) || (len < max)) {
group-Farnell24-IOT-Team 0:766454e296c3 264 _rxbuf = (char)(tmp & 0x00FF);
group-Farnell24-IOT-Team 0:766454e296c3 265 _rxbuf = (char)((tmp >>8)& 0xFF);
group-Farnell24-IOT-Team 0:766454e296c3 266 len += 2;
group-Farnell24-IOT-Team 0:766454e296c3 267 }
group-Farnell24-IOT-Team 0:766454e296c3 268 }
group-Farnell24-IOT-Team 0:766454e296c3 269 }
group-Farnell24-IOT-Team 0:766454e296c3 270 disable_nss();
group-Farnell24-IOT-Team 0:766454e296c3 271
group-Farnell24-IOT-Team 0:766454e296c3 272 if (len >= _buf_size) {
group-Farnell24-IOT-Team 0:766454e296c3 273 debug_if(local_debug, "firmware ERROR ES_WIFI_ERROR_STUFFING_FOREVER\r\n");
group-Farnell24-IOT-Team 0:766454e296c3 274 return -1;
group-Farnell24-IOT-Team 0:766454e296c3 275 }
group-Farnell24-IOT-Team 0:766454e296c3 276
group-Farnell24-IOT-Team 0:766454e296c3 277 debug_if(local_debug, "SPI READ %d BYTES\r\n", len);
group-Farnell24-IOT-Team 0:766454e296c3 278
group-Farnell24-IOT-Team 0:766454e296c3 279 return len;
group-Farnell24-IOT-Team 0:766454e296c3 280 }
group-Farnell24-IOT-Team 0:766454e296c3 281
group-Farnell24-IOT-Team 0:766454e296c3 282 void BufferedSpi::txIrq(void)
group-Farnell24-IOT-Team 0:766454e296c3 283 { /* write everything available in the _txbuffer */
group-Farnell24-IOT-Team 0:766454e296c3 284 int value = 0;
group-Farnell24-IOT-Team 0:766454e296c3 285 int dbg_cnt = 0;
group-Farnell24-IOT-Team 0:766454e296c3 286 while (_txbuf.available() && (_txbuf.getNbAvailable()>0)) {
group-Farnell24-IOT-Team 0:766454e296c3 287 value = _txbuf.get();
group-Farnell24-IOT-Team 0:766454e296c3 288 if (_txbuf.available() && ((_txbuf.getNbAvailable()%2)!=0)) {
group-Farnell24-IOT-Team 0:766454e296c3 289 value |= ((_txbuf.get()<<8)&0XFF00);
group-Farnell24-IOT-Team 0:766454e296c3 290 SPI::write(value);
group-Farnell24-IOT-Team 0:766454e296c3 291 dbg_cnt++;
group-Farnell24-IOT-Team 0:766454e296c3 292 }
group-Farnell24-IOT-Team 0:766454e296c3 293 }
group-Farnell24-IOT-Team 0:766454e296c3 294 debug_if(local_debug, "SPI Sent %d BYTES\r\n", 2*dbg_cnt);
group-Farnell24-IOT-Team 0:766454e296c3 295 // disable the TX interrupt when there is nothing left to send
group-Farnell24-IOT-Team 0:766454e296c3 296 BufferedSpi::attach(NULL, BufferedSpi::TxIrq);
group-Farnell24-IOT-Team 0:766454e296c3 297 // trigger callback if necessary
group-Farnell24-IOT-Team 0:766454e296c3 298 if (_cbs[TxIrq]) {
group-Farnell24-IOT-Team 0:766454e296c3 299 _cbs[TxIrq]();
group-Farnell24-IOT-Team 0:766454e296c3 300 }
group-Farnell24-IOT-Team 0:766454e296c3 301 return;
group-Farnell24-IOT-Team 0:766454e296c3 302 }
group-Farnell24-IOT-Team 0:766454e296c3 303
group-Farnell24-IOT-Team 0:766454e296c3 304 void BufferedSpi::prime(void)
group-Farnell24-IOT-Team 0:766454e296c3 305 {
group-Farnell24-IOT-Team 0:766454e296c3 306 BufferedSpi::txIrq(); // only write to hardware in one place
group-Farnell24-IOT-Team 0:766454e296c3 307 return;
group-Farnell24-IOT-Team 0:766454e296c3 308 }
group-Farnell24-IOT-Team 0:766454e296c3 309
group-Farnell24-IOT-Team 0:766454e296c3 310 void BufferedSpi::attach(Callback<void()> func, IrqType type)
group-Farnell24-IOT-Team 0:766454e296c3 311 {
group-Farnell24-IOT-Team 0:766454e296c3 312 _cbs[type] = func;
group-Farnell24-IOT-Team 0:766454e296c3 313 }
group-Farnell24-IOT-Team 0:766454e296c3 314
group-Farnell24-IOT-Team 0:766454e296c3 315 void BufferedSpi::sigio(Callback<void()> func) {
group-Farnell24-IOT-Team 0:766454e296c3 316 core_util_critical_section_enter();
group-Farnell24-IOT-Team 0:766454e296c3 317 _sigio_cb = func;
group-Farnell24-IOT-Team 0:766454e296c3 318 if (_sigio_cb) {
group-Farnell24-IOT-Team 0:766454e296c3 319 if (_sigio_event == 1) {
group-Farnell24-IOT-Team 0:766454e296c3 320 _sigio_cb();
group-Farnell24-IOT-Team 0:766454e296c3 321 _sigio_event = 0;
group-Farnell24-IOT-Team 0:766454e296c3 322 }
group-Farnell24-IOT-Team 0:766454e296c3 323 }
group-Farnell24-IOT-Team 0:766454e296c3 324 core_util_critical_section_exit();
group-Farnell24-IOT-Team 0:766454e296c3 325 }
group-Farnell24-IOT-Team 0:766454e296c3 326