annotate Scan/ISSILed/led_scan.c @ 356:58d2c5bf95eb

Adding ISSILed API calls and default brightness - Adds override functionality in main module
author Jacob Alexander <haata@kiibohd.com>
date Sun, 02 Aug 2015 16:28:21 -0700
parents 66eccdd9ced5
children 86b937945313
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1 /* Copyright (C) 2014-2015 by Jacob Alexander
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
2 *
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
3 * This file is free software: you can redistribute it and/or modify
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
4 * it under the terms of the GNU General Public License as published by
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
5 * the Free Software Foundation, either version 3 of the License, or
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
6 * (at your option) any later version.
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
7 *
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
8 * This file is distributed in the hope that it will be useful,
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
11 * GNU General Public License for more details.
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
12 *
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
13 * You should have received a copy of the GNU General Public License
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
14 * along with this file. If not, see <http://www.gnu.org/licenses/>.
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
15 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
16
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
17 // ----- Includes -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
18
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
19 // Compiler Includes
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
20 #include <Lib/ScanLib.h>
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
21
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
22 // Project Includes
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
23 #include <cli.h>
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
24 #include <kll.h>
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
25 #include <led.h>
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
26 #include <print.h>
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
27
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
28 // Local Includes
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
29 #include "led_scan.h"
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
30
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
31
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
32
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
33 // ----- Defines -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
34
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
35 #define I2C_TxBufferLength 300
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
36 #define I2C_RxBufferLength 8
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
37
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
38 #define LED_BufferLength 144
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
39
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
40
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
41 // ----- Structs -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
42
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
43 typedef struct I2C_Buffer {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
44 uint16_t head;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
45 uint16_t tail;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
46 uint8_t sequencePos;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
47 uint16_t size;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
48 uint8_t *buffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
49 } I2C_Buffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
50
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
51 typedef struct LED_Buffer {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
52 uint8_t buffer[LED_BufferLength];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
53 } LED_Buffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
54
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
55
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
56
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
57 // ----- Function Declarations -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
58
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
59 // CLI Functions
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
60 void cliFunc_i2cRecv ( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
61 void cliFunc_i2cSend ( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
62 void cliFunc_ledRPage( char* args );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
63 void cliFunc_ledStart( char* args );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
64 void cliFunc_ledTest ( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
65 void cliFunc_ledWPage( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
66 void cliFunc_ledZero ( char* args );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
67
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
68 uint8_t I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
69 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
70 uint16_t I2C_BufferLen( I2C_Buffer *buffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
71 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
72
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
73
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
74
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
75 // ----- Variables -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
76
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
77 // Scan Module command dictionary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
78 CLIDict_Entry( i2cRecv, "Send I2C sequence of bytes and expect a reply of 1 byte on the last sequence." NL "\t\tUse |'s to split sequences with a stop." );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
79 CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
80 CLIDict_Entry( ledRPage, "Read the given register page." );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
81 CLIDict_Entry( ledStart, "Disable software shutdown." );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
82 CLIDict_Entry( ledTest, "Test out the led pages." );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
83 CLIDict_Entry( ledWPage, "Write to given register page starting at address. i.e. 0x2 0x24 0xF0 0x12" );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
84 CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
85
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
86 CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
87 CLIDict_Item( i2cRecv ),
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
88 CLIDict_Item( i2cSend ),
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
89 CLIDict_Item( ledRPage ),
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
90 CLIDict_Item( ledStart ),
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
91 CLIDict_Item( ledTest ),
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
92 CLIDict_Item( ledWPage ),
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
93 CLIDict_Item( ledZero ),
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
94 { 0, 0, 0 } // Null entry for dictionary end
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
95 };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
96
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
97
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
98
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
99 // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
100 // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
101 volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
102 volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
103
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
104 volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
105 volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
106
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
107 LED_Buffer LED_pageBuffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
108
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
109 // A bit mask determining which LEDs are enabled in the ISSI chip
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
110 const uint8_t LED_ledEnableMask1[] = {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
111 0xE8, // I2C address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
112 0x00, // Starting register address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
113 ISSILedMask1_define
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
114 };
341
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
115
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
116 // Default LED brightness
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
117 const uint8_t LED_defaultBrightness1[] = {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
118 0xE8, // I2C address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
119 0x24, // Starting register address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
120 ISSILedBrightness1_define
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
121 };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
122
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
123
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
124
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
125 // ----- Interrupt Functions -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
126
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
127 void i2c0_isr()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
128 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
129 cli(); // Disable Interrupts
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
130
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
131 uint8_t status = I2C0_S; // Read I2C Bus status
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
132
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
133 // Master Mode Transmit
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
134 if ( I2C0_C1 & I2C_C1_TX )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
135 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
136 // Check current use of the I2C bus
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
137 // Currently sending data
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
138 if ( I2C_TxBuffer.sequencePos > 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
139 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
140 // Make sure slave sent an ACK
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
141 if ( status & I2C_S_RXAK )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
142 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
143 // NACK Detected, disable interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
144 erro_print("I2C NAK detected...");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
145 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
146
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
147 // Abort Tx Buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
148 I2C_TxBuffer.head = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
149 I2C_TxBuffer.tail = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
150 I2C_TxBuffer.sequencePos = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
151 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
152 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
153 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
154 // Transmit byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
155 I2C0_D = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
156 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
157 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
158 // Receiving data
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
159 else if ( I2C_RxBuffer.sequencePos > 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
160 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
161 // Master Receive, addr sent
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
162 if ( status & I2C_S_ARBL )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
163 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
164 // Arbitration Lost
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
165 erro_print("Arbitration lost...");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
166 // TODO Abort Rx
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
167
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
168 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
169 I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
170 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
171 if ( status & I2C_S_RXAK )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
172 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
173 // Slave Address NACK Detected, disable interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
174 erro_print("Slave Address I2C NAK detected...");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
175 // TODO Abort Rx
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
176
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
177 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
178 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
179 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
180 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
181 dbug_print("Attempting to read byte");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
182 I2C0_C1 = I2C_RxBuffer.sequencePos == 1
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
183 ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
184 : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
185 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
186 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
187 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
188 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
189 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
190 dbug_msg("STOP - ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
191 printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
192 print(NL);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
193 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
194
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
195 // Delay around STOP to make sure it actually happens...
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
196 delayMicroseconds( 1 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
197 I2C0_C1 = I2C_C1_IICEN; // Send STOP
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
198 delayMicroseconds( 7 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
199
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
200 // If there is another sequence, start sending
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
201 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
202 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
203 // Clear status flags
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
204 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
205
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
206 // Wait...till the master dies
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
207 while ( I2C0_S & I2C_S_BUSY );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
208
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
209 // Enable I2C interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
210 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
211
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
212 // Transmit byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
213 I2C0_D = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
214 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
215 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
216 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
217 // Master Mode Receive
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
218 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
219 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
220 // XXX Do we need to handle 2nd last byte?
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
221 //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
222
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
223 // Last byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
224 if ( I2C_TxBuffer.sequencePos <= 1 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
225 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
226 // Change to Tx mode
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
227 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
228
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
229 // Grab last byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
230 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
231
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
232 delayMicroseconds( 1 ); // Should be enough time before issuing the stop
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
233 I2C0_C1 = I2C_C1_IICEN; // Send STOP
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
234 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
235 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
236 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
237 // Retrieve data
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
238 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
239 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
240 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
241
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
242 I2C0_S = I2C_S_IICIF; // Clear interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
243
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
244 sei(); // Re-enable Interrupts
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
245 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
246
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
247
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
248
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
249 // ----- Functions -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
250
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
251 inline void I2C_setup()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
252 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
253 // Enable I2C internal clock
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
254 SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
255
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
256 // External pull-up resistor
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
257 PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
258 PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
259
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
260 // SCL Frequency Divider
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
261 // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
262 I2C0_F = 0x85;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
263 I2C0_FLT = 4;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
264 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
265 I2C0_C2 = I2C_C2_HDRS; // High drive select
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
266
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
267 // Enable I2C Interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
268 NVIC_ENABLE_IRQ( IRQ_I2C0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
269 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
270
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
271 void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
272 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
273 // Page Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
274 uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
275
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
276 // Max length of a page + chip id + reg start
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
277 uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
278 fullPage[0] = 0xE8; // Set chip id
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
279 fullPage[1] = startReg; // Set start reg
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
280
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
281 // Iterate through given pages, zero'ing out the given register regions
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
282 for ( uint8_t page = startPage; page < startPage + numPages; page++ )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
283 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
284 // Set page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
285 pageSetup[2] = page;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
286
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
287 // Setup page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
288 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
289 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
290
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
291 // Zero out page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
292 while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
293 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
294 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
295 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
296
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
297 void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
298 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
299 // Page Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
300 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
301
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
302 // Setup page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
303 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
304 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
305
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
306 // Write page to I2C Tx Buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
307 while ( I2C_Send( buffer, len, 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
308 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
309
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
310 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
311
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
312 void LED_readPage( uint8_t len, uint8_t page )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
313 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
314 // Page Setup
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
315 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
316
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
317 // Setup page
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
318 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
319 delay(1);
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
320
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
321 // Register Setup
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
322 uint8_t regSetup[] = { 0xE8, 0x00 };
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
323
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
324 // Setup starting register
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
325 while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
326 delay(1);
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
327
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
328 // Register Read Command
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
329 uint8_t regReadCmd[] = { 0xE9 };
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
330
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
331 // Read each register in the page
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
332 for ( uint8_t reg = 0; reg < len; reg++ )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
333 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
334 // Request register data
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
335 while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 0 ) == 0 )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
336 delay(1);
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
337 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
338 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
339
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
340 void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
341 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
342 // Page Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
343 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
344
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
345 // Reg Write Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
346 uint8_t writeData[] = { 0xE8, reg, val };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
347
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
348 // Setup page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
349 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
350 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
351
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
352 while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
353 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
354 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
355
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
356 // Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
357 inline void LED_setup()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
358 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
359 // Register Scan CLI dictionary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
360 CLI_registerDictionary( ledCLIDict, ledCLIDictName );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
361
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
362 // Initialize I2C
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
363 I2C_setup();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
364
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
365 // Zero out Frame Registers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
366 // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
367 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
368
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
369 // Disable Hardware shutdown of ISSI chip (pull high)
341
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
370 GPIOB_PDDR |= (1<<16);
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
371 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
372 GPIOB_PSOR |= (1<<16);
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
373
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
374 // Clear LED Pages
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
375 LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
376
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
377 // Enable LEDs based upon mask
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
378 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
379
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
380 // Set default brightness
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
381 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
382
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
383 // Disable Software shutdown of ISSI chip
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
384 LED_writeReg( 0x0A, 0x01, 0x0B );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
385 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
386
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
387
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
388 inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
389 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
390 uint8_t reTurn = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
391
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
392 // If sendLen is greater than buffer fail right away
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
393 if ( sendLen > buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
394 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
395
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
396 // Calculate new tail to determine if buffer has enough space
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
397 // The first element specifies the expected number of bytes from the slave (+1)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
398 // The second element in the new buffer is the length of the buffer sequence (+1)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
399 uint16_t newTail = buffer->tail + sendLen + 2;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
400 if ( newTail >= buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
401 newTail -= buffer->size;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
402
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
403 if ( I2C_BufferLen( buffer ) < sendLen + 2 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
404 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
405
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
406 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
407 print("|");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
408 printHex( sendLen + 2 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
409 print("|");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
410 printHex( *tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
411 print("@");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
412 printHex( newTail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
413 print("@");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
414 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
415
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
416 // If buffer is clean, return 1, otherwise 2
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
417 reTurn = buffer->head == buffer->tail ? 1 : 2;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
418
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
419 // Add to buffer, already know there is enough room (simplifies adding logic)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
420 uint8_t bufferHeaderPos = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
421 for ( uint16_t c = 0; c < sendLen; c++ )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
422 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
423 // Add data to buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
424 switch ( bufferHeaderPos )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
425 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
426 case 0:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
427 buffer->buffer[ buffer->tail ] = recvLen;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
428 bufferHeaderPos++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
429 c--;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
430 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
431
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
432 case 1:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
433 buffer->buffer[ buffer->tail ] = sendLen;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
434 bufferHeaderPos++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
435 c--;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
436 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
437
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
438 default:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
439 buffer->buffer[ buffer->tail ] = data[ c ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
440 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
441 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
442
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
443 // Check for wrap-around case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
444 if ( buffer->tail + 1 >= buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
445 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
446 buffer->tail = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
447 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
448 // Normal case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
449 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
450 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
451 buffer->tail++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
452 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
453 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
454
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
455 return reTurn;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
456 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
457
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
458
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
459 inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
460 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
461 // Tail >= Head
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
462 if ( buffer->tail >= buffer->head )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
463 return buffer->head + buffer->size - buffer->tail;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
464
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
465 // Head > Tail
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
466 return buffer->head - buffer->tail;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
467 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
468
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
469
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
470 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
471 {
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
472 dbug_msg("DATA: ");
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
473 printHex( byte );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
474
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
475 // Make sure buffer isn't full
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
476 if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
477 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
478 warn_msg("I2C_BufferPush failed, buffer full: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
479 printHex( byte );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
480 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
481 return;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
482 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
483
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
484 // Check for wrap-around case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
485 if ( buffer->tail + 1 >= buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
486 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
487 buffer->tail = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
488 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
489 // Normal case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
490 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
491 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
492 buffer->tail++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
493 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
494
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
495 // Add byte to buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
496 buffer->buffer[ buffer->tail ] = byte;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
497 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
498
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
499
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
500 uint8_t I2C_TxBufferPop()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
501 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
502 // Return 0xFF if no buffer left (do not rely on this)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
503 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
504 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
505 erro_msg("No buffer to pop an entry from... ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
506 printHex( I2C_TxBuffer.head );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
507 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
508 printHex( I2C_TxBuffer.tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
509 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
510 printHex( I2C_TxBuffer.sequencePos );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
511 print(NL);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
512 return 0xFF;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
513 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
514
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
515 // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
516 if ( I2C_TxBuffer.sequencePos == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
517 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
518 I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
519 I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
520 I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
521 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
522
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
523 uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
524
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
525 // Prune head
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
526 I2C_TxBuffer.head++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
527
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
528 // Wrap-around case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
529 if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
530 I2C_TxBuffer.head = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
531
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
532 // Decrement buffer sequence (until next stop will be sent)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
533 I2C_TxBuffer.sequencePos--;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
534
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
535 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
536 dbug_msg("Popping: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
537 printHex( data );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
538 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
539 printHex( I2C_TxBuffer.head );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
540 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
541 printHex( I2C_TxBuffer.tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
542 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
543 printHex( I2C_TxBuffer.sequencePos );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
544 print(NL);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
545 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
546 return data;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
547 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
548
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
549
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
550 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
551 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
552 // Check head and tail pointers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
553 // If full, return 0
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
554 // If empty, start up I2C Master Tx
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
555 // If buffer is non-empty and non-full, just append to the buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
556 switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
557 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
558 // Not enough buffer space...
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
559 case 0:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
560 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
561 erro_msg("Not enough Tx buffer space... ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
562 printHex( I2C_TxBuffer.head );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
563 print(":");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
564 printHex( I2C_TxBuffer.tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
565 print("+");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
566 printHex( sendLen );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
567 print("|");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
568 printHex( I2C_TxBuffer.size );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
569 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
570 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
571 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
572
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
573 // Empty buffer, initialize I2C
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
574 case 1:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
575 // Clear status flags
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
576 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
577
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
578 // Check to see if we already have control of the bus
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
579 if ( I2C0_C1 & I2C_C1_MST )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
580 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
581 // Already the master (ah yeah), send a repeated start
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
582 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
583 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
584 // Otherwise, seize control
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
585 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
586 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
587 // Wait...till the master dies
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
588 while ( I2C0_S & I2C_S_BUSY );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
589
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
590 // Now we're the master (ah yisss), get ready to send stuffs
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
591 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
592 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
593
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
594 // Enable I2C interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
595 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
596
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
597 // Depending on what type of transfer, the first byte is configured for R or W
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
598 I2C0_D = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
599
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
600 return 1;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
601 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
602
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
603 // Dirty buffer, I2C already initialized
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
604 return 2;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
605 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
606
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
607
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
608
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
609 // LED State processing loop
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
610 inline uint8_t LED_scan()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
611 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
612
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
613 // I2C Busy
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
614 // S & I2C_S_BUSY
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
615 //I2C_S_BUSY
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
616
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
617 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
618 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
619
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
620
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
621
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
622 // ----- CLI Command Functions -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
623
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
624 void cliFunc_i2cSend( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
625 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
626 char* curArgs;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
627 char* arg1Ptr;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
628 char* arg2Ptr = args;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
629
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
630 // Buffer used after interpretting the args, will be sent to I2C functions
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
631 // NOTE: Limited to 8 bytes currently (can be increased if necessary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
632 #define i2cSend_BuffLenMax 8
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
633 uint8_t buffer[ i2cSend_BuffLenMax ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
634 uint8_t bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
635
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
636 // No \r\n by default after the command is entered
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
637 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
638 info_msg("Sending: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
639
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
640 // Parse args until a \0 is found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
641 while ( bufferLen < i2cSend_BuffLenMax )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
642 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
643 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
644 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
645
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
646 // Stop processing args if no more are found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
647 if ( *arg1Ptr == '\0' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
648 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
649
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
650 // If | is found, end sequence and start new one
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
651 if ( *arg1Ptr == '|' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
652 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
653 print("| ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
654 I2C_Send( buffer, bufferLen, 0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
655 bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
656 continue;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
657 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
658
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
659 // Interpret the argument
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
660 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
661
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
662 // Print out the arg
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
663 dPrint( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
664 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
665 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
666
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
667 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
668
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
669 I2C_Send( buffer, bufferLen, 0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
670 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
671
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
672 void cliFunc_i2cRecv( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
673 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
674 char* curArgs;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
675 char* arg1Ptr;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
676 char* arg2Ptr = args;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
677
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
678 // Buffer used after interpretting the args, will be sent to I2C functions
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
679 // NOTE: Limited to 8 bytes currently (can be increased if necessary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
680 #define i2cSend_BuffLenMax 8
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
681 uint8_t buffer[ i2cSend_BuffLenMax ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
682 uint8_t bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
683
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
684 // No \r\n by default after the command is entered
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
685 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
686 info_msg("Sending: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
687
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
688 // Parse args until a \0 is found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
689 while ( bufferLen < i2cSend_BuffLenMax )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
690 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
691 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
692 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
693
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
694 // Stop processing args if no more are found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
695 if ( *arg1Ptr == '\0' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
696 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
697
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
698 // If | is found, end sequence and start new one
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
699 if ( *arg1Ptr == '|' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
700 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
701 print("| ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
702 I2C_Send( buffer, bufferLen, 0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
703 bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
704 continue;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
705 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
706
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
707 // Interpret the argument
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
708 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
709
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
710 // Print out the arg
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
711 dPrint( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
712 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
713 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
714
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
715 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
716
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
717 I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
718 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
719
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
720 void cliFunc_ledRPage( char* args )
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
721 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
722 // Parse number from argument
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
723 // NOTE: Only first argument is used
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
724 char* arg1Ptr;
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
725 char* arg2Ptr;
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
726 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
727
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
728 // Default to 0 if no argument is given
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
729 uint8_t page = 0;
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
730
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
731 if ( arg1Ptr[0] != '\0' )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
732 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
733 page = (uint8_t)numToInt( arg1Ptr );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
734 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
735
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
736 // No \r\n by default after the command is entered
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
737 print( NL );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
738
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
739 LED_readPage( 0xB4, page );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
740 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
741
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
742 void cliFunc_ledWPage( char* args )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
743 {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
744 char* curArgs;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
745 char* arg1Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
746 char* arg2Ptr = args;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
747
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
748 // First process page and starting address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
749 curArgs = arg2Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
750 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
751
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
752 // Stop processing args if no more are found
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
753 if ( *arg1Ptr == '\0' )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
754 return;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
755 uint8_t page[] = { 0xE8, 0xFD, numToInt( arg1Ptr ) };
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
756
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
757 curArgs = arg2Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
758 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
759
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
760 // Stop processing args if no more are found
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
761 if ( *arg1Ptr == '\0' )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
762 return;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
763 uint8_t data[] = { 0xE8, numToInt( arg1Ptr ), 0 };
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
764
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
765 // Set the register page
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
766 while ( I2C_Send( page, sizeof( page ), 0 ) == 0 )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
767 delay(1);
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
768
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
769 // Process all args
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
770 for ( ;; )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
771 {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
772 curArgs = arg2Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
773 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
774
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
775 // Stop processing args if no more are found
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
776 if ( *arg1Ptr == '\0' )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
777 break;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
778
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
779 data[2] = numToInt( arg1Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
780
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
781 // Write register location and data to I2C
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
782 while ( I2C_Send( data, sizeof( data ), 0 ) == 0 )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
783 delay(1);
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
784
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
785 // Increment address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
786 data[1]++;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
787 }
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
788 }
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
789
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
790 void cliFunc_ledStart( char* args )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
791 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
792 print( NL ); // No \r\n by default after the command is entered
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
793 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
794 //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
795 LED_writeReg( 0x0A, 0x01, 0x0B );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
796 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
797
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
798 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
799
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
800 void cliFunc_ledTest( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
801 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
802 print( NL ); // No \r\n by default after the command is entered
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
803 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
804 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
805
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
806 void cliFunc_ledZero( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
807 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
808 print( NL ); // No \r\n by default after the command is entered
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
809 LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
810 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
811