Skip to content

Commit 169dd54

Browse files
author
diogo
committed
added i2c support for stm32l475
1 parent fdf5ba6 commit 169dd54

File tree

6 files changed

+339
-178
lines changed

6 files changed

+339
-178
lines changed

lib/atca_command.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,9 @@ void deleteATCACommand(ATCACommand *ca_cmd) // destructor
788788
* \param[out] crc_le Pointer to the place where the two-bytes of CRC will be
789789
* returned in little-endian byte order.
790790
*/
791-
void atCRC(size_t length, const uint8_t *data, uint8_t *crc_le)
791+
void atCRC(uint32_t length, const uint8_t *data, uint8_t *crc_le)
792792
{
793-
size_t counter;
793+
uint32_t counter;
794794
uint16_t crc_register = 0;
795795
uint16_t polynom = 0x8005;
796796
uint8_t shift_register;

lib/atca_command.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void deleteATCACommand(ATCACommand *); // destructor
184184
/*---- end of ATCACommand ----*/
185185

186186
// command helpers
187-
void atCRC(size_t length, const uint8_t *data, uint8_t *crc_le);
187+
void atCRC(uint32_t length, const uint8_t *data, uint8_t *crc_le);
188188
void atCalcCrc(ATCAPacket *pkt);
189189
ATCA_STATUS atCheckCrc(const uint8_t *response);
190190

lib/hal/atca_hal.c

+138-128
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/**
22
* \file
3-
* \brief low-level HAL - methods used to setup indirection to physical layer interface.
4-
* this level does the dirty work of abstracting the higher level ATCAIFace methods from the
5-
* low-level physical interfaces. Its main goal is to keep low-level details from bleeding into
3+
* \brief low-level HAL - methods used to setup indirection to physical layer
4+
* interface.
5+
* this level does the dirty work of abstracting the higher level ATCAIFace
6+
* methods from the
7+
* low-level physical interfaces. Its main goal is to keep low-level details
8+
* from bleeding into
69
* the logical interface implemetation.
710
*
811
* \copyright (c) 2017 Microchip Technology Inc. and its subsidiaries.
@@ -32,165 +35,172 @@
3235
* TERMS.
3336
*/
3437

38+
/* when incorporating ATCA HAL into your application, you need to adjust the
39+
* #defines in atca_hal.h to include
40+
* and exclude appropriate interfaces - this optimizes memory use when not using
41+
* a specific iface implementation in your application */
3542

36-
/* when incorporating ATCA HAL into your application, you need to adjust the #defines in atca_hal.h to include
37-
* and exclude appropriate interfaces - this optimizes memory use when not using a specific iface implementation in your application */
38-
39-
#include "cryptoauthlib.h"
4043
#include "atca_hal.h"
44+
#include "cryptoauthlib.h"
45+
#include "hal_i2c_stm32l475.h"
46+
47+
#define ATCA_HAL_I2C
48+
void atca_delay_us(uint32_t delay) { HAL_Delay(0); }
49+
void atca_delay_10us(uint32_t delay) { HAL_Delay(0); }
50+
void atca_delay_ms(uint32_t delay) { HAL_Delay(0); }
4151

4252
/** \brief Standard HAL API for ATCA to initialize a physical interface
4353
* \param[in] cfg pointer to ATCAIfaceCfg object
4454
* \param[in] hal pointer to ATCAHAL_t intermediate data structure
4555
* \return ATCA_SUCCESS on success, otherwise an error code.
4656
*/
4757

48-
ATCA_STATUS hal_iface_init(ATCAIfaceCfg *cfg, ATCAHAL_t *hal)
49-
{
50-
// Because C isn't a real object oriented language or dynamically typed, some switch in the overall system is unavoidable
51-
// The key here is to provide the flexibility to include just the types of interfaces you want/need without breaking the
52-
// object model. The former is needed so in an embedded, constrained memory environment, you don't have to pay the price
53-
// (in terms of memory) for interfaces you don't use in your application.
54-
ATCA_STATUS status = ATCA_COMM_FAIL;
58+
ATCA_STATUS hal_iface_init(ATCAIfaceCfg *cfg, ATCAHAL_t *hal) {
59+
// Because C isn't a real object oriented language or dynamically typed, some
60+
// switch in the overall system is unavoidable
61+
// The key here is to provide the flexibility to include just the types of
62+
// interfaces you want/need without breaking the
63+
// object model. The former is needed so in an embedded, constrained memory
64+
// environment, you don't have to pay the price
65+
// (in terms of memory) for interfaces you don't use in your application.
66+
ATCA_STATUS status = ATCA_COMM_FAIL;
5567

56-
switch (cfg->iface_type)
57-
{
58-
case ATCA_I2C_IFACE:
59-
#ifdef ATCA_HAL_I2C
60-
hal->halinit = &hal_i2c_init;
61-
hal->halpostinit = &hal_i2c_post_init;
62-
hal->halreceive = &hal_i2c_receive;
63-
hal->halsend = &hal_i2c_send;
64-
hal->halsleep = &hal_i2c_sleep;
65-
hal->halwake = &hal_i2c_wake;
66-
hal->halidle = &hal_i2c_idle;
67-
hal->halrelease = &hal_i2c_release;
68-
hal->hal_data = NULL;
68+
switch (cfg->iface_type) {
69+
case ATCA_I2C_IFACE:
70+
#ifdef ATCA_HAL_I2C
71+
hal->halinit = &hal_i2c_init;
72+
hal->halpostinit = &hal_i2c_post_init;
73+
hal->halreceive = &hal_i2c_receive;
74+
hal->halsend = &hal_i2c_send;
75+
hal->halsleep = &hal_i2c_sleep;
76+
hal->halwake = &hal_i2c_wake;
77+
hal->halidle = &hal_i2c_idle;
78+
hal->halrelease = &hal_i2c_release;
79+
hal->hal_data = NULL;
6980

70-
status = ATCA_SUCCESS;
71-
#endif
72-
break;
73-
case ATCA_SWI_IFACE:
74-
#ifdef ATCA_HAL_SWI
75-
hal->halinit = &hal_swi_init;
76-
hal->halpostinit = &hal_swi_post_init;
77-
hal->halreceive = &hal_swi_receive;
78-
hal->halsend = &hal_swi_send;
79-
hal->halsleep = &hal_swi_sleep;
80-
hal->halwake = &hal_swi_wake;
81-
hal->halidle = &hal_swi_idle;
82-
hal->halrelease = &hal_swi_release;
83-
hal->hal_data = NULL;
81+
status = ATCA_SUCCESS;
82+
#endif
83+
break;
84+
case ATCA_SWI_IFACE:
85+
#ifdef ATCA_HAL_SWI
86+
hal->halinit = &hal_swi_init;
87+
hal->halpostinit = &hal_swi_post_init;
88+
hal->halreceive = &hal_swi_receive;
89+
hal->halsend = &hal_swi_send;
90+
hal->halsleep = &hal_swi_sleep;
91+
hal->halwake = &hal_swi_wake;
92+
hal->halidle = &hal_swi_idle;
93+
hal->halrelease = &hal_swi_release;
94+
hal->hal_data = NULL;
8495

85-
status = ATCA_SUCCESS;
86-
#endif
87-
break;
88-
case ATCA_UART_IFACE:
89-
#ifdef ATCA_HAL_UART
90-
// TODO - initialize UART iface
91-
#endif
92-
#ifdef ATCA_HAL_KIT_CDC
93-
hal->halinit = &hal_kit_cdc_init;
94-
hal->halpostinit = &hal_kit_cdc_post_init;
95-
hal->halreceive = &hal_kit_cdc_receive;
96-
hal->halsend = &hal_kit_cdc_send;
97-
hal->halsleep = &hal_kit_cdc_sleep;
98-
hal->halwake = &hal_kit_cdc_wake;
99-
hal->halidle = &hal_kit_cdc_idle;
100-
hal->halrelease = &hal_kit_cdc_release;
101-
hal->hal_data = NULL;
96+
status = ATCA_SUCCESS;
97+
#endif
98+
break;
99+
case ATCA_UART_IFACE:
100+
#ifdef ATCA_HAL_UART
101+
// TODO - initialize UART iface
102+
#endif
103+
#ifdef ATCA_HAL_KIT_CDC
104+
hal->halinit = &hal_kit_cdc_init;
105+
hal->halpostinit = &hal_kit_cdc_post_init;
106+
hal->halreceive = &hal_kit_cdc_receive;
107+
hal->halsend = &hal_kit_cdc_send;
108+
hal->halsleep = &hal_kit_cdc_sleep;
109+
hal->halwake = &hal_kit_cdc_wake;
110+
hal->halidle = &hal_kit_cdc_idle;
111+
hal->halrelease = &hal_kit_cdc_release;
112+
hal->hal_data = NULL;
102113

103-
status = ATCA_SUCCESS;
104-
#endif
105-
break;
106-
case ATCA_SPI_IFACE:
107-
#ifdef ATCA_HAL_SPI
108-
// TODO - initialize SPI iface
109-
#endif
110-
break;
111-
case ATCA_HID_IFACE:
112-
#ifdef ATCA_HAL_KIT_HID
113-
hal->halinit = &hal_kit_hid_init;
114-
hal->halpostinit = &hal_kit_hid_post_init;
115-
hal->halreceive = &hal_kit_hid_receive;
116-
hal->halsend = &hal_kit_hid_send;
117-
hal->halsleep = &hal_kit_hid_sleep;
118-
hal->halwake = &hal_kit_hid_wake;
119-
hal->halidle = &hal_kit_hid_idle;
120-
hal->halrelease = &hal_kit_hid_release;
121-
hal->hal_data = NULL;
114+
status = ATCA_SUCCESS;
115+
#endif
116+
break;
117+
case ATCA_SPI_IFACE:
118+
#ifdef ATCA_HAL_SPI
119+
// TODO - initialize SPI iface
120+
#endif
121+
break;
122+
case ATCA_HID_IFACE:
123+
#ifdef ATCA_HAL_KIT_HID
124+
hal->halinit = &hal_kit_hid_init;
125+
hal->halpostinit = &hal_kit_hid_post_init;
126+
hal->halreceive = &hal_kit_hid_receive;
127+
hal->halsend = &hal_kit_hid_send;
128+
hal->halsleep = &hal_kit_hid_sleep;
129+
hal->halwake = &hal_kit_hid_wake;
130+
hal->halidle = &hal_kit_hid_idle;
131+
hal->halrelease = &hal_kit_hid_release;
132+
hal->hal_data = NULL;
122133

123-
status = ATCA_SUCCESS;
124-
#endif
125-
break;
126-
case ATCA_CUSTOM_IFACE:
127-
#ifdef ATCA_HAL_CUSTOM
128-
hal->halinit = cfg->atcacustom.halinit;
129-
hal->halpostinit = cfg->atcacustom.halpostinit;
130-
hal->halreceive = cfg->atcacustom.halreceive;
131-
hal->halsend = cfg->atcacustom.halsend;
132-
hal->halsleep = cfg->atcacustom.halsleep;
133-
hal->halwake = cfg->atcacustom.halwake;
134-
hal->halidle = cfg->atcacustom.halidle;
135-
hal->halrelease = cfg->atcacustom.halrelease;
136-
hal->hal_data = NULL;
134+
status = ATCA_SUCCESS;
135+
#endif
136+
break;
137+
case ATCA_CUSTOM_IFACE:
138+
#ifdef ATCA_HAL_CUSTOM
139+
hal->halinit = cfg->atcacustom.halinit;
140+
hal->halpostinit = cfg->atcacustom.halpostinit;
141+
hal->halreceive = cfg->atcacustom.halreceive;
142+
hal->halsend = cfg->atcacustom.halsend;
143+
hal->halsleep = cfg->atcacustom.halsleep;
144+
hal->halwake = cfg->atcacustom.halwake;
145+
hal->halidle = cfg->atcacustom.halidle;
146+
hal->halrelease = cfg->atcacustom.halrelease;
147+
hal->hal_data = NULL;
137148

138-
status = ATCA_SUCCESS;
139-
#endif
140-
break;
141-
default:
142-
break;
143-
}
144-
return status;
149+
status = ATCA_SUCCESS;
150+
#endif
151+
break;
152+
default:
153+
break;
154+
}
155+
return status;
145156
}
146157

147158
/** \brief releases a physical interface, HAL knows how to interpret hal_data
148159
* \param[in] iface_type - the type of physical interface to release
149-
* \param[in] hal_data - pointer to opaque hal data maintained by HAL implementation for this interface type
160+
* \param[in] hal_data - pointer to opaque hal data maintained by HAL
161+
* implementation for this interface type
150162
* \return ATCA_SUCCESS on success, otherwise an error code.
151163
*/
152164

153-
ATCA_STATUS hal_iface_release(ATCAIfaceType iface_type, void *hal_data)
154-
{
155-
ATCA_STATUS status = ATCA_GEN_FAIL;
165+
ATCA_STATUS hal_iface_release(ATCAIfaceType iface_type, void *hal_data) {
166+
ATCA_STATUS status = ATCA_GEN_FAIL;
156167

157-
switch (iface_type)
158-
{
159-
case ATCA_I2C_IFACE:
168+
switch (iface_type) {
169+
case ATCA_I2C_IFACE:
160170
#ifdef ATCA_HAL_I2C
161-
status = hal_i2c_release(hal_data);
171+
status = hal_i2c_release(hal_data);
162172
#endif
163-
break;
164-
case ATCA_SWI_IFACE:
165-
#ifdef ATCA_HAL_SWI
166-
status = hal_swi_release(hal_data);
173+
break;
174+
case ATCA_SWI_IFACE:
175+
#ifdef ATCA_HAL_SWI
176+
status = hal_swi_release(hal_data);
167177
#endif
168-
break;
169-
case ATCA_UART_IFACE:
170-
#ifdef ATCA_HAL_UART
171-
// TODO - release HAL UART
178+
break;
179+
case ATCA_UART_IFACE:
180+
#ifdef ATCA_HAL_UART
181+
// TODO - release HAL UART
172182
#endif
173183
#ifdef ATCA_HAL_KIT_CDC
174-
status = hal_kit_cdc_release(hal_data);
184+
status = hal_kit_cdc_release(hal_data);
175185
#endif
176-
break;
177-
case ATCA_SPI_IFACE:
186+
break;
187+
case ATCA_SPI_IFACE:
178188
#ifdef ATCA_HAL_SPI
179-
// TODO - release HAL SPI
189+
// TODO - release HAL SPI
180190
#endif
181-
break;
182-
case ATCA_HID_IFACE:
191+
break;
192+
case ATCA_HID_IFACE:
183193
#ifdef ATCA_HAL_KIT_HID
184-
status = hal_kit_hid_release(hal_data);
194+
status = hal_kit_hid_release(hal_data);
185195
#endif
186-
break;
187-
case ATCA_CUSTOM_IFACE:
196+
break;
197+
case ATCA_CUSTOM_IFACE:
188198
#ifdef ATCA_HAL_CUSTOM
189199
#endif
190-
break;
191-
default:
192-
break;
193-
}
200+
break;
201+
default:
202+
break;
203+
}
194204

195-
return status;
205+
return status;
196206
}

0 commit comments

Comments
 (0)