1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Drivers for the AS1115 I2C LED/segment-display driver
10 Software License Agreement (BSD License)
12 Copyright (c) 2012, microBuilder SARL
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.
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.
37 /**************************************************************************/
39 #include "core/systick/systick.h"
41 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
42 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
43 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
45 static bool _as1115Initialised
= false;
47 /**************************************************************************/
49 @brief Sends a single command byte over I2C to the specified address
51 /**************************************************************************/
52 as1115Error_t
as1115WriteCmd (uint8_t address
, uint8_t cmd
)
54 // Clear write buffers
56 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
58 I2CMasterBuffer
[i
] = 0x00;
61 // ToDo: Add real I2C error checking
64 I2CMasterBuffer
[0] = address
; // device sub address
65 I2CMasterBuffer
[1] = cmd
; // Command register
67 return AS1115_ERROR_OK
;
70 /**************************************************************************/
72 @brief Writes an command byte followed by a data byte over I2C
74 /**************************************************************************/
75 as1115Error_t
as1115WriteCmdData (uint8_t address
, uint8_t cmd
, uint8_t data
)
77 // Clear write buffers
79 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
81 I2CMasterBuffer
[i
] = 0x00;
84 // ToDo: Add real I2C error checking
87 I2CMasterBuffer
[0] = address
; // I2C device address
88 I2CMasterBuffer
[1] = cmd
; // Command register
89 I2CMasterBuffer
[2] = data
; // Value to write
91 return AS1115_ERROR_OK
;
94 /**************************************************************************/
96 @brief Reads a single byte over I2C
98 /**************************************************************************/
99 as1115Error_t
as1115Read8(uint8_t address
, uint8_t *value
)
101 // Clear write buffers
103 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
105 I2CMasterBuffer
[i
] = 0x00;
108 // ToDo: Add real I2C error checking
111 I2CMasterBuffer
[0] = address
| AS1115_READBIT
;
114 *value
= I2CSlaveBuffer
[0];
116 return AS1115_ERROR_OK
;
119 /**************************************************************************/
121 @brief Initialises the I2C block
123 /**************************************************************************/
124 as1115Error_t
as1115Init(void)
127 if (i2cInit(I2CMASTER
) == false)
129 return AS1115_ERROR_I2CINIT
; /* Fatal error */
132 // Reset AS1115 to normal operation with default settings
133 as1115WriteCmdData(AS1115_ADDRESS
, AS1115_SHUTDOWN
, 0x01);
135 // Use user-set I2C address (000000 + address pins)
136 as1115WriteCmdData(AS1115_ADDRESS
, AS1115_SELFADDR
, 0x01);
141 as1115SetDecodeMode(0);
143 // Set the brightness to the maximum value (0x0F)
144 as1115SetBrightness(0xF);
146 // Turn on all digits by default (0..7)
147 as1115WriteCmdData(AS1115_SUBADDRESS
, AS1115_SCANLIMIT
, 0x07);
149 _as1115Initialised
= true;
151 return AS1115_ERROR_OK
;
154 /**************************************************************************/
156 @brief Send test command
158 /**************************************************************************/
159 as1115Error_t
as1115Test(void)
161 as1115Error_t error
= AS1115_ERROR_OK
;
164 if (!_as1115Initialised
) as1115Init();
166 // Use test register to detect LED status
167 error
= as1115WriteCmd(AS1115_SUBADDRESS
, AS1115_DISPTEST
);
168 if (error
) return error
;
170 error
= as1115Read8(AS1115_SUBADDRESS
, &results
);
171 if (error
) return error
;
173 // Throw an error since something is wrong with the wiring (use 'results' to check)
174 if ((results
& 0xF8) != 0x80)
176 return AS1115_ERROR_UNEXPECTEDRESPONSE
;
179 // Turn everything on
180 error
= as1115WriteCmdData(AS1115_SUBADDRESS
, AS1115_DISPTEST
, 0x01);
181 if (error
) return error
;
184 // Turn everything off
185 error
= as1115WriteCmdData(AS1115_SUBADDRESS
, AS1115_DISPTEST
, 0x00);
186 if (error
) return error
;
191 /**************************************************************************/
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.
196 /**************************************************************************/
197 as1115Error_t
as1115SetDecodeMode(uint8_t x
)
199 as1115Error_t error
= AS1115_ERROR_OK
;
201 error
= as1115WriteCmdData(AS1115_SUBADDRESS
, AS1115_DECODEMODE
, x
);
205 /**************************************************************************/
207 @brief Sets the intensity control register (0..15 or 0x00..0x0F)
209 /**************************************************************************/
210 as1115Error_t
as1115SetBrightness(uint8_t x
)
212 as1115Error_t error
= AS1115_ERROR_OK
;
214 if (x
> 0xF) x
= 0xF;
216 error
= as1115WriteCmdData(AS1115_SUBADDRESS
, AS1115_INTENSITY
, x
);
220 /**************************************************************************/
222 @brief Sets the feature bit (controls blinking, clock source, etc.)
224 /**************************************************************************/
225 as1115Error_t
as1115SetFeature(uint8_t feature
)
227 as1115Error_t error
= AS1115_ERROR_OK
;
229 error
= as1115WriteCmdData(AS1115_SUBADDRESS
, AS1115_FEATURE
, feature
);
233 /**************************************************************************/
235 @brief Writes an 8-bit buffer to the display
237 /**************************************************************************/
238 as1115Error_t
as1115WriteBuffer(uint8_t *buffer
)
240 as1115Error_t error
= AS1115_ERROR_OK
;
243 if (!_as1115Initialised
) as1115Init();
245 // Clear write buffers
246 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
248 I2CMasterBuffer
[i
] = 0x00;
251 // Update individual digits
252 for ( i
= 0; i
< 8; i
++ )
256 I2CMasterBuffer
[0] = AS1115_SUBADDRESS
; // I2C device address
257 I2CMasterBuffer
[1] = i
+1; // Digit register
258 I2CMasterBuffer
[2] = buffer
[i
]; // Value
262 return AS1115_ERROR_OK
;