1 /*
2 * Code generated from Atmel Start.
3 *
4 * This file will be overwritten when reconfiguring your Atmel Start project.
5 * Please copy examples or other code you want to keep to a separate file
6 * to avoid losing it when reconfiguring.
7 */
8
9 #include "driver_examples.h"
10 #include "driver_init.h"
11 #include "utils.h"
12
13 /**
14 * Example of using ADC_0 to generate waveform.
15 */
ADC_0_example(void)16 void ADC_0_example(void)
17 {
18 uint8_t buffer[2];
19
20 adc_sync_enable_channel(&ADC_0, 0);
21
22 while (1) {
23 adc_sync_read_channel(&ADC_0, 0, buffer, 2);
24 }
25 }
26
27 static uint8_t src_data[128];
28 static uint8_t chk_data[128];
29 /**
30 * Example of using FLASH_0 to read and write Flash main array.
31 */
FLASH_0_example(void)32 void FLASH_0_example(void)
33 {
34 uint32_t page_size;
35 uint16_t i;
36
37 /* Init source data */
38 page_size = flash_get_page_size(&FLASH_0);
39
40 for (i = 0; i < page_size; i++) {
41 src_data[i] = i;
42 }
43
44 /* Write data to flash */
45 flash_write(&FLASH_0, 0x3200, src_data, page_size);
46
47 /* Read data from flash */
48 flash_read(&FLASH_0, 0x3200, chk_data, page_size);
49 }
50
51 /**
52 * Example of using FLASH_0 to read and write Flash RWWEE array.
53 */
RWW_FLASH_0_example(void)54 void RWW_FLASH_0_example(void)
55 {
56 uint32_t page_size;
57 uint16_t i;
58
59 /* Init source data */
60 page_size = _rww_flash_get_page_size(&FLASH_0.dev);
61
62 for (i = 0; i < page_size; i++) {
63 src_data[i] = i;
64 }
65
66 /* Write data to RWWEE flash */
67 if (_rww_flash_write(&FLASH_0.dev, NVMCTRL_RWW_EEPROM_ADDR, src_data, page_size) != ERR_NONE) {
68 while (1)
69 ; /* Trap here when flash write error happen */
70 }
71
72 /* Read data from RWWEE flash */
73 if (_rww_flash_read(&FLASH_0.dev, NVMCTRL_RWW_EEPROM_ADDR, chk_data, page_size) != ERR_NONE) {
74 while (1)
75 ; /* Trap here when flash read error happen */
76 }
77
78 /* Check data */
79 for (i = 0; i < page_size; i++) {
80 if (src_data[i] != chk_data[i]) {
81 while (1)
82 ; /* Trap here when check error happen */
83 }
84 }
85 }
86
I2C_0_example(void)87 void I2C_0_example(void)
88 {
89 struct io_descriptor *I2C_0_io;
90
91 i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io);
92 i2c_m_sync_enable(&I2C_0);
93 i2c_m_sync_set_slaveaddr(&I2C_0, 0x12, I2C_M_SEVEN);
94 io_write(I2C_0_io, (uint8_t *)"Hello World!", 12);
95 }
96
97 /**
98 * Example of using TARGET_IO to write "Hello World" using the IO abstraction.
99 *
100 * Since the driver is asynchronous we need to use statically allocated memory for string
101 * because driver initiates transfer and then returns before the transmission is completed.
102 *
103 * Once transfer has been completed the tx_cb function will be called.
104 */
105
106 static uint8_t example_TARGET_IO[12] = "Hello World!";
107
tx_cb_TARGET_IO(const struct usart_async_descriptor * const io_descr)108 static void tx_cb_TARGET_IO(const struct usart_async_descriptor *const io_descr)
109 {
110 /* Transfer completed */
111 }
112
TARGET_IO_example(void)113 void TARGET_IO_example(void)
114 {
115 struct io_descriptor *io;
116
117 usart_async_register_callback(&TARGET_IO, USART_ASYNC_TXC_CB, tx_cb_TARGET_IO);
118 /*usart_async_register_callback(&TARGET_IO, USART_ASYNC_RXC_CB, rx_cb);
119 usart_async_register_callback(&TARGET_IO, USART_ASYNC_ERROR_CB, err_cb);*/
120 usart_async_get_io_descriptor(&TARGET_IO, &io);
121 usart_async_enable(&TARGET_IO);
122
123 io_write(io, example_TARGET_IO, 12);
124 }
125
CAN_0_tx_callback(struct can_async_descriptor * const descr)126 void CAN_0_tx_callback(struct can_async_descriptor *const descr)
127 {
128 (void)descr;
129 }
CAN_0_rx_callback(struct can_async_descriptor * const descr)130 void CAN_0_rx_callback(struct can_async_descriptor *const descr)
131 {
132 struct can_message msg;
133 uint8_t data[64];
134 msg.data = data;
135 can_async_read(descr, &msg);
136 return;
137 }
138
139 /**
140 * Example of using CAN_0 to Encrypt/Decrypt datas.
141 */
CAN_0_example(void)142 void CAN_0_example(void)
143 {
144 struct can_message msg;
145 struct can_filter filter;
146 uint8_t send_data[4];
147 send_data[0] = 0x00;
148 send_data[1] = 0x01;
149 send_data[2] = 0x02;
150 send_data[3] = 0x03;
151
152 msg.id = 0x45A;
153 msg.type = CAN_TYPE_DATA;
154 msg.data = send_data;
155 msg.len = 4;
156 msg.fmt = CAN_FMT_STDID;
157 can_async_register_callback(&CAN_0, CAN_ASYNC_TX_CB, (FUNC_PTR)CAN_0_tx_callback);
158 can_async_enable(&CAN_0);
159
160 /**
161 * CAN_0_tx_callback callback should be invoked after call
162 * can_async_write, and remote device should recieve message with ID=0x45A
163 */
164 can_async_write(&CAN_0, &msg);
165
166 msg.id = 0x100000A5;
167 msg.fmt = CAN_FMT_EXTID;
168 /**
169 * remote device should recieve message with ID=0x100000A5
170 */
171 can_async_write(&CAN_0, &msg);
172
173 /**
174 * CAN_0_rx_callback callback should be invoked after call
175 * can_async_set_filter and remote device send CAN Message with the same
176 * content as the filter.
177 */
178 can_async_register_callback(&CAN_0, CAN_ASYNC_RX_CB, (FUNC_PTR)CAN_0_rx_callback);
179 filter.id = 0x469;
180 filter.mask = 0;
181 can_async_set_filter(&CAN_0, 0, CAN_FMT_STDID, &filter);
182
183 filter.id = 0x10000096;
184 filter.mask = 0;
185 can_async_set_filter(&CAN_0, 1, CAN_FMT_EXTID, &filter);
186 }
187