Spieler splodiert.
[hackover2013-badge-firmware.git] / drivers / displays / segment / as1115 / as1115.c
1 /**************************************************************************/
2 /*!
3 @file as1115.c
4 @author K. Townsend (microBuilder.eu)
5
6 @brief Drivers for the AS1115 I2C LED/segment-display driver
7
8 @section LICENSE
9
10 Software License Agreement (BSD License)
11
12 Copyright (c) 2012, microBuilder SARL
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. Neither the name of the copyright holders nor the
23 names of its contributors may be used to endorse or promote products
24 derived from this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
27 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
30 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 /**************************************************************************/
38 #include "as1115.h"
39 #include "core/systick/systick.h"
40
41 extern volatile uint8_t I2CMasterBuffer[I2C_BUFSIZE];
42 extern volatile uint8_t I2CSlaveBuffer[I2C_BUFSIZE];
43 extern volatile uint32_t I2CReadLength, I2CWriteLength;
44
45 static bool _as1115Initialised = false;
46
47 /**************************************************************************/
48 /*!
49 @brief Sends a single command byte over I2C to the specified address
50 */
51 /**************************************************************************/
52 as1115Error_t as1115WriteCmd (uint8_t address, uint8_t cmd)
53 {
54 // Clear write buffers
55 uint32_t i;
56 for ( i = 0; i < I2C_BUFSIZE; i++ )
57 {
58 I2CMasterBuffer[i] = 0x00;
59 }
60
61 // ToDo: Add real I2C error checking
62 I2CWriteLength = 2;
63 I2CReadLength = 0;
64 I2CMasterBuffer[0] = address; // device sub address
65 I2CMasterBuffer[1] = cmd; // Command register
66 i2cEngine();
67 return AS1115_ERROR_OK;
68 }
69
70 /**************************************************************************/
71 /*!
72 @brief Writes an command byte followed by a data byte over I2C
73 */
74 /**************************************************************************/
75 as1115Error_t as1115WriteCmdData (uint8_t address, uint8_t cmd, uint8_t data)
76 {
77 // Clear write buffers
78 uint32_t i;
79 for ( i = 0; i < I2C_BUFSIZE; i++ )
80 {
81 I2CMasterBuffer[i] = 0x00;
82 }
83
84 // ToDo: Add real I2C error checking
85 I2CWriteLength = 3;
86 I2CReadLength = 0;
87 I2CMasterBuffer[0] = address; // I2C device address
88 I2CMasterBuffer[1] = cmd; // Command register
89 I2CMasterBuffer[2] = data; // Value to write
90 i2cEngine();
91 return AS1115_ERROR_OK;
92 }
93
94 /**************************************************************************/
95 /*!
96 @brief Reads a single byte over I2C
97 */
98 /**************************************************************************/
99 as1115Error_t as1115Read8(uint8_t address, uint8_t *value)
100 {
101 // Clear write buffers
102 uint32_t i;
103 for ( i = 0; i < I2C_BUFSIZE; i++ )
104 {
105 I2CMasterBuffer[i] = 0x00;
106 }
107
108 // ToDo: Add real I2C error checking
109 I2CWriteLength = 0;
110 I2CReadLength = 1;
111 I2CMasterBuffer[0] = address | AS1115_READBIT;
112 i2cEngine();
113
114 *value = I2CSlaveBuffer[0];
115
116 return AS1115_ERROR_OK;
117 }
118
119 /**************************************************************************/
120 /*!
121 @brief Initialises the I2C block
122 */
123 /**************************************************************************/
124 as1115Error_t as1115Init(void)
125 {
126 // Initialise I2C
127 if (i2cInit(I2CMASTER) == false)
128 {
129 return AS1115_ERROR_I2CINIT; /* Fatal error */
130 }
131
132 // Reset AS1115 to normal operation with default settings
133 as1115WriteCmdData(AS1115_ADDRESS, AS1115_SHUTDOWN, 0x01);
134
135 // Use user-set I2C address (000000 + address pins)
136 as1115WriteCmdData(AS1115_ADDRESS, AS1115_SELFADDR, 0x01);
137
138 systickDelay(20);
139
140 // Use hex decoding
141 as1115SetDecodeMode(0);
142
143 // Set the brightness to the maximum value (0x0F)
144 as1115SetBrightness(0xF);
145
146 // Turn on all digits by default (0..7)
147 as1115WriteCmdData(AS1115_SUBADDRESS, AS1115_SCANLIMIT, 0x07);
148
149 _as1115Initialised = true;
150
151 return AS1115_ERROR_OK;
152 }
153
154 /**************************************************************************/
155 /*!
156 @brief Send test command
157 */
158 /**************************************************************************/
159 as1115Error_t as1115Test(void)
160 {
161 as1115Error_t error = AS1115_ERROR_OK;
162 uint8_t results;
163
164 if (!_as1115Initialised) as1115Init();
165
166 // Use test register to detect LED status
167 error = as1115WriteCmd(AS1115_SUBADDRESS, AS1115_DISPTEST);
168 if (error) return error;
169
170 error = as1115Read8(AS1115_SUBADDRESS, &results);
171 if (error) return error;
172
173 // Throw an error since something is wrong with the wiring (use 'results' to check)
174 if ((results & 0xF8) != 0x80)
175 {
176 return AS1115_ERROR_UNEXPECTEDRESPONSE;
177 }
178
179 // Turn everything on
180 error = as1115WriteCmdData(AS1115_SUBADDRESS, AS1115_DISPTEST, 0x01);
181 if (error) return error;
182 systickDelay(1000);
183
184 // Turn everything off
185 error = as1115WriteCmdData(AS1115_SUBADDRESS, AS1115_DISPTEST, 0x00);
186 if (error) return error;
187
188 return error;
189 }
190
191 /**************************************************************************/
192 /*!
193 @brief Sets the decode enable register to indicate if BCD or HEX
194 decoding should be used. Set to 0 for no BCD on all digits.
195 */
196 /**************************************************************************/
197 as1115Error_t as1115SetDecodeMode(uint8_t x)
198 {
199 as1115Error_t error = AS1115_ERROR_OK;
200
201 error = as1115WriteCmdData(AS1115_SUBADDRESS, AS1115_DECODEMODE, x);
202 return error;
203 }
204
205 /**************************************************************************/
206 /*!
207 @brief Sets the intensity control register (0..15 or 0x00..0x0F)
208 */
209 /**************************************************************************/
210 as1115Error_t as1115SetBrightness(uint8_t x)
211 {
212 as1115Error_t error = AS1115_ERROR_OK;
213
214 if (x > 0xF) x = 0xF;
215
216 error = as1115WriteCmdData(AS1115_SUBADDRESS, AS1115_INTENSITY, x);
217 return error;
218 }
219
220 /**************************************************************************/
221 /*!
222 @brief Sets the feature bit (controls blinking, clock source, etc.)
223 */
224 /**************************************************************************/
225 as1115Error_t as1115SetFeature(uint8_t feature)
226 {
227 as1115Error_t error = AS1115_ERROR_OK;
228
229 error = as1115WriteCmdData(AS1115_SUBADDRESS, AS1115_FEATURE, feature);
230 return error;
231 }
232
233 /**************************************************************************/
234 /*!
235 @brief Writes an 8-bit buffer to the display
236 */
237 /**************************************************************************/
238 as1115Error_t as1115WriteBuffer(uint8_t *buffer)
239 {
240 as1115Error_t error = AS1115_ERROR_OK;
241 uint32_t i;
242
243 if (!_as1115Initialised) as1115Init();
244
245 // Clear write buffers
246 for ( i = 0; i < I2C_BUFSIZE; i++ )
247 {
248 I2CMasterBuffer[i] = 0x00;
249 }
250
251 // Update individual digits
252 for ( i = 0; i < 8; i++ )
253 {
254 I2CWriteLength = 3;
255 I2CReadLength = 0;
256 I2CMasterBuffer[0] = AS1115_SUBADDRESS; // I2C device address
257 I2CMasterBuffer[1] = i+1; // Digit register
258 I2CMasterBuffer[2] = buffer[i]; // Value
259 i2cEngine();
260 }
261
262 return AS1115_ERROR_OK;
263 }
264
This page took 0.05825 seconds and 5 git commands to generate.