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.
|
|
|
|
*/
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
#include "shared-bindings/fourwire/FourWire.h"
|
2018-08-15 14:01:01 -04:00
|
|
|
|
|
|
|
#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"
|
2018-08-15 14:01:01 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_fourwire_fourwire_construct(fourwire_fourwire_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
busio_spi_obj_t *spi, const mcu_pin_obj_t *command,
|
|
|
|
const mcu_pin_obj_t *chip_select, const mcu_pin_obj_t *reset, uint32_t baudrate,
|
2020-03-25 14:22:46 -04:00
|
|
|
uint8_t polarity, uint8_t phase) {
|
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-01-09 13:19:07 -05:00
|
|
|
|
2019-06-17 20:48:05 -04:00
|
|
|
self->frequency = baudrate;
|
2020-03-25 14:22:46 -04:00
|
|
|
self->polarity = polarity;
|
|
|
|
self->phase = phase;
|
2019-01-09 13:19:07 -05:00
|
|
|
|
2018-08-15 14:01:01 -04:00
|
|
|
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);
|
|
|
|
|
2022-03-03 16:42:50 -05:00
|
|
|
self->command.base.type = &mp_type_NoneType;
|
|
|
|
if (command != NULL) {
|
|
|
|
self->command.base.type = &digitalio_digitalinout_type;
|
|
|
|
common_hal_digitalio_digitalinout_construct(&self->command, command);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
|
|
|
|
common_hal_never_reset_pin(command);
|
|
|
|
}
|
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);
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_fourwire_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(chip_select);
|
2018-08-15 14:01:01 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_fourwire_fourwire_deinit(fourwire_fourwire_obj_t *self) {
|
2019-01-17 03:20:16 -05:00
|
|
|
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);
|
2020-12-28 09:34:00 -05:00
|
|
|
common_hal_reset_pin(self->reset.pin);
|
2019-01-17 03:20:16 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_fourwire_fourwire_reset(mp_obj_t obj) {
|
|
|
|
fourwire_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);
|
2020-09-22 11:25:31 -04:00
|
|
|
common_hal_mcu_delay_us(1000);
|
2019-08-02 19:17:38 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->reset, true);
|
2020-09-22 11:25:31 -04:00
|
|
|
common_hal_mcu_delay_us(1000);
|
2019-08-21 14:49:49 -04:00
|
|
|
return true;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_fourwire_fourwire_bus_free(mp_obj_t obj) {
|
|
|
|
fourwire_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-08-02 19:17:38 -04:00
|
|
|
if (!common_hal_busio_spi_try_lock(self->bus)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
common_hal_busio_spi_unlock(self->bus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_fourwire_fourwire_begin_transaction(mp_obj_t obj) {
|
|
|
|
fourwire_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-01-16 15:04:42 -05:00
|
|
|
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,
|
2021-03-15 09:57:36 -04:00
|
|
|
self->phase, 8);
|
2018-08-15 14:01:01 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_fourwire_fourwire_send(mp_obj_t obj, display_byte_type_t data_type,
|
2021-03-15 09:57:36 -04:00
|
|
|
display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) {
|
2023-10-17 19:09:47 -04:00
|
|
|
fourwire_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2022-03-03 16:42:50 -05:00
|
|
|
if (self->command.base.type == &mp_type_NoneType) {
|
|
|
|
// When the data/command pin is not specified, we simulate a 9-bit SPI mode, by
|
|
|
|
// adding a data/command bit to every byte, and then splitting the resulting data back
|
|
|
|
// into 8-bit chunks for transmission. If the length of the data being transmitted
|
|
|
|
// is not a multiple of 8, there will be additional bits at the end of the
|
|
|
|
// transmission. We toggle the CS pin to make the receiver discard them.
|
|
|
|
uint8_t buffer = 0;
|
|
|
|
uint8_t bits = 0;
|
|
|
|
uint8_t dc = (data_type == DISPLAY_DATA);
|
|
|
|
|
2019-07-05 22:01:54 -04:00
|
|
|
for (size_t i = 0; i < data_length; i++) {
|
2022-03-03 16:42:50 -05:00
|
|
|
bits = (bits + 1) % 8;
|
|
|
|
|
|
|
|
if (bits == 0) {
|
|
|
|
// send the previous byte and the dc bit
|
|
|
|
// we will send the current byte later
|
|
|
|
buffer = (buffer << 1) | dc;
|
|
|
|
common_hal_busio_spi_write(self->bus, &buffer, 1);
|
|
|
|
// send the current byte, because previous byte already filled all bits
|
|
|
|
common_hal_busio_spi_write(self->bus, &data[i], 1);
|
|
|
|
} else {
|
|
|
|
// send remaining bits from previous byte, dc and beginning of current byte
|
|
|
|
buffer = (buffer << (9 - bits)) | (dc << (8 - bits)) | (data[i] >> bits);
|
|
|
|
common_hal_busio_spi_write(self->bus, &buffer, 1);
|
|
|
|
}
|
|
|
|
// save the current byte
|
|
|
|
buffer = data[i];
|
|
|
|
}
|
|
|
|
// send any remaining bits
|
|
|
|
if (bits > 0) {
|
|
|
|
buffer = buffer << (8 - bits);
|
|
|
|
common_hal_busio_spi_write(self->bus, &buffer, 1);
|
|
|
|
// toggle CS to discard superfluous bits
|
2019-07-05 22:01:54 -04:00
|
|
|
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 {
|
2022-03-03 16:42:50 -05:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA);
|
|
|
|
if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_fourwire_fourwire_end_transaction(mp_obj_t obj) {
|
|
|
|
fourwire_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
|
|
|
}
|
2023-10-02 19:49:31 -04:00
|
|
|
|
|
|
|
void common_hal_fourwire_fourwire_collect_ptrs(mp_obj_t obj) {
|
|
|
|
fourwire_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
|
|
|
|
gc_collect_ptr((void *)self->bus);
|
|
|
|
}
|