Skip to content
Snippets Groups Projects
Select Git revision
  • 5bf9b99c984fef82a05b586095bd4b0bc0f5e93e
  • master default protected
  • fix-warnings
  • tvbgone-fixes
  • genofire/ble-follow-py
  • schneider/ble-stability-new-phy-adv
  • schneider/ble-stability
  • msgctl/gfx_rle
  • schneider/ble-stability-new-phy
  • add_menu_vibration
  • plaetzchen/ios-workaround
  • blinkisync-as-preload
  • schneider/max30001-pycardium
  • schneider/max30001-epicaridum
  • schneider/max30001
  • schneider/stream-locks
  • schneider/fundamental-test
  • schneider/ble-buffers
  • schneider/maxim-sdk-update
  • ch3/splashscreen
  • koalo/bhi160-works-but-dirty
  • v1.11
  • v1.10
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
  • bootloader-v1
  • v0.0
36 results

ble.c

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    portexpander.c 1.81 KiB
    #include "portexpander.h"
    
    #include "i2c.h"
    
    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdbool.h>
    
    static bool detected = false;
    static uint8_t output_state;
    
    void portexpander_init(void)
    {
    	uint8_t addr = 0x21;
    	int ret;
    
    	// Enable pull-ups for buttons
    	uint8_t command[] = { 0x43, 0x68 };
    	ret = I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
    
    	if (ret != 2) {
    		printf("portexpander NOT detected\n");
    		detected = false;
    		return;
    	}
    	detected = true;
    
    	// Set _all_ outputs to open-drain to support the high side p-channel transistors.
    	command[0] = 0x4F;
    	command[1] = 0x01;
    	I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
    
    	// Enable outputs for the transistors, the LED and the LCD reset
    	command[0] = 0x03;
    	command[1] = 0x68;
    	I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
    
    	// Set outputs to high (i.e. open-drain)
    	output_state = 0x97;
    	command[0]   = 0x01;
    	command[1]   = output_state;
    	I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
    
    	// Turn on LEDs
    	// TODO: only turn on LEDs if value != 0,0,0 && dim > 0
    	command[0] = 0x01;
    	command[1] = 0x90;
    	I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 2, 0);
    }
    
    uint8_t portexpander_get(void)
    {
    	uint8_t addr      = 0x21;
    	uint8_t command[] = { 0x00 };
    	uint8_t buf       = 0xFF;
    
    	if (detected) {
    		I2C_MasterWrite(MXC_I2C1_BUS0, addr << 1, command, 1, 1);
    		I2C_MasterRead(MXC_I2C1_BUS0, addr << 1, &buf, 1, 0);
    	}
    
    	return buf;
    }
    
    bool portexpander_detected(void)
    {
    	return detected;
    }