2018-08-15 14:01:01 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/displayio/FourWire.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-04-08 19:58:50 -04:00
|
|
|
#include "py/gc.h"
|
2018-08-15 14:01:01 -04:00
|
|
|
#include "shared-bindings/busio/SPI.h"
|
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2019-11-15 12:47:00 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2019-06-17 20:48:05 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2019-03-27 23:11:32 -04:00
|
|
|
#include "shared-bindings/time/__init__.h"
|
2019-08-23 18:27:21 -04:00
|
|
|
#include "shared-module/displayio/display_core.h"
|
2018-08-15 14:01:01 -04:00
|
|
|
|
|
|
|
#include "tick.h"
|
|
|
|
|
|
|
|
void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
|
2019-01-16 15:04:42 -05:00
|
|
|
busio_spi_obj_t* spi, const mcu_pin_obj_t* command,
|
2019-06-17 20:48:05 -04:00
|
|
|
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate) {
|
2018-08-15 14:01:01 -04:00
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
self->bus = spi;
|
|
|
|
common_hal_busio_spi_never_reset(self->bus);
|
2019-04-08 20:15:08 -04:00
|
|
|
// Our object is statically allocated off the heap so make sure the bus object lives to the end
|
|
|
|
// of the heap as well.
|
2019-04-08 19:58:50 -04:00
|
|
|
gc_never_free(self->bus);
|
2019-01-09 13:19:07 -05:00
|
|
|
|
2019-06-17 20:48:05 -04:00
|
|
|
self->frequency = baudrate;
|
2019-12-05 11:44:21 -05:00
|
|
|
self->polarity = 0;
|
|
|
|
self->phase = 0;
|
2019-01-09 13:19:07 -05:00
|
|
|
|
2018-08-15 14:01:01 -04:00
|
|
|
common_hal_digitalio_digitalinout_construct(&self->command, command);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
|
|
|
|
common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
|
|
|
|
|
2019-08-21 14:49:49 -04:00
|
|
|
self->reset.base.type = &mp_type_NoneType;
|
2019-01-17 21:19:07 -05:00
|
|
|
if (reset != NULL) {
|
2019-08-21 14:49:49 -04:00
|
|
|
self->reset.base.type = &digitalio_digitalinout_type;
|
2019-01-17 21:19:07 -05:00
|
|
|
common_hal_digitalio_digitalinout_construct(&self->reset, reset);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL);
|
2019-11-15 12:47:00 -05:00
|
|
|
common_hal_never_reset_pin(reset);
|
2019-08-02 19:17:38 -04:00
|
|
|
common_hal_displayio_fourwire_reset(self);
|
2019-01-17 21:19:07 -05:00
|
|
|
}
|
2018-08-15 14:01:01 -04:00
|
|
|
|
2019-11-15 12:47:00 -05:00
|
|
|
common_hal_never_reset_pin(command);
|
|
|
|
common_hal_never_reset_pin(chip_select);
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
|
2019-01-17 03:20:16 -05:00
|
|
|
void common_hal_displayio_fourwire_deinit(displayio_fourwire_obj_t* self) {
|
|
|
|
if (self->bus == &self->inline_bus) {
|
|
|
|
common_hal_busio_spi_deinit(self->bus);
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:47:00 -05:00
|
|
|
common_hal_reset_pin(self->command.pin);
|
|
|
|
common_hal_reset_pin(self->chip_select.pin);
|
|
|
|
common_hal_reset_pin(self->reset.pin);
|
2019-01-17 03:20:16 -05:00
|
|
|
}
|
|
|
|
|
2019-08-21 14:49:49 -04:00
|
|
|
bool common_hal_displayio_fourwire_reset(mp_obj_t obj) {
|
2019-08-02 19:17:38 -04:00
|
|
|
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
|
2019-08-21 14:49:49 -04:00
|
|
|
if (self->reset.base.type == &mp_type_NoneType) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->reset, false);
|
|
|
|
common_hal_time_delay_ms(1);
|
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->reset, true);
|
|
|
|
common_hal_time_delay_ms(1);
|
2019-08-21 14:49:49 -04:00
|
|
|
return true;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_displayio_fourwire_bus_free(mp_obj_t obj) {
|
|
|
|
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
|
|
|
|
if (!common_hal_busio_spi_try_lock(self->bus)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
common_hal_busio_spi_unlock(self->bus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) {
|
|
|
|
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
|
|
|
|
if (!common_hal_busio_spi_try_lock(self->bus)) {
|
2018-08-15 14:01:01 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-06 09:08:31 -04:00
|
|
|
common_hal_busio_spi_configure(self->bus, self->frequency, self->polarity,
|
|
|
|
self->phase, 8);
|
2018-08-15 14:01:01 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:27:21 -04:00
|
|
|
void common_hal_displayio_fourwire_send(mp_obj_t obj, display_byte_type_t data_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length) {
|
2019-01-16 15:04:42 -05:00
|
|
|
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
|
2019-08-23 18:27:21 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA);
|
|
|
|
if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) {
|
2019-07-05 22:01:54 -04:00
|
|
|
// Toggle chip select after each command byte in case the display driver
|
|
|
|
// IC latches commands based on it.
|
|
|
|
for (size_t i = 0; i < data_length; i++) {
|
|
|
|
common_hal_busio_spi_write(self->bus, &data[i], 1);
|
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
|
|
|
|
common_hal_mcu_delay_us(1);
|
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
common_hal_busio_spi_write(self->bus, data, data_length);
|
2019-03-27 23:11:32 -04:00
|
|
|
}
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
void common_hal_displayio_fourwire_end_transaction(mp_obj_t obj) {
|
|
|
|
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
|
2018-08-15 14:01:01 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
|
2019-01-16 15:04:42 -05:00
|
|
|
common_hal_busio_spi_unlock(self->bus);
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|