Skip to content

Commit 5ea792c

Browse files
committed
- Added error checking for trng_get_bytes function;
- Added curly brackets to single line conditions in gpio_qpi.c and gpio_irq_api.c; - Changed rx and tx buffers in serial module as local variables; - Minor i2c & spi updates for github pull request; - Added function definition for spi_master_block_write.
1 parent d1b81b2 commit 5ea792c

File tree

6 files changed

+74
-27
lines changed

6 files changed

+74
-27
lines changed

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/gpio_api.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ void gpio_init(gpio_t *obj, PinName pin)
7171
{
7272
obj->pin = pin;
7373

74-
if (pin == (PinName)NC)
74+
if (pin == (PinName)NC) {
7575
return;
76+
}
7677

7778
// Initialize the GPIO driver. This function
7879
// initializes the GPIO driver only once globally.
79-
if (!gpio_initialized)
80+
if (!gpio_initialized) {
8081
adi_gpio_Init(gpioMemory, ADI_GPIO_MEMORY_SIZE);
82+
}
8183

8284
pin_function(pin, MUX_FUNC_0);
8385
}
@@ -134,11 +136,12 @@ int gpio_read(gpio_t *obj)
134136
uint16_t Data;
135137

136138
// check whether the pin is configured as input or output
137-
if ((gpio_oen[port] >> pin_num) & 1)
139+
if ((gpio_oen[port] >> pin_num) & 1) {
138140
Data = gpio_output_val[port] & (1 << pin_num);
139-
else
141+
} else {
140142
// otherwise call GetData
141143
adi_gpio_GetData((ADI_GPIO_PORT)port, (1 << pin_num), &Data);
144+
}
142145

143146
return ((((uint32_t)Data) >> pin_num) & 1);
144147
}

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/gpio_irq_api.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
183183
uint32_t pin_num = pin & 0xFF;
184184

185185
// check for valid pin and ID
186-
if ((pin == NC) || (id == 0))
186+
if ((pin == NC) || (id == 0)) {
187187
return -1;
188+
}
188189

189190
// make sure gpio driver has been initialized
190191
if (!gpio_initialized) {
@@ -193,8 +194,9 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
193194
}
194195

195196
// save the handler
196-
if (handler)
197+
if (handler) {
197198
irq_handler = handler;
199+
}
198200

199201
// disable the interrupt for the given pin
200202
disable_pin_interrupt((ADI_GPIO_PORT)port, pin_num);
@@ -242,27 +244,30 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
242244
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
243245
uint32_t pin_num = obj->pinname & 0xFF;
244246

245-
if (event == IRQ_NONE)
247+
if (event == IRQ_NONE) {
246248
return;
249+
}
247250

248251
// read the current polarity register
249252
adi_gpio_GetGroupInterruptPolarity((ADI_GPIO_PORT)port, 1 << pin_num, &int_polarity_reg);
250253

251-
if (event == IRQ_RISE)
254+
if (event == IRQ_RISE) {
252255
int_polarity_reg |= (1 << pin_num);
253-
else
256+
} else {
254257
int_polarity_reg &= ~(1 << pin_num);
258+
}
255259

256260
// set the polarity register
257261
adi_gpio_SetGroupInterruptPolarity((ADI_GPIO_PORT)port, int_polarity_reg);
258262

259263
channel_ids[port][pin_num].event = event;
260264

261265
// enable interrupt for this pin if enable flag is set
262-
if (enable)
266+
if (enable) {
263267
gpio_irq_enable(obj);
264-
else
268+
} else {
265269
gpio_irq_disable(obj);
270+
}
266271
}
267272

268273
/** Enable GPIO IRQ
@@ -275,8 +280,9 @@ void gpio_irq_enable(gpio_irq_t *obj)
275280
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
276281
uint32_t pin_num = obj->pinname & 0xFF;
277282

278-
if (channel_ids[port][pin_num].event == IRQ_NONE)
283+
if (channel_ids[port][pin_num].event == IRQ_NONE) {
279284
return;
285+
}
280286

281287
// Group all RISE interrupts in INTA and FALL interrupts in INTB
282288
if (channel_ids[port][pin_num].event == IRQ_RISE) {
@@ -302,14 +308,17 @@ void gpio_irq_disable(gpio_irq_t *obj)
302308
uint32_t port = obj->pinname >> GPIO_PORT_SHIFT;
303309
uint32_t pin_num = obj->pinname & 0xFF;
304310

305-
if (channel_ids[port][pin_num].event == IRQ_NONE)
311+
if (channel_ids[port][pin_num].event == IRQ_NONE) {
306312
return;
313+
}
307314

308315
// Group all RISE interrupts in INTA and FALL interrupts in INTB
309-
if (channel_ids[port][pin_num].event == IRQ_RISE)
316+
if (channel_ids[port][pin_num].event == IRQ_RISE) {
310317
disable_pin_interrupt((ADI_GPIO_PORT)port, pin_num);
311-
else if (channel_ids[port][pin_num].event == IRQ_FALL)
318+
}
319+
else if (channel_ids[port][pin_num].event == IRQ_FALL) {
312320
disable_pin_interrupt((ADI_GPIO_PORT)port, pin_num);
321+
}
313322

314323
channel_ids[port][pin_num].int_enable = 0;
315324
}

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/i2c_api.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
110110

111111
int i2c_start(i2c_t *obj)
112112
{
113-
return 0;
113+
/* The Hardware does not support this feature. */
114+
return -1;
114115
}
115116

116117

117118
int i2c_stop(i2c_t *obj)
118119
{
119-
return 0;
120+
/* The Hardware does not support this feature. */
121+
return -1;
120122
}
121123

122124

@@ -204,13 +206,15 @@ void i2c_reset(i2c_t *obj)
204206

205207
int i2c_byte_read(i2c_t *obj, int last)
206208
{
207-
return 0;
209+
/* The Hardware does not support this feature. */
210+
return -1;
208211
}
209212

210213

211214
int i2c_byte_write(i2c_t *obj, int data)
212215
{
213-
return 0;
216+
/* The Hardware does not support this feature. */
217+
return -1;
214218
}
215219

216220
#endif // #if DEVICE_I2C

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/serial_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@
5353
#include "PeripheralPins.h"
5454
#include "drivers/uart/adi_uart.h"
5555
#define ADI_UART_MEMORY_SIZE (ADI_UART_BIDIR_MEMORY_SIZE)
56-
#define ADI_UART_NUM_DEVICES 2
56+
#define ADI_UART_NUM_DEVICES 2
5757

58-
static ADI_UART_HANDLE hDevice[ADI_UART_NUM_DEVICES];
58+
static ADI_UART_HANDLE hDevice[ADI_UART_NUM_DEVICES];
5959
static uint32_t UartDeviceMem[ADI_UART_NUM_DEVICES][(ADI_UART_MEMORY_SIZE + 3)/4];
6060
static uint32_t serial_irq_ids[ADI_UART_NUM_DEVICES] = {0};
6161
static uart_irq_handler irq_handler = NULL;
6262
int stdio_uart_inited = 0;
6363
serial_t stdio_uart;
64-
int rxbuffer[2];
65-
int txbuffer[2];
64+
static int rxbuffer[2];
65+
static int txbuffer[2];
6666

6767
static void uart_callback(void *pCBParam, uint32_t Event, void *pArg)
6868
{

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/spi_api.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,28 @@ int spi_master_write(spi_t *obj, int value)
305305
*/
306306
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill)
307307
{
308-
return 0;
308+
ADI_SPI_TRANSCEIVER transceive;
309+
ADI_SPI_HANDLE SPI_Handle;
310+
ADI_SPI_RESULT SPI_Return = ADI_SPI_SUCCESS;
311+
312+
transceive.pReceiver = rx_buffer;
313+
transceive.ReceiverBytes = rx_length; /* link transceive data size to the remaining count */
314+
transceive.nRxIncrement = 1; /* auto increment buffer */
315+
transceive.pTransmitter = tx_buffer; /* initialize data attributes */
316+
transceive.TransmitterBytes = tx_length; /* link transceive data size to the remaining count */
317+
transceive.nTxIncrement = 1; /* auto increment buffer */
318+
319+
transceive.bDMA = false;
320+
transceive.bRD_CTL = false;
321+
SPI_Handle = *obj->pSPI_Handle;
322+
SPI_Return = adi_spi_MasterReadWrite(SPI_Handle, &transceive);
323+
if (SPI_Return) {
324+
obj->error = SPI_EVENT_ERROR;
325+
return -1;
326+
}
327+
else {
328+
return((int)tx_length);
329+
}
309330
}
310331

311332
#endif

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/trng_api.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,28 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
8888
ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
8989
bool bRNGRdy;
9090
uint32_t nRandomNum, i;
91+
ADI_RNG_RESULT result;
9192

9293
for (i = 0; i < length; i++) {
9394
// Loop until the device has data to be read
9495
do {
95-
adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
96+
result = adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
97+
if (result != ADI_RNG_SUCCESS)
98+
{
99+
return -1;
100+
}
96101
} while (!bRNGRdy);
97102

98103
// Read the RNG
99-
adi_rng_GetRngData(RNGhDevice, &nRandomNum);
104+
result = adi_rng_GetRngData(RNGhDevice, &nRandomNum);
105+
106+
if (result != ADI_RNG_SUCCESS)
107+
{
108+
return -1;
109+
}
100110

101111
// Save the output
102-
output[i] = (uint8_t)nRandomNum;
112+
output[i] = (uint8_t)(nRandomNum & 0xFF);
103113
}
104114

105115
*output_length = length;

0 commit comments

Comments
 (0)