Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Wed Oct 10 00:33:53 2018 +0000
Revision:
0:8fdf9a60065b
how to make mbed librry

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 /* mbed Microcontroller Library
kadonotakashi 0:8fdf9a60065b 2 * Copyright (c) 2006-2013 ARM Limited
kadonotakashi 0:8fdf9a60065b 3 *
kadonotakashi 0:8fdf9a60065b 4 * Licensed under the Apache License, Version 2.0 (the "License");
kadonotakashi 0:8fdf9a60065b 5 * you may not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 6 * You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 7 *
kadonotakashi 0:8fdf9a60065b 8 * http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 9 *
kadonotakashi 0:8fdf9a60065b 10 * Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 11 * distributed under the License is distributed on an "AS IS" BASIS,
kadonotakashi 0:8fdf9a60065b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 13 * See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 14 * limitations under the License.
kadonotakashi 0:8fdf9a60065b 15 */
kadonotakashi 0:8fdf9a60065b 16 #include "drivers/SerialBase.h"
kadonotakashi 0:8fdf9a60065b 17 #include "platform/mbed_wait_api.h"
kadonotakashi 0:8fdf9a60065b 18 #include "platform/mbed_critical.h"
kadonotakashi 0:8fdf9a60065b 19 #include "platform/mbed_power_mgmt.h"
kadonotakashi 0:8fdf9a60065b 20
kadonotakashi 0:8fdf9a60065b 21 #if DEVICE_SERIAL
kadonotakashi 0:8fdf9a60065b 22
kadonotakashi 0:8fdf9a60065b 23 namespace mbed {
kadonotakashi 0:8fdf9a60065b 24
kadonotakashi 0:8fdf9a60065b 25 SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
kadonotakashi 0:8fdf9a60065b 26 #if DEVICE_SERIAL_ASYNCH
kadonotakashi 0:8fdf9a60065b 27 _thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
kadonotakashi 0:8fdf9a60065b 28 _rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL),
kadonotakashi 0:8fdf9a60065b 29 _rx_callback(NULL),
kadonotakashi 0:8fdf9a60065b 30 #endif
kadonotakashi 0:8fdf9a60065b 31 _serial(), _baud(baud)
kadonotakashi 0:8fdf9a60065b 32 {
kadonotakashi 0:8fdf9a60065b 33 // No lock needed in the constructor
kadonotakashi 0:8fdf9a60065b 34
kadonotakashi 0:8fdf9a60065b 35 for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
kadonotakashi 0:8fdf9a60065b 36 _irq[i] = NULL;
kadonotakashi 0:8fdf9a60065b 37 }
kadonotakashi 0:8fdf9a60065b 38
kadonotakashi 0:8fdf9a60065b 39 serial_init(&_serial, tx, rx);
kadonotakashi 0:8fdf9a60065b 40 serial_baud(&_serial, _baud);
kadonotakashi 0:8fdf9a60065b 41 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
kadonotakashi 0:8fdf9a60065b 42 }
kadonotakashi 0:8fdf9a60065b 43
kadonotakashi 0:8fdf9a60065b 44 void SerialBase::baud(int baudrate)
kadonotakashi 0:8fdf9a60065b 45 {
kadonotakashi 0:8fdf9a60065b 46 lock();
kadonotakashi 0:8fdf9a60065b 47 serial_baud(&_serial, baudrate);
kadonotakashi 0:8fdf9a60065b 48 _baud = baudrate;
kadonotakashi 0:8fdf9a60065b 49 unlock();
kadonotakashi 0:8fdf9a60065b 50 }
kadonotakashi 0:8fdf9a60065b 51
kadonotakashi 0:8fdf9a60065b 52 void SerialBase::format(int bits, Parity parity, int stop_bits)
kadonotakashi 0:8fdf9a60065b 53 {
kadonotakashi 0:8fdf9a60065b 54 lock();
kadonotakashi 0:8fdf9a60065b 55 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
kadonotakashi 0:8fdf9a60065b 56 unlock();
kadonotakashi 0:8fdf9a60065b 57 }
kadonotakashi 0:8fdf9a60065b 58
kadonotakashi 0:8fdf9a60065b 59 int SerialBase::readable()
kadonotakashi 0:8fdf9a60065b 60 {
kadonotakashi 0:8fdf9a60065b 61 lock();
kadonotakashi 0:8fdf9a60065b 62 int ret = serial_readable(&_serial);
kadonotakashi 0:8fdf9a60065b 63 unlock();
kadonotakashi 0:8fdf9a60065b 64 return ret;
kadonotakashi 0:8fdf9a60065b 65 }
kadonotakashi 0:8fdf9a60065b 66
kadonotakashi 0:8fdf9a60065b 67
kadonotakashi 0:8fdf9a60065b 68 int SerialBase::writeable()
kadonotakashi 0:8fdf9a60065b 69 {
kadonotakashi 0:8fdf9a60065b 70 lock();
kadonotakashi 0:8fdf9a60065b 71 int ret = serial_writable(&_serial);
kadonotakashi 0:8fdf9a60065b 72 unlock();
kadonotakashi 0:8fdf9a60065b 73 return ret;
kadonotakashi 0:8fdf9a60065b 74 }
kadonotakashi 0:8fdf9a60065b 75
kadonotakashi 0:8fdf9a60065b 76 void SerialBase::attach(Callback<void()> func, IrqType type)
kadonotakashi 0:8fdf9a60065b 77 {
kadonotakashi 0:8fdf9a60065b 78 lock();
kadonotakashi 0:8fdf9a60065b 79 // Disable interrupts when attaching interrupt handler
kadonotakashi 0:8fdf9a60065b 80 core_util_critical_section_enter();
kadonotakashi 0:8fdf9a60065b 81 if (func) {
kadonotakashi 0:8fdf9a60065b 82 // lock deep sleep only the first time
kadonotakashi 0:8fdf9a60065b 83 if (!_irq[type]) {
kadonotakashi 0:8fdf9a60065b 84 sleep_manager_lock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 85 }
kadonotakashi 0:8fdf9a60065b 86 _irq[type] = func;
kadonotakashi 0:8fdf9a60065b 87 serial_irq_set(&_serial, (SerialIrq)type, 1);
kadonotakashi 0:8fdf9a60065b 88 } else {
kadonotakashi 0:8fdf9a60065b 89 // unlock deep sleep only the first time
kadonotakashi 0:8fdf9a60065b 90 if (_irq[type]) {
kadonotakashi 0:8fdf9a60065b 91 sleep_manager_unlock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 92 }
kadonotakashi 0:8fdf9a60065b 93 _irq[type] = NULL;
kadonotakashi 0:8fdf9a60065b 94 serial_irq_set(&_serial, (SerialIrq)type, 0);
kadonotakashi 0:8fdf9a60065b 95 }
kadonotakashi 0:8fdf9a60065b 96 core_util_critical_section_exit();
kadonotakashi 0:8fdf9a60065b 97 unlock();
kadonotakashi 0:8fdf9a60065b 98 }
kadonotakashi 0:8fdf9a60065b 99
kadonotakashi 0:8fdf9a60065b 100 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type)
kadonotakashi 0:8fdf9a60065b 101 {
kadonotakashi 0:8fdf9a60065b 102 SerialBase *handler = (SerialBase *)id;
kadonotakashi 0:8fdf9a60065b 103 if (handler->_irq[irq_type]) {
kadonotakashi 0:8fdf9a60065b 104 handler->_irq[irq_type]();
kadonotakashi 0:8fdf9a60065b 105 }
kadonotakashi 0:8fdf9a60065b 106 }
kadonotakashi 0:8fdf9a60065b 107
kadonotakashi 0:8fdf9a60065b 108 int SerialBase::_base_getc()
kadonotakashi 0:8fdf9a60065b 109 {
kadonotakashi 0:8fdf9a60065b 110 // Mutex is already held
kadonotakashi 0:8fdf9a60065b 111 return serial_getc(&_serial);
kadonotakashi 0:8fdf9a60065b 112 }
kadonotakashi 0:8fdf9a60065b 113
kadonotakashi 0:8fdf9a60065b 114 int SerialBase::_base_putc(int c)
kadonotakashi 0:8fdf9a60065b 115 {
kadonotakashi 0:8fdf9a60065b 116 // Mutex is already held
kadonotakashi 0:8fdf9a60065b 117 serial_putc(&_serial, c);
kadonotakashi 0:8fdf9a60065b 118 return c;
kadonotakashi 0:8fdf9a60065b 119 }
kadonotakashi 0:8fdf9a60065b 120
kadonotakashi 0:8fdf9a60065b 121 void SerialBase::send_break()
kadonotakashi 0:8fdf9a60065b 122 {
kadonotakashi 0:8fdf9a60065b 123 lock();
kadonotakashi 0:8fdf9a60065b 124 // Wait for 1.5 frames before clearing the break condition
kadonotakashi 0:8fdf9a60065b 125 // This will have different effects on our platforms, but should
kadonotakashi 0:8fdf9a60065b 126 // ensure that we keep the break active for at least one frame.
kadonotakashi 0:8fdf9a60065b 127 // We consider a full frame (1 start bit + 8 data bits bits +
kadonotakashi 0:8fdf9a60065b 128 // 1 parity bit + 2 stop bits = 12 bits) for computation.
kadonotakashi 0:8fdf9a60065b 129 // One bit time (in us) = 1000000/_baud
kadonotakashi 0:8fdf9a60065b 130 // Twelve bits: 12000000/baud delay
kadonotakashi 0:8fdf9a60065b 131 // 1.5 frames: 18000000/baud delay
kadonotakashi 0:8fdf9a60065b 132 serial_break_set(&_serial);
kadonotakashi 0:8fdf9a60065b 133 wait_us(18000000 / _baud);
kadonotakashi 0:8fdf9a60065b 134 serial_break_clear(&_serial);
kadonotakashi 0:8fdf9a60065b 135 unlock();
kadonotakashi 0:8fdf9a60065b 136 }
kadonotakashi 0:8fdf9a60065b 137
kadonotakashi 0:8fdf9a60065b 138 void SerialBase::lock()
kadonotakashi 0:8fdf9a60065b 139 {
kadonotakashi 0:8fdf9a60065b 140 // Stub
kadonotakashi 0:8fdf9a60065b 141 }
kadonotakashi 0:8fdf9a60065b 142
kadonotakashi 0:8fdf9a60065b 143 void SerialBase:: unlock()
kadonotakashi 0:8fdf9a60065b 144 {
kadonotakashi 0:8fdf9a60065b 145 // Stub
kadonotakashi 0:8fdf9a60065b 146 }
kadonotakashi 0:8fdf9a60065b 147
kadonotakashi 0:8fdf9a60065b 148 SerialBase::~SerialBase()
kadonotakashi 0:8fdf9a60065b 149 {
kadonotakashi 0:8fdf9a60065b 150 // No lock needed in destructor
kadonotakashi 0:8fdf9a60065b 151
kadonotakashi 0:8fdf9a60065b 152 // Detaching interrupts releases the sleep lock if it was locked
kadonotakashi 0:8fdf9a60065b 153 for (int irq = 0; irq < IrqCnt; irq++) {
kadonotakashi 0:8fdf9a60065b 154 attach(NULL, (IrqType)irq);
kadonotakashi 0:8fdf9a60065b 155 }
kadonotakashi 0:8fdf9a60065b 156 }
kadonotakashi 0:8fdf9a60065b 157
kadonotakashi 0:8fdf9a60065b 158 #if DEVICE_SERIAL_FC
kadonotakashi 0:8fdf9a60065b 159 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)
kadonotakashi 0:8fdf9a60065b 160 {
kadonotakashi 0:8fdf9a60065b 161 lock();
kadonotakashi 0:8fdf9a60065b 162 FlowControl flow_type = (FlowControl)type;
kadonotakashi 0:8fdf9a60065b 163 switch (type) {
kadonotakashi 0:8fdf9a60065b 164 case RTS:
kadonotakashi 0:8fdf9a60065b 165 serial_set_flow_control(&_serial, flow_type, flow1, NC);
kadonotakashi 0:8fdf9a60065b 166 break;
kadonotakashi 0:8fdf9a60065b 167
kadonotakashi 0:8fdf9a60065b 168 case CTS:
kadonotakashi 0:8fdf9a60065b 169 serial_set_flow_control(&_serial, flow_type, NC, flow1);
kadonotakashi 0:8fdf9a60065b 170 break;
kadonotakashi 0:8fdf9a60065b 171
kadonotakashi 0:8fdf9a60065b 172 case RTSCTS:
kadonotakashi 0:8fdf9a60065b 173 case Disabled:
kadonotakashi 0:8fdf9a60065b 174 serial_set_flow_control(&_serial, flow_type, flow1, flow2);
kadonotakashi 0:8fdf9a60065b 175 break;
kadonotakashi 0:8fdf9a60065b 176
kadonotakashi 0:8fdf9a60065b 177 default:
kadonotakashi 0:8fdf9a60065b 178 break;
kadonotakashi 0:8fdf9a60065b 179 }
kadonotakashi 0:8fdf9a60065b 180 unlock();
kadonotakashi 0:8fdf9a60065b 181 }
kadonotakashi 0:8fdf9a60065b 182 #endif
kadonotakashi 0:8fdf9a60065b 183
kadonotakashi 0:8fdf9a60065b 184 #if DEVICE_SERIAL_ASYNCH
kadonotakashi 0:8fdf9a60065b 185
kadonotakashi 0:8fdf9a60065b 186 int SerialBase::write(const uint8_t *buffer, int length, const event_callback_t &callback, int event)
kadonotakashi 0:8fdf9a60065b 187 {
kadonotakashi 0:8fdf9a60065b 188 if (serial_tx_active(&_serial)) {
kadonotakashi 0:8fdf9a60065b 189 return -1; // transaction ongoing
kadonotakashi 0:8fdf9a60065b 190 }
kadonotakashi 0:8fdf9a60065b 191 start_write((void *)buffer, length, 8, callback, event);
kadonotakashi 0:8fdf9a60065b 192 return 0;
kadonotakashi 0:8fdf9a60065b 193 }
kadonotakashi 0:8fdf9a60065b 194
kadonotakashi 0:8fdf9a60065b 195 int SerialBase::write(const uint16_t *buffer, int length, const event_callback_t &callback, int event)
kadonotakashi 0:8fdf9a60065b 196 {
kadonotakashi 0:8fdf9a60065b 197 if (serial_tx_active(&_serial)) {
kadonotakashi 0:8fdf9a60065b 198 return -1; // transaction ongoing
kadonotakashi 0:8fdf9a60065b 199 }
kadonotakashi 0:8fdf9a60065b 200 start_write((void *)buffer, length, 16, callback, event);
kadonotakashi 0:8fdf9a60065b 201 return 0;
kadonotakashi 0:8fdf9a60065b 202 }
kadonotakashi 0:8fdf9a60065b 203
kadonotakashi 0:8fdf9a60065b 204 void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event)
kadonotakashi 0:8fdf9a60065b 205 {
kadonotakashi 0:8fdf9a60065b 206 _tx_callback = callback;
kadonotakashi 0:8fdf9a60065b 207
kadonotakashi 0:8fdf9a60065b 208 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
kadonotakashi 0:8fdf9a60065b 209 sleep_manager_lock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 210 serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage);
kadonotakashi 0:8fdf9a60065b 211 }
kadonotakashi 0:8fdf9a60065b 212
kadonotakashi 0:8fdf9a60065b 213 void SerialBase::abort_write(void)
kadonotakashi 0:8fdf9a60065b 214 {
kadonotakashi 0:8fdf9a60065b 215 // rx might still be active
kadonotakashi 0:8fdf9a60065b 216 if (_rx_callback) {
kadonotakashi 0:8fdf9a60065b 217 sleep_manager_unlock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 218 }
kadonotakashi 0:8fdf9a60065b 219 _tx_callback = NULL;
kadonotakashi 0:8fdf9a60065b 220 serial_tx_abort_asynch(&_serial);
kadonotakashi 0:8fdf9a60065b 221 }
kadonotakashi 0:8fdf9a60065b 222
kadonotakashi 0:8fdf9a60065b 223 void SerialBase::abort_read(void)
kadonotakashi 0:8fdf9a60065b 224 {
kadonotakashi 0:8fdf9a60065b 225 // tx might still be active
kadonotakashi 0:8fdf9a60065b 226 if (_tx_callback) {
kadonotakashi 0:8fdf9a60065b 227 sleep_manager_unlock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 228 }
kadonotakashi 0:8fdf9a60065b 229 _rx_callback = NULL;
kadonotakashi 0:8fdf9a60065b 230 serial_rx_abort_asynch(&_serial);
kadonotakashi 0:8fdf9a60065b 231 }
kadonotakashi 0:8fdf9a60065b 232
kadonotakashi 0:8fdf9a60065b 233 int SerialBase::set_dma_usage_tx(DMAUsage usage)
kadonotakashi 0:8fdf9a60065b 234 {
kadonotakashi 0:8fdf9a60065b 235 if (serial_tx_active(&_serial)) {
kadonotakashi 0:8fdf9a60065b 236 return -1;
kadonotakashi 0:8fdf9a60065b 237 }
kadonotakashi 0:8fdf9a60065b 238 _tx_usage = usage;
kadonotakashi 0:8fdf9a60065b 239 return 0;
kadonotakashi 0:8fdf9a60065b 240 }
kadonotakashi 0:8fdf9a60065b 241
kadonotakashi 0:8fdf9a60065b 242 int SerialBase::set_dma_usage_rx(DMAUsage usage)
kadonotakashi 0:8fdf9a60065b 243 {
kadonotakashi 0:8fdf9a60065b 244 if (serial_tx_active(&_serial)) {
kadonotakashi 0:8fdf9a60065b 245 return -1;
kadonotakashi 0:8fdf9a60065b 246 }
kadonotakashi 0:8fdf9a60065b 247 _rx_usage = usage;
kadonotakashi 0:8fdf9a60065b 248 return 0;
kadonotakashi 0:8fdf9a60065b 249 }
kadonotakashi 0:8fdf9a60065b 250
kadonotakashi 0:8fdf9a60065b 251 int SerialBase::read(uint8_t *buffer, int length, const event_callback_t &callback, int event, unsigned char char_match)
kadonotakashi 0:8fdf9a60065b 252 {
kadonotakashi 0:8fdf9a60065b 253 if (serial_rx_active(&_serial)) {
kadonotakashi 0:8fdf9a60065b 254 return -1; // transaction ongoing
kadonotakashi 0:8fdf9a60065b 255 }
kadonotakashi 0:8fdf9a60065b 256 start_read((void *)buffer, length, 8, callback, event, char_match);
kadonotakashi 0:8fdf9a60065b 257 return 0;
kadonotakashi 0:8fdf9a60065b 258 }
kadonotakashi 0:8fdf9a60065b 259
kadonotakashi 0:8fdf9a60065b 260
kadonotakashi 0:8fdf9a60065b 261 int SerialBase::read(uint16_t *buffer, int length, const event_callback_t &callback, int event, unsigned char char_match)
kadonotakashi 0:8fdf9a60065b 262 {
kadonotakashi 0:8fdf9a60065b 263 if (serial_rx_active(&_serial)) {
kadonotakashi 0:8fdf9a60065b 264 return -1; // transaction ongoing
kadonotakashi 0:8fdf9a60065b 265 }
kadonotakashi 0:8fdf9a60065b 266 start_read((void *)buffer, length, 16, callback, event, char_match);
kadonotakashi 0:8fdf9a60065b 267 return 0;
kadonotakashi 0:8fdf9a60065b 268 }
kadonotakashi 0:8fdf9a60065b 269
kadonotakashi 0:8fdf9a60065b 270
kadonotakashi 0:8fdf9a60065b 271 void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match)
kadonotakashi 0:8fdf9a60065b 272 {
kadonotakashi 0:8fdf9a60065b 273 _rx_callback = callback;
kadonotakashi 0:8fdf9a60065b 274 _thunk_irq.callback(&SerialBase::interrupt_handler_asynch);
kadonotakashi 0:8fdf9a60065b 275 sleep_manager_lock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 276 serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage);
kadonotakashi 0:8fdf9a60065b 277 }
kadonotakashi 0:8fdf9a60065b 278
kadonotakashi 0:8fdf9a60065b 279 void SerialBase::interrupt_handler_asynch(void)
kadonotakashi 0:8fdf9a60065b 280 {
kadonotakashi 0:8fdf9a60065b 281 int event = serial_irq_handler_asynch(&_serial);
kadonotakashi 0:8fdf9a60065b 282 int rx_event = event & SERIAL_EVENT_RX_MASK;
kadonotakashi 0:8fdf9a60065b 283 bool unlock_deepsleep = false;
kadonotakashi 0:8fdf9a60065b 284
kadonotakashi 0:8fdf9a60065b 285 if (_rx_callback && rx_event) {
kadonotakashi 0:8fdf9a60065b 286 unlock_deepsleep = true;
kadonotakashi 0:8fdf9a60065b 287 _rx_callback.call(rx_event);
kadonotakashi 0:8fdf9a60065b 288 }
kadonotakashi 0:8fdf9a60065b 289
kadonotakashi 0:8fdf9a60065b 290 int tx_event = event & SERIAL_EVENT_TX_MASK;
kadonotakashi 0:8fdf9a60065b 291 if (_tx_callback && tx_event) {
kadonotakashi 0:8fdf9a60065b 292 unlock_deepsleep = true;
kadonotakashi 0:8fdf9a60065b 293 _tx_callback.call(tx_event);
kadonotakashi 0:8fdf9a60065b 294 }
kadonotakashi 0:8fdf9a60065b 295 // unlock if tx or rx events are generated
kadonotakashi 0:8fdf9a60065b 296 if (unlock_deepsleep) {
kadonotakashi 0:8fdf9a60065b 297 sleep_manager_unlock_deep_sleep();
kadonotakashi 0:8fdf9a60065b 298 }
kadonotakashi 0:8fdf9a60065b 299 }
kadonotakashi 0:8fdf9a60065b 300
kadonotakashi 0:8fdf9a60065b 301 #endif
kadonotakashi 0:8fdf9a60065b 302
kadonotakashi 0:8fdf9a60065b 303 } // namespace mbed
kadonotakashi 0:8fdf9a60065b 304
kadonotakashi 0:8fdf9a60065b 305 #endif